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:
"""
kari1 = '2'
kari2 = '4 8' #8
#kari1 = '3'
#kari2 = '1 1 3' #3
#kari1 = '3'
#kari2 = '4 2 5' #5
#kari1 = '4'
#kari2 = '-100 -100 -100 -100' #0
in1 = kari1
in2 = kari2.split()
"""
in1 = input()
in2 = input().split()

AVEa = 0
RC   = 0
for item in in2:
    AVEa = AVEa + int(item)

AVEa = round(AVEa / len(in2))

for item in in2:
    if int(item) != AVEa:
        RC = RC + ((int(item) - AVEa) ** 2)

print(RC)

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split())
ans=10**9
for i in range(-100,101):
  c=0
  for a_i in a:
    c+=(a_i-i)**2
  ans=min(ans,c)
print(ans)

Submission status: Accepted

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

ans = 10**9

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

print(ans)

Submission status: Accepted

Solution code:
import math

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

sub1 = math.floor(mean)
sub2 = math.ceil(mean)

ans1 = 0
ans2 = 0

for i in range(n):
    ans1 += (a[i]-sub1)**2
    ans2 += (a[i]-sub2)**2
    
print(min(ans1,ans2))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# /usr/bin/python3
# -*- coding: utf-8 -*-
from queue import Queue
from queue import LifoQueue as Stack
from math import sqrt, floor, ceil, log2
from fractions import gcd
from itertools import permutations, combinations
from operator import itemgetter
from functools import cmp_to_key



__MOD__=(10**9)+7
yn = 'YNeos'
judge = False
cnt = 0
ans = None


def lcm(a, b):
    return (a*b) // gcd(a,b)

def intinput():
    return int(input())

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

def lineinputs(func=intinput):
    datas = []
    while True:
        try:
            datas.append(func())
        except EOFError:
            break
    return datas

class ModInt():
    def __init__(self, x):
        self.__x = (x % __MOD__)
    def __add__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x + other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x + other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __sub__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x - other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x - other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __mul__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x * other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x * other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __truediv__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x * other.__modinv())%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x * other.__modinv())%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __pow__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__(pow(self.__x, other.__x, __MOD__)))
        elif type(other) == ModInt:
            return self.__class__(pow(self.__x, other.__x, __MOD__))
        else:
            raise Exception("Not Int or Not ModInt")
    def __modinv(self, m=__MOD__):
        a = self.__x
        if a == 0:
            raise ZeroDivisionError()
        if gcd(a, m) != 1:
            raise Exception("%sの逆数は求まりません。" % a)
        b, u, v = m, 1, 0
        while b != 0:
            t = a//b;
            a -= t*b
            a, b = b, a
            u -= t * v
            u, v = v, u
        u %= m
        if u < 0:
            u += m
        return u
    def __int__(self):
        return self.__x



if __name__ == '__main__':
    N = intinput()
    As = list(map(int, input().split()))

    # total = (sum(As) + N - 1)//N
    # min_total = 0
    # for a in As:
    #     min_total += (a-total)*(a-total)
    min_total = 100010001
    for i in range(-100, 101):
        tmp = 0
        for a in As:
            tmp += (a-i)*(a-i)
        min_total = min(min_total, tmp)

    # 出力
    print(min_total)



# EOF

Submission status: Accepted

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

min = None
for y in range(-100, 101):
    sum = 0
    for x in a:
        sum += abs(x - y) ** 2
    if min is None or min > sum:
        min = sum

print(min)

Submission status: Accepted

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

upper_ave = int(sum(l)/len(l)) + 1
lower_ave = int(sum(l)/len(l))

ans_1, ans_2 = 0, 0

for i in l:
    ans_1 += (i - upper_ave) ** 2
    ans_2 += (i - lower_ave) ** 2

print(min(ans_1,ans_2))

Submission status: Accepted

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

y = 4*10**6
for i in range(min(l), max(l) + 1):
    x = 0
    for j in range(n):
        x += (l[j] - i)**2
    y = min(y, x)

print(y)

Submission status: Accepted

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

alist.sort()

