#include <bits/stdc++.h>
using namespace std;
int n;
int a[10010];
priority_queue<int, vector<int>, greater<int>>q;
//堆顶元素 q.top();
int main(){
cin >> n;
for(int i = 1; i <= n; i ++ ){
cin >> a[i];
q.push(a[i]);
}
int ans = 0;
while(q.size() > 1){
int x = q.top();
q.pop();
int y = q.top();
q.pop();
ans += x + y;
q.push(x + y);
}
cout << ans << endl;
return 0;
}