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 heapq, math
from itertools import zip_longest, permutations, combinations, combinations_with_replacement
from itertools import accumulate, dropwhile, takewhile, groupby
from functools import lru_cache
from copy import deepcopy

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

ans = 1 << 28

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

print(ans)

Submission status: Accepted

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

ans =10**8
for i in range(-100,101):
    cost = 0
    for k in a:
        cost += (k-i)**2
    ans = min(cost,ans)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
ans = 1000000
a = sorted(a)
for i in range(a[0],a[n-1]+1):
    counter = 0
    for j in range(n):
        dif = abs(a[j] - i)
        dif *= dif
        counter += dif
    ans = min(ans,counter)
print(ans)

Submission status: Accepted

Solution code:
from statistics import mean 
import math
n=int(input())
arr=[int(x) for x in input().split()]
a=math.ceil(mean(arr))
b=math.floor(mean(arr))
s1=0
s2=0
for x in arr:
    s1=s1+((x-a)**2)
    s2=s2+((x-b)**2)
print(min(s1,s2))

Submission status: Accepted

Solution code:
n = int(input())
a_ls = list(map(int, input().split()))
small = sum(a_ls)//n
big = small + 1
ans = float('inf')
for goal in [big, small]:
    Sum = 0
    for a in a_ls:
        Sum += (goal - a) ** 2
    ans = min(ans, Sum)
print(ans)

Submission status: Accepted

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

ans = 10**7
for i in range(102):
    p = 0
    m = 0
    for j, aa in enumerate(a):
        p += (aa-i)**2
        m += (aa+i)**2
    ans = min(ans,p,m)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
arr = list(map(int,input().split()))
add = sum(arr)
if add>0:
	x = int(add/n + 0.5)
else:
	x = int(add/n - 0.5)

ans = 0
for i in range(len(arr)):
	ans += ((x-arr[i])*(x-arr[i]))
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
a = list(map(int, input().split()))
mean = int(sum(a) / n + np.sign(sum(a)) * 0.5)
#print(mean)
ans = 0
for i in range(n):
    ans += (a[i] - mean) ** 2

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
cmin = ((max(a) - min(a)) ** 2) * n
for x in range(min(a), max(a) + 1):
    c = 0
    for i in a:
        c += (i - x) ** 2
    cmin = min(c, cmin)
print(cmin)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())                                                
a_list = list(map(int, input().split()))
a_list.sort()
cost = 0
min_cost=0
 
for i in range(a_list[0], a_list[-1]):
    for j in range(len(a_list)):
        cost = cost + (a_list[j] - i)**2
 
    if i == a_list[0]:
        min_cost=cost
 
    min_cost = min(cost, min_cost)
    cost = 0
 
print(min_cost)

Submission status: Accepted

Solution code:
n=int(input())
a=[]
a=list(map(int,input().split()))
ave=sum(a)/n
if ave<int(ave)+0.5:
  ave=int(ave)
else:
  ave=int(ave)+1
ans=0
for i in range(n):
  ans+=(a[i]-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):
    tmp = 0
    for j in range(N):
        tmp += (A[j] - i)**2
    if ans > tmp:
        ans = tmp

print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
average = round(sum(A) / N)
ans = 0
for x in A: ans += (average-x)**2
print(ans)

Submission status: Accepted

Solution code:
from math import floor, ceil


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

x = sum(A) / n
if abs(floor(x) - x) < abs(ceil(x) - x):
    x = floor(x)
else:
    x = ceil(x)

answer = 0
for a in A:
    answer += (a - x)**2

print(answer)

Submission status: Accepted

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

def main():
    N = int(readline())
    A = list(map(int, readline().rstrip().split()))
    ans = 10 ** 7
    for i in range(min(A), max(A)+1):
        c = sum([(a-i)**2 for a in A])
        ans = min(ans, c)
    
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

m = sum(a) / len(a)
num = round(m)

loss = 0
for n in a:
	loss += (n - num)**2

print(loss)

Submission status: Accepted

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

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 a in A:
        cnt += (a - i) ** 2
    ans = min(ans, cnt)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
