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 6 s 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 2 s 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 1 s.       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 4 s 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 .

Solution (Python):

def LI(): return list(map(int, input().split()))
def II(): return int(input())
def LS(): return input().split()
def S(): return input()
def LIR(n): return [LI() for i in range(n)]


"""
s = S()
if s[0] != "A":
    print("WA")
else:
    count = []
    for i in range(2,len(s)-1):
        if s[i] == "C": count.append(i)
    if len(count) != 1:
        print("WA")
    else:
        for i in range(1,len(s)):
            if i not in count:
                if s[i] == s[i].upper():
                    print("WA")
                    quit()
        print("AC")
"""


"""
n,m,k = LI()
for i in range((n+1)//2):
    if (k - m*i) % (n - 2*i) == 0 and (k-m*i)//(n-2*i) >= 0 and (k-m*i)//(n-2*i) <= m:
        print("Yes")
        quit()
print("No")
"""


"""
from collections import defaultdict
n,k = LI()
d = defaultdict(int)
for i in range(n):
    a,b = LI()
    d[a] += b
s = list(d.items())
s.sort(key = lambda x:x[0])
su = 0
for i in range(len(s)):
    su += s[i][1]
    if su >= k:
        print(s[i][0])
        break
"""


"""
n,k = LI()
d = LI()
lis = [0,1,2,3,4,5,6,7,8,9]
for i in d:
    if i in lis:
        lis.remove(i)
n = list(str(n))
i = 0
while i < len(n):
    while int(n[i]) not in lis and n[i] != "10":
        n[i] = str(int(n[i])+1)
    if n[i] == "10":
        if i == 0:
            n[i] = "0"
            n.insert(i,"1")
        else:
            n[i-1] = str(int(n[i-1])+1)
            n[i] = "0"
            i -= 1
        i -= 1
    i += 1
for i in n:
    print(i,end = "")
print()
"""

n = II()
a = LI()
ans = float("inf")
for i in range(min(a),max(a)+1):
    cos = 0
    for j in a:
        cos += (i-j)**2
    ans = min(ans, cos)
print(ans)

Solution (Python):
a=int(input())
b=list(map(int,input().split()))
c=sum(b)//a
d=[0,0]
for i in range(a):
    d[0]+=(b[i]-c)**2
    d[1]+=(b[i]-c-1)**2
print(min(d))

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
sum = 0
for i in range(N):
    sum += A[i]
av = round(sum / N)
ans = 0
for i in range(N):
    ans += (A[i] - av)** 2
print(ans)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
m=round(sum(a)/n)
ans=0
for i in range(n):
  ans+=(a[i]-m)**2
print(ans)

Solution (Python):
a = int(input())
ar = list(map(int,input().split(" ")))
br = []
for i in range(min(ar),max(ar)+1):
    count = 0
    for r in ar:
        count += (i - r) ** 2
    br.append(count)
print(min(br))

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

s = sum(a)
ave_new = round(s/n)

ans = 0
for i in a:
    ans += (i-ave_new)**2

print(ans)

Solution (Python):
N = int(input())
A = [int(_) for _ in input().split()]
ans = 0
print(min([sum([(a - i)**2 for a in A]) for i in range(-100, 101)]))

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
ave1 = int(sum(a)/N+0.5)
ave2 = ave1-1
ave3 = ave1+1
ans1, ans2, ans3 = 0, 0, 0
for i in range(N):
    ans1 += (ave1 - a[i])**2
    ans2 += (ave2 - a[i])**2
    ans3 += (ave3 - a[i])**2

print(min(ans1, ans2, ans3))

Solution (Python):
N=int(input())
A=[int(i) for i in input().split()]
ans=10**9
def f(x,A):
    s=0
    for a in A:
        s+=pow(abs(x-a),2)
    return s
for i in range(201):
    x=-100+i
    t=f(x,A)
    if ans>t:
        ans=t
print(ans)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
A = sorted(A)
ANS = []
for i in range(A[0],A[-1]+1) :
    p = 0
    for j in range(N) :
        p += (i-A[j])**2
    ANS.append(p)
ans = min(ANS)
print(ans)

Solution (Python):
import numpy as np
N=int(input())
a=list(map(int,input().split()))

a=np.array(a)
min_s = 10**29
for i in range(201):
    s=np.sum((a-np.ones(N)*(i-100))**2)
    min_s = min(int(s),min_s)
