Problem:


Score : 200 points

Problem Statement

Evi has N integers a_1,a_2,..,a_N. His objective is to have N equal integers by transforming some of them.

He may transform each integer at most once. Transforming an integer x into another integer y costs him (x-y)^2 dollars. Even if a_i=a_j (i≠j), he has to pay the cost separately for transforming each of them (See Sample 2).

Find the minimum total cost to achieve his objective.

Constraints

1≦N≦100

-100≦a_i≦100

Input

The input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the minimum total cost to achieve Evi's objective.

Sample Input 1

2
4 8

Sample Output 1

8

Transforming the both into 6s will cost (4-6)^2+(8-6)^2=8 dollars, which is the minimum.

Sample Input 2

3
1 1 3

Sample Output 2

3

Transforming the all into 2s will cost (1-2)^2+(1-2)^2+(3-2)^2=3 dollars. Note that Evi has to pay (1-2)^2 dollar separately for transforming each of the two 1s.

Sample Input 3

3
4 2 5

Sample Output 3

5

Leaving the 4 as it is and transforming the 2 and the 5 into 4s will achieve the total cost of (2-4)^2+(5-4)^2=5 dollars, which is the minimum.

Sample Input 4

4
-100 -100 -100 -100

Sample Output 4

0

Without transforming anything, Evi's objective is already achieved. Thus, the necessary cost is 0.



Sample input:
2
4 8


Sample output:
8

Submission status: Accepted

Solution code:
N = int(input())
A = [int(n) for n in input().split()]

avg = round(sum(A)/N)

cost = sum([(a-avg)**2 for a in A])

print(cost)

Submission status: Accepted

Solution code:
n = int(input())
arr = list(map(int,input().split()))
ave = round(sum(arr)/n)
arr = [(x-ave)**2 for x in arr]
print(sum(arr))

Submission status: Accepted

Solution code:
import math
n = input()
l = map(int, raw_input().strip().split())
m = 0
for x in l:
	m += x
ma = m/float(n)
m = int(math.ceil(ma))
ans = 0
for x in l:
	ans += (m-x)*(m-x)
m = int(math.floor(ma))
ans1 = 0
for x in l:
	ans1 += (m-x)*(m-x)
print(min(ans,ans1))

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
total=0
for i in range(N):
  total+=A[i]
average1=int(total/N)
average2=average1+1
ans1=0
ans2=0
for i in range(N):
  ans1+=(A[i]-average1)**2
  ans2+=(A[i]-average2)**2
print(min(ans1,ans2))

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
A = round(sum(a)/N)
if float(A) - int(A) == 0.5:
  A += 1
x = 0
for i in a:
  x += (A-i)**2
print(x)

Submission status: Accepted

Solution code:
n=int(input())
l=[int(x) for x in input().split()]
avg=round(sum(l)/n)
l=[(x-avg)**2 for x in l]
print(sum(l))

Submission status: Accepted

Solution code:
from collections import defaultdict


def main():
    N = int(input())
    a_list = list(map(int, input().split()))

    ans = 10 ** 10
    for m in range(min(a_list), max(a_list) + 1):
        cost = sum((m - a) ** 2 for a in a_list)
        ans = min(ans, cost)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())

a = list(map(int, input().split()))

mean = int(round(sum(a)/len(a)))
 
ans = 0
for i in range(N):
    ans += (a[i] - mean)**2
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
MIN,MAX = min(A),max(A)

cost = 100000000000
for i in range(MIN,MAX+1):
	c = 0
	for a in A:
		c += (i - a)*(i - a)
	cost = min(cost,c)
print(cost)

Submission status: Accepted

Solution code:
N = input()
a = map(int, raw_input().split())

mn = min(a)
mx = max(a)
mincost = float('inf')
for n in range(mn, mx+1):
    cost = 0
    for e in a:
        cost += (e-n)**2
    mincost = min(cost, mincost)
print mincost

Submission status: Accepted

Solution code:
N = input()
a = map(int, raw_input().split())
best = (10**10)
for v in range(-100, 101):
    cost = sum([(a[i] - v)**2 for i in range(N)])
    best = min(best, cost)
print best

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = input()
a = map(int,raw_input().split())

ave = int(0.5 + 1.0*sum(a)/len(a)) if sum(a)>=0 else int(-0.5 + 1.0*sum(a)/len(a)) 
cost = 0
for i in a:
    cost += (i-ave)**2
