Fence Repair POJ - 3253
这个合并果子版的题和哈夫曼树极为相似
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
priority_queue <long long> q;
long long x;
long long ans;
long long n;
int main(){
//while(scanf("%d",&x)!=EOF){
scanf("%d",&n);
for(long long i=1;i<=n;++i){
scanf("%d",&x);
q.push(-x);
}
for(long long i=1;i<n;++i)
{
x=-q.top();
q.pop();
x-=q.top();
q.pop();
ans+=x;
q.push(-x);
// cout<<ans<<endl;
}
cout<<ans<<endl;
return 0;
}