Problem:


ミルクショップ

 鈴木さんは会津地域に新しく搾りたてミルクの移動販売のお店を開きました。その日買い求めに来るお客さんは全員持ち帰るためのボトルを持って既にお店に並んでいて、それ以上増えないものとします。お客さんはそれぞれ1回だけしか注文しません。タンクの蛇口が一つしかないので、一人ずつ順番に販売しなければなりません。そこで、鈴木さんはなるべく並んでいるお客さんの待ち時間を少なくしたいと考えています。

 お客さんの人数とお客さんが牛乳を注ぎきるのに要する時間が入力として与えられます。あなたはお客さんの「一人一人の待ち時間の合計」(以下「待ち時間の合計」とする)を最小にするための注文の順序を鈴木さんに代わって調べ、そのときの「待ち時間の合計」を出力して終了するプログラムを作成してください。ただし、お客さんは 10,000 人以下で 1 人あたりに要する時間は 60 分以下とします。

 例えば、お客さんの人数が 5 人で、各お客さんが要する時間が順に 2,6,4,3,9 分の場合、そのままの順序だと「待ち時間の合計」は 37 分になります。次の例では、最初の列の順の 2 人目と 3 人目を入れ替えています。この場合、「待ち時間の合計」は 35 分になります。最適な順序だと 31 分で済みます。

待ち時間

1 人目 2 分   0 分

2 人目 6 分   2 分

3 人目 4 分   8 分

4 人目 3 分  12 分

5 人目 9 分  15 分

            37 分 ← 「待ち時間の合計」

2 人目と 3 人目を入れ替えた例

待ち時間

1 人目 2 分   0 分

2 人目 4 分   2 分

3 人目 6 分   6 分

4 人目 3 分  12 分

5 人目 9 分  15 分

            35 分 ← 「待ち時間の合計」

Input

複数のデータセットが与えられます。各データセットは以下の形式で与えられます。

n
t1
t2
:
tn

1 行目にお客さんの人数 n (n ≤ 10,000) が与えられます。続く n 行に i 人目のお客さんが要する時間を表す整数 ti (0 ≤ ti ≤ 60) がそれぞれ１行に与えられます。

入力は１つの 0 を含む行で終わります。データセットの数は 50 を超えません。

Output

各データセットごとに、待ち時間の合計(整数)を１行に出力してください。

Sample Input

5
2
6
4
3
9
0

Output for the Sample Input

31



Sample input:
5
2
6
4
3
9
0


Sample output:
31

Submission status: Wrong Answer

Solution code:
n = int(input())
t = []
s = []
temp = 0
sum = 0
for i in range(n+1):
    t.append(i)
    t[i] = int(input())
    if t[i] == 0:
        break
    else:
        t.sort()
print(t)
for m in range(n):
    sum = sum + t[m]*(n-m-1)

print(sum)

Submission status: Wrong Answer

Solution code:
n = int(input())
t = []
s = []
temp = 0
sum = 0
for i in range(n+1):
    t.append(i)
    t[i] = int(input())
    if t[i] == 0:
        break
    else:
        t.sort()

for m in range(n):
    sum = sum + t[m]*(n-m-1)

print(sum)

Submission status: Wrong Answer

Solution code:
n = int(input())
t = []
s = 0
temp = 0
sum = 0
for i in range(n+1):

    s = int(input())
    if s == 0:
        break
    else:
        t.append(i)
        t[i] = s
        t.sort()

for m in range(n):
    sum = sum + t[m]*(n-m-1)

print(sum)

Submission status: Wrong Answer

Solution code:
n = int(input())
t = []
s = 0
temp = 0
sum = 0

if n == 0:
    print(n)
else:
    for i in range(n+1):

        s = int(input())
        if s == 0:
            break
        else:
            t.append(i)
            t[i] = s
            t.sort()

    for m in range(n):
        sum = sum + t[m]*(n-m-1)

    print(sum)

Submission status: Wrong Answer

Solution code:
n = int(input())
t = []
s = 0
temp = 0
sum = 0

if n == 0:
    print(n)
elif n == 1:
    print(0)
