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):
import sys
readline = sys.stdin.buffer.readline

n = int(readline())
lst1 = list(map(int,readline().split()))

heikin = sum(lst1)//n
ans = 10**9
for i in (heikin,heikin+1):
    res = 0
    for j in lst1:
        res += (i-j)**2

    ans = min(ans,res)

print(ans)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
c=10**10
for i in range(min(a),max(a)+1):
  t=0
  for j in a:
    t+=(i-j)**2
  if c>t:
    c=t
print(c)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
ave=sum(a)//n
print(min(sum([pow(i-ave,2) for i in a]),sum([pow(i-ave-1,2) for i in a])))

Solution (Python):
from statistics import mean, median,variance
N = int(input())
A = list(map(int, input().split()))

m = round(mean(A))
cost = 0
for a in A:
  cost += (a-m)**2
print(cost)

Solution (Python):
N = int(input())
a = list(map(int , input().split()))
m = []
for i in range(201):
    k = i - 100
    s = 0
    for j in range (N):
        s = s + (a[j] - k)**2
    m.append(s)
print(min(m))

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

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
avg = sum(A)//n
avg2 = sum(A)//n + 1
ans = 0
ans2 = 0
for i in A:
    ans += (i-avg)**2
    ans2 += (i-avg2)**2
print(min(ans, ans2))

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
ans = 10**10
for i in range(-100, 101):
    t = 0
    for j in A:
        t += (j-i)**2
    if t < ans:
        ans = t
print(ans)

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
mini = 40000*100
for n in range(-100,101):
    cost = 0
    for item in A:
        cost += (item-n)**2
    if cost < mini:
        mini = cost
print(mini)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
r = round(sum(a)/len(a))
l = []
for i in a:
  l.append((i-r)**2)
print(sum(l))

Solution (Python):
N=int(input())
A=[int(x) for x in input().split()]
array=[]
for i in range(min(A),max(A)+1):
    S=0
    for j in range(len(A)):
        S+=(i-A[j])**2
    array.append(S)
print(min(array))

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

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

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

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

Solution (Python):

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

ave = round(sum(a) / n)
result = 0
for i in a:
    result += (i-ave) ** 2
print(result)

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

ans = 10**10
for i in range(min(a), max(a)+1) :
    temp = 0
    for n in a :
        temp += (i-n)**2
    ans = min(ans,temp)

print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
AVE = round(sum(A)/len(A))
print(sum((AVE - i)**2 for i in A))

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

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

high = max(a)
low = min(a)
cost = 0
ans = 10**9

for i in range(low,high+1):
    for j in a:
        cost += pow(j-i,2)
        
    if cost < ans:
        ans = cost
        cost = 0
    else:
        cost = 0
        
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):
    total = 0
    for b in a:
        total += (b-i)**2
    if total <= ans:
        ans = total
