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 main():
    N = int(input())
    A = list(map(int, input().split()))
    ans = float('inf')
    for k in range(min(A), max(A)+1):
        cost = 0
        for a in A:
            cost += (a-k)**2
        ans = min(ans, cost)
        if ans == 0:
            break
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
ans = 4000000
for a in range(-100, 101):
  m = 0
  for b in A:
    m += (a - b) ** 2
  if m < ans:
    ans = m
print(ans)

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

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

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
avg_a = sum(a) // n
avg_b = avg_a + 1
total_one = 0
total_two = 0
for num in a:
    total_one += (num - avg_a) ** 2
    total_two += (num - avg_b) ** 2
print(min(total_one, total_two))

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

x = sum(aa) // len(aa)
cost1 = 0
cost2 = 0
for i in range(n):
  cost1 += (x - aa[i])**2
  cost2 += (x + 1 - aa[i])**2
print(min(cost1, cost2))

Solution (Python):
n = int(input())
x = sorted(list(map(int, input().split())), reverse = True)
x_2 = list(map(lambda x:x ** 2, x))
ichi = sum(x)
jijou = sum(x_2)
re = 10 ** 100






for p in range(x[-1], x[0] + 1):
    second = n * (p ** 2)
    third = -2 * p * ichi
    summ = jijou + second + third

    if re > summ:
        re = summ

print(re)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
s=sum(a)
m=round(s/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()))

X = round(sum(A)/N)

ans = 0
for a in A:
    ans += (X-a)**2
print(ans)

Solution (Python):
from decimal import Decimal, ROUND_HALF_UP
N = int(input())
a = list(map(int, input().split()))
f = sum(a)/N
t = Decimal(str(f)).quantize(Decimal('0'), rounding=ROUND_HALF_UP)
c = 0
for i in range(N):
    c += (a[i]-t)**2
print(c)

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

p = sum(A)//n
q = p+1
r = p-1

ans = float("inf")
for x in [p,q,r]:
    tmp = 0
    for a in A:
        tmp += (a-x)**2
    ans = min(ans, tmp)
    
print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
med_l = int(sum(a)/n)
med_h = med_l + 1
sum_l = 0
sum_h = 0

for i in range(n):
  sum_l += (med_l - a[i])**2
  sum_h += (med_h - a[i])**2
  
print(min(sum_l, sum_h))

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

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

Solution (Python):
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    n = I()
    a_lst = LI()
    ans = float("inf")
    for i in range(-100,100+1):
        cost = 0
        for a in a_lst:
            cost += (a-i)**2
        ans = min(ans, cost)
    print(ans)
main()

Solution (Python):
import math
n = int(input())
a = sorted(map(int, input().split()))
ans = []
for i in range(a[0], a[-1] + 1):
  c = 0
  for j in a:
    c += (j - i) ** 2
  ans.append(c)
print(min(ans))

Solution (Python):
import sys
from sys import exit
from collections import deque
from bisect import bisect_left, bisect_right, insort_left, insort_right 
from heapq import heapify, heappop, heappush
from itertools import product, permutations, combinations, combinations_with_replacement
from functools import reduce
from math import sin, cos, tan, asin, acos, atan

sys.setrecursionlimit(10**6)
INF = 10**20
eps = 1.0e-20
MOD = 10**9+7

def lcm(x,y):
    return x*y//gcd(x,y)
def lgcd(l):
    return reduce(gcd,l)
def llcm(l):
    return reduce(lcm,l)
def powmod(n,i,mod):
    return pow(n,mod-1+i,mod) if i<0 else pow(n,i,mod)
def div2(x):
    return x.bit_length()
def div10(x):
    return len(str(x))-(x==0)
def perm(n,mod=None):
    ans = 1
    for i in range(1,n+1):
        ans *= i
        if mod!=None:
            ans %= mod
    return ans
def intput():
    return int(input())
def mint():
    return map(int,input().split())
def lint():
    return list(map(int,input().split()))
def ilint():
    return int(input()), list(map(int,input().split()))
def judge(x, l=['Yes', 'No']):
    print(l[0] if x else l[1])
def lprint(l, sep='\n'):
    for x in l:
        print(x, end=sep)
def ston(c, c0='a'):
    return ord(c)-ord(c0)
def ntos(x, c0='a'):
    return chr(x+ord(c0))
