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 = [int(x) for x in input().split()]

def solve(A):
    def cost(x):
        return sum((x -a)**2 for a in A)
    l = min(A)
    r = max(A)

    l = -100
    r = 100
    while r - l >= 3:
        d = (r - l) // 3
        m1 = l + d
        m2 = m1 + d
        #print(cost(l), cost(r), "d=%d"%d, (l, m1, m2, r))
        L = [(cost(l), l), (cost(r), r), (cost(m1), m1), (cost(m2), m2)]
        #print(L)
        (_, r), (_, l) = sorted(L)[:2]
        if l > r:
            r, l = l, r
        #print("l=%d, r=%d" % (l,r))
    c, a = sorted([(cost(i), i)for i in range(l, r+1)])[0]
    print(c)


solve(A)

Submission status: Accepted

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

Submission status: Accepted

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

ave = round(sum(a_i) / len(a_i))
ans = 0
for x in a_i:
    ans += (x - 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):
    sum = 0
    for n in range(N):
        sum += (a[n] - i) ** 2

    ans = min(ans, sum)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
AS  = list(map(int,input().split()))
cost = float('inf')

for i in range(min(AS),max(AS)+1):
  c = 0
  for a in AS:
    c += (i-a)**2
  if c < cost:
    cost = c
print(cost)

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):
    cost = 0
    for aa in a:
        cost += (i - aa)**2
    if cost < ans :
        ans = cost
print(ans)

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, floor
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

n = INT()
A = LIST()
a = 0
ans = INF

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

    ans = min(ans, a)
print(ans)

    





exit()
ave = sum(L)/n
if ave == int(ave):
    print(int(ave))
    a = ave
    for i in range(n):
        ans += (L[i] - a) ** 2
    print(int(ans))
    exit()
left = 0
if ave != int(ave):
    for i in range(n):
        if L[i] < ave:
            left = i + 1
    print(left)
    right = n - left
    if left < right:
        a = ceil(ave)
    else:
        #print(floor(ave))
        a = floor(ave)
    print(a)
    for i in range(n):
        ans += abs(L[i] - a) ** 2
    print(ans)

Submission status: Accepted

Solution code:
_ = int(input())
A = list(map(int, input().split()))
mi, ma = min(A), max(A)

cost = 10**10

for x in range(mi, ma+1):
    cost = min(cost, sum((x-a)**2 for a in A))

print(cost)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
def f(x):
    return sum([(a_i-x)**2 for a_i in A])
print(min([f(x) for x in range(-100, 101)]))

Submission status: Accepted

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

x = int(sum(A)/N)
cnt1 = 0
cnt2 = 0
for a in A:
    cnt1 += (a-x)**2
    cnt2 += (a-x-1)**2

print(min(cnt1, cnt2))

Submission status: Accepted

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

c1 = int(sum(l)/n)
c2 = c1 + 1

for i in range(len(l)):
    a1 = a1 + (c1-l[i])**2
    a2 = a2 + (c2-l[i])**2
ans = min(a1,a2)
print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
nums = list(map(int, input().split()))
nums_kazu = len(nums)
nums_sum = 0
average = 0
ans = 0
for i in nums:
    nums_sum += i
average = round(nums_sum/nums_kazu)

for i in nums:
     ans += abs(i - average)**2

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
b=sum(A)//N
a=b+1
c=b+1

x=0
y=0
z=0
for i in A:
  x+=(a-i)**2
  y+=(b-i)**2
  z+=(c-i)**2
print(min(x,y,z))

Submission status: Accepted

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

Submission status: Accepted

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

