import java.util.*;
public class Main
{
public static void main
(String[] args
) { int arr[] = { 4, 6, 8 };
System.
out.
println(solve
(arr
)); }
private static int solve(int[] arr){
PriorityQueue<Integer> pq = new PriorityQueue<>();
for(int a : arr)
pq.add(a);
int cost = 0;
while(pq.size() >= 2){
int x = pq.remove();
int y = pq.remove();
cost += x+y;
pq.add(x+y);
}
return cost;
}
}
aW1wb3J0IGphdmEudXRpbC4qOwpwdWJsaWMgY2xhc3MgTWFpbgp7CiAgICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKSB7CiAgICAgICAgaW50IGFycltdID0geyA0LCA2LCA4IH07CiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKHNvbHZlKGFycikpOwogICAgfQogICAgCiAgICBwcml2YXRlIHN0YXRpYyBpbnQgc29sdmUoaW50W10gYXJyKXsKICAgICAgICBQcmlvcml0eVF1ZXVlPEludGVnZXI+IHBxID0gbmV3IFByaW9yaXR5UXVldWU8PigpOwogICAgICAgIAogICAgICAgIGZvcihpbnQgYSA6IGFycikKICAgICAgICAgICAgcHEuYWRkKGEpOwogICAgICAgIGludCBjb3N0ID0gMDsKICAgICAgICB3aGlsZShwcS5zaXplKCkgPj0gMil7CiAgICAgICAgICAgIGludCB4ID0gcHEucmVtb3ZlKCk7CiAgICAgICAgICAgIGludCB5ID0gcHEucmVtb3ZlKCk7CiAgICAgICAgICAgIGNvc3QgKz0geCt5OwogICAgICAgICAgICBwcS5hZGQoeCt5KTsKICAgICAgICB9CiAgICAgICAgCiAgICAgICAgcmV0dXJuIGNvc3Q7CiAgICB9Cn0=