#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#define int long long
using namespace std;
typedef pair<int, int> PII;
int n;
int ans;
inline int read(){
int x = 0,f = 1;
char ch = getchar();
while(ch < '0'||ch > '9'){
if(ch == '-')
f = -1;
ch = getchar();
}
while(ch>='0'&&ch<='9'){
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
return x * f;
}
inline void write(int x){
if(x > 9) write(x / 10);
putchar(x % 10 + '0');
}
signed main() {
set<PII> s;
n = read();
for (int i = 0, a; i < n; i++) {
a = read();
s.insert(PII(a, i));
}
for (int i = 1; i < n; i++) {
int a = s.begin() -> first;
s.erase(s.begin());
int b = s.begin() -> first;
s.erase(s.begin());
ans += a + b;
s.insert(PII(a + b,n + i));
}
write(ans);
return 0;
}