print (ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
m = 10 ** 9
for i in range(min(a), max(a) + 1):
    c = 0
    for j in a:
        c += (i - j) ** 2
    if m > c:
        m = c
print(m)

Solution (Python):
import sys

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7


N = I()
A = LMI()

avg = sum(A) // N

ans = float('inf')
for a in [avg - 1, avg, avg + 1]:
    cost = 0
    for b in A:
        cost += (a - b) ** 2
    ans = min(ans, cost)
print(ans)

Solution (Python):


n = int(input())
a = list(map(int,input().split()))
c = sum([(a[j]-min(a)) ** 2 for j in range(n)])
for i in range(min(a),max(a) + 1):
    b = [(a[j]-i) ** 2 for j in range(n)]
    s = sum(b)
    if s < c:
        c = s
print(c)

Solution (Python):
N=int(input())
a=input().split()
numbers=[]
for n in a:
    b=int(n)
    numbers.append(b)
a=round(sum(numbers)/N)
answers=[]
for z in numbers:
    y=(z-a)*(z-a)
    answers.append(y)
print(sum(answers))

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

if len(set(A)) == 1:
    print(0)
    exit()

v = 0
min_val = 10000000
for i in range(min(A), 101):
    v = 0
    for a in A:
        v += (a - i)**2
    if min_val > v:
        min_val = v

print(min_val)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
ans = 10**9
for ai in range(min(a), max(a)+1):
    w = 0
    for aj in a:
        w += (ai - aj)**2
    ans = min(w, ans)
print(ans)

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

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

Solution (Python):

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

Solution (Python):
import numpy as np

N =int(input())

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

a_min = min(a)
a_max =max(a)
ans = 0

for i in range(a_min,a_max+1):
    diff_a = a - i
    dummy = int(sum([i**2 for i in diff_a]))
    if i == a_min:
        ans = dummy
    elif i>a_min:
        if ans > dummy:
            ans = dummy

print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
x = -((-sum(a))//n)
y = x-1
result = 0
for i in range(n):
    result += (x-a[i])**2
result_y = 0
for i in range(n):
    result_y += (y-a[i])**2
print(min([result, result_y]))

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

mini = 10**10
for i in range(min(A), max(A)+1):
    count = 0
    for j in range(N):
        count = count + (A[j] - i)**2
    if count < mini:
        mini = count
print(mini)

Solution (Python):
import sys, math

def input():
    return sys.stdin.readline()[:-1]

def main():
    N = int(input())
    a = list(map(int,input().split()))
    ans = 10**9
    for k in range(-100,101):
        temp = 0
        for e in a:
            temp += (k-e)**2
        ans = min(ans,temp)
    print(ans)
if __name__ == '__main__':
    main()

Solution (Python):
from decimal import Decimal, ROUND_HALF_UP


def main():
    length = int(input())
    numbers = list(map(int, input().split()))
    target = Decimal(str(sum(numbers) / length)).quantize(Decimal('0'), rounding=ROUND_HALF_UP)
    answer = 0
    for num in numbers:
        answer += (num - target) ** 2
    print(answer)


if __name__ == '__main__':
    main()

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

m = int(1e9)
for i in range(-100, 101):
  l = 0
  for x in a:
    l += (x - i) ** 2
  m = min(m, l)
print(m)

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

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

def calc(lis,m):
    res=0
    for i in range(len(lis)):
        res+=(lis[i]-m)**2
    return res
  
mi=calc(a,a[0])
for i in range(a[0]+1,a[-1]+1):
    tmp=calc(a,i)
    if tmp<mi: mi=tmp
print(mi)

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

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

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

ret = 100 ** 100
for x in range(-100, 101):
    ret = min(ret, sum([(x - n) ** 2 for n in lst]))

print(ret)

Solution (Python):
n = int(input())
l = list(map(int,input().split()))
c = int(sum(l)/n)
ans = 10**10
for i in range(c-1,c+2):
    count = 0
    for j in l:
        count += (i-j)**2
    ans = min(ans,count)
print(ans)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
s=sum(a)
s//=n
cost=0
for i in a:
  cost+=(i-s)*(i-s)
ans=cost
s+=1
cost=0
for i in a:
  cost+=(i-s)*(i-s)
ans=min(ans,cost)
print(ans)

Solution (Python):
import numpy as np

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

if (a/a[0]).sum() == len(a):
    total = 0

total = 10**9
for i in range(a.min(), a.max()+1):
    total_new = ((a - i)**2).sum()
    if total_new <= total:
        total = total_new
print(total)

Solution (Python):
def main():
  n = int(input())
  nums = list(map(int, input().split()))
  cmin = 0
  for x in nums:
    cmin += (x - min(nums)) ** 2
  for i in range(min(nums), max(nums)+1):
    c = getcost(i, nums)
    if c < cmin:
      cmin = c
  print(cmin)
  
def getcost(x, nums):
  c = 0
  for n in nums:
    c += (n - x) ** 2
  return c
  
if __name__ == '__main__':
  main()

Solution (Python):
n=int(input())
A=list(map(int,input().split()))
ans=int(1000000000)
for i in range(-1000,1000):
    now=int(0)
    for j in range(n):
        now+=(i-A[j])*(i-A[j])
    if now<ans:
        ans=now
        
print(ans)

Solution (Python):



n = int(input())
a = [int(x) for x in input().split()]
ans = []
for i in range(min(a), max(a) + 1):
    ans.append(sum([(ax - i)**2 for ax in a]))
print(min(ans))

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

k=round(sum(a)/n)
ans=0

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

Solution (Python):


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

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

ans = 0

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

print(ans)

Solution (Python):


N = int(input())

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


num = int(round( sum(a) / len(a) ,0))

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

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

mi = min(A)
ma = max(A)

ans = 10**9

for k in range(mi, ma+1):
  tmp = 0
  for a in A:
    tmp += abs(a - k) ** 2

  if ans > tmp:
    ans = tmp

print(ans)

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

v = round(sum(a) / len(a))
ans = 0
for i in a:
    ans += (v - i)**2
print(ans)

Solution (Python):
N = int(input())
nums = input().split()
costs = []
if nums.count(nums[0]) != len(nums):
    for i in range(-100, 101):
        sum = 0
        for num in nums:
            sum += (int(num) - i) ** 2
        costs.append(sum)
    ans = min(costs)
else:
    ans = 0
print(ans)

Solution (Python):
import itertools
import math
n=int(input())
a=list(map(int,input().split()))
ave=round(sum(a)/n)

count=0
for i in range(n):
	count+=(a[i]-ave)**2
print(count)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
l = sum(A)
if l % N == 0:
  x = l // N
  s = 0
  for a in A:
    s += (a - x) ** 2
  print(s)
else:
  x = l // N
  y = x + 1
  s = 0
  t = 0
  for a in A:
    s += (a - x) ** 2
    t += (a - y) ** 2
  print(min(s, t))

Solution (Python):
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
n = int(input())
a = list(map(int,input().split()))
h = Decimal(str(sum(a)/n)).quantize(Decimal('0'), rounding=ROUND_HALF_UP)
a = list(map(lambda x:(x-h)**2,a))
print(sum(a))

Solution (Python):

import sys
try:
    from typing import List
except ImportError:
    pass


def solve(N: int, a: "List[int]"):
    print(min(sum((ai - y) ** 2 for ai in a) for y in range(-100, 101)))


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    a = [int(next(tokens)) for _ in range(N)]  
    solve(N, a)


if __name__ == '__main__':
    main()

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

mn = min(l)
mx = max(l)

ms = 10**9
for j in range(mn, mx+1):
    s = sum([(i-j)**2 for i in l])
    ms = min(ms, s)
    
print(ms)

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

mn = min(l)
mx = max(l)

ms = 10**9
ll = [0]*n
for j in range(mn, mx+1):
    for i in range(n):
        ll[i] = (l[i]-j)**2
        
    s = sum(ll)
    ms = min(ms, s)
    
print(ms)

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

mn = min(l)
mx = max(l)

ms = 10**9
for j in range(mn, mx+1):
    s = sum([(i-j)**2 for i in l])
    ms = min(ms, s)
    
print(ms)

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

mn = min(l)
mx = max(l)

ms = 10**9
for j in range(mn, mx+1):
    s = sum([(i-j)**2 for i in l])
    ms = min(ms, s)
    
print(ms)

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

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

Solution (Python):
n = int(input())
given = [int(x) for x in input().split()]
avgs  = round(sum(given) / n)
cost  = sum([(x - avgs) ** 2 for x in given])
print(cost)

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

Solution (Python):
N=int(input())
a=list(map(int,input().split()))
avg=0
for j in range(N):
    avg+=a[j]
avg=avg/N
t=int(avg)
if abs(t-avg)>abs(t+1-avg):
    t=t+1
cost=0
for j in range(N):
    cost+=(a[j]-t)**2
print(cost)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
answer = 10 ** 15
for i in range(-100,101):
    tmp = 0
    for m in A:
        tmp += (m-i) ** 2
    answer = min(answer, tmp)
print(answer)

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

ans = 100000000
for i in range(-100, 101):
    s = 0
    for j in a:
        s += (j - i) **2
    ans = min(ans , s)

print(ans)

Solution (Python):
n = int(input())
numlist = [int(i) for i in input().split()]
cost = 0
ave_f = sum(numlist) / n
ave_ch = sum(numlist) // n
if ave_f - ave_ch >= 0.5 :
    ave_i = ave_ch + 1
else :
    ave_i = ave_ch
for i in range(n) :
    cost += (ave_i - numlist[i]) ** 2
print(cost)

Solution (Python):


from decimal import Decimal, ROUND_HALF_UP

N = int(input())
a = list(map(int, input().split()))
m = Decimal(sum(a) / N).quantize(Decimal('0'), rounding=ROUND_HALF_UP) 



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

print(ans)

Solution (Python):

import sys

def solve(N: int, a: "List[int]"):
    min_cost = 10**9

    for target in range(-100,101):
        tmp = 0
        for i in range(N):
            tmp += (a[i]-target)**2
        min_cost = min(min_cost,tmp)
    print(min_cost)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    a = [int(next(tokens)) for _ in range(N)]  
    solve(N, a)

if __name__ == '__main__':
    main()

Solution (Python):




def main():
    N = int(input())
    A = list(map(int, input().split()))
    result = sum([(a - max(A))**2 for a in A])
    for i in range(min(A), max(A)):
        tmp = sum([(i - a)**2 for a in A])
        if result > tmp:
            result = tmp

    print(result)


if __name__ == "__main__":
    main()

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

ans = float("inf")

for i in range(-100, 101):
    t = 0
    for _a in a:
        t += (i-_a)**2
    ans = min(ans, t)

print(ans)

Solution (Python):
def S(a,N,M):
    m=0
    for i in range(N):
        m+=(a[i]-M)**2
    return m
    

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

m=S(a,N,-100)

for i in range(-99,101):
    if m > S(a,N,i):
        m=S(a,N,i)
        
print(m)

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
minA = min(A)
maxA = max(A)

ans = float("inf")
for i in range(minA, maxA+1):
    cost = 0
    for a in A:
        cost += (i - a)**2
    if cost < ans:
        ans = cost

print(ans)

Solution (Python):
n = int(input())
a = [int(e) for e in input().split()]
ans = float("inf")
MIN, MAX = min(a), max(a)
for i in range(MIN, MAX+1):
    cost = 0
    for j in a:
        cost = cost + (i-j)**2
    ans = min(ans, cost)
print(ans)

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

def solve(A):
    def cost(x):
        return sum((x -a)**2 for a in A)
    l = min(A)
    r = max(A)

    l = -100
    r = 100
    while r - l >= 3:
        d = (r - l) // 3
        m1 = l + d
        m2 = m1 + d
        
        L = [(cost(l), l), (cost(r), r), (cost(m1), m1), (cost(m2), m2)]
        
        (_, r), (_, l) = sorted(L)[:2]
        if l > r:
            r, l = l, r
        
    c, a = sorted([(cost(i), i)for i in range(l, r+1)])[0]
    print(c)


solve(A)

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

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

ave = round(sum(a_i) / len(a_i))
ans = 0
for x in a_i:
    ans += (x - ave) ** 2
print(ans)

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

ans = float('INF')
for i in range(-100, 101):
    sum = 0
    for n in range(N):
        sum += (a[n] - i) ** 2

    ans = min(ans, sum)

print(ans)

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

for i in range(min(AS),max(AS)+1):
  c = 0
  for a in AS:
    c += (i-a)**2
  if c < cost:
    cost = c
print(cost)

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

ans = float('INF')
for i in range(min(a), max(a)+1):
    cost = 0
    for aa in a:
        cost += (i - aa)**2
    if cost < ans :
        ans = cost
print(ans)

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, floor
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

n = INT()
A = LIST()
a = 0
ans = INF

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

    ans = min(ans, a)
print(ans)

    





exit()
ave = sum(L)/n
if ave == int(ave):
    print(int(ave))
    a = ave
    for i in range(n):
        ans += (L[i] - a) ** 2
    print(int(ans))
    exit()
left = 0
if ave != int(ave):
    for i in range(n):
        if L[i] < ave:
            left = i + 1
    print(left)
    right = n - left
    if left < right:
        a = ceil(ave)
    else:
        
        a = floor(ave)
    print(a)
    for i in range(n):
        ans += abs(L[i] - a) ** 2
    print(ans)

Solution (Python):
_ = int(input())
A = list(map(int, input().split()))
mi, ma = min(A), max(A)

cost = 10**10

for x in range(mi, ma+1):
    cost = min(cost, sum((x-a)**2 for a in A))

print(cost)

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, floor
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

n = INT()
A = LIST()
a = 0
ans = INF

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

    ans = min(ans, a)
print(ans)

    





exit()
ave = sum(L)/n
if ave == int(ave):
    print(int(ave))
    a = ave
    for i in range(n):
        ans += (L[i] - a) ** 2
    print(int(ans))
    exit()
left = 0
if ave != int(ave):
    for i in range(n):
        if L[i] < ave:
            left = i + 1
    print(left)
    right = n - left
    if left < right:
        a = ceil(ave)
    else:
        
        a = floor(ave)
    print(a)
    for i in range(n):
        ans += abs(L[i] - a) ** 2
    print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
c = float("inf")
for m in range(sum(A)//N-1,sum(A)//N+2):
    c = min(c, sum(map(lambda a: (a-m)**2, A)))
print(c)

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
def f(x):
    return sum([(a_i-x)**2 for a_i in A])
print(min([f(x) for x in range(-100, 101)]))

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

x = int(sum(A)/N)
cnt1 = 0
cnt2 = 0
for a in A:
    cnt1 += (a-x)**2
    cnt2 += (a-x-1)**2

print(min(cnt1, cnt2))

Solution (Python):
n = int(input())
l = list(map(int,input().split()))
a1 = 0
a2 = 0

c1 = int(sum(l)/n)
c2 = c1 + 1

for i in range(len(l)):
    a1 = a1 + (c1-l[i])**2
    a2 = a2 + (c2-l[i])**2
ans = min(a1,a2)
print(ans)

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

ans = 10**10
for i in range(-100, 101):
    tmp = 0
    for j in range(N):
        tmp += (i - a[j]) ** 2
    if tmp < ans:
        ans = tmp

print(ans)

Solution (Python):
n = int(input())
nums = list(map(int, input().split()))
nums_kazu = len(nums)
nums_sum = 0
average = 0
ans = 0
for i in nums:
    nums_sum += i
average = round(nums_sum/nums_kazu)

for i in nums:
     ans += abs(i - average)**2

print(ans)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
b=sum(A)//N
a=b+1
c=b+1

x=0
y=0
z=0
for i in A:
  x+=(a-i)**2
  y+=(b-i)**2
  z+=(c-i)**2
print(min(x,y,z))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
mean = sum(a) / n
if mean >= -0.5:
  common = int(mean + 0.5)
else:
  common = int(mean - 0.5)
cost = 0
for i in range(n):
  cost += (a[i] - common) ** 2
print(cost)

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

m = int(((sum(a)/n)*2 + 1) //2)


r = 0
for i in a:
  r += (i - m)**2
  
print(r)

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


x = sum(a)/N
x_large = math.ceil(x)
x_small = math.floor(x)


sum_large = 0
sum_small = 0
for i in a:
	sum_large += (x_large-i)**2
	sum_small += (x_small-i)**2

print(min(sum_large,sum_small))

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
def dist(x):
  ret = 0
  for i in A:
    ret+=(x-i)**2
  return ret
ans = dist(100)
for X in range(-100,100):
  ans=min(ans,dist(X))
print(ans)

Solution (Python):
n = int(input())
s = [int(c) for c in input().split()]
def solution(s):
    cost = 0
    mean = round(sum(s)/len(s))
    for i in s:
        cost += abs(i-mean) ** 2
        
    return cost
  
print(solution(s))

Solution (Python):
import math
import sys
from functools import reduce
input = sys.stdin.readline

def fullsearch(a):
    for i in range(-100, 101):
        yield reduce(lambda total, x: (x - i) ** 2 + total, a, 0)

def main():
    input()
    A = [int(c) for c in input().split()]

    print(min(fullsearch(A)))

main()

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

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

Solution (Python):
import numpy as np
n=int(input())
a=np.array([int(x) for x in input().split()])
x=[]
for i in range(-100,101):
	s=np.sum((a-i)*(a-i))
	x.append(s)
print(min(x))

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
a.sort()
ans = 10**9
for i in range(a[0],a[len(a)-1]+1):
    temp = sum([(abs(j-i))**2 for j in a])
    ans = min(ans,temp)
print(ans)

Solution (Python):
def solve(n, A):
    ave = round(sum(A) / n)
    return sum((a - ave) ** 2 for a in A)

_n = int(input())
_A = list(map(int, input().split()))
print(solve(_n, _A))

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

Solution (Python):
import sys
input = sys.stdin.readline

def main():
    N = int(input())
    A = [int(x) for x in input().split()]


    ans = float("inf")
    for j in range(-101, 101):
        tmp = 0
        for i in A:
            tmp += (i - j) ** 2

        ans = min(ans, tmp)

    print(ans)


    
    

if __name__ == '__main__':
    main()

Solution (Python):
def pow_k(x, n):
    """
    O(log n)
    """
    if n == 0:
        return 1

    K = 1
    while n > 1:
        if n % 2 != 0:
            K *= x
        x *= x
        n //= 2

    return K * x

N=int(input())
L=list(map(int,input().split()))
import sys
ans = sys.maxsize

for i in range(-100,101):
  m_diffs = 0
  for j in range(N):
    m_diffs += pow_k((L[j] - i),2)
  ans = min(ans, m_diffs)

print(ans)

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

average = (sum(a) + N // 2) // N
cost = 0

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

print(cost)

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

answer = 10 ** 15
for i in range(-100, 101):
  tmp = 0
  for a in A:
    tmp += (a - i) ** 2
  answer = min(answer, tmp)

print(answer)

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



median = nums[n//2]
mean = sum(nums) // n

ans = 10 ** 10
for diff in range(min(median, mean), max(median, mean)+2):
    diff_sum = 0
    for num in nums:
        diff_sum += (num - diff) ** 2
    ans = min(ans, diff_sum)

print(ans)

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

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
def main():
    N = I()
    A = LI()
    ans = inf
    for targ in range(-100, 101):
        s = sum([(x - targ) ** 2 for x in A])
        ans = min(s, ans)
    print(ans)
main()

Solution (Python):
N = int(input())
nolist = list(map(int, input().split()))
minimum = 10**9
for i in range(min(nolist), max(nolist)+1):
  cost = sum((i-nolist[j])**2 for j in range(N))
  if cost < minimum:
    minimum = cost
print(minimum)

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

sD = sum(dList)/n
ig = int(sD)
po = sD-int(sD)

ad = 0
if abs(po) >= 0.5:
  if sD >= 0:
    ad = ig + 1
  else:
    ad = ig -1
else:
  ad = ig

result = 0
for i in range(len(dList)):
  result += (dList[i]-ad)**2

print(result)

Solution (Python):


if __name__ == "__main__":
    N = int(input())
    a_str = list(map(int, input().split(" ")))

    min_a = min(a_str)
    max_a = max(a_str)

    min = float("inf")
    ans = 0
    for i in range(min_a, max_a):

        temp = sum(list(map(lambda x: (x - i)**2, a_str)))
        if min > temp:
            min = temp
            ans = temp
    print(ans)

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

cost = []

for i in range(-100,101):
    cc = 0
    for j in range(n):
        cc += (i - a[j])*(i - a[j])
    cost.append(cc)

cost.sort()
print(cost[0])

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

num_of_equal = sum([1 for i, j in zip(a[:-1], a[1:]) if i == j]) + 1

if num_of_equal == len(a): print(0)
else:
    answer = []
    for y in range(min(a), max(a)+1):
        answer.append(sum([(x - y)**2 for x in a]))

    print(min(answer))

Solution (Python):
import sys
input = sys.stdin.readline
n=int(input())
A=list(map(int,input().split()))
ans=float("inf")
for i in range(-100,101):
    s=0
    for a in A:
        s+=(a-i)**2
    ans=min(ans,s)
print(ans)

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

ave1 = sum(a) // len(a)
ave2 = ave1 + 1

cost1 = 0
for i in range(n):
    cost1 += (a[i] - ave1)**2

cost2 = 0
for i in range(n):
    cost2 += (a[i] - ave2)**2

print(min(cost1,cost2))