list_A = list(map(int, input().split()))
ans = pow(10, 10)
for num in range(-100, 101):
    list_B = list(map(lambda x:(x-num)**2, list_A))
    sum_value = sum(list_B)
    if ans > sum_value:
        ans = sum_value

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
a = list(map(int, input().split()))
n = len(a)
Mean1 = np.floor(sum(a) / n)
Mean2 = np.ceil(sum(a) / n)
sum1 = sum([(x-Mean1)**2 for x in a])
sum2 = sum([(x-Mean2)**2 for x in a])
print(int(min(sum1, sum2)))

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
a = list(map(int, input().split()))
n = len(a)
Mean1 = np.floor(sum(a) / n)
Mean2 = -(-sum(a) // n)
sum1 = sum([(x-Mean1)**2 for x in a])
sum2 = sum([(x-Mean2)**2 for x in a])
print(int(min(sum1, sum2)))

Submission status: Accepted

Solution code:
n=int(input())
lis=list(map(int,input().split()))
ave=0
ans=0
for i in lis:
  ave+=i
ave/=n
if ave>=int(ave)+0.5:
  ave=int(ave)+1
else:
  ave=int(ave)
for i in lis:
  ans+=(i-ave)**2
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
ma=max(a)
mi=min(a)
ans=10**9

for i in range(mi,ma+1):
    tm=0
    for j in a:
        tm+=(j-i)**2
    else:
        ans=min(ans,tm)

print(ans)

Submission status: Accepted

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

R = []

for i in range(min(A), max(A)):
    cost = 0
    for j in range(len(A)):
        cost += (i - A[j])**2
    R.append(cost)

if R == []:
    print(0)
else:
    print(min(R))

Submission status: Accepted

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

mina = min(Alist)
maxa = max(Alist)
costlist =list()

for i in range(mina,maxa+1):
    res = 0
    for j in Alist:
        temp = (j-i)**2
        res = res + temp
    costlist.append(res)

res = min(costlist)
print(res)

Submission status: Accepted

Solution code:
def main():
  N = int(input())
  A = [int(_) for _ in input().split()]
  ret = 10 ** 10
  for tar in range(-100, 101):
    cc = 0
    for a in A:
      cc += (tar - a) ** 2
    ret = min(ret, cc)
  print(ret)
  return

if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
import math

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

X = round(sum(a) / n)

P = 0

for i in range(n):
    P += (X - a[i])**2

print(P)

Submission status: Accepted

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

start = min(a_L)
end = max(a_L)

ans = float("inf")
for i in range(start,end+1):
    cost = 0
    for val in a_L:
        cost += (val-i)**2
    
    ans = min(ans,cost)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int,input().split()))
a = sum(l)//n
b = a+1
s = 0
m = 0
for i in l:
  s += (i-a)**2
for j in l:
  m += (j-b)**2
print(min(s,m))

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
mincost = 10000000000000000000
for i in range(-100, 101):
    cost = 0
    for j in a:
        cost += (j - i) * (j - i)
    if cost < mincost:
        mincost = cost
print(mincost)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
mean = sum(a) / len(a)
#print(mean)
tar = round(mean)
#print(tar)
score = 0
for x in a:
    score += (x-tar)*(x-tar)
print(score)

Submission status: Accepted

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

asummin=40000*100
for i in range(-100,101,1):
    asum=0
    for ii in range(n):
        asum+=(a[ii]-i)**2
    asummin=min(asummin,asum)
print(asummin)

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):
    x = 0
    for j in range(N):
        x += (i-A[j])**2
    ans = min(ans, x)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
data=int(input())
nim=list(map(int,input().split()))
mike=2000000000000
for i in range(-100,101):
  counter=0
  for j in nim:
    counter+=(i-j)*(i-j)
  if counter<mike:
    mike=counter
print(mike)

Submission status: Accepted

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

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

Submission status: Accepted

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

print(min([sum([(a-i)**2 for a in A]) for i in range(-100, 101)]))

Submission status: Accepted

Solution code:
def solve(N, vals):
    result = float("inf")
    for i in range(-100, 101):
        curCost = 0
        for val in vals:
            curCost += (i - val) ** 2
        result = min(result, curCost)
    print(result)


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

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n = ni()
a = na()

ans = 1000000000000

