#include <iostream>
#include <queue>
#include <vector>
using namespace std;
//哈夫曼树应用

int main() {
    int n;
    while (cin >> n && n != 0) {
        priority_queue<int, vector<int>, greater<int>> q;
        while(n --){
            int x;
            cin >> x;
            q.push(x);
        }
        int sum = 0;
        while(q.size() > 1){
            int a = q.top(); q.pop();
            int b = q.top(); q.pop();
            q.push(a + b);
            sum += (a + b);
        }

        cout << sum << endl;
    }

    return 0;
}
// 64 位输出请用 printf("%lld")