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):
N = int(input())
input_list = [int(i) for i in input().split()]

ans = 10 ** 20

for i in range(min(input_list), max(input_list)+1):
    ans1 = 0
    for input in input_list:
        ans1 += (i - input) ** 2
    ans = min(ans, ans1)

print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
num = float("inf")

for i in range(-101, 101, 1):
    rec = 0
    for j in range(n):
        rec += (i - a[j]) ** 2
    num = min(num, rec)

print(num)

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

avg = round(sum(a)/n)

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

print (ans)

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

ave = round(sum(a)/n)

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

print(ans)

Solution (Python):
import numpy as np
import math
N = input()
a = np.array([int(ai) for ai in input().split()])
m = np.mean(a)
a1 = (a - math.floor(m))**2
a2 = (a - math.ceil(m))**2
print(min(sum(a1), sum(a2)))

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

Solution (Python):
from statistics import mean
import numpy as np
n=input()
a=list(map(int,input().split()))
ave= np.round(mean(a))
total=0
for i in a:
    total += (i-ave)**2
print(int(total))

Solution (Python):
import math

n = int(input())
array = [int(x) for x in input().split()]

tmp = math.ceil(sum(array)/n)
tmp2 = math.floor(sum(array)/n)

print(min(sum([pow(abs(x-tmp2),2) for x in array]), sum([pow(abs(x-tmp),2) for x in array])))

Solution (Python):


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

ans = float('inf')
for i in range(min(aN), max(aN)+1):
    sum1 = 0
    for j in range(N):
        sum1 += (aN[j] - i) ** 2
    ans = min(sum1, ans)
print(ans)

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

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

Solution (Python):
import numpy as np
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
N=int(input())
a=list(map(int,input().split()))
ave=int(Decimal(np.average(a)).quantize(Decimal('0'), rounding=ROUND_HALF_UP))
sum=0
for i in range(N):
    sum+=pow(a[i]-ave, 2)
print(sum)

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

ans = 9999999999

for i in range(-100, 101):
    sumof = 0
    for j in a:
        sumof += (i - j)*(i - j)
    ans = min(ans, sumof)
    
print(ans)

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

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

ans = 10**9

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

print(ans)

Solution (Python):
from math import floor, ceil

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

target_f = floor(sum(a)/n)
target_c = ceil(sum(a)/n)

ans_f = sum([(ai - target_f)**2 for ai in a])
ans_c = sum([(ai - target_c)**2 for ai in a])


print(min(ans_f, ans_c))

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

res=INF
for y in range(-100,101):
	tmp=0
	for x in a:
		tmp+=(x-y)**2
	res=min(res,tmp)
print(res)

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

ans = float('inf')
for i in range(-100,101):
    cost = 0
    for a in src:
        cost += (a-i)**2
    ans = min(ans, cost)
print(ans)

Solution (Python):


def main():
    n = int(input())
    a_list = list(map(int, input().split()))
    threshold = round(sum(a_list) / n)
    
    
    ans = 0
    
    
    for i in a_list:
        ans += (i - threshold) ** 2
        
        

    print(ans)
    

if __name__ == '__main__':
    main()

Solution (Python):


