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()))

solver = []
for M in range(-100,100+1):
  cost = 0
  for i in range(N):
    cost += (A[i] - M) ** 2
  solver.append(cost)
  
print(min(solver))

Submission status: Accepted

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

sol = 10 ** 15
for i in range(-100, 101):
	tmp = 0
	for j in range(n):
		tmp += (num[j] - i) ** 2  
	sol = min(sol, tmp)
print(int(sol))

Submission status: Accepted

Solution code:
import sys

N = int(sys.stdin.readline())
a = list(map(int, sys.stdin.readline().split()))


min_val = -100
for i in range(-100, 101):
    sum_calc = 0
    for j in range(N):
        sum_calc += (a[j] - i)*(a[j] - i)
    if i == -100:
        min_val = i
        min_sum_calc = sum_calc
    if min_sum_calc > sum_calc:
        min_val = i
        min_sum_calc = sum_calc

print(min_sum_calc)

Submission status: Accepted

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

x_1 = sum(a) // n
x_2 = x_1 + 1

cost_1 = 0
cost_2 = 0

for _a in a:
  cost_1 += (x_1 - _a) ** 2
  cost_2 += (x_2 - _a) ** 2

print(min(cost_1, cost_2))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
min = 5000000000000000
for i in range(-100, 101):
	sum = 0
	for j in range(0, N):
		sum += (i-A[j])*(i-A[j])
	if sum < min: min = sum
print(min)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = list(map(int,input().split(' ')))
    cost = 100**2*100
    for i in range(-100,101):
        tmp = 0
        for j in a:
            if tmp >= cost:
                break
            tmp += (j-i)**2
        if cost > tmp:
            cost = tmp

    print(cost)

main()

Submission status: Accepted

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

_mean1 = sum(a)//n
_mean2 = _mean1+1

_sum1=0
_sum2=0

for i in a:
    _sum1+=(i - _mean1)**2
    _sum2+=(i-_mean2)**2
    
print(min(_sum1, _sum2))

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)
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:
# -*- coding: utf-8 -*-
n = int(input())
a = list(map(int,input().split()))

#最小値と最大値の間の値になる。
min_a = min(a)
max_a = max(a)

cost = []

i = min_a
while i <= max_a:
    cost.append(sum(map(lambda x:(x-i)**2,a)))
    i+=1
print(min(cost))

Submission status: Accepted

Solution code:
n = int(input())

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

x = round(sum(arr) / n)

print(sum((x - i)**2 for i in arr))

Submission status: Accepted

Solution code:
# まさかの
# 問題文をちゃんと読もう 文ってか制約
n = int(input())
arr = list(map(int, input().split()))
ans = 10e9
for t in range(-100, 101):
    ans_tmp = 0
    for v in arr:
        ans_tmp += abs(t - v)**2
    ans = min(ans, ans_tmp)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
mx, mn = max(a), min(a)
ans = float('inf')
for i in range(mn,mx+1):
    ret = 0
    for j in a:
        ret += (j-i)**2
    ans = min(ans,ret)
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import math
from decimal import Decimal as dec


N = int(input())
An = list(map(int, input().split()))
x = math.floor(dec(sum(An))/dec(N))

cost = [0,0]
for a in An:
  cost[0] += (x-a)**2
  cost[1] += (x+1-a)**2
print(min(cost))

Submission status: Accepted

Solution code:
import math
import fractions
import copy
#以下てんぷら
def j(q):
    if q==1: print("YES")
    elif q == 0:print("NO")
    exit(0)
rem = pow(10,9)+7
"""
def ct(x,y):
    if (x>y):print("+")
    elif (x<y): print("-")
    else: print("?")
"""

def ip():
    return int(input())
def printrow(a):
    for i in a:
        print(i)
def combinations(n,r):
    if n<r:return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
def permutations(n,r):
    if n<r:return 0
    return math.factorial(n) // math.factorial(n - r)
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)


