题解 | #哈夫曼树#
哈夫曼树
https://www.nowcoder.com/practice/162753046d5f47c7aac01a5b2fcda155
#include <iostream> #include <queue> #include <vector> using namespace std; int main() { int n; while(cin >> n){ priority_queue<int, vector<int>, greater<int>> q; while (n --) { int x; cin >> x; q.push(x); } int sum = 0;//记录带权路径和 while(q.size() > 1){ int a = q.top(); q.pop(); int b = q.top(); q.pop(); q.push(a + b); sum += (a + b); } cout << sum << endl; } return 0; } // 64 位输出请用 printf("%lld")