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())
L = list(map(int, input().split()))

r = (200 ** 2)*100

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

Solution (Python):

from collections import defaultdict
import sys
sys.setrecursionlimit(10**8)
input = sys.stdin.readline

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

ans = 10**10 
for i in range(-100, 101):
    ret = 0
    for item in a:
        ret += (item - i) * (item - i)
    ans = min(ans, ret)

print(ans)

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

ans = 100 * 100 * 100

for y in range(min(a), max(a) + 1):
        sum = 0
        for x in a:
            sum = sum + (x - y) * (x - y)
        if sum < ans:
            ans = sum

print(ans)

Solution (Python):
import numpy as np
N = int(input())
alist = np.array(list(map(int, input().split())))
min_cost = 10**9
for num in range(-100, 101, 1):
    sbn_array = np.abs(alist - num)
    cost = np.sum(sbn_array**2)
    min_cost = min(min_cost, cost)
print(min_cost)

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

m = sum(A)/n
if m - int(m) < 0.5:
    m = int(m)
else:
    m = int(m) + 1

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

print(ans)

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 range(n):
        cost += (i-A[j])**2

    ans = min(cost,ans)

print(ans)

Solution (Python):
_ = input()
a = list(map(int,input().split()))
mean = sum(a) / len(a)
mean_ = int(mean + 0.5)
mean = int(mean - 0.5)

coust_ = sum([(mean_ - i)** 2 for i in a])
coust = sum([(mean - i) ** 2 for i in a])

print(coust_ if coust_ < coust else coust)

Solution (Python):
def examC():
    N = I()
    a = LI()
    lower = min(a)
    L = (max(a)-min(a))
    cur = [0]*L
    for i in range(L):
        for j in range(N):
            cur[i] +=(a[j]-(i+lower))**2
    if cur:
        ans = min(cur)
    else:ans = 0
    print(ans)

import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examC()

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

b = sum(a_list) // n
ans1 = sum([(a-b)**2 for a in a_list])

b += 1
ans2 = sum([(a-b)**2 for a in a_list])

print(min(ans1,ans2))

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

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

    print(ans)



if __name__ == "__main__":
    main()

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

    
    avg = sum(A) / N
    avg = round(avg)

    
    
    
    
    
    
    
    
    ans = 0
    for ai in A:
        x = (avg - ai) ** 2
        ans += x

    print(ans)



if __name__ == "__main__":
    main()

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

    avg = sum(A) // N

    ans = 10 ** 10
    
    for i in [avg, avg+1]:
        tmp = 0
        for ai in A:
            x = (i - ai) ** 2
            tmp += x
        ans = min(ans, tmp)

    print(ans)



if __name__ == "__main__":
    main()

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

Solution (Python):

import sys
input = sys.stdin.readline


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

ans = float("inf")
for d in range(-100,101):
    total = 0
    for i in range(N):
        total += (d-A[i])**2
    ans = min(ans,total)

print(ans)

Solution (Python):

import sys


def solve(N: int, a: "List[int]"):
    c = round(sum(a)/N)
    ans = 0
    for i in range(N):
        ans += (c-a[i])**2

    print(ans)
    return



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

if __name__ == '__main__':
    main()

Solution (Python):
import numpy as np

N = int(input())
a = list(map(int, input().split()))
ave = int('{0:.0f}'.format(np.mean(a)))

ans = 0

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

Solution (Python):
from statistics import mean
N = int(input())
a = list(map(int, input().split()))
m = round(mean(a),0)
ans = 0

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

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

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

costSum = [0] * (a_max - a_min + 1)
k = 0

for i in range(a_min, a_max + 1):
    for j in range(len(a)):
        costSum[k] += (i - a[j]) ** 2

    k += 1

print(min(costSum))

Solution (Python):
def main():
    N, *an = map(int, open(0).read().split())
    print(sum((ai - round(sum(an) / N)) ** 2 for ai in an))
    return


main()

