#include <bits/stdc++.h>

using namespace std;

int main(){
	int n, num, small1, small2;
	
	
	while(cin>>n){
		priority_queue<int, vector<int>, greater<int>> pq;
		size_t sum = 0;
		if(n==0) break;
		
		for(int i=0; i<n; i++){
			cin>>num;
			pq.push(num);
		}
		for (int i = 0; i < n-1; i++){
			small1=pq.top();	pq.pop();
			small2=pq.top();	pq.pop();
			pq.push(small1+small2);
			sum += small1 + small2;
		}
		cout<<sum<<"\n"; 
	}
	
}	

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: