昨天是我入队的第一场个人赛,第一道最短路很快就A了,然后就跟榜跟F题,以为是一道简单的贪心,wa了两次心态就崩了,然后去做别的题了,F题到最后几乎所有小伙伴都a了,orz。后来才知道这道Huffman编码入队早的都做四五遍了 =_=
代码:
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
int main() {
// freopen("input.txt", "r", stdin);
int n;
while(~scanf("%d", &n)) {
priority_queue<int, vector<int>, greater<int> > qu;
while(n--) {
int t;
scanf("%d", &t);
qu.push(t);
}
if (qu.size() == 1) {
printf("%d", qu.top());
}
else {
long long ans = 0;
while(qu.size() > 1) {
int a = qu.top();
qu.pop();
int b = qu.top();
ans += (a + b);
qu.pop();
qu.push(a+b);
}
printf("%lld\n", ans);
}
}
return 0;
}