C++#include<bits/stdc++.h>
using namespace std;
const int maxn=100100;
int heap[maxn];
int size=0;
int n;
long long ans=0;
int top1;
int top2;
int get(){
int pa,son,res;
res=heap[1];
heap[1]=0;
heap[1]=heap[size--];
pa=1;
while(pa*2<=size){
son=pa*2;
if(son<size&&heap[son+1]<heap[son]){
son++;
}
if(heap[pa]<=heap[son]){
break;
}
swap(heap[pa],heap[son]);
pa=son;
}
return res;
}
void put(int x){
int son,pa;
heap[++size]=x;
son=size;
while(son>1){
pa=son>>1;
if(heap[son]>=heap[pa]){
break;
}
swap(heap[son],heap[pa]);
son=pa;
}
return;
}
int main(){
cin>>n;
for(int i=1; i<=n; i++){
int a;
cin>>a;
put(a);
size++;
}
while(size>=2){
top1=get();
top2=get();
ans+=(top1+top2);
put(top1+top2);
}
cout<<ans<<endl;
return 0;
}