#include <stdio.h>
#include <queue>
using namespace std;
priority_queue<int, vector<int>, greater<int> > Q;
int main() {
int n;
while (scanf("%d", &n) != EOF)
{
while (Q.empty() == false)
Q.pop();
for (int i = 1; i <= n; i++)
{
int x;
scanf("%d", &x);
Q.push(x);
}
int ans = 0;
while (Q.size() > 1)
{
int a = Q.top();
Q.pop();
int b = Q.top();
Q.pop();
ans += a + b;
Q.push(a + b);
}
printf("%d\n", ans);
}
return 0;
}
/**************************************************************
Problem: 1106
User: mykelia
Language: C++
Result: Accepted
Time:0 ms
Memory:932 kb
****************************************************************/
北邮机试 哈夫曼树
最新推荐文章于 2023-12-18 21:25:12 发布