哈夫曼树的经典题
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>
#include <cstdio>
#include <cctype>
#include <unordered_map>
#include <map>
#include <cstring>
using namespace std;
const int N = 55;
int main(){
int n, x;
cin>>n;
priority_queue<int, vector<int>, greater<int>> pq;
for(int i = 0; i < n; i++){
cin>>x;
pq.push(x);
}
int res = 0;
while(pq.size() > 1){
int a = pq.top();
pq.pop();
int b = pq.top();
pq.pop();
res += a+b;
pq.push(a+b);
}
cout<<res<<endl;
return 0;
}