http://poj.org/problem?id=3253
#include<iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
using namespace std;
int main()
{
//freopen("data.txt","r",stdin);
//freopen("A-large-practice.out","w",stdout);
priority_queue<long long,vector<long long>,greater<long long> >str;
long long ans;
int n,i;
while(scanf("%d",&n)!=EOF)
{
long long temp1,temp2,temp3;
for(i=0;i<n;i++)
{
cin>>temp1;
str.push(temp1);
}
ans=0;
temp2=0;
temp3=0;
do
{
ans+=temp2;
ans+=temp3;
temp2=str.top();
str.pop();
if(str.empty())
break;
temp3=str.top();
str.pop();
str.push(temp2+temp3);
}while(!str.empty());
cout<<ans<<endl;
}
return 0;
}