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

total_costs = {}
if( len(set(A)) == 1 ):
    print(0)
else:
    for i in range(-100, 101):
        cost = 0
        for a in A:
            cost += (a - i)*(a - i)
        total_costs[i] = cost
    print(min(total_costs.values()))

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

total_costs = {}
if( len(set(A)) == 1 ):
    print(0)
else:
    for i in range(-100, 101):
        cost = 0
        for a in A:
            cost += (a - i)**2
        total_costs[i] = cost
    print(min(total_costs.values()))

Solution (Python):
INF = float("inf")
nl = lambda: list(map(int, input().split()))
sl = lambda: input().split()
n = lambda: int(input())
s = lambda: input()

N = n()
A = nl()

res = INF
for t in range(-100, 101):
    cost = 0
    for i in range(N):
        cost += (A[i] - t) * (A[i] - t)
    if cost < res:
        res = cost

print(res)

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

center=round(sum(a_list)/n)
ans=0
for a in a_list:
    ans+=(a-center)**2
print(ans)

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

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

print(ans)

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

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

d = defaultdict(int)

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

ans = []

for i in range(-100,101):
    cnt = 0
    for j in d.keys():
        cnt += d[j]*(i-j)**2
    ans.append(cnt)

print(min(ans))

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

s = 0
s_pre = 10000000000

for j in range(-100, 101):
    for i in range(n):
        s += pow((a[i] - j),2)
    if s_pre > s:
        s_pre = s
    s = 0
print(s_pre)

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

ans = 10000 ** 100
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()))

mn = 10**9
for j in range(min(A), max(A)+1):
  cost = 0
  for i in range(N):
    cost += (A[i]-j)**2
  mn = min(cost, mn)
print(mn)

Solution (Python):

import sys


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

ans = 10 ** 9 + 7
for i in range(min(a), max(a) + 1):
    ans_tmp = 0
    for j, aj in enumerate(a):
        ans_tmp += (i - a[j]) ** 2
    if ans > ans_tmp: ans = ans_tmp

print(ans)

Solution (Python):
import sys
from collections import Counter, deque, defaultdict
from itertools import accumulate, permutations, combinations, takewhile, compress, cycle
from functools import reduce
from math import ceil, floor, log10, log2, factorial
from pprint import pprint

sys.setrecursionlimit(1000000)






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

ans = float('inf')
for y in range(-100, 101):
    ans = min(ans, sum([(x - y) ** 2 for x in A]))

print(ans)

Solution (Python):
n=int(input())
l=[]
p=input().rstrip().split(' ')
for i in range(-100,101):
    s=0;
    for j in range(0,len(p)):
        t=(int(p[j])-i)**2;
        s=s+t;
    l.append(s)
print(min(l))

Solution (Python):

import copy
import random
import bisect 
import fractions 
import math
import sys

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

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
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())


N = int(input())
a = LI()

if len(set(a)) == 1:
    print(0)
    exit()
ans = mod
for i in range(min(a),max(a)):
    tmp = 0
    for j in range(len(a)):
        tmp += (a[j]-i)**2
    ans = min(ans,tmp)
print(ans)

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

mean = int(round(sum(a) / len(a)))

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

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


def get_cost(now, target):
    return (now - target) * (now - target)


ans = 10000000
for i in range(min(a), max(a) + 1):
    cost_sum = 0
    for j in a:
        cost_sum += get_cost(j, i)
    if cost_sum < ans:
        ans = cost_sum
print(ans)

Solution (Python):
N = int(input())
a = list(map(int,input().split()))
av = sum(a)//N
ans1 = []
ans2 = []
for i in range(N):
    ans1.append((a[i]-av)**2)
    ans2.append((a[i]-av-1)**2)

print(min(sum(ans1),sum(ans2)))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
ans = float("inf")
m = min(a)
M = max(a)
for i in range(m,M+1):
  s = 0
  for j in a:
    s += (j-i)**2
  ans = min(ans, s)
print(ans)

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

answer = 10**10
for i in range(min(a), max(a)+1):
  cost = 0
  for j in a:
    cost += (i-j)**2
  answer = min(answer, cost)