m = int(((sum(a)/n)*2 + 1) //2)
# m = statistics.mean(a)
# m = int(m) + 1 if m % 1 > 0.5 else int(m)
r = 0
for i in a:
  r += (i - m)**2
  
print(r)

Submission status: Accepted

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

#残差最小化の解
x = sum(a)/N
x_large = math.ceil(x)
x_small = math.floor(x)

#大きい残差と小さい残差を計算
sum_large = 0
sum_small = 0
for i in a:
	sum_large += (x_large-i)**2
	sum_small += (x_small-i)**2
#最小残差
print(min(sum_large,sum_small))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
def dist(x):
  ret = 0
  for i in A:
    ret+=(x-i)**2
  return ret
ans = dist(100)
for X in range(-100,100):
  ans=min(ans,dist(X))
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
s = [int(c) for c in input().split()]
def solution(s):
    cost = 0
    mean = round(sum(s)/len(s))
    for i in s:
        cost += abs(i-mean) ** 2
        #print(f"DEBUG >> {mean} {i} {cost}")
    return cost
  
print(solution(s))

Submission status: Accepted

Solution code:
import math
import sys
from functools import reduce
input = sys.stdin.readline

def fullsearch(a):
    for i in range(-100, 101):
        yield reduce(lambda total, x: (x - i) ** 2 + total, a, 0)

def main():
    input()
    A = [int(c) for c in input().split()]

    print(min(fullsearch(A)))

main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
n=int(input())
a=np.array([int(x) for x in input().split()])
x=[]
for i in range(-100,101):
	s=np.sum((a-i)*(a-i))
	x.append(s)
print(min(x))

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[len(a)-1]+1):
    temp = sum([(abs(j-i))**2 for j in a])
    ans = min(ans,temp)
print(ans)

Submission status: Accepted

Solution code:
def solve(n, A):
    ave = round(sum(A) / n)
    return sum((a - ave) ** 2 for a in A)

_n = int(input())
_A = list(map(int, input().split()))
print(solve(_n, _A))

Submission status: Accepted

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

Submission status: Accepted

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

def main():
    N = int(input())
    A = [int(x) for x in input().split()]


    ans = float("inf")
    for j in range(-101, 101):
        tmp = 0
        for i in A:
            tmp += (i - j) ** 2

        ans = min(ans, tmp)

    print(ans)


    
    

if __name__ == '__main__':
    main()


#2
#-1 -4
#5

Submission status: Accepted

Solution code:
def pow_k(x, n):
    """
    O(log n)
    """
    if n == 0:
        return 1

    K = 1
    while n > 1:
        if n % 2 != 0:
            K *= x
        x *= x
        n //= 2

    return K * x

N=int(input())
L=list(map(int,input().split()))
import sys
ans = sys.maxsize

for i in range(-100,101):
  m_diffs = 0
  for j in range(N):
    m_diffs += pow_k((L[j] - i),2)
  ans = min(ans, m_diffs)

print(ans)

Submission status: Accepted

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

