解法和合并果子是一样的, 每次取最小的两个, 更新答案, 加入队列
#include<cstdio>
#include<queue>
#define REP(i, a, b) for(int i = (a); i < (b); i++)
using namespace std;
int main()
{
int n, x;
while(~scanf("%d", &n) && n)
{
priority_queue<int, vector<int>, greater<int> > q;
REP(i, 0, n) scanf("%d", &x), q.push(x);
int ans = 0;
REP(i, 0, n - 1)
{
int a = q.top(); q.pop();
int b = q.top(); q.pop();
ans += a + b;
q.push(a+b);
}
printf("%d
", ans);
}
return 0;
}