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-----
2
4 8

-----Sample Output-----
8

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

Difficulty: introductory

Starter code:

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

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

print(ans)


n = int(input())
a = [int(x) for x in input().split()]
res = 10 ** 9

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

print(res)

N = int(input())
a = list(map(int, input().split()))
avg = round(sum(a)/N)
sums = []
for i in range(min(a), avg+1):
    s = 0
    for j in range(N):
        s += (i-a[j])**2
    sums.append(s)
print(min(sums))

from typing import List


def answer(n: int, a: List[int]) -> int:
    cost = 0
    num = round(sum(a) / n)
    for i in a:
        cost += (i - num) ** 2

    return cost


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


def __starting_point():
    main()
__starting_point()

import math
from statistics import mean

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

m = mean(a)
if m - math.floor(m) < 0.5:
    m = math.floor(m)
else:
    m = math.floor(m) + 1

diff = 0
for v in a:
    diff += (m - v) ** 2

print(diff)


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

a_u = round(sum(a)/n)
a_d = int(sum(a)/n)
ans_u = 0
ans_d = 0
for i in range(n):
    ans_u += (a[i]-a_u)**2
    ans_d += (a[i]-a_d)**2
print((min(ans_u,ans_d)))
#print(round(-2.5),int(-2.5))


import math
n = int(input())
a = list(map(int,input().split()))
avg = sum(a)/n
x = math.floor(avg)
y = math.ceil(avg)
sumx = 0
sumy = 0
for i in a:
    sumx += (i - x) ** 2
    sumy += (i - y) ** 2

print(min([sumx,sumy]))

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

mx=max(A)
mn=min(A)


if len(set(A))==1:
    print((0))
else:
    ans=float('inf')
    for i in range(mn,mx):
        ans=min(ans, sum([(k-i)**2 for k in A]))
    print(ans)


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

t = 10**9
for i in range(min(a),max(a)+1):
    num = 0
    for h in a:
        num += (h-i)**2
    if num <= t:
        t = num
print(t)

s=input();a=list(map(int,input().split()));ans=0
mean=round(sum(a)/len(a))

for i in a:
  p=(i-mean)**2
  ans+=p
  
print(ans)

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

maxa=(10)**11
N=int(input())
L=list(map(int,input().split()))
for i in range(-100,101):
  R=[(i-y)**2 for y in L]
  a=sum(R)
  maxa=min(maxa,a)
print(maxa)

N=int(input())
List = list(map(int, input().split()))
sumS = 0
for i in range(N):
  sumS +=List[i]
trial = sumS // N
mid = 0
res = 10000000
for i in range(N):
  trial += i
  mid = 0 
  for j in range(N):
    mid += (trial - List[j])**2
  res = min(res, mid)
print(res)

num = int(input())
str_line = input().split(" ")
num_line = [int(n) for n in str_line]

ave = 0
for i in range(num):
    ave += num_line[i]

if ave%num == 0:
    ave //= num

