#include <bits/stdc++.h>
using namespace std;
int n;
int main(){
// Huffman树问题:贪心策略, 每次合并最小的两个点值
priority_queue<int, vector<int>, greater<int>> pq;
cin>>n;
while(n--){
int x;
cin>>x;
pq.push(x);
}
int res=0;
while(pq.size()>1){
int a=pq.top(); pq.pop();
int b=pq.top(); pq.pop();
res+=a+b;
pq.push(a+b);
}
cout<<res<<endl;
return 0;
}