#include<bits/stdc++.h>
using namespace std;
const int N=1e9;
priority_queue<long long,vector<long long>,greater<long long> > a;
int ans;
int main(){
int n;
cin>>n;
for(int i=0;i<n;i++){
int tmp;
cin>>tmp;
a.push(tmp);
}
while(a.size()>=2){
int tmpa,tmpb;
tmpa=a.top();
a.pop();
tmpb=a.top();
a.pop();
ans+=tmpa+tmpb;
a.push(tmpa+tmpb);
}
cout<<ans<<endl;
return 0;
}
STL秒了