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 = float("inf")

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

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

x = round(sum(A) / n)
ans = 0
for i in range(n):
    ans += (A[i] - x) ** 2
    
print(ans)

Solution (Python):
from math import ceil
from math import floor
n = int(input())
nums = list(map(int, input().split()))
ave = sum(nums)/n
if ave%1 > 0.5:
    ave = ceil(ave)
elif ave%1 <= 0.5:
    ave = floor(ave)
ans = 0
for i in range(n):
    ans += (nums[i]-ave)**2
print(ans)

Solution (Python):
n=int(input())
a=sorted([int(i) for i in input().split()])
ans=1000000000
for i in range(-100,101):
    cost=0
    for j in range(n):
        cost+=(a[j]-i)**2
    if cost<ans:
        ans=cost
print(ans)

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

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

min_sum = 1000000000

for i in range(min(a_input), max(a_input)+1):
    tmp_sum=0
    for j in range(n):
        tmp_sum += (a_input[j]-i)**2
    
    min_sum = min(min_sum, tmp_sum)

print(min_sum)

Solution (Python):
import numpy as np

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


def calc_total_cost(t,list):
    total_cost=0
    for i in range(len(list)):
        total_cost += (t-list[i])**2
    return total_cost


t = round(np.mean(a))



c1 = calc_total_cost(t,a)
c2 = calc_total_cost(t+1,a)
print(int(min(c1,c2)))

Solution (Python):
import numpy as np

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


def calc_total_cost(t,list):
    total_cost=0
    for i in range(len(list)):
        total_cost += (t-list[i])**2
    return total_cost


t = round(np.mean(a))



c1 = calc_total_cost(t,a)
c2 = calc_total_cost(t+1,a)
print(int(min(c1,c2)))

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
b1=[]
for y in range(-100,101):
    c=0
    for x in a:
        c+=abs(x-y)**2
    b1.append(c)
b2=sorted(b1)
print(b2[0])

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

res = 10 ** 9
for i in range(-100, 101):
    tmp = 0
    for j in range(N):
        tmp += (L[j] - i) ** 2
        
    res = min(res, tmp)
    
print(res)

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

ans = 10**9+7
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())
A=list(map(int,input().split()))
ans=10**9
for i in range(-100,101):
  ans=min(sum(list(map(lambda x:(x-i)**2,A))),ans)
print(ans)

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

Solution (Python):
"""
与えられた数字で要件を満たす値を探す。
条件として、－100～100の範囲で合計が最小になる値を探す。脳筋検索法でいく( ´∀｀ )

"""


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

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

    total_comp.append(total_prc)
print('{}'.format(min(total_comp)))

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



def solve():
    min_cost = float("inf")
    for x in range(min(NS), max(NS) + 1):
        cost = 0
        for n in NS:
            cost += abs(n - x) ** 2
        if cost < min_cost:
            min_cost = cost
    return min_cost

print(solve())

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

cost_min = 10**9
for target in range(-100, 101):
    cost = 0
    for a in A:
        cost += (a - target)**2
    cost_min = min(cost_min, cost)

print(cost_min)

Solution (Python):
N = int(input())
L = list(map(int,input().split()))
L.sort()
M = 0
ans = float('inf')
for i in range(L[0],L[N-1]+1):
  for j in range(N):
    M += (L[j]-i)**2
  if M <= ans:
    ans = M
    M = 0
  else:
    M = 0
print(ans)

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

results = []
for i in range(0, 201):
  result = sum([(a-(i-100))**2 for a in a_list])
  results.append(result)
  
print(min(results))

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

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

import math
from statistics import mean, median
mean_ai = mean(ai_list)
median_ai = median(ai_list)
mean_lo = math.floor(mean_ai)
mean_high = math.ceil(mean_ai)

ans_list = []
cost1 = 0
cost2 = 0
cost3 = 0
for n in ai_list:
  cost1 += (n - mean_lo)**2
  cost2 += (n - mean_high)**2
  cost3 += (n - median_ai)**2
ans_list.append(cost1)
ans_list.append(cost2)
ans_list.append(cost3)
print(min(ans_list))

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

Solution (Python):

from collections import Counter
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


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

C = Counter(A)

C_freq = C.most_common()
ma = C_freq[-1][0]
mi = C_freq[0][0]