y = alist[0]

res = float('INF')

if y == alist[-1]:
    print(0)
    exit(0)

for i in range(y, alist[-1] + 1):
    cost = 0
    for a in alist:
        cost += (a - i)**2
    # print('rescos', res, cost)
    res = min(res, cost)
    if cost > res:
        break

print(res)

Submission status: Accepted

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

ans = 10 ** 10
for x in range(min(a), max(a)+1):
    ans = min(ans, sum((x - ai) ** 2 for ai in a))
    
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

for y in range(-100, 101):
    s = 0
    for i in range(N):
        s += (a[i] - y) ** 2
    if sm > s:
        sm = s

print(sm)

Submission status: Accepted

Solution code:
n = int(input())

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

mean = sum(A) // n

t = 0
s = 0
for i in A:
    t += (mean - i)**2
    s += (mean + 1 - i)**2
print(min(t, s))

Submission status: Accepted

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

Submission status: Accepted

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

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

def main():
    mod=10**9+7
    N=I()
    a=LI()
    ave1=sum(a)//N
    
    ans=[0,0]
    for i in range(N):
        ans[0]+=(ave1-a[i])**2
        ans[1]+=(ave1-a[i]+1)**2
    print(min(ans))
    

main()

Submission status: Accepted

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

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

def main():
    mod=10**9+7
    N=I()
    a=LI()
    
    ans=10**10
    for t in range(-100,101,1):
        temp=0
        for i in range(N):
            temp+=(t-a[i])**2
        ans=min(ans,temp)
        
    print(ans)

main()

Submission status: Accepted

Solution code:
import sys

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

answer = sys.maxsize
for y in range(-100, 100 + 1):
    acc = 0
    for x in a:
        acc += (x - y) * (x - y)
    answer = min(answer, acc)
print(answer)

Submission status: Accepted

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

answer = 10**100
for y in range(-100, 100 + 1):
    acc = 0
    for x in a:
        acc += (x - y) * (x - y)
    answer = min(answer, acc)
print(answer)

Submission status: Accepted

Solution code:
import math
N = int(input())
A = [int(x) for x in input().split()]
s = sum(A)
if s % N== 0:
    ave = s // N
    ans = [(x - ave)**2 for x in A]
    ans = sum(ans)
    print(ans)
    exit()
else:
    ave1, ave2 = math.ceil(s / N), math.floor(s / N)
    ans1 = [(x - ave1)**2 for x in A]
    ans2 = [(x - ave2)**2 for x in A]
    ans = min(sum(ans1), sum(ans2))
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

x = round(sum(a) / N)

ans = 0
for i in range(N):
    ans += math.pow(x - a[i], 2)

print(int(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
ans=float("INF")
for i in range(-100,101):
    ans_=sum((a-i)**2 for a in A)
    ans=min(ans,ans_)
print(ans)

Submission status: Accepted

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

m = sum(a) // n
ans = 0
for x in a:
    ans += (x - m) ** 2
ansp = 0
for x in a:
    ansp += (x - m - 1) ** 2
print(min(ans, ansp))

Submission status: Accepted

Solution code:
#numpy使ってみる
import numpy as np
n = int(input())
a = np.array(list(map(int, input().split( ))))

ans = 10**10

for i in range(-100,101):
    tmp = ((a-i)**2).sum()
    ans = min(tmp,ans)
print(ans)

Submission status: Accepted

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

x = round(sum(A)/n)
print(sum((a-x)**2 for a in A))

Submission status: Accepted

Solution code:
#C
import math
n=int(input())
a=list(map(int,input().split()))
h=round(sum(a)*10/n,-1)/10
s=0
for i in a:
    s+=(i-h)**2
print(int(s))

Submission status: Accepted

Solution code:
num = int(input())
input_num = input()
num_list = [int(i) for i in input_num.split(" ")]
num_list.sort()
min = num_list[0]
max = num_list[num-1]
if min== max:
    print(0)
    exit()
min_sum = 100**3
for i in range(min,max):
    temp_sum = 0
    for j in range(0,num):
        temp_sum += (num_list[j] - i)**2
    if min_sum>temp_sum:
        min_sum = temp_sum
print(min_sum)

Submission status: Accepted

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

n = int(readline())
lst1 = list(map(int,readline().split()))

heikin = sum(lst1)//n
ans = 10**9
for i in (heikin,heikin+1):
    res = 0
    for j in lst1:
        res += (i-j)**2

    ans = min(ans,res)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
ave=sum(a)//n
print(min(sum([pow(i-ave,2) for i in a]),sum([pow(i-ave-1,2) for i in a])))

Submission status: Accepted

Solution code:
from statistics import mean, median,variance
N = int(input())
A = list(map(int, input().split()))

m = round(mean(A))
cost = 0
for a in A:
  cost += (a-m)**2
print(cost)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
avg = sum(A)//n
avg2 = sum(A)//n + 1
ans = 0
ans2 = 0
for i in A:
    ans += (i-avg)**2
    ans2 += (i-avg2)**2
print(min(ans, ans2))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
mini = 40000*100
for n in range(-100,101):
    cost = 0
    for item in A:
        cost += (item-n)**2
    if cost < mini:
        mini = cost
print(mini)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
A=[int(x) for x in input().split()]
array=[]
for i in range(min(A),max(A)+1):
    S=0
    for j in range(len(A)):
        S+=(i-A[j])**2
    array.append(S)
print(min(array))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

ave = round(sum(a) / n)
result = 0
for i in a:
    result += (i-ave) ** 2
print(result)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
AVE = round(sum(A)/len(A))
print(sum((AVE - i)**2 for i in A))

Submission status: Accepted

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

Submission status: Accepted

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

high = max(a)
low = min(a)
cost = 0
ans = 10**9

for i in range(low,high+1):
    for j in a:
        cost += pow(j-i,2)
        
    if cost < ans:
        ans = cost
        cost = 0
    else:
        cost = 0
        
print(ans)

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):
    total = 0
    for b in a:
        total += (b-i)**2
    if total <= ans:
        ans = total
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):
    c = 0
    for j in a:
        c += (i - j) ** 2
    if m > c:
        m = c
print(m)

Submission status: Accepted

Solution code:
import sys

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7


N = I()
A = LMI()

avg = sum(A) // N

ans = float('inf')
for a in [avg - 1, avg, avg + 1]:
    cost = 0
    for b in A:
        cost += (a - b) ** 2
    ans = min(ans, cost)
print(ans)

Submission status: Accepted

Solution code:
#ABC043 C

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

Submission status: Accepted

Solution code:
N=int(input())
a=input().split()
numbers=[]
for n in a:
    b=int(n)
    numbers.append(b)
a=round(sum(numbers)/N)
answers=[]
for z in numbers:
    y=(z-a)*(z-a)
    answers.append(y)
print(sum(answers))

Submission status: Accepted

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

if len(set(A)) == 1:
    print(0)
    exit()

v = 0
min_val = 10000000
for i in range(min(A), 101):
    v = 0
    for a in A:
        v += (a - i)**2
    if min_val > v:
        min_val = v

print(min_val)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n = int(input())
a = list(map(int,input().split()))
ans = 10**7
for i in range(-100,101):
    tmp = 0
    for j in range(n):
        tmp += (a[j]-i)**2
    ans = min(ans,tmp)
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

N =int(input())

a = np.array([int(i) for i in input().split()])

a_min = min(a)
a_max =max(a)
ans = 0

for i in range(a_min,a_max+1):
    diff_a = a - i
    dummy = int(sum([i**2 for i in diff_a]))
    if i == a_min:
        ans = dummy
    elif i>a_min:
        if ans > dummy:
            ans = dummy

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

mini = 10**10
for i in range(min(A), max(A)+1):
    count = 0
    for j in range(N):
        count = count + (A[j] - i)**2
    if count < mini:
        mini = count
print(mini)

Submission status: Accepted

Solution code:
import sys, math

def input():
    return sys.stdin.readline()[:-1]

