#include <bits/stdc++.h> using namespace std; int main(){ int n; while (cin >> n){ if (n == 0){ break; } priority_queue<int, vector<int>, greater<int>> heap; for (int i=0; i<n; i++){ int a; cin >> a; heap.push(a); } int res = 0; while (heap.size() > 1){ auto a = heap.top(); heap.pop(); auto b = heap.top(); heap.pop(); auto c = a + b; res += c; heap.push(c); } printf("%d\n", res); } return 0; }