print(answer)

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
s=sum(l)
ans=10**7
for i in range(min(l),max(l)+1):
	ans=min(ans, sum([(x-i)**2 for x in l]))
print(ans)

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

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

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

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

solver = []
for M in range(-100,100+1):
  cost = 0
  for i in range(N):
    cost += (A[i] - M) ** 2
  solver.append(cost)
  
print(min(solver))

Solution (Python):
from math import ceil
n = int(input())
num = list(map(int, input().split()))

sol = 10 ** 15
for i in range(-100, 101):
	tmp = 0
	for j in range(n):
		tmp += (num[j] - i) ** 2  
	sol = min(sol, tmp)
print(int(sol))

Solution (Python):
import sys

N = int(sys.stdin.readline())
a = list(map(int, sys.stdin.readline().split()))


min_val = -100
for i in range(-100, 101):
    sum_calc = 0
    for j in range(N):
        sum_calc += (a[j] - i)*(a[j] - i)
    if i == -100:
        min_val = i
        min_sum_calc = sum_calc
    if min_sum_calc > sum_calc:
        min_val = i
        min_sum_calc = sum_calc

print(min_sum_calc)

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

x_1 = sum(a) // n
x_2 = x_1 + 1

cost_1 = 0
cost_2 = 0

for _a in a:
  cost_1 += (x_1 - _a) ** 2
  cost_2 += (x_2 - _a) ** 2

print(min(cost_1, cost_2))

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
min = 5000000000000000
for i in range(-100, 101):
	sum = 0
	for j in range(0, N):
		sum += (i-A[j])*(i-A[j])
	if sum < min: min = sum
print(min)

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

Solution (Python):
def main():
    n = int(input())
    a = list(map(int,input().split(' ')))
    cost = 100**2*100
    for i in range(-100,101):
        tmp = 0
        for j in a:
            if tmp >= cost:
                break
            tmp += (j-i)**2
        if cost > tmp:
            cost = tmp

    print(cost)

main()

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

_mean1 = sum(a)//n
_mean2 = _mean1+1

_sum1=0
_sum2=0

for i in a:
    _sum1+=(i - _mean1)**2
    _sum2+=(i-_mean2)**2
    
print(min(_sum1, _sum2))

Solution (Python):

n = int(input())
lst = list(map(int, input().rstrip().split()))
ans = float("inf")
MIN, MAX = min(lst), max(lst)
for i in range(MIN, MAX + 1):
    cnt = 0
    for j in lst:
        cnt += (i - j) ** 2
    ans = min(ans, cnt)
print(ans)

Solution (Python):

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


min_a = min(a)
max_a = max(a)

cost = []

i = min_a
while i <= max_a:
    cost.append(sum(map(lambda x:(x-i)**2,a)))
    i+=1
print(min(cost))

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

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

x = round(sum(arr) / n)

print(sum((x - i)**2 for i in arr))

Solution (Python):


n = int(input())
arr = list(map(int, input().split()))
ans = 10e9
for t in range(-100, 101):
    ans_tmp = 0
    for v in arr:
        ans_tmp += abs(t - v)**2
    ans = min(ans, ans_tmp)

print(ans)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
mx, mn = max(a), min(a)
ans = float('inf')
for i in range(mn,mx+1):
    ret = 0
    for j in a:
        ret += (j-i)**2
    ans = min(ans,ret)
print(ans)

Solution (Python):


import math
from decimal import Decimal as dec


N = int(input())
An = list(map(int, input().split()))
x = math.floor(dec(sum(An))/dec(N))

cost = [0,0]
for a in An:
  cost[0] += (x-a)**2
  cost[1] += (x+1-a)**2
print(min(cost))

Solution (Python):
import math
import fractions
import copy

def j(q):
    if q==1: print("YES")
    elif q == 0:print("NO")
    exit(0)
rem = pow(10,9)+7
"""
def ct(x,y):
    if (x>y):print("+")
    elif (x<y): print("-")
    else: print("?")
"""

def ip():
    return int(input())
def printrow(a):
    for i in a:
        print(i)
def combinations(n,r):
    if n<r:return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
def permutations(n,r):
    if n<r:return 0
    return math.factorial(n) // math.factorial(n - r)
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)