else:
    if ave%num <= num/2:
        ave //= num
    else:
        ave = -(-ave//num)

wa = 0
for i in range(num):
    temp = num_line[i] - ave
    wa += temp*temp
    
print(wa)

def main():
    n = int(input())
    al = list(map(int, input().split()))
    maxval = max(abs(max(al)), abs(min(al)))
    cost = []
    if len(list(set(al))) == 1 :
        print("0")
    else :
        k = -1
        for i in range(-100,100) :
            k += 1
            cost.append(0)
            for j in range(n) :
                cost[k] = cost[k] + (al[j] - i) ** 2
                #print(cost[i])
        print((min(cost)))



def __starting_point():
    main()

__starting_point()

import statistics
import math
n = int(input())
a = list(map(int,input().split()))
ans1,ans2 = 0,0
a_avr1 = math.ceil((statistics.mean(a)))
a_avr2 = math.floor((statistics.mean(a)))
for i in range(n):
  ans1 += (a[i] - a_avr1)**2
  ans2 += (a[i] - a_avr2)**2
print(min(ans1,ans2))

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

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

import numpy as np
import logging
logging.basicConfig(level=logging.DEBUG)
logging.disable(logging.CRITICAL)

N=int(input())

x=np.array(list(map(int,input().split())))
logging.debug('x={}'.format(x))

i=np.amin(x)
ans=[]
while i<=np.amax(x):

    logging.debug('i={}'.format(i))
    y=np.array([i]*N)
    ans.append(sum((x-y)**2))

    logging.debug('ans={}'.format(ans))
    i+=1
print((min(ans)))


n = int(input())
a = list(map(int, input().split()))
avg_a = round(sum(a)/n)

result = 0
for num in a:
    result += (num-avg_a)**2

print(result)

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

n = int(input())
a = [int(x) for x in input().split()]
a.sort()
ans = []
for i in range(a[0], a[-1]+1):
    cnt = 0
    for j in range(n):
        cnt += (a[j] - i)**2
    ans.append(cnt)

print(min(ans))

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

my_round_int = lambda x: int((x * 2 + 1) // 2)
m = my_round_int(sum(a)/len(a))

cost = 0

for i in a:
    cost += (i - m)**2

print(cost)

N = int(input())
a = list(map(int, input().split()))
b = []
for i in range(-100, 101):
	t = 0
	for j in a:
		t += (j - i) ** 2
	b.append(t)
print(min(b))

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

min_a = min(A)
max_a = max(A)

ans = float("inf")
for i in range(min_a, max_a+1):
  tmp = 0
  for j in A:
    tmp += (i - j) ** 2
  ans = min(ans, tmp)
  
print(ans)

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

avg = round(sum(a) / n)

cost = 0

for e in a:
    cost += (e-avg)**2

print(cost)


n = int(input())
lst = list(map(int, input().split()))
m = 10 ** 10
for i in range(201):
  a = i - 100
  temp = 0
  for j in range(n):
    temp += ((lst[j] - a) ** 2)
  m = min(m, temp)
print(m)

N = int(input())
a = list(map(int, input().split()))
mi = min(a)
ma = max(a)

ans = float('inf')
for i in range(mi, ma + 1):
    ans = min(ans, sum((i - ai) ** 2 for ai in a))

print(ans)

def cost(x):
    cost = (x - ave)**2
    return cost


n = input()
a = list(map(int, input().split()))
ave = round(sum(a)/int(n))
print(sum(list(map(cost, a))))

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

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

N=int(input())
n=list(map(int, input().split()))
m=int(sum(n)/N)

def fun(x):
  ans=0
  for i in n:
    ans+=(i-x)**2
  return(ans)
  
p=fun(m)
q=fun(m+1)
if p<q:
  print(p)
else:
  print(q)

n = int(input())
a = [int(x) for x in input().split()]
A = min(a)
B = max(a)
temp = 10000000000000000000000
for i in range(A,B+1):
    ans = 0
    for j in a:
        ans+= (abs(j-i))**2
    #print(ans)
    temp = min(temp,ans)
print(temp)


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

n = float(input())

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

ave = round(sum(a)/n)

print(sum(list(map(lambda x:(x-ave)**2,a))))

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

N=int(input())
n=list(map(int, input().split()))
m=int(sum(n)/N)
 
def fun(x):
  ans=0
  for i in n:
    ans+=(i-x)**2
  return(ans)
  
s=min(fun(m),fun(m+1))
print(s)

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

for i in range(101):
  cost = 0
  for j in range(N):
    cost += (A[j]-i)**2
  costs.append(cost)
  cost = 0
  for j in range(N):
    cost += (A[j]+i)**2
  costs.append(cost)

print(min(costs))

N = int(input())
a = list(map(int, input().split()))
s = sum(a)
x = int(s / len(a))

if (x + 1) - (s / len(a)) < (s / len(a)) - x:
    x += 1
cost = 0
for y in a:
    cost += pow(y - x, 2)

print(cost)

import math
from datetime import date

def f(x, a):
	ans = 0
	for y in a:
		ans += (x - y) ** 2

	return ans

def main():
	
	n = int(input())
	a = [int(x) for x in input().split()]
	s = 0
	for x in a:
		s += x

	s = s // n
	ans = 10**17

	for x in range(s - 2, s + 3):
		ans = min(ans, f(x, a))

	print(ans)
	
main()


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

result = 10**9
for num in range(-100,101):
  cost = 0
  for i in range(n):
    cost += (a[i] - num) ** 2
  result = min(result, cost)
print(result)


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

A = sum(list)//N
ans = 0

for k in range(N):
    ans = ans + (list[k] - A) ** 2

for i in range(-100,100):
    sum_new = 0
    for j in range(N):
        sum_new = sum_new + (list[j] - i) ** 2
    if(ans > sum_new):
        ans = sum_new

print(ans)


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

ans=10**9
for i in range(a[0],a[-1]+1):
  s=0
  for j in range(N):
    x=(a[j]-i)**2
    s+=x
  ans=min(ans,s)

print(ans)


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

for i in range(n):
    b += a[i]

c = int(b/n)
d = c + 1
e = 0
f = 0

for i in range(n):
    e += (a[i]-c)**2
    f += (a[i]-d)**2

print((min(e,f)))



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

# int_listの各整数をyに書き換えるときのコストの総和を計算
def calc_cost(int_list, y):
    cost = 0
    for x in int_list:
        cost += (x - y)**2
    return cost

cost_list = [calc_cost(int_list, y) for y in range(min(int_list), max(int_list)+1)]
print(min(cost_list))

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

A=sum(a)//N
B=(sum(a)-1)//N+1


print((min(sum((x-A)*(x-A) for x in a),sum((x-B)*(x-B) for x in a))))


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

#!/usr/bin/env python3
import sys


def solve(N: int, a: "List[int]"):
    ans = float("inf")
    for m in range(-100, 101):
        ans = min(ans,
                  sum((aa-m)**2 for aa in a))
    return ans


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    print((solve(N, a)))

def test():
    import doctest
    doctest.testmod()

def __starting_point():
    #test()
    main()

__starting_point()

import sys

sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inl = lambda: [int(x) for x in sys.stdin.readline().split()]
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))