print(cost)

Submission status: Accepted

Solution code:
n = float(raw_input())
l = map(int, raw_input().split())
c = int(round(sum(l)/n))
ret = 0
for i in l:
    ret += (c-i)**2
print ret

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))

mi = min(a)
ma = max(a)

ans = 10 ** 10
for i in range(mi, ma + 1):
    buf = sum(map(lambda x: (x - i) ** 2, a))
    if ans > buf:
        ans = buf
print(ans)

Submission status: Accepted

Solution code:
n = int(raw_input())
a = map(float, raw_input().split())
ave = int(round(sum(a) / len(a), 0))

ans = 0
for value in a:
 ans += (value - ave) ** 2
print int(ans)

Submission status: Accepted

Solution code:
import sys
n = int(input())
a = sorted(map(int,input().split()))
ans = sys.maxsize
for i in range(202):
    x = -100 + i
    cost = 0
    for v in a:
        cost += ( x - v )**2
    ans = min(ans,cost)
print(ans)

Submission status: Accepted

Solution code:
import sys

def debug(x, table):
    for name, val in table.items():
        if x is val:
            print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr)
            return None

def solve():
    N = int(input())
    A = [int(i) for i in input().split()]
    ans = float('inf')

    for base in range(-100, 101):
        ans = min(ans, sum((a - base)**2 for a in A))

    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
INF=10**10

def main():
    
    n=int(input())
    array=list(map(int,input().split()))
    
    answer=INF
    
    for a in range(-100,101):
        temp=0
        for b in array:
            temp+=(a-b)**2
        if temp<answer:
            answer=temp
    print(answer)
            

if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

input()
nums = list(map(int, input().split(' ')))


def avg(nums):
    avg = sum(nums)/len(nums)
    return round(avg)


def calc(nums, base):
    total = 0
    for n in nums:
        sub = n - base
        total += (sub * sub)
    return total


if __name__ == "__main__":
    base = avg(nums)
    c = calc(nums, base)
    print(c)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))

def dist(x, y):
    return (x - y) ** 2


def calc(x):
    return sum(map(lambda y: dist(x, y), a))


avg = sum(a) // n
print(min((calc(avg + 1), calc(avg))))

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-
import math
N = int(input())
a = list(map(int,input().split()))
#alpha, beta = (sum(a), sum(list(map(lambda n:n**2, a))))
#beta -= int(alpha**2/N)
alpha = sum([(tmp - round(sum(a)/N))**2 for tmp in a]) 
print(alpha)

Submission status: Accepted

Solution code:

N = int(input())
a = [int(_) for _ in input().split()]

ans = float('inf')
for n in range(-100, 101):
    ans = min(ans, sum([(n-x)**2 for x in a]))

print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8;

def main():
    n = int(input())
    al = list(map(int, input().split()))
    mc = 10 ** 20

    for i in range(-100, 101):
        c = 0
        for a in al:
            c += (a - i) ** 2
        if mc > c: mc = c

    print(mc)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(raw_input())
A = map(int, raw_input().split())
v = 10**10
for i in range(min(A), max(A)+1):
    v = min(v, sum([(a-i)**2 for a in A]))
print v

Submission status: Accepted

Solution code:
n=int(input())
a=[int(i) for i in input().split()]
min=0
for i in range(-100,101):
    sum=0
    for j in range(n):
        sum=sum+(a[j]-i)**2
    if i==-100:
        min=sum
    elif sum<min:
        min=sum
print(min)

Submission status: Accepted

Solution code:
from collections import defaultdict


def main():
    n = int(input())
    l = list(map(int, input().split()))
    ans = 10 ** 20
    for x in range(-100, 101):
        ans = min(ans, sum([(x - a) ** 2 for a in l]))

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]

mincost = 10000000
for i in range(-100, 101):
    cost = 0
    for j in range(n):
        cost += (a[j]-i) ** 2
    mincost = min(cost, mincost)

print(mincost)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = list(map(int, input().split()))
    ans = 10 ** 10

    for y in range(min(a), max(a) + 1):
        total = 0
        for x in a:
            total += (x - y) ** 2
        ans = min(ans, total)

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math
n = int(input())
a = list(map(int, input().split()))
avr = 0
ans = 0
for ai in a:
    avr += ai
