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
2013-06-30 06:46:51 UTKARSH
<snip>
pls tell me whats wrong with my code

Last edit: 2022-10-07 23:11:03
2013-05-29 00:57:03 Spar!k
@Chris: it's O(n) ...
2013-05-28 11:24:03 Erti-Chris Eelmaa
Passed this baby. You need to devise O(nlogn) algorithm atleast, in order to pass it. My O(nn) solution got TIME OUT.
2013-05-01 18:19:33 Parshant garg
TLE. help me?
2013-02-23 15:16:38 Sivaraman Nagarajan
I thought I'm using a worst solution of parsing inputs twice but it worked , So is that the only solution possible?
2013-01-31 17:27:20 reva yoga pradana
int input[10000]; --> WA

but,
int input[10001]; --> AC

-___-
2013-01-03 05:06:04 Hasil Sharma
NZEC error in python :( , can anyone plz help ,what can be the reason for that
Got AC finally ...

Last edit: 2013-07-22 10:37:56
2012-12-11 12:18:18 arijit pande
got it! :D
2012-09-16 13:33:23 mani
is qsort function in c allowed in spoj?? pls reply ffs... :)
2012-09-02 17:38:46 Abhash Singh
remember one move means moving one candy at a time!not moving a number of candies to even out a packet.
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.