首页 > 代码库 > UVA - 10954 A - Add All

UVA - 10954 A - Add All

https://odzkskevi.qnssl.com/cd5694d5c85f39f2036f7473917563e5?v=1502195814

 

 

 1 #include <stdio.h>
 2 #include <queue>
 3 using namespace std;
 4 struct cmp{   ////建立越小的数优先级越大的队列
 5     bool operator()(const int &a,const int &b){
 6     return a>b;
 7     }
 8 };
 9 priority_queue<int ,vector<int>,cmp>q;
10 //priority_queue<int ,vector<int>,greater<int> >q;
11 //或者,这样定义,可以省略前面的struct
12 int main(){
13     int n,i,x,y;
14     while(scanf("%d",&n),n){
15         for(i=1;i<=n;i++){
16             scanf("%d",&x);
17             q.push(x);
18         }
19         x=0;
20         /*
21         for(int i=1;i<n;i++){
22             int a=q.top();q.pop();
23             int b=q.top();q.pop();
24             x=x+a+b;
25             q.push(x);
26             }
27             cout<<x<<endl;
28         */
29         while(q.size()>1){
30             y=q.top();
31             q.pop();
32             y+=q.top();
33             q.pop();
34             x+=y;
35             q.push(y);
36         }
37         q.pop();
38         printf("%d\n",x);//y,6
39         }
40 }

 

UVA - 10954 A - Add All