else:
    for i in range(n+1):

        s = int(input())
        if s == 0:
            break
        else:
            t.append(i)
            t[i] = s
            t.sort()

    for m in range(n):
        sum = sum + t[m]*(n-m-1)

    print(sum)

Submission status: Wrong Answer

Solution code:
n = int(input())
t = []
s = 0
sum = 0

if n == 0:
    print(n)

else:
    for i in range(n+1):
        s = int(input())
        if s == 0:
            break
        else:
            t.append(i)
            t[i] = s
            t.sort()
    if n == 1:
        print(0)
    else:
        for m in range(n):
            sum = sum + t[m]*(n-m-1)

        print(sum)

Submission status: Wrong Answer

Solution code:
n = int(input())
t = []
s = 0
sum = 0

if n == 0:
    print(n)

else:
    for i in range(n):
        s = int(input())
        if s == 0:
            break
        else:
            t.append(i)
            t[i] = s
            t.sort()

    for m in range(n):
        sum = sum + t[m]*(n-m-1)

    print(sum)

Submission status: Wrong Answer

Solution code:
n=input()
x=sorted([input() for _ in [0]*n])
s=[sum(x[:i]) for i in range(n)]
print sum(s)

Submission status: Wrong Answer

Solution code:
while True:
    n = int(input())
    if n==0:break
    wt = [int(input()) for _ in range(n)]
    total = wt[0]
    s = []
    for v in sorted(wt[1:]):
        s.append(total)
        total += v
    print(sum(s))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
capacity = [0 for i in range(int(input()))]
data = sorted(list(map(lambda x: int(input()), capacity)))
sum = 0
total = 0
for index in range(1,len(data)):
    sum += data[index-1]
    total += sum

print(total)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
count = int(input())
capacity = [0 for i in range(count)]
data = sorted(list(map(lambda x: int(input()), capacity)))
sum = 0
for index in range(0,len(data)):
    sum += data[index] * (count - 1 - index)
print(sum)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
count = int(input())
capacity = [0 for i in range(count)]
data = sorted(list(map(lambda x: int(input()), capacity)))
sum = 0
for index in range(0,len(data)):
    sum += data[index] * (count - 1 - index)
