1629 合并石头问题 优先队列 最小堆
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1e3+10,inf = 0x3f3f3f3f; priority_queue<int,vector<int>,greater<int> >q; int n,ans,x; int main() { cin >> n; for(int i = 1;i <= n;i++) { int x; cin >> x; q.push(x); } while(q.size() > 1) { int a = q.top(); q.pop(); int b = q.top(); q.pop(); ans += a + b; q.push(a + b); } cout << ans; return 0; }

浙公网安备 33010602011771号