P1090 合并果子

点击查看代码
#include<bits/stdc++.h>
using namespace std;

int n,ans;
priority_queue<int,vector<int>,greater<int>> pq;

int main()
{
    cin>>n;
    for(int i=0;i<n;i++){
        int x;
        cin>>x;
        pq.push(x);
    }
    while(pq.size()>1){
        int a=pq.top();pq.pop();
        int b=pq.top();pq.pop();
        ans+=a+b;
        pq.push(a+b);
    }

    cout<<ans<<endl;

    return 0;
}
OK,依旧是一道堆的练习题。stl,取出最大值,移除最大值,真是方便呢
posted @ 2025-12-02 16:40  gosaky  阅读(0)  评论(0)    收藏  举报