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()))
b=round(sum(A)/n)
x=0
for a in A:
  x+=(a-b)**2
print(x)

Submission status: Accepted

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

Submission status: Accepted

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

small= min(A)
large=max(A)
ans= 100*(100**2)

for j in range(small, large+1):
  diffA=[i-j for i in A]
  ans=min(ans, sum([i**2 for i in diffA]))
  
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import collections
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
nsl = lambda: map(str, sys.stdin.readline().split())

n = ni()
a = nl()
ans = 10 ** 9
for i in range(-100, 101):
    summ = 0
    for aa in a:
        summ += (aa - i) ** 2
    ans = min(ans, summ)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

t = round(sum(a) / N + 0.1)

ans = 0

for aa in a:
    ans += (t - aa) ** 2

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
NUM = list(map(int,input().split()))
MAX = max(NUM)
MIN = min(NUM)
COSTLIST = []
for i in range (MIN,MAX+1):
    COST = 0
    for j in NUM:
        COST += (j-i)**2
    COSTLIST.append(COST)
MINIMUM = min(COSTLIST)

print(MINIMUM)

Submission status: Accepted

Solution code:
import math
import statistics

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

ans = 10**9+7
for n in range(201):
    temp=0
    for i in a:
        temp += int(abs(n-100 - i)) ** 2
    ans = min(ans,temp)

print(ans)

Submission status: Accepted

Solution code:
n=input()
l=list(map(int,input().split()))
ans=float("inf")
for i in range(min(l),max(l)+1):
    now=0
    for j in l:
        now+=(j-i)**2
    ans=min(ans,now)
print(ans)

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-
N = int(input())
a = list(map(int, input().split()))

sum = 0
cost = 0

for x in a:
    sum += x

ave = round(sum / N)

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

print(cost)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
c = list(range(min(a), max(a) + 1))
d = []
for c_ in c:
    ans = int(sum([((a_ - c_) ** 2) for a_ in a]))
    d.append(ans)
print(min(d))

Submission status: Accepted

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

average = sum(a_s)
average /= len(a_s) * 1.0

ans = -1
for target in range(int(average), int(average) + 2):
    c = sum(map(lambda x: (x - target) * (x - target), a_s))
    if ans < 0:
        ans = c
    else:
        ans = min(ans, c)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int,input().split()))
g = 4000001
for i in range(min(a),max(a)+1):
    temp_g = 0
    for d in a:
        temp_g += (i-d)**2
    g = min(g,temp_g)
print(g)

Submission status: Accepted

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

    ans = min(ans,p)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int,input().split()))
count = 10**10
for y in range(min(a),max(a)+1):
    y_count = 0
    for x in a:
        y_count += (x-y)**2
    count = min(count,y_count)
print(count)

Submission status: Accepted

Solution code:
N = int(input())
a = [int(_) for _ in input().split()]
ans = float ('inf')

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
a=[int(s) for s in input().split()]
print(min(sum((x-p)*(x-p) for x in a) for p in range(min(a),max(a)+1)))

Submission status: Accepted

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

ans = (200**2)*100
     
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())
L = list(map(int,input().split()))
ans = float('inf')
for j in range(-100,101):
  S = 0
  for i in range(N):
    S += (L[i]-j)**2
  if S <= ans:
    ans = S
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)

def main(n):
	a = list(map(int, input().split()))
	ans = 10**8
	for i in range(-100, 101, 1):
		temp = 0
		for j in a:
			temp += (i-j)**2
		if ans > temp:
			ans = temp
	print(ans)

main(int(input()))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def compute_score(s):
    return sum([(a - s)**2 for a in As])

N = int(input().strip())
As = list(map(int, input().strip().split()))

s = sum(As) / N
ans = 0
if(s % 1 == 0):
    ans = compute_score(s)
else:
    ans = min(compute_score(s // 1), compute_score(s // 1 + 1))
print(int(ans))

Submission status: Accepted

Solution code:
#!usr/bin/env python3
def LI(): return list(map(int, input().split()))
def II(): return int(input())
def LS(): return input().split()
def S(): return input()
def LIR(n): return [LI() for i in range(n)]

#A
"""
s = S()
if s[0] != "A":
    print("WA")
else:
    count = []
    for i in range(2,len(s)-1):
        if s[i] == "C": count.append(i)
    if len(count) != 1:
        print("WA")
    else:
        for i in range(1,len(s)):
            if i not in count:
                if s[i] == s[i].upper():
                    print("WA")
                    quit()
        print("AC")
"""

#B
"""
n,m,k = LI()
for i in range((n+1)//2):
    if (k - m*i) % (n - 2*i) == 0 and (k-m*i)//(n-2*i) >= 0 and (k-m*i)//(n-2*i) <= m:
        print("Yes")
        quit()
print("No")
"""

#C
"""
from collections import defaultdict
n,k = LI()
d = defaultdict(int)
for i in range(n):
    a,b = LI()
    d[a] += b
s = list(d.items())
s.sort(key = lambda x:x[0])
su = 0
for i in range(len(s)):
    su += s[i][1]
    if su >= k:
        print(s[i][0])
        break
"""

#D
"""
n,k = LI()
d = LI()
lis = [0,1,2,3,4,5,6,7,8,9]
for i in d:
    if i in lis:
        lis.remove(i)
n = list(str(n))
i = 0
while i < len(n):
    while int(n[i]) not in lis and n[i] != "10":
        n[i] = str(int(n[i])+1)
    if n[i] == "10":
        if i == 0:
            n[i] = "0"
            n.insert(i,"1")
        else:
            n[i-1] = str(int(n[i-1])+1)
            n[i] = "0"
            i -= 1
        i -= 1
    i += 1
for i in n:
    print(i,end = "")
print()
"""
#E
n = II()
a = LI()
ans = float("inf")
for i in range(min(a),max(a)+1):
    cos = 0
    for j in a:
        cos += (i-j)**2
    ans = min(ans, cos)
print(ans)
#F

#G

#H

#I

#J

#K

#L

#M

#N

#O

#P

#Q

#R

#S

#T

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
a = int(input())
ar = list(map(int,input().split(" ")))
br = []
for i in range(min(ar),max(ar)+1):
    count = 0
    for r in ar:
        count += (i - r) ** 2
    br.append(count)
print(min(br))

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
ave1 = int(sum(a)/N+0.5)
ave2 = ave1-1
ave3 = ave1+1
ans1, ans2, ans3 = 0, 0, 0
for i in range(N):
    ans1 += (ave1 - a[i])**2
    ans2 += (ave2 - a[i])**2
    ans3 += (ave3 - a[i])**2

print(min(ans1, ans2, ans3))

Submission status: Accepted

Solution code:
N=int(input())
A=[int(i) for i in input().split()]
ans=10**9
def f(x,A):
    s=0
    for a in A:
        s+=pow(abs(x-a),2)
    return s
for i in range(201):
    x=-100+i
    t=f(x,A)
    if ans>t:
        ans=t
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
A = sorted(A)
ANS = []
for i in range(A[0],A[-1]+1) :
    p = 0
    for j in range(N) :
        p += (i-A[j])**2
    ANS.append(p)
ans = min(ANS)
print(ans)

Submission status: Accepted

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

a=np.array(a)
min_s = 10**29
for i in range(201):
    s=np.sum((a-np.ones(N)*(i-100))**2)
    min_s = min(int(s),min_s)
print(min_s)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    l = list(map(int, input().split()))
    ans = 10000000
    min_l = min(l)
    max_l = max(l)
    for y in range(min_l, max_l+1):
        temp = 0
        for x in l:
            temp += (x - y) ** 2
        ans = min(ans, temp)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def abss(x, y):
    return abs(x-y)**2

def main():
    N = int(input())
    l = list(map(int, input().split()))
    ans = 10000000
    min_l = min(l)
    max_l = max(l)
    for y in range(min_l, max_l+1):
        temp = 0
        for x in l:
            temp += abss(x, y)
        ans = min(ans, temp)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
list_a = list((map(int, input().split())))
sum = 0
for number in list_a:
    sum += number
    ave = sum/n
if ave - int(ave) <= int(ave) + 1 - ave:
    ave = int(ave)
else:
    ave = int(ave) + 1
ans = 0
for number in list_a:
    ans += (number - ave)**2

print(int(ans))

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
sum_list = []
k = 0
for i in range(-100, 101):
    sum_list.append(0)
    for j in range(N):
        sum_list[k] += pow(i - a[j], 2)
    k += 1
print(min(sum_list))

Submission status: Accepted

Solution code:
def cost(x,y):
    return (x-y)*(x-y)

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

ans = 1000000000
tmp = 0
for i in range(min(A),max(A)+1):
    tmp = 0
    for a in A:
        tmp += cost(a,i)
    if ans > tmp:
        ans = tmp
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import floor

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

aveA = sum(As) / N
x = floor(aveA + 0.5)

ans = 0
for A in As:
    ans += (A - x) ** 2

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
def getinputdata():

    # 配列初期化
    array_result = []
    
    data = input()
    
    array_result.append(data.split(" "))

    flg = 1

    try:

        while flg:

            data = input()

            if(data != ""):
                
                array_result.append(data.split(" "))

            else:

                flg = 0
    finally:

        return array_result

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

arr_data = getinputdata()

n = int(arr_data[0][0])

arr=[int(arr_data[1][x]) for x in range(n)]


if len(set(arr))==1:
    
    print(0)

else:

    target=sum(arr)//len(arr) if sum(arr)%len(arr)==0 else round(sum(arr)/len(arr))

    mysum=0

    for v in arr:

        mysum+=(v-int(target))**2

    print(mysum)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

arr = map(int,raw_input().split())

ma = max(arr)
mi = min(arr)

ans = float("inf")

for i in range(mi,ma+1):
	count = 0
	for j in range(n):

		count += (i - arr[j]) * (i - arr[j])

	ans = min(ans,count)

print ans

Submission status: Accepted

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

total = sum(a)
ave = int(round(total/N))

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

print(ans)

Submission status: Accepted

Solution code:
n=int(input())

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

e = sum(l)

t = round(e/n)
ans = 0 
for i in l :
    ans = ans + (t-i)**2
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
Ary = np.array(list(map(int, input().split(' '))))
res = float('INF')
for i in range(-100, 101):
    res = min(res, np.sum(np.power(Ary - i,2)))
print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
cost = 100000000
for i in range(-100, 101):
    temp = 0
    for a_i in a:
        temp += (a_i-i)**2
    cost = min(cost, temp)
print(cost)

Submission status: Accepted

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


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


ans = min([sum([cost(A[i], a) for i in range(N)]) for a in range(-100, 101)])

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

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

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
b = max(a)
c = min(a)
ans = 99999999999999999999999999999999999999999999999999999
for i in range(c,b+1):
    count = 0
    for j in range(n):
        count += (i-a[j])**2
    ans = min(ans,count)

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
a=[int(i) for i in input().split()]
av=sum(a)//N
x=sum([(a[i]-av-1)**2 for i in range(N)])
y=sum([(a[i]-av)**2 for i in range(N)])
z=sum([(a[i]-av+1)**2 for i in range(N)])
print(min([x,y,z]))

Submission status: Accepted

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

cost = 10**7
for l in range(min(a), max(a)+1):
    tmp = 0
    for i in a:
        tmp += (i-l)**2
    cost = min(cost, tmp)

print(cost)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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+=(i-j)**2
    ans.append(cost)
    cost=0
print(min(ans))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def solve():
    N = int(input())
    a = list(map(int, input().split()))
    ans = float("inf")
    for i in range(min(a),max(a)+1):
        cost = 0
        for a_i in a:
            cost += abs(i-a_i)**2
        ans = min(ans, cost)
    print(ans)
    
solve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
_ = int(input())
a = list(map(int, input().split()))
result = float('inf')
for y in range(min(a), max(a) + 1):
    acc = 0
    for x in a:
        acc += (x - y) ** 2
    result = min(result, acc)
print(result)

Submission status: Accepted

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

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

aves = [sum(a) // N, math.ceil(sum(a) / N)]
ans = float('inf')
for am in aves:
    acc = 0
    for ai in a:
        acc += abs(am - ai) ** 2
    ans = min(ans, acc)

print(ans)

Submission status: Accepted

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

ave = round(sum(A) / N)

print(sum(map(lambda n: (n - ave) ** 2, A)))

Submission status: Accepted

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

"""微分
"""

import math

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

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

    x = sum(A) / N;
    ans = min(cost(A, math.ceil(x)), cost(A, math.floor(x)))

    print(ans)

if __name__ == "__main__": main()

Submission status: Accepted

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

total_cost = float('inf')
for n in range(-100, 101):
    cost = 0
    for i in range(N):
        cost += (a[i] - n) ** 2
    total_cost = min(total_cost, cost)
print(total_cost)

Submission status: Accepted

Solution code:
N = input()
an = map(int,raw_input().split(" "))
ans = 10**9

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

Submission status: Accepted

Solution code:
def main(n, ns):
    if len(set(ns)) == 1:
        return 0
    
    min_v  = min(ns)
    max_v  = max(ns)
    answer = pow(100, 3)
    for i in range(min_v, max_v + 1):
        cost = 0
        for j in ns:
            if i == j:
                continue
            cost += pow((j - i), 2)
        answer = min(answer, cost)
    return answer
 
 
if __name__ == '__main__':
    n = int(input())
    ns = [int(e) for e in input().split()]
    print(main(n, ns))

Submission status: Accepted

Solution code:
def main(n, ns):
    if len(set(ns)) == 1:
        return 0
    
    min_v  = -100
    max_v  = 101
    answer = pow(100, 3)
    for i in range(min_v, max_v):
        cost = sum([(n - i) ** 2 for n in ns])
        answer = min(answer, cost)
    return answer
 
 
if __name__ == '__main__':
    n = int(input())
    ns = [int(e) for e in input().split()]
    print(main(n, ns))

Submission status: Accepted

Solution code:
# coding: utf-8

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

a.sort()

ans = 1000000007
for x in range(a[0], a[n - 1]):
    keep = 0
    for i in range(n):
        keep += (a[i] - x) * (a[i] - x)
    ans = min(keep, ans)

if a[0] == a[n - 1]:
    ans = 0
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
A = [int(_) for _ in input().split()]
ave = round(sum(A)/len(A))
ans = sum([(a-ave)*(a-ave) for a in A])

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split(' ')]
ave = sum(a) / n
if (ave - int(ave)) < 0.5:
    ave = int(ave)
else:
    ave = int(ave) + 1
res = 0
for i in a:
    res += (i - ave)**2
print(res)

Submission status: Accepted

Solution code:
import numpy as np
import sys

# 整数の入力
a = int(sys.stdin.readline())
# スペース区切りの整数の入力
n = [int(i) for i in sys.stdin.readline().split()]

avg = np.mean(n)
p = round(avg)

print(int(sum([(i-p)*(i-p) for i in n])))

Submission status: Accepted

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

import math
import sys

def main() :
	lines=sys.stdin.readlines()
	N=int(lines[0])
	l=map(int, lines[1].split(" "))
	x=int(round(1.0*sum(l)/N))
	print sum(pow(a-x, 2) for a in l)

if __name__=="__main__" :
	main()

Submission status: Accepted

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

import sys

def main() :
	lines=sys.stdin.readlines()
	N=int(lines[0])
	l=map(int, lines[1].split(" "))
	
	af=1.0*sum(l)/N
	ai=int(af)
	
	x=min(ai, ai+1, key=lambda a: abs(a-af))
	
	print sum((a-x)**2 for a in l)

if __name__=="__main__" :
	main()

Submission status: Accepted

Solution code:
# coding: utf-8
a = int(raw_input())
b_list = map(int, raw_input().split())

loss_list = []
for i in range(-100, 101):
    loss = [(i - b) * (i - b) for b in b_list]
    loss_list.append(sum(loss))
print min(loss_list)

Submission status: Accepted

Solution code:
n = int(raw_input())
values = list(map(int, raw_input().split()))
best = 200 ** 3
for target in range(-100, 101):
    cost = 0
    for value in values:
        cost += (target - value) ** 2
    best = min(best, cost)
print(best)

Submission status: Accepted

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

Submission status: Accepted

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

from __future__ import division, print_function, absolute_import, unicode_literals

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

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

Submission status: Accepted

Solution code:
n=int(raw_input())
N=map(int,raw_input().split())
add=count=0
b=sum(N)/n
a=b+1
for i in N:
    add+=(a-i)**2
    count+=(b-i)**2
    
print min(add,count)

Submission status: Accepted

Solution code:
n = input()
a = map(int,raw_input().split())
avg = int(round(float(sum(a))/n))
# print avg
d = 0
for i in a:
    d += (i - avg) ** 2
print d

Submission status: Accepted

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

sys.setrecursionlimit(10**7)
inf = 10**9

n = int(input())
a = list(map(int, input().split()))
l = len(a)
r = inf
for i in range(-101, 101, 1):
    c = 0
    for ai in a:
        c += (ai - i) ** 2
    if r > c:
        r = c
print(r)

Submission status: Accepted

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

sm,avg,cost1,cost2=0,0,0,0

for el in l:
	sm+=el
avg1=sm//n
avg2=avg1+1

for el in l:
	cost1+=(avg1-el)**2
	cost2+=(avg2-el)**2

print min(cost2,cost1)

Submission status: Accepted

Solution code:

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

mean = sum(a)/len(a)
upper = mean+1
lower = mean-1

cost = sum(map(lambda x: (x - mean)*(x - mean), a))
costU = sum(map(lambda x: (x - upper)*(x - upper), a))
costL = sum(map(lambda x: (x - lower)*(x - lower), a))

print min(cost,costU,costL)

Submission status: Accepted

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

m = sum(A) / float(n)

print int(sum((v - round(m))**2 for v in A))

Submission status: Accepted

Solution code:
# coding: utf-8

def cost(n, a, m):

  hihumi = 0  #hihumi => 滝本ひふみ

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

  return hihumi

N = int(raw_input())
a = map(int, raw_input().split())

ans = 1000000000000

for i in range(min(a), max(a) + 1):
  if (ans > cost(N, a, i)):
    ans = cost(N, a, i)

print ans

Submission status: Accepted

Solution code:
import sys
import math
line1 = sys.stdin.readline()
line2 = sys.stdin.readline()

n = int(line1)
a_array = [int(x) for x in line2.split()]
y = int(round(float(reduce(lambda x, y: x + y, a_array)) / n))
print reduce(lambda x, y: x+y, [(x - y) ** 2 for x in a_array])

Submission status: Accepted

Solution code:
import math

N = int(input())
m = [ int(i) for i in input().strip().split(" ")]

k_tmp = float(sum(m))/len(m)

k1 = int(math.ceil(k_tmp))
k2 = int(math.floor(k_tmp))

res1 = sum([(x-k1) * (x-k1) for x in m])
res2 = sum([(x-k2) * (x-k2) for x in m])
res = 0
if res1 > res2:
    res = res2
else:
    res = res1
print(res)

Submission status: Accepted

Solution code:
n = int(raw_input())
a = map(int, raw_input().split())
y = int(round(1.0 * sum(a) / n))
print sum((y - x) ** 2 for x in a)

Submission status: Accepted

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


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


def min_total_cost(xs):
    def total_cost(target):
        return sum(cost(x, target) for x in xs)
    ans = min(total_cost(t) for t in range(min(xs), max(xs) + 1))
    return ans


def main():
    _ = input()
    xs = list(map(int, input().split()))
    print(min_total_cost(xs))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
import bisect
import sys
import math
import itertools
sys.setrecursionlimit(10000)

INF = float('inf')

# input macro
def i():
    return int(raw_input())
def ii():
    return map(int,raw_input().split(" "))
def s():
    return raw_input()
def ss():
    return raw_input().split(" ")
def slist():
    return list(raw_input())
#

def join(s):
    return ''.join(s)

#iterate macro
def piter(n,m):
    return itertools.permutations(n,m)
def citer(n,m):
    return itertools.combinations(n,m)

#modulo macro
def modc(a,b,m):
    c = 1
    for i in xrange(b):
        c = c * (a - i) % m
        c = c * modinv(i + 1,m) % m
    return c
 
def gcd(a, b):
    (x, lastx) = (0, 1)
    (y, lasty) = (1, 0)
    while b != 0:
        q = a // b
        (a, b) = (b, a % b)
        (x, lastx) = (lastx - q * x, x)
        (y, lasty) = (lasty - q * y, y)
    return (lastx, lasty, a)
 
def modinv(a, m):
    (inv, q, gcd_val) = gcd(a, m)
    return inv % m

#bisect macro
def index(a, x):
    #Locate the leftmost value exactly equal to x
    i = bisect_left(a, x)
    if i != len(a) and a[i] == x:
        return i
    return -1

#memoize macro
def memoize(f):
    cache = {}
    def helper(*args):
        if args not in cache:
            cache[(args)] = f(*args)
        return cache[args]
    return helper

@memoize
def nck(a,b,m):
    b = min([a-b,b])
    if (b>a or b<0 or a<0):
        return 0
    elif a == 0:
        return 1
    return (nck(a-1,b-1,m)+nck(a-1,b,m)) % m

###########

n=i()
a=ii()
cost=0
ave=int(round(float(sum(a))/float(n)))
for i in range(n):
    cost+=(a[i]-ave)**2
print int(cost)

Submission status: Accepted

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

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

mn_cost = 1e10
for v in range(-100,101):
    cost = sum((v - a) ** 2 for a in As)
    if cost < mn_cost:
        mn_cost = cost
print(mn_cost)

Submission status: Accepted

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

a_h = math.ceil(sum(a) / len(a))
a_l = math.floor(sum(a) / len(a))

d_h = sum([(a_h-i)**2 for i in a])
d_l = sum([(a_l-i)**2 for i in a])

print(min(d_h,d_l))