import sys
test_cases = open(sys.argv[1], 'r')
for test in test_cases:
arr = test.split()
n = int(arr[0])
sum = 0
for i in range(n):
sum += int(arr[i+1])
avg = int (sum / n)
dist = 0
for i in range(n):
dist += abs(avg-int(arr[i+1]))
avgInc = avg + 1
while True:
sumDist = 0
for i in range(n):
sumDist += abs(avgInc-int(arr[i+1]))
if dist < sumDist :
break
dist = sumDist
avgInc += 1
avgDec = avg - 1
while True:
sumDist = 0
for i in range(n):
sumDist += abs(avgDec-int(arr[i+1]))
if dist < sumDist :
break
dist = sumDist
avgDec -= 1
print dist
test_cases.close()
test_cases = open(sys.argv[1], 'r')
for test in test_cases:
arr = test.split()
n = int(arr[0])
sum = 0
for i in range(n):
sum += int(arr[i+1])
avg = int (sum / n)
dist = 0
for i in range(n):
dist += abs(avg-int(arr[i+1]))
avgInc = avg + 1
while True:
sumDist = 0
for i in range(n):
sumDist += abs(avgInc-int(arr[i+1]))
if dist < sumDist :
break
dist = sumDist
avgInc += 1
avgDec = avg - 1
while True:
sumDist = 0
for i in range(n):
sumDist += abs(avgDec-int(arr[i+1]))
if dist < sumDist :
break
dist = sumDist
avgDec -= 1
print dist
test_cases.close()
No comments:
Post a Comment