Solution (Python):
def main():
    N, *an = map(int, open(0).read().split())
    print(min(sum((ai - (sum(an) // N)) ** 2 for ai in an),
              sum((ai + (-sum(an) // N)) ** 2 for ai in an)))
    return


main()

Solution (Python):
INF = int(1e9 + 7)


def main():
    N, *an = map(int, open(0).read().split())
    ans = INF
    for i in range(min(an), max(an) + 1):
        tmp = 0
        for ai in an:
            tmp += (ai - i) ** 2
        ans = min(ans, tmp)
    print(ans)
    return


main()

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
ans = []
for i in range(201):
    x = i-100
    b = 0
    for i in range(n):
        b += abs(x-a[i])**2
    ans.append(b)
ans.sort()
print(ans[0])

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

ave=round(sum(a)/len(a))

cost=0
for i in a:
    cost+=(i-ave)**2
print(cost)

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


def mean(numbers):
    return sum(a) / N


target = round(mean(a))
cost = 0
for num in a:
    cost += (num - target)**2

print(cost)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
x = sum(A)//N
y = x+1
tot1 = 0
tot2 = 0
for i in range(N):
    tot1 += (x-A[i])**2
    tot2 += (y-A[i])**2
print(min(tot1,tot2))

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

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

Solution (Python):
import math
N = int(input())
a = list(map(int,input().split()))
s = 0
t = 0
for i in range(N):
    s += a[i]
    t += (a[i])**2
a = math.floor(s/N)
if (s/N) - a > 0.5:
    a += 1
ans = N*a**2 - 2*s*a + t
print(ans)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
ans=10**12
C=0

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

print(ans)

Solution (Python):

import sys
import numpy as np

def solve(N: int, a: "List[int]"):
    a_num = np.array(a)
    min_value = np.min(a_num)
    max_value = np.max(a_num)

    answer = 10**7
    for i in range(min_value,max_value+1):
        goal = np.full(N,i)
        dif = a_num-goal
        nijo = dif**2
        answer = min(answer,np.sum(nijo))
    print(int(answer))
    return


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

if __name__ == '__main__':
    main()

Solution (Python):

dv = int(input())
l_si = input().split(" ")
l_si_i = [int(s) for s in l_si]

sm = sum(l_si_i)
av = round(sm /dv)
ret = 0

for it in l_si_i:
  ret = ret + (it -av) **2


print(ret)

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

for i in a:
  sum+=i
ave=sum//N

var1,var2=0,0
for i in a:
  var1+=(i-ave)**2
  var2+=(i-ave-1)**2
print(min(var1,var2))

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

minCost = None
cost = 0

for i in range(-100, 101):
    for a in A:
        cost += (a - i) ** 2
    if minCost is None:
        minCost = cost
    else:
        minCost = min(minCost, cost)
    cost = 0

print(minCost)

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

Solution (Python):
def custom_round(number, ndigits=0):
    if type(number) == int:
        return number
    d_point = len(str(number).split('.')[1])
    if ndigits >= d_point:
        return number
    c = (10 ** d_point) * 2
    return round((number * c + 1) / c, ndigits)

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

Solution (Python):
n = int(input())
ints = [int(num) for num in list(input().split())]
res = []
for x in range(min(ints), max(ints)+1):
    total = 0
    for num in ints:
        total += (x - num)**2
    
    res.append(total)

ans = min(res)
print(ans)

Solution (Python):


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

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


ans=10**18
for i in range(m,M+1):
    ans=min(ans,sum(list(map(lambda x:(i-x)**2,A))))

print(ans)

Solution (Python):


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

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





for j in a:
    cost += (j - mean) * (j - mean)

print(cost)

Solution (Python):
N = int(input())
a = list(map(int,input().split()))
import copy
b = copy.copy(a)
ave = (sum(a) // N) + 1
ave2 = sum(a) // N
for i in range(N):
    a[i] = (a[i] - ave)**2
for i in range(N):
    b[i] = (b[i] - ave2)**2
print(min(sum(a),sum(b)))

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

ans=float('inf')
for i in range(-100,101):
  cost=0
  for j in range(n):
    cost+=(i-A[j])**2
  ans=min(ans,cost)
print(ans)

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

Solution (Python):

ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

N = ni()
A = nl()

s = min(A)
l = max(A)

ans = 10**9
cost= 0
for i in range(s,l+1):
    cost= 0
    for a in A:
        cost += (a-i)**2
    ans = min(ans,cost)

print(ans)

Solution (Python):
n = int(input())
a_array = [int(x) for x in input().split()]
sum_a = sum(a_array)
if sum_a % n == 0:
    sum_a = int(sum_a/len(a_array))
    ans = 0
    for a in a_array:
        ans += abs(sum_a-a)**2
    print(int(ans))
else:
    sum_a1 = int(sum_a/len(a_array))
    sum_a2 = int(sum_a/len(a_array))+1
    ans1 = 0
    ans2 = 0
    for a in a_array:
        ans1 += abs(sum_a1-a)**2
        ans2 += abs(sum_a2-a)**2
    print(int(min(ans1, ans2)))

Solution (Python):
n = int(input())
a_array = [int(x) for x in input().split()]
ans = float("inf")
for i in range(-100, 101):
    tmp = 0
    for a in a_array:
        tmp += abs(a-i)**2
    ans = min(ans, tmp)
print(ans)

Solution (Python):

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

unique_numbers = list(set(numbers))
if len(unique_numbers) == 1:
  print(0)
  exit(0)

min_num = min(numbers)
max_num = max(numbers)

results = []
for i in range(min_num, max_num):
  r = 0
  for num in numbers:
    r += abs((num - i) ** 2)
  results.append(r)

min_r = min(results)
print(min_r)

Solution (Python):




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

ans = float("inf")
for x in [sum(a)//N, -(-sum(a)//N)]:
    tmp = 0
    for idx in range(N):
        tmp += (a[idx]-x)**2
    ans = min(ans, tmp)

print(ans)

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

Solution (Python):
Data = []

def calc(a):
    sum1 = 0
    for d in Data:
        sum1 += (a-d)*(a-d)
    return sum1

N = int(input())
Data = list(map(int,input().split()))
sum1 = sum(Data)
if sum1 % N == 0:
    ave1 = sum1 // N
    ans = calc(ave1)
else:
    ave1 = int(sum1 // N)
    ave2 = ave1-1
    ave3 = ave1+1
    ans = min(min(calc(ave1),calc(ave2)),calc(ave3))
print(ans)

Solution (Python):
mini=10**29 
n=int(input())
l=[int(i) for i in input().split()]
i = round(sum(l) / n )
for z in range(1):
    sm=0  
    for j in l:
        sm+=(i-j)*(i-j)
    mini=min(mini,sm)
print(mini)

Solution (Python):
mini=10**29 
n=int(input())
l=[int(i) for i in input().split()]
for i in range(min(l)-19,max(l)+3):
    sm=0  
    for j in l:
        sm+=(i-j)*(i-j)
    mini=min(mini,sm)
print(mini)

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

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

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

print(ans)

Solution (Python):
import math

from decimal import Decimal, ROUND_HALF_UP


def quantize(f):
    return int(Decimal(str(f)).quantize(Decimal('0'), rounding=ROUND_HALF_UP))


def check(n, numbers):
    average = sum(numbers) / n
    average = quantize(average)
    costs = [int(math.pow(n - average, 2)) for n in numbers]
    return sum(costs)


def main():
    N = int(input())
    numbers = list(map(int, input().split()))
    print(check(n=N, numbers=numbers))


if __name__ == '__main__':
    main()

Solution (Python):
import math

from functools import lru_cache


@lru_cache(maxsize=4096)
def get_cost(x, y):
    return int(math.pow((x - y), 2))


def get_costs(target, numbers):
    return sum([get_cost(target, n) for n in numbers])


def check(n, numbers):
    n_max = max(numbers)
    n_min = min(numbers)
    costs = []
    for target in range(n_min, n_max + 1):
        costs.append(get_costs(target=target, numbers=numbers))

    return min(costs)


def main():
    N = int(input())
    numbers = list(map(int, input().split()))
    print(check(n=N, numbers=numbers))


if __name__ == '__main__':
    main()

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

Solution (Python):

def f(A):
    s = sum(A)
    c = len(A)
    r = s / float(c)
    r = int(round(r))

    cost = 0
    for j in range(len(A)):
        diff = A[j] - r
        cost += (diff * diff)
    return cost

f([4, 2, 5]) == 5
f([1, 1, 3]) == 3

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

Solution (Python):
N=int(input())
li=list(map(int,input().split()))
ave1=sum(li)//N
ave2=sum(li)//N+1
ans1=0
ans2=0
for n in range(len(li)):
  ans1+=(li[n]-ave1)**2
  ans2+=(li[n]-ave2)**2
print(min(ans1,ans2))

Solution (Python):
N=int(input())
li=list(map(int,input().split()))
ave1=sum(li)//N
ave2=sum(li)//N+1
ave3=sum(li)//N-1
ans1=0
ans2=0
ans3=0
for n in range(len(li)):
  ans1+=(li[n]-ave1)**2
  ans2+=(li[n]-ave2)**2
  ans3+=(li[n]-ave3)**2
print(min(ans1,ans2,ans3))

Solution (Python):
N = int(input())
a = [int(i) for i in input().split()]
avg = int(((sum(a) / N)*2+1) //2)
print(sum([(i - avg)**2 for i in a]))

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

print(min([sum([(a - x) ** 2 for a in A]) for x in range(-100, 100+1)]))

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

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

Solution (Python):
from collections import defaultdict

if __name__ == "__main__":
    n = int(input())
    mean = 0
    numbers = defaultdict(int)
    for i in map(int, input().split()):
        numbers[i] += 1
        mean += i
    mean = round(mean / n)
    res = 0
    for number in numbers:
        res += numbers[number] * (mean - number) ** 2

    print(res)

Solution (Python):
from collections import defaultdict

if __name__ == "__main__":
    n = int(input())
    mean = 0
    numbers = defaultdict(int)
    tmp = [int(i) for i in input().split()]
    for i in tmp:
        numbers[i] += 1
        mean += i
    mean = round(mean / n)
    res = 0
    for number in numbers:
        res += numbers[number] * (mean - number) ** 2

    print(res)

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

start = min(a)
end = max(a)

ans = float('inf')
for i in range(start, end + 1):
    temp = 0
    for j in a:
        temp += (j - i) ** 2
    ans = min(ans, temp)
print(ans)

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

Solution (Python):
def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    avg1 = sum(A)//N
    avg2 = avg1 + 1
    ans1 = 0
    ans2 = 0
    for a in A:
        ans1 += (a - avg1)**2
        ans2 += (a - avg2)**2
    print(min(ans1,ans2))

if __name__ == '__main__':
    main()

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

b = sum(a) / n
ceil = math.ceil(b)
floor = math.floor(b)

ans1 = 0
ans2 = 0
for i in range(n):
    ans1 += (a[i] - ceil)**2
for i in range(n):
    ans2 += (a[i] - floor)**2
print(min(ans1, ans2))

Solution (Python):
n = int(input())
ls = list(map(int, input().split()))
m = round(sum(ls) / n)
print(sum([(x - m)**2 for x in ls]))

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time
sys.setrecursionlimit(10**7)
inf = 10**20

N = input()
a = list(map(int,input().split()))
answer = inf
for goal in range(-100,101):
    squaredSum = sum(map(lambda x: (x-goal)**2, a))
    answer = min(answer, squaredSum)
print(answer)

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

ave = sum(A) // N 
if sum(A) % N >= N / 2:
  ave += 1

ans = 0
for i in range(N):
  ans += (A[i] - ave) * (A[i] - ave) 

print(int(ans))

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

mean = sum(As)/len(As)
if mean - int(mean) > 0.5:
    mean = int(mean)+1
else:
    mean = int(mean)

print(sum([(a-mean)**2 for a in As]))

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

x=[]
for i in range(a[0], a[-1]+1):
  b=0
  for j in range(n):
    b+= (a[j]-i)**2
  x.append(b)
print(min(x))

Solution (Python):
n=int(input())
A=list(map(int,input().split()))
ans=0
y=round(sum(A)/n)

for x in A:
  ans+=(x-y)**2
print(ans)

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

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

tmp = 10 ** 9

for i in range(min(As), max(As)+1):
  tmp = min(tmp, sum(map(lambda x: (x-i) ** 2, As)))
  
print(tmp)

Solution (Python):
import numpy as np

N = int(input())
a = list(map(int, input().split()))
ans = 10 ** 6

for i in range(min(a), max(a) + 1):
    b = np.array(a)
    ans = min(ans, np.sum((b - i) ** 2))

print(ans)

Solution (Python):
import numpy as np

N = int(input())
a = list(map(int, input().split()))
ans = 10 ** 6

for i in range(min(a), max(a) + 1):
    b = np.array(a)
    ans = min(ans, np.sum(abs(b - i) ** 2))

print(ans)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
A.sort()
ans = float("inf")
for i in range(A[0],A[-1]+1):
  tmp = 0
  for j in range(N):
    tmp += (i - A[j]) ** 2
  ans = min(ans,tmp)
print(ans)

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

for i in range(a[0],a[-1]+1):
    b=0
    
    for j in range(len(a)):
        b+=(a[j]-i)**2
    s.append(b)

print(min(s))

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

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

Solution (Python):
import math
import functools

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

mean = math.floor(sum(a) / n + 0.5)

print(functools.reduce(lambda x, y: x + (y - mean) ** 2, a, 0))

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

Solution (Python):
N = input()

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


def calc_sq(n):
	return n ** 2

a2 = list(map(calc_sq, a))
"""
map関数の戻り値はリストではなく、シーケンスのmapオブジェクト
なので、インデックスでアクセスしたり、ループで回したりできるが、
appendなどのlistメソッドは使用できない。
また、print関数で出力しても、カンマ区切りの要素では表示されず、
オブジェクトの型名が表示されるだけ。

だが、map関数の戻り値はシーケンスなので、
list関数を使ってlist型に変換することができる！！
"""

sum_a = sum(a)

sum_a2 = sum(a2)

cost = []
for x in range(-101, 101):
	cost.append(int(N) * x**2 - 2 * x * int(sum_a) + int(sum_a2))

print(min(cost))

Solution (Python):
N = input()

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


def calc_sq(n):
	return n ** 2

a2 = list(map(calc_sq, a))
"""
map関数の戻り値はリストではなく、シーケンスのmapオブジェクト
なので、インデックスでアクセスしたり、ループで回したりできるが、
appendなどのlistメソッドは使用できない。
また、print関数で出力しても、カンマ区切りの要素では表示されず、
オブジェクトの型名が表示されるだけ。

だが、map関数の戻り値はシーケンスなので、
list関数を使ってlist型に変換することができる！！
"""

sum_a = sum(a)

sum_a2 = sum(a2)

cost = []
for x in range(-200, 200):
	cost.append(int(N) * x**2 - 2 * x * int(sum_a) + int(sum_a2))

print(min(cost))

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

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
m = round(sum(A) / N)
t = sum(list(map(lambda x: (x - m) ** 2, A)))
print(t)

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

cost_min = float('inf')
for i in range(-100,100+1):
   cost = 0
   for j in range(N):
      cost += (i-A[j])**2
   cost_min = min(cost_min,cost)
  
print(cost_min)

Solution (Python):
from sys import stdin
N = int(stdin.readline().rstrip())
a = [int(x) for x in stdin.readline().rstrip().split()]
m = round(sum(a)/N)
ans = sum([(i-m)**2 for i in a])
print(ans)

Solution (Python):

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


ans = 2**30-1
X = [i-100 for i in range(201)]
for a in X:
    tmp = 0
    for b in A:
        tmp += (b-a)*(b-a)
    ans = min(ans,tmp)
print(ans)

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

costs = []
for y in range(-100, 101):
    l = [(x-y)**2 for x in a]
    costs.append(sum(l))
ans = min(costs)
print(ans)

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

p = min(l)
q = max(l)

ms = []
for i in range(p, q+1):
  m = 0
  for j in range(n):
    m += (l[j]-i)**2
  ms.append(m)
print(min(ms))

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

p = min(l)
q = max(l)

ms = []
for i in range(p, q+1):
  m = 0
  for j in range(n):
    m += (l[j]-i)**2
  ms.append(m)
print(min(ms))

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

ans = float("inf")
for i in range(-100, 101):
    cost = 0
    for ele in a:
        cost += (ele - i) ** 2
    ans = min(ans, cost)

print(ans)

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

cost = [0] * 201
for i in range(201):
    for j in a:
        cost[i] += ((i - 100) - j) ** 2

print(min(cost))

Solution (Python):
n=int(input())
hoge=list(map(int,input().split()))
ans=-1
for i in range(201):
  i=-100+i
  same=0
  for hg in hoge:
    same+=(i-hg)**2
  if(ans==-1):
    ans=same
  else:
    ans=min(same,ans)
print(ans)

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

ave = round(sum(l) / n)
print(sum(list(map(lambda x: (x-ave)**2, l))))

Solution (Python):
from collections import Counter,defaultdict
import sys,heapq,bisect,math,itertools,string,queue
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n = inp()
a = inpl()
min_a = min(a)
max_a = max(a)
res = mod
for i in range(min_a, max_a + 1):
    cost = 0
    for j in a:
        cost += abs(i-j) ** 2
    res = min(res, cost)
print(res)

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

'''
全探索
'''
cost_tmp = 0
cost_min = 10**8

for i in range(min(a),max(a)+1):
    for j in range(n):
        cost_tmp += pow(a[j]-i,2)
    if cost_tmp < cost_min:
        cost_min = cost_tmp
    cost_tmp=0

print(cost_min)

Solution (Python):
def f(x, y):
    return (x - y)**2


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

ans = 10**9
for k in range(-100, 101):
    temp = sum([f(x, k) for x in A])
    ans = min(ans, temp)
print(ans)

Solution (Python):
import numpy as np
N = int(input())
A = np.array([int(a) for a in input().split()])
ans = 200**2 * 100
for i in range(min(A), max(A)+1):
    a = sum((A-i)**2)
    if a < ans:
        ans = a
print(ans)

Solution (Python):
import statistics
N = int(input())
a = list(map(int, input().split()))
s = sorted(a)
ans = 10 ** 9 + 7
for i in range(s[0], s[-1] + 1):
    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(" ")))
s=sum(a)
ave=s/N
ave=round(ave)
b=list(map(lambda x:(x-ave)**2,a))
print(sum(b))

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
y = sum(a) / N
if y < 0:
  y = int(y - 0.5)
else:
  y = int(y + 0.5)
print(sum((x - y) * (x - y) for x in a))

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

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

Solution (Python):
n = int(input())
list_a = list(map(int, input().split(" ")))
target = sum(list_a) / n
if target - int(target) >= 0.5:
  target = int(target) + 1
else:
  target = int(target)
cost = 0
for one_a in list_a:
  cost += (one_a - target) ** 2
print(cost)

Solution (Python):
def resolve():
    n = int(input())
    a = list(map(int, input().split()))
    ave = round(sum(a) / n)
    ans = sum([(ai - ave) ** 2 for ai in a])
    print(ans)


if __name__ == "__main__":
    resolve()

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

ans=10**10
for i in range(-100,101):
  ans=min(sum(list(map(lambda x: (x-i)**2,A))),ans)

print(ans)

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

Solution (Python):
n,A=open(0);print(min(sum((x-int(a))**2for a in A.split())for x in range(-100,101)))

Solution (Python):
n,*A=map(int,open(0).read().split());print(min(sum((x-a)**2for a in A)for x in range(-100,101)))

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
cost = 10000000000
for i in range(-100, 101):
    cur_cost = 0
    for e in a:
        cur_cost += (e - i) ** 2
    cost = min(cur_cost, cost)
    
print(cost)

Solution (Python):

N = int(input())
lst = list(map(int, input().split()))
high = max(lst)
low = min(lst)
ans = 5000000
for i in range(low, high+1):
    cost = 0
    for j in range(N):
        cost += (i-lst[j])**2
    if (cost <= ans):
        ans = cost
print(ans)

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

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

    ans = sum((a - min(a)) ** 2)
    for i in range(min(a)+1, max(a)+1):
        ans = min(ans, sum((a - i) ** 2))    
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):

import sys
input = sys.stdin.readline
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7


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

ans = INF
for i in range(min(A), max(A)+1):
    x = 0
    for j in range(N):
        x += (A[j]-i)**2

    ans = min(ans, x)

print(ans)

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

a1=0
a2=0
for i in range(len(a)):
  m1=sum(a)//n
  m2=-(-sum(a)//n)
  a1+=(a[i]-m1)**2
  a2+=(a[i]-m2)**2
  
print(min(a1,a2))

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

ans = 10**100

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

Solution (Python):
N = int(input())
l = list(map(int, input().split()))
c = 0
b = 0
for i in range(N):
    c += l[i] ** 2
    b += -2 * l[i]
x = -b / (2 * N)
x = round(x)
Ans = N * x ** 2 + b * x + c

print(Ans)

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

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

print(aSum)

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

print(aSum)

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

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

ans = 200**2 * 105
for b in range(a_min, a_max+1):
    cost = 0
    for i in range(n):
        cost += (a[i] - b) ** 2
    ans = min(ans, cost)

print(ans)

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

for i in range(-100,101):
    total = 0
    for j in range(N):
        total+=(A[j]-i)*(A[j]-i)
    if mini>=total:
        mini=total

print(mini)

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