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

hide comments
strikas: 2018-09-07 07:17:34

AC in one go...

vishalroybitp: 2018-08-16 04:56:06

Can ayone plz explain the test case.

jonmanu: 2018-08-14 13:33:32

my code is running perfectly fine in devc++,but im getting wa here????very annoyed....!

krritik: 2018-07-16 15:36:20

what would be the answer if the test case is
5
10
6
3
3
3

nadstratosfer: 2018-06-04 19:06:27

Just resubmitted my old solution and got AC without any defensive input techniques. I'd encourage you to try and refine your IO methods with problems like INTEST. If your current scheme gives you issues here, you're gonna suffer plenty of headache in problems where the input file does have formatting errors.

wrzoboo: 2018-06-04 14:03:19

Garbage problem description. Spoj judge input contains empty lines (or some other whitespace addition) which are never mentioned. Add something to handle them or expect NZEC in python3.

waqar_ahmad224: 2018-04-01 21:28:12

0 @ Rohitsai

rohitsai: 2018-03-01 12:38:36

what is the output of
1
5
-1

rv111: 2018-02-22 08:39:28

keenly look at right constraints , else like me u may hv some WA's

abhi007rider: 2018-01-28 07:40:51




why it showing time TLE??

Last edit: 2018-01-28 07:41:31

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