import std.stdio, std.string, std.conv, std.range, std.algorithm, std.array;

void main()
{
    while(true)
    {
        int num = readln.chomp.to!int;
        if(!num)break;
        int[3][10_000] dp;
        foreach(i, time; num.iota.map!(a => readln.chomp.to!int).array.sort)
        {
            dp[i][0] = time;
            if(i)
            {
                dp[i][1] = dp[i - 1][0] + dp[i - 1][1];
                dp[i][2] = dp[i - 1][2] + dp[i][1];
            }
        }
        dp[num - 1][2].writeln;
    }
}