def solve():
    n = ini()
    a = inl()
    s = sum(a)
    m1 = s // n
    m2 = (s + n - 1) // n

    s1 = sum((x - m1) ** 2 for x in a)
    s2 = sum((x - m2) ** 2 for x in a)
    return min(s1, s2)


print(solve())


import sys
import math

N = int(input())

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

a.sort()
ans = 10**9
for i in range(a[0], a[-1]+1):
    cost = 0
    for j in a:
        cost += (i-j)**2
    ans = min(cost, ans)
print(ans)

inf = float('inf')
n = int(input())
arr = list(map(int, input().split()))

min_cost = inf
for i in range(-100, 101):
    cost = 0
    for j in arr:
        cost += (j - i)**2
    min_cost = min(min_cost, cost)
print(min_cost)

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

inf=float('inf')

ans=inf

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

n=int(input())
a=list(map(int,input().split()))
# n=2
# a=[4,8]
minimum=10000000
for i in range(-100,101):
    cost=0
    for j in range(n):
        cost+=(i-(a[j]))**2
        if j==n-1:
            if cost<minimum:
                minimum=cost

print(minimum)




from sys import stdin
input = stdin.readline

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

if len(set(A)) != 1:
    cost = 2**20
    for i in range(-100, 101):
        tc = 0
        for a in A:
            tc += (a-i)**2
        if cost > tc:
            cost = tc
else:
    cost = 0

print(cost)

N = int(input())
l = list(map(int, input().split()))
cal = []
for i in range(-100,101):
    a = 0
    for j in l:
        a += (i - j)**2
    cal.append(a)

