#include <bits/stdc++.h> using namespace std; int main() { int n;//水果种类数 int x; while (scanf("%d", &n) != EOF) { if (n == 0) { break; } priority_queue<int, vector<int>, greater<int>> fruit;//建立小根堆 for (int i = 0; i < n; ++i) { scanf("%d", &x); fruit.push(x); } long long wpl = 0; while (fruit.size() != 1) { int a = fruit.top(); fruit.pop(); int b = fruit.top(); fruit.pop(); wpl = wpl + a + b; fruit.push(a+b); } printf("%d", wpl); } return 0; }