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:
n=int(input())
a=list(map(int,input().split()))
avg=sum(a)//n
ans=10**18
for i in (avg-1,avg,avg+1):
  ansc=0
  for j in a:
    ansc+=(j-i)**2
  ans=min(ans,ansc)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
ans = (200**2)*N

for i in range(-100, 101):
    tmp = 0
    for b in a:
        tmp += (b-i)*(b-i)
    if tmp < ans:
        ans = tmp

print(ans)

Submission status: Accepted

Solution code:
def two_int():
    N, K = map(int, input().split())
    return N,K

def one_int():
    return int(input())

def one_str():
    return input()

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

N=one_int()
X=many_int()

sort_X=sorted(X)
min_X = sort_X[0]
max_X = sort_X[-1]

min_num = 0
min_sum = 99999999999
for i in range(min_X, max_X+1):
    temp_sum = 0
    for j in X:
        temp_sum += (j-i)**2
    
    if temp_sum < min_sum:
        min_sum = temp_sum
        min_num = i

print(min_sum)

Submission status: Accepted

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

mid = sum(a)//len(a)
ans1 = 0
ans2 = 0

for i in a:
  ans1 += pow(abs(i - mid),2)
  ans2 += pow(abs(i - (mid+1)),2)
print(min(ans1, ans2))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
average = round(sum(a) / len(a))
costs = 0

for i in a:
    costs += (i - average)**2

print(costs)

Submission status: Accepted

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

n = round(sum(a)/N)

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

print(int(ans))

Submission status: Accepted

Solution code:
import sys

# N個の整数
N = int(input())

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

ans = 10**18
# 最小値から最大値の間の値のどれに書き換えるかをループで調べる
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())
a=list(map(int,input().split()))
ans=10**18
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())
a=list(map(int,input().split()))
y=round(sum(a)/n)
ans=n*y*y-2*y*sum(a)
for x in a:
  ans+=x*x
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

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

mx = max(A)
mn = min(A)

R = -1
for i in range(mn, mx + 1):
    s = 0
    for a in A:
        s += (i - a)** 2
    R = s if R < 0 or s < R else R

print(R)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int,input().split()))
ave = sum(a) / len(a)
ave = round(ave)
#print(ave)
ans = 0
for val in a:
    ans += (val - ave) ** 2
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):
    cost = 0
    for a in A:
        cost += (i-a)**2
    ans = min(ans, cost)

print(ans)

Submission status: Accepted

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

min_cost = 10 ** 10

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

print(min_cost)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

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


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


tmp = sum(A) // N
print(min(
    sum(map(lambda x: f(x, tmp), A)),
    sum(map(lambda x: f(x, tmp + 1), A))
)
)

Submission status: Accepted

Solution code:
N = int(input())
arr = list(map(int, input().split()))
ans = 10**10
for y in range(-100,101):
    tmp = 0
    for x in arr:
        tmp += (x-y)**2
    if ans > tmp:
        ans = tmp
print(ans)

Submission status: Accepted

Solution code:
a=int(input())
b=input().rstrip().split()
l=[]
SUM = 0
for i in range(a):
    l.append(int(b[i]))
    SUM+=int(b[i])
ave=SUM//a
ave1=SUM//a+1

def keisan(ave,l):
    y=0
    for i in range(len(l)):
        y+=(int(l[i])-ave)**2
    return y

if (keisan(ave,l)<keisan(ave1,l)):
    print(keisan(ave,l))
else:
    print(keisan(ave1,l))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin
import math

n = int(stdin.readline().rstrip())
li = list(map(int,stdin.readline().rstrip().split()))

ave = math.ceil(sum(li)/n)
ave2 = sum(li)//n

point = 0
point2 = 0

for i in li:
    point += (ave-i)**2
    point2 += (ave2-i)**2

print(min(point,point2))

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int,input().split()))
med = sum(A)//n
ans = float('inf')
for m in range(med-1,med+2):
    res = 0
    for a in A:
        res += abs(a-m)**2
    ans = min(ans,res)
print(ans)

Submission status: Accepted

Solution code:
def calcCost(ave_num, num):
    cost = (num - ave_num) ** 2
    return cost
 