print(sum)
input()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0112
"""
import sys





W = [0]                         #  ??????x???????????§??????????????????????????????????????????????????°?????????????????§0?????????
def solve(data):
    global W
    total = 0
    data.sort()
    for i, t in enumerate(data):
        W.append(W[i] + t)      #  ??????????¬?????????????????????? = ??????????????????????????§??????????????????????¨? + ???????????????
    W[-1] = 0                   #  ????????????????¬???????????????¨??????????????§???????????°??????0?????????
    return sum(W)               #  ??¨?????????????????????????¨?


def main(args):
    while True:
        n = int(input())
        if n == 0:
            break
        data = [int(input()) for _ in range(n)]
        result = solve(data)
        print(result)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Wrong Answer

Solution code:
n = int(input())
a = sorted([int(input()) for i in range(n)])
s = 0
for i in range(n-1):
    s += (n-i-1)*a[i]
print(s)

Submission status: Wrong Answer

Solution code:
# AOJ 0112: A Milk Shop
# Python3 2018.6.18 bal4u

while True:
	n = int(input())
	if n == 0: break
	f = [0]*65
	for i in range(n):
		f[int(input())] += 1
	if n == 1:
		print(0)
		continue
	n -= f[0]
	i, ans, m = 1, 0, n
	while m > 0:
		k = f[i];
		if k == 0:
			i += 1
			continue
		if k == 1: ans += (m-1)*i
		else: 
			a = m*k - (k*(k+1))/2
			ans += a*i;
		m -= k;
		i += 1
	print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools
while 1:
    n=int(input())
    if n==0:break
    l=[int(input()) for _ in range(n)]
    print(min(sum([sum(j[:i]) for i in range(1,5)]) for j in itertools.permutations(l,len(l)-1)))

Submission status: Time Limit Exceeded

Solution code:
import itertools

while 1:
    n=int(input())
    if n==0:break
    l=[int(input()) for _ in range(n)]
    print(sum([sum(sorted(l)[:len(l)-1][:i]) for i in range(1,len(l))]))

Submission status: Memory Limit Exceeded

Solution code:
while 1:
  n=input()
  if n==0: break
  x=sorted([input() for _ in [0]*n])
  b=sum(map(sum,[x[:i] for i in range(n)]))
  print b

Submission status: Accepted

Solution code:
from sys import stdin
from itertools import accumulate

while(True):
    n = int(stdin.readline())
    if not n: break
    a = sorted([int(stdin.readline()) for _ in range(n)])
    b = list(accumulate(a))
    print(sum(b[:-1]))

Submission status: Accepted

Solution code:
while True:
    n = int(input())
    t = []
    s = 0
    sum = 0

    if n == 0:
        break

    else:
        for i in range(n):
            t.append(i)
            t[i] = int(input())
            t.sort()

        for m in range(n):
            sum = sum + t[m]*(n-m-1)

        print(sum)

Submission status: Accepted

Solution code:
#import math

while True:
    n = int(input())
    if n == 0: break
    t = []
    for i in range(n):
        t.append(int(input()))
    t.sort()
    time = 0
    sum = 0
    for i in range(len(t)):
        time += t[i]
        sum += time
    print(sum-time)

Submission status: Accepted

Solution code:
while 1:
  n=input()
  if n==0: break
  x=sorted([input() for _ in [0]*n])[::-1]
  s=[x[i]*i for i in range(n)]
  print sum(s)

Submission status: Accepted

Solution code:
while 1:
  n=input()
  if n==0: break
  x=sorted([input() for _ in [0]*n])
  a,b=0,0
  for e in x[:-1]: a+=e; b+=a;
  print b

Submission status: Accepted

Solution code:
while 1:
  n=input()
  if n==0: break
  x=sorted([input() for _ in [0]*n])
  s=0
  a=0
  for e in x[:-1]: a+=e; s+=a
  print s

Submission status: Accepted

Solution code:
while 1:
  n=input()
  if n==0: break
  x=sorted([int(raw_input()) for _ in [0]*n])
  s=0
  a=0
  for e in x[:-1]: a+=e; s+=a
  print s

Submission status: Accepted

Solution code:
while 1:
  n=int(raw_input())
  if n==0: break
  x=sorted([int(raw_input()) for _ in [0]*n])
  s=0
  a=0
  for e in x[:-1]: a+=e; s+=a
  print s

Submission status: Accepted

Solution code:
while 1:
    n=int(raw_input())
    if n==0: break
    s=t=0
    for i in sorted([int(raw_input()) for _ in range(n)]):
        t+=s
        s+=i
    print t
#(s=t=0
#(1..$_.to_i).map{gets.to_i}.sort.map{|i|t+=s;s+=i}
#p t)while gets>?1

Submission status: Accepted

Solution code:
while 1:
    n=int(raw_input())
    if n==0: break
    s=t=0
    for i in sorted([int(raw_input()) for _ in range(n)]):
        t+=s
        s+=i
    print t

Submission status: Accepted

Solution code:
while 1:
  n=int(raw_input())
  if n==0: break
  x=sorted(map(int,[raw_input() for _ in [0]*n]))
  s=a=0
  for e in x[:-1]: a+=e; s+=a
  print s

Submission status: Accepted

Solution code:
while(1):
    n=int(raw_input())
    if n==0:
        break
    else:
        t=[]
        for i in range(n):
            t.append(int(raw_input()))
        t.sort()
        s=[0]
        for i in range(n-1):
            s.append(s[i]+t[i])
        print sum(s)

Submission status: Accepted

Solution code:

while 1:
	n = int(raw_input())
	if n == 0:
		break;
	
	li = []
	t = 0
	for i in range(n):
		li.append(int(raw_input()))
	
	li.sort()
	for i in range(n - 1, 0, -1):
		t += i * li[n - i - 1]
		
	print t

Submission status: Accepted

Solution code:
while True:
    n = input()
    if n == 0: break
    ls, sum = [], 0
    for i in range(n):
        ls.append(input())
    ls = sorted(ls)
    for i in range(n-1):
        sum += ls[i]*(n-i-1)
    print sum

Submission status: Accepted

Solution code:
while True:
    lis = []
    N = int(raw_input())
    if N == 0: break
    for i in range(N):
        lis.append(int(raw_input()))
    else:
        lis.sort()
    ans = []
    w = 0
    for v in lis[:-1]:
        w += v
        ans.append(w)
    else:
        print sum(ans)

Submission status: Accepted

Solution code:
def doc_string():
	"""
	I can fly.
	"""

while True:
	num = int(raw_input())
	if num == 0:
		break
	
	list = []
	for i in xrange(num):
		list.append(int(raw_input()))
	
	list.sort()
	ans = []
	for i in xrange(num):
		ans.append(sum(list[:i]))
		
	print sum(ans)

Submission status: Accepted

Solution code:
while True:
  n = int(raw_input())
  if n == 0: break
  l = []
  for i in range(n): l.append(int(raw_input()))
  l.sort()
  print sum([sum(l[:i]) for i in range(len(l))])

Submission status: Accepted

Solution code:
while True:
  n = int(input().rstrip())
  if n == 0:
    break
  else:
    l = []
    while n > 0:
      a = int(input())
      l.append(a)
      n -= 1
    l.sort()
    ans = 0
    t = 0
    for i in l:
      ans += t
      t += i
    print(ans)

Submission status: Accepted

Solution code:
while True:
  n=int(input())
  if n==0:
    break
  dataset = [int(input()) for i in range(n)]
  dataset.sort()
  s,l=0,len(dataset)
  for i in range(l):
    s += dataset[i]*(l-1-i)
  print(s)

Submission status: Accepted

Solution code:
while True:
    n = int(input())
    if n == 0:
        break
    Lis = []
    for i in range(n):
        num = int(input())
        Lis.append(num)
        Lis = sorted(Lis)
    ans = 0
    for j in range(n - 1):
        ans += Lis[j] * (n -  1 - j)
    print(ans)

Submission status: Accepted

Solution code:
while 1:
    n=int(input())
    if n==0:break
    a=sorted([int(input()) for i in range(n)])
    print(sum([a[i]*(n-i-1) for i in range(n-1)]))

Submission status: Accepted

Solution code:
while True:
    n = int(input())
    if n==0:break
    wt = sorted([int(input()) for _ in range(n)])
    total = wt[0]
    s = []
    for v in wt[1:]:
        s.append(total)
        total += v
    print(sum(s))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys
import os
import math

for s in sys.stdin:
    n = int(s)

    if n == 0:
        break

    A = []
    for i in range(n):
        A.append(int(input()))

    A.sort()

    ans = 0
    for i, a in enumerate(A):
        ans += a * (n - i - 1)

    print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
while True:
    count = int(input())
    if count == 0:
        break
    capacity = [0 for i in range(count)]
    data = sorted(list(map(lambda x: int(input()), capacity)))
    sum = 0
    for index in range(0,len(data)):
        sum += data[index] * (count - 1 - index)
    print(sum)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0112
"""
import sys