n = ip()                                     

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









s = pow(10,15)
for i in range(min(a),max(a)+1):
    c = 0
    for k in range(n):
        c+=pow(a[k]-i,2)
    s= min(c,s)
print(s)

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

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

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  n=I()
  l=LI()

  mn=inf
  for i in range(-100,101):
    c=0
    for x in l:
      c+=pow(x-i,2)
    mn=min(mn,c)

  return mn

print(main())

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

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

med_a = sum(a) / N

candidate = [int(med_a), int(med_a + 0.9)]
cost = [0, 0]

for i in a:
    for j in range(len(candidate)):
        cost[j] += (i - candidate[j]) ** 2

print(min(cost))

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
c=1000000
for i in range(-100,101):
  c1=0
  for j in l:
    c1+=(j-i)**2
  if c>c1:
    c=c1
print(c)

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

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

num_list = [ int(v) for v in input().split() ]
min_num, max_num = min(num_list), max(num_list)
sum_num = sum(num_list)
square_sum_num = sum([ v**2 for v in num_list ])


ans = square_sum_num

for i in range(min_num,max_num+1):
	ans = min(ans, square_sum_num-2*i*sum_num+n*i**2)
print(ans)

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

min_cost = 10 ** 7
for y in range(-100, 101):
    cost = sum([(a[i] - y) ** 2 for i in range(N)])
    if cost < min_cost:
        min_cost = cost
print(min_cost)

Solution (Python):

import sys
input = sys.stdin.readline

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

Solution (Python):
N, *a = map(int, open(0).read().split())
tgt = round(sum(a) / N)
print(sum((v - tgt) ** 2 for v in a))

Solution (Python):
import numpy as np
N = int(input())
A = np.array(input().split(),dtype=np.int64)
target = np.arange(-100,101,1)
answer = (np.add.outer(A,target)**2).sum(axis=0).min()
print(answer)

Solution (Python):
N = int(input())
a = [int(x) for x in input().split()]
amin, amax = min(a), max(a)

temp = [ai - amin for ai in a]
ans = sum([ai**2 for ai in temp])

for A in range(amin,amax+1):
    for i in range(N):  temp[i] -= 1
    nans = sum([ai**2 for ai in temp])
    if ans > nans:  ans = nans

print(ans)

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

Solution (Python):
N = int(input())
a = sorted(list(map(int, input().split())))
cost = []
total = 0
for i in range(a[0], a[-1] + 1):
    for x in a:
        total += (x - i)**2
    cost.append(total)
    total = 0
print(min(cost))

Solution (Python):
n=int(input())
A=tuple(map(int,input().split()))
s=sum(A)
ave=s/n
m=s//n
if m+1-ave<ave-m:
    m+=1
print(sum((a-m)**2 for a in A))

Solution (Python):
a=int(input())
b=list(map(int,input().split()))
cost=9999999
for i in range(min(b),max(b)+1):
    c=0
    for j in b:
        c=c+(i-j)**2
    if c<cost:
        cost=c
print(cost)

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

Solution (Python):
n,a=open(0);*a,=map(int,a.split());print(min(sum((i-j-100)**2for j in a)for i in range(201)))

Solution (Python):
n,a=open(0);*a,=map(int,a.split());print(min(sum((i-j-100)**2for j in a)for i in range(201)))

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

minc = 1e12
for i in range(min(xs), max(xs) + 1):
    c = 0
    for x in xs:
        c += (x - i) ** 2

    minc = min(minc, c)

print(minc)

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

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

N = int(input())
L = [int(v) for v in input().split()]


ans = 10 ** 18
for i in range(-100, 101):
    t = 0
    
    for v in L:
        t += (v - i) ** 2

    ans = min(ans, t)

print(ans)

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

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
ans = (200**2) * 100

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


print(ans)

Solution (Python):
N = int(input())
*A, = map(int,input().split())
c = (400**2)*100
for b in range(-100,101):
    c = min(c,sum((a-b)**2 for a in A))
print(c)

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

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

Solution (Python):

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

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

result = 100**2 * 100

