Little chief has his own restaurant in the city. There are N workers there. Each worker has his own salary. The salary of the i-th worker equals to W_{i} (i = 1, 2, ..., N). Once, chief decided to equalize all workers, that is, he wants to make salaries of all workers to be equal. But for this goal he can use only one operation: choose some worker and increase by 1 salary of each worker, except the salary of the chosen worker. In other words, the chosen worker is the loser, who will be the only worker, whose salary will be not increased during this particular operation. But loser-worker can be different for different operations, of course. Chief can use this operation as many times as he wants. But he is a busy man. That's why he wants to minimize the total number of operations needed to equalize all workers. Your task is to find this number.

------ Input ------ 

The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains a single integer N denoting the number of workers. The second line contains N space-separated integers  W_{1}, W_{2}, ..., W_{N} denoting the salaries of the workers.

------ Output ------ 

For each test case, output a single line containing the minimum number of operations needed to equalize all workers.

------ Constraints ------ 

$1 ≤ T ≤ 100$
$1 ≤ N_{} ≤ 100$
$0 ≤ W_{i} ≤ 10000 (10^{4})$

----- Sample Input 1 ------ 
2
3
1 2 3
2
42 42
----- Sample Output 1 ------ 
3
0
----- explanation 1 ------ 
Example Case 1. Chief can equalize all salaries in 3 turns:
Turn ID IDs of involved workers Salaries after the move1  1 2  2 3 32  1 2  3 4 33  1 3  4 4 4Example Case 2. All salaries are already equal. He doesn't need to do anything.

Difficulty: EASY
Tags: Constructive algorithms

Data structures

Mathematics

Ad-hoc
Skill types: Data structures

Starter code:

Solutions:
for i in range(int(input())):
	n = int(input())
	w = list(map(int, input().split()))
	op = sum(w) - n * min(w)
	print(op)


t = int(input())
for T in range(t):
	n = int(input())
	arr = [int(x) for x in input().split()]
	print(sum(arr) - n * min(arr))


for t in range(int(input())):
	n = int(input())
	w = list(map(int, input().split()))
	k = min(w)
	s = 0
	if len(set(w)) == 1:
		s = 0
	else:
		for i in w:
			s = s + i - k
	print(s)


from collections import Counter
for _ in range(int(input())):
	n = int(input())
	w = list(map(int, input().split()))
	print(sum(w) - n * min(w))


t = int(input())
for t_ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	print(sum(arr) - min(arr) * n)


T = int(input())
for _ in range(T):
	N = int(input())
	W = list(map(int, input().split()))
	W0 = min(W)
	s = sum(W)
	print(s - N * W0)


t = int(input())
for i in range(t):
	n = int(input())
	x = input().split(' ')
	m = int(x[0])
	s = 0
	for j in x:
		y = int(j)
		if m > y:
			m = y
		s += y
	print(s - n * m)


T = int(input())
for i in range(T):
	n = int(input())
	l = list(map(int, input().split()))
	print(sum(l) - min(l) * len(l))


T = int(input())
for t in range(T):
	N = int(input())
	w = list(map(int, input().split()))
	s = sum(w)
	w1 = min(w)
	print(s - w1 * N)


for _ in range(int(input())):
	n = int(input())
	salary = list(map(int, input().split()))
	(oper, mini) = (0, min(salary))
	for n in salary:
		oper += n - mini
	print(oper)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	print(sum(arr) - n * min(arr))


for p in range(int(input())):
	h = int(input())
	arr = list(map(int, input().split()))
	sumarr = sum(arr)
	minarr = min(arr)
	print(sumarr - minarr * h)


test = int(input())
for z in range(test):
	n = int(input())
	A = list(map(int, input().split()))
	print(sum(A) - n * min(A))


t = int(input())
for test in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	count = 0
	while len(set(arr)) != 1:
		arr.sort()
		(a, b) = (arr[0], arr[-1])
		count += b - a
		arr = [i + b - a for i in arr[:-1]]
		arr.append(b)
	print(count)


t = int(input())
for _ in range(t):
	n = int(input())
	w = list(map(int, input().split()))
	print(sum(w) - n * min(w))


testCase = int(input())

def computeResult(n, arr):
	arr = sorted(arr)
	maxVal = max(arr)
	minVal = min(arr)
	diffVal = sum(arr) - n * minVal
	return diffVal