N = int(input())
numbers = [int(i) for i in input().split()]
 
if len(set(numbers)) == 1:
    print(0)
else :
    
    ave_num = round(sum(numbers) / N)
 
    total_cost = 0
    for num in numbers:
        total_cost += calcCost(num, ave_num)
    
    print(total_cost)

Submission status: Accepted

Solution code:
def calcCost(ave_num, num):
    cost = (num - ave_num) ** 2
    return cost

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

if len(set(numbers)) == 1:
    print(0)
else :
    
    ave_num = round(sum(numbers) / N)

    total_cost = 0
    for num in numbers:
        total_cost += calcCost(num, ave_num)
    
    print(total_cost)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int,input().split(' ')))
ans = []
for i in range(-100,101):
    cost = 0
    for j in a:
        cost += (j-i)*(j-i)
    ans.append(cost)

print(min(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
ans=10**9+1
for i in (0,1):
  val=0
  for a in A:
    val+=(a-int(sum(A)/len(A))-i)**2
  ans=min(ans,val)
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
st=int(sum(A)/len(A))
go=st+1
ans=10**9+1
for i in range(st,go+1):
  val=0
  for a in A:
    val+=(a-i)**2
  ans=min(ans,val)
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
st=min(A)
go=max(A)
ans=10**9+1
for i in range(st,go+1):
  val=0
  for a in A:
    val+=(a-i)**2
  ans=min(ans,val)
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n=int(input())
    A=list(map(int,input().split()))

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

Submission status: Accepted

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

p = min(a)
q = max(a)

res = 4000000
for x in range(p, q+1):
    tmp = 0
    for y in a:
        tmp += (x - y) ** 2
    res = min(res, tmp)

print(res)

Submission status: Accepted

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

avg = round(sum(nums) / len(nums))
cost = 0
for x in nums:
    if x == avg:
        continue
    cost += (x - avg)**2

print(cost)

Submission status: Accepted

Solution code:
import math

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

avg_ceil = math.ceil(sum(nums) / len(nums))
avg_floor = math.floor(sum(nums) / len(nums))

cost_ceil = 0
cost_floor = 0

for x in nums:
    if x == avg_ceil:
        continue
    cost_ceil += (x - avg_ceil)**2

for x in nums:
    if x == avg_floor:
        continue
    cost_floor += (x - avg_floor)**2

if cost_ceil <= cost_floor:
    print(cost_ceil)
else:
    print(cost_floor)

Submission status: Accepted

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

y = int(round(float(sum(a)) / N))

print sum((x - y) ** 2 for x in a)

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-i)**2 for a in A]))

print(mi)

Submission status: Accepted

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

ans = sum([i**2 for i in a])

for i in range(-100, 100+1):
    s = sum([(j-i)**2 for j in a])
    if ans > s:
        ans = s

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
sumA = sum(A)
t = sumA//n
p, q = 0, 0
for i in range(n):
  p += (t-A[i])**2
  q += (t+1-A[i])**2
print(min(p, q))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
import math
mean = sum(a) / n
smaller = math.floor(mean)
larger = math.ceil(mean)
a_smaller = [(i - smaller) ** 2 for i in a]
a_larger = [(i - larger) ** 2 for i in a]
if sum(a_smaller) <= sum(a_larger) :
    print(sum(a_smaller))
elif sum(a_smaller) > sum(a_larger) :
    print(sum(a_larger))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
#str 注意
def main():
    n = int(input())
    ls = [int(x) for x in input().split()]
    ans = 10**10
    for i in range(-100,101):
        can = 0
        for j in range(n):
            can += (ls[j]-i)**2
        if can<=ans:
            ans = can
    
    print(ans)
if __name__=='__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = [int(i) for i in input().split()]
s = int()
cost = 1000000

for i in range(-100,101):
    for t in range(N):
        s += (a[t] - i)*(a[t] - i)
    if  s < cost:
        cost = s
    s =int(0)

print(cost)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys, math, itertools, heapq, collections, bisect, fractions
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
sys.setrecursionlimit(10**8)
inf = float('inf')
ans = inf
count = 0

