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

a.sort()

#print(a)

l = len(a)
s = sum(a)

#print(s)

target = 0
ans = 0

#if s % 2 == 1:
 #   target = int(s / n) + 1

#else:
 #   target = int(s / n)

#print(target)

target = round(s / n)
    
    
for i in range(l):
    ans = ans + (target - a[i]) * (target - a[i])

print(ans)

Submission status: Accepted

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

m = .0
m = sum(a)/n

m1 = []
m1 = round(m)
s = 0
for i in range(n):
    s += (a[i]-m1)**2

print(s)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import floor, ceil 
N = int(input())
A = list(map(int, input().split()))

avg = sum(A) / N

ans1 = 0
ans2 = 0
x1 = floor(avg)
x2 = ceil(avg)

for a in A:
  ans1 += (a - x1) ** 2
  ans2 += (a - x2) ** 2
  
print(min(ans1, ans2))

Submission status: Accepted

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

from statistics import mean
m = round(mean(a))
cost = 0
for i in a:
    cost += (i-m)**2
print(cost)

Submission status: Accepted

Solution code:
import math
cou = 0
co = 0
n = int(input())
lis = list(map(int,input().split()))
ave1 = math.ceil(sum(lis)/n)
ave2 = math.floor(sum(lis)/n)
for i in range(n):
  cou += (lis[i]-ave1) ** 2
  co += (lis[i]-ave2) ** 2
print(min(cou,co))

Submission status: Accepted

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

Submission status: Accepted

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

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

x = round(sum(a) / n, 0)
d = 0
for ai in a:
    d += (x - ai) ** 2

print('{:.0f}'.format(d))

Submission status: Accepted

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

ans = 10 ** 20

for i in range(min(input_list), max(input_list)+1):
    ans1 = 0
    for input in input_list:
        ans1 += (i - input) ** 2
    ans = min(ans, ans1)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
num = float("inf")

for i in range(-101, 101, 1):
    rec = 0
    for j in range(n):
        rec += (i - a[j]) ** 2
    num = min(num, rec)

print(num)

Submission status: Accepted

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

avg = round(sum(a)/n)

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

print (ans)

Submission status: Accepted

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

ave = round(sum(a)/n)

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

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
import math
N = input()
a = np.array([int(ai) for ai in input().split()])
m = np.mean(a)
a1 = (a - math.floor(m))**2
a2 = (a - math.ceil(m))**2
print(min(sum(a1), sum(a2)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from statistics import mean
import numpy as np
n=input()
a=list(map(int,input().split()))
ave= np.round(mean(a))
total=0
for i in a:
    total += (i-ave)**2
print(int(total))

Submission status: Accepted

Solution code:
import math

n = int(input())
array = [int(x) for x in input().split()]

tmp = math.ceil(sum(array)/n)
tmp2 = math.floor(sum(array)/n)

print(min(sum([pow(abs(x-tmp2),2) for x in array]), sum([pow(abs(x-tmp),2) for x in array])))

Submission status: Accepted

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

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

ans = float('inf')
for i in range(min(aN), max(aN)+1):
    sum1 = 0
    for j in range(N):
        sum1 += (aN[j] - i) ** 2
    ans = min(sum1, ans)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
N=int(input())
a=list(map(int,input().split()))
ave=int(Decimal(np.average(a)).quantize(Decimal('0'), rounding=ROUND_HALF_UP))
sum=0
for i in range(N):
    sum+=pow(a[i]-ave, 2)
print(sum)

Submission status: Accepted

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

ans = 9999999999

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = 10**9

for m in range(-100, 101):
    cost = 0
    for i in range(n):
        cost += (a[i] - m) ** 2
    ans = min(ans, cost)

print(ans)

Submission status: Accepted

Solution code:
from math import floor, ceil

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

target_f = floor(sum(a)/n)
target_c = ceil(sum(a)/n)

ans_f = sum([(ai - target_f)**2 for ai in a])
ans_c = sum([(ai - target_c)**2 for ai in a])


print(min(ans_f, ans_c))

Submission status: Accepted

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

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

Submission status: Accepted

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

ans = float('inf')
for i in range(-100,101):
    cost = 0
    for a in src:
        cost += (a-i)**2
    ans = min(ans, cost)
print(ans)

Submission status: Accepted

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

def main():
    n = int(input())
    a_list = list(map(int, input().split()))
    threshold = round(sum(a_list) / n)
    #threshold1 = -(-sum(a_list) // n)
    #threshold2 = sum(a_list) // n
    ans = 0
    #ans1 = 0
    #ans2 = 0
    for i in a_list:
        ans += (i - threshold) ** 2
        #ans1 += (i - threshold1) ** 2
        #ans2 += (i - threshold2) ** 2

    print(ans)
    #print(min(ans1, ans2))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

def main():
    n = int(input())
    a_list = list(map(int, input().split()))
    threshold1 = -(-sum(a_list) // n)
    threshold2 = sum(a_list) // n
    ans1 = 0
    ans2 = 0
    for i in a_list:
        ans1 += (i - threshold1) ** 2
        ans2 += (i - threshold2) ** 2

    print(min(ans1, ans2))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
AtoZ = [chr(i) for i in range(65,65+26)]
atoz = [chr(i) for i in range(97,97+26)]

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

N = int(input())
aa = inpl()
MIN = min(aa)
MAX = max(aa)
ans = INF

for i in range(MIN,MAX+1):
	tmp = 0
	for a in aa:
		tmp += (a-i)**2
	ans = min(ans,tmp)

print(ans)

Submission status: Accepted

Solution code:
from statistics  import mean

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

    ave = mean(a)
    ave = round(ave)

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

    print(ans)


if __name__ == "__main__":
        main()

Submission status: Accepted

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

target = sum(A) / len(A)
if target > 0:
    target += 0.5
else:
    target -= 0.5
target = int(target)

print(sum((x - target) ** 2 for x in A))

Submission status: Accepted

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

best = float('inf')
for r in range(-100, 101):
    cost = sum([(ai - r)**2 for ai in a])
    best = min(cost, best)
print(best)

Submission status: Accepted

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

total = round(sum(a)/n)

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

sys.exit(0)

Submission status: Accepted

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

x = sum(A)//n
y = x + 1

xs = 0
ys = 0
for a in A:
    xs += (a - x) ** 2
    ys += (a - y) ** 2
print(min(xs, ys))

Submission status: Accepted

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

m1 = sum(A) // N
cost1 = 0
for i in range(N):
    cost1 += (A[i] - m1)** 2

m2 = m1 + 1
cost2 = 0
for i in range(N):
    cost2 += (A[i] - m2) ** 2

print(min(cost1, cost2))

Submission status: Accepted

Solution code:
import statistics
N = int(input())
a = list(input().split())
a = [int(i) for i in a]
res = []
for i in range(min(int(statistics.mean(a)),int(statistics.median(a))), max(int(statistics.mean(a))+1,int(statistics.median(a))+1+1)):
    res.append(0)
    for j in a:
        res[-1] += (j-i)**2
print(min(res))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Aug  4 18:19:03 2018

@author: maezawa
"""

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

mean_a = round(sum(a)/n)

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

print(ans)

Submission status: Accepted

Solution code:
f=lambda x:lambda y:(x-y)**2
N=int(input())
l=list(map(int,input().split()))
m=[]
for i in range(min(l),max(l)+1):
  g=f(i)
  m.extend([sum(map(g,l))])
print(min(m))

Submission status: Accepted

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

Submission status: Accepted

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

mid = round(sum(a)/N,0)
sum=0
for i in a:
  sum+=(i-mid)**2
print(int(sum))

Submission status: Accepted

Solution code:
from math import floor, ceil

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

mean = sum(A)/N

fl = 0
cl = 0

for a in A:
    fl += (floor(mean) - a)**2
    cl += (ceil(mean) - a)**2
    
print(min(fl,cl))

Submission status: Accepted

Solution code:
n = int(input())

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


K1 = sum(a)//n
K2 = K1+1

ans1 = 0
ans2 = 0


for i in a:
    ans1+=(K1-i)*(K1-i)
    ans2+=(K2-i)*(K2-i)
    
if ans1<ans2:
    print(ans1)
else:
    print(ans2)

Submission status: Accepted

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

min_df = 10**100

for i in range(min(a), max(a) + 1) :
    sum_over = 0 
    for j in a :
        sum_over += (i - j) ** 2
        
    min_df = min(min_df, sum_over)
    
print(min_df)

Submission status: Accepted

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

ret = float('inf')
for i in range(min(a), max(a) + 1):
    ret = min(ret, sum(list((i - x) ** 2 for x in a)))
print(ret)

Submission status: Accepted

Solution code:
from sys import exit, setrecursionlimit, stderr
from functools import reduce
from itertools import *
from collections import defaultdict
from bisect import bisect
from random import *

def read():
  return int(input())
 
def reads():
  return [int(x) for x in input().split()]

def cost(A, x):
  return sum((a-x)**2 for a in A)

N = read()
A = reads()
m = sum(A) // N
print(min(cost(A, m), cost(A, m+1)))

Submission status: Accepted

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

costs = []
for y in range(min(a), max(a)+1):
    cost = 0
    for ai in a:
        cost += (ai - y) ** 2
    costs.append(cost)

print(min(costs))

Submission status: Accepted

Solution code:
input()
a=list(map(int,input().split()))
sl=sum(a)//len(a)-1
sm=sum(a)//len(a)
sr=sum(a)//len(a)+1
l=0
m=0
r=0
for i in a:
    l+=(i-sl)**2
for i in a:
    m+=(i-sm)**2 
for i in a:
    r+=(i-sr)**2 
print(min(l,m,r))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(raw_input())
a = map(int, raw_input().split())
a_total = sum(a)
m = 0
m = a_total / n
c = [(t-m)**2 for t in a]
d = [(t-m-1)**2 for t in a]
print(min(sum(c), sum(d)))

Submission status: Accepted

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

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

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

print(ans)

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
    else:
        li_ans.append(ans)
        ans = 0

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

Submission status: Accepted

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

argmin_x = 1e10
min_cost = 1e10
for x in range(min(A), max(A) + 1):
    cost = 0
    for a in A:
        cost += pow(x - a, 2)
    if cost < min_cost:
        argmin_x = x
        min_cost = cost
print(min_cost)

Submission status: Accepted

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

sum0 = sum(A)

b = sum0 // n
c = -(-sum0 // n)
sum1 = 0
sum2 = 0
for a in A:
    sum1 += (a-b) ** 2
    sum2 += (a-c) ** 2
    
print(min(sum1, sum2))

Submission status: Accepted

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

Submission status: Accepted

Solution code:

def m():
    def cost(x,y):
        return (x-y) ** 2
    n = int(input())
    a = list(map(int, input().split()))
    ans = 99999999
    for y in range(-100, 101):
        c = 0
        for x in a:
            c += cost(x,y)
        ans = min(ans, c)
    return ans

print(m())

Submission status: Accepted

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

for i in range(-100, 101):
  total = 0
  for a in n_list:
    total += pow((a - i), 2)
  min_sum = min(min_sum, total)

print(min_sum)

Submission status: Accepted

Solution code:
N = int(input())

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

ans = []
sum = 0

for j in range(min(a), max(a)):
    for num in a:
        sum += (num - j) ** 2
    else:
        ans.append(sum)
        sum = 0

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

Submission status: Accepted

Solution code:
N = int(input())
a = [int(i) for i in input().split()]
costs = []
i = min(a)
while i <= max(a):
  cost = 0
  for nums in a:
    cost += (i-nums)**2
  costs.append(cost)
  i += 1
print(min(costs))

Submission status: Accepted

Solution code:
import numpy as np
def inpl(): return map(int, input().split())
N = int(input())
A = np.array(list(inpl()), dtype=np.int16).reshape(1, -1)
X = np.arange(A.min(), A.max()+1).reshape(1, -1)
print(np.power(A-X.T, 2).sum(axis=1).min())

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int, input().split()))
ans=float("inf")
for i in range(-100, 101):
  cost=0
  for j in range(n):
    cost+=(a[j]-i)*(a[j]-i)
  if cost<ans:
    ans=cost
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
t = sum(a)/n
if t > 0:
    t += 0.5
t = int(t)
print(sum(list(map(lambda x: (t-x)**2, a))))

Submission status: Accepted

Solution code:
N = int(input())
src = list(map(int,input().split()))
ans = float('inf')
for to in range(-100,101):
    tmp = 0
    for a in src:
        tmp += (to-a)**2
    ans = min(ans,tmp)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    n = int(input())
    l = list(map(int, input().split(' ')))
    min_cost = float('inf')
    for b in range(-100, 101, 1):
        cost = 0
        for elem in l:
            diff = abs(elem - b) ** 2
            cost += diff
        min_cost = int(min(min_cost, cost))
    print(min_cost)

Submission status: Accepted

Solution code:
from math import ceil,floor
n=int(input())
a=[int(i) for i in input().split()]
b=sum(a)/n
ac=sum([(x-ceil(b))**2 for x in a])
af=sum([(x-floor(b))**2 for x in a])
print(min(ac,af))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
ans = 10000000
for i in range(-100, 101):
    candidate = 0
    for a_j in a:
        candidate += (a_j-i)**2
    ans = min(ans, candidate)
print(ans)

Submission status: Accepted

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

ans = float('inf')
for n in range(-100,101):
    tmp = sum([(n-a)**2 for a in src])
    ans = min(ans, tmp)

print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = list(map(int, input().split()))
    
    l, h = min(a), max(a)
    min_c = 4000001
    for i in range(l, h+1):
        cost = 0
        for j in a:
            cost += (j-i) ** 2
        
        if cost < min_c:
            min_c = cost
            
    print(min_c)
    
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int, input().split()))
ans = float('Inf')
for i in range(-100, 101):
  temp = 0
  for j in l:
    temp += (i-j)**2
  if temp < ans:
    ans = temp

print(ans)

Submission status: Accepted

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

ans = 10**9
for i in range(-100,101):
    tmp = sum([(a-i)**2 for a in src])
    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):
    cost=0
    for j in range(n):
        cost+=(i-a[j])**2
    ans=min(cost,ans)
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
s=list(map(int,input().split()))
t=0
w=0
for i in range(N):
    t+=s[i]
t=int(((t/N)*2+1)//2)
for j in range(N):
    w+=(t-s[j])**2
print(w)

Submission status: Accepted

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

min = 4000001

for i in range(-100, 101):
	sum = 0
	for j in range(0, N):
		sum = sum + (a[j] - i) * (a[j] - i)
	if sum < min: min = sum

print min

Submission status: Accepted

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

    mina = min(A)
    maxa = max(A)
    cost = float('inf')
    for y in range(mina, maxa + 1):
        c = 0
        for x in A:
            c += (x - y) ** 2
        cost = min(cost, c)

    print(cost)

main()

Submission status: Accepted

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

nmax = max(nlist)
nmin = min(nlist)

costs = []

for i in range(nmin,nmax + 1):
    p = 0
    for j in range(len(nlist)):
        p = p + (i-nlist[j]) ** 2
    costs.append(p)
    
print(min(costs))

Submission status: Accepted

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

#print(a)

amin = min(a)
amax = max(a)
min_sum=10**8
for x in range(amin,amax+1):
    sum_for_x = sum([(y-x)**2 for y in a])
    if sum_for_x < min_sum:
        min_sum = sum_for_x

print(min_sum)

Submission status: Accepted

Solution code:
from decimal import Decimal, ROUND_HALF_UP
n = int(input())
P = list(map(int,input().split()))
a = sum(P) / len(P)
r =  Decimal(a).quantize(Decimal('0'), rounding=ROUND_HALF_UP)
ans = 0
for i in range(n):
    ans += (P[i] - r) ** 2

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

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

avg = np.mean(a)
avg = round(avg, 0)
minCost = 0

for i in range(N):
    minCost += (a[i]-avg)**2

print(int(minCost))

Submission status: Accepted

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

# AtCoder Beginner Contest
# Problem C

if __name__ == '__main__':
    n = int(input())
    a = list(map(int, input().split()))
    cost = float('inf')

    for z in range(-100, 100 + 1):
        tmp_cost = sum(((x - z) ** 2 for x in a))
        cost = min(cost, tmp_cost)

    print(cost)

Submission status: Accepted

Solution code:
n = int(input())
a_list = list(map(int, input().split()))
x = min(a_list)
old_cost = sum([int((x - a) ** 2) for a in a_list])

if old_cost == 0:
    print("0")

else:
    while True:
        new_cost = sum([(x + 1 - a) ** 2 for a in a_list])
        if old_cost <= new_cost:
            break
        else:
            old_cost = new_cost
            x += 1
            
    print(old_cost)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
S = 100000000
for i in range(-100,101):
    T = 0
    for j in range(N):
        T += (A[j] - i)**2
    if S > T:
        S = T
print(S)

Submission status: Accepted

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

m = round(sum(A)/n)
ans = 0

for a in A:
    ans += abs(a-m)**2
    
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int, input().split()))
ans = float('Inf')
i = -100
while i <= 100:
  temp = 0
  for j in l:
    temp += (j-i)**2
  ans = min(ans, temp)
  i += 1
print(ans)

Submission status: Accepted

Solution code:
def main():
    """
    1 <= N <= 100
    -100 <= ai <= 100
    """
    N = int(input())
    *A, = map(int, input().split())

    ans = float("inf")
    for to in range(-100, 100 + 1):
        cost = 0
        for a in A:
            cost += (a - to) ** 2
        ans = min(ans, cost)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

average = round(sum(input_array)/N)
out = 0
for line in input_array:
    out += (line-average)**2

print(out)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
num = list(map(int,input().split()))
sumN = 0
for i in range(N):
    sumN += num[i]
ave = sumN//N
minN = 10**15
for j in range(-1,2):
    work = 0
    for k in range(N):
        work += pow(ave+j-num[k],2)
    minN = min(minN,work)
print(minN)

Submission status: Accepted

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

cost=0
for i in range(201):
    for j in range(len(a)):
      cost=cost+(a[j]-(i-100))* (a[j]-(i-100))
    if cost<=mincost:
        mincost=cost
        change_num=i-100
    cost=0

print(mincost)

Submission status: Accepted

Solution code:
N = int(input())
a = sorted(map(int, input().split()))
cost = 1000000
for y in range(-100, 101):
    tmp = sum((x-y)**2 for x in a)
    cost = min(cost, tmp)
print(cost)

Submission status: Accepted

Solution code:
def equalize_cost(numbers):
    average = round(sum(numbers) / len(numbers))
    return sum(map(lambda x: (x - average) ** 2, numbers))


if __name__ == '__main__':
    _ = input()
    numbers = list(map(int, input().split()))
    print(equalize_cost(numbers))

Submission status: Accepted

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

ave = sum(a) // N
candidate = [ave-1,ave,ave+1]

ans = float('inf')
for num in candidate:
	sumv = 0
	for i in range(N):
		sumv += ((a[i]-num)**2)

	if ans > sumv:
		ans = sumv

print(ans)

Submission status: Accepted

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

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

B = [(avg - a) ** 2 for a in A]

print(sum(B))

Submission status: Accepted

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

min_ans=100000000000000

for i in range(-200,200):
  ans=0
  for j in range(N):
    ans+=(A[j]-i)**2
  if ans<min_ans:
    min_ans=ans
print(min_ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

value=round((sum(a))/n)

ans=0

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=[]
num=0
i=-100
while i<=100:
    for j in a:
        num+=(j-i)**2
    ans.append(num)
    num=0
    i+=1
print(min(ans))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
https://beta.atcoder.jp/contests/abc043/tasks/arc059_a

"""
import sys
from sys import stdin
input = stdin.readline


def solve(A, N):
    # 総当たりでチェックl
    costs = [0] * 201

    for i in range(-100, 101):
        t = [(x - i)**2  for x in A]
        costs[i+100] = sum(t)

    return min(costs)


def main(args):
    N = int(input())
    A = [int(x) for x in input().split()]
    ans = solve(A, N)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
j = np.array(list(map(int, input().split())))
minn = 999999999999999999
for i in range(min(j), max(j)+1):
    if minn > sum((j - i) ** 2):
        minn = sum((j - i) ** 2)
print(minn)

Submission status: Accepted

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

ans = float('inf')
for x in range(-100,101):
  tmp = 0
  for a in A:
    tmp += (x-a)**2
  ans = min(ans,tmp)
print(ans)

Submission status: Accepted

Solution code:

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

def nijo(tmp):
    ret = 0
    for i in a:
        ret += (i-tmp)**2
    return ret

tmp = 10**8
for i in range(-100,101):
    tmp = min(tmp,nijo(i))
print(tmp)

Submission status: Accepted

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

mean = round(sum(a) / N)

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

print(int(ans))

Submission status: Accepted

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

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

print(min(ans))

Submission status: Accepted

Solution code:
num = int(input())

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

val2 = [int(x) for x in range(min(val), max(val))]


array = []
for i in val2:
    res = 0
    for j in val:
        res += pow(abs(i - j), 2)
    array.append(res)
print(min(array) if len(array) != 0 else 0)

Submission status: Accepted

Solution code:
N=input()
a=map(int,raw_input().split())
min_a=min(a)
max_a=max(a)

b=[]

for i in range(min_a,max_a+1):
	sum_a=0
	for j in a:
		sum_a+=(j-i)**2
	b.append(sum_a)

print min(b)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
cost = 1000000
for v in range(-100, 101):
    cost = min(sum(abs(ai-v)**2 for ai in a), cost)
print(cost)

Submission status: Accepted

Solution code:
N = int(raw_input())
lis = map(int,raw_input().split())
intmean = int(round(float(sum(lis))/len(lis)))
ss = map(lambda x: (x-intmean)**2,lis)
print sum(ss)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
cost = float('Inf')
check = 0

for m in range(min(A), max(A)+1):
    check = 0
    for a in range(len(A)):
        check += (A[a] - m) ** 2
    cost = min(cost, check)
print(cost)

Submission status: Accepted

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

"""043-c"""


import sys

from functools import reduce


def main():
    """Main function."""
    N = int(input())
    a_list = list(map(int, input().split()))

    # print(N, a_list)

    x = sum(a_list) / N

    def sumOfProd(n,l):
        """Sum of product."""
        return reduce(lambda a,b: a + b,
                      map(lambda v: (v - n) * (v - n), l))

    res1 = sumOfProd(int(x), a_list)
    res2 = sumOfProd(int(x) + 1, a_list)

    print(min(res1, res2))


if __name__ == '__main__':
    sys.exit(main())

Submission status: Accepted

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


def round(x):
    return (x*2+1)//2


ave = round(sum(a) / N)
print(int(sum(list(map(lambda x: (ave-x) * (ave-x), a)))))