题解 | #[NOIP2004]合并果子#

[NOIP2004]合并果子

https://ac.nowcoder.com/acm/problem/16663

每次合并最轻的两堆

Code

#include <bits/stdc++.h>

using namespace std;

const int N = 10010;

typedef long long ll;

priority_queue<int,vector<int>,greater<int> >q;
int a[N];
int n;

int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        int x;
        cin>>x;
        q.push(x);
    }

    ll res=0;
    while(q.size()>=2){
        auto t1=q.top(); q.pop();
        auto t2=q.top(); q.pop();
        res+=t1+t2;
        q.push(t1+t2);
    }
    cout<<res<<endl;

    return 0;
}
全部评论

相关推荐

我的offer呢😡:这不才9月吗,26到明年毕业前能一直找啊,能拿下提前批,转正的,offer打牌的都是有两把刷子的,为什么非要跟他们比。如果别人是9本硕+金牌+好几段大厂实习呢?如果别人是双非通天代呢?如果别人是速通哥呢?,做好自己就行了,我们做不到他们一样提前杀死比赛,但晚点到终点也没啥关系吧
双非应该如何逆袭?
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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