最小堆问题,直接调用priority_queue.
#include <iostream>
#include <queue>
#include <cstdio>
using namespace std;
typedef long long LL;
int main()
{
priority_queue<LL, vector<LL>, greater<LL> > q;
LL n,m;
while(~scanf("%I64d",&n)){
for(int i = 0; i < n; ++i){
scanf("%I64d",&m);
q.push(m);
}
LL sum = 0;
for(int i = 1; i < n; ++i){
int a = q.top();
q.pop();
int b = q.top();
q.pop();
sum += (a + b);
q.push(a+b);
}
printf("%I64d\n",sum);
q.pop();
}
return 0;
}