for x in range(-100, 101):
    r = sum((x - a) ** 2 for a in A)
    result = min(result, r)

print(result)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
result = 10 ** 9
for y in range(-100, 101):
    num = sum([(a - y) ** 2 for a in A])
    result = min(num, result)
print(result)

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

mean = sum(A)//N
ans = 0
ans2 = 0
for a in A:
    ans += (mean-a)**2
mean += 1
for a in A:
    ans2 += (mean-a)**2
print(min(ans, ans2))

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

min_a=min(a)
max_a=max(a)
Cost=[]
for i in range(max_a - min_a+1):
    cost=0
    for k in range(N):
        cost+=(a[k] - (min_a + i))**2
    Cost.append(cost)
print(min(Cost))

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

w = round(sum(a) / N)
for i in range(N):
    a[i] = (w - a[i]) ** 2

print(int(sum(a)))

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

	ans = 10**9

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


if __name__ == '__main__':
	main()

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

Solution (Python):
n = int(input())
lists = list(map(int,input().split()))
sums = sum(lists)
num = int(round(sums/n,0))
cost=0
for i in lists:
    cost+=pow(i-num,2)
print(cost)

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

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

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

print(ans)

Solution (Python):

import math

def read_input():
    n = int(input())
    alist = list(map(int, input().split()))
    return n, alist


def calc_se(t, alist):
    return sum([(t - a) ** 2 for a in alist])


def submit():
    n, alist = read_input()
    avg_alist = sum(alist)/n

    cand1 = math.floor(avg_alist)
    cand2 = math.ceil(avg_alist)

    if cand1 == cand2:
        se = calc_se(cand1, alist)
    else:
        se1 = calc_se(cand1, alist)
        se2 = calc_se(cand2, alist)
        se = min(se1, se2)

    print(se)

if __name__ == '__main__':
    submit()

Solution (Python):


def solve(N, A):
    a_max = max(A)
    a_min = min(A)
    res = float("INF")
    for i in range(a_min, a_max+1, 1):
        sum = 0
        for j in range(N):
            sum += (A[j] - i)**2
        res = min(res, sum)
    print(res)
    
if __name__ == '__main__':
    N = int(input())
    A = [int(a) for a in input().split()]
    
    solve(N, A)

Solution (Python):


def solve(N, A):
    a_max = max(A)
    a_min = min(A)
    sum_list = []
    for i in range(a_min, a_max+1, 1):
        sum = 0
        for j in range(N):
            sum += (A[j] - i)**2
        sum_list.append(sum)
    print(min(sum_list))
    
if __name__ == '__main__':
    N = int(input())
    A = [int(a) for a in input().split()]
    
    solve(N, A)

Solution (Python):
import sys
import numpy as np

def main():
    input = sys.stdin.readline
    N = int(input())
    A = np.array(list(map(int, input().split())))
    
    a, b = min(A), max(A)
    ans = float('inf')
    for i in range(a, b+1):
        ans = min(ans, ((A - i)**2).sum())

    return ans


if __name__ == '__main__':
    print(main())

Solution (Python):
n = int(input())
L = [int(_) for _ in input().split()]
L.sort()
total = 100000000000000000
num = 0
if L[0] == L[-1]:
    print(0)
else:
    for l in range(L[0], L[-1]):
        tot = 0
        for j in range(n):
            tot += abs(L[j] - l)**2
        if tot < total:
            num = l
            total = tot
            
    print(total)

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

Solution (Python):
from statistics import mean

N = int(input())
An = list(map(int, input().split()))
x = round(mean(An))

min_sum = 0
for a in An:
    min_sum += (a - x) ** 2
print(min_sum)

Solution (Python):
N = int(input())
As = list(map(int,input().split()))
anss=[]
for i in range(-101,101):
    anss.append(sum([abs(i-a)**2 for a in As]))
print(min(anss))

Solution (Python):
from decimal import Decimal, ROUND_HALF_UP


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


a_ave = sum(lst_a) / N
target_val = int(Decimal(str(a_ave)).quantize(Decimal("0"), rounding=ROUND_HALF_UP))
ans = sum([(a - target_val)**2 for a in lst_a])
print(ans)

Solution (Python):

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