for tc in range(testCase):
	num = int(input())
	array = list(map(int, input().split()))
	res = computeResult(num, array)
	print(res)


for i in range(int(input())):
	n = int(input())
	lst = list(map(int, input().split()))
	lst.sort()
	c = 0
	mi = lst[0]
	for i in lst[1:]:
		c += i - mi
	print(c)


t = int(input())
for _ in range(t):
	n = int(input())
	list1 = list(map(int, input().split()))
	a = sum(list1)
	b = min(list1)
	c = a - b * n
	print(c)


T = int(input())
for _ in range(T):
	N = int(input())
	W = list(map(int, input().split()))
	W.sort()
	s = sum(W)
	print(s - N * W[0])


test = int(input())
for z in range(test):
	n = int(input())
	A = list(map(int, input().split()))
	m = min(A)
	M = -1
	for i in range(n):
		if M < A[i]:
			M = A[i]
			temp = i
	moves = 0
	while M != m:
		for i in range(n):
			if i != temp:
				A[i] += M - m
		moves += M - m
		m = M
		for i in range(n):
			if M < A[i]:
				M = A[i]
				temp = i
	print(moves)


T = int(input())
for test_case in range(T):
	N = int(input())
	W = tuple((int(_) for _ in input().split()))
	print(sum(W) - min(W) * N)


T = int(input())
for _ in range(T):
	N = int(input())
	arr = list(map(int, input().split()))
	S = sum(arr)
	K = max(arr)
	W1 = min(arr)
	print(S - N * W1)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	d = {}
	for i in arr:
		if i in d:
			d[i] += 1
		else:
			d[i] = 1
	if len(d) == 1:
		print(0)
	else:
		print(sum(arr) - n * min(arr))


for _ in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	print(sum(l) - len(l) * min(l))


r2 = lambda n: range(1, n + 1)
alph = lambda s: s.isalpha()
mod = lambda n: n % (10 ** 9 + 7)
sl = lambda l: list(set(l))
soli = lambda : sorted(list(map(int, input().split())))
soi = lambda : sorted(input())
ii = lambda : int(input())
li = lambda : list(map(int, input().split()))
si = lambda : input()
lsi = lambda : list(input())
sepi = lambda : map(int, input().split())
seti = lambda : set(map(int, input().split()))
for i in range(int(input())):
	workers = ii()
	salaries = soli()
	lowestSalary = salaries[0]
	moves = 0
	for i in range(workers - 1):
		moves += salaries[i + 1] - lowestSalary
	print(moves)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	m = min(a)
	q = 0
	for i in a:
		q += i - m
	print(q)


T = int(input())
for i in range(T):
	(sui, res) = (0, 0)
	N = int(input())
	l = [int(x) for x in input().split()]
	while max(l) != min(l):
		l.sort()
		res = max(l) - min(l)
		l = [x + res for x in l]
		l[len(l) - 1] = l[len(l) - 1] - res
		sui = sui + res
	print(sui)


T = int(input())
for i in range(T):
	n = int(input())
	A = list(map(int, input().split()))
	m = min(A)
	count = 0
	for j in A:
		s = j - m
		count += s
	print(count)


t = int(input())
for i in range(t):
	n = int(input())
	workers = list(map(int, input().split()))
	min_salary = min(workers)
	total_ops = 0
	for salary in workers:
		total_ops += salary - min_salary
	print(total_ops)


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	print(sum(a) - n * min(a))


T = int(input())
for i in range(T):
	n = int(input())
	w = [0] * n
	w = list(map(int, input().split()))
	minop = sum(w) - len(w) * min(w)
	print(minop)


t = int(input())
for i in range(t):
	n = int(input())
	w = list(map(int, input().split()))
	p = sum(w)
	m = min(w)
	print(p - m * n)


for t in range(int(input())):
	n = int(input())
	w = list(map(int, input().split()))
	a = sum(w)
	b = min(w)
	print(a - b * n)


def read_input():
	T = int(input())
	NWnL = [(int(input()), input().split(' ')) for _ in range(T)]
	NWnL = [(N, [int(e) for e in Wn]) for (N, Wn) in NWnL]
	return (NWnL, T)

def solve_move(Wn, N):
	if all((e == Wn[0] for e in Wn[1:])):
		return 0
	return sum(Wn) - N * min(Wn)
