#include <bits/stdc++.h> using namespace std; int main() { int n,m; while(cin>>n) { if(!n) break; priority_queue<int,vector<int>,greater<int>> myQueue; while(n--){ cin>>m; myQueue.push(m); } int res=0; while(myQueue.size()>1){ int a=myQueue.top(); myQueue.pop(); int b=myQueue.top(); myQueue.pop(); res+=a+b; myQueue.push(a+b); } cout<<res<<endl; } return 0; }