标签:
#include <bits/stdc++.h> using namespace std; priority_queue<int, vector<int>, greater<int>> Q; int main() { int n, input, ans = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> input; Q.push(input); } while (!Q.empty()) { int a, b; a = Q.top(); Q.pop(); if (Q.empty()) break; b = Q.top(); Q.pop(); ans += a+b; Q.push(a+b); } cout << ans << endl; return 0; }
Reference:
[1] 百度百科:哈夫曼树.
标签:
原文地址:http://www.cnblogs.com/wubdut/p/5671812.html