print(min_s)

Solution (Python):
def main():
    N = int(input())
    l = list(map(int, input().split()))
    ans = 10000000
    min_l = min(l)
    max_l = max(l)
    for y in range(min_l, max_l+1):
        temp = 0
        for x in l:
            temp += (x - y) ** 2
        ans = min(ans, temp)
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
def abss(x, y):
    return abs(x-y)**2

def main():
    N = int(input())
    l = list(map(int, input().split()))
    ans = 10000000
    min_l = min(l)
    max_l = max(l)
    for y in range(min_l, max_l+1):
        temp = 0
        for x in l:
            temp += abss(x, y)
        ans = min(ans, temp)
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
ans = 10 ** 9
for y in range(-100, 101):
    ans = min(ans, sum((x - y) ** 2 for x in a))
print(ans)

Solution (Python):
n = int(input())
list_a = list((map(int, input().split())))
sum = 0
for number in list_a:
    sum += number
    ave = sum/n
if ave - int(ave) <= int(ave) + 1 - ave:
    ave = int(ave)
else:
    ave = int(ave) + 1
ans = 0
for number in list_a:
    ans += (number - ave)**2

print(int(ans))

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
sum_list = []
k = 0
for i in range(-100, 101):
    sum_list.append(0)
    for j in range(N):
        sum_list[k] += pow(i - a[j], 2)
    k += 1
print(min(sum_list))

Solution (Python):
def cost(x,y):
    return (x-y)*(x-y)

N = int(input())
A = list(map(int, input().split()))

ans = 1000000000
tmp = 0
for i in range(min(A),max(A)+1):
    tmp = 0
    for a in A:
        tmp += cost(a,i)
    if ans > tmp:
        ans = tmp
print(ans)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
ans = float('inf')
for i in range(min(a),max(a)+1):
    l = [(i-k)**2 for k in a]
    ans = min(ans,sum(l))
print(ans)

Solution (Python):
from math import floor

N = int(input())
As = list(map(int, input().split()))

aveA = sum(As) / N
x = floor(aveA + 0.5)

ans = 0
for A in As:
    ans += (A - x) ** 2

print(ans)

Solution (Python):
from math import floor

N = int(input())
As = list(map(int, input().split()))

aveA = sum(As) / N
x = floor(aveA + 0.5)

ans = 0
for A in As:
    ans += (A - x) ** 2

print(ans)

Solution (Python):
n=int(input())
res = float('inf')
a=list(map(int,input().split()))
for i in range(min(a),max(a)+1):
    tmp = 0
    for j in a:
        tmp += (i-j)**2
    res = min(res,tmp)
print(res)

Solution (Python):
import math
def getinputdata():

    
    array_result = []
    
    data = input()
    
    array_result.append(data.split(" "))

    flg = 1

    try:

        while flg:

            data = input()

            if(data != ""):
                
                array_result.append(data.split(" "))

            else:

                flg = 0
    finally:

        return array_result

def round(x):
    return (x*2+1)//2

arr_data = getinputdata()

n = int(arr_data[0][0])

arr=[int(arr_data[1][x]) for x in range(n)]


if len(set(arr))==1:
    
    print(0)

else:

    target=sum(arr)//len(arr) if sum(arr)%len(arr)==0 else round(sum(arr)/len(arr))

    mysum=0

    for v in arr:

        mysum+=(v-int(target))**2

    print(mysum)

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
ans = pow(100, 9)
for i in range(-100, 101):
	temp = 0
	for j in range(len(a)):
		temp += pow(i - a[j], 2)
	ans = min(ans, temp)
