#include<bits/stdc++.h> usingnamespace std; priority_queue<int, vector<int>, greater<int> > q; intmain(){ int n; while (cin >> n) { while (!q.empty()) { q.pop(); } for (int i = 0; i < n; ++i) { int x; cin >> x; q.push(x); } int ans = 0; while (q.size() > 1) { int a = q.top(); q.pop(); int b = q.top(); q.pop(); ans += a + b; q.push(a + b); } cout << ans << endl; } return0; }