cost = float('inf')


for j in range(-100, 100+1):
    tmp = 0
    for i, v in C.items():
        tmp += v*(j-i)**2
    cost = min(cost, tmp)
print(cost)

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

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

m2 = int(sum(A) / N) + 1

print(min(sum([(a - m1) ** 2 for a in A]), sum([(a - m2) ** 2 for a in A])))

Solution (Python):
N=int(input())
*a,=map(int,input().split())
print(min([sum(map(lambda x:(x-n)**2,a)),n] for n in range(-100,101))[0])

Solution (Python):
n = int(input())
a = list(int(i) for i in input().split())
def costcalc(x):
  a2 = 0
  for i in range(n):
    a2 += (a[i]-x)**2
  return a2

cost = 10**15
for i in range(201):
  check = i-100
  cost = min(cost,costcalc(check))
  
print(cost)

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

ans=10**10
for x in range(min(a),max(a)+1):
    crnt=0
    for aa in a:
        crnt+=(aa-x)**2
    if crnt<ans:
        ans=crnt

print(ans)

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

mean = sum(a) // len(a)
print(min(sum([pow(x-mean, 2) for x in a]), sum([pow(x-mean-1, 2) for x in a])))

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

Solution (Python):
am = int(input())
n_l = list(map(int, input().split(' ')))

m = round(sum(n_l)/len(n_l))

S = 0
for a in range(am):
    S += (n_l[a] - m)**2

print(int(S))

Solution (Python):
import statistics

am = int(input())
n_l = list(map(int, input().split(' ')))

m = round(statistics.mean(n_l))

S = 0
for a in range(am):
    S += (n_l[a] - m)**2

print(int(S))

Solution (Python):
from math import ceil, floor
from statistics import mean

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

upper = ceil(mean(A))
lower = floor(mean(A))
if abs(upper - mean(A)) > abs(lower - mean(A)) :
    indicator = lower
else :
    indicator = upper

ans = 0
for i in A :
    ans += (i - indicator)**2
print(ans)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
ans = float("inf")
for b in range(-100,101):
    cost = 0
    for a in A:
        cost += pow(a-b,2)
    if cost < ans:
        ans = cost
print(ans)

Solution (Python):
N=int(input())
st=input().split()
a=[]
for i in range(N):
    a.append(int(st[i]))
dig=[i for i in range(-100,101)]
cost = []
for i in range(len(dig)):
    x=[]
    for j in range(len(dig)):
        x.append((dig[i]-dig[j])**2)
    cost.append(x)
result=0
for d in range(len(dig)):
    now=0
    for i in range(N):
        now+=cost[d][a[i]+100]
    if d==0:result=now
    elif now<result:result=now
print(result)

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


if sum(a) % n == 0:
    c = sum(a)//n
else:
    if sum(a)/n < sum(a)//n + 0.5:
        c = sum(a)//n
    else:
        c = sum(a)//n + 1

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

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

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

mean = round(sum(A)/N)

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

Solution (Python):
N = int(input())
arr = sorted([int(i) for i in input().split()])
def calc(x,arr):
    cnt = 0
    for i in arr:
        cnt += (i-x)**2
    return cnt
numlist = [-100 + i for i in range(201)]
mincost = calc(100,arr)
numcost = 100
for i in numlist:
    tmp = calc(i,arr)
    if mincost > tmp:
        mincost = tmp
        numcost = i
print(mincost)

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

Solution (Python):
n = int(input())
a = input().split()
a = [int(m) for m in a]
ans = float("INF")

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

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

Ans = 10**10
for i in range(-100,101):
    ans = 0
    for j in range(N):
        ans += (A[j]-i)**2
    Ans = min(Ans,ans)

print(Ans)

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

p = (sum(a) / n * 2 + 1) // 2
print(int(sum(map(lambda x: (x - p) ** 2, a))))

Solution (Python):
import math
N = int(input())
a = list(map(int,input().split()))
x = sum(a)/N
if math.ceil(x)-x < x-math.floor(x):
    x = math.ceil(x)
else:
    x = math.floor(x)
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()))
ans = 100000000000

def sumpow(a, x):
    res = 0
    for i in range(len(a)):
        res += (x - a[i]) ** 2
    return res


for x in range(-105, 105):
    ans = min(ans, sumpow(a, x))
print(ans)

