难度:3
霍夫曼编码,优先队列的简单应用
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pa;
int main() {
int n;
while (cin >> n && n) {
priority_queue<int, vi, greater<int> > q;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
q.push(x);
}
ll ans = 0;
while (q.size() > 1) {
int a = q.top(); q.pop();
int b = q.top(); q.pop();
q.push(a + b);
ans += (ll) (a + b);
}
cout << ans << endl;
}
return 0;
}