C++ 代码
#include <iostream>
#include <queue>
using namespace std;
int main()
{
int n;
cin >> n;
priority_queue<int, vector<int>, greater<int>> pq;
while(n --) {
int a;
scanf("%d", &a);
pq.push(a);
}
int res = 0;
while(pq.size() > 1){
int a = pq.top(); pq.pop();
int b = pq.top(); pq.pop();
res += a + b;
pq.push(a + b);
}
printf("%d\n", res);
return 0;
}