def main():
    N = int(input())
    a = list(map(int,input().split()))
    ans = 10**9
    for k in range(-100,101):
        temp = 0
        for e in a:
            temp += (k-e)**2
        ans = min(ans,temp)
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from decimal import Decimal, ROUND_HALF_UP


def main():
    length = int(input())
    numbers = list(map(int, input().split()))
    target = Decimal(str(sum(numbers) / length)).quantize(Decimal('0'), rounding=ROUND_HALF_UP)
    answer = 0
    for num in numbers:
        answer += (num - target) ** 2
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

m = int(1e9)
for i in range(-100, 101):
  l = 0
  for x in a:
    l += (x - i) ** 2
  m = min(m, l)
print(m)

Submission status: Accepted

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

Submission status: Accepted

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

def calc(lis,m):
    res=0
    for i in range(len(lis)):
        res+=(lis[i]-m)**2
    return res
  
mi=calc(a,a[0])
for i in range(a[0]+1,a[-1]+1):
    tmp=calc(a,i)
    if tmp<mi: mi=tmp
print(mi)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=10**10
for i in range(-100,101):
    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 = float('inf')
for cand in range(-100, 101):
    tmp = 0
    for ai in a:
        tmp += (ai - cand) ** 2
    ans = min(tmp, ans)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
lst = [int(x) for x in input().split()]

ret = 100 ** 100
for x in range(-100, 101):
    ret = min(ret, sum([(x - n) ** 2 for n in lst]))

print(ret)

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int,input().split()))
c = int(sum(l)/n)
ans = 10**10
for i in range(c-1,c+2):
    count = 0
    for j in l:
        count += (i-j)**2
    ans = min(ans,count)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

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

if (a/a[0]).sum() == len(a):
    total = 0

total = 10**9
for i in range(a.min(), a.max()+1):
    total_new = ((a - i)**2).sum()
    if total_new <= total:
        total = total_new
print(total)

Submission status: Accepted

Solution code:
def main():
  n = int(input())
  nums = list(map(int, input().split()))
  cmin = 0
  for x in nums:
    cmin += (x - min(nums)) ** 2
  for i in range(min(nums), max(nums)+1):
    c = getcost(i, nums)
    if c < cmin:
      cmin = c
  print(cmin)
  
def getcost(x, nums):
  c = 0
  for n in nums:
    c += (n - x) ** 2
  return c
  
if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
ans=int(1000000000)
for i in range(-1000,1000):
    now=int(0)
    for j in range(n):
        now+=(i-A[j])*(i-A[j])
    if now<ans:
        ans=now
        
print(ans)

Submission status: Accepted

Solution code:
# /usr/bin/env python3
# https://atcoder.jp/contests/abc043/tasks/arc059_a

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

Submission status: Accepted

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

k=round(sum(a)/n)
ans=0

for i in range(n):
    ans+=(k-a[i])**2
print(ans)

Submission status: Accepted

Solution code:
#!/use/bin/env python3

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

s = int(round(sum(a) / n))

ans = 0

for i in a:
    ans += pow(i - s, 2)

print(ans)

Submission status: Accepted

Solution code:
#C - いっしょ

N = int(input())

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

#書き換える数字num
num = int(round( sum(a) / len(a) ,0))

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

Submission status: Accepted

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

mi = min(A)
ma = max(A)

ans = 10**9

for k in range(mi, ma+1):
  tmp = 0
  for a in A:
    tmp += abs(a - k) ** 2

  if ans > tmp:
    ans = tmp

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
nums = input().split()
costs = []
if nums.count(nums[0]) != len(nums):
    for i in range(-100, 101):
        sum = 0
        for num in nums:
            sum += (int(num) - i) ** 2
        costs.append(sum)
    ans = min(costs)
else:
    ans = 0
print(ans)

Submission status: Accepted

Solution code:
import itertools
import math
n=int(input())
a=list(map(int,input().split()))
ave=round(sum(a)/n)
#print(ave)
count=0
for i in range(n):
	count+=(a[i]-ave)**2
