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 6s 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 2s 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 1s.

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 4s 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.



Sample input:
2
4 8


Sample output:
8

Submission status: Accepted

Solution code:
import sys
import os
if sys.platform=="darwin":
	base = os.path.dirname(os.path.abspath(__file__))
	name = os.path.normpath(os.path.join(base, '../Documents/input.txt'))
	#print(name)
	sys.stdin = open(name)


n = int(input())

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

if n == 1:
	print(0)
	exit()


minans = float('inf')
chk = float('inf')

for i in range(min(l),max(l)+1):
	test = 0
	#print(i)
	for j in range(len(l)):
		#print(i,(l[j]-i)**2)
		test = test + (l[j]-i)**2
		#print(test)

	if test < minans:
		#print(minans)
		#print(test)
		minans = test
		chk = i

#print(chk)
print(minans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
l = [int(i) for i in input().split()]
 
ans = float('inf')
for i in range(-100,101):
  num = 0
  for j in l:
    num += (i-j)**2
  ans = min(ans,num)
  
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
s=sum(l)
a,b=s//n,-(-s//n)
print(min(sum([(z-a)**2 for z in l]),sum([(x-b)**2 for x in l])))

Submission status: Accepted

Solution code:
# coding: utf-8
from math import ceil
from statistics import median, mean
N = int(input())
A = list(map(int, input().split()))
ans = 10**9
for m in range(-100, 101):
    tmp = 0
    for i in range(N):
        tmp += (A[i] - m)**2
    ans = min(ans, tmp)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = [int(a) for a in input().split()]
mi = 1<<100
for i in range(min(A), max(A)+1):
    mi = min(mi, sum([(A[j]-i)**2 for j in range(N)]))

print(mi)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline

N = int(readline())
A = list(map(int, readline().split()))
inf = 10**9 +7
ans = inf
for j in range(min(A), max(A) + 1):
    res = 0
    for a in A:
        res += (a-j)**2
    ans = min(ans, res)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = sorted([int(i) for i in input().split()])
ans = 10**9
for i in range(-100,101):
    cnt = 0
    for j in range(n):
        cnt += (a[j] - i)**2
    ans = min(ans,cnt)
print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split()))
    m = min(A)
    M = max(A)

    p = 10**10
    for i in range(m, M+1):
        c = 0
        for a in A:
            c += (a-i)**2
        if c < p:
            p = c

    print(p)


main()

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
A = np.array(list(map(int, input().split())))
answer = 10 ** 6
#全探索
for i in range(-100, 101):
    tmp = sum((A-i) ** 2)
    if answer > tmp:
        answer = tmp
print(answer)

Submission status: Accepted

Solution code:
# ARC 059: C – いっしょ / Be Together
import math

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

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

cost_ceil = 0
cost_floor = 0
for i in range(n):
    cost_ceil += (a[i] - average_ceil) ** 2
    cost_floor += (a[i] - average_floor) ** 2

print(min(cost_ceil, cost_floor))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
alist = [int(i) for i in input().split()]
bestcost = 1000000000
besti = 1000000
for i in range(min(alist),max(alist)+1):
    cost = sum([(a-i)**2 for a in alist])
    if cost < bestcost:
        bestcost = cost
        besti = i

print(bestcost)

Submission status: Accepted

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

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

t=0
for a in A:
    t+=(a-ave)**2
print(t)

Submission status: Accepted

Solution code:
import sys
n = int(input())
a = list(map(int, input().split()))
d = max(a)-min(a)
if d == 0:
    print(0)
    sys.exit()
m = 0
for i in a:
    m += (d-i) ** 2
for i in range(d):
    c = min(a) + i
    cost = 0
    for j in a:
        cost += (c-j) ** 2
    if m > cost:
        m = cost
print(m)

Submission status: Accepted

Solution code:
def sds(a,x):
    return sum([(ai-x)**2 for ai in a])

