1.要考虑到规模为20,000 累加起来肯定会超的,要用long long
2. 思想就是先从正着推,一定是先切掉最长的那块,这样之后都不会受影响;再反着来想,就是先合并最小的
//#define LOCAL
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define N 20000
using namespace std;
int main(void){
#ifdef LOCAL
freopen("data.in", "r", stdin);
#endif
int n;
long long temp;
priority_queue<long long, vector<long long >, greater<long long> > q;
scanf("%d", &n);
for(int i = 0; i < n; i++){
scanf("%lld", &temp);
q.push(temp);
}
if(q.size() == 1)
printf("%lld\n", q.top());
else{
long long ans = 0;
while(q.size() > 1){
int x = q.top();
q.pop();
int y = q.top();
q.pop();
//printf("x:%d y:%d\n", x, y);
int xy = x + y;
ans += xy;
q.push(xy);
}
printf("%lld\n", ans);
}
return 0;
}