n=int(input())
A=list(map(int,input().split()))
for num in range(-100,101):
    tmp=0
    for ai in A:
        tmp+=(num-ai)**2
    ans=min(ans,tmp)
print(ans)

Submission status: Accepted

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

ans = 10 ** 10

for i in range(-100, 101):
    x = 0
    for a in A:
        x += (a-i)**2
    
    if x < ans:
        ans = x

print(ans)

Submission status: Accepted

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

s = 200**2 * 100
for x in range(min(A), max(A)+1):
    s = min(s, sum((x-a)**2 for a in A))
print(s)

Submission status: Accepted

Solution code:
import math

N = int(input())
a = [int(i) for i in input().split()]
b = round(sum(a)/N)

c = sum([(i - b)**2 for i in a])
print(c)

Submission status: Accepted

Solution code:
import math

def solve():
    N = int(input())
    A = list(map(int, input().split()))
    m = sum(A)/N
    ceil_eval = sum((a-math.ceil(m))**2 for a in A)
    floor_eval = sum((a-math.floor(m))**2 for a in A)
    ret = min(ceil_eval, floor_eval)
    print(ret)
    
solve()

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
mincost=float('inf')
mina = min(A)
_ = max(A) - mina

for i in range(mina,mina+_+1):
    cost = 0
    for a in A:
        cost += (a-i)**2
    if cost < mincost:
        mincost = cost
print(mincost)

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)/len(a))
cost = 0
for i in range(len(a)):
    cost += (a[i] - ave)**2
    
print(cost)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
a = [int(i) for i in input().split()]
oppai = np.array(a)

max = np.max(oppai)
min = np.min(oppai)
x = (max+min)//2
sum = [0] * 201

for i in range(-100,101):
    oppai = np.array(a) #oppaiの初期化
    oppai -= i
    oppai = oppai**2
    sum[i+100] = np.sum(oppai)
    #print('sum[{}]'.format(i+100),sum[i+100])

chinko = np.array(sum)
#print(np.argmin(chinko)+x-1)
print(np.min(chinko))

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = list(int(x) for x in input().split())
    ans = float('inf')


    for i in range(-100,100+1):
        ans_tmp = 0
        for a in A:
            ans_tmp += (a-i)**2

        if ans_tmp < ans:
            ans = ans_tmp

    print(int(ans))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def calc_cost(val):
  return sum([(a-val)**2 for a in A])

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

print(min([calc_cost(num) for num in range(min(A), max(A)+1)]))

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
r=4*10**6
for i in range(min(A),max(A)+1):
	d=0
	for j in range(n):
		d+=(A[j]-i)**2
	r=min(r,d)
print(r)

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
m=sum(A)//n
print(min(sum(map(lambda x:(x-m)**2,A)),sum(map(lambda x:(x-m-1)**2,A))))

Submission status: Accepted

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

ans=10**9
x = -100
while(x < 101):
    ans = min(ans, sum(map(lambda a: (x-a)**2, A)))
    x+=1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
ma=max(a)
mi=min(a)
ans=[]
for i in range(mi,ma+1):
  l=0
  for s in a:
    l+=(i-s)**2
  ans.append(l)  
print(min(ans))

Submission status: Accepted

Solution code:
N = int(input())
A =[int(x) for x in input().split()]
result=float('inf')
for i in range(min(A),max(A)+1):
  s=0
  for j in range(N):
    s += (i-A[j])**2
  result=min(result,s)
print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
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)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

    print(ans)



if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

    # 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)
    ans = 0
    for ai in A:
        x = (avg - ai) ** 2
        ans += x

    print(ans)



if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# python3 (3.4.3)
import sys
input = sys.stdin.readline

# main
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)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
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


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, a)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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


main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
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))  # type: int
    a = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, a)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:

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)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
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)

Submission status: Accepted

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

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

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

# print(n)
# print(a)
# print(mean)

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

print(cost)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# ABC043 C - Be Together
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)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
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)

Submission status: Accepted

Solution code:
# C - いっしょ / Be Together

# 平均値は平方誤差を最小化する

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)

Submission status: Accepted

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