P1090 [NOIP 2004 提高组] 合并果子
标程
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
// 小根堆:随时能取出当前最小的两堆
priority_queue<int, vector<int>, greater<int>> heap;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
heap.push(a);
}
long long cost = 0;
while (heap.size() > 1) {
int a = heap.top();
heap.pop();
int b = heap.top();
heap.pop();
int merged = a + b; // 本次合并消耗的体力
cost += merged;
heap.push(merged);
}
cout << cost << "\n";
return 0;
}