for y in range(-100,101):
    c = 0
    for i in a:
        c += (y-i)**2
    ans = min(c,ans)

print(ans)

Submission status: Accepted

Solution code:
import math
n, *a = map(int, open(0).read().split())

y1 = math.ceil(sum(a)/n)
y2 = sum(a)//n
ans1 = 0
ans2 = 0

for i in range(n):
    ans1 += (a[i] - y1) ** 2
    ans2 += (a[i] - y2) ** 2

print(min(ans1,ans2))

Submission status: Accepted

Solution code:
import math
n = int(input())
a = list(map(int, input().rstrip().split()))
s = sum(a)
t1 = 0
t2 = 0
mean1 = s // n
mean2 = mean1 + 1
for x in a:
    t1 += math.pow(abs(x - mean1), 2)
    t2 += math.pow(abs(x - mean2), 2)
print(int(min(t1, t2)))

Submission status: Accepted

Solution code:
def resolve():
	n = int(input())
	a = list(map(int, input().split()))
	ans = 10000000
	for i in range(-100, 101):
		s = 0
		for j in a:
			s += (i-j)**2
		if ans > s:
			ans = s
	print(ans)
resolve()

Submission status: Accepted

Solution code:
from decimal import Decimal, ROUND_HALF_UP
n = int(input())
x = list(map(int, input().split()))
num = Decimal(str(sum(x)/n)).quantize(Decimal('0'), rounding=ROUND_HALF_UP)
num = int(num)

cost=0
for i in range(n):
    cost += (x[i]-num)*(x[i]-num)
print(cost)

Submission status: Accepted

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

ans=10**9

def sum(x):
    r=0
    for i in range(0,N):
        r+=(A[i]-x)**2
    return r

for j in range(-100,101):
    if sum(j)<=ans:
        ans=sum(j)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int,input().split()))
ans = 10**9
b = [i for i in range(-100,101)]
for i in range(len(b)):
    ansa = 0
    for j in range(N):
        ansa += (b[i]-a[j])**2
    ans = min(ans,ansa)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