avr = round(avr/len(a))
for ai in a:
    ans += (ai - avr)**2
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=[int(i) for i in input().split()]
ave_a=round(sum(a)/n)
ans=0
for i in a:
    ans+=(i-ave_a)**2
print(ans)

Submission status: Accepted

Solution code:
N = int(raw_input())
a = map(int, raw_input().split())

# average = reduce(lambda x, y: x + y, a) / len(a)
average = int(round(sum(a) / float(len(a))))

cost = 0
for num in a:
    cost += (num - average) ** 2

print cost

Submission status: Accepted

Solution code:
N = int(raw_input())
a = map(int, raw_input().split())
avg = int(round(sum(a) / float(N)))
ans = 0
for ai in a:
    ans += (ai - avg) ** 2
print ans

Submission status: Accepted

Solution code:
n = int(input())
data = input().split(" ")
sum = 0
for i in data:
    sum += int(i)
r1 = int(round(sum / n, 0))
r2 = int(round(sum / n + 0.5, 0))
r3 = int(round(sum / n - 0.5, 0))

res = 0
keep = 0

def search(intvalue, data):
    sum = 0
    for i in data:
        k = abs(intvalue - int(i))
        if k == 0:
            continue
        sum += k ** 2
    return sum
res = [0 for i in range(3)]
res[0] = search(r1, data)
res[1]= search(r2, data)
res[2] = search(r3, data)
print(min(res))

Submission status: Accepted

Solution code:
#C
from math import ceil
N = int(input())
a = [int(i) for i in input().split()]
ave = float(sum(a)) / float(len(a))
print(sum([(a[i] - round(ave))**2 for i in range(N)]))

Submission status: Accepted

Solution code:
N = int(input())
a_list = list(map(int,input().split()))
b = 0
for ai in a_list:
        b += ai
average = int(round(b/N))

sigma = 0

for ai in a_list:
        sigma += (ai - average) ** 2

print(sigma)

Submission status: Accepted

Solution code:
from functools import reduce
n = int(input())
arr = list(map(int, input().split()))
s = reduce(lambda sum,x: sum+x, arr)
h = round(s/n)
m = 0
for a in arr:
    m += (a-h)**2
print(int(m))

Submission status: Accepted

Solution code:
N = int(input())
a_list = list(map(int,input().split()))
b = 0
for ai in a_list:
        b += ai
average = int(b/N)

sigma = 0
sigma2 = 0
sigma3 = 0
for ai in a_list:
        sigma += (ai - average) ** 2
        sigma2 += (ai - (average-1)) ** 2
        sigma3 += (ai - (average+1)) ** 2
print(min(sigma,sigma2,sigma3))

Submission status: Accepted

Solution code:
n = int(input())
a = input()
a = list(map(int, a.split()))

#print(a) 

x_min = round(sum(a) / n )

cost = sum([(x_min - i) * (x_min - i) for i in a])

print(cost)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys,copy,math,heapq,itertools as it,fractions,re,bisect,collections as coll

N = int(raw_input())
a = map(int, raw_input().split())
ans = 1e10
for b in xrange(-100, 101):
    tmp = 0
    for ai in a:
        tmp += (ai - b)**2
    ans = min(ans, tmp)
print ans

Submission status: Accepted

Solution code:
def read():
    return int(input())

def reads(sep=None):
    return list(map(int, input().split(sep)))

def main():
    n = read()
    a = reads()
    r = float('inf')
    for i in range(-100, 100+1):
        r = min(r, sum([abs(x-i)**2 for x in a]))
    print(r)

main()

Submission status: Accepted

Solution code:
N=int(input())
a=list(map(int,input().split()))
m,M=min(a),max(a)
min_cost=4000000
for i in range(m,M+1):
    cost=sum(map(lambda x:(x-i)*(x-i),a))
    min_cost=cost if cost<min_cost else min_cost
print(min_cost)

Submission status: Accepted

Solution code:
class BeTogether:
	def getMinimumValue(self,N,a):
		average = round(float(sum(a))/len(a),0)
		cost = [(a[i]-int(average))**2 for i in range(len(a))]
		print sum(cost)

if __name__ == "__main__":
	N = int(raw_input())
	x = [int(i) for i in raw_input().split()]
	b = BeTogether()
	b.getMinimumValue(N,x)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))

