#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
int main(){
int n;
cin >> n;
priority_queue<int,vector<int>,greater<int>> heap; //建立小根堆
while(n--){
int x;
cin >> x;
heap.push(x);
}
int res = 0;
while(heap.size()>1){ //取出最小的两个数
auto a = heap.top(); heap.pop();
auto b = heap.top(); heap.pop();
res += a + b;
heap.push(a+b);
}
cout << res;
return 0;
}