c = float("inf")
for m in range(sum(A)//N-1,sum(A)//N+2):
    c = min(c, sum(map(lambda a: (a-m)**2, A)))
print(c)

Submission status: Accepted

Solution code:
def resolve():
	n = int(input())
	a = list(map(int, input().split()))
	ans = 100**6
	for i in range(-100, 101):
		tmp = 0
		for j in a:
			if i == j:
				continue
			tmp += (j - i)**2
		if tmp < ans:
			ans = tmp
	print(ans)
resolve()

Submission status: Accepted

Solution code:
def resolve():
	n = int(input())
	ans = 1000000
	a = list(map(int, input().split()))
	for i in range(-100, 101):
		tmp = 0

		for j in a:
			tmp += (j-i)**2
		if tmp < ans:
			ans = tmp
	print(ans)

resolve()

Submission status: Accepted

Solution code:
import math
n= int(input()) 
x = list(map(int, input().split()))
y = sum(x)*1.0/n
z =round(y)

print(sum([(i-z)**2 for i in x]))

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby
    #from itertools import product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

    n = int(input())
    a = list(map(int, input().split()))
    res = 10**20

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def resolve():
    _ = input()
    A = [int(i) for i in input().split()]
    print(min([sum([(i - a) ** 2 for a in A]) for i in range(min(A), max(A) + 1)]))


if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
n = int(input())
ns = list(map(int, input().split()))
res = float('inf')
for i in range(-100, 101):
    res = min(res, sum((i-j)**2 for j in ns))
print(res)

Submission status: Accepted

Solution code:
def solve(s):
    sum1 = 0
    sum2 = 0
    length = 0
    for i in s.split():
        x = int(i)
        sum1 += x**2
        sum2 += x
        length += 1

    score = 10**8

    for y in range(100,-101, -1):
        tmp = sum1 - sum2*2*y + length*(y**2)
        score = min(tmp, score)

    return score

if __name__ == "__main__":
    n = int(input())
    s = input()
    print(solve(s))

Submission status: Accepted

Solution code:

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

amin=min(a)
amax=max(a)

smin=(amax**2)*n
for y in range(amin,amax+1):
    ssum=0
    for i in range(n):
        ssum=ssum+(a[i]-y)**2
    smin=min(smin,ssum)

print(smin)

Submission status: Accepted

Solution code:
import sys

n, *a = map(int, sys.stdin.read().split())

def main():
  x = round(sum(a) / n)
  s = 0
  for i in range(n):
    s += pow(x - a[i], 2)
  print(s)
  
if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:

def solve(n, arr):
    s = sum(arr)

    k = int(round(s / len(arr), 0))

    cost = 0

    for num in arr:
        cost += (k-num) ** 2

    return cost


n = int(input().strip())
arr = list(map(int, input().strip().split()))

print(solve(n, arr))

Submission status: Accepted

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

ave = sum(a)/N

if ave - int(ave) >= 0.5:
    ave = int(ave)+1
else:
    ave =int(ave)
    
a = [(a[i]-ave)**2 for i in range(N)]

print(sum(a))

Submission status: Accepted

Solution code:
class atcorder:
    N=int(input())
    add = list(map(int,input().split()))
    total=0
    for i in range(N):
        total+=add[i]
    y=total/N
    
    i=0
    total=0
    for i in range(N):
        
        total+=(add[i]-round(y))**2
        
    print(int(total))

Submission status: Accepted

Solution code:
n=int(input())

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

t.sort()

ans = 100000000000000

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

Submission status: Accepted

Solution code:
#
# Written by NoKnowledgeGG @YlePhan
# ('ω')
#
#import math
#mod = 10**9+7
#import itertools
#import fractions
#import numpy as np
#mod = 10**4 + 7
"""def kiri(n,m):
  r_ = n / m
  if (r_ - (n // m)) > 0:
    return (n//m) + 1
  else:
    return (n//m)"""

""" n! mod m 階乗
mod = 1e9 + 7
N = 10000000
fac = [0] * N
def ini():
  fac[0] = 1 % mod
  for i in range(1,N):
    fac[i] = fac[i-1] * i % mod"""

"""mod = 1e9+7
N = 10000000
pw = [0] * N
def ini(c):
  pw[0] = 1 % mod
  for i in range(1,N):
    pw[i] = pw[i-1] * c % mod"""

"""
def YEILD():
  yield 'one'
  yield 'two'
  yield 'three'
generator = YEILD()
print(next(generator))
print(next(generator))
print(next(generator))
"""
"""def gcd_(a,b):
  if b == 0:#結局はc,0の最大公約数はcなのに
    return a
  return gcd_(a,a % b) # a = p * b + q"""
"""def extgcd(a,b,x,y):
  d = a
  if b!=0:
    d = extgcd(b,a%b,y,x)
    y -= (a//b) * x
    print(x,y)
  else:
    x = 1
    y = 0
  return d"""


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

def main():
  n = int(input())
  A = readInts()
  
  SPLIT = round(sum(A)/n)
  
  ANS = 0
  #print(SPLIT)
  for i in range(n):
    ANS += abs(SPLIT-A[i])**2
  print(ANS)
  
if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)


############ ---- Input Functions ---- ############
def in_int():
    return (int(input()))


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


def in_str():
    s = input()
    return (list(s[:len(s) - 1]))


def in_ints():
    return (map(int, input().split()))


n = in_int()

a = in_list()

av=  sum(a)//n

ans  = 0

p = 0
for xx in a:
    p += (xx-av)*(xx-av)

ans = p


p = 0
for xx in a:
    p += (xx-av-1)*(xx-av-1)


ans = min(ans, p)

print(ans)

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())

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

a_array = np.array(a)
a_min = np.min(a_array)
a_max = np.max(a_array)
a_med = np.median(a_array)

# print(a_min, a_max, a_med)

sum_array = np.zeros((3, n), dtype = np.int)
# print(sum_array)

opt = False
target = int(a_med)
while not opt:
  sum_array[0, :] = (a_array[:] - (target-1)) ** 2
  sum_array[1, :] = (a_array[:] - (target)) ** 2
  sum_array[2, :] = (a_array[:] - (target+1)) ** 2
  sum = np.sum(sum_array, axis = 1)
  if sum[1] < sum[0] and sum[1] < sum[2]:
    opt = True
  elif sum[0] < sum[1]:
    target -= 1
  else:
    target += 1
  
