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())
li = list(map(int,input().split()))
s = 0
ans =10**6
a,b = min(li),max(li)
for i in range(a,b+1):
    for j in range(len(li)):
        s += (i-li[j])**2
    ans = min(ans,s)
    s = 0

print(ans)

Solution (Python):
N = int(input())
a = [int(i) for i in input().split()]
num = int(sum(a) / N)
ans = float('inf')

for i in [num - 1, num, num + 1]:
    ans = min(ans, sum([abs(j - i) ** 2 for j in a]))
    
print(ans)

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

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

print(ans)

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

sum = 0
result = 0

for i in A:
    sum+=i
ave = round(sum/len(A))

for i in A:
    result += pow(i-ave,2)

print(round(result))

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

num.sort()
s = num[0]
e = num[-1] + 1

cost = []
for i in range(s, e):
  cost.append(sum(list(map(lambda x: (x - i)**2, num))))
  
print(min(cost))

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

set_a = set(a)

import sys

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

totals = []

for i in range(min(a),max(a)+1):
    temp = 0
    for j in range(N):
        temp += (a[j]-i)**2
    totals.append(temp)

res = min(totals)
print(res)

Solution (Python):
N = int(input())
A = sorted(list(map(int,input().split())))
z = 10**6

for i in range(A[0],A[-1]+1):
  z = min(z,sum([(a-i)**2 for a in A]))

print(z)

Solution (Python):
n = int(input())
L = [int(i) for i in input().split()]
tg = round(sum(L)/n)

cost = 0
for i in range(n):
  cost += pow((L[i]-tg),2)
print(cost)

Solution (Python):
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools,pdb
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
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())
def _pf(s): return print(s, flush=True)

N = _I()
A = LI()

"""
全数字を例えば1に変える場合など、試していけば良い
"""

ans = []
for i in range(min(A), max(A)+1):
    tmp = 0
    for a in A:
        tmp += abs(a - i) ** 2
    ans.append(tmp)

print(min(ans))

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

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

print(min(cost))

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

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

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

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

check=float('inf')
for i in range(-100,101):
    ans=0
    for j in a:
        ans+=(i-j)**2
    if check>ans:
        check=ans

print(check)

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

Solution (Python):
a = input()
b = list(map(int, input().split()))
c = round(sum(b) / len(b))
s = 0
for i in range(len(b)):
    s += ((b[i] - c) ** 2)
print(s)

Solution (Python):
n = input()
a = [int(i) for i in input().split()]
print(min([sum((x - y) ** 2 for x in a) for y in range(min(a), max(a) + 1)]))

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

Solution (Python):


import statistics
N = int(input().split()[0])
a_list = list(map(int, input().split()))


min_cost = float('inf')

for x in range(-100, 101):
    cost = 0
    for a in a_list:
        cost += (a - x) ** 2
    if cost < min_cost:
        min_cost = cost

print(min_cost)

Solution (Python):
import sys

stdin = sys.stdin
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()

n = ni()
li = na()
ans = float("inf")
for j in range(-100, 101):
    ct = 0
    for i in range(n):
        ct += (li[i] - j) ** 2
    ans = min(ans, ct)
print(ans)

Solution (Python):
def cul(li,s):
  a = 0
  for i in li:
    a += (i - s)**2
  
  return a

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

s1 = sum(li)//len(li)
s2 = s1 + 1

ans1 = cul(li,s1)
ans2 = cul(li,s2)

print(min(ans1,ans2))

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
al = min(a)
ar = max(a)
ans = 1000000000000000
for i in range(al, ar+1):
    tmp = 0
    for j in a:
        tmp += (j-i) ** 2
    if ans > tmp:
        ans = tmp
print(ans)

Solution (Python):
from statistics import mean

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

ave = mean(a_list)

ave = round(ave)

sum = 0

for x in a_list:
    sum = sum + (x - ave) * (x - ave)

print(sum)

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

Solution (Python):
def main():
  n = int(raw_input())
  row = [int(x) for x in raw_input().split(' ')]
  min_val = float('inf')
  
  is_eq = True
  for x in row:
    if x != row[0]:
      is_eq = False
  if is_eq:
    print('0')
    return
  
  for x in xrange(-100, 101):
    data = [(elem - x)**2 for elem in row]
    if sum(data) < min_val:
      min_val = sum(data)
  print(min_val)

main()

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

costs = []

for i in range(-100, 101):
  cost = 0
  for j in range(n):
    cost += (a[j] - i) * (a[j] - i)
  costs.append(cost)

print(min(costs))

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

