#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main(){
int n,ans=0;
priority_queue<int,vector<int>,greater<int>> q;
cin >> n;
for (int i=0;i<n;++i){
int t;
cin >> t;
q.push(t);
}
while (q.size()>1){
int a=q.top();
q.pop();
int b=q.top();
q.pop();
ans+=a+b;
q.push(a+b);
}
cout << ans << endl;
return 0;
}