您的位置:首页 > 其它

UVA-10954 Add All

2017-10-11 20:42 204 查看
题目链接:https://vjudge.net/problem/UVA-10954

贪心法,每次选最小的两个相加就可以了

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
typedef long long ll;
const int N=5000+10;
int n;
int main()
{
//freopen("/home/zlwang/C++/t2.txt","r",stdin);
while(~scanf("%d",&n)&&n)
{
int a;
priority_queue<int> q;
for(int i=0;i<n;i++)
{
scanf("%d",&a);
q.push(-a);
}
ll ans=0;
for(int i=0;i<n-1;i++)
{
int a=-q.top();q.pop();
int b=-q.top();q.pop();
ans+=a+b;
q.push(-a-b);
}
printf("%lld\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: