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 }

 

转载于:https://www.cnblogs.com/z-712/p/7324467.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值