Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
710 B
4
Indexable
Never
import java.util.Arrays;
import java.util.List;

public class EPI64 {
    public static void main(String[] args) {
        int[] arr = {3,4,4,1,5};
        Arrays.sort(arr);
        int total = arr[0] + arr[1];
        int i = arr.length - 1;
        while (i > 2) {
            if (arr[0] + arr[i - 1] > 2 * arr[1]) {
                total += arr[0] + arr[i] + 2 * arr[1];
            }
            else {
                total += arr[0] + arr[i] + arr[0] + arr[i - 1];
            }
            i -= 2;
        }
        if (i == 1) {
            total -= arr[0];
        }
        else if (i == 2) {
            total += arr[2];
        }
        System.out.println(total);
    }
}