考点:哈夫曼树
#include<bits/stdc++.h>
using namespace std;
const int MAXSIZE=1001;
int main() {
int n;
while(cin>>n) {
if(n==0) {
break;
}
priority_queue<int,vector<int>,greater<int> > q;
int x;
for(int i=0; i<n; i++) {
cin>>x;
q.push(x);
}
int ans=0;
while(q.size()>1) {
int a=q.top();
q.pop();
int b=q.top();
q.pop();
q.push(a+b);
ans+=a+b;
}
cout<<ans<<endl;
}
return 0;
}