#include <deque>
#include <iostream>
#include <algorithm>
using namespace std;
int binary_search(deque<int> nums, int t, int hi, int lo)
{
if (hi == lo)
{
return hi;
}
int m = (hi + lo) / 2;
if (nums[m] <= t)
{
return binary_search(nums, t, hi, m + 1);
}
else
{
return binary_search(nums, t, m, lo);
}
}
int main()
{
int n;
while (cin >> n)
{
if (n == 0)
{
break;
}
deque<int> nums;
long long int cost = 0;
for (int i = 0, tmp = 0; i < n; i++)
{
cin >> tmp;
nums.push_back(tmp);
}
sort(nums.begin(), nums.begin() + n);
for (int i = 0, tmp; i < n - 1; i++)
{
tmp = nums[0] + nums[1];
cost += tmp;
nums.pop_front();
nums.pop_front();
deque<int>::iterator it;
int k = binary_search(nums, tmp, nums.size(), 0);
it = nums.begin() + k;
it = nums.insert(it, tmp);
}
printf("%d\n", cost);
}
return 0;
}