Solution (Python):



def main():
    n = int(input())
    a = list(map(int, input().split()))
    cost = float('inf')

    for i in range(-100, 100 + 1):
        total = 0

        for ai in a:
            total += (ai - i) ** 2

        cost = min(cost, total)

    print(cost)


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
ans = float("inf")
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 cost(a, b):
    c = 0
    for ai in a:
        c += (ai-b)**2
    return c

def findcmin(a):
    cmin = cost(a, max(a))
    for b in range(min(a), max(a)):
        if cost(a, b) < cmin:
            cmin = cost(a, b)
    return cmin

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

Solution (Python):
def cost(a, b):
    c = 0
    for ai in a:
        c += (ai-b)**2
    return c

def findcmin(a):
    cmin = cost(a, 100)
    for b in range(-100, 100):
        if cost(a, b) < cmin:
            cmin = cost(a, b)
    return cmin

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

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

ans = 1000000000
for i in range(-100, 101):
    cost = 0
    for j in range(n):
        cost += (a[j] - i) * (a[j] - i)
    ans = min(ans, cost)

print(ans)

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

for x in range(A[0],A[n-1]+1):
    a = 0
    for item in A:
        a += (x-item)**2
    ans = min(a,ans)

print(ans)

Solution (Python):
N = int(input())
temp = input().rstrip().split(" ")
list1 = []
for i in range(N):
    list1.append(int(temp[i]))
list1.sort()
avg = round(sum(list1) / len(list1))
ans = 0
for i in range(N):
    ans += (list1[i] - avg)**2
print(ans)

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

Solution (Python):
num = int(input())
A = list(map(int, input().split()))
cost = 100 ** 100
tmp = 0
for x1 in range(-100, 101):
    for x2 in range(num):
        tmp += (A[x2] - x1) ** 2
    cost = min(tmp, cost)
    tmp = 0
print(cost)

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

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

if(A.count(A[0]) == N):
  print(0)
  exit()
  
num = []
x = 0
for i in range(min(A),max(A)):
  for a in A:
    x += (a - i)**2
  num.append(x)
  x = 0
print(min(num))

Solution (Python):
import sys

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

ans = sys.maxsize

for k in range(min(a), max(a) + 1):
    cnt = 0
    for ai in a:
        cnt += (ai - k)**2
    ans = min(ans, cnt)

print(ans)

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

Solution (Python):
def main():
    _ = input()
    aa = sorted(map(int, input().split()))

    cost_min = 1 << 30
    for x in range(aa[0], aa[-1] + 1):
        cost_min = min(cost_min, sum((x - a) ** 2 for a in aa))

    print(cost_min)


if __name__ == '__main__':
    main()

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

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

min_num = 100**2*N
for i in range(-100, 101):
	min_num = min(min_num, sum((a-i)**2 for a in A))
print(min_num)

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

min_list = []

for i in range(-100, 101):
    r = 0
    for a in a_list:
        num = abs(i-a)**2
        r += num
    min_list.append(r)
print(min(min_list))

