#include<bits/stdc++.h>
using namespace std;
//FILE *fin, *fout;
void read(long &x) {
int f = 1;
x = 0;
char s = getchar();
while (s < '0' || s > '9') {
if (s == '-')f = -1;
s = getchar();
}
while (s >= '0' && s <= '9') {
x = x * 10 + s - '0';
s = getchar();
}
x *= f;
}
//inline void write(int x) {
// if (x < 0) return fputc('-', fout), write(-x);
// if (x > 9) write(x / 10);
// fputc((x % 10) | 48, fout);
//}
const int maxn = 1000100;
long n, x;
long long ans = 0;
priority_queue<int, vector<int>, greater<int>>p; //小
//queue <int> p;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
read(n);
for (int i = 0; i < n; i++) {
read(x);
p.push(x);
}
while (p.size() != 1) {
int x = p.top();
p.pop();
int y = p.top();
p.pop();
ans += (x + y);
p.push(x + y);
}
// write(ans);
cout<<ans;
return 0;
}//TLE 就开O2