print(sum[1])

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
work = sum(a)/n
work = int(work) if work-int(work)<0.5 else int(work)+1
result = 0
for el in a:
    result += (work-el)**2
print(result)

Submission status: Accepted

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

Submission status: Accepted

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

res = float("inf")
for x in range(min(A), max(A) + 1):
    cost = 0
    for j in range(n):
        y = A[j]
        cost += pow(x - y, 2)
    res = min(res, cost)
print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
ans = 10**6
for i in range(-105,106):
    s = 0
    for j in range(n):
        s += (a[j]-i)**2
    ans = min(ans,s)
print(ans)

Submission status: Accepted

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

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

min_cost = 0
for each in A:
    min_cost += (each-target)**2
print(min_cost)

Submission status: Accepted

Solution code:
import math
import numpy as np

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

def solve(lst):
  avg = math.floor(np.mean(lst))
  ans = min(sum([cost(i, avg) for i in lst]), sum([cost(i, avg+1) for i in lst]))
  return ans


if __name__ == "__main__":
  n = int(input())
  a_list = [int(i) for i in input().split()]
  print(int(solve(a_list)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
min_ = 10 ** 9
for i in range(-100, 101):
    scr = 0
    for j in range(N):
        scr += (i - a[j])**2
    #min_ = min(min_, scr)
    if min_ > scr:
        min_ = scr
print(min_)

Submission status: Accepted

Solution code:
# python 3.4.3
import sys
input = sys.stdin.readline

# -------------------------------------------------------------
# library
# -------------------------------------------------------------



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

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

print(Ans)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int,input().split()))
ave1 = sum(A)//len(A)
ave2 = ave1+1
Ans1 = 0
Ans2 = 0
for i in range(n):
  Ans1 += (A[i] - ave1)**2
  Ans2 += (A[i] - ave2)**2
Answer = min(Ans1,Ans2)
print(Answer)

Submission status: Accepted

Solution code:
n = int(input())
*L, =map(int, input().split())
x = 0
ans = 0
min_L = min(L)
max_L = max(L)
for i in range(min_L,max_L):
    x = sum([((L[j])-i)**2 for j in range(len(L))])
    if i == min(L):
        ans = x
    if x - ans < 0:
        ans = x
print(ans)

Submission status: Accepted

Solution code:
import sys
n = int(input())
a_ls = [int(i) for i in sys.stdin.readline().split()]
best = 10**18
for i in range(-100,101):
    score = sum([(i-a)**2 for a in a_ls])
    best = min(score, best)
print(best)

Submission status: Accepted

Solution code:
url = "https://atcoder.jp/contests/abc043/tasks/arc059_a"

def main():

    all = int(input())
    num_li = list(map(int, input().split()))

    n = sum(num_li) // all + 1
    end = min(num_li)
    ans = 10000000
    for i in range(n, end - 1, -1):
        tmp = 0
        for v in num_li:
            if v == i:
                continue
            else:
                tmp += keisan(v, i)

        if tmp < ans:
            ans = tmp
    print(ans)

def keisan(v, x):
    return (v - x) ** 2

if __name__ == '__main__':
    main()

Submission status: Accepted

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

for i in range(-100,101):
    count = 0
    for j in range(n):
        if a[j] != i:
            tmp = (a[j] - i)**2
            count += tmp
    if count < mn:
        mn = count

print(mn)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
L = list(map(int, input().split()))
L2 = np.square(L)

A = N
B = sum(L) * 2
C = sum(L2)

lim = round(B / A / 2)

l = list(map(lambda x: x - lim, L))
l2 = list(map(lambda x: x ** 2, l))

print(sum(l2))

Submission status: Accepted

Solution code:
n=int(input())
a=sorted(map(int,input().split()))
m=1000000
for i in range(a[0],a[-1]+1):
    s=sum(map(lambda j:(j-i)**2,a))
    if m>s:m=s
print(m)

Submission status: Accepted

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

Submission status: Accepted

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

cost=[]
for t in range(-100, 101):
    cost.append(sum([(val-t)**2 for val in a]))
