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

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

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

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



if __name__ == '__main__':
    main()

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

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

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

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

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

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

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

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

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

Solution (Python):

N = int(input())

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

R = 0
for i in aN:
    R += (i - A)**2
print(R)

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

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

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
mi=min(a)
ma=max(a)
i=mi
ans=10**10
while i<=ma:
    x=sum((a[j]-i)**2 for j in range(n))
    ans=min(ans,x)
    i+=1
print(ans)

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

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


if __name__ == '__main__':
    main()

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

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

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

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

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

if __name__ == '__main__':
    solve()

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

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

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

Solution (Python):
N=int(input())
*A,=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)

Solution (Python):

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

if __name__ == "__main__":
    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)

Solution (Python):

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



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)]  
    print(solve(N, a))

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

if __name__ == '__main__':
    
    main()

Solution (Python):
import sys, os, math, bisect, itertools, collections, heapq, queue, copy, array





sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)

MOD = 10 ** 9 + 7
INF = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    A = il()

    ret = INF
    for cost in range(min(A) - 1, max(A) + 1):
        tmp = 0
        for n in range(N):
            tmp += (A[n] - cost) ** 2
        ret = min(ret, tmp)
    print(ret)


if __name__ == '__main__':
    main()

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

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

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

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

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

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

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


if __name__ == "__main__":
    main()

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

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

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

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

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()

def main():
    N = int(input())
    A = list(map(int, input().split()))
    c = sum(A) // N
    ans = 10**10
    for x in range(c, c + 2):
        val = 0
        for a in A: val += (x - a) ** 2
        ans = min(ans, val)
    print(ans)

if __name__ == "__main__":
    main()

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

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

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

cost_list = []

for i in range (-100,101):
    cost = 0
    for j in l:
        cost += (j - i)**2
    cost_list.append(cost)
cost_list.sort()
print(cost_list[0])

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

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

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

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

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

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

Solution (Python):
n=int(input())
a = list(map(int,input().split()))
_sum =sum(a) / n
s  = round(_sum)
ans = 0

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

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

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

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

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

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

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

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

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


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

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

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

Solution (Python):

n = int(raw_input())
ais = map(int, raw_input().split())
ais.sort()
vv= sum(ais)/len(ais)
def g(ais,m):
	s= 0
	for ai in ais: s+=abs(ai - m) **2
	return s
print min([g(ais,m) for m in [vv+dela for dela in [-1,0,+1]]])

Solution (Python):

n = int(raw_input())
ais = map(int, raw_input().split())
ais.sort()

def f(ais):
	return [sum(ais)/len(ais),sum(ais)/len(ais)+1,sum(ais)/len(ais)-1]
	if len(ais) % 2:
		return [ais[len(ais)/2]]
	
	v = ais[len(ais)/2 -1] + ais[len(ais)/2]
	return [v/2,v/2-1,v/2+1]
def g(ais,m):
	s= 0
	for ai in ais: s+=abs(ai - m) **2
	return s
print min([g(ais,m) for m in f(ais)])

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

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

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

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



if __name__ == "__main__":
    main()

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

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


if __name__ == '__main__':
    main()

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

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

res = float('inf')
for i in range(-100, 101):
    s = 0
    for v in a:
        s += (v - i) ** 2
    res = min(res, s)

print(res)

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

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

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

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

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

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

a.sort()

def cost(a, y):
    ans = 0
    for i in a:
        ans += (i - y) * (i - y)
    return ans



left = a[0]
right = a[n-1]

while left < right:
    center = (left + right) // 2
    if cost(a, center) < cost(a, center+1):
        right = center
    else:
        left = center + 1

print(cost(a, left))

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

Solution (Python):
from math import *
n=int(input())
l=list(map(int,input().split()))
s=ceil(sum(l)/n)
ss=s-1
c=0
x=0
for i in range(n):
    c+=(s-l[i])**2
for i in range(n):
    x+=(ss-l[i])**2
print(min(c,x))

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

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

l=a[0]
r=a[-1]

ans=float('inf')
for pin in range(l,r+1):
  cnt=0
  for i in range(n):
    cnt+=(a[i]-pin)**2
    
  ans=min(ans,cnt)
  
print(ans)

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

Solution (Python):





import sys,os
from io import BytesIO, IOBase
import collections,itertools,bisect,heapq,math,string
from decimal import *


BUFSIZE = 8192

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

def main():
    n=int(input())
    a=list(map(int,input().split()))
    mn,mx=min(a),max(a)
    ans=10**9
    for i in range(mn,mx+1):
        temp=0
        for j in a:
            temp+=(j-i)*(j-i)
        ans=min(ans,temp)
    print (ans)
if __name__ == "__main__":
    main()

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

Solution (Python):

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

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

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

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

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

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

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


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)

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

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

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

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

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

N=int(N)

list1=list(map(int,a))





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

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

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

Solution (Python):

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)

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

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

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

m = sum(a)//n
ans = float("inf")
for i in range(m, m+2):
  t = 0
  for j in a:
    t += abs(j-i)**2
  ans = min(t,ans)
print(ans)

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

Solution (Python):
from sys  import stdin,stdout

import bisect

import math

def st():
    return list(stdin.readline().strip())

def inp():
    return int(stdin.readline())

def li():
    return list(map(int,stdin.readline().split()))

def mp():
    return map(int,stdin.readline().split())

def pr(n):
    stdout.write(str(n)+"\n")

def soe(limit):
    l=[1]*(limit+1)
    prime=[]
    for i in range(2,limit+1):
        if l[i]:
            for j in range(i*i,limit+1,i):
                l[j]=0

    for i in range(2,limit+1):
        if l[i]:
            prime.append(i)
    return prime

def segsoe(low,high):
    limit=int(high**0.5)+1
    prime=soe(limit)
    n=high-low+1
    l=[0]*(n+1)
    for i in range(len(prime)):
        lowlimit=(low//prime[i])*prime[i]
        if lowlimit<low:
            lowlimit+=prime[i]
        if lowlimit==prime[i]:
            lowlimit+=prime[i]
        for j in range(lowlimit,high+1,prime[i]):
            l[j-low]=1
    for i in range(low,high+1):
        if not l[i-low]:
            if i!=1:
                print(i)
                
def gcd(a,b):
    while b:
        a=a%b
        b,a=a,b
    return a

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

def check(n,l):
    s=0
    for i in range(len(l)):
        s+=(l[i]-n)*(l[i]-n)
    return s

def solve():
    n=inp()
    x=[]
    l=li()
    for i in range(min(l),max(l)+1):
        x.append(check(i,l))
    pr(min(x))
        

for _ in range(1):
    solve()

Solution (Python):
import math
import collections
import fractions
import itertools
import functools
import operator

def solve():
    n = int(input())
    a = list(map(int, input().split()))
    cost = []
    for i in range(-100, 101):
        ramen = 0
        for j in range(n):
            ramen += abs(a[j]-i)**2
        cost.append(ramen)
    print(min(cost))
    return 0

if __name__ == "__main__":
    solve()

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

print(cost)