while gets
  n=$_.to_i
  if n==0
    puts 0
    next
  end
  ts = gets.split.map(&:to_i)
  ts.sort!
  ps = (0..n-1).inject([0]){|seq,i| seq<<(seq[i]+ts[i])}
  total = ps.inject{|x,y| x+y}
  puts total
end