#include<iostream>
#include<queue>
using namespace std;
const int N = 1e4 + 10;
priority_queue<int, vector<int>, greater<int>> h;
int main()
{
int n;
cin >> n;
for(int i = 0; i < n; i++)
{
int a;
cin >> a;
h.push(a);
}
int res = 0;
while(h.size() > 1)
{
int a = h.top();h.pop();
int b = h.top();h.pop();
res += a + b;
h.push(a + b);
}
cout << res << endl;
return 0;
}