#include <iostream>
#include <vector>
#include <queue>
using namespace std;

int n;

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