N=int(input())
a=list(map(int,input().split()))
ans=min([sds(a,x) for x in range(-100,101)])
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
mn = min(a)
mx = max(a)+1
ans = 10**9
for i in range(mn, mx):
  cost = 0
  for aa in a:
    cost += (i - aa)**2
  if ans > cost:
    ans = cost
print(ans)

Submission status: Accepted

Solution code:
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
s = 0
b = 0
count = int(input())
list_A = list(map(int, input().split()))
for a in list_A:
  s = s + a
s = s/count
s = Decimal(s).quantize(Decimal('0'), rounding=ROUND_HALF_UP)
for a in list_A:
  b = b + (a - s) * (a - s)
print(b)

Submission status: Accepted

Solution code:
import numpy as np
N=int(input())
A=list(map(int,input().split()))
mA=int(np.mean(A))
mA2=mA+1
print(min(sum([(i-mA)**2 for i in A]),sum([(i-mA2)**2 for i in A])))

Submission status: Accepted

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

x = sum(A)/len(A)
totals = []
for i in range(min(A), max(A)+1):
    total = 0
    for a in A:
      total += (a-i)**2
    totals.append(total)
print(min(totals))

Submission status: Accepted

Solution code:
"ARC 059 C - いっしょ / Be Together"

if __name__ == "__main__":
    N = int(input())
    a = list(map(int, input().split()))

    ans = 10**12

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
ans=10**10
for i in range(min(A),max(A)+1):
    cnt=0
    for j in A:
        cnt +=(i-j)**2
    ans=min(ans,cnt)
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline


def main():
    N = int(input())
    A = list(map(int, input().split()))
    ans = (200**2)*100
    for m in range(-100, 101):
        x = 0
        for a in A:
            x += (m-a)**2
        ans = min(ans, x)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
l,r=max(A),min(A)
cnt=10**10
for i in range(r,l+1):
    ans=0
    for j in A:
        ans +=(j-i)**2
    cnt=min(cnt,ans)
print(cnt)

Submission status: Accepted

Solution code:
from statistics import mean
n = int(input())
a = list(map(int, input().split()))
t = round(mean(a))

sum = 0
for i in a:
    sum += (i - t)**2

print(sum)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
m=min(a)
M=max(a)
ans=10**8
cnt=0
if m-M==0:
    ans=0
for i in range(m,M):
    for j in range(n):
        cnt+=(a[j]-i)**2
    ans=min(ans,cnt)
    cnt=0
print(ans)

Submission status: Accepted

Solution code:
# ABC043C - いっしょ / Be Together (ARC059C)
def main():
    # find the min cost to rewrite all to i (min(A) <= i <= max(A))
    N, *A = map(int, open(0).read().split())
    ans = 1 << 60
    for i in range(min(A), max(A) + 1):
        ans = min(ans, sum((i - a) ** 2 for a in A))
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
ave=sum(a)//n
cost=0
for i in range(n):
    cost+=(a[i]-ave)**2
for i in range( min(a),max(a)+1 ):
    cost2=0
    for j in range(n):
        cost2+=(a[j]-i)**2
        if cost2 > cost:
            break
    if cost2 < cost:
        cost=cost2
print(cost)

Submission status: Accepted

Solution code:
def slove():
    import sys
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    a = list(map(int, input().rstrip('\n').split()))
    mt = 10 ** 10
    mi = min(a)
    ma = max(a)
    for i in range(mi, ma + 1):
        t = 0
        for v in a:
            t += ((i - v) ** 2)
        mt = min(mt, t)
    print(mt)


if __name__ == '__main__':
    slove()

Submission status: Accepted

Solution code:
n = int(input())
List = list(map(int,input().split()))
mean = sum(List)/n
round_mean = round(mean)
cost = [(i-round_mean)**2 for i in List]

print(int(sum(cost)))

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
a = np.array([int(_) for _ in input().split()])
b =[]
for i in range(n):
  m = int(round(a.mean()))
  c = (a[i]-m)**2
  b.append(c)
print(sum(b))

