while True: try:#哈夫曼树思想 n = int(input()) num = list(map(int, input().split(" "))) sum = 0 num.sort(reverse=True) while len(num) > 1: num.sort(reverse=True) x = num.pop() + num.pop() sum += x num.append(x) print(sum) except: break