首页 > 代码库 > poj3253

poj3253

#include<iostream>
#include<queue>
using namespace std;
#define read(x) scanf("%lld",&x)
int main()
{
	priority_queue<long long,vector<long long>,greater<long long> >q;
	long long n,temp,sum;
	read(n);
	if(n==1)
	{
		read(temp);
		printf("%lld\n",temp);
		return 0;
	}
	while(n--)
	{
		read(temp);
		q.push(temp);
	}
	sum=0;
	while(q.size()>1)
	{
		temp=q.top();
		q.pop();
		temp+=q.top();
		q.pop();
		sum+=temp;
		q.push(temp);
	}
	printf("%lld\n",sum);
}
小顶堆实现哈夫曼树