http://47.104.209.207/problem/old1070
问Huffman树的费用,直接用堆解决。
1 #include<iostream> 2 #include<queue> 3 4 using namespace std; 5 6 int main(){ 7 int n; 8 cin>>n; 9 priority_queue<int,vector<int>,greater<int>> q; 10 for(int i=0;i<n;i++){ 11 int t; 12 cin>>t; 13 q.push(t); 14 } 15 int res=0; 16 while(q.size()>=2){ 17 int a=q.top(); 18 q.pop(); 19 int b=q.top(); 20 q.pop(); 21 res+=a+b; 22 q.push(a+b); 23 } 24 cout<<res; 25 return 0; 26 }