#include <iostream> #include <queue> #include <unordered_map> using namespace std; int main() { int n; while (cin >> n) { // 注意 while 处理多个 case if(!n)break; priority_queue<int,vector<int>, greater<int>> heap; while(n--){ int a; cin>>a; heap.push(a); } int ans=0; while(heap.size()){ if(heap.size()==1){ break; } int a = heap.top(); heap.pop(); int b = heap.top(); heap.pop(); ans+=a+b; heap.push(a+b); } cout<<ans<<endl; } } // 64 位输出请用 printf("%lld")