Fence Repair POJ - 3253

这个合并果子版的题和哈夫曼树极为相似

Miku


#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;
}
posted @ 2020-10-02 23:16  Simex  阅读(100)  评论(0编辑  收藏  举报