average = (sum(a) + N // 2) // N
cost = 0

for i in range(N):
    cost += (average - a[i]) ** 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 a in A:
    tmp += (a - i) ** 2
  answer = min(answer, tmp)

print(answer)

Submission status: Accepted

Solution code:
n = int(input())
nums = sorted([int(i) for i in input().split()])
# n = 3
# nums = [1, 1, 3]

median = nums[n//2]
mean = sum(nums) // n

ans = 10 ** 10
for diff in range(min(median, mean), max(median, mean)+2):
    diff_sum = 0
    for num in nums:
        diff_sum += (num - diff) ** 2
    ans = min(ans, diff_sum)

print(ans)

Submission status: Accepted

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

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

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
def main():
    N = I()
    A = LI()
    ans = inf
    for targ in range(-100, 101):
        s = sum([(x - targ) ** 2 for x in A])
        ans = min(s, ans)
    print(ans)
main()

Submission status: Accepted

Solution code:
N = int(input())
nolist = list(map(int, input().split()))
minimum = 10**9
for i in range(min(nolist), max(nolist)+1):
  cost = sum((i-nolist[j])**2 for j in range(N))
  if cost < minimum:
    minimum = cost
print(minimum)

Submission status: Accepted

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

sD = sum(dList)/n
ig = int(sD)
po = sD-int(sD)

ad = 0
if abs(po) >= 0.5:
  if sD >= 0:
    ad = ig + 1
  else:
    ad = ig -1
else:
  ad = ig

result = 0
for i in range(len(dList)):
  result += (dList[i]-ad)**2

print(result)

Submission status: Accepted

Solution code:


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

    min_a = min(a_str)
    max_a = max(a_str)

    min = float("inf")
    ans = 0
    for i in range(min_a, max_a):

        temp = sum(list(map(lambda x: (x - i)**2, a_str)))
        if min > temp:
            min = temp
            ans = temp
    print(ans)

Submission status: Accepted

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

cost = []

for i in range(-100,101):
    cc = 0
    for j in range(n):
        cc += (i - a[j])*(i - a[j])
    cost.append(cc)

cost.sort()
print(cost[0])

Submission status: Accepted

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

num_of_equal = sum([1 for i, j in zip(a[:-1], a[1:]) if i == j]) + 1

if num_of_equal == len(a): print(0)
else:
    answer = []
    for y in range(min(a), max(a)+1):
        answer.append(sum([(x - y)**2 for x in a]))

    print(min(answer))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n=int(input())
A=list(map(int,input().split()))
ans=float("inf")
for i in range(-100,101):
    s=0
    for a in A:
        s+=(a-i)**2
    ans=min(ans,s)
print(ans)

Submission status: Accepted

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

ave1 = sum(a) // len(a)
ave2 = ave1 + 1

cost1 = 0
for i in range(n):
    cost1 += (a[i] - ave1)**2

cost2 = 0
for i in range(n):
    cost2 += (a[i] - ave2)**2

print(min(cost1,cost2))

Submission status: Accepted

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

Submission status: Accepted

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


min_cost = -1
for i in range(-100, 101):
  cost = 0
  for k in range(N):
    cost += (i - a[k])**2
  if min_cost == -1:
    min_cost = cost
  else:
    min_cost = min(min_cost, cost)

print(min_cost)

Submission status: Accepted

Solution code:
#043c
N = int(input()) #N <= 100
a_list = list(map(int, input().split())) #-100 <= ai <= 100

#ループ数は200*100 = 20000.総当たりでOK
min_cost = 111*(200**2)

for y in range(min(a_list),max(a_list) + 1):
    cost = 0
    
    for x in a_list:
        cost += (x - y)**2
    
    if cost < min_cost:
        min_cost = cost

print(min_cost)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8

from statistics import mean

N=int(input())
A=list(map(int,input().split()))
a1=int(mean(A))
a2=a1+1
ans=10**9

tmp=0
for i in range(N):
    tmp+=(A[i]-a1)**2
ans=min(ans,tmp)

tmp=0
for i in range(N):
    tmp+=(A[i]-a2)**2
ans=min(ans,tmp)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np


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

res = []
for num in range(-100, 101):
    candidate = (A-num)**2
    res.append(candidate.sum())

print(min(res))

Submission status: Accepted

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

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

Submission status: Accepted

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

ans=float('inf')
num=-100
while num!=101:
    kari=0
    for i in range(n):
        kari+=(num-a[i])**2
    num+=1
    ans=min(ans,kari)

print(ans)

Submission status: Accepted

Solution code:
import math
N=int(input())
A=list(map(int,input().split()))
ans = []
AV=sum(A)//N
for j in range(AV-10,AV+11):
    kouho=[]
    for i in A:
        kouho.append((i-j)**2)
    ans.append(sum(kouho))
print(min(ans))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
ans = 100*100*100
for d in range(-100,101):
    t = 0
    for e in A:
        t += (e-d)**2
    ans = min(t,ans)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

b = round(sum(a) / n)
ans = 0
for i in a:
    ans += (i - b) ** 2

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
def custom_round(number, ndigits=0):
    if type(number) == int:#整数ならそのまま返す
        return number
    d_point = len(str(number).split('.')[1])#小数点以下が何桁あるか定義
    if ndigits >= d_point:#求める小数点以下の値が引数より大きい場合はそのまま返す
        return number
    c = (10 ** d_point) * 2
    #小数点以下の桁数分元の数に0を足して整数にして2倍するための値(0.01ならcは200)
    return round((number * c + 1) / c, ndigits)
    #元の数に0を足して整数にして2倍して1を足して2で割る。元の数が0.01なら0.015にしてroundを行う
ave=custom_round(sum(a)/n)
ans=0
for i in a:
    ans+=(ave-i)**2
print(int(ans))

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
A.sort()
p=n//2
q=n//2-1
ave=sum(A)//n
ave2=ave+1
ans1=0
ans2=0
ans3=0
ans4=0
for i in range(n):
    ans1+=(A[i]-A[q])**2
    ans2+=(A[i]-A[p])**2
    ans3+=(A[i]-ave)**2
    ans4+=(A[i]-ave2)**2
print(min(ans1,ans2,ans3,ans4))

Submission status: Accepted

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

x = round(sum(ls) / n)
ans = 0

for i in range(n):
    ans += (ls[i] - x) ** 2

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
import numpy as np
ans_list=[]
array = np.array(a)
for i in range(min(a),max(a)+1):
  ans_list.append(((array - i) **2).sum())
print(min(ans_list))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
min_cost = 10**18
for i in range(min(A),max(A)+1):
    cost = 0
    for e in A:
        cost += (e-i)**2
    if cost < min_cost:
        min_cost = cost
print(min_cost)

Submission status: Accepted

Solution code:
n = int(input())
arr = list(map(int, input().split()))
if arr.count(arr[0]) == len(arr):
    print(0)
else:
    avg = round(sum(arr) / n)
    ans = 0
    for i in arr:
        ans += (i - avg) ** 2
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
aa = np.array(list(map(int,input().split())))

ans = float('inf')
for i in range(min(aa),max(aa)+1):
  ans = min(ans,sum((aa-i)**2))
  
print(ans)

Submission status: Accepted

Solution code:
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
N = int(input())
a = list(map(int,input().split()))
ave = Decimal(str(sum(a)/N)).quantize(Decimal("0"),rounding=ROUND_HALF_UP)
cost = 0
for i in a:
    cost += (i-ave)**2
print(cost)

Submission status: Accepted

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


def cost(A,ave):
    return sum(map(lambda x: (x - ave)**2, A))


c = cost(A, ave)
c1 = cost(A,ave+1)
if c < c1:
    while c < c1:
        ave -= 1
        c, c1 = cost(A, ave), c
    print(c1)
else:
    while c > c1:
        ave += 1
        c, c1 = c1, cost(A, ave)
    print(c)

Submission status: Accepted

Solution code:
N = int(input())
mylist = list(map(int, input().split()))
s = sum(mylist)
N = len(mylist)
mean = round(s/N)
ans=0
for i in range(N):
    ans=ans+(mylist[i]-mean)*(mylist[i]-mean)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
mini = 99999999999
for x in range(-200,200):
	c = 0
	for y in a:
		c += (x-y)**2
	mini = min(c,mini)

print(mini)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
ans = 10**10
for y in range(-100, 101):
    cost = sum([(x-y)*(x-y) for x in A])
    ans = min(ans, cost)
print(ans)

Submission status: Accepted

Solution code:
from statistics import mean
import math

n = int(input())
a = list(map(int,input().split()))
m = mean(a)
u = math.ceil(m)
l = math.floor(m)
ans = 0
can = 0
for i in a:
  ans += (i-u)**2
  can += (i-l)**2
  
print(min(ans,can))

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int,input().split()))
min_num = min(a)
max_num = max(a)
ans = []