W = [0]                         #  ??????x???????????§??????????????????????????????????????????????????°?????????????????§0?????????
def solve(data):
    global W
    total = 0
    data.sort()
    for i, t in enumerate(data):
        W.append(W[i] + t)      #  ??????????¬?????????????????????? = ??????????????????????????§??????????????????????¨? + ???????????????
    W[-1] = 0                   #  ????????????????¬???????????????¨??????????????§???????????°??????0?????????
    return sum(W)               #  ??¨?????????????????????????¨?


def main(args):
    global W
    while True:
        n = int(input())
        if n == 0:
            break
        data = [int(input()) for _ in range(n)]
        result = solve(data)
        print(result)
        W = [0]


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
# Aizu Problem 0112: A Milk Shop

#
import sys, math, os

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input.txt", "rt")


while True:
    N = int(input())
    if N == 0:
        break
    waits = sorted([int(input()) for _ in range(N)])
    latence = 0
    for k in range(N - 1):
        latence += waits[k] * (N - 1 - k)
    print(latence)

Submission status: Accepted

Solution code:
while True:
    try:
        n = int(input())
    except:
        break
    if n == 0:
        break
    a = sorted([int(input()) for i in range(n)])
    s = 0
    for i in range(n-1):
        s += (n-i-1)*a[i]
    print(s)

