#include <iostream>
#include <queue>
using namespace std;

int main(){
  priority_queue<int> pq;
  int ans;
  int n;
  while (cin >> n,n){
    pq = priority_queue<int>();
    ans = 0;
    for (int i = 0; i < n; ++i){
      int t;
      cin >> t;
      pq.push(-t);
    }
    while (pq.size() > 1){
      int a = pq.top();
      pq.pop();
      int b = pq.top();
      pq.pop();
      ans -= a+b;
      pq.push(a+b);
    }
    cout << ans << endl;
  }
}