n = ip()                                     #入力整数1つ
#right,up,n= (int(i) for i in input().split())       #入力整数横2つ以上
a = [int(i) for i in input().split()]        #入力整数配列
#a = input()                                  #入力文字列
#a = input().split()                          #入力文字配列
#n = ip()                      #入力セット(整数改行あり)(1/2)
#a=[ip() for i in range(n)]    #入力セット(整数改行あり)(2/2)
#a=[input() for i in range(n)]    #入力セット(整数改行あり)(2/2)
#jの変数はしようできないので注意
#全足しにsum変数使用はsum関数使用できないので注意
#a.pop(0)は10^5あるときTLEしやすい!注意!抜くなら逆ソート!
#これを消すとなぜかWAになる呪い
s = pow(10,15)
for i in range(min(a),max(a)+1):
    c = 0
    for k in range(n):
        c+=pow(a[k]-i,2)
    s= min(c,s)
print(s)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  n=I()
  l=LI()

  mn=inf
  for i in range(-100,101):
    c=0
    for x in l:
      c+=pow(x-i,2)
    mn=min(mn,c)

  return mn

print(main())

Submission status: Accepted

Solution code:
N = int(input())

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

med_a = sum(a) / N

candidate = [int(med_a), int(med_a + 0.9)]
cost = [0, 0]

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

print(min(cost))

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
c=1000000
for i in range(-100,101):
  c1=0
  for j in l:
    c1+=(j-i)**2
  if c>c1:
    c=c1
print(c)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

num_list = [ int(v) for v in input().split() ]
min_num, max_num = min(num_list), max(num_list)
sum_num = sum(num_list)
square_sum_num = sum([ v**2 for v in num_list ])


ans = square_sum_num

for i in range(min_num,max_num+1):
	ans = min(ans, square_sum_num-2*i*sum_num+n*i**2)
print(ans)

Submission status: Accepted

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

min_cost = 10 ** 7
for y in range(-100, 101):
    cost = sum([(a[i] - y) ** 2 for i in range(N)])
    if cost < min_cost:
        min_cost = cost
print(min_cost)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N, *a = map(int, open(0).read().split())
tgt = round(sum(a) / N)
print(sum((v - tgt) ** 2 for v in a))

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
A = np.array(input().split(),dtype=np.int64)
target = np.arange(-100,101,1)
answer = (np.add.outer(A,target)**2).sum(axis=0).min()
print(answer)

Submission status: Accepted

Solution code:
N = int(input())
a = [int(x) for x in input().split()]
amin, amax = min(a), max(a)

temp = [ai - amin for ai in a]
ans = sum([ai**2 for ai in temp])

for A in range(amin,amax+1):
    for i in range(N):  temp[i] -= 1
    nans = sum([ai**2 for ai in temp])
    if ans > nans:  ans = nans

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=0
def f(x):
    b=0
    for i in a:
        b+=(x-i)**2
    return b