Submission status: Accepted

Solution code:
while True:
    n = int(input())
    if n == 0:
        break
    a = sorted([int(input()) for i in range(n)])
    s = 0
    for i in range(n-1):
        s += (n-i-1)*a[i]
    print(s)

Submission status: Accepted

Solution code:
while True:

    inputCount = int(input())

    if inputCount == 0:
        break

    timeList = []

    for lp in range(inputCount):
        timeList.append(int(input()))

    timeList.sort()

    totalTimeList = [0]

    for lp in range(inputCount - 1):
        waitTime = totalTimeList[-1] + timeList[lp]
        totalTimeList.append(waitTime)

    print(str(sum(totalTimeList)))

Submission status: Accepted

Solution code:
while True:

    inputCount = int(input())

    if inputCount == 0:
        break

    timeList = [int(input()) for item in range(inputCount)]
    timeList.sort()

    totalTimeList = [0]

    for lp in range(inputCount - 1):
        waitTime = totalTimeList[-1] + timeList[lp]
        totalTimeList.append(waitTime)

    print(str(sum(totalTimeList)))

Submission status: Accepted

Solution code:
while True:

    inputCount = int(input())

    if inputCount == 0:
        break

    timeList = [int(input()) for item in range(inputCount)]
    timeList.sort()

    waitTimeList = [0]

    for lp in range(inputCount - 1):
        waitTime = waitTimeList[-1] + timeList[lp]
        waitTimeList.append(waitTime)

    print(str(sum(waitTimeList)))

Submission status: Accepted

Solution code:
def main():
    while True:
        n = int(input())
        if n == 0:
            break

        cost = []
        for i in range(n):
            cost.append(int(input()))

        cost.sort()
        wait = 0
        for i in range(n):
            wait += sum(cost[:i])

        print(wait)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def get_input():
    while True:
        try:
            yield ''.join(input())
        except EOFError:
            break

while True:
    N = int(input())
    if N == 0:
        break
    p = []
    for l in range(N):
        p.append(int(input()))
    p.sort()
    S = 0
    s = 0
    for l in range(N-1):
        s += p[l]
        S += s
    print(S)

Submission status: Accepted

Solution code:
while True:
  n = int(input())
  if not n:
    break
  else:
    lst = [int(input()) for _ in range(n)]
    lst.sort()
    acc = ans = 0
    for i in lst:
      ans += acc
      acc += i
    print(ans)

Submission status: Accepted

Solution code:
while True:
  n = int(input())
  if not n:
    break
  lst = sorted([int(input()) for _ in range(n)])
  print(sum([(n - i - 1) * lst[i] for i in range(n)]))

Submission status: Accepted

Solution code:
import itertools

while 1:
    n=int(input())
    if n==0:break
    l=[int(input()) for _ in range(n)]
    ll=len(l)
    m=sorted(l)[:ll-1]
    print(sum([sum(m[:i]) for i in range(1,ll)]))

Submission status: Accepted

Solution code:
# AOJ 0112: A Milk Shop
# Python3 2018.6.18 bal4u

while True:
	n = int(input())
	if n == 0: break
	f = [0]*65
	for i in range(n):
		f[int(input())] += 1
	if n == 1:
		print(0)
		continue
	n -= f[0]
	i, ans, m = 1, 0, n
	while m > 0:
		k = f[i];
		if k == 0:
			i += 1
			continue
		if k == 1: ans += (m-1)*i
		else: 
			a = m*k - (k*(k+1))//2
			ans += a*i;
		m -= k;
		i += 1
	print(ans)

Submission status: Accepted

Solution code:
while True:
    n = int(raw_input())
    if n == 0: break

    result = 0
    times = []
    for i in range(n):
        times.append(int(raw_input()))
    times.sort()
    for i in range(len(times)):
        result += sum(times[:i])
    print result