print(min(cost))

Submission status: Accepted

Solution code:
N = int(input())
l = [int(x) for x in input().split()]
cost = 1000000000000
for i in range(-100,101):
  co = 0
  for j in range(N):
    co += (i - l[j]) ** 2
  if cost > co:
    cost = co
print(cost)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
ans=1000000000
for i in range(-100,101):
	ss=0
	for j in A:
		ss+=(j-i)*(j-i)
	ans=min(ans, ss)
print(ans)

Submission status: Accepted

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

from statistics import mean
c_n =mean(A_s)
c_n = round(c_n)
cost = 0
for i in range(N):
    cost+=(A_s[i]-c_n)**2
print(cost)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int, input().split()))
m=99999999999999
for i in range(min(a),max(a)+1):
    k=0
    for j in a:
        k=k+(j-i)**2
    if k<m:
        m=k
print(m)

Submission status: Accepted

Solution code:
n = int(input())
numbers = list(map(int, input().split()))
min_num = min(numbers)
max_num = max(numbers)
cost = -1
for i in range(min_num, max_num):
    temp = 0
    for number in numbers:
        temp += (number - i) ** 2
    
    if cost == -1 or temp < cost:
        cost = temp
if cost == -1:
    cost = 0
print(cost)

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc043/tasks/arc059_a
n = int(input())
a = list(map(int, input().split()))
min = float('inf')
for y in range(-100, 101):
    sum = 0
    for x in a:
        sum += (x - y) ** 2
    if min > sum:
        min = sum

print(min)

Submission status: Accepted

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

num = round(sum(A) / len(A))
cost = [(a - num)**2 for a in A]

print(sum(cost))

Submission status: Accepted

Solution code:
import numpy as np
from sys import maxsize

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

res = maxsize
for i in range(a.min(), a.max()+1):
    val = np.sum((a-i)**2)
    if val < res:
        res = val

print(res)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

n = int(input())
numeros = [int(x) for x in input().split()]
saida = sys.maxsize

for i in range(min(numeros), max(numeros)+1):
	valor = 0
	for j in numeros:
		valor += (j-i)**2
	
	saida = min(valor, saida)

print(saida)

Submission status: Accepted

Solution code:
from math import ceil

n = int(input())
numeros = [int(x) for x in input().split()]
saida = 2000000000000000

for i in range(min(numeros), max(numeros)+1):
	valor = 0
	for j in numeros:
		valor += (j-i)**2
	
	saida = min(valor, saida)

print(saida)

Submission status: Accepted

Solution code:
a = int(input())

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

c = round(sum(b)/len(b))

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

print(ans)

Submission status: Accepted

Solution code:
import sys
 
_input1 = int(input())
_input2 = [int(s) for s in input().split()]
_output = sys.maxsize
 
for i in range(min(_input2),max(_input2)+1):
    score = 0
    for j in _input2:
        score += (j-i)**2
 
    if score < _output:
        _output = score
 
print(_output)

Submission status: Accepted

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

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

print(all_cost)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**8)
INF = float("inf")


def solve(N: int, a: "List[int]"):

    target = int(round(sum(a)/N))

    s = 0
    for aa in a:
        s += (target-aa)**2
    print(s)

    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:
import sys

_input1 = int(input())
_input2 = [int(s) for s in input().split()]
_output = sys.maxsize

for i in range(min(_input2),max(_input2)+1):
    score = 0
    for j in _input2:
        score += (j-i)**2

    if score < _output:
        _output = score

print(_output)

Submission status: Accepted

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

def rewrite(n,l):
    cost=0
    for i in l:
        cost += (i-n)**2
    return cost

r1=rewrite(int(sum(a)/len(a)),a)
r2=rewrite(int(sum(a)/len(a))+1,a)

print(r1 if r1<r2 else r2)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=10**9
ab=0
for i in range(-100,101):
    count=0
    for j in range(n):
        count+=abs(a[j]-i)**2
    if ans>count:
        ans=count
        ab=i
print(ans)

Submission status: Accepted

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

mean = round(sum(a)/len(a))
ans = 0
for i in range(N):
    ans += (a[i]-mean)**2

print(ans)