时/空限制:1s / 64MB
题解 :
- 注意是pq数量为1就停下,不是为0
- 每次取两个,然后放入和
- greater表示小顶堆
#include <iostream>
#include <queue>
using namespace std;
int main() {
int n;
cin >> n;
priority_queue<int, vector<int>, greater<int>> pq;
while (n -- ) {
int x;
cin >> x;
pq.push(x);
}
int ans = 0;
while (pq.size() > 1) {
int t1 = pq.top(); pq.pop();
int t2 = pq.top(); pq.pop();
int now = t1 + t2;
ans += now;
pq.push(now);
}
cout << ans;
}