print(count)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
l = sum(A)
if l % N == 0:
  x = l // N
  s = 0
  for a in A:
    s += (a - x) ** 2
  print(s)
else:
  x = l // N
  y = x + 1
  s = 0
  t = 0
  for a in A:
    s += (a - x) ** 2
    t += (a - y) ** 2
  print(min(s, t))

Submission status: Accepted

Solution code:
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
n = int(input())
a = list(map(int,input().split()))
h = Decimal(str(sum(a)/n)).quantize(Decimal('0'), rounding=ROUND_HALF_UP)
a = list(map(lambda x:(x-h)**2,a))
print(sum(a))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
try:
    from typing import List
except ImportError:
    pass


def solve(N: int, a: "List[int]"):
    print(min(sum((ai - y) ** 2 for ai in a) for y in range(-100, 101)))


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:
n = int(input())
l = list(map(int, input().split()))

mn = min(l)
mx = max(l)

ms = 10**9
for j in range(mn, mx+1):
    s = sum([(i-j)**2 for i in l])
    ms = min(ms, s)
    
print(ms)

Submission status: Accepted

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

mn = min(l)
mx = max(l)

ms = 10**9
ll = [0]*n
for j in range(mn, mx+1):
    for i in range(n):
        ll[i] = (l[i]-j)**2
        
    s = sum(ll)
    ms = min(ms, s)
    
print(ms)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
given = [int(x) for x in input().split()]
avgs  = round(sum(given) / n)
cost  = sum([(x - avgs) ** 2 for x in given])
print(cost)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
a=list(map(int,input().split()))
avg=0
for j in range(N):
    avg+=a[j]
avg=avg/N
t=int(avg)
if abs(t-avg)>abs(t+1-avg):
    t=t+1
cost=0
for j in range(N):
    cost+=(a[j]-t)**2
print(cost)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
answer = 10 ** 15
for i in range(-100,101):
    tmp = 0
    for m in A:
        tmp += (m-i) ** 2
    answer = min(answer, tmp)
print(answer)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
numlist = [int(i) for i in input().split()]
cost = 0
ave_f = sum(numlist) / n
ave_ch = sum(numlist) // n
if ave_f - ave_ch >= 0.5 :
    ave_i = ave_ch + 1
else :
    ave_i = ave_ch
for i in range(n) :
    cost += (ave_i - numlist[i]) ** 2
print(cost)

Submission status: Accepted

Solution code:
# C - いっしょ

from decimal import Decimal, ROUND_HALF_UP

N = int(input())
a = list(map(int, input().split()))
m = Decimal(sum(a) / N).quantize(Decimal('0'), rounding=ROUND_HALF_UP) 
#print(sum(a) / N)
#print(m)

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

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys

def solve(N: int, a: "List[int]"):
    min_cost = 10**9

    for target in range(-100,101):
        tmp = 0
        for i in range(N):
            tmp += (a[i]-target)**2
        min_cost = min(min_cost,tmp)
    print(min_cost)
    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:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-


def main():
    N = int(input())
    A = list(map(int, input().split()))
    result = sum([(a - max(A))**2 for a in A])
    for i in range(min(A), max(A)):
        tmp = sum([(i - a)**2 for a in A])
        if result > tmp:
            result = tmp

    print(result)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

ans = float("inf")

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

print(ans)

Submission status: Accepted

Solution code:
def S(a,N,M):
    m=0
    for i in range(N):
        m+=(a[i]-M)**2
    return m
    

N=int(input())
a=[int(x) for x in input().split()]

m=S(a,N,-100)

for i in range(-99,101):
    if m > S(a,N,i):
        m=S(a,N,i)
        
print(m)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
minA = min(A)
maxA = max(A)

ans = float("inf")
for i in range(minA, maxA+1):
    cost = 0
    for a in A:
        cost += (i - a)**2
    if cost < ans:
        ans = cost

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(e) for e in input().split()]
ans = float("inf")
MIN, MAX = min(a), max(a)
for i in range(MIN, MAX+1):
    cost = 0
    for j in a:
        cost = cost + (i-j)**2
    ans = min(ans, cost)
print(ans)