每次合并最轻的两堆

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;
}