if sum(a)%n==0:
    ans=f(sum(a)//n)
else:
    ans=f(round(sum(a)/n))
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = sorted(list(map(int, input().split())))
cost = []
total = 0
for i in range(a[0], a[-1] + 1):
    for x in a:
        total += (x - i)**2
    cost.append(total)
    total = 0
print(min(cost))

Submission status: Accepted

Solution code:
n=int(input())
A=tuple(map(int,input().split()))
s=sum(A)
ave=s/n
m=s//n
if m+1-ave<ave-m:
    m+=1
print(sum((a-m)**2 for a in A))

Submission status: Accepted

Solution code:
a=int(input())
b=list(map(int,input().split()))
cost=9999999
for i in range(min(b),max(b)+1):
    c=0
    for j in b:
        c=c+(i-j)**2
    if c<cost:
        cost=c
print(cost)

Submission status: Accepted

Solution code:
n=int(input())
a=[int(i) for i in input().split()]
d=sum(a)
t=[d//n,d//n+1]
ans=[0]*2
for i in a:
    ans[0]+=(i-t[0])**2
    ans[1]+=(i-t[1])**2
print(min(ans))

Submission status: Accepted

Solution code:
n,a=open(0);*a,=map(int,a.split());print(min(sum((i-j-100)**2for j in a)for i in range(201)))

Submission status: Accepted

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

minc = 1e12
for i in range(min(xs), max(xs) + 1):
    c = 0
    for x in xs:
        c += (x - i) ** 2

    minc = min(minc, c)

print(minc)

Submission status: Accepted

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

Submission status: Accepted

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

N = int(input())
L = [int(v) for v in input().split()]


ans = 10 ** 18
for i in range(-100, 101):
    t = 0
    
    for v in L:
        t += (v - i) ** 2

    ans = min(ans, t)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
ans = (200**2) * 100

for i in range(-100, 101):
	num = sum([(a - i) ** 2 for a in A])
	ans = min(num, ans)


print(ans)

Submission status: Accepted

Solution code:
N = int(input())
*A, = map(int,input().split())
c = (400**2)*100
for b in range(-100,101):
    c = min(c,sum((a-b)**2 for a in A))
print(c)

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

Submission status: Accepted

Solution code:
#C問題
N = int(input())
A = list(map(int, input().split()))
A_mean = sum(A)/len(A)
A_m = round(A_mean)
ans = 0
for i in range(N):
    ans += (A[i] - A_m)**2
print(ans)

Submission status: Accepted

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

result = 100**2 * 100

for x in range(-100, 101):
    r = sum((x - a) ** 2 for a in A)
    result = min(result, r)

print(result)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
result = 10 ** 9
for y in range(-100, 101):
    num = sum([(a - y) ** 2 for a in A])
    result = min(num, result)
print(result)

Submission status: Accepted

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

mean = sum(A)//N
ans = 0
ans2 = 0
for a in A:
    ans += (mean-a)**2
mean += 1
for a in A:
    ans2 += (mean-a)**2
print(min(ans, ans2))

Submission status: Accepted

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

min_a=min(a)
max_a=max(a)
Cost=[]
for i in range(max_a - min_a+1):
    cost=0
    for k in range(N):
        cost+=(a[k] - (min_a + i))**2
    Cost.append(cost)
print(min(Cost))

Submission status: Accepted

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

w = round(sum(a) / N)
for i in range(N):
    a[i] = (w - a[i]) ** 2

print(int(sum(a)))

Submission status: Accepted

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

	ans = 10**9

	for i in range(-100, 101):
		this_ans = 0
		for ai in a:
			this_ans += (ai-i)*(ai-i)
		ans = min(ans, this_ans)
	print(ans)


if __name__ == '__main__':
	main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
lists = list(map(int,input().split()))
sums = sum(lists)
num = int(round(sums/n,0))
cost=0
for i in lists:
    cost+=pow(i-num,2)
print(cost)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:

import math

def read_input():
    n = int(input())
    alist = list(map(int, input().split()))
    return n, alist


def calc_se(t, alist):
    return sum([(t - a) ** 2 for a in alist])


def submit():
    n, alist = read_input()
    avg_alist = sum(alist)/n

    cand1 = math.floor(avg_alist)
    cand2 = math.ceil(avg_alist)

    if cand1 == cand2:
        se = calc_se(cand1, alist)
    else:
        se1 = calc_se(cand1, alist)
        se2 = calc_se(cand2, alist)
        se = min(se1, se2)

    print(se)

if __name__ == '__main__':
    submit()

Submission status: Accepted

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

def solve(N, A):
    a_max = max(A)
    a_min = min(A)
    res = float("INF")
    for i in range(a_min, a_max+1, 1):
        sum = 0
        for j in range(N):
            sum += (A[j] - i)**2
        res = min(res, sum)
    print(res)
    
if __name__ == '__main__':
    N = int(input())
    A = [int(a) for a in input().split()]
    
    solve(N, A)

Submission status: Accepted

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

def solve(N, A):
    a_max = max(A)
    a_min = min(A)
    sum_list = []
    for i in range(a_min, a_max+1, 1):
        sum = 0
        for j in range(N):
            sum += (A[j] - i)**2
        sum_list.append(sum)
    print(min(sum_list))
    
if __name__ == '__main__':
    N = int(input())
    A = [int(a) for a in input().split()]
    
    solve(N, A)

Submission status: Accepted

Solution code:
import sys
import numpy as np

def main():
    input = sys.stdin.readline
    N = int(input())
    A = np.array(list(map(int, input().split())))
    
    a, b = min(A), max(A)
    ans = float('inf')
    for i in range(a, b+1):
        ans = min(ans, ((A - i)**2).sum())

    return ans


if __name__ == '__main__':
    print(main())

Submission status: Accepted

Solution code:
n = int(input())
L = [int(_) for _ in input().split()]
L.sort()
total = 100000000000000000
num = 0
if L[0] == L[-1]:
    print(0)
else:
    for l in range(L[0], L[-1]):
        tot = 0
        for j in range(n):
            tot += abs(L[j] - l)**2
        if tot < total:
            num = l
            total = tot
            
    print(total)

Submission status: Accepted

Solution code:
n=input()
n=int(n)
z=map(int,input().split())
z=list(z)
def cost(a,b):
    c=0
    for i in range(n):
        c=c+(b[i]-a)**2
    return c
l=[]
for i in range(min(z),max(z)+1):
    l.append(cost(i,z))
print(min(l))

Submission status: Accepted

Solution code:
from statistics import mean

N = int(input())
An = list(map(int, input().split()))
x = round(mean(An))

min_sum = 0
for a in An:
    min_sum += (a - x) ** 2
print(min_sum)

Submission status: Accepted

Solution code:
N = int(input())
As = list(map(int,input().split()))
anss=[]
for i in range(-101,101):
    anss.append(sum([abs(i-a)**2 for a in As]))
print(min(anss))

Submission status: Accepted

Solution code:
from decimal import Decimal, ROUND_HALF_UP


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


a_ave = sum(lst_a) / N
target_val = int(Decimal(str(a_ave)).quantize(Decimal("0"), rounding=ROUND_HALF_UP))
ans = sum([(a - target_val)**2 for a in lst_a])
print(ans)

Submission status: Accepted

Solution code:
# 目標値を全探索
N = int(input())
lst_a = list(map(int, input().split()))


target_values = range(-100, 101)
ans = float("inf")
for target_val in target_values:
    cost = sum([(a - target_val) ** 2 for a in lst_a])
    if cost <= ans:
        ans = cost
print(ans)

Submission status: Accepted

Solution code:
def my_round(x,d=0):
    p=10**d
    return (x*p*2+1)//2/p


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


a_ave = sum(lst_a) / N
target_val = int(my_round(a_ave))
ans = sum([(a - target_val)**2 for a in lst_a])
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def be_together(N: int, A: list) -> int:
    ave = sum(A) // N

    def f(y: int) -> int:
        return sum((a-y)**2 for a in A)

    return min(f(ave), f(ave + 1))


if __name__ == "__main__":
    N = int(input())
    A = [int(s) for s in input().split()]
    ans = be_together(N, A)
    print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
min_cost = 10 ** 18
for y in range(-100, 101):
    cost = 0
    for x in a:
        cost += (x - y) ** 2
    min_cost = min(min_cost, cost)
print(min_cost)

Submission status: Accepted

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

Submission status: Accepted

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

INF = 10**9
min_cost = INF

for target in range(min(A), max(A)+1):
    tmp_cost = 0
    for i in range(N):
        tmp_cost += (target-A[i])**2
    if tmp_cost <= min_cost:
        min_cost = tmp_cost

print(min_cost)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#C問題  
N=int(input())
a=list(map(int,input().split()))
a.sort()
MINA=a[0]
MAXA=a[-1]
roop=MAXA-MINA
MIN=(a[-1]*N)**2
s=MINA
for i in range(roop+1):
    che=0
    for j in range(N):
        che+=(a[j]-s)**2
    MIN=min(MIN,che)
    s+=1
print(MIN)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
A.sort()
ans=10**9

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

print(ans)

Submission status: Accepted

Solution code:
# https://qiita.com/_-_-_-_-_/items/34f933adc7be875e61d0
# abcde	s=input()	s='abcde'
# abcde	s=list(input())	s=['a', 'b', 'c', 'd', 'e']
# 5(1つだけ)	a=int(input())	a=5
# 1 2	| x,y = s_inpl()   |	x=1,y=2
# 1 2 3 4 5 ... n 　	li = input().split()	li=['1','2','3',...,'n']
# 1 2 3 4 5 ... n 　	li = inpl()	li=[1,2,3,4,5,...,n]
# FFFTFTTFF 　	li = input().split('T')	li=['FFF', 'F', '', 'FF']

# INPUT
# 3
# hoge
# foo
# bar
# ANSWER
# n=int(input())
# string_list=[input() for i in range(n)]

import math
import copy
from collections import defaultdict, Counter
# 直積 A={a, b, c}, B={d, e}:のとき，A×B={(a,d),(a,e),(b,d),(b,e),(c,d),(c,e)}: product(A, B)
from itertools import product
# 階乗 P!: permutations(seq), 順列 {}_len(seq) P_n: permutations(seq, n)
from itertools import permutations
# 組み合わせ {}_len(seq) C_n: combinations(seq, n)
from itertools import combinations
from bisect import bisect_left, bisect_right
# import numpy as np

def s_inpl(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
INF = float("inf")

N = int(input())
a = inpl()

ans = INF
for i in range(-100, 101):
    tmp = sum([ (abs(ai-i))**2 for ai in a])
    if tmp<ans:
        ans = tmp

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = [ int(i) for i in input().split() ]
b = 1e9
for ans in range(-100,100+1):
    b = min(b, sum([ (i-ans)**2 for i in a ]))
print(b)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin
input = stdin.readline
N = int(input().rstrip())
a = [int(x) for x in input().rstrip().split()]
min = 1000000
for i in range(-100, 101):
	sum = 0
	tmp = [i] * len(a)
	for i in range(len(a)):
		sum += (tmp[i] - a[i]) ** 2
	if sum < min:
		min = sum

print(min)

Submission status: Accepted

Solution code:
n = int(input())
listA = list(map(int, input().split()))
a = 0
b =  100000000
for i in range(min(listA), max(listA)+1):
  for j in range(n):
    a = a + (listA[j]-i)**2
  if a<b:
    b=a
  a=0
print(b)

Submission status: Accepted

Solution code:
from functools import reduce

def main():
    # 初期値用:十分大きい数(100億)
    # 1e10

    # 初期値用:十分小さい数(-100億)
    # -1e10
    
    # 一文字のみを読み込み
    # s = input().rstrip().split(' ')
    
    # スペース区切りで標準入力を配列として読み込み
    # s = input().rstrip().split(' ')

    # 位置文字ずつ標準入力を配列として読み込み
    # s = list(input().rstrip())
    
    N = int(input().rstrip())
    a_list = input().rstrip().split(' ')
    cost = 0
    min_cost = 201 * 201 * 100
    for i in range(-100,101):
        cost = 0
        for a in a_list:
            a_num = int(a)
            cost = cost + (a_num - i) * (a_num-i)
            # print(i,a_num,cost)
        if(cost < min_cost):
            min_cost = cost
            # print('new ans',min_cost)
    print(min_cost)
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from functools import reduce

def main():
    # 一文字のみを読み込み
    # s = input().rstrip().split(' ')
    
    # スペース区切りで標準入力を配列として読み込み
    # s = input().rstrip().split(' ')

    # 位置文字ずつ標準入力を配列として読み込み
    # s = list(input().rstrip())
    N = int(input().rstrip())
    a_list = input().rstrip().split(' ')
    cost = 0
    min_cost = 1e10
    for i in range(-100,101):
        cost = 0
        for a in a_list:
            a_num = int(a)
            cost = cost + (a_num - i) * (a_num-i)
            # print(i,a_num,cost)
        if(cost < min_cost):
            min_cost = cost
            # print('new ans',min_cost)
    print(min_cost)
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np
N=int(input())
A=np.array(list(map(int, input().split())))
m=A.mean().round()

print(int(sum((A-m)**2)))

Submission status: Accepted

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

mu = round(sum(a)/n)

print(sum(list(map(lambda x:(x-mu)**2,a))))

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = list(map(lambda x: int(x)+100, input().split()))
    avg = int(sum(A)/N + 0.5)
    ans = 0
    for i in range(N):
        ans += (A[i] - avg)**2
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

print(cost)

Submission status: Accepted

Solution code:
N = int(input())
S = list(map(int, input().split()))
cost_all = 1e10
for s_set in range(min(set(S)), max(set(S)) + 1):
	cost = 0
	for s in S:
		cost += (s_set - s)** 2
		#print(s, "->", s_set, ":", cost)
	cost_all = min(cost, cost_all)
print(cost_all)

Submission status: Accepted

Solution code:
# coding: UTF-8

import numpy as np

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

m = round(a.mean())

print(int(np.sum((a - m)**2)))

Submission status: Accepted

Solution code:
a,b=int(input()),list(map(int,input().split()))
ans=-1
for i in range(-100,100+1):
  c=0
  for j in range(a):c+=(b[j]-i)**2
  if ans==-1:ans=c
  elif ans>c:ans=c
print(ans)

Submission status: Accepted

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

ans = 10**10
for i in range(-100,100+1):
    s = 0
    for j in range(N):
        s += (i - a[j])**2
    ans = min(s,ans)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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


ave = int(sum(X)/n)

sum = 0
sum1 = 0
sum2 = 0
for i in X:
    sum += (ave - i)**2
    sum1 += (ave - 1 - i)**2
    sum2 += (ave + 1 - i)**2
print(min(sum, sum1, sum2))

Submission status: Accepted

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

print(int(sum([abs(a[i]-n)**2 for i in range(N)])))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
M=int(input())
n=list(map(int,input().split()))
x=int(round(np.mean(n)))
ans=0
for i in n:
    ans=ans+(i-x)**2
print(ans)

Submission status: Accepted

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

ans = 10 ** 10
for x in range(min(a), max(a) + 1):
    temp = 0
    for b in a:
        temp += (b - x) ** 2
    ans = min(ans, temp)

print(ans)

Submission status: Accepted

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

print(min(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = input()
a = map(int, raw_input().split())
ans = 99999999
for t in range(-100,101):
  ans = min(ans, sum((t-x)**2 for x in a))
print ans

Submission status: Accepted

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

Submission status: Accepted

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

b = sum(a)//n

co = 10**9
su = 0
for k in range(3):
    for i in a:
        su += abs(i-b+(k-1)) ** 2
    co = min(co,su)
    su = 0

print(co)

Submission status: Accepted

Solution code:
n = int(input())

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

result = -1
for t in range(min(li_a), max(li_a) + 1):
    sum = 0
    for a in li_a:
        sum += (a - t)**2
    if result < 0:
        result = sum
    else:
        result = min(result, sum)

print(result)

Submission status: Accepted

Solution code:
def main():
  N = int(input())
  a_list = list(map(int, input().split()))
  cost = 0
  if not (len(a_list) == N):
    print('ERROR')
  a_ave = sum(a_list)/N # 平均値取得
  a_ave = round(a_ave) # 平均値を整数で四捨五入
  for i in range(len(a_list)):
    cost = cost + (a_list[i]-a_ave)**2
  print(cost)
main()

Submission status: Accepted

Solution code:
n = int(input())
li = list(map(int, input().split()))
s =int(sum(li) / len(li))
p = 0
q = 0

if s < 0:
    a = s - 1
    b = s
else:
    a = s
    b = s + 1

for i in range(len(li)):
    p = p + (li[i] - a) ** 2
    q = q + (li[i] - b) ** 2

print(min(p, q))

Submission status: Accepted

Solution code:
import math
n=int(input())
#li=input().split()
li=list(map(int,input().split()))
#n=input()
A=0
for i in range(n):
    A+=li[i]
x=A/n
a=math.ceil(x)-x
b=x-math.floor(x)
if a-b>=0:
    N=math.floor(x)
else:
    N=math.ceil(x)
ans=0
for i in range(n):
    ans+=(N-li[i])**2

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
s=2000000000000
for i in range(-100,101):
  su=0
  for j in l:
    su+=(i-j)*(i-j)
  if su<s:
    s=su
print(s)

Submission status: Accepted

Solution code:
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN

N=int(input())
lst=list(map(int,input().split()))
average=Decimal(str(sum(lst)/len(lst))).quantize(Decimal('0'),rounding=ROUND_HALF_UP)
_lst=[(i-average)**2 for i in lst]
print(sum(_lst))

Submission status: Accepted

Solution code:
N = int(input())
ans = 10**10
L = list(map(int,input().split()))
for i in range(-101,101): 
  tmp = 0
  for x in L:
    tmp += (i - x) ** 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):
  tmp_ans = 0
  for j in range(n):
    tmp_ans += (i-a[j])**2
  ans = min(ans,tmp_ans)
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:
from math import ceil
from math import floor
n = int(input())
nums = list(map(int, input().split()))
ave = sum(nums)/n
if ave%1 > 0.5:
    ave = ceil(ave)
elif ave%1 <= 0.5:
    ave = floor(ave)
ans = 0
for i in range(n):
    ans += (nums[i]-ave)**2
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

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

min_sum = 1000000000

for i in range(min(a_input), max(a_input)+1):
    tmp_sum=0
    for j in range(n):
        tmp_sum += (a_input[j]-i)**2
    
    min_sum = min(min_sum, tmp_sum)

print(min_sum)