#include <bits/stdc++.h>
using namespace std;
int n,x,ans;
priority_queue<int,vector<int>,greater<int> >q;
int main(){
freopen("fruit.in","r",stdin) ;
freopen("fruit.out","w",stdout);
scanf("%d",&n) ;
for(int i = 1;i <= n;i++){
scanf("%d",&x);q.push(x);
}
while(q.size() > 1){
int a = q.top();q.pop();
int b = q.top();q.pop();
ans += a + b;
q.push(a+b);
}
printf("%d\n",ans);
return 0;
}