print(ans)

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
z = sum(a) / N
if z - z // 1 >= z // 1 + 1 -z:
	x = int(z // 1 + 1)
else:
	x = int(z // 1)
s = 0
for i in range(N):
	s += (a[i] - x) ** 2
print(s)

Solution (Python):
n=int(input())
a=[int(i) for i in input().split()]
b=round(sum(a)/len(a))
c=0
for i in a:
    c=c+(i-b)**2
print(c)

Solution (Python):
n = int(input())

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

ma = max(arr)
mi = min(arr)

ans = float("inf")

for i in range(mi,ma+1):
	count = 0
	for j in range(n):

		count += (i - arr[j]) * (i - arr[j])

	ans = min(ans,count)

print ans

Solution (Python):
N = int(input())
a = [int(i) for i in input().split()]

total = sum(a)
ave = int(round(total/N))

ans = 0
for i in range(N):
    ans = ans + (a[i]-ave)**2

print(ans)

Solution (Python):
n=int(input())

l = input()
l = [int(i) for i in l.split()]

e = sum(l)

t = round(e/n)
ans = 0 
for i in l :
    ans = ans + (t-i)**2
print(ans)

Solution (Python):
n = input()
a = map(int, raw_input().split())
x = (sum(a) + len(a) / 2) / len(a)
print sum((i - x) ** 2 for i in a)

Solution (Python):
import numpy as np
N = int(input())
Ary = np.array(list(map(int, input().split(' '))))
res = float('INF')
for i in range(-100, 101):
    res = min(res, np.sum(np.power(Ary - i,2)))
print(res)

Solution (Python):
import math
n= int(input())
lis = list(map(int,input().split()))
a = math.floor(sum(lis)/n)
b = math.ceil(sum(lis)/n)
cou = 0
co = 0
for i in range(n):
  cou += abs(lis[i] - a) ** 2
  co += abs(lis[i] - b ) ** 2
print(min(cou,co))

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
cost = 100000000
for i in range(-100, 101):
    temp = 0
    for a_i in a:
        temp += (a_i-i)**2
    cost = min(cost, temp)
print(cost)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))


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


ans = min([sum([cost(A[i], a) for i in range(N)]) for a in range(-100, 101)])

print(ans)

Solution (Python):
N = int(input())

A = list(map(int,input().split()))[:N]

a = int(round(sum(A)/N))

cost = 0
for i in A:
    cost += (i-a)*(i-a)
    
print(cost)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
b = max(a)
c = min(a)
ans = 99999999999999999999999999999999999999999999999999999
for i in range(c,b+1):
    count = 0
    for j in range(n):
        count += (i-a[j])**2
    ans = min(ans,count)

print(ans)

Solution (Python):
N=int(input())
a=[int(i) for i in input().split()]
av=sum(a)//N
x=sum([(a[i]-av-1)**2 for i in range(N)])
y=sum([(a[i]-av)**2 for i in range(N)])
z=sum([(a[i]-av+1)**2 for i in range(N)])
print(min([x,y,z]))

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

cost = 10**7
for l in range(min(a), max(a)+1):
    tmp = 0
    for i in a:
        tmp += (i-l)**2
    cost = min(cost, tmp)

print(cost)

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

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
ave=sum(a)//n
mi=10**15
c=0
for i in a:
  c+=(i-ave)**2
mi=min(mi,c)
ave+=1
c=0
for i in a:
  c+=(i-ave)**2
mi=min(mi,c)
ave-=2
c=0
for i in a:
  c+=(i-ave)**2
mi=min(mi,c)
print(mi)

Solution (Python):
import math
n = int(input())
lis = list(map(int,input().split()))
co = 0
cou = 0
a = math.floor(sum(lis)/n)
b = math.ceil(sum(lis)/n)
for i in range(n):
  co += (abs(a-lis[i])) ** 2
  cou += (abs(b-lis[i])) ** 2
print(min(co,cou))

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

Solution (Python):
n=int(input())
a=sorted(list(map(int,input().split())))
ans=float('inf')
for i in range(a[0],a[-1]+1):
    tot=0
    for j in range(len(a)):
        tot+=(a[j]-i)**2
    ans=min(ans,tot)
print(ans)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
x=int(sum(a)/len(a)+(0.5 if sum(a)/len(a)>0 else -0.5))

ans=0
for m in a:
    ans+=(m-x)**2
print(ans)

Solution (Python):
def solve():
    N = int(input())
    a = list(map(int, input().split()))
    ans = float("inf")
    for i in range(min(a),max(a)+1):
        cost = 0
        for a_i in a:
            cost += abs(i-a_i)**2
        ans = min(ans, cost)
    print(ans)
    
solve()

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
y = round(sum(a)/n)
print(sum(map(lambda x: (x-y)**2,a)))

Solution (Python):

_ = int(input())
a = list(map(int, input().split()))
result = float('inf')
for y in range(min(a), max(a) + 1):
    acc = 0
    for x in a:
        acc += (x - y) ** 2
    result = min(result, acc)
print(result)

