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

int main() {
    priority_queue<int,vector<int>,greater<int>> smallerH;
    int n;
    while(cin>>n){
        if(n==0) break;
        for(int i=0;i<n;i++){
            int w;
            cin>>w;
            smallerH.push(w);
        }
        int minw=0;
        while(smallerH.size()>1){
            int a=smallerH.top();
            smallerH.pop();
            int b=smallerH.top();
            smallerH.pop();
            int c=a+b;
            minw+=c;
            smallerH.push(c);
        }
        cout<<minw<<endl;
    } 
}