Solution (Python):
def c_be_together(N, A):
    a_sum = sum(A)
    option = (a_sum // N, (a_sum + N - 1) // N)  
    ans = min(sum([(a - opt)**2 for a in A]) for opt in option)  
    return ans

N = int(input())
A = [int(i) for i in input().split()]
print(c_be_together(N, A))

Solution (Python):
def c_be_together(N, A):
    a_sum = sum(A)
    option = (a_sum // N, (a_sum + N - 1) // N)
    ans = float('inf')
    for opt in option:
        ans = min(ans, sum([(a - opt)**2 for a in A]))
    return ans

N = int(input())
A = [int(i) for i in input().split()]
print(c_be_together(N, A))

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

ANS=10**12

for c in range(-100,101):
    COST=sum([(A[i]-c)**2 for i in range(N)])
    if COST<ANS:
        ANS=COST
print(ANS)

Solution (Python):
n=int(input())
a=[int(i) for i in input().split()]
av=sum(a)//n
ans=40000000
for i in range(av-2,av+2):
    chk=0
    for j in a:
        chk+=(i-j)**2
    ans=min(ans,chk)
print(ans)

Solution (Python):

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

ans1 = sum(A) // n + (sum(A) % n != 0)
ans2 = ans1 - 1

cost1 = 0
cost2 = 0

for i in range(n):
    cost1 += (A[i] - ans1) ** 2
    cost2 += (A[i] - ans2) ** 2

print(min(cost1, cost2))

Solution (Python):
N = int(input())
a = input().split(" ")

sum_a = []
for j in range(-100, 101):
    result = 0
    for i in a:
        result += (int(i) - j) ** 2
    sum_a.append(result)

print(min(sum_a))

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

minCost = 10000*1000
for i in range(101):
    cost = 0
    for j in range(len(a)):
        cost += (i - a[j])**2
    if cost < minCost:
        minCost = cost
    cost = 0
    for j in range(len(a)):
        cost += (i + a[j]) ** 2
    if cost < minCost:
        minCost = cost
print(minCost)

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]
che1 = sum(A)//N
che2 = sum(A)//N + 1
ans1 = 0
ans2 = 0

for i in A:
    ans1 += (i-che1)**2
    ans2 += (i-che2)**2
    
print(min(ans1, ans2))

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

min_cost = 40000000
target = 0
for i in range(-100, 101):
    sum_cost = 0
    for a in A:
        num = a-i
        sum_cost += num*num
    if sum_cost < min_cost:
        target = i
        min_cost = sum_cost
print(min_cost)

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

for i in range(-100, 101):
  temp = 0
  for num in nums:
    temp += (num - i)**2
  ans = min(ans, temp)

print(ans)

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

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

print(cost)

Solution (Python):
N = int(input())
A = [int(x) for x in input().split()]
cost = N * 200 ** 2
for i in range(-100, 101):
  cost = min(sum([(i - a) ** 2 for a in A]), cost)
print(cost)

Solution (Python):
import numpy as np
N = int(input())
a_list = list(map(int, input().split()))
ave1 = int(np.average(np.array(a_list)))
ave2 = ave1 + 1
ans1 = sum([ (x-ave1)**2 for x in a_list ])
ans2 = sum([ (x-ave2)**2 for x in a_list ])
if ans1 < ans2:
    ans = ans1
else:
    ans = ans2
print(ans)

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

ans = (200**2)*100
for y in range(-100, 100+1): 
    cost = 0
    for x in a:
        cost += (y-x)**2
    ans = min(ans, cost)
print(ans)

Solution (Python):
import sys
import math
import collections
import itertools
import array
import inspect


sys.setrecursionlimit(1000000)



def chkprint(*args):
    names = {
        id(v): k
        for k, v in inspect.currentframe().f_back.f_locals.items()
    }
    print(', '.join(
        names.get(id(arg), '???') + ' = ' + repr(arg) for arg in args))



def to_bin(x):
    return bin(x)[2:]


def li_input():
    return [int(_) for _ in input().split()]


def gcd(n, m):
    if n % m == 0:
        return m
    else:
        return gcd(m, n % m)


def gcd_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = gcd(v, L[i])

    return v


def lcm(n, m):
    return (n * m) // gcd(n, m)


def lcm_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = lcm(v, L[i])

    return v



def wfs_d(D, N, K):
    """
    D: 隣接行列(距離付き)
    N: ノード数
    K: 始点ノード
    """

    dfk = [-1] * (N + 1)
    dfk[K] = 0

    cps = [(K, 0)]
    r = [False] * (N + 1)
    r[K] = True
    while len(cps) != 0:
        n_cps = []
        for cp, cd in cps:
            for i, dfcp in enumerate(D[cp]):
                if dfcp != -1 and not r[i]:
                    dfk[i] = cd + dfcp
                    n_cps.append((i, cd + dfcp))
                    r[i] = True

        cps = n_cps[:]

    return dfk



def dfs_d(v, pre, dist):
    """
    v:  現在のノード
    pre: １つ前のノード
    dist: 現在の距離

    以下は別途用意する
    D: 隣接リスト(行列ではない)
    D_dfs_d: dfs_d関数で用いる，始点ノードから見た距離リスト
    """

    global D
    global D_dfs_d

    D_dfs_d[v] = dist

    for next_v, d in D[v]:
        if next_v != pre:
            dfs_d(next_v, v, dist + d)

    return


def sigma(N):
    ans = 0
    for i in range(1, N + 1):
        ans += i
    return ans


def comb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2, r + 1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p - 1, r, p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def bisearch(L, target):
    low = 0
    high = len(L) - 1
    
    while low <= high:
        mid = (low + high) // 2
        guess = L[mid]
        if guess == target:
            return True
        elif guess < target:
            low = mid + 1
        elif guess > target:
            high = mid - 1
    if guess != target:
        return False



dp = None


def main():
    N = int(input())
    A = li_input()

    M = round(sum(A) / len(A))

    ans = 0

    for a in A:
        ans += (a - M) ** 2

    print(ans)


main()

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

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

print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
a.sort()
minc = 100000000
for i in range(a[0], a[-1] + 1):
    count = 0
    for j in a:
        count += (i - j)** 2
    minc = min(minc, count)

print(minc)

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

Solution (Python):

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

sub = []

heikin = [sum(an)//len(an), sum(an)//len(an)+1]

for hei in heikin:
    s_sub = 0
    for a in an:
        s_sub += (hei - a)**2
    sub.append(s_sub)

print(min(sub))

Solution (Python):
n = int(input())
A = [int(_) for _ in input().split()]
m = round(sum(A)/n)
ans = 0
for a in A:
    if a == m: continue
    ans += (a - m) ** 2
print(ans)

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

ans = 1e9
for y in range(-100, 101):
    cost = 0
    for x in a:
        cost += (y - x) ** 2
    ans = min(ans, cost)
    
print(ans)

Solution (Python):


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

c1 = 0
c2 = 0
t1 = int(math.ceil(sum(a)/n))
t2 = int(sum(a)//n)
for i in range(0,n):
  if a[i]!=t1:
    c1=c1+(t1-a[i])*(t1-a[i])
  if a[i]!=t2:
    c2=c2+(t2-a[i])*(t2-a[i])

print(min(c1,c2))

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
dif = max(a) - min(a)
ans = -1
for i in range(dif+1):
    cost = 0
    target = min(a) + i
    for j in a:
        cost += (j - target) **2
    if ans == -1 or ans > cost:
        ans = cost
print(ans)

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

for i in range(max(a)-min(a)+1):
    mini.append(int(0))
    for j in range(n):
        mini[i]=mini[i]+(i+min(a)-a[j])**2
        if min(mini)<mini[i]:
            break

print(min(mini))

Solution (Python):


import math

N = int(input())
a = list(map(int, input().split()))
n_sum = []
num_sum = []

for n in range(-100, 101):
    num_sum.append(sum(list(map(lambda x: (x - n)**2, a))))

print(min(num_sum))

Solution (Python):
a=int(input())
b=input().split(" ")
i1=0
i2=0
i3=0
for i in b:
    i1+=int(i)
i2=round(i1/a)
for i in b:
    i3+=(int(i)-i2)**2
print(i3)

Solution (Python):

""" いっしょ / Be Together """

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


test = False

def calc_cost(l):
    """ コストを全探索してリストで返す関数 """
    cost = []
    max_l = max(l)
    min_l = min(l)

    for i in range(min_l, max_l + 1):
        temp_cost = 0
        for x in l:
            temp_cost += (x-i)**2
        if test:
            print('i =', i, end='\t')
            print('temp_cost =', temp_cost)
        cost.append(temp_cost)

    if test:
        print('cost         =', cost)
        print('sorted(cost) =', sorted(cost))
    return cost


def main():
    """ 最小コストを出力する関数 """
    answer = min(calc_cost(a))
    print(answer)


if __name__ == '__main__':
    main()

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

    m = min(A)
    M = max(A)

    Result = 0
    for a in A:
        Result += (a-m) * (a-m)

    for i in range(m+1, M+1):
        Result_tmp = 0
        for a in A:
            Result_tmp += (a - i) * (a - i)
        Result = min(Result_tmp, Result)
    print(Result)


if __name__ == "__main__":
    main()

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

    m = min(A)
    M = max(A)

    Result = 100000000

    for i in range(m, M+1):
        Result_tmp = 0
        for a in A:
            Result_tmp += (a - i) * (a - i)
        Result = min(Result_tmp, Result)
    print(Result)


if __name__ == "__main__":
    main()

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

    m = min(A)
    M = max(A)

    Result = 1 << 30

    for i in range(m, M+1):
        Result_tmp = 0
        for a in A:
            Result_tmp += (a - i) * (a - i)
        Result = min(Result_tmp, Result)
    print(Result)


if __name__ == "__main__":
    main()

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

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

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

cnt1=0
for i in range(N):
    cnt1+=(A[i]-Ave)**2
    
cnt2=0
for j in range(N):
    cnt2+=(A[j]-(Ave+1))**2
print(min(cnt1,cnt2))

Solution (Python):
x = int(input())
ar = list(map(int,input().split(" ")))
count = 0
for a in ar:
    count += a
c = round(count / x)
cost = 0
for a in ar:
    cost += (a - c) ** 2
print(cost)

Solution (Python):
N = int(input())
a = sorted(map(int, input().split()))
cost = 10**9

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

Solution (Python):

from sys import stdin

def shisha(f):
    if f - int(f) >= 0.5:
        return int(f) + 1
    else:
        return int(f)

N = int(stdin.readline().rstrip())
a = [int(x) for x in stdin.readline().rstrip().split()]
ans = 0

near = shisha(sum(a)/N)

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

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

Solution (Python):
N = int(input())
A = [int(x) for x in input().split()]
A_min = min(A)
A_max = max(A)
ans = N * (A_max - A_min) ** 2
for p in range(A_min, A_max + 1):
    cost = 0
    for a in A:
        cost += (a - p) ** 2
    if cost < ans:
        ans = cost
print(ans)

Solution (Python):
N=int(input())
a=list(map(int, input().split()))
sumlist=[]
for i in range(min(a),max(a)+1):
    sum=0
    for j in range(N):
        sum+=(a[j]-i)*(a[j]-i)
    sumlist.append((sum,i))
print(sorted(sumlist, key=lambda x: x[0])[0][0])

Solution (Python):
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


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

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


if __name__ == '__main__':
    main()

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

Solution (Python):
n = int(input())
array = [int(x) for x in input().split()]

tmp = round(sum(array)/n)

print(sum([(x-tmp)**2 for x in array]))

Solution (Python):
round = lambda x: int((x*2+1)//2)

n = int(input())
A = list(map(int, input().split()))
target = round(sum(A) / n)
print(sum([(a-target)**2 for a in A]))

Solution (Python):

iN = int(input())
aA = [int(x) for x in input().split()]


iMM = sum(aA) // len(aA)








iLM = iMM
iUM = iMM + 1

aR = []
for iM in range(iLM,iUM+1):
    aR.append(0)
    for iA in aA:
        aR[-1] += (iA - iM) ** 2
print(min(aR))

Solution (Python):
iN = int(input())
aA = [int(x) for x in input().split()]

i2M = (min(aA) + max(aA)) // 2
iMM = sum(aA) // len(aA)

if i2M < iMM :
    iLM = i2M
    iUM = iMM + 1
else:
    iLM = iMM
    iUM = i2M + 1

aR = []
for iM in range(iLM,iUM+1):
    aR.append(0)
    for iA in aA:
        aR[-1] += (iA - iM) ** 2
print(min(aR))

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

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

a.sort()



l = len(a)
s = sum(a)



target = 0
ans = 0


 


 



target = round(s / n)
    
    
for i in range(l):
    ans = ans + (target - a[i]) * (target - a[i])

print(ans)

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

m = .0
m = sum(a)/n

m1 = []
m1 = round(m)
s = 0
for i in range(n):
    s += (a[i]-m1)**2

print(s)

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

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

avg = sum(A) / N

ans1 = 0
ans2 = 0
x1 = floor(avg)
x2 = ceil(avg)

for a in A:
  ans1 += (a - x1) ** 2
  ans2 += (a - x2) ** 2
  
print(min(ans1, ans2))

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

from statistics import mean
m = round(mean(a))
cost = 0
for i in a:
    cost += (i-m)**2
print(cost)

Solution (Python):
import math
cou = 0
co = 0
n = int(input())
lis = list(map(int,input().split()))
ave1 = math.ceil(sum(lis)/n)
ave2 = math.floor(sum(lis)/n)
for i in range(n):
  cou += (lis[i]-ave1) ** 2
  co += (lis[i]-ave2) ** 2
print(min(cou,co))

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

Solution (Python):



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

x = round(sum(a) / n, 0)
d = 0
for ai in a:
    d += (x - ai) ** 2

print('{:.0f}'.format(d))