ans = int(1e9)
for i in range(-100, 101):
	res = sum(map(lambda x: (x-i)*(x-i), a))
	ans = min(ans, res)

print(ans)

Submission status: Accepted

Solution code:
N = int(raw_input())
alist = raw_input().split()
a = {}
for i in range(len(alist)):
  a[i] = int(alist[i])

minv = -101
minsum = 99999999
for i in range(-100, 101):
  sums = 0
  for k in range(len(a)):
    sums += (a[k] - i) * (a[k] - i)
  if sums < minsum:
    minv = i
    minsum = sums

print minsum

Submission status: Accepted

Solution code:
n=int(input())
s=[int(x) for x in input().split()]
s.sort()
a=s[0]
b=s[-1]
m=9999999999999
for i in range(a,b+1):
    sum=0    
    for j in s:
        sum+=(j-i)**2
    if sum < m:
        m=sum
        
print(m)

Submission status: Accepted

Solution code:
import numpy as np

N = input()
a = map(int,raw_input().split(' '))
a = map(lambda x:x+500,a)

avg = int(np.average(a)+0.5)
ret = np.linalg.norm(np.array(a)-avg)
ret = ret * ret
print(int(ret+0.5))

Submission status: Accepted

Solution code:
raw_input()
a = map(int, raw_input().split())
res = 10**9+7
for i in range(min(a), max(a)+1):
    tmp = sum([(i-x)**2 for x in a])
    if tmp < res:
        res = tmp
print res

Submission status: Accepted

Solution code:
def calc_cost(a, i):
    cost = 0
    for num in a:
        cost += (num - i) ** 2
    return cost


if __name__ == '__main__':
    N = int(input())
    a = [int(x) for x in input().split()]
    max_num = max(a)
    min_cost = calc_cost(a, a[0])
    for i in range(-100, 101):
        cost = calc_cost(a, i)
        if cost < min_cost:
            min_cost = cost
    print(min_cost)

Submission status: Accepted

Solution code:
N = input()
An = map(int, raw_input().split())

avg = sum(An)*1.0/len(An)
avg = int(round(avg))

avg_cost = 0
for a in An:
    avg_cost += (a-avg)**2

print avg_cost

Submission status: Accepted

Solution code:
N = input()
An = map(int, raw_input().split())

avg = sum(An)*1.0/len(An)
avg = int(round(avg))

avg_cost = 0
for a in An:
    avg_cost += (a-avg)**2

while True:
    l_avg = avg
    g_avg = avg
    l_avg -= 1
    g_avg += 1
    l_cost = 0
    g_cost = 0

    for a in An:
        l_cost += (a-l_avg)**2
        g_cost += (a-g_avg)**2

    if min(l_cost, avg_cost, g_cost) == avg_cost:
        ans = avg_cost
        break
    else:
        avg_cost = l_cost
        avg = l_cost


print ans

Submission status: Accepted

Solution code:
from functools import reduce

n = int(input())
a = list(map(int,input().split()))

end = max(a)
ansAry = []

b = min(a)
while(True):
    suma = []
    for i in range(0,len(a)):
        if b == a[i]:
            pass
        else:
            suma.append((a[i] - b)*(a[i] - b))

    try:
        ansAry.append(reduce(lambda x,y:x+y,suma))
    except:pass

    if b == end:
        break
    b += 1

if len(ansAry) > 0:
    print(min(ansAry))
else:
    print(0)

Submission status: Accepted

Solution code:
N = int(raw_input())

numbers = map(int, raw_input().split())

min_num = min(numbers)
max_num = max(numbers)
min_cost = None 


for result in xrange(min_num, max_num+1):
    cost = 0 
    for number in numbers:
        cost += (number - result)**2
    if min_cost:
        if min_cost >= cost:
            min_cost = cost 
    else:
        min_cost = cost
print min_cost

Submission status: Accepted

Solution code:
N=input()
a=map(int,raw_input().split( ))
mean=sum(a)/len(a)
meanU=mean+1

cost=0
costU=0

for i in a:
    cost+=(mean-i)*(mean-i)
    costU+=(meanU-i)*(meanU-i)

if cost < costU:
    print cost
else:
    print costU

Submission status: Accepted

