Python Tutorial

Tuesday, January 15, 2013

Problem solving using python : Candy I


Candy I: https://www.spoj.com/problems/CANDY/
All source code available on github

n=input()
while n != -1:
    a=[]
    s =0
    for i in range(0,n):
        d=input()
        s  += d
        a.append(d)
    avg = int(s/n)
    if avg*n != s:
        print -1
    else:
        r=0
        for i in range(0,n):
            if a[i]<avg:
                r += (avg - a[i])
        print r
    n = input()

0 comments:

Post a Comment