Files
python/TangDou/AcWing/Huffman/148.cpp

27 lines
478 B
C++
Raw Normal View History

2025-08-30 18:35:01 +08:00
#include <bits/stdc++.h>
using namespace std;
// 升序队列,小顶堆
priority_queue<int, vector<int>, greater<int>> q;
int res;
int main() {
int n;
cin >> n;
while (n--) {
int x;
cin >> x;
q.push(x);
}
while (q.size() > 1) {
int a = q.top();
q.pop();
int b = q.top();
q.pop();
res += a + b;
q.push(a + b);
}
cout << res << endl;
return 0;
}