题解 | #[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;
}
全部评论

相关推荐

评论
1
收藏
分享

创作者周榜

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