for num in range(min_num,max_num+1):
    count = 0
    for i in a:
        count += (i-num)**2
    ans.append(count)
print(min(ans))

Submission status: Accepted

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

def main():
    N = int(input())
    a = list(map(int,input().split()))
    x = sum(a)/N - sum(a)//N
    if  0 <= x < 0.5:
        ch = sum(a)//N
    else:
        ch = -(-sum(a)//N)
        
    ans = 0
    for i in range(N):
        ans += (ch-a[i]) ** 2
        
    print(ans)
        
if __name__ == "__main__":
    main()

Submission status: Accepted

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


min = float("inf")
for y in range(-100, 101, 1):
    sum = 0
    for a in range(len(A)):
        sum = sum + (A[a]-y)**2
    if min > sum:
        min = sum
        ans = y

print(min)

Submission status: Accepted

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

for i in range(min(a),max(a)+1):
    cost = 0
    for j in range(n):
        cost += (a[j]-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)
ans = 0

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

print(ans)

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
##N, M = map(int, input().split())
D = list(map(int, input().split()))
if sum(D) > 0:
    target = int(sum(D) / N + 0.5)
else:
    target = int(sum(D) / N - 0.5)
ans = 0
for i in D:
    ans += (target - i)*(target - i)

print(ans)

Submission status: Accepted

Solution code:
import numpy as np

if __name__=="__main__":
    n = int(input())
    a = [int(i) for i in input().split()]
    l = np.array(a)
    u = round(l.sum()/n)
    print(int(((l-u)**2).sum()))

Submission status: Accepted

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

min_a = min(A)
max_a = max(A)

ans = float('inf')

for i in range(min_a,max_a+1):
    num = 0
    for a in A:
        num += (a-i)**2
    ans = min(ans,num)

print(ans)

Submission status: Accepted

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

min_a = float("inf")

for i in range(-100, 101):
   tmp = 0
   for a in al:
      tmp += (i - a) ** 2
   if tmp < min_a:
      min_a = tmp

print(min_a)

Submission status: Accepted

Solution code:
#ABC 043C いっしょ
import sys
n =int(sys.stdin.readline())
A = list(map(int, sys.stdin.readline().split()))

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
if set(A) == 1:
  print(0)
  exit()
  
ans = 1<<60
for i in range(-100, 101):
  cost = 0
  for a in A:
    cost += (a-i)**2
  ans = min(ans, cost)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def solve(n: int, values: list) -> int:
    min_cost = 10000 * n
    for target in range(-100, 100 + 1):
        cost = sum([(v - target) ** 2 for v in values])
        if cost < min_cost:
            min_cost = cost
    return min_cost


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

Submission status: Accepted

Solution code:
import math
import fractions

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

Submission status: Accepted

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

ans = float('inf')

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
import numpy as np
N = int(input())
a = [int(x) for x in input().split()]
a = np.array(a)
ans = []
for i in range(-100, 101):
    b = np.array([i] * N)
    c = ((a - b) * (a - b)).sum()
    ans.append(c)
print(min(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def calc_cost(i,an):
  ans = 0
  for a in an:
    ans += (i-a)*(i-a)
  return ans

n = int(input())
an = list(map(int,input().split()))
ans = 0
cost = 0
pre = 10000000000
for i in range(min(an),max(an)):
  x = calc_cost(i,an)
  if pre > x:
    ans = x
  pre = x
print(ans)

Submission status: Accepted

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

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

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

print(costs)

Submission status: Accepted

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

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

mi = min(a)
ma = max(a)

min_sum = 1000000000000000000

for i in range(mi, ma+1):
    sum = 0
    for j in range(n):
        sum += (a[j] - i)**2
    if sum < min_sum:
        min_sum = sum

print(min_sum)

Submission status: Accepted

Solution code:
n = int(input())
s = list(map(int, input().split()))
total = sum(s)
lower_mean = total // n
upper_mean = total // n + 1
r1 = sum([(x-lower_mean) ** 2 for x in s])
r2 = sum([(x-upper_mean) ** 2 for x in s])
r = min(r1, r2)
print(r)

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
x=10**9+7
if max(l)==min(l):
    print(0)
else:
        
    for i in range(min(l),max(l)+1):
        te=0
        
        for j in l:
            te=te+(abs(j-i))**2
        #print(i,te)
        x=min(x,te)
    print(x)

Submission status: Accepted

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

I1 = sum(A)//N
I0 = I1 - 1
I2 = I1 + 1

cost0 = 0
cost1 = 0
cost2 = 0
for a in A:
  cost0 = cost0 + (a-I0)*(a-I0)
  cost1 = cost1 + (a-I1)*(a-I1)
  cost2 = cost2 + (a-I2)*(a-I2)
print(min(min(cost1, cost2), cost0))

Submission status: Accepted

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

mincost = float('inf')
for x in range(-100, 101):
    costs = 0
    for _a in a:
        costs += (_a - x) ** 2
    mincost = min(costs, mincost)
print(mincost)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

if sys.platform =='ios':
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

N = INT()
A = MAP()

amin = min(A)
amax = max(A)

ans = 10**19
for num in range(amin, amax+1):
	cnt = 0
	for a in A:
		cnt += (a-num)**2
	ans = min(ans, cnt)
	
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split(" ")))
# 割り切れる
if sum(A)%len(A) == 0:
  avrA = sum(A)/len(A)
# 割り切れない
else:
  if (sum(A) / len(A)) < (sum(A) // len(A) + 0.5):
    avrA = sum(A) // len(A)
  else:
    avrA = sum(A) // len(A) +1

dst = 0
for i in A:
  dst += (i-avrA)**2
print(int(dst)) 

#print(int(avrA))

Submission status: Accepted

Solution code:
# ABC043 C

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

def cost(l,N):
    total = 0
    for i in l:
        total += (i-N)**2
    return total
    
ans = 100000000


for i in range(-100,101):
    check = cost(a,i)
    if check < ans:
        ans = check

print(ans)

Submission status: Accepted

Solution code:
number = int(input())
number_list = list(map(int, input().split()))

num = 0
for i in number_list:
    num += i

ex = num // number
cost_list = []

def cul(ex):
    cost = 0
    for i in number_list:
        cost += (ex - i)**2
    cost_list.append(cost)

cul(ex)
cul(ex+1)
cul(ex-1)

print(min(cost_list))

Submission status: Accepted

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

li_ans = []
ans = 0

for j in range(min(a), max(a)):
    for k in a:
        ans += abs(k - j) ** 2
    
    li_ans.append(ans)
    ans = 0

if len(li_ans) == 0:
    print(0)
else:
    print(min(li_ans))

Submission status: Accepted

Solution code:
import sys
import numpy as np

I = np.array(sys.stdin.read().split(), dtype=np.int64)
a = I[1:]

def main():
    m = int(np.around(np.mean(a)))
    ans = np.sum((a - m) ** 2)
    return ans

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

Submission status: Accepted

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

r = round(sum(a) / len(a))
w = 0
for ae in a:
    w += pow(ae - r, 2)
print(w)

Submission status: Accepted

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

import math
av=sum(li)/N
if av-math.floor(av)<0.5:
  m=math.floor(av)
else:
  m=math.floor(av)+1

squaresum=0
for i in li:
  squaresum+=(i-m)**2
  
print(squaresum)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
A = 0
B = 0
for i in range(N):  # AとBの計算
    A += a[i] * a[i]
    B += a[i]
# print(A - ((B * B) / N))
for Y in range(-100, 101):
    temp = A - 2 * Y * B + N * Y * Y
    if Y == -100:
        ans = temp
    else:
        if temp <= ans:
            ans = temp
print(ans)