Submission status: Accepted

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

cost = 0
ave = sum(A)/N
if math.ceil(ave)-ave < 0.5:
    ave = math.ceil(ave)
else:
    ave = math.floor(ave)
for a in A:
    cost += (a-ave)**2
print(cost)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
minA = min(A)
maxA = max(A)
ans = float("inf")
for i in range(minA, maxA + 1):
    cnt = 0
    for a in A:
        cnt += (i - a) ** 2
    ans = min(ans, cnt)
print(ans)

Submission status: Accepted

Solution code:
# C - いっしょ / Be Together
N = int(input())
A = list(map(int, input().split()))
ans = 10**8

for X in range(-100,101):
    tmp = 0
    for i in range(N):
        tmp += (A[i]-X)**2
    if ans >= tmp:
        ans = tmp
    else:
        pass

print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
import sys

def main():
    def input():
        return sys.stdin.readline()[:-1]
    N = int(input())
    a = list(map(int,input().split()))
    ans = 1000000
    for k in range(-100,101):
        t = 0
        for e in a:
            t += (e-k)**2
        ans = min(ans,t)
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Accepted

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

ans = float('inf')
for k in range(-100,101):
    tmp = 0
    for a in A:
        tmp += (a-k)**2
    ans = min(ans, tmp)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
ans = float('inf')
for i in range(-100, 101):
    ans = min(ans, sum([abs(a - i) ** 2 for a in A]))
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
A = sorted(A)
S = sum(A)

if S//N*N==S:
    C1 = S//N
    C2 = S//N
else:
    C1 = S//N
    C2 = S//N+1
ans1 = 0
ans2 = 0
for i in range(N):
    ans1 += (A[i]-C1)**2
    ans2 += (A[i]-C2)**2
#print(ans1,ans2)
print(min(ans1,ans2))

Submission status: Accepted

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

a1=sum(a)//N
a2=(sum(a)-1+N)//N

ans1=0
ans2=0

for i in range(N):
    ans1+=(a[i]-a1)**2
    ans2+=(a[i]-a2)**2

print(min(ans1,ans2))

Submission status: Accepted

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

y = round(sum(A) / N)
ans = 0

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

s = sum(a)/n