Submission status: Accepted

Solution code:
while True:
    n=input()
    if n==0:
        break
    wt=sorted([input() for u in xrange(n)])
    print sum([sum(wt[:i]) for i in xrange(n)])

Submission status: Accepted

Solution code:
while True:
    n=input()
    if n==0:break
    L=sorted(input() for i in range(n))
    s,t=0,0
    for i in L:
        s+=t
        t+=i
    print s

Submission status: Accepted

Solution code:
#!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin, exit


def main(readline=stdin.readline):
    while 1:
        n = int(readline())
        if not n:
            exit()

        data = sorted(int(readline()) for _ in range(n))
        print(sum(i * t for i, t in zip(range(n-1, 0, -1), data)))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
while 1:
    n = input()
    if n==0: break
    t = [input() for i in xrange(n)]
    t.sort()
    print sum((n-1-i)*t[i] for i in xrange(n-1))

Submission status: Accepted

Solution code:
while 1:
	n = input()
	if n == 0: break
	que = sorted([int(raw_input()) for i in range(n)])
	print sum([sum(que[:i]) for i in range(1,n)])

Submission status: Accepted

Solution code:
while 1:
	n = input()
	if n == 0: break
	q = sorted([int(raw_input()) for i in range(n)])
	t = [0]*n
	for i in range(1,n):
		t[i] = t[i-1] + q[i-1]
	print sum(t)

Submission status: Accepted

Solution code:
while 1:
	n = input()
	if n == 0: break
	q = sorted([int(raw_input()) for i in range(n)])
	t = [0]*n
	for i in range(n-1):
		t[i + 1] = t[i] + q[i]
	print sum(t)

Submission status: Accepted

Solution code:
while 1:
	n = input()
	if n == 0: break
	q = sorted([input() for i in range(n)])
	t = [0]*n
	for i in range(n-1):
		t[i + 1] = t[i] + q[i]
	print sum(t)

Submission status: Accepted

Solution code:
while True:
  N = input()
  if not N:
    break
  A = sorted([input() for i in range(N)])
  B = [0] * N
  for i in range(1, N):
    B[i] += A[i - 1] + B[i - 1]
  print(sum(B))

Submission status: Accepted

Solution code:
while True :
    n = int(input())
    if n == 0 :
        break
    
    que = []
    for i in range(n) :
        que.append(int(input()))
    que.sort()
    
    time = 0
    for i in range(n) :
        time += sum(que[0:i])
    print(time)

Submission status: Accepted

Solution code:
while True: #無限ループ
    i = int(input()) #人数を標準入力する
    if i == 0:break #0人ならループを終わる
    else: #0人以外なら処理をする
        num_list = [] #空リスト
        for _ in range(i): #人数回繰り返す
            num_list.append(int(input())) #待ち時間をリストに加える
        num_list.sort() #リスト中身を小さい順にする
        kei = 0 #変数を初期化する
        kei_list = [] #空リスト作成
        for j in range(0,i - 1): #繰り返し
            kei += num_list[j] #待ち時間を計算する
            kei_list.append(kei) #リストに加える
        print(sum(kei_list)) #待ち時間の合計を計算する

Submission status: Accepted

Solution code:
while True:
    try:
        n = int(input())
        if n == 0:
            break
        ans = 0
        add = 0
        arr = []
        for i in range(0,n):
            arr.append(int(input()))
        arr = sorted(arr)

        for i in range(0,n - 1):
            add += arr[i]
            ans += add
        print(ans)          
    except:
    	break

Submission status: Accepted

Solution code:
import heapq
from collections import deque
from enum import Enum
import sys
import math
from _heapq import heappush, heappop
import copy
from test.support import _MemoryWatchdog

BIG_NUM = 2000000000
HUGE_NUM = 99999999999999999
#MOD = 1000000007
MOD = 10000
EPS = 0.000000001
sys.setrecursionlimit(100000)


while True:

    N = int(input())
    if N == 0:
        break

    table = [int(input()) for _ in range(N)]
    table.sort() #待ち時間が短い人を先に処理する

    ans = 0
    current = 0

    for i in range(N):
        ans += current
        current += table[i]

    print("%d"%(ans))