(NWnL, T) = read_input()
for i in range(T):
	print(solve_move(NWnL[i][1], NWnL[i][0]))


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = sum(arr)
	minw = min(arr)
	res = ans - minw * n
	print(res)


for _ in range(int(input())):
	n = int(input())
	sal = list(map(int, input().split()))
	minval = min(sal)
	sum = 0
	for j in range(n):
		sum += sal[j] - minval
	print(sum)


T = int(input())
for i in range(T):
	N = int(input())
	W = list(map(int, input().split()))
	W.sort()
	ops = 0
	print(sum(W) - len(W) * W[0])


for _ in range(int(input())):
	n = int(input())
	w = [int(i) for i in input().split()]
	m = min(w)
	q = 0
	for i in w:
		q += i - m
	print(q)


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	print(sum([i - min(a) for i in a]))


T = int(input())
for i in range(T):
	N = int(input())
	w = list(map(int, input().split()))
	print(sum(w) - N * min(w))


for _ in range(int(input())):
	n = int(input())
	ar = list(map(int, input().split()))
	macs = min(ar)
	humpty = [0] * n
	for j in range(n):
		humpty[j] -= macs - ar[j]
	print(sum(humpty))


tests = int(input())
for _ in range(tests):
	workers = int(input())
	salaries = sorted([int(x) for x in input().split()])
	moves = 0
	for i in range(len(salaries) - 1):
		moves += salaries[i + 1] - salaries[0]
	print(moves)


T = int(input())
for i in range(T):
	N = int(input())
	W = list(map(int, input().split()))
	print(sum(W) - len(W) * min(W))


for i in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	sumarr = sum(arr)
	minarr = min(arr)
	print(sumarr - minarr * n)


for _ in range(int(input())):
	n = int(input())
	w = [int(x) for x in input().split()]
	wo = min(w)
	print(sum(w) - n * wo)


t = int(input())
for i in range(0, t):
	n = int(input())
	salary = list(map(int, input().split()))
	min_mov = []
	MIN = min(salary)
	for i in salary:
		min_mov.append(i - MIN)
	count = sum(min_mov)
	print(count)


for i in range(int(input())):
	n = int(input())
	l = list(map(int, input().split()))
	a = sum(l)
	b = min(l)
	print(a - b * n)


for i in range(int(input())):
	n = int(input())
	min1 = float('INF')
	moves = 0
	li = list(map(int, input().split()))
	for x in li:
		if min1 > x:
			min1 = x
	for x in li:
		moves += x - min1
	print(moves)


t = int(input())
while t:
	t -= 1
	n = int(input())
	v = list(map(int, input().split()))
	x = [0] * n
	m = min(v)
	for i in range(n):
		x[i] = v[i] - m
	print(sum(x))


number_of_test_cases = int(input())
salaries_list = []
for tc in range(number_of_test_cases):
	x = input()
	salaries_list.append([eval(i) for i in input().split(' ')])
for salaries in salaries_list:
	sum = 0
	salaries.sort()
	for salary in salaries:
		sum = sum + salary
	print(sum - len(salaries) * salaries[0])


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	o = sum(l) - n * min(l)
	print(o)


def equalize_salaries(n, salaries):
	return sum(salaries) - n * min(salaries)
t = int(input().strip())
for _ in range(t):
	n = int(input().strip())
	salaries = list(map(int, input().strip().split()))
	print(equalize_salaries(n, salaries))


for _ in range(int(input())):
	moves = 0
	x = int(input())
	lst = list(map(int, input().split()))
	mi = min(lst)
	for i in range(0, x):
		moves += lst[i] - mi
	print(moves)


for _ in range(int(input())):
	n = int(input())
	m = float('INF')
	mo = 0
	l = list(map(int, input().split()))
	for i in l:
		if m > i:
			m = i
	for i in l:
		mo += i - m
	print(mo)


T = int(input())
for case in range(T):
	N = int(input())
	W_i = list(map(int, input().split(' ')))
	print(sum(W_i) - N * min(W_i))


for _ in range(int(input())):
	n = int(input())
	w = list(map(int, input().split()))
	w.sort()
	if n == 1 or len(list(set(w))) == 1:
		print(0)
	else:
		c = 0
		for i in range(1, n):
			c += (n - i) * (w[i] - w[i - 1])
		print(c)