ans = min(cal)
print(ans)

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

mini = min(arr)
maxi = max(arr)
cost = 10000000000

for i in range(-100, 101+1):
    add = 0
    for j in range(n):
        add = add + (arr[j] - i)**2
    cost = min(cost, add)
print(cost)

N,*a = open(0).read().split()

N=int(N)

list1=list(map(int,a))

#print(N)

#print(list1)

a=[]

for i in range(-100,101):
    int1=0
    for n in range(N):
        x = (list1[n]-i)
        y = x**2
        int1+=y
        if n==N-1:
            a.append(int1)
    
#print(a)

#print(max(a))

print(min(a))

def sam(lst):
  return lst[1:] == lst[:-1]

n = int(input())
s = [int(x) for x in input().split()]
if(sam(s)):
  print(0)
else:
  x = round(sum(s)/n)
  n = 0
  for elem in s:
    n += (elem-x)**2
  print(n)

N = int(input())
num_list = list(map(int, input().split()))
cost_list = []
max_num = max(num_list)
min_num = min(num_list)

for i in range(min_num,max_num):
    cost = 0
    for j in num_list:
        cost += (i-j)**2
    cost_list.append(cost)

if len(cost_list) == 0:
    cost_list.append(0)
    
print(min(cost_list))

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

min_cost = 10 ** 9 + 7
for i in range(-100, 101):
    cost = 0
    for j in range(N):
        cost += (A[j] - i) ** 2
    min_cost = min(cost, min_cost)

print(min_cost)


N = input()
list1 = list(map(int,input().split()))
list2 = []
for i in range(min(list1),max(list1)+1):
    a = 0
    for j in range(len(list1)):
        a += (list1[j] - i)**2
    list2.append(a)
print(min(list2))

n=int(input())
a=list(map(int, input().split()))
a.sort()
ans=10**10
for x in range(a[0],a[-1]+1):
    tmp=0
    for i in range(n):
        tmp+=(a[i]-x)**2
    ans=min(ans,tmp)
print(ans)

import math
import statistics
N = int(input())
A = [int(x) for x in input().split()]
avg = math.floor(statistics.mean(A))
a = 0
b = 0
for i in range(len(A)):
    a += (A[i] - avg)**2
    b += (A[i] - avg - 1)**2
print(min(a,b))

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

a=float('inf')
for i in range(min(A),max(A)+1):a=min(a, sum([(k-i)**2 for k in A]))
print(a)

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


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

start = round(sum(L) / N)
f = lambda x: (x - start)**2

print(sum(list(map(f, L))))

N=int(input())
s=list(map(int,input().split()))
s_max=max(s)
s_min=min(s)
A=[]
for i in range(s_min,s_max+1):
  b=0
  for j in s:
    b+=(j-i)**2
  A.append(b)
print(min(A))

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

def calc_cost(x,avg):
  return (x-avg)**2

avg = sum(a)//N

delta = [-2,-1,0,1,2]

ans=10**20
for i in range(len(delta)):
  val = sum(map(lambda x:calc_cost(x,avg+delta[i]),a))
  ans = min(ans,val)
  
print(ans)

n = int(input())
a = list(map(int, input().split()))
ans = 10**10
for i in range(min(a), max(a)+1):
    cnt = 0
    for j in range(n):
        cnt += (a[j]-i) ** 2
    ans = min(ans, cnt)
print(ans)

N=int(input())
A=[int(x) for x in input().split()]
maxA=max(A)
minA=min(A)
ans=10**9
for i in range(minA,maxA+1):
    kouho=0
    for j in range(N):
        kouho+=(i-A[j])**2
    ans=min(ans,kouho)
print(ans)

n=int(input())
a=list(map(int,input().split()))
ans=100000000
for i in range(-100,101):
    now=0
    for k in a:
        now+=(i-k)*(i-k)
    ans=min(ans,now)
print(ans)

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

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

