#include <cstdio> #include <iostream> #include <string> #include <queue> using namespace std; int main(){ int n; while(scanf("%d",&n)!=EOF){ if(n==0){ break; } priority_queue<int,vector<int>,greater<int>> myPriorityQueue; while(n--){ int x; scanf("%d",&x); myPriorityQueue.push(x); } int answer=0; while(1<myPriorityQueue.size()){ int a,b; a=myPriorityQueue.top(); myPriorityQueue.pop(); b=myPriorityQueue.top(); myPriorityQueue.pop(); answer=answer+a+b; myPriorityQueue.push(a+b); } printf("%d\n",answer); } return 0; }