A - 树-堆结构练习——合并果子之哈夫曼树
http://acm.sdut.edu.cn:8080/vjudge/contest/view.action?cid=203#problem/A
#include <stdio.h> #include <queue> #include <vector> #include <string.h> #include<functional> using namespace std; int a[10001],n; struct cmp { bool operator()(int &a,int &b) { return a>b; } }; int main() { int i; int s1,s2; long long ans = 0; priority_queue<int>q; priority_queue<int,vector<int>,cmp>que; scanf("%d",&n); for(i = 0;i<n;i++) { scanf("%d",&a[i]); que.push(a[i]); } while(!que.empty()) { s1 = que.top(); que.pop(); if(que.empty()) break; s2 = que.top(); que.pop(); ans+=s1 + s2; que.push(s1 + s2); } printf("%lld\n",ans); return 0; }