def main():
    n = int(input())
    a_list = list(map(int, input().split()))
    threshold1 = -(-sum(a_list) // n)
    threshold2 = sum(a_list) // n
    ans1 = 0
    ans2 = 0
    for i in a_list:
        ans1 += (i - threshold1) ** 2
        ans2 += (i - threshold2) ** 2

    print(min(ans1, ans2))

if __name__ == '__main__':
    main()

Solution (Python):
from collections import defaultdict
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
AtoZ = [chr(i) for i in range(65,65+26)]
atoz = [chr(i) for i in range(97,97+26)]

def inpl(): return list(map(int, input().split()))
def inpl_s(): return list(input().split())

N = int(input())
aa = inpl()
MIN = min(aa)
MAX = max(aa)
ans = INF

for i in range(MIN,MAX+1):
	tmp = 0
	for a in aa:
		tmp += (a-i)**2
	ans = min(ans,tmp)

print(ans)

Solution (Python):
from statistics  import mean

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

    ave = mean(a)
    ave = round(ave)

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

    print(ans)


if __name__ == "__main__":
        main()

Solution (Python):
N = int(input())
A = [int(x) for x in input().split()]

target = sum(A) / len(A)
if target > 0:
    target += 0.5
else:
    target -= 0.5
target = int(target)

print(sum((x - target) ** 2 for x in A))

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

best = float('inf')
for r in range(-100, 101):
    cost = sum([(ai - r)**2 for ai in a])
    best = min(cost, best)
print(best)

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

best = float('inf')
for r in range(-100, 101):
    cost = sum([(ai - r)**2 for ai in a])
    best = min(cost, best)
print(best)

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

total = round(sum(a)/n)

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

sys.exit(0)

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

x = sum(A)//n
y = x + 1

xs = 0
ys = 0
for a in A:
    xs += (a - x) ** 2
    ys += (a - y) ** 2
print(min(xs, ys))

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

x = sum(A)//n
y = x + 1

xs = 0
ys = 0
for a in A:
    xs += (a - x) ** 2
    ys += (a - y) ** 2
print(min(xs, ys))

Solution (Python):
N = int(input())
A = [int(x) for x in input().split()]

m1 = sum(A) // N
cost1 = 0
for i in range(N):
    cost1 += (A[i] - m1)** 2

m2 = m1 + 1
cost2 = 0
for i in range(N):
    cost2 += (A[i] - m2) ** 2

print(min(cost1, cost2))

Solution (Python):
import statistics
N = int(input())
a = list(input().split())
a = [int(i) for i in a]
res = []
for i in range(min(int(statistics.mean(a)),int(statistics.median(a))), max(int(statistics.mean(a))+1,int(statistics.median(a))+1+1)):
    res.append(0)
    for j in a:
        res[-1] += (j-i)**2
print(min(res))

Solution (Python):

"""
Created on Sat Aug  4 18:19:03 2018

@author: maezawa
"""

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

mean_a = round(sum(a)/n)

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

print(ans)

Solution (Python):
f=lambda x:lambda y:(x-y)**2
N=int(input())
l=list(map(int,input().split()))
m=[]
for i in range(min(l),max(l)+1):
  g=f(i)
  m.extend([sum(map(g,l))])
print(min(m))

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

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

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

Solution (Python):
from math import floor, ceil

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

mean = sum(A)/N

fl = 0
cl = 0

for a in A:
    fl += (floor(mean) - a)**2
    cl += (ceil(mean) - a)**2
    
print(min(fl,cl))

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

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


K1 = sum(a)//n
K2 = K1+1

ans1 = 0
ans2 = 0


for i in a:
    ans1+=(K1-i)*(K1-i)
    ans2+=(K2-i)*(K2-i)
    
if ans1<ans2:
    print(ans1)
else:
    print(ans2)

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

min_df = 10**100

for i in range(min(a), max(a) + 1) :
    sum_over = 0 
    for j in a :
        sum_over += (i - j) ** 2
        
    min_df = min(min_df, sum_over)
    
print(min_df)

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

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

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

best = float('inf')
for r in range(-100, 101):
    cost = sum([(ai - r)**2 for ai in a])
    best = min(cost, best)
print(best)

Solution (Python):
from sys import exit, setrecursionlimit, stderr
from functools import reduce
from itertools import *
from collections import defaultdict
from bisect import bisect
from random import *

def read():
  return int(input())
 
def reads():
  return [int(x) for x in input().split()]

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

N = read()
A = reads()
m = sum(A) // N
print(min(cost(A, m), cost(A, m+1)))

Solution (Python):
n = int(input())
a = [int(ai) for ai in input().split()]

costs = []
for y in range(min(a), max(a)+1):
    cost = 0
    for ai in a:
        cost += (ai - y) ** 2
    costs.append(cost)

print(min(costs))

Solution (Python):
input()
a=list(map(int,input().split()))
sl=sum(a)//len(a)-1
sm=sum(a)//len(a)
sr=sum(a)//len(a)+1
l=0
m=0
r=0
for i in a:
    l+=(i-sl)**2
for i in a:
    m+=(i-sm)**2 
for i in a:
    r+=(i-sr)**2 
print(min(l,m,r))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
mean1 = int(sum(a)/n)
mean2 = mean1+1
ans1 = 0
ans2 = 0
for aa in a:
    ans1 += (aa-mean1)**2
    ans2 += (aa-mean2)**2
print(min(ans1, ans2))

Solution (Python):
n = int(raw_input())
a = map(int, raw_input().split())
a_total = sum(a)
m = 0
m = a_total / n
c = [(t-m)**2 for t in a]
d = [(t-m-1)**2 for t in a]
print(min(sum(c), sum(d)))

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

ans = 0
ave = round(sum(a) / len(a))

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

print(ans)

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

li_ans = []
ans = 0

for j in range(min(a), max(a)):
    for k in a:
        ans += abs(k - j) ** 2
    else:
        li_ans.append(ans)
        ans = 0

if len(li_ans) == 0:
    print(0)
else:
    print(min(li_ans))

Solution (Python):
N = int(input())
A = [int(s) for s in input().split()]

argmin_x = 1e10
min_cost = 1e10
for x in range(min(A), max(A) + 1):
    cost = 0
    for a in A:
        cost += pow(x - a, 2)
    if cost < min_cost:
        argmin_x = x
        min_cost = cost
print(min_cost)

Solution (Python):
n = int(input())
A = [int(i) for i in input().split()]

sum0 = sum(A)

b = sum0 // n
c = -(-sum0 // n)
sum1 = 0
sum2 = 0
for a in A:
    sum1 += (a-b) ** 2
    sum2 += (a-c) ** 2
    
print(min(sum1, sum2))

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

Solution (Python):

def m():
    def cost(x,y):
        return (x-y) ** 2
    n = int(input())
    a = list(map(int, input().split()))
    ans = 99999999
    for y in range(-100, 101):
        c = 0
        for x in a:
            c += cost(x,y)
        ans = min(ans, c)
    return ans

print(m())

Solution (Python):
n = input()
n_list = list(map(int, input().split()))
min_sum = float('inf')

for i in range(-100, 101):
  total = 0
  for a in n_list:
    total += pow((a - i), 2)
  min_sum = min(min_sum, total)

print(min_sum)

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

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

ans = []
sum = 0

for j in range(min(a), max(a)):
    for num in a:
        sum += (num - j) ** 2
    else:
        ans.append(sum)
        sum = 0

if len(ans) == 0:
    print(0)
else:
    print(min(ans))

Solution (Python):
N = int(input())
a = [int(i) for i in input().split()]
costs = []
i = min(a)
while i <= max(a):
  cost = 0
  for nums in a:
    cost += (i-nums)**2
  costs.append(cost)
  i += 1
print(min(costs))

Solution (Python):
import numpy as np
def inpl(): return map(int, input().split())
N = int(input())
A = np.array(list(inpl()), dtype=np.int16).reshape(1, -1)
X = np.arange(A.min(), A.max()+1).reshape(1, -1)
print(np.power(A-X.T, 2).sum(axis=1).min())

Solution (Python):
n=int(input())
a=list(map(int, input().split()))
ans=float("inf")
for i in range(-100, 101):
  cost=0
  for j in range(n):
    cost+=(a[j]-i)*(a[j]-i)
  if cost<ans:
    ans=cost
print(ans)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
t = sum(a)/n
if t > 0:
    t += 0.5
t = int(t)
print(sum(list(map(lambda x: (t-x)**2, a))))

Solution (Python):
N = int(input())
src = list(map(int,input().split()))
ans = float('inf')
for to in range(-100,101):
    tmp = 0
    for a in src:
        tmp += (to-a)**2
    ans = min(ans,tmp)
print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
mean = int(round(sum(A)/N))
ans = 0
for a in A:
    ans += (a - mean)**2
print(ans)

Solution (Python):
if __name__ == '__main__':
    n = int(input())
    l = list(map(int, input().split(' ')))
    min_cost = float('inf')
    for b in range(-100, 101, 1):
        cost = 0
        for elem in l:
            diff = abs(elem - b) ** 2
            cost += diff
        min_cost = int(min(min_cost, cost))
    print(min_cost)

Solution (Python):
from math import ceil,floor
n=int(input())
a=[int(i) for i in input().split()]
b=sum(a)/n
ac=sum([(x-ceil(b))**2 for x in a])
af=sum([(x-floor(b))**2 for x in a])
print(min(ac,af))

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

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
ans = 10000000
for i in range(-100, 101):
    candidate = 0
    for a_j in a:
        candidate += (a_j-i)**2
    ans = min(ans, candidate)
print(ans)

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

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

print(ans)

Solution (Python):
def main():
    n = int(input())
    a = list(map(int, input().split()))
    
    l, h = min(a), max(a)
    min_c = 4000001
    for i in range(l, h+1):
        cost = 0
        for j in a:
            cost += (j-i) ** 2
        
        if cost < min_c:
            min_c = cost
            
    print(min_c)
    
    
if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
l = list(map(int, input().split()))
ans = float('Inf')
for i in range(-100, 101):
  temp = 0
  for j in l:
    temp += (i-j)**2
  if temp < ans:
    ans = temp

print(ans)

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

ans = 10**9
for i in range(-100,101):
    tmp = sum([(a-i)**2 for a in src])
    ans = min(ans, tmp)
print(ans)

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

Solution (Python):
N=int(input())
s=list(map(int,input().split()))
t=0
w=0
for i in range(N):
    t+=s[i]
t=int(((t/N)*2+1)//2)
for j in range(N):
    w+=(t-s[j])**2
print(w)

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

min = 4000001

for i in range(-100, 101):
	sum = 0
	for j in range(0, N):
		sum = sum + (a[j] - i) * (a[j] - i)
	if sum < min: min = sum

print min

Solution (Python):
def main():
    N = int(input())
    A = tuple(map(int, input().split()))

    mina = min(A)
    maxa = max(A)
    cost = float('inf')
    for y in range(mina, maxa + 1):
        c = 0
        for x in A:
            c += (x - y) ** 2
        cost = min(cost, c)

    print(cost)

main()

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

nmax = max(nlist)
nmin = min(nlist)

costs = []

for i in range(nmin,nmax + 1):
    p = 0
    for j in range(len(nlist)):
        p = p + (i-nlist[j]) ** 2
    costs.append(p)
    
print(min(costs))

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



amin = min(a)
amax = max(a)
min_sum=10**8
for x in range(amin,amax+1):
    sum_for_x = sum([(y-x)**2 for y in a])
    if sum_for_x < min_sum:
        min_sum = sum_for_x

print(min_sum)

Solution (Python):
from decimal import Decimal, ROUND_HALF_UP
n = int(input())
P = list(map(int,input().split()))
a = sum(P) / len(P)
r =  Decimal(a).quantize(Decimal('0'), rounding=ROUND_HALF_UP)
ans = 0
for i in range(n):
    ans += (P[i] - r) ** 2

print(ans)

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

Solution (Python):
import numpy as np

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

avg = np.mean(a)
avg = round(avg, 0)
minCost = 0

for i in range(N):
    minCost += (a[i]-avg)**2

print(int(minCost))

Solution (Python):





if __name__ == '__main__':
    n = int(input())
    a = list(map(int, input().split()))
    cost = float('inf')

    for z in range(-100, 100 + 1):
        tmp_cost = sum(((x - z) ** 2 for x in a))
        cost = min(cost, tmp_cost)

    print(cost)

Solution (Python):
n = int(input())
a_list = list(map(int, input().split()))
x = min(a_list)
old_cost = sum([int((x - a) ** 2) for a in a_list])

if old_cost == 0:
    print("0")

else:
    while True:
        new_cost = sum([(x + 1 - a) ** 2 for a in a_list])
        if old_cost <= new_cost:
            break
        else:
            old_cost = new_cost
            x += 1
            
    print(old_cost)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
S = 100000000
for i in range(-100,101):
    T = 0
    for j in range(N):
        T += (A[j] - i)**2
    if S > T:
        S = T
print(S)

Solution (Python):
n = int(input())
A = [int(i) for i in input().split()]

m = round(sum(A)/n)
ans = 0

for a in A:
    ans += abs(a-m)**2
    
print(ans)

Solution (Python):
n = int(input())
l = list(map(int, input().split()))
ans = float('Inf')
i = -100
while i <= 100:
  temp = 0
  for j in l:
    temp += (j-i)**2
  ans = min(ans, temp)
  i += 1
print(ans)

Solution (Python):
def main():
    """
    1 <= N <= 100
    -100 <= ai <= 100
    """
    N = int(input())
    *A, = map(int, input().split())

    ans = float("inf")
    for to in range(-100, 100 + 1):
        cost = 0
        for a in A:
            cost += (a - to) ** 2
        ans = min(ans, cost)

    print(ans)


if __name__ == '__main__':
    main()

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

average = round(sum(input_array)/N)
out = 0
for line in input_array:
    out += (line-average)**2

print(out)

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

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
x = (sum(a)-sum(a)%n)//n
answer,answer2 = 0,0
for i in a:
	answer += (i-x)**2
for i in a:
	answer2 += (i-(x+1))**2
print(min(answer,answer2))

Solution (Python):
N = int(input())
num = list(map(int,input().split()))
sumN = 0
for i in range(N):
    sumN += num[i]
ave = sumN//N
minN = 10**15
for j in range(-1,2):
    work = 0
    for k in range(N):
        work += pow(ave+j-num[k],2)
    minN = min(minN,work)
print(minN)

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

cost=0
for i in range(201):
    for j in range(len(a)):
      cost=cost+(a[j]-(i-100))* (a[j]-(i-100))
    if cost<=mincost:
        mincost=cost
        change_num=i-100
    cost=0

print(mincost)

Solution (Python):
N = int(input())
a = sorted(map(int, input().split()))
cost = 1000000
for y in range(-100, 101):
    tmp = sum((x-y)**2 for x in a)
    cost = min(cost, tmp)
print(cost)

Solution (Python):
def equalize_cost(numbers):
    average = round(sum(numbers) / len(numbers))
    return sum(map(lambda x: (x - average) ** 2, numbers))


if __name__ == '__main__':
    _ = input()
    numbers = list(map(int, input().split()))
    print(equalize_cost(numbers))

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

ave = sum(a) // N
candidate = [ave-1,ave,ave+1]

ans = float('inf')
for num in candidate:
	sumv = 0
	for i in range(N):
		sumv += ((a[i]-num)**2)

	if ans > sumv:
		ans = sumv

print(ans)

Solution (Python):
N = int(input())
A = [int(s) for s in input().split()]

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

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

print(sum(B))

Solution (Python):
N=int(input())
A=[int(x) for x in input().split()]

min_ans=100000000000000

for i in range(-200,200):
  ans=0
  for j in range(N):
    ans+=(A[j]-i)**2
  if ans<min_ans:
    min_ans=ans
print(min_ans)

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

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+=(j-i)**2
    ans.append(cost)
    cost=0
print(min(ans))

Solution (Python):
import math

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

value=round((sum(a))/n)

ans=0

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

print(ans)

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

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

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

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

Solution (Python):
N=int(input())
a=input().split()
b = []
for i in range(N):
    b.append(int(a[i]))
c=sum(b)
d=round(c/N)
ans=0
for i in range (N):
  ans+=(b[i]-d)**2
print(int(ans))

Solution (Python):

"""
https://beta.atcoder.jp/contests/abc043/tasks/arc059_a

"""
import sys
from sys import stdin
input = stdin.readline


def solve(A, N):
    
    costs = [0] * 201

    for i in range(-100, 101):
        t = [(x - i)**2  for x in A]
        costs[i+100] = sum(t)

    return min(costs)


def main(args):
    N = int(input())
    A = [int(x) for x in input().split()]
    ans = solve(A, N)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Solution (Python):
import numpy as np
n = int(input())
j = np.array(list(map(int, input().split())))
minn = 999999999999999999
for i in range(min(j), max(j)+1):
    if minn > sum((j - i) ** 2):
        minn = sum((j - i) ** 2)
print(minn)

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

ans = float('inf')
for x in range(-100,101):
  tmp = 0
  for a in A:
    tmp += (x-a)**2
  ans = min(ans,tmp)
print(ans)

Solution (Python):

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

def nijo(tmp):
    ret = 0
    for i in a:
        ret += (i-tmp)**2
    return ret

tmp = 10**8
for i in range(-100,101):
    tmp = min(tmp,nijo(i))
print(tmp)

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

mean = round(sum(a) / N)

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

print(int(ans))

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

for i in range(-100, 101):
    B = list(map(lambda x: (x-i)**2, A))
    ans.append(sum(B))

print(min(ans))

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

val = [int(x) for x in input().split()]

val2 = [int(x) for x in range(min(val), max(val))]


array = []
for i in val2:
    res = 0
    for j in val:
        res += pow(abs(i - j), 2)
    array.append(res)
print(min(array) if len(array) != 0 else 0)

Solution (Python):
N=input()
a=map(int,raw_input().split())
min_a=min(a)
max_a=max(a)

b=[]

for i in range(min_a,max_a+1):
	sum_a=0
	for j in a:
		sum_a+=(j-i)**2
	b.append(sum_a)

print min(b)

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
cost = 1000000
for v in range(-100, 101):
    cost = min(sum(abs(ai-v)**2 for ai in a), cost)
print(cost)

Solution (Python):
N = int(raw_input())
lis = map(int,raw_input().split())
intmean = int(round(float(sum(lis))/len(lis)))
ss = map(lambda x: (x-intmean)**2,lis)
print sum(ss)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
cost = float('Inf')
check = 0

for m in range(min(A), max(A)+1):
    check = 0
    for a in range(len(A)):
        check += (A[a] - m) ** 2
    cost = min(cost, check)
print(cost)

Solution (Python):



"""043-c"""


import sys

from functools import reduce


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

    

    x = sum(a_list) / N

    def sumOfProd(n,l):
        """Sum of product."""
        return reduce(lambda a,b: a + b,
                      map(lambda v: (v - n) * (v - n), l))

    res1 = sumOfProd(int(x), a_list)
    res2 = sumOfProd(int(x) + 1, a_list)

    print(min(res1, res2))


if __name__ == '__main__':
    sys.exit(main())

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


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


ave = round(sum(a) / N)
print(int(sum(list(map(lambda x: (ave-x) * (ave-x), a)))))

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

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

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

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

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

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

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

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

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

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

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

Solution (Python):

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)

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

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

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

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

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

Solution (Python):

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()

Solution (Python):



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)