import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
PriorityQueue<Long> q = new PriorityQueue<>(); // 优先队列自动排序
for (int i = 0; i < n; i++) {
q.add(sc.nextLong());
}
long total = 0;
while (q.size() >= 2) {
long sum = q.poll() + q.poll(); // 取出头部最小的两个数
total += sum;
q.add(sum); // 合并后放回队列
}
System.out.println(total);
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~