#include<iostream>
#include<queue>
using namespace std;
int main(){
int n;
cin >> n;
// 用一个优先队列
priority_queue <int, vector<int>, greater<int> > heap; //greater是从小到大,less是从大到小
while(n--){
int x;
cin >> x;
heap.push(x);
}
int ans = 0, sum = 0;
int a, b;
//因为一次性要pop两个
while(heap.size() > 1){
a = heap.top();
heap.pop();
b = heap.top();
heap.pop();
sum = a + b;
ans += sum;
heap.push(sum);
}
cout << ans << endl;
return 0;
}