Solution (Python):
N=int(input())
A = list(map(int, input().split()))
s = 0
m = 99999999999999
for i in range(-100, 101):
  s = 0
  for a in A:
    s += (a-i)**2
  m = min(m, s)
print(m)

Solution (Python):
N = int(input())
a = list(map(int,input().split()))
b = sum(a)
c = sum(map(lambda x: x ** 2, a))
d = round(b/N) 
ans = N*d**2 - 2*d*b+c
print(int(ans))

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

ave = round(sum(A) / N)

var = 0

for a in A:
    var += (a - ave) ** 2

print(var)

Solution (Python):
n=int(input())
a=[int(j) for j in input().split()]
ans=10**100

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

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

squares_sums = []
for y in range(min(numbers), max(numbers)+1):
    squares_sums.append(sum([((x - y) ** 2) for x in numbers]))
print(min(squares_sums))

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

min_n = min(numbers)
max_n = max(numbers)

squares_sums = []
for y in range(min_n, max_n+1):
    squares_sums.append(sum([((x - y) ** 2) for x in numbers]))
print(min(squares_sums))

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

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

ave = round(sum(a)/n)
cost=0
for i in range(n):
    cost+=(a[i]-ave)**2
print(cost)

Solution (Python):

import sys
import copy
import collections
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush, heapify
import math
import itertools
import random
 




 
INF = float('inf')
def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
 
def main():
    N = inputInt()
    a = inputList()
    
    flg = True
    for i,vol in enumerate(a):
        if i == 0:
            continue
        if vol != a[i-1]:
            flg = False
            break
            
    if flg == True:
        print(0)
        sys.exit()
    
    ans = INF
    for i in range(-100,101):
        cst = 0
        for j,vol in enumerate(a):
            cst += (vol-i)**2
            
        if ans > cst:
            ans = cst
            
            
    print(ans)
    
if __name__ == "__main__":
	main()

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

ans = 10**10

m1 = a[int(n/2)]
m2 = int(sum(a) / n)
m3 = ceil(sum(a) / n)

for v in [m1, m2, m3]:
    cost = 0
    for i in range(n):
        cost += (a[i] - v) ** 2
    ans = min(cost, ans)
print(ans)

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

ave=sum(a)/n
ave_upper=math.ceil(ave)
ave_lower=math.floor(ave)

ans_lower=0
ans_upper=0
for i in a:
    ans_lower+=(ave_lower-i)**2
    ans_upper+=(ave_upper-i)**2

print(min(ans_lower, ans_upper))

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
a.sort()
cost=10000000
for i in range(a[0],a[-1]+1):
  wa=0
  for j in range(n):
    wa+=(a[j]-i)**2
  if wa<cost:
    cost=wa
print(cost)

Solution (Python):
def main():
    N = int(input())
    A = [int(a) for a in input().split()]
    ave = sum(A) // N
    min_cost = float('inf')
    for v in [ave, ave+1]:
        cost = 0
        for a in A:
            cost += pow(a-v, 2)
        min_cost = min(min_cost, cost)

    return min_cost

if __name__ == '__main__':

    print(main())

Solution (Python):


def main():
    
    N = int(input())
    A = tuple(map(int, input().split()))
    ans = []
    for i in range(min(A), max(A) + 1):
        cnt = sum((i - a) ** 2 for a in A)
        ans += [cnt]
    print(min(ans))


if __name__ == "__main__":
    main()

Solution (Python):

def main():
    N = int(input())
    A = tuple(map(int, input().split()))
    ans = []
    for i in range(min(A), max(A) + 1):
        cnt = sum((i - a) ** 2 for a in A)
        ans += [cnt]
    print(min(ans))


if __name__ == "__main__":
    main()

Solution (Python):
from collections import defaultdict
from heapq import heappush, heappop
import math
import bisect
import random

