AcWing 3531. 哈夫曼树
原题链接
简单
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int N = 1e3 + 10;
int n, t,sum;
priority_queue<int,vector<int>,greater<int> > pq;
void solve() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> t;
pq.push(t);
}
while (pq.size() >= 2) {
int n1 = pq.top();
pq.pop();
int n2 = pq.top();
pq.pop();
pq.push(n1 + n2);
sum += n1+n2;
}
cout << sum;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
solve();
return 0;
}