Solution (Python):


import math

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

aves = [sum(a) // N, math.ceil(sum(a) / N)]
ans = float('inf')
for am in aves:
    acc = 0
    for ai in a:
        acc += abs(am - ai) ** 2
    ans = min(ans, acc)

print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))

ave = round(sum(A) / N)

print(sum(map(lambda n: (n - ave) ** 2, A)))

Solution (Python):



"""微分
"""

import math

def cost(A, x):
    return sum((x-a)**2 for a in A)

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

    x = sum(A) / N;
    ans = min(cost(A, math.ceil(x)), cost(A, math.floor(x)))

    print(ans)

if __name__ == "__main__": main()

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

total_cost = float('inf')
for n in range(-100, 101):
    cost = 0
    for i in range(N):
        cost += (a[i] - n) ** 2
    total_cost = min(total_cost, cost)
print(total_cost)

Solution (Python):
N = input()
an = map(int,raw_input().split(" "))
ans = 10**9

for i in range(-100,101):
    x = 0
    for a in an:
        x += (a - i) ** 2
    ans = min(x,ans)
print ans

Solution (Python):
def main(n, ns):
    if len(set(ns)) == 1:
        return 0
    
    min_v  = min(ns)
    max_v  = max(ns)
    answer = pow(100, 3)
    for i in range(min_v, max_v + 1):
        cost = 0
        for j in ns:
            if i == j:
                continue
            cost += pow((j - i), 2)
        answer = min(answer, cost)
    return answer
 
 
if __name__ == '__main__':
    n = int(input())
    ns = [int(e) for e in input().split()]
    print(main(n, ns))

Solution (Python):
def main(n, ns):
    if len(set(ns)) == 1:
        return 0
    
    min_v  = -100
    max_v  = 101
    answer = pow(100, 3)
    for i in range(min_v, max_v):
        cost = sum([(n - i) ** 2 for n in ns])
        answer = min(answer, cost)
    return answer
 
 
if __name__ == '__main__':
    n = int(input())
    ns = [int(e) for e in input().split()]
    print(main(n, ns))

Solution (Python):


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

a.sort()

ans = 1000000007
for x in range(a[0], a[n - 1]):
    keep = 0
    for i in range(n):
        keep += (a[i] - x) * (a[i] - x)
    ans = min(keep, ans)

if a[0] == a[n - 1]:
    ans = 0
print(ans)

Solution (Python):
n = int(input())
A = [int(_) for _ in input().split()]
ave = round(sum(A)/len(A))
ans = sum([(a-ave)*(a-ave) for a in A])

print(ans)

Solution (Python):
n = int(input())
a = [int(i) for i in input().split(' ')]
ave = sum(a) / n
if (ave - int(ave)) < 0.5:
    ave = int(ave)
else:
    ave = int(ave) + 1
res = 0
for i in a:
    res += (i - ave)**2
print(res)

Solution (Python):
import numpy as np
import sys


a = int(sys.stdin.readline())

n = [int(i) for i in sys.stdin.readline().split()]

avg = np.mean(n)
p = round(avg)

print(int(sum([(i-p)*(i-p) for i in n])))

Solution (Python):



import math
import sys

def main() :
	lines=sys.stdin.readlines()
	N=int(lines[0])
	l=map(int, lines[1].split(" "))
	x=int(round(1.0*sum(l)/N))
	print sum(pow(a-x, 2) for a in l)

if __name__=="__main__" :
	main()

Solution (Python):



import sys

def main() :
	lines=sys.stdin.readlines()
	N=int(lines[0])
	l=map(int, lines[1].split(" "))
	
	af=1.0*sum(l)/N
	ai=int(af)
	
	x=min(ai, ai+1, key=lambda a: abs(a-af))
	
	print sum((a-x)**2 for a in l)

if __name__=="__main__" :
	main()

Solution (Python):

a = int(raw_input())
b_list = map(int, raw_input().split())

loss_list = []
for i in range(-100, 101):
    loss = [(i - b) * (i - b) for b in b_list]
    loss_list.append(sum(loss))
print min(loss_list)

Solution (Python):
n = int(raw_input())
values = list(map(int, raw_input().split()))
best = 200 ** 3
for target in range(-100, 101):
    cost = 0
    for value in values:
        cost += (target - value) ** 2
    best = min(best, cost)
