#include <iostream>
#include <queue>
using namespace std;
int main()
{
int n;
priority_queue<int,vector<int>,greater<int>> heap;
cin>>n;
for(int i=0;i<n;i++){
int x;
cin>>x;;
heap.push(x);
}
int res=0;
for(int i=0;i<n-1;i++){
int a=heap.top();heap.pop();
int b=heap.top();heap.pop();
res+=a+b;
heap.push(a+b);
}
cout<<res;
//cout << "Hello world!" << endl;
return 0;
}