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