C++
#include <iostream>
#include <queue>
using namespace std;
int main() {
int n, w;
cin >> n;
priority_queue<int, vector<int>, greater<int> > pq;
while (n--) {
cin >> w;
pq.push(w);
}
int cnt = 0;
while (pq.size() > 1) {
int x = pq.top(); pq.pop();
int y = pq.top(); pq.pop();
cnt += x + y;
pq.push(x+y);
}
cout << cnt << endl;
return 0;
}