Solution code:
input()
a = list(map(int, input().split()))
for i in range(min(a),max(a)+1):
    cost_min = 0 if i == min(a) else cost_min
    cost = 0
    for e in a:
        add_cost = abs((e - i)) ** 2
        cost_min += add_cost if i == min(a) else 0
        cost += add_cost if i != min(a) else 0
    if not i == min(a) and cost_min > cost:
        cost_min = cost
print(cost_min)

Submission status: Accepted

Solution code:
from sys import stdout, stdin

stdin.readline()
nums = stdin.readline().strip()

nums = [int(c) for c in nums.split()]

target = round(sum(nums) / float(len(nums)))

res = sum([abs(n - target) ** 2 for n in nums])

stdout.write(str(res))

Submission status: Accepted

Solution code:
n=int(raw_input())
a=map(int,raw_input().split())

mean=sum(a)/len(a)

import numpy as np
na=np.array(a)

print min(sum((na-int(np.mean(na)))**2),sum((na-int(np.mean(na)+1))**2))

Submission status: Accepted

Solution code:
n = float(input())
a = list(map(int,input().split()))
ave = int( round(sum(a)/n) )
ans = 0
for i in a:
    ans += (ave-i)**2
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split(' ')))

def cal(l, x):
    ret = 0
    for i in l:
        ret += (i - x) * (i - x)
    return ret

aver = int(sum(a) / len(a))

ans = 1e9
for i in range(aver - 3, aver + 3):
    f = cal(a, i)
    if f < ans:
        ans = f

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
A=[]
A=list(map(int,list(input().split())))
S=[0]*201

C=10**7
for k in range(0,201):
    for i in range(N):
        B=(k-100)-A[i]
        S[k]+=B*B
    if C>S[k]:
        C=S[k]
print(C)

Submission status: Accepted

Solution code:
import math
n=int(input())
a=list(map(int, input().split()))
ave = sum(a)/n
if math.ceil(ave)-ave > ave-math.floor(ave):
    t = math.floor(ave)
else:
    t = math.ceil(ave)
total = 0
for v in a:
    total += (t-v) ** 2
print(total)

Submission status: Accepted

Solution code:
raw_input()
a = [int(x) for x in raw_input().split()]
bsc = 1e10
for i in range(-101, 102):
  sc = 0
  for j in a:
    sc += (j-i)**2
  bsc = min(bsc, sc)
print bsc

Submission status: Accepted

Solution code:
def sqsum(a, x):
    s = 0
    for i in range(len(a)):
        s += (a[i]-x)**2
    return s
    
n = int(input())
a = [int(i) for i in input().strip().split()]
s = sum(a)
sq = []
for x in range(-100, 101):
    sq.append(sqsum(a, x))
print(min(sq))

Submission status: Accepted

Solution code:
N = input()
S = map(float, raw_input().split())

a = round(sum(S)/N)
c = 0
for i in S:
    c += (i-a)**2
print int(c)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
cost_list = []
cost = 0
for i in range(-100, 101):
    cost = 0
    for j in a:
        cost += (j - i) ** 2
    cost_list.append(cost)
print(min(cost_list))

Submission status: Accepted

Solution code:
from __future__ import division
n = input()
a = map(int, raw_input().split(' '))
tot = 0
ave = round(reduce((lambda x,y: x+y),a) / n)
for x in a:
    tot += (x-ave)**2
print int(tot)

Submission status: Accepted

Solution code:
input()
A = map(int, raw_input().split())
print min(sum((e-x)**2 for e in A) for x in range(-100,101))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
result = 2 ** 60
for y in range(-100, 101):
    cost = 0
    for x in A:
        cost += (x - y) ** 2
    result = min(result, cost)
print (result)

Submission status: Accepted

Solution code:
def cost(a, x):
    c = 0
    for ai in a:
        c += (ai - x) * (ai - x)
    return c

N = int(input())
a = list(map(int, input().split()))
avg = 0
for n in a:
    avg += n