def LI(): return list(map(int, input().split()))
def I(): return int(input())
def LIM(): return list(map(lambda x:int(x) - 1, input().split()))
def LS(): return input().split()
def S(): return input()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def LIRM(n): return [LIM() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
mod = 1000000007



def main():
    n = I()
    n_list =  sorted(LI())
    ans = float('inf')
    for i in range(n_list[0], n_list[-1] + 1):
        ret = 0
        for j in n_list:
            ret += (i - j) ** 2
        ans = min(ans, ret)


    return ans








print(main())

Solution (Python):

import copy
n = int(input())
a = list(map(int, input().split()))
a_avg = round(sum(a)/len(a))
b = copy.copy(a)
while(len(set(b)) > 1):
    for i in range(len(a)):
        if b[i] > a_avg:
            b[i] -= 1
        if b[i] < a_avg:
            b[i] += 1
s = 0
for i in range(len(a)):
    s += (a[i]-b[i])**2
print(s)

Solution (Python):
import sys
sys.setrecursionlimit(12345678)
import itertools
from collections import Counter
from collections import defaultdict
from collections import deque
import bisect
from heapq import heappush, heappop


def main():
    n = int(input())
    al = list(map(int, input().split()))
    ans = 10**15
    for i in range(-100,101):
        y = i
        cost = 0
        for x in al:
            cost += (x - y) ** 2
        ans = min(ans, cost)

    print(ans)


if __name__ == '__main__':
    main()

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

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

l_min = min(lst)
l_max = max(lst)

m = 10**8

for i in range(l_min,l_max+1):
    c = 0
    
    for j in range(n):
        c += (lst[j]-i)**2
    
    if c < m:
        m = c

print(m)

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

Solution (Python):
n = int(input())
an = [int(x) for x in input().split()]
avg = round(sum(an)/n)
print(sum([(ai - avg)**2for ai in an]))

Solution (Python):
N = int(input())
l = list(map(int, input().split()))
l.sort()
min = 1000000000000
for v in range(l[0],l[-1]+1):
    tmp = sum([(i-v)**2 for i in l])
    if tmp < min:
        min = tmp
print(min)

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

Solution (Python):
N = int(input())
As = list(map(int, input().split()))
sAs = sum(As)
s2As = sum(map(lambda a: a*a, As))

cs = []
for x in range(min(As), max(As)+1):
  c = N*x*x-2*x*sAs+s2As
  cs.append(c)
r = min(cs)
print(r)

Solution (Python):
N = int(input())
A = [int(x) for x in input().split()]
from math import floor, ceil

def cost(x):
    return sum((x-a)**2 for a in A)

print(min(
  cost(ceil(sum(A)/N)),
  cost(floor(sum(A)/N))
         )
     )

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

a.sort()

d=[[0]*N for i in range(max(a)-min(a)+1)]
for i in range(max(a)-min(a)+1):
  for j in range(N):
      d[i][j]=(a[j]-(min(a)+i))**2

print(min([sum(dd) for dd in d]))

Solution (Python):
def C_BeTogether():
    N = int(input())
    a = list(map(int,input().split()))
    average = round(sum(a) / N)
    ans = 0
    for x in range(N):
        ans += pow(a[x] - average, 2)
    print(ans)
    
if __name__ == "__main__":
    C_BeTogether()

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

min_score = None
for target in range(-100, 101):
  score = 0
  for a in a_list:
    score += (target - a) * (target - a)
  if min_score is None or min_score > score:
    min_score = score
print(min_score)

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

ans = 100100100
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):
n = int(input())
a = []
a = input().split()
Sum = 0

for i in range(n):
  Sum = Sum + int(a[i])

average = int(Sum / n)

answer_1 = 0
answer_2 = 0
for i in range(n):
  answer_1 = answer_1 + (int(a[i]) - average) * (int(a[i]) - average)
  answer_2 = answer_2 + (int(a[i]) - average - 1) * (int(a[i]) - average - 1)

  answer = min(answer_1, answer_2)
print(answer)

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

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

sum = 0
for i in range(N):
	sum += (a[i] - ave)**2

print(sum)

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

Solution (Python):

import numpy as np

N = int(input())
a = list( map(int, input().split()) )
b = np.asarray(a, np.float)

ave = np.average(b)
ave_floor = np.floor(ave)
ave_ceil = np.ceil(ave)

cost1 = np.sum((b-ave_floor)**2)
cost2 = np.sum((b-ave_ceil)**2)
min_cost = int( np.min([cost1, cost2]) )