class counter(dict):
    def __init__(self, *args):
        super().__init__(args)
    def add(self,x):
        self.setdefault(x,0)
        self[x] += 1
class comb():
    def __init__(self, n, mod=None):
        self.l = [1]
        self.n = n
        self.mod = mod
    def get(self,k):
        l,n,mod = self.l, self.n, self.mod
        k = n-k if k>n//2 else k
        while len(l)<=k:
            i = len(l)
            l.append(l[i-1]*(n+1-i)//i if mod==None else (l[i-1]*(n+1-i)*powmod(i,-1,mod))%mod)
        return l[k]

N,A = ilint()
ans = INF
for x in range(-100,101):
    tmp = 0
    for y in A:
        tmp += (x-y)**2
    ans = min(ans,tmp)
print(ans)

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

Solution (Python):
n, *a = map(int, open(0).read().split())
if sum(a) % n == 0:
    avg = sum(a)//n
    ans = 0
    for e in a:
        ans += (e - avg)**2
    print(ans)
else:
    avg = sum(a)//n + 1
    avg2 = sum(a)//n - 1
    avg3 = sum(a)//n
    ans2 = 0
    ans = 0
    ans3 = 0
    for e in a:
        ans += (e - avg) ** 2
        ans2 += (e-avg2) ** 2
        ans3 += (e-avg3) ** 2
    print(min(ans, ans2, ans3))

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

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

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
ans = 10000000000
for num in range(-100, 101):
    kari_ans = 0
    for _a in a:
        kari_ans += (num - _a) ** 2
    ans = min(ans, kari_ans)
print(ans)

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

Solution (Python):
import math
from math import gcd,pi,sqrt
INF = float("inf")

import sys
sys.setrecursionlimit(10**6)
import itertools
from collections import Counter,deque
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]


def main():
    n = i_input()
    a = i_list()
    a_min = min(a)
    a_max = max(a)
    ans = INF
    for i in range(a_min,a_max+1):
        trial = 0
        for j in a:
            trial += (i-j)**2
        ans = min(ans,trial)
    print(ans)



if __name__=="__main__":
    main()

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
mn,mx=min(a),max(a)
m=10**9
for i in range(mn,mx+1):
	m=min(m,sum([(t-i)**2 for t in a]))
print(m)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
Sum =0
for i in range(len(A)):
  Sum += (A[0]-A[i])**2
Answer = Sum
for j in range(-100,101):
  Sum =0
  for k in range(len(A)):
    Sum+= (A[k]-j)**2
  Answer =min(Answer, Sum)
print(Answer)

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

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

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

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

ave = sum(a) // n
ans1 = sum((ai - ave)** 2 for ai in a)
ans2 = sum((ai - ave - 1)** 2 for ai in a)
print(min(ans1,ans2))

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