Submission status: Accepted

Solution code:
while True:
    n = int(input())
    if n == 0:
        break
    L = []
    for _ in range(n):
        L.append(int(input()))
    L.sort()
    total = 0
    for i in range(len(L)):
        total += sum(L[:i])
    print(total)

Submission status: Accepted

Solution code:
while True :
    n = int(input())
    if(n == 0) :
        break
    else :
        T = list()
        for i in range(n) :
            T.append(int(input()))
        
        T.sort()
        W = [0 for i in range(n)]
        
        for i in range(1, n) :
            W[i] = sum(T[0 : i])
     
        print(sum(W))

Submission status: Accepted

Solution code:

if __name__ == '__main__':

	A = []
	while True:
		try:
			n = int(input())
			if n == 0:
				break
			else:
				for _ in range(n):
					m = int(input())
					A.append(m)

				A = sorted(A)

				b = 0
				B = []
				for x in range(len(A)-1):
					b += A[x]
					B.append(b)

				print(sum(B))
				A = []

		except EOFError:
			break

Submission status: Accepted

Solution code:
time = []
while 1:
    n = int(input())
    if n==0:break
    time = sorted([int(input()) for i in range(n)])
    box=answer=0
    for i in range(n-1):
        box +=time[i]
        answer +=box
    print(answer)

Submission status: Accepted

Solution code:
while True:
    n=int(input())
    if n==0:
        break
    t=[]
    for i in range(n):
        t.append(int(input()))
    t=sorted(t)

    retval=0
    for i in range(n):
        retval+=t[i]*(n-i-1)
    print(retval)

Submission status: Accepted

Solution code:
while 1:
    cust_list=[]
    ans=0
    n=int(input())
    if n==0:break
    for i in range(n):
        cust_list.append(int(input()))
    cust_list.sort()
    for i in range(len(cust_list)):
        ans+=sum(cust_list[:i])
    print(ans)

Submission status: Accepted

Solution code:
while True:
    n = int(input())
    if n == 0:
        break
    t_list = []
    for i in range(n):
        t_list.append(int(input()))
    t_list.sort()
    ans = 0
    for i in range(n - 1):
        ans += t_list[i]*(n - i -1)
    print(ans)

Submission status: Accepted

Solution code:
while True:
  n = int(input())
  if n == 0:
    break
  a = sorted([int(input()) for i in range(n)])
  print(sum([a[i] * (n-i-1) for i in range(n-1)]))

Submission status: Accepted

Solution code:
while 1:
    n = int(input())
    if n == 0:
        break

    datas = []
    for _ in range(n):
        t = int(input())
        datas.append(t)

    datas.sort()
    waits = [0]
    for i, data in enumerate(datas):
        waits.append(waits[i] + data)
    waits.pop(-1)

    print(sum(waits))

Submission status: Accepted

Solution code:
while True:
    n = int(input())
    if n == 0: break
    array = sorted([int(input()) for _ in range(n)])
    result, wait = [], 0
    for time in array[:-1]:
        wait += time
        result.append(wait)
    print(sum(result))

Submission status: Accepted

Solution code:
while True:
    num_length = int(input())
    if num_length==0:break
    eachwait = []
    wait = []
    for i in range(0, num_length):
        eachwait.append(int(input()))
    eachwait.sort()
    for i, num in enumerate(eachwait):
        wait.append(sum(eachwait[:i]))
    print(sum(wait))

Submission status: Runtime Error

Solution code:
while True:
    n = int(input())
    t = []
    s = 0
    sum = 0

    if n == 0:
        break

    else:
        for i in range(n):
            s = int(input())
            if s == 0:
                break
            else:
                t.append(i)
                t[i] = s
                t.sort()

        for m in range(n):
            sum = sum + t[m]*(n-m-1)

        print(sum)

Submission status: Runtime Error

Solution code:
while 1:
	n = input()
	if n == 0: break
	q = sorted([input() afor i in range(n)])
	t = [0]*n
	for i in range(n-1):
		t[i + 1] = t[i] + q[i]
	print sum(t)