很简单的贪心,用优先队列维护一下就行了
AC代码
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e6+10;
int trie[MAXN][2];
const int mod = 998244353;
int main() {
int n, x;
cin >> n;
priority_queue<int,vector<int>,greater<int> > que;
for(int i = 0; i < n; i++) {
cin >> x;
que.push(x);
}
int ans = 0;
while(que.size()>1) {
int x = que.top(); que.pop();
int y = que.top(); que.pop();
ans += (y+x);
que.push(x+y);
}
cout << ans << endl;
return 0;
}