# poj 3253 Huffman树（最优二叉树）

Huffman树又叫做最优二叉树，表示带权路径长度最短的树，对于每一个叶子节点来说，代价为点权*路径深度，解决问题的思路为将最小的两个节点成树，然后插回原队列，重复这一过程。
Huffman编码就是利用了Huffman树的过程，将频率当做点权，而树的结构本身就保证了没有公共前缀。

Problem:

Solution:

note:

#include<cstdio>
#include<iostream>
#include<sstream>
#include<cstdlib>
#include<cmath>
#include<cctype>
#include<string>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<ctime>
#include<vector>
#include<fstream>
#include<list>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define ms(s) memset(s,0,sizeof(s))

const double PI = 3.141592653589;
const int INF = 0x3fffffff;

priority_queue<ll, vector<ll>, greater<ll> > q;

int main() {
//        freopen("/Users/really/Documents/code/input","r",stdin);
//    freopen("/Users/really/Documents/code/output","w",stdout);
ios::sync_with_stdio(false);

int n;
ll t;
cin >> n;
ll tot;
ll ans = 0;
while(n--) {
cin >> t;
q.push(t);
}
while(q.size() > 1) {
tot = q.top();  q.pop();
tot += q.top(); q.pop();
ans += tot;
q.push(tot);
}
cout << ans << endl;

return 0;
}