//本题仅仅只是哈夫曼树的变形
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
std::vector<int> v;



int main() {
    int n;
    while (cin >> n && n) {
        v.clear();
        int ans = 0;
        int input;
        for (int i = 0; i < n; i++) {
            cin >> input;
            v.push_back(input);
        }
        sort(v.begin(), v.end());
        int tep = 0;
        for (int i = 0; i < n - 1; i++) {
            vector<int>::iterator it = v.begin();
            tep= (*it) + (*(it + 1));
            ans += tep;
            // v.pop_back();这是vectot数组的尾删
            v.erase(v.begin());
            v.erase(v.begin());
            v.push_back(tep);
            sort(v.begin(), v.end());


        }
        cout << ans;





    }
}