N = int(input())
A = [int(T) for T in input().split()]
Min = 10**9
for TN in range(min(A),max(A)+1):
    Min = min(Min,sum((TN-TA)**2 for TA in A))
print(Min)

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

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

mx=max(A)
mn=min(A)

ans=float('inf')
for i in range(mn,mx+1):
    ans=min(ans, sum([(k-i)**2 for k in A]))
print(ans)

import sys
import math

def main():

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

    y = round(sum(a)/len(a))

    ans = 0
    for i in a:
        ans += pow((i - y), 2)

    print(ans)


def __starting_point():
    main()
__starting_point()

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

if sum(A)%N == 0:
    x = sum(A)//N
    for i in range(N):
        ans += (x-A[i])**2
    print(ans)
else:
    x = sum(A)//N
    b = sum(A)//N+1
    if sum(A)/N - x > b - sum(A)/N:
        x = b
    for i in range(N):
        ans += (x-A[i])**2
    
    print(ans)



def main():
    n = int(input())
    As = list(map(int, input().split()))
    ans = float("inf")

    for i in range(-100, 101):
        ans_temp = 0
        for j in range(n):
            ans_temp += (As[j] + i)**2
        ans = min(ans, ans_temp)
    print(ans)


def __starting_point():
    main()

__starting_point()

import math
 
N = int(input())
A = [int(a) for a in input().split(" ")]
ave = sum(A) / N
can1 = math.floor(ave)
can2 = math.ceil(ave)
 
cost1 = sum([(a - can1) ** 2 for a in A])
cost2 = sum([(a - can2) ** 2 for a in A])
print(min([cost1, cost2]))

N = int(input())
lst = list(map(int, input().split()))
avg = round(sum(lst) / N)

result = 0
for i in lst:
    result += (i - avg)**2

print(result)


N = int(input())
a = list(map(int,input().split()))
b = 0
for i in range(N):
    b += a[i]
r = round(b/N)
ans = 0
for i in range(N):
    ans += (a[i]-r)**2
print(ans)

# C - いっしょ
def main():
    import math
    _ = int(input())
    a = list(map(int, input().split()))
    t = float('inf')

    for i in range(min(a), max(a)+1):
        cnt = 0
        for j in a:
            cnt += (j-i)**2
        else:
            if cnt < t:
                t = cnt
    else:
        print(t)

    
if __name__ ==  "__main__":
    main()

def keisan(N,A):
     
     KAI = 0
     A.sort
 
     min = -100
     max = 101
     
     i = min
     
     while i <= max:
         kari = 0
         j = 0
         while j < N:
             kari += (A[j]-i)**2
             j += 1
        
         
         if kari <= KAI or i == min:
             KAI = kari
         i += 1
 
     
     return KAI
 
 
N = int(input())
 
A = input().split()
int_list = list(map(int,A))
 
print(keisan(N,int_list))

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

a_sum = sum(a_list)
f = a_sum // n
c = (a_sum + n - 1) // n
b = f if abs(a_sum - f * n) < abs(a_sum - c * n) else c

ans = 0
for a in a_list:
    ans += (a - b) ** 2
print(ans)

n = int(input())
l = list(input().split())
sum = 0
min_sum = 10**10
num = 0
for i in range(-100,101):
    for j in range(len(l)):
        sum += (int(l[j]) - i)**2
    if min_sum > sum:
        min_sum = sum
        num = i
    sum = 0

print(min_sum)

n = int(input())
a = list(map(int, input().split()))
m1 = sum(a) // n
m2 = (sum(a) + n - 1) // n

res1 = 0
res2 = 0
for i in range(n):
    res1 += (a[i] - m1) ** 2
    res2 += (a[i] - m2) ** 2

print((min(res1, res2)))



