** Equal Candies solution codeforces** – There are 𝑛n boxes with different quantities of candies in each of them. The 𝑖i-th box has 𝑎𝑖ai candies inside.

## [Solution] Equal Candies solution codeforces

You also have 𝑛n friends that you want to give the candies to, so you decided to give each friend a box of candies. But, you don’t want any friends to get upset so you decided to eat some (possibly none) candies from each box so that all boxes have the same quantity of candies in them. Note that you may eat a different number of candies from different boxes and you cannot add candies to any of the boxes.

What’s the minimum total number of candies you have to eat to satisfy the requirements?

The first line contains an integer 𝑡t (1≤𝑡≤10001≤t≤1000) — the number of test cases.

The first line of each test case contains an integer 𝑛n (1≤𝑛≤501≤n≤50) — the number of boxes you have.

The second line of each test case contains 𝑛n integers 𝑎1,𝑎2,…,𝑎𝑛a1,a2,…,an (1≤𝑎𝑖≤1071≤ai≤107) — the quantity of candies in each box.

## [Solution] Equal Candies solution codeforces

For each test case, print a single integer denoting the minimum number of candies you have to eat to satisfy the requirements.

5 5 1 2 3 4 5 6 1000 1000 5 1000 1000 1000 10 1 2 3 5 1 2 7 9 13 5 3 8 8 8 1 10000000

10 4975 38 0 0

## [Solution] Equal Candies solution codeforces

For the first test case, you can eat 11 candy from the second box, 22 candies from the third box, 33 candies from the fourth box and 44 candies from the fifth box. Now the boxes have [1,1,1,1,1][1,1,1,1,1] candies in them and you ate 0+1+2+3+4=100+1+2+3+4=10 candies in total so the answer is 1010.

For the second test case, the best answer is obtained by making all boxes contain 55 candies in them, thus eating 995+995+0+995+995+995=4975995+995+0+995+995+995=4975 candies in total.