print(best)

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
ret = N * 200**2
for i in range(-100, 101):
    cost = 0
    for j in range(N):
        cost += (i - a[j])**2
    ret = min(ret, cost)
print(ret)

Solution (Python):



from __future__ import division, print_function, absolute_import, unicode_literals

N = int(input())
A = list(map(int, input().split()))

minimum_score = float('inf')
for i in range(-100, 101):
    score = 0
    for a in A:
        score += (a - i) ** 2
    minimum_score = min(minimum_score, score)
print(minimum_score)

Solution (Python):
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)

Solution (Python):
n = input()
a = map(int,raw_input().split())
avg = int(round(float(sum(a))/n))

d = 0
for i in a:
    d += (i - avg) ** 2
print d

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf = 10**9

n = int(input())
a = list(map(int, input().split()))
l = len(a)
r = inf
for i in range(-101, 101, 1):
    c = 0
    for ai in a:
        c += (ai - i) ** 2
    if r > c:
        r = c
print(r)

Solution (Python):
n=int(raw_input())
l=map(int,raw_input().split())

sm,avg,cost1,cost2=0,0,0,0

for el in l:
	sm+=el
avg1=sm//n
avg2=avg1+1

for el in l:
	cost1+=(avg1-el)**2
	cost2+=(avg2-el)**2

print min(cost2,cost1)

Solution (Python):

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

mean = sum(a)/len(a)
upper = mean+1
lower = mean-1

cost = sum(map(lambda x: (x - mean)*(x - mean), a))
costU = sum(map(lambda x: (x - upper)*(x - upper), a))
costL = sum(map(lambda x: (x - lower)*(x - lower), a))

print min(cost,costU,costL)

Solution (Python):
n = input()
A = map(int, raw_input().split())

m = sum(A) / float(n)

print int(sum((v - round(m))**2 for v in A))

Solution (Python):


def cost(n, a, m):

  hihumi = 0  

  for i in range(n):
    hihumi += (a[i] - m) ** 2

  return hihumi

N = int(raw_input())
a = map(int, raw_input().split())

ans = 1000000000000

for i in range(min(a), max(a) + 1):
  if (ans > cost(N, a, i)):
    ans = cost(N, a, i)

print ans

Solution (Python):
import sys
import math
line1 = sys.stdin.readline()
line2 = sys.stdin.readline()

n = int(line1)
a_array = [int(x) for x in line2.split()]
y = int(round(float(reduce(lambda x, y: x + y, a_array)) / n))
print reduce(lambda x, y: x+y, [(x - y) ** 2 for x in a_array])

Solution (Python):
import math

N = int(input())
m = [ int(i) for i in input().strip().split(" ")]

k_tmp = float(sum(m))/len(m)

k1 = int(math.ceil(k_tmp))
k2 = int(math.floor(k_tmp))

res1 = sum([(x-k1) * (x-k1) for x in m])
res2 = sum([(x-k2) * (x-k2) for x in m])
res = 0
if res1 > res2:
    res = res2
else:
    res = res1
print(res)

Solution (Python):
n = int(raw_input())
a = map(int, raw_input().split())
y = int(round(1.0 * sum(a) / n))
print sum((y - x) ** 2 for x in a)

Solution (Python):



def cost(x, y):
    return (x - y) * (x - y)


def min_total_cost(xs):
    def total_cost(target):
        return sum(cost(x, target) for x in xs)
    ans = min(total_cost(t) for t in range(min(xs), max(xs) + 1))
    return ans


def main():
    _ = input()
    xs = list(map(int, input().split()))
    print(min_total_cost(xs))


if __name__ == '__main__':
    main()

Solution (Python):
n=input()
a=map(int,raw_input().split())
ans=10**9
for j in xrange(-100,101):
    asum=0
    for i in xrange(n):
        asum+=(a[i]-j)**2
    ans=min(ans,asum)
print(ans)

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

ans = 10 ** 10
for x in range(-100, 101):
    calc = 0
    for y in a:
        calc += (x - y) ** 2
    ans = min(ans, calc)

print(ans)

Solution (Python):
import bisect
import sys
import math
import itertools
sys.setrecursionlimit(10000)

INF = float('inf')


def i():
    return int(raw_input())
def ii():
    return map(int,raw_input().split(" "))
def s():
    return raw_input()
def ss():
    return raw_input().split(" ")
