题目:https://cn.vjudge.net/contest/178387#problem/G
可以利用优先队列,每次取最小的两个值。
#include <iostream>
#include <queue>
using namespace std;
int main() {
priority_queue<int, vector<int>, greater<int> > q;
int n, tmp;
while (cin >> n) {
for (int i = 0; i<n; i++) {
cin >> tmp;
q.push(tmp);
}
int ans = 0;
while (!q.empty()) {
int x = q.top();
q.pop();
if (q.empty()) break;
int y = q.top();
q.pop();
q.push(x + y);
ans += (x + y);
}
cout << ans << endl;
}
return 0;
}