from collections import Counter
c=Counter(a)
avr=sum(a)//n
c[avr]=0
if sum(a)%n!=0:
	c[-(-sum(a)//n)]=0
ans = abs(max(a)-min(a))**n
for key in c:
	res=0
	for aa in a:
		res+=(aa-key)**2
	ans=min(ans,res)
print(ans)

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

def cost(number):
    res = 0
    for x in a:
        res += (x - number) ** 2
    return res

ans = 1001001001
for i in range(-100, 101):
    tmp = cost(i)
    if tmp < ans:
        ans = tmp

print(ans)

Solution (Python):


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

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

print(ans)

Solution (Python):
def cost(x,a,b):
    cost=0
    for i in range(b):
        cost+=(x-a[i])**2
    return cost

n=int(input())
a=list(map(int,input().split()))
ans=0
for i in range(n):
    ans+=a[i]
ans/=n
ans=int(ans)
print(min(cost(ans,a,n),cost(ans+1,a,n)))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
t = round(sum(a) / n)

print(sum([(i - t) ** 2 for i in a]))

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

x = round(sum(a) / n)

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

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
mi = min(a)
ma = max(a)
if mi == ma:
    print(0)
    exit()
ans = 0
for i in a:
    ans += (i - mi)**2
for i in range(mi + 1, ma + 1):
    val = 0
    for j in a:
        val += (i - j)**2
    ans = min(ans, val)
print(ans)

Solution (Python):
import numpy as np
from math import ceil

n = int(input())
a = list(map(int, input().split()))
a = np.array(a)
ans = 10**9
for i in range(-100,101):
    ans = min(ans,(np.abs(a-i)**2).sum())
print(ans)

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

ans = []
for num in range(101):
    cost1, cost2 = 0, 0
    for a in A:
        cost1 += (a-num)**2
        cost2 += (a-(-num))**2
    ans.append(cost1)
    ans.append(cost2)
print(min(ans))

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

mean = round(sum(a)/n)

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

print(cnt)

Solution (Python):
a = lambda :map(int,input().split())
lma = lambda :list(map(int,input().split()))
tma = lambda :tuple(map(int,input().split()))
ni = lambda:int(input())
yn = lambda fl:print("Yes") if fl else print("No")
import collections
import math
import itertools
import heapq as hq

n = ni()
A = lma()
ans = 10**9
for i in range(-100,101):
    tmp=0
    for a1 in A:
        tmp+=(i-a1)**2
    ans = min(tmp,ans)
print(ans)

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

ans = 10**10
for i in range(-200, 201, 1):
    tmp = 0
    for a in A:
        tmp += (a-i)**2
    ans = min(ans, tmp)
print(ans)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
ans = float('inf')
check = 0
for i in range(-100,100+1):
    for j in A:
        check +=(j-i)**2
    ans = min(ans,check)
    check=0
print(ans)

Solution (Python):
import sys

n = int(input())
a = list(map(int,input().split()))
s = ((sum(a))//n)+1
t = sum(a)//n
u = ((sum(a))//n)-1
l = []
l.append(s)
l.append(t)
l.append(u)
ans = []
ans2 = []
aa = set(a)

if len(aa) == 1:
    print(0)
    sys.exit()

for j in range(len(l)):
    ans = []
    for i in range(n):
        if a[i] == l[j]:
            continue
        else:
            b = (a[i]-l[j])**2
            ans.append(b)
    ans2.append(sum(ans))


print(min(ans2))

Solution (Python):
N = int(input())
AAA = map(int,input().split())
AA = list(AAA)
AA.sort()
mini = 10**7
x = AA[-1]
for i in range(AA[0],x+1):
  total = 0
  for j in AA:
    total += (j-i)**2
  if total < mini:
    mini = total
print(mini)

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

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

mu=sum(a)//n

print(min(sum([(aa-mu)**2 for aa in a]),sum([(aa-mu-1)**2 for aa in a])))

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

ans = float("INF")
for k in range(-100, 100+1):
    tmp = 0
    for a in A:
        tmp += pow(a - k, 2)
    ans = min(ans, tmp)

print(ans)

Solution (Python):






INF = 10**9


def solve(n,a):
  ans = INF
  k = min(a)
  while k <= max(a):
    ans = min(ans,sum((x-k)**2 for x in a))
    k += 1
  return ans



def main():
  N = int(input())
  
  a = list(map(int,input().split()))
  print(solve(N,a))
  return

if __name__ == '__main__':
  main()

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

ans=1000000
for i in range(min(A),max(A)+1):
    cost=0
    for x in range(len(A)):
        cost+=(A[x]-i)**2
    ans=min(ans,cost)

print(ans)

Solution (Python):
import sys

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

tin=lambda : map(int, input().split())
lin=lambda : list(tin())
mod=1000000007



def main():
	n = int(input())
	
	
	al = lin()
	ret = mod
	for i in range(-102, 102):
		v = sum([(x-i)**2 for x in al])
		ret = min(v, ret)
	print(ret)
	
	
	

isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		
			
	ret = main()
	if ret is not None:
		print(ret)

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

ave = sum(al)//n
ans = 10**9
for y in range(n-5000,n+5000+1):
    cost = 0
    for x in al:
        cost += (x - y)**2
    ans = min(ans,cost)
print(ans)

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
av=sum(l)//n
sm=0
sm1=0
for m in l:
  sm+=(m-av)**2
  sm1+=(m-av-1)**2
print(min(sm,sm1))

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

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

res = 10**10
for i in range(min(A), max(A)+1):
    x = 0
    for a in A:
        x += abs(a-i)**2
    res = min(x, res)

print(res)

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

M=float("inf")
for x in range(-100,101):
    K=0
    for a in A:
        K+=(a-x)*(a-x)
    M=min(M,K)
print(M)

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

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

Solution (Python):

n, *a = map(int, open(0).read().split())
p = round(sum(a) / n)
print(sum((i - p)**2 for i in a))

Solution (Python):

n, *a = map(int, open(0).read().split())
p = sum(a) // n
q = -(-sum(a) // n)
print(min(sum((i - p)**2 for i in a), sum((i - q)**2 for i in a)))

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

s = math.ceil(sum(A)/N)
t = math.floor(sum(A)/N)

ans_s = 0
ans_t = 0
for a in A:
    ans_s += (a-s)**2
    ans_t += (a-t)**2
print(min(ans_s, ans_t))

Solution (Python):
import sys, math
from itertools import combinations
sys.setrecursionlimit(500000)
MOD = 10**9+7

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

def mi():
    return map(int, input().split())

def ii():
    return int(input())

def i2(n):
    tmp = [list(mi()) for i in range(n)]
    return [list(i) for i in zip(*tmp)]

def main():
    N = ii()
    a = list(mi())
    m = 10**100
    for K in range(-100, 101):
        tmp = sum((a[i]-K)**2 for i in range(N))
        m = min(m, tmp)
    print(m)

if __name__ == '__main__':
    main()

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
amax=max(a)
amin=min(a)
ans=1000000000000
for i in range(amin,amax+1):
  t=0
  for j in range(n):
    t=t+(a[j]-i)**2
  ans=min(t,ans)
print(ans)

Solution (Python):
n=int(input())
ar=list(map(int,input().split()))
def f(i):
    ans=0
    for e in ar:
        ans+=(e-i)**2
    return ans
cur=float('inf')
for i in range(-200,200):
    cur=min(cur,f(i))
print(cur)

Solution (Python):






from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN





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





x = int(Decimal(str(sum(a)/N)).quantize(Decimal('0'), rounding=ROUND_HALF_UP))
ans = 0
for i in range(N):
    ans += (x-a[i])**2
print(ans)

Solution (Python):
INF = 10 ** 18
N = int(input())
A = list(map(int, input().split()))
up = max(A)
low = min(A)
res = INF
for i in range(low, up + 1):
    t = 0
    for a in A:
        t += (i - a) ** 2
    res = min(res, t)
print(res)

Solution (Python):
import math
n = int(input())
a = list(map(int,input().split()))
p = 0
if sum(a) % n == 0:
    b = sum(a) // n
    for i in range(n):
        p += abs(a[i]-b) ** 2
    print(p)
else:
    b = sum(a) / n
    x = math.floor(b)
    y = math.ceil(b)
    q = 0
    for i in range(n):
        p += abs(a[i]-x) ** 2
        q += abs(a[i]-y) ** 2
    print(min(p,q))

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):
def resolve():
    '''
    code here
    '''
    import math
    N = int(input())
    As = [int(item) for item in input().split()]

    
    res = 2000**2

    for mid in range(min(As), max(As)+1):
        temp_res = 0
        for item in As:
            temp_res += (item - mid)**2
        res = min(temp_res, res)
    print(res)

if __name__ == "__main__":
    resolve()

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


ans = float('inf')

mean = sum(A)//n
means = [mean-1, mean, mean+1]
for m in means:
    tmp = 0
    for a in A:
        tmp += (m - a)**2
    ans = min(ans, tmp)

print(ans)

Solution (Python):
def main():
    n = int(input())
    A = list(map(int, input().split()))
    m = int(sum(A) / len(A) + 0.5)
    ans = float('inf')
    for i in range(m - 1, m + 2):
        ans = min(ans, sum([(a - i) ** 2 for a in A]))
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import math

N = int(input())
a_li = list(map(int, input().split()))
mid_1 = math.ceil(sum(a_li) / N)
mid_2 = sum(a_li) // N
ans_1 = 0
ans_2 = 0
for a in a_li:
    ans_1 += (a - mid_1) ** 2
for a in a_li:
    ans_2 += (a - mid_2) ** 2
print(min(ans_1, ans_2))

Solution (Python):

import sys
from collections import deque, defaultdict
from math import sqrt, factorial, gcd, ceil



import string

from bisect import bisect_left

def solve():
    n = int(input())
    a = [int(x) for x in input().split()]
    ans = float('inf')
    for y in range(min(a), max(a) + 1):
        sm = 0
        for x in a:
            sm += (x - y) ** 2
        ans = min(ans, sm)
    print(ans)

t = 1

for case in range(1,t+1):
    ans = solve()


"""

azyxwvutsrqponmlkjihgfedcb

"""

Solution (Python):
import sys,math
import numpy as np
input = sys.stdin.readline

N = int(input())
a = np.array(list(map(int,input().split())))
c = int(round(sum(a)/N))

print( sum((a-c)**2))

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 a:
    tmp+=(j-i)**2
  ans=min(ans,tmp)
print(ans)

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

x = sum(a) // n
count1 = 0
count2 = 0
for i in range(n):
    count1 += (a[i] - x)**2
    count2 += (a[i] - (x + 1))**2

print(min(count1, count2))

Solution (Python):
N=int(input())
A=list(map(int, input().split()))
cent=sum(A)/N
if abs(cent-int(cent))>=0.5:
  if cent<0:
    cent=int(cent)-1
  else:
    cent=int(cent)+1
else:
  cent=int(cent)
ans=0
for a in A:
  ans+=(a-cent)**2
print(ans)

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

ave = sum(a)//n
ans1 = sum((ai - ave) ** 2 for ai in a)
ans2 = sum((ai - ave - 1) ** 2 for ai in a)
print(min(ans1,ans2))

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

ans1 = sum((ai - sum(a) // n) ** 2 for ai in a)
ans2 = sum((ai - sum(a) // n - 1) ** 2 for ai in a)
print(min(ans1,ans2))

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

ave = sum(a) // n
ans = sum((ai - ave)** 2 for ai in a)
ans = min(ans, sum((ai - ave - 1)** 2 for ai in a))
print(ans)

Solution (Python):


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


if __name__ == "__main__":
    main()

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

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

Solution (Python):
import bisect,copy,heapq,string
from collections import *
from math import *
from itertools import *
import sys
sys.setrecursionlimit(10 ** 6)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n = INT()
a = LIST()

ans = inf
for i in range(-100,101):
    tmp = 0
    for j in range(len(a)):
        tmp += (i-a[j])**2
    ans = min(ans, tmp)

print(ans)

Solution (Python):
if __name__ == '__main__':
    n = int (input())
    a = [int(i) for i in input().split()]
    minN = min(a)
    maxN = max(a)
    cost = -1
    for i in range(minN-1,maxN+1):
        temp =0
        for j in a:
            temp+=(i-j)*(i-j)
        if cost == -1 or ( cost > temp):
            cost = temp
    print(cost)

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

INF = 10**18
ans = INF
for X in range(-100, 101):
    cost = sum(map(lambda a: (a-X)**2, A))
    if cost < ans:
        ans = cost
print(ans)

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

ans = 10 ** 10

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

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

Solution (Python):
import sys


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()


INF = float('inf')

n = I()
a = LI()
ans = INF
for i in range(-100, 101):
    cost = 0
    for j in a:
        cost += (i - j) ** 2
    ans = min(ans, cost)
print(ans)

Solution (Python):
n = int(input())
ps = list(map(int, input().split()))
 
p_avg = round(sum(ps)/len(ps))
 
print(sum([abs(p-p_avg)**2 for p in ps]))

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

p_min = min(ps)
p_max = max(ps)

a = 999999999999999999999

for i in range(p_min, p_max+1):
    b = sum([abs(p-i)**2 for p in ps])
    a = min(a, b)

print(a)

Solution (Python):


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

ans = float('inf')
for i in range(-100, 101):
    t = 0
    for num in nums:
        t += (num - i) ** 2
    ans = min(ans, t)
print(ans)

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

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

Solution (Python):
import math

n = int(input())
l = list(map(int, input().split()))
avg1 = sum(l)//n
avg2 = math.ceil(sum(l)/n)

res1 = 0
res2 = 0
for i in range(n):
    res1 += abs(l[i] - avg1)*abs(l[i] - avg1)
    res2 += abs(l[i] - avg2)*abs(l[i] - avg2)
print(min(res1, res2))

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


def log(*args):
    print(*args, file=sys.stderr)


def main():
    ans = 1000000000
    _n = int(input())
    a = list(map(int, input().rstrip().split()))
    for i in range(-100, 101):
        tmp = 0
        for v in a:
            tmp += pow((v - i), 2)
        ans = min(tmp, ans)
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
b = sum(A) / n
if b - int(b) < 0.5:
    b = int(b)
else:
    b = int(b) + 1
ans = 0
for a in A:
    ans += (b - a) ** 2
print(ans)

Solution (Python):



N = int(input())
A = list(map(int, input().split()))
candi1 = sum(A) // N
candi2 = candi1 + 1

ans1 = ans2 = 0
for a in A:
    ans1 += (a - candi1)**2
    ans2 += (a - candi2) ** 2
print(min(ans1, ans2))

Solution (Python):
n = int(input())
a_li = list(map(int,input().split()))
a_min = min(a_li)
a_max = max(a_li)
a_average = round(sum(a_li)/n)
ans1 = 0
ans2 = 0
ans3 = 0
for i in range(n):
    ans1 += (a_min-a_li[i])**2
    ans2 += (a_max-a_li[i])**2
    ans3 += (a_average-a_li[i])**2
print(min(ans1,ans2,ans3))

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
ans=10**9+7
for tar in range(-100,101):
    cost=0
    for i in a:
        cost+=(tar-i)**2
    if cost<ans: ans=cost
print(ans)

Solution (Python):


import sys
read = sys.stdin.read
readline = sys.stdin.readline

n,*a = map(int,read().split())

ans = 1<<60

for i in range(-101,101):
    v = 0
    for ai in a:
        v += (ai-i)**2
    
    ans = min(ans,v)


print(ans)

Solution (Python):
import numpy as np
n=int(input())
a=list(map(int,input().split()))
a=np.array(a)
all=np.sum(a)
c=int(all/n+0.5)
d=int(all/n)
ans1=a-c
ans1=ans1**2
ans2=a-d
ans2=ans2**2
ans1=np.sum(ans1)
ans2=np.sum(ans2)
if ans1<=ans2:
    print(ans1)
else:
    print(ans2)

Solution (Python):
import math

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

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

print(b)

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

ans=1001001001001
cost_sum=0

for i in range(min(a),max(a)+1):
    for j in range(n):
        cost=(a[j]-i)**2
        cost_sum+=cost
        if ans < cost_sum:
            continue
    if ans > cost_sum:
        ans = cost_sum
    cost_sum=0
print(ans)

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

Solution (Python):
from sys import stdin, setrecursionlimit


def main():
    input = stdin.buffer.readline
    n = int(input())
    a = list(map(int, input().split()))
    x1 = sum(a) // n
    x2 = x1 + 1
    ans1 = 0
    ans2 = 0
    for ai in a:
        ans1 += (ai - x1) ** 2
        ans2 += (ai - x2) ** 2

    print(min(ans1, ans2))


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Solution (Python):
n = int(input())
alst = list(map(int, input().split()))
ans = 10 ** 10
min_a = min(alst)
max_a = max(alst)
for y in range(min_a, max_a + 1):
    tmp = 0
    for num in alst:
        tmp += (num - y) ** 2
    ans = min(ans, tmp)
print(ans)

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

med=sum(a)/n

med_1=int(med)
med_2=int(med)+1

ans_1=0
ans_2=0
for i in range(n):
    ans_1+=(a[i]-med_1)**2
    ans_2+=(a[i]-med_2)**2
print(min(ans_1,ans_2))

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
ans=10**9+7
for b in range(-100,101):
    cnt=0
    for a in A:
        cnt+=(a-b)**2
    ans=min(ans,cnt)
print(ans)

Solution (Python):





import math
import fractions
import sys
import bisect
import heapq  
import collections
from collections import Counter
from collections import deque
import pprint

sr = lambda: input()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

"""nを素因数分解"""
"""2以上の整数n => [[素因数, 指数], ...]の2次元リスト"""
def factorization(n):
    arr = []
    temp = n
    if n == 1:
        return arr

    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr


def power(a,n,mod):
    x = 1
    while n:
        if n & 1:
            x *= a % mod
        n >>= 1
        a *= a % mod
    return x % mod


def kaijo(n, l, mod):
    if n == 0:
        return 1
    a = n
    tmp = n-1
    while(tmp >= l):
        a = a*tmp%mod
        tmp -= 1
    return a

inf = 10**18
mod = 10**9+7

n = ir()
a = lr()

total = 0
for num in a:
    total+=num
mean_min = total//n

ans = [0,0]
for i in range(mean_min, mean_min+2):
    ind = i%2
    for num in a:
        ans[ind]+=(num-i)*(num-i)
print(min(ans))