import Control.Monad
import Data.List
main = do
    n <- readLn
    if n == 0 then return ()
              else do
                  mins <- replicateM n $ fmap read getLine
                  print.minimum.map totalWaitTime.permutations $ mins
                  main
totalWaitTime = sum.map sum.inits.init