//和1045思路和代码完全一样,不再赘述
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
int n;
struct compare{
bool operator()(int a, int b){
return a > b;
}
};
int main(){
while(cin >> n){
priority_queue<int, vector<int>, compare> queue;
for(int i = 0; i < n; i++){
int num;
cin >> num;
queue.push(num);
}
int coast = 0;
while(queue.size() != 1){
int num_1 = queue.top();
queue.pop();
int num_2 = queue.top();
queue.pop();
coast += (num_1 + num_2);
queue.push(num_1 + num_2);
}
cout << coast << endl;
}
return 0;
}
//presented by 大吉大利,今晚AC