CANDY - Candy I

Jennifer is a teacher in the first year of a primary school. She has gone for a trip with her class today. She has taken a packet of candies for each child. Unfortunately, the sizes of the packets are not the same.

Jennifer is afraid that each child will want to have the biggest packet of candies and this will lead to quarrels or even fights among children. She wants to avoid this. Therefore, she has decided to open all the packets, count the candies in each packet and move some candies from bigger packets to smaller ones so that each packet will contain the same number of candies. The question is how many candies she has to move.

Input specification

The input file consists of several blocks of data. Each block starts with the number of candy 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 specification

The output file should contain one line with the smallest number of moves for each block of data. One move consists of taking one candy 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.

Example

Input file:
5
1
1
1
1
6
2
3
4
-1

Output file:
4
-1

Added by:Fudan University Problem Setters
Date:2007-12-01
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: C99 ERL JS-RHINO
Resource:IPSC 1999

hide comments
2015-01-01 13:16:09 SM_92
so much of mistakes make me crazy
1.confused by smallest number of moves
2.long long and "\n"
2014-12-25 03:23:46 Atharva Chandra
What should I do if i get TLE?
2014-12-09 18:08:45 MKM
@numerix ohh now I understood the input file xD

Last edit: 2014-12-09 18:09:13
2014-12-09 18:03:02 numerix
@Manish Kumar Mishra: You find the explanation in the input description. There are several blocks of data. Each block ...
2014-12-09 17:55:32 MKM
My question may look dumb but if each block starts with integer N followed by N number of integers giving the number of candies in each packet, shouldn't only 5 integers follow after the block started with 5? Why was 5 followed by 8 integers instead of 5?
2014-12-08 06:53:04 Arpit Jain
Can someone tell the problem in my code ?
<snip>

Last edit: 2022-10-07 23:11:19
2014-12-07 08:46:54 Neeraj
<snip>
pls tell me whats wrong with my code

Last edit: 2022-10-07 23:11:24
2014-11-25 13:24:33 Dhruv k14
@shankar people who are actually trying to improve their coding skills may sort the list of problems so that they are able to identify tough problems

Last edit: 2014-11-25 13:25:53
2014-11-22 06:37:20 matematikaadit
@shalini try following input:

2
3
4
5
1
1
1
1
6
-1
2014-11-14 15:51:58 shalini
I could solve the problem but when submitted showing wrong answer.. please help me in solving this problem.. here is the solution
<snip>

Last edit: 2022-10-07 23:11:15
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.