为什么过的了poj,却过不了老汪出的数据~~
用优先队列,从小到大排序,每次取出最前面的两个,求和,和在放进优先队列中,直到队列仅有一项为止,结束操作
post code:
#include<stdio.h>
#include<iostream>
#include<queue>
using namespace std;
priority_queue< int,vector<int>,greater<int> > my;
int main()
{
int n,num;
while(scanf("%d",&n)!=EOF){
while(my.empty()==0){
my.pop();
}
for(int i=1;i<=n;i++){
scanf("%d",&num);
my.push(num);
}
if(n==1){
printf("0\n");
continue;
}
int a,b,he;
__int64 sum=0;
while(my.empty()==0){
a=my.top();
my.pop();
b=my.top();
my.pop();
sum=sum+a+b;
// printf("! %d\n",sum);
if(my.empty()==1)break;
my.push(a+b);
}
printf("%I64d\n",sum);
}
return 0;
}