题解 | #搬水果#

搬水果

https://www.nowcoder.com/practice/e4c775b0f3ee42a4bb72c26d2e1eef8a

//1+2 3+9 12
#include <iostream>
#include <queue>
using namespace std;

int main() {
    int n;
    while (cin >>n) { 
        if(n==0)break;
        priority_queue<int,vector<int>,greater<>> q;
        for(int i=1;i<=n;i++)
        {
            int t;cin>>t;q.push(t);
        }
        int answer=0;
        while(q.size()>1)
        {
            int a,b;
            a=q.top();q.pop();
            b=q.top();q.pop();
            int c=a+b;
            answer+=c;
            q.push(c);
        }
        cout<<answer<<endl;
    }
}

全部评论

相关推荐

强大的马里奥:不太可能,我校计算机硕士就业率99%
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-08 17:10
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务