print(min_cost)

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

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
s = sum(a)
m = s/n
if s%n==0:
    cost = 0
    for i in range(n):
        cost += (a[i]-s//n)**2
else:
    m1 = m//1
    m2 = m//1+1
    cost1 = 0
    cost2 = 0
    for i in range(n):
        cost1 += (a[i]-m1)**2
        cost2 += (a[i]-m2)**2
    cost = min(cost1,cost2)
print(int(cost))

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

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

Solution (Python):

import statistics
import math

N= int(input())

k = input()
l = [int(j) for j in k.split(" ")]
w = 0

mean = round(statistics.mean(l))
for i in range(len(l)):
	w += (l[i] - mean) ** 2
print(w)

Solution (Python):

import collections
import itertools
import sys
import time

def getint(): return int(input())
def getints(): return list(map(int, input().split()))

n = getint()
xs= getints()

res = 100**2 * n

for g in range(-100,101):
    tmp = 0
    for x in xs:
        tmp += (x-g)**2
    res = min(res, tmp)

print(res)

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

Solution (Python):
n = int(input())
*l, = map(int,input().split())
ans = 10**6
for i in range(-100,101):
  tmp = sum([(y-i)**2 for y in l])
  ans = min(tmp,ans)
print(ans)

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
a.sort()
search = [i for i in range(a[0], a[-1]+1)]
ans = 100000

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


print(min(cost))

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

Solution (Python):
N = input()
a = list(map(int,input().split()))
N = int(N)
ave = 0.0
for i in a:
    ave += i

ave /= N
ave = round(ave)

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

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

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

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

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

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

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

Solution (Python):
n=input()
kazu=list(map(int, input().split()))
kotae=9999999999
for i in range(-100,101):
	kei=0
	for j in kazu:
		jijo=(i-j)**2
		kei+=jijo
	if kei<kotae:
		kotae=kei
print(kotae)

Solution (Python):
N = int(input())
nums = list(map(int, input().split()))
min_cost = 4*100**3
for i in range(-100, 101):
  cost = 0
  for num in nums:
    cost += (i-num)**2
  min_cost = min(min_cost, cost)
print(min_cost)

Solution (Python):
N = raw_input()
a = map(int,raw_input().split())
max_num = max(a)
min_num = min(a)

ans = []
b = 0
for i in range(min_num,max_num+1):
    for j in range(0,int(N)):
        b +=(a[j]-i)*(a[j]-i) 
    ans.append(b)
    b = 0
print min(ans)

Solution (Python):

import sys, math, fractions, itertools


def solve(N: int, a: "List[int]"):
    s = sum(a)
    k1 = math.floor(s / N)
    k2 = math.ceil(s / N)

    d1 = 0
    d2 = 0
    for i in range(N):
        d1 += (a[i] - k1) ** 2
        d2 += (a[i] - k2) ** 2
    print(min(d1, d2))
    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):
num = int(input())
A = list(map(int, input().split()))

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

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

sumA = sum(A)
obj = round(sumA/num)
sum2A = 0
for i in range(num):
    sum2A += A[i] ** 2
print(sum2A - (2*obj*sumA) + (num*obj**2))

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

mean = round(sum(a) / n)

ans = 0

for i in a:
  ans += (i-mean) ** 2

print(ans)

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

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

min_sum = 100000000
temp_sum = 0
ans = 0
for i in range(-100,101):
    for j in range(N):
        temp_sum += pow(i-a_list[j],2)
        if j==N-1:
            if temp_sum < min_sum:
                min_sum = temp_sum
            temp_sum = 0
print(min_sum)

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

ave = sum(numbers) / N

ave_int = round( ave )


ans = 0
for number in numbers :
  ans += (number - ave_int ) **2
print( ans )

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

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

for i in range(-100, 101):
    tmp = 0
    for j in range(N):
        tmp += (i-a[j])**2
    ans_lsit.append(tmp)

ans = min(ans_lsit)
print(ans)

Solution (Python):

import sys
sys.setrecursionlimit(300000)


def solve(N: int, a: "List[int]"):
    ret = float('inf')
    mn = min(a)
    mx = max(a)
    for v in range(mn, mx + 1):
        tmp = 0
        for x in a:
            tmp += (x - v) ** 2
        ret = min(ret, tmp)
    print(ret)
    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 math
n = int(input())
a = list(map(int, input().split()))

ave = sum(a)/n
s_ave = math.ceil(ave)
l_ave = math.floor(ave)
s_cost = 0
l_cost = 0
for i in range(n):
  s_cost += (a[i]-s_ave)**2
  l_cost += (a[i]-l_ave)**2

print(min(s_cost, l_cost))

Solution (Python):

import sys


def main():
    num = int(input())
    data = list(map(int, input().split()))
    half1, half2 = sum(data) // num, sum(data) // num + 1

    ans1 = 0
    for i in range(num):
        ans1 += (data[i] - half1) ** 2

    ans2 = 0
    for i in range(num):
        ans2 += (data[i] - half2) ** 2

    print(min(ans1, ans2))


if __name__ == '__main__':
    main()

Solution (Python):


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

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


if __name__ == "__main__":
    solve()

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

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

print(cost)

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

ans = 200**2*1000
for k in range(-100, 101):
    l = 0
    for a in A:
        l += abs(a-k)**2
    ans = min(ans, l)

