#include<bits/stdc++.h>
using namespace std;
priority_queue<int,vector<int>,greater<int> >q;
queue<int>v;
int n,x;
long long ans=0;
void read(int &x){
int f=1;
char s=getchar();
x=0;
while(s<'0'||s>'9'){
if(s=='-')f=-1;
s=getchar();
}
while(s>='0'&&s<='9'){
x=x*10+s-'0';
s=getchar();
}
x*=f;
}
int main(){
read(n);
while(n--){
read(x);
q.push(x);
}int a,b;
while(q.size()>1){
a=q.top();
q.pop();
b=q.top();
q.pop();
ans+=a+b;
q.push(a+b);
}
cout<<ans;
return 0;
}