POJ - 3253 Fence Repair

https://vjudge.net/problem/POJ-3253

和合并果子的题一样

#include<cstdio> 
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
int main()
{
	int n,x;
	long long ans;
	while(~scanf("%d",&n))
	{
		priority_queue<int,vector<int>,greater<int> > q;
		for(int i=0;i<n;i++)
		{
			scanf("%d",&x);
			q.push(x);
		}
		int a,b;
		ans=0;
		while(q.size()>=2)
		{
			a=q.top();
			q.pop();
			b=q.top();
			q.pop();
			ans+=a+b;
			q.push(a+b);
		}
		printf("%lld\n",ans);
	}
	return 0;
}
posted @ 2021-07-27 10:34  斯文~  阅读(16)  评论(0)    收藏  举报

你好!