洛谷 P1090合并果子题解--zhengjun
思路
本蒟蒻表示:我只会用优先级队列做。
贪心:每一次只要取出最少的两堆合并就可以了。
代码
#include<bits/stdc++.h>
using namespace std;
int n;
priority_queue<long long,vector<long long>,greater<long long> > q;
long long ans;
int main(){
long long x,y;
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%lld",&x),q.push(x);
for(int i=1;i<n;i++){
x=q.top();q.pop();
y=q.top();q.pop();
ans+=x+y;
q.push(x+y);
}
printf("%lld",ans);
return 0;
}