target_values = range(-100, 101)
ans = float("inf")
for target_val in target_values:
    cost = sum([(a - target_val) ** 2 for a in lst_a])
    if cost <= ans:
        ans = cost
print(ans)

Solution (Python):
def my_round(x,d=0):
    p=10**d
    return (x*p*2+1)//2/p


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


a_ave = sum(lst_a) / N
target_val = int(my_round(a_ave))
ans = sum([(a - target_val)**2 for a in lst_a])
print(ans)

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

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

Solution (Python):
def be_together(N: int, A: list) -> int:
    ave = sum(A) // N

    def f(y: int) -> int:
        return sum((a-y)**2 for a in A)

    return min(f(ave), f(ave + 1))


if __name__ == "__main__":
    N = int(input())
    A = [int(s) for s in input().split()]
    ans = be_together(N, A)
    print(ans)

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
min_cost = 10 ** 18
for y in range(-100, 101):
    cost = 0
    for x in a:
        cost += (x - y) ** 2
    min_cost = min(min_cost, cost)
print(min_cost)

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

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

INF = 10**9
min_cost = INF

for target in range(min(A), max(A)+1):
    tmp_cost = 0
    for i in range(N):
        tmp_cost += (target-A[i])**2
    if tmp_cost <= min_cost:
        min_cost = tmp_cost

print(min_cost)

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

Solution (Python):

N=int(input())
a=list(map(int,input().split()))
a.sort()
MINA=a[0]
MAXA=a[-1]
roop=MAXA-MINA
MIN=(a[-1]*N)**2
s=MINA
for i in range(roop+1):
    che=0
    for j in range(N):
        che+=(a[j]-s)**2
    MIN=min(MIN,che)
    s+=1
print(MIN)

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

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

Solution (Python):
n=int(input())
A=list(map(int,input().split()))
A.sort()
ans=10**9

for i in range(A[0],A[n-1]+1):
    res=0
    for j in range(n):
        res+=(i-A[j])**2
    ans=min(ans,res)

print(ans)

Solution (Python):


















import math
import copy
from collections import defaultdict, Counter

from itertools import product

from itertools import permutations

from itertools import combinations
from bisect import bisect_left, bisect_right


