知识点:贪心,霍夫曼编码,优先队列
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mk make_pair
#define sz(x) ((int) (x).size())
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pa;
int main() {
int n;
while (cin >> n && n) {
priority_queue<int, vi, greater<int> > q;
for (int i = 0; i < n; i++) { int x; cin >> x; q.push(x); }
ll ans = 0;
for (int i = 1; i < n; i++) {
int a = q.top(); q.pop();
int b = q.top(); q.pop();
q.push(a + b);
ans += a + b;
}
cout << ans << endl;
}
return 0;
}