#include <bits/stdc++.h>
using namespace std;
priority_queue<int> q;
int main() {
freopen("fruit.in", "r", stdin);
freopen("fruit.out", "w", stdout);
int n, ans = 0;
cin >> n;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
q.push(-x);
}
for (int i = 1; i <= n - 1; i++) {
int k = -1 * q.top();
q.pop();
int b = -1 * q.top();
q.pop();
ans += k + b;
q.push(-1 * (k + b));
}
cout << ans;
}