p=0
m=0
for i in range(n):
  p += (a[i]-s//1)**2
  m += (a[i]-(-(-s//1)))**2
print(int(min(p,m)))

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
cost_odd = 0
cost_even = 0

if a.count(a[0]) == N:
    print(0)
    exit()

num_even = sum(a) // N + 1
num_odd = sum(a) // N

for i in range(N):
    cost_even += (a[i] - num_even)**2
    cost_odd +=  (a[i] - num_odd)**2

print(min(cost_odd, cost_even))

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
a = np.array([int(i) for i in input().split()])
m = int(round(a.mean()))
print(np.sum((a-m)**2))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

E = round(sum(A) / N)

print(sum([(a - E) ** 2 for a in A]))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
s = 0
m = 100000000
for j in range(-100,101):
  for i in range(N):
    s += (A[i]-j)**2
  m = min(m, s)
  s = 0
print(m)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
temp = sum(a)
if temp / N > temp // N + 0.5:
  temp = temp // N + 1
else: temp = temp // N
res = 0
for i in range(N):
  res += (a[i] - temp) ** 2
print(res)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = float('inf')

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

print(ans)

Submission status: Accepted

Solution code:
# 2019/08/15

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

res=float('inf')
for y in range(min(x),max(x)+1):
    cnt=0
    for i in range(n):
        cnt+=(x[i]-y)**2
    res=min(res,cnt)
print(res)

Submission status: Accepted

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

ans = float('inf')

for base in range(min(A), max(A) + 1):
    ans = min(ans, sum([abs(base - a)**2 for a in A]))

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
cost=9999999999
for i in range(-100,101):
  c=sum((i-a)**2 for a in A)
  if c<cost:
    cost=c
print(cost)

Submission status: Accepted

Solution code:
n = int(input())

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

ans = 10**10


for i in range(-100,101):
    temp = 0
    for x in li:
        temp += (x - i) ** 2
    if ans > temp:
        ans = temp
        
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

N = int(input())
a = list(map(float, input().split()))
a_sum = sum(a)
ave = round(a_sum / N)
b = list(map(lambda n : pow(n - ave, 2), a))
print(int(sum(b)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
a = int(input())
b = list(map(int, input().split()))
d = 10 ** 9
e = min(b)
f = max(b)
for i in range(e, f + 1):
    g = 0
    for j in b:
        g += abs(j - i) ** 2
    d = min(d, g)
print(d)

Submission status: Accepted

Solution code:
a = int(input())
b = list(map(int, input().split()))
b.sort()
c = (sum(b)) // len(b)
d = c + 1
mc = 0
md = 0
for i in b:
    mc += abs(i - c) ** 2
    md += abs(i - d) ** 2
print(min(mc, md))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
INF = 10**18

MAX = max(A)
MIN = min(A)

res = INF
for s in range(MIN,MAX+1):
    t = 0
    for i in range(N):
        t += (s - A[i])**2
    #print(s,t)
    if t < res:
        res = t
print(res)

Submission status: Accepted

Solution code:
n = int(input())
A = [int(i) for i in input().split()]
ans = float('inf')
for i in range(-100,101):
    tmp = 0
    for a in A:
        if a != i: tmp+=(a-i)**2
    ans = min(ans,tmp)
print(ans)

Submission status: Accepted

Solution code:
###template###
import sys
def input(): return sys.stdin.readline().rstrip()
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt, ceil, floor
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
import heapq
INF = float("inf")
MOD = 10**9 + 7
sys.setrecursionlimit(10**7)
# 4近傍（右, 下, 左, 上）
dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]
def inside(y: int, x: int, H: int, W: int) -> bool: return 0 <= y < H and 0 <= x < W
def mi(): return map(int, input().split())
def ii(): return int(input())
###template###

N = ii()
As = list(mi())
ave = round(sum(As) / N)

ans = 0
for a in As:
  ans += (ave - a) ** 2

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
INF=10**9+7
ans=INF
for i in range(min(l),max(l)+1):
    tmp=sum([(i-j)**2 for j in l])
    if tmp<ans:ans=tmp
print(ans)

Submission status: Accepted

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

min_cost = 10 ** 12
for n in range(-100, 100 + 1):
	cost = 0
	for a in A:
		cost += (n - a)** 2
	min_cost = min(min_cost, cost)
print(min_cost)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
num=list(map(int,input().split()))
ans=10**9
s,e=min(num),max(num)
for j in range(s,e+1):
  c=0
  for i in range(n):
    c+=(num[i]-j)**2
  if c<ans:
    ans=c
print(ans)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
def div_round(x, y): return (x + (y // 2)) // y

N = int(input())
*A, = map(int, input().split())
mean = div_round(sum(A), N)
ans = 0
for a in A:
    ans += (a - mean) ** 2
print(ans)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

tmp = sum(a)
ave1 = tmp // N
ave2 = ave1 + 1

dev1 = 0
dev2 = 0

for aa in a:
    dev1 += (aa - ave1)**2
    dev2 += (aa - ave2)**2

print(min(dev1,dev2))

Submission status: Accepted

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

u = round(sum(a) / N)

print(sum([(x-u)**2 for x in a]))

Submission status: Accepted

Solution code:
# ARC059C - いっしょ / Be Together (ABC043C)
def main():
    n = int(input())
    lst = list(map(int, input().rstrip().split()))
    ans = float("inf")
    MIN, MAX = min(lst), max(lst)

    for i in range(MIN, MAX + 1):
        cnt = 0
        for j in lst:
            cnt += (i - j) ** 2
        ans = min(ans, cnt)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

ans = float("inf")
for y in range(-100, 101):
    temp_y = 0
    for x in a:
        temp_y += (x - y) ** 2

    ans = min(ans, temp_y)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
ans = 10 ** 9
for i in range(min(A), max(A) + 1):
    cost = sum(list(map(lambda x: (x - i) ** 2, A)))
    ans = min(ans, cost)
print(ans)

Submission status: Accepted

Solution code:
# ARC059C - いっしょ / Be Together (ABC043C)
n = int(input())
lst = list(map(int, input().rstrip().split()))
ans = float("inf")
for i in range(-100, 101):
    cnt = 0
    for j in lst:
        cnt += (i - j) ** 2
    ans = min(ans, cnt)
print(ans)

Submission status: Accepted

Solution code:
# ARC059C - いっしょ / Be Together (ABC043C)
n = int(input())
lst = list(map(int, input().rstrip().split()))
ans = float("inf")
MIN, MAX = min(lst), max(lst)

if MIN == MAX:
    print(0)
else:
    for i in range(MIN, MAX + 1):
        cnt = 0
        for j in lst:
            cnt += (i - j) ** 2
        ans = min(ans, cnt)
    print(ans)

Submission status: Accepted

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

h = sum(a) // N

def xx(h):
  return sum(((h - ai)**2 for ai in a))
print(min(xx(h), xx(h+1)))

Submission status: Accepted

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

Submission status: Accepted

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

costs = []


o = int(round(sum(a)/N))
cost = 0
for j in range(N):
    cost += (o - a[j])**2

print(cost)

Submission status: Accepted

Solution code:
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 += (i - a[j]) ** 2
  ans= min(ans, tmp)
print(ans)

Submission status: Accepted

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

ans = 10 ** 7
for x in range(-200, 201):
    cost = 0
    for i in range(N):
        cost += (a[i] - x) ** 2
    ans = min(cost, ans)

print(ans)

Submission status: Accepted

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

mini = 10000000000000
for i in range(min(As) - 1, max(As) + 1):
    tmp = 0
    for a in As:
        tmp += (i - a) ** 2

    if tmp < mini:
        mini = tmp

print(mini)

Submission status: Accepted

Solution code:
N = int(input())
*a, = map(int, input().split())
ans = float("inf")
for i in range(min(a), max(a) + 1):
	tmp = 0
	for m in a:
		tmp += (i - m) ** 2
	ans = min(ans, tmp)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = [int(x) for x in input().split()]
min_a = min(a)
max_a = max(a)
ans = 10**10
for i in range(min_a,max_a+1):
    sum_c = 0
    for x in a:
        sum_c += (x-i)**2
    ans = min(ans,sum_c)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
ave = sum(a) // len(a)
ans1 = 0
ans2 = 0
for i in a:
  ans1 += (i - ave) ** 2
  ans2 += (i - ave - 1) ** 2
print(min(ans1, ans2))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools
import math
N = input()
l = list(map(int,input().split(" ")))
av = sum(l)/len(l)
can1 = int(math.ceil(av))
can2 = int(math.floor(av))
ans1 = sum(list(map(lambda x:(x-can1)**2,l)))
ans2 = sum(list(map(lambda x:(x-can2)**2,l)))
print(min(ans2,ans1))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split(" ")))
S = []

i = min(A)
j = max(A)
while i <= j:
    S.append(sum(map(lambda x:(x-i)**2,A)))
    i += 1

print(min(S))

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
mini = 10000000000000000000
for x in range(-100, 101):
    tmp = [(x - y) ** 2 for y in a]
    mini = min(mini, sum(tmp))
print(mini)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
average = round(sum(a)/len(a))
print(sum([(average - x) ** 2 for x in a]))

Submission status: Accepted

Solution code:
import numpy as e
n,*a=map(int,open(0).read().split())
a=e.array(a)
print(min([sum((a-i)**2) for i in range(-100,101)]))

Submission status: Accepted

Solution code:
n=int(input())
arr=list(map(int,input().split()))
ans=10**10
for i in range(-100,101):
  tmp=0
  for j in range(n):
    tmp+=(arr[j]-i)**2
  if tmp<ans:
    ans=tmp
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

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

def cost(np_values, a):
    """
    input:
        np_values = number list of input_values (N,)
        a = constant (1)
    
    output:
        cost = summation of the each L2 cost
    """
    
    cost = np.sum((np_values - a) ** 2)
    return cost
  
#test_values = np.array([1, 2, 3, 4])
#test_a = 3
#cost(test_values, test_a)

cost_list = []
for a in range(-100, 101):
    cost_list.append(cost(np_values, a))
print(min(cost_list))

Submission status: Accepted

Solution code:
from decimal import Decimal, ROUND_HALF_UP


N = int(input())
a = list(map(int, input().split(' ')))
result = sum(a) / N
r = Decimal(result).quantize(Decimal('0'), rounding=ROUND_HALF_UP)
cost = 0
for i in a:
    cost += (i - r) ** 2
print(cost)

Submission status: Accepted

Solution code:
input()
A=[int(i) for i in input().split()]
print(sum((a-round(sum(A)/len(A)))**2 for a in A))

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]

m = round(sum(A) / N)
print(sum((a - m) ** 2 for a in A))

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]

mi = 1e10
for m in range(-100, 101):
    mi = min(mi, sum((a - m) ** 2 for a in A))
print(mi)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]

m = sum(A) // N
print(min(sum((a - m) ** 2 for a in A), sum((a - m - 1) ** 2 for a in A)))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

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

cost = 100000001
for i in range(-100, 101):
    costMin = 0
    for j in nums:
        costMin += (i -j)**2
    if costMin < cost:
        cost = costMin        

print(cost)

Submission status: Accepted

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

min_cost = 10**15
for i in range (-100,101):
    cost = 0
    for j in range (len(a)):
        cost +=(a[j]-i)**2
    
    if min_cost >=cost:
        min_cost = cost

print(min_cost)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
mn = 1e9
for i in range(-100, 101):
  cost = 0
  for v in a: cost += (i - v) ** 2
  mn = min(mn, cost)
print(mn)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
ave = round(sum(A) / N)

ans = [(ave - a)**2 for a in A]

print(sum(ans))

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
mn = 1e9
for i in range(-100, 101):
  cost = 0
  for v in a:
    cost += (i-v) * (i-v)
  mn = min(mn, cost)
print(mn)

Submission status: Accepted

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

ans = int(1e8)
for i in range(a[0], a[-1]+1):
    sub = 0
    for j in a:
        sub += (j-i)**2
    ans = min(ans, sub)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
an = list(map(int, input().split()))
ave = sum(an)/N
rep = int(ave + 0.5) if ave > 0 else int(ave - 0.5)
ans = sum([(a - rep) ** 2 for a in an])
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = [int(i) for i in input().split()]
b = sum(a)//N
if sum(a)%N*2 >=  N:
    p = 1
else:
    p = 0
b =b+p
ans = 0
for i in range(N):
    ans += (a[i]-b)**2
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

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

a_sum = sum(a)

flag = True
sum2 = 0
for i in range(n):
    sum2 += a[i] ** 2
    if i >= 1:
        if a[i] != a[i-1]:
            flag = False

cand = []
for i in range(-100,101):
        cand.append(sum2 - 2 * i * a_sum + n * (i ** 2))

print(min(cand))

Submission status: Accepted

Solution code:
inf = 10**10
import sys
input = sys.stdin.readline

n = int(input())
A = list(map(int,input().split()))
ans = inf
for a in range(-100,101):
    cur = 0
    for i in range(n):
        cur += (A[i]-a)**2
    ans = min(ans,cur)
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
s=sum(a)//n
ans_1=0
ans_2=0
for i in range(n):
    ans_1+=(a[i]-s)**2
    ans_2+=(a[i]-s-1)**2
print(min(ans_1,ans_2))

Submission status: Accepted

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