print(ans)

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

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

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

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

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

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

print(min(l))

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

print(min(l))

Solution (Python):
def smaller(x, y):
	if x < y:
		return x
	return y

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 += (A[j]-i)**2
	min = smaller(min, sum)
print(min)

Solution (Python):

N = int(input())


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


ans = float("inf")

for i in range(201):

    i -= 100

    now = 0

    for a in A:

        now += ( a - i ) ** 2

    ans = min (ans,now)

print (ans)

Solution (Python):
import numpy as np
N = int(input())
a = list(map(int, input().split()))
a_np = np.array(a)
b = []
for i in range(a_np.min(), a_np.max()+1):
  c = a_np - i
  d = [j*j for j in c]
  b.append(sum(d))
print(min(b))

Solution (Python):
a = int(input())
b = list(map(int, input().split()))
b.sort()
c = []
for i in range(b[0], b[len(b)-1] + 1):
    d = 0
    for j in range(len(b)):
        d += (b[j] - i)**2
    c.append(d)
c.sort()
print(c[0])

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

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

min = 100*(200)**2+10

for n in range(-100,101):
    tmp = 0
    for a in A:
        if a == n:
            pass
        else:
            tmp += (a - n)**2

    if min > tmp:
        min = tmp

print(min)

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

big = max(A)
sml = min(A)

ans = float('inf')

for i in range(sml, big + 1):
    cnt = 0
    for a in A:
        cnt += (abs(i - a))**2
    ans = min(ans, cnt)
print(ans)

Solution (Python):



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

mi = float('inf')

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

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



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

min_cost = 10000 * 100
for num in range(min_a, max_a + 1):
    cost = 0
    for z in a:
        cost += (z - num) ** 2
    if min_cost > cost:
        min_cost = cost

print(min_cost)

Solution (Python):
_ = input()
a = list(map(int, input().split()))
ans = 10 ** 18
for val in range(-200, 200):
    curr = 0
    for it in a:
        curr += (it - val) ** 2
    ans = min(ans, curr)
print(ans)

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

ret = float('inf')
for i in range(-100, 101) :
    ret = min(ret, sum([(a_ - i) ** 2 for a_ in a]))
    
print(ret)

Solution (Python):


import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline 

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


m = sum(a)//n

ans = 100000000
for i in {-1,0,1}:
    res = sum((x-m-i)**2 for x in a)
    ans = min(ans,res)

print(ans)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
ans=10**9+7
for i in range(201):
    memo=0
    for j in A:
        memo+=(i+j-100)**2
    ans=min(ans,memo)
print(ans)

Solution (Python):
from math import floor, ceil
from statistics import mean
n = int(input())
a = list(map(int, input().split()))
mc = ceil(mean(a))
mf = floor(mean(a))
dc = sum([(i-mc)**2 for i in a])
df = sum([(i-mf)**2 for i in a])
print(min(dc, df))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
nijo_list = []
for i in range(min(a), max(a)):
    nijo_sum = 0
    for j in range(len(a)):
        nijo = (a[j] - i)**2
        nijo_sum += nijo
    nijo_list.append(nijo_sum)
if nijo_list:
    print(min(nijo_list))
else:
    print(0)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
ttl1, ttl2, ttl3 = 0, 0, 0
avarage1 = -(-sum(A) // N)
avarage2 = sum(A) // N
for i in A:
    ttl1 += (i - avarage1) ** 2
    ttl2 += (i - avarage2) ** 2
print(min(ttl1, ttl2))

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
ttl1, ttl2, ttl3 = 0, 0, 0
avarage1 = -(-sum(A) // N)
avarage2 = sum(A) // N
avarage3 = round(sum(A) / N)
for i in A:
    ttl1 += (i - avarage1) ** 2
    ttl2 += (i - avarage2) ** 2
    ttl3 += (i - avarage3) ** 2
print(min(ttl1, ttl2, ttl3))

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

r = []
for i in range(-100, 101):
	sum = 0
	for j in range(n):
		sum += (a[j]-i)**2
	r.append(sum)

r.sort()
print(r[0])

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

def fn(m):
    rtn=0
    for a in al:
        rtn+=(a-m)**2
    return rtn

mean=sum(al)//n
print(min(fn(mean),fn(mean+1)))

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

floor = math.floor(sum(a) / len(a))
ceil = math.ceil(sum(a) / len(a))

ceil_sum = sum([(item - ceil) ** 2 for item in a])
floor_sum = sum([(item - floor) ** 2 for item in a])

print(min(ceil_sum, floor_sum))