def s_inpl(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
INF = float("inf")

N = int(input())
a = inpl()

ans = INF
for i in range(-100, 101):
    tmp = sum([ (abs(ai-i))**2 for ai in a])
    if tmp<ans:
        ans = tmp

print(ans)

Solution (Python):
n = int(input())
a = [ int(i) for i in input().split() ]
b = 1e9
for ans in range(-100,100+1):
    b = min(b, sum([ (i-ans)**2 for i in a ]))
print(b)

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

Solution (Python):
from sys import stdin
input = stdin.readline
N = int(input().rstrip())
a = [int(x) for x in input().rstrip().split()]
min = 1000000
for i in range(-100, 101):
	sum = 0
	tmp = [i] * len(a)
	for i in range(len(a)):
		sum += (tmp[i] - a[i]) ** 2
	if sum < min:
		min = sum

print(min)

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

Solution (Python):
from functools import reduce

def main():
    
    

    
    
    
    
    
    
    
    

    
    
    
    N = int(input().rstrip())
    a_list = input().rstrip().split(' ')
    cost = 0
    min_cost = 201 * 201 * 100
    for i in range(-100,101):
        cost = 0
        for a in a_list:
            a_num = int(a)
            cost = cost + (a_num - i) * (a_num-i)
            
        if(cost < min_cost):
            min_cost = cost
            
    print(min_cost)
    
if __name__ == '__main__':
    main()

Solution (Python):
from functools import reduce

def main():
    
    
    
    
    

    
    
    N = int(input().rstrip())
    a_list = input().rstrip().split(' ')
    cost = 0
    min_cost = 1e10
    for i in range(-100,101):
        cost = 0
        for a in a_list:
            a_num = int(a)
            cost = cost + (a_num - i) * (a_num-i)
            
        if(cost < min_cost):
            min_cost = cost
            
    print(min_cost)
    
if __name__ == '__main__':
    main()

Solution (Python):
import numpy as np
N=int(input())
A=np.array(list(map(int, input().split())))
m=A.mean().round()

print(int(sum((A-m)**2)))

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

mu = round(sum(a)/n)

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

Solution (Python):
def main():
    N = int(input())
    A = list(map(lambda x: int(x)+100, input().split()))
    avg = int(sum(A)/N + 0.5)
    ans = 0
    for i in range(N):
        ans += (A[i] - avg)**2
    print(ans)


if __name__ == "__main__":
    main()

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

num = round(sum(a)/len(a))
cost = 0
for i in a:
    cost += (num - i) ** 2

print(cost)

Solution (Python):
N = int(input())
S = list(map(int, input().split()))
cost_all = 1e10
for s_set in range(min(set(S)), max(set(S)) + 1):
	cost = 0
	for s in S:
		cost += (s_set - s)** 2
		
	cost_all = min(cost, cost_all)
print(cost_all)

Solution (Python):


import numpy as np

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

m = round(a.mean())

print(int(np.sum((a - m)**2)))

Solution (Python):
a,b=int(input()),list(map(int,input().split()))
ans=-1
for i in range(-100,100+1):
  c=0
  for j in range(a):c+=(b[j]-i)**2
  if ans==-1:ans=c
  elif ans>c:ans=c
print(ans)

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

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

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

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


ave = int(sum(X)/n)

sum = 0
sum1 = 0
sum2 = 0
for i in X:
    sum += (ave - i)**2
    sum1 += (ave - 1 - i)**2
    sum2 += (ave + 1 - i)**2
print(min(sum, sum1, sum2))

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

print(int(sum([abs(a[i]-n)**2 for i in range(N)])))

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

Solution (Python):
import numpy as np
M=int(input())
n=list(map(int,input().split()))
x=int(round(np.mean(n)))
ans=0
for i in n:
    ans=ans+(i-x)**2
print(ans)

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

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

print(ans)

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

print(min(ans))

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

Solution (Python):
N = input()
a = map(int, raw_input().split())
ans = 99999999
for t in range(-100,101):
  ans = min(ans, sum((t-x)**2 for x in a))
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):
    t=0
    for a in A:
        t+=(a-i)**2
    ans=min(ans,t)
print(ans)

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

b = sum(a)//n

co = 10**9
su = 0
for k in range(3):
    for i in a:
        su += abs(i-b+(k-1)) ** 2
    co = min(co,su)
    su = 0

print(co)

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

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

result = -1
for t in range(min(li_a), max(li_a) + 1):
    sum = 0
    for a in li_a:
        sum += (a - t)**2
    if result < 0:
        result = sum
    else:
        result = min(result, sum)

print(result)

Solution (Python):
def main():
  N = int(input())
  a_list = list(map(int, input().split()))
  cost = 0
  if not (len(a_list) == N):
    print('ERROR')
  a_ave = sum(a_list)/N 
  a_ave = round(a_ave) 
  for i in range(len(a_list)):
    cost = cost + (a_list[i]-a_ave)**2
  print(cost)
main()

Solution (Python):
n = int(input())
li = list(map(int, input().split()))
s =int(sum(li) / len(li))
p = 0
q = 0

if s < 0:
    a = s - 1
    b = s
else:
    a = s
    b = s + 1

for i in range(len(li)):
    p = p + (li[i] - a) ** 2
    q = q + (li[i] - b) ** 2

print(min(p, q))

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

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

A=0
for i in range(n):
    A+=li[i]
x=A/n
a=math.ceil(x)-x
b=x-math.floor(x)
if a-b>=0:
    N=math.floor(x)
else:
    N=math.ceil(x)
ans=0
for i in range(n):
    ans+=(N-li[i])**2

print(ans)

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
s=2000000000000
for i in range(-100,101):
  su=0
  for j in l:
    su+=(i-j)*(i-j)
  if su<s:
    s=su
print(s)

Solution (Python):
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN

N=int(input())
lst=list(map(int,input().split()))
average=Decimal(str(sum(lst)/len(lst))).quantize(Decimal('0'),rounding=ROUND_HALF_UP)
_lst=[(i-average)**2 for i in lst]
print(sum(_lst))

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