Toffee Reward

0

0 votes
Easy
Problem

Assume Sami is a Professor at NIT Hamirpur. He has decided to conduct quiz based on C language. For each correct answer, he will reward student with packet of toffees. It should be noted that the sizes of the packets are not the same.

Sami sir is afraid that this will be injustice to students as all questions of the quiz are of same weightage. He wants to avoid this. Therefore, he has decided to open all the packets, count the candies in each packet and move some toffees from bigger packets to smaller ones so that each packet will contain the same number of toffees. The question is how many toffees he has to move.

Input Format-

The input file consists of several blocks of data. Each block starts with the number of toffee packets N(1<= N <=10000) followed by N integers (each less than 1000) in separate lines, giving the number of candies in each packet. After the last block of data there is the number -1.

Output Format-

The output should contain one line with the smallest number of moves for each block of data. One move consists of taking one toffee from a packet and putting it into another one. If it is not possible to have the same number of candies in each packet, output the number -1.

Time Limit: 1.5
Memory Limit: 256
Source Limit:
Editor Image

?