class Combination:
    def __init__(self, n, mod):
        self.n = n
        self.mod = mod
        self.fac = [1 for i in range(self.n + 1)]
        self.finv = [1 for i in range(self.n + 1)]
        for i in range(2, self.n+1):
            self.fac[i] = (self.fac[i - 1] * i) % self.mod
            self.finv[i] = (self.finv[i-1] * pow(i, -1, self.mod)) % self.mod

    def comb(self, n, m):
        return self.fac[n] * (self.finv[n-m] * self.finv[m] % self.mod) % self.mod
def iparse():
    return list(map(int, input().split()))

def __starting_point():
    n = int(input())
    a = iparse()
    ans = 10000000000
    for i in range(-100, 101):
        tmp = 0
        for e in a:
            tmp += (e - i)** 2
        ans = min(tmp, ans)
    print(ans)
    

__starting_point()

def main():
    n = int(input())
    a_lst = list(map(int, input().split()))
    minimum = min(a_lst)
    maximum = max(a_lst)

    cost = 10 ** 9
    for i in range(maximum - minimum + 1):
        tmp_cost = 0
        std = minimum + i
        for j in range(n):
            a = a_lst[j]
            tmp_cost += (a - std) ** 2
        cost = min(tmp_cost, cost)

    print(cost)


def __starting_point():
    main()
__starting_point()

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

    ans = 10 ** 10

    for kouho1 in range(-100,101):
        tmp1 = 0
        for i in range(n):
            tmp1 += (inlis[i]- kouho1) ** 2
        if tmp1 < ans:
            ans = tmp1
    print(ans)



def __starting_point():
    main()
__starting_point()

N = int(input())
l = list(map(int,input().split()))
l_ans = [[] for _ in range(201)]

for i in range(-100,101):
    sum = 0

    for j in range(N):
        sum = sum + (i-l[j])**2

    l_ans[i+100] = sum

print(min(l_ans))

n = int(input())
A = list(map(int,input().split()))
ans = 10**9
for i in range(-100,101):
  tmp = 0
  for a in A:
    tmp += (a-i)**2
  ans = min(ans, tmp)
print(ans)

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

avg = round((sum(a) / N))
ans = 0
for i in a:
  ans = ans + (i-avg) * (i-avg)

print(ans)

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

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

avg = round(sum(nums)/len(nums))

res = 0
for n in nums:
    res += cost(n, avg)
print(res)


import numpy as np

input()
a = np.array(sorted(map(int, input().split())))
print(min(sum((a - i) ** 2) for i in range(min(a), max(a) + 1)))

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

N = int(input())
A = [int(x) for x in input().split()]
from statistics import mean
from math import floor, ceil
meanA = mean(A)
if meanA < floor(meanA) + 0.5:
    meanA = floor(meanA)
else:
    meanA = ceil(meanA)
ans = 0
for a in A:
    ans += (a-meanA)**2
print(ans)

n = input()
a = list(map(int, input().split()))
ans = list()
for i in range(min(a), max(a)+1):
    cost = 0
    for num in a:
        cost += (num - i)**2
    ans.append(cost)
print(min(ans))

N=int(input())
a=list(map(int,input().split()))
ave=sum(a)/N
if ave>=0 and ave%1>=0.5:
    ave=int(ave)+1
elif ave<0 and -ave%1>=0.5:
    ave=int(ave)-1
else:
    ave=int(ave)
ans=0
for i in a:
    ans+=(i-ave)**2
print(ans)


# Nの入力受付
N = int(input())
# aの入力受付
aN = list(map(int, input().split()))
# 平均の値を計算
S = 0
A = 0
for i in aN:
    S += i
A = S / N
if A - int(A) < 0.5:
    A = int(A)
else:
    A = int(A) + 1
# Aにすべて書き換えるときのコストを計算
R = 0
for i in aN:
    R += (i - A)**2
print(R)


#!/usr/bin/env python

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

ans = 1000100
ch = 0 
for ch in range(min(a), max(a)+1):
    tmp = 0 
    for i in range(n):
        tmp += (a[i]-ch)**2
    if tmp <= ans:
        ans = tmp 
print(ans)