if avg % N == 0:
    print(cost(a, avg // N))
else:
    print(min(cost(a, avg // N), cost(a, avg // N + 1)))

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
arr = np.array(list(map(int,input().split())))
rmean = round(sum(arr)/len(arr))
cost = sum((arr-rmean)**2)
print(int(cost))

Submission status: Accepted

Solution code:
N = input()
a = map(int, raw_input().split())
m = int(round(sum(a)/float(N)))
print sum(map(lambda x:(x-m)**2, a))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
mn = 1 << 30
for x in range(-100, 101):
    v = 0
    for a in A:
        v += (a - x) ** 2
    mn = min(v, mn)
print(mn)

Submission status: Accepted

Solution code:
import sys

#sys.stdin = open('c_4.txt', 'r')
min_cost = sys.maxint
N = int(raw_input())
num_list = map(int, raw_input().split())

for x in range(-100, 101, 1):
	new_cost = 0
	for y in range(N):
		new_cost += (num_list[y] - x) ** 2
	min_cost = min(new_cost, min_cost)

print str(min_cost)

Submission status: Accepted

Solution code:
n = int(input())

list_n = [int(n) for n in input().split(' ')]

A = 0

for n in list_n:
        A += (100-n)**2

for n in range(-100,100):
        B = 0
        for num in list_n:
                B += (n-num)**2
        if A > B:
                A = B

print(A)

Submission status: Accepted

Solution code:
# coding: utf-8
import sys
     
n = int(raw_input())

list_num = [int(num) for num in raw_input().split(' ')]

cost = 0
for num in list_num:
    cost += (100-num)**2
 
for ans in range(-100, 100):
    cost_tmp = 0
    for num in list_num:
        cost_tmp += (ans-num)**2
    if cost > cost_tmp:
        cost = cost_tmp

print cost

Submission status: Accepted

Solution code:
#coding: utf-8
#ABC043C
import sys

n=int(raw_input())
l=map(int,raw_input().split())
res=float("inf")
cost=0
for i in xrange(-100,101):
 for j in l:
  cost+=(j-i)**2
 if cost<res:
  res=cost
 cost=0

print res

Submission status: Accepted

Solution code:
N = input()
l = list(map(int,list(input().split())))
cost_min = (200**2)*100
cost = 0
for i in range(-100,101):
    for j in l:
        cost += (j - i)**2
        
    if cost < cost_min:
        cost_min = cost
    cost = 0
print(cost_min)

Submission status: Accepted

Solution code:
import math as m
 
def main():
    n  = raw_input()
    a = map(float,raw_input().split())
    
    avg =  m.ceil( sum(a)/len(a) )
    avg2 =  m.floor( sum(a)/len(a) )

    cost = 0
    for i in a:
        cost += (avg - i) ** 2
    
    cost2 = 0
    for i in a:
        cost2 += (avg2 - i) ** 2
    
    print int(min([cost,cost2]))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 13 20:59:33 2016

@author: kt
"""
max=-1000
min=1000
N=input()
i=0
minx=0
l = map(int, raw_input().split())
sum=0
for var in l:
     sum+=var
sum=float(sum)/float(N)
isum=int(sum)
if sum==isum:
    minx=sum
elif isum+1-sum >sum-isum:
    minx=isum
else: 
    minx=isum+1
result=0
for var in l:
    result+=(var-minx)*(var-minx)
print int(result)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int,input().split()))

S = [sum((i-a)**2 for a in A) for i in range(min(A),max(A)+1)]
print(min(S))

Submission status: Accepted

Solution code:
import math,sys
n = input()
a = map(int, raw_input().split())
if len(set(a)) == 1:
    print 0
    sys.exit()
res = []
for af in range(-100,101):
    ans = 0
    for i in a:
        ans += (i-af)**2
    res.append(ans)
print min(res)

Submission status: Accepted

Solution code:
n=int(input())
I=[int(i) for i in input().split(" ")]
MAX=max(I)
MIN=min(I)

ans=10000000000000000000000000000
for x in range(MIN,MAX+1):
    SUM=0
    for i in I:
        SUM+=(i-x)**2
    if SUM<ans:
        ans=SUM
print(ans)

Submission status: Accepted

Solution code:
from math import floor

out = 0
N = int(input())
a = list(map(int, input().split()))

t = int(round(sum(a) / N))
for i in a:
    out += (i - t)**2

print(int(out))

Submission status: Accepted

Solution code:
import numpy as np
N = int(raw_input())
a = np.array(map(int, raw_input().split()))
cost = []
for y in xrange(a.min(), a.max() + 1):
    cost.append(np.sum((a - y) ** 2))
print min(cost)

Submission status: Accepted

Solution code:
n=int(raw_input())
N=map(int,raw_input().split())
add=count=0
b=sum(N)/n
a=b+1
for i in N:
    add+=(a-i)**2
    count+=(b-i)**2
    
print min(add,count)

Submission status: Accepted

Solution code:
n = int(input())
data = list(map(int,input().split()))
mini = 100000
maxi = -100000
for i in range(n):
    if mini > data[i]:
        mini = data[i]
    if maxi < data[i]:
        maxi = data[i]
mincost = 1000000000
ans = 0
for i in range(mini,maxi+1):
    cost = 0
    for j in range(n):
        cost += (data[j] - i)**2
    if mincost > cost:
        ans = i
        mincost = cost
print(mincost)

Submission status: Accepted

Solution code:
import numpy as np
import math
N = int(input())

a = np.array(list(map(int, input().split())))

c = round(a.mean())

res = int(0)

for i in a:
    res += (c - i) ** 2

print(int(res))

Submission status: Accepted

Solution code:
# coding: utf-8

count = int(input())
numbers = [int(i) for i in input().rstrip().split(" ")]
average = sum(numbers) / count

def calc_single_cost(x, y):
    return (x - y) ** 2

def calc_total_cost(avg, targets):
    cost = 0
    for x in targets:
        cost += calc_single_cost(avg, x)
    return cost

cost = []
for candidate in [int(average), int(average) + 1]:
    cost.append(calc_total_cost(candidate, numbers))

print(min(cost))

Submission status: Accepted

Solution code:
n = int(input())
retu = input().split()
for i in range(0,n):
    retu[i] = int(retu[i])
retuS = sorted(retu)
resultA = []
for j in range(retuS[0],retuS[n-1]+1):
    result = 0
    for k in retu:
        result = result + (k - j) * (k - j)
    resultA.append(result)
resultA.sort()
print(resultA[0])

Submission status: Accepted

Solution code:
#! /usr/bin/env python3
input()
a = list(map(int, input().split()))
s = sorted(a)
cost = []
for y in range(s[0], s[-1]+1):
    cost += [sum((x - y) * (x - y) for x in a)]
print(sorted(cost)[0])

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int,input().split()))
b = 0
for i in range(N):
	b += a[i]

if type(b/N) == int:
	m = b/N
	c = 0
	for i in range(N):
		c += (a[i] - m)**2
	print(int(c))
else:
	m = int(b/N) + 1
	n = int(b/N)
	c = 0
	d = 0
	for i in range(N):
		c += (a[i] - m)**2
		d += (a[i] - n)**2
	e = min(c,d)
	print(e)

Submission status: Accepted

Solution code:
n = int(raw_input())
a = map(int, raw_input().split())

sum = sum(a)
if(sum >= 0):
    ave = int(float(sum) / n + 0.5)
else:
    ave = int(float(sum) / n - 0.5)   

cost = 0
for item in a:
    cost += pow(abs(item - ave), 2)
    

print cost,

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int, input().split()))
avg=round(sum(a)/n)
print(sum(map(lambda x:(x-avg)*(x-avg), a)))

Submission status: Accepted

Solution code:
def solve(a):
  mean = round(sum(a) / len(a))
  return sum(((aa - mean) ** 2 for aa in a))

input()
print(solve(list(map(int, input().split(' ')))))

Submission status: Accepted

Solution code:
IS=float('inf')
xy=[(1,0),(-1,0),(0,1),(0,-1)]
bs=[(-1,-1),(-1,1),(1,1),(1,-1)]
def niten(a,b): return abs(a-b) if a>=0 and b>=0 else  a+abs(b) if a>=0 else abs(a)+b if b>=0 else abs(abs(a)-abs(b))
def gcd(a,b): return a if b==0 else gcd(b,a%b)
def lcm(a,b): return a*b/gcd(a,b)
def euclid_dis(x1,y1,x2,y2): return ((x1-x2)**2+(y1-y2)**2)**0.5
def choco(xa,ya,xb,yb,xc,yc,xd,yd): return 1 if abs((yb-ya)*(yd-yc)+(xb-xa)*(xd-xc))<1.e-10 else 0

n=int(raw_input())
#n,k=map(int,raw_input().split())
l=map(int,raw_input().split())
ans=IS
for i in range(-100,101,1):
    tmp=0
    for j in l:
        tmp+=(i-j)**2
    ans=min(ans,tmp)
print ans

Submission status: Accepted

Solution code:
input()
a = [int(i) for i in raw_input().split()]
b = []
for i in range(min(a), max(a) + 1):
    b.append(sum(pow(j - i, 2) for j in a))
print min(b)

Submission status: Accepted

Solution code:
n = int(input())
li = list(map(int, input().split()))
minli = min(li)
maxli = max(li)
somme = []
for i in range(minli,maxli+1):
	s = 0
	for j in range(len(li)):
		s += (li[j] - i)**2
	somme.append(s)

print(min(somme))

Submission status: Accepted

Solution code:
n=int(input())
ans1=0
ans2=0

a=list(map(int,input().split()))
avg=sum(a)/n
m1=int(avg)
m2=m1+1
for i in range(n):
    ans1 += (a[i]-m1)**2
    ans2 += (a[i]-m2)**2

print(min(ans1,ans2))

Submission status: Accepted

Solution code:
n = int(raw_input())
a = map(int,raw_input().split())

ave = float(sum(a))/float(len(a))
if ave - int(ave) >= 0.5:
    ave = int(ave)+1
else:
    ave = int(ave)

cost = 0

for var in a:
    cost = cost + (var-ave)**2

print cost

Submission status: Accepted

Solution code:
import re
import doctest
import functools

class IO_for_Contest(object):
    @staticmethod
    def my_input():
        #return raw_input()
        return input()

    @staticmethod
    def read_from_input():
        n = IO_for_Contest.read_int()
        return IO_for_Contest.read_n_int(n)

    @staticmethod
    def read_line():
        return IO_for_Contest.my_input().strip()

    @staticmethod
    def read_int():
        return int(IO_for_Contest.my_input().strip())

    @staticmethod
    def read_n_int(n):
        return list(map( \
                int, \
                re.split('\s+', IO_for_Contest.my_input().strip())))[ : n]
def solve():
    a = IO_for_Contest.read_from_input()
    print(think_min_cost_for(a))

def think_min_cost_for(arr):
    """
    >>> think_min_cost_for([4, 8])
    8
    """
    ceil = 100
    floor = -100
    cost = []
    for to in range(floor, ceil + 1):
        cost.append(think_min_cost_about(arr, to))
    return min(cost)

def think_min_cost_about(arr, to):
    diff = list(map(lambda x: x - to, arr))
    diff_2 = list(map(lambda x: x ** 2, diff))
    return sum(diff_2)

if __name__ == '__main__':
    #doctest.testmod()
    solve()

Submission status: Accepted

Solution code:
n = input()
a = raw_input()
a = [int(i) for i in a.split()]
min_cost = 1000000000
for i in range(min(a),max(a)+1):
    cost = 0
    for j in a:
        cost += (i-j)**2

    if(cost<min_cost):
        min_cost = cost

print min_cost

Submission status: Accepted

Solution code:
a = input()
l = map(int, raw_input().split())

s = 100000000
ma = max(l)
mi = min(l)

for i in range(mi, ma + 1):
	t = 0
	for j in l:
		t += (j - i) ** 2
	if t < s:
		s = t

print s

Submission status: Accepted

Solution code:
n = int(raw_input())
a = map(int, raw_input().split())
ave = round(float(sum(a))/n)
print int(sum([(x-ave)**2 for x in a]))

Submission status: Accepted

Solution code:
n = int(raw_input())
t = [int(i) for i in raw_input().split()]

miin = 1000000000000000000
for tar in range(-100, 101):
    s = 0
    for e in t:
        s += (tar-e)*(tar-e)
    if s<miin:
        miin = s
print miin

Submission status: Accepted

Solution code:
def fn(m):
    res = 0
    for a in A:
        res += (a - m) ** 2
    return res


N = int(input())
A = list(map(int, input().split()))
mean = int(sum(A) // N)
lo = fn(mean)
hi = fn(mean + 1)
print(min(lo, hi))

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input(split())))

ans = 1e9
for p in a:
  sub = 0
  for q in a:
    sub += pow(p - q, 2)
  ans = min(ans, sub)

print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
a = list(map(int,input().split()))
b,c= 0,0
s =[]
a.sort()
for i in range(0,N):
  b += a[i]
for i in range(0,N):
  c += a[i]**2
for i in range(a[0],a[N-1]):
  s.append(N*(i**2)-2*b*i+c)
m = min(s)
print(m)