def slist():
    return list(raw_input())


def join(s):
    return ''.join(s)


def piter(n,m):
    return itertools.permutations(n,m)
def citer(n,m):
    return itertools.combinations(n,m)


def modc(a,b,m):
    c = 1
    for i in xrange(b):
        c = c * (a - i) % m
        c = c * modinv(i + 1,m) % m
    return c
 
def gcd(a, b):
    (x, lastx) = (0, 1)
    (y, lasty) = (1, 0)
    while b != 0:
        q = a // b
        (a, b) = (b, a % b)
        (x, lastx) = (lastx - q * x, x)
        (y, lasty) = (lasty - q * y, y)
    return (lastx, lasty, a)
 
def modinv(a, m):
    (inv, q, gcd_val) = gcd(a, m)
    return inv % m


def index(a, x):
    
    i = bisect_left(a, x)
    if i != len(a) and a[i] == x:
        return i
    return -1


def memoize(f):
    cache = {}
    def helper(*args):
        if args not in cache:
            cache[(args)] = f(*args)
        return cache[args]
    return helper

@memoize
def nck(a,b,m):
    b = min([a-b,b])
    if (b>a or b<0 or a<0):
        return 0
    elif a == 0:
        return 1
    return (nck(a-1,b-1,m)+nck(a-1,b,m)) % m



n=i()
a=ii()
cost=0
ave=int(round(float(sum(a))/float(n)))
for i in range(n):
    cost+=(a[i]-ave)**2
print int(cost)

Solution (Python):



N = int(input())
As = list(map(int,input().split()))

mn_cost = 1e10
for v in range(-100,101):
    cost = sum((v - a) ** 2 for a in As)
    if cost < mn_cost:
        mn_cost = cost
print(mn_cost)

Solution (Python):
import math
N = int(input())
a = [int(i) for i in input().strip().split(" ")]

a_h = math.ceil(sum(a) / len(a))
a_l = math.floor(sum(a) / len(a))

d_h = sum([(a_h-i)**2 for i in a])
d_l = sum([(a_l-i)**2 for i in a])

print(min(d_h,d_l))

Solution (Python):
while 1:
    try:
        n=int(input())
        a=list(map(int,input().split()))
    except: break

    res=float('inf')
    for p in range(min(a),max(a)+1):
        res=min(res,sum((x-p)**2 for x in a))
    print(res)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
m=round(sum(a)/n)
ans = 0
for i in range(n):
    ans += (a[i]-m)**2
print(ans)

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

m = sum(a)//N

ans1 = 0
ans2 = 0
ans3 = 0
for k in range(N):
    ans1 += (a[k]-m)**2
    ans2 += (a[k]-m-1)**2
    ans3 += (a[k]-m+1)**2

print(min(ans1,ans2,ans3))

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
Y=sum(A) // N
Sol=10**9
for y in range(Y-2,Y+2):
    Sol=min(Sol,sum(((a-y)**2 for a in A)))
print(Sol)

Solution (Python):

import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys

def s():
    return raw_input().strip()
def n():
    return int(raw_input())
def d():
    return float(raw_input())

def ls():
    return raw_input().strip().split()
def ln():
    return map(int, raw_input().strip().split())
def ld():
    return map(float, raw_input().strip().split())

def fs():
    return [raw_input().strip() for i in xrange(input())]
def fn():
    return [int(raw_input().strip()) for i in xrange(input())]
def fd():
    return [float(raw_input().strip()) for i in xrange(input())]

N = n()
A = ln()

min = 10e8
for i in xrange(-100, 101):
    s = 0
    for a in A:
        s += (a - i) ** 2
    if s < min:
        min = s
print min

Solution (Python):
N = int(raw_input())
a = map(int,raw_input().split())
ans =  10**9+7
for i in xrange(-100,101):
	cost = sum((i-a[j])**2 for j in xrange(N))
	if cost < ans:
		ans = cost
print ans

Solution (Python):


N = int(input())
A = list(map(int, input().split()))

res = 1<<30
cand = -100
while cand <= 100:
    tmp = 0
    for a in A:
        dif = cand - a
        tmp += dif*dif
    res = min(res, tmp)


    cand += 1

print(res)

Solution (Python):
N = int(input())
A = [int(x) for x in input().split()]
print(min(sum((a-x)**2 for a in A) for x in range(-100, 101)))