//1+2 3+9 12 #include <iostream> #include <queue> using namespace std; int main() { int n; while (cin >>n) { if(n==0)break; priority_queue<int,vector<int>,greater<>> q; for(int i=1;i<=n;i++) { int t;cin>>t;q.push(t); } int answer=0; while(q.size()>1) { int a,b; a=q.top();q.pop(); b=q.top();q.pop(); int c=a+b; answer+=c; q.push(c); } cout<<answer<<endl; } }