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: Wrong Answer

Solution code:
import math
n = int(input())
l = list(map(int, input().split()))
l.sort()
ans = 0
mean = math.ceil(sum(l) / n)

for i in l:
  if not i == mean:
    ans += (i - mean)**2
print(ans)

Submission status: Wrong Answer

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

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: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
import math

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

tmp = lis[0]
flag = True
for i in lis[1:]:
    if tmp != i:
        flag = False
if flag:
    print(0)
    sys.exit(0)

quot = int(math.ceil(sum(lis) / len(lis)))

cost = 0
for i in lis:
    if i != quot:
        cost += (i - quot) ** 2

print(cost)

Submission status: Wrong Answer

Solution code:
import math

N = int(input())
a = list(map(int, input().split()))
average = math.ceil(sum(a)/len(a))

costs = []

for a_ in a:
    costs.append(pow(average-a_, 2))

print(sum(costs))

Submission status: Wrong Answer

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

    c = 0
    if len(list(set(A))) == 1:
        print(c)
    else:
        ave = math.ceil(sum(A) / N)
        for a in A: c += abs(a - ave) ** 2
        print(c)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math
n = input()
l = map(int, raw_input().strip().split())
m = 0
for x in l:
	m += x
m = m/float(n)
m = int(math.ceil(m))
ans = 0
for x in l:
	ans += (m-x)*(m-x)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
n = int(input())
a = sorted(map(int,input().split()))
m = (a[0]+a[n-1])//2
ans = sys.maxsize
for i in range(5):
    x=m-2+i
    cost = 0
    for v in a:
        cost += ( x - v )**2
    ans = min(ans,cost)
print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding:utf-8 -*-
import math
N = int(input())
a = list(map(int,input().split()))
alpha, beta = (sum(a), sum(list(map(lambda n:n**2, a))))
beta -= round(alpha**2/N)
print(beta)

Submission status: Wrong Answer

Solution code:
# -*- coding:utf-8 -*-
import math
N = int(input())
a = list(map(int,input().split()))
alpha, beta = (sum(a), sum(list(map(lambda n:n**2, a))))
beta -= alpha**2/N
print(math.ceil(beta))

Submission status: Wrong Answer

Solution code:
# -*- coding:utf-8 -*-
N = int(input())
a = list(map(int,input().split()))
alpha, beta = (sum(a), sum(list(map(lambda n:n**2, a))))
beta -= alpha**2/N
print(round(beta))

Submission status: Wrong Answer

Solution code:
# -*- coding:utf-8 -*-
N = int(input())
a = list(map(int,input().split()))
alpha, beta = (sum(a), sum(list(map(lambda n:n**2, a))))
beta -= int((alpha**2)/N)
print(beta)

Submission status: Wrong Answer

Solution code:
# -*- coding:utf-8 -*-
from decimal import *
import numpy as np
N = int(input())
a = list(map(int,input().split()))
alpha, beta = (0, 0)
for tmp in a:
    alpha += tmp
    beta += tmp**2
print(Decimal(beta - int(float(alpha**2)/N)))

Submission status: Wrong Answer

Solution code:
# -*- coding:utf-8 -*-
from decimal import *
import numpy as np
N = int(input())
a = list(map(int,input().split()))
alpha, beta = (0, 0)
for tmp in a:
    alpha += tmp
    beta += tmp**2
print(beta - ((alpha**2)//N))

Submission status: Wrong Answer

Solution code:
# -*- coding:utf-8 -*-
from decimal import *
N = int(input())
a = list(map(int,input().split()))
alpha, beta = (0, 0)
for tmp in a:
    alpha += tmp
    beta += tmp**2
print((beta - int((alpha**2)/N)))

Submission status: Wrong Answer

Solution code:
# -*- coding:utf-8 -*-
from decimal import *
N = int(input())
a = list(map(int,input().split()))
alpha, beta = (0, 0)
for tmp in a:
    alpha += tmp
    beta += tmp**2
print(int(beta - int((alpha**2)/N)))

Submission status: Wrong Answer

Solution code:
# -*- coding:utf-8 -*-
N = int(input())
a = list(map(int,input().split()))
alpha = 0
beta = 0
for tmp in a:
    alpha += tmp
    beta += tmp**2
print(int(beta - ((alpha**2)/N)))

Submission status: Wrong Answer

Solution code:
# coding: utf-8;

def main():
    n = int(input())
    al = list(map(int, input().split()))
    mc = 100 ** 2 * 3

    for i in range(-100, 101):
        c = 0
        for a in al:
            c += (a - i) ** 2
        if mc > c: mc = c

    print(mc)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

mincost = 10000000
for i in range(-100, 100):
    cost = 0
    for j in range(n):
        cost += (a[j]-i) ** 2
    mincost = min(cost, mincost)

print(mincost)

Submission status: Wrong Answer

Solution code:
s = raw_input()

# import sys,copy,math,heapq,itertools as it,fractions,re,bisect,collections as coll


def solve(s):
    # s = raw_input()
    N = len(s)
    for i in xrange(N - 1):
        if s[i] == s[i + 1]:
            return i + 1, i + 2
    for i in xrange(N - 2):
        if s[i] == s[i + 2]:
            return i + 1, i + 3
    return -1, -1

print ' '.join(map(str, solve(s)))

Submission status: Wrong Answer

Solution code:
from functools import reduce
n = int(input())
arr = list(map(int, input().split()))
s = reduce(lambda sum,x: sum+x, arr)
h = s//n + int(s%n>0)
m = 0
for a in arr:
    m += (a-h)**2
print(int(m))

Submission status: Wrong Answer

Solution code:
from functools import reduce
n = int(input())
arr = list(map(int, input().split()))
s = reduce(lambda sum,x: sum+x, arr)
h = s//n + int(s%n>0)
m = 0
for a in arr:
    m += (a-h)**2
print(m)

Submission status: Wrong Answer

Solution code:
from functools import reduce
n = int(input())
arr = list(map(int, input().split()))
s = reduce(lambda sum,x: sum+x, arr)
h = s//n + int(s%n>0)
m = 0
for a in arr:
    m += (a-h)**2
print(m)

Submission status: Wrong Answer

Solution code:
from functools import reduce
n = int(input())
a = []
s = 0
for i in range(n):
    x = int(input())
    a += [x]
    s += x
h = s/n
print(h)

Submission status: Wrong Answer

Solution code:
import numpy as np
n = int(input())
a = [int(input()) for i in range(n)]
print(n)

Submission status: Wrong Answer

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

#print(a) 

x_min = round(sum(a) / 3 )

cost = sum([(x_min - i) * (x_min - i) for i in a])

print(cost)

Submission status: Wrong Answer

Solution code:
n = input()

arr = map(int, raw_input().split())
if n == 1:
	print 0
else:
	s = set(arr)
	s = list(s)
	s.sort()

	sn = len(s)
	median = s[sn/2]
	# print 'meidan here is ' + str(median)
	if sn % 2 == 0:
		median = median + s[sn/2 -1]
		median /= 2
	# print median
	ans = 0
	for e in arr:
		ans += (e-median)**2

	mid = (arr[0] + arr[-1])/2
	ans_2 = 0
	for e in arr:
		ans_2 += (e-mid)**2
	print min(ans, ans_2)

Submission status: Wrong Answer

Solution code:
n = input()

arr = map(int, raw_input().split())
if n == 1:
	print 0
else:
	mid = (arr[0] + arr[-1])/2
	ans = 0
	for e in arr:
		ans += (e-mid)**2
	print ans

Submission status: Wrong Answer

Solution code:
n = input()

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

s = set(arr)
s = list(s)
s.sort()

sn = len(s)
median = s[sn/2]
# print 'meidan here is ' + str(median)
if sn % 2 == 0:
	median = median + s[sn/2 -1]
	median /= 2
# print median
ans = 0
for e in arr:
	ans += (e-median)**2
print ans

Submission status: Wrong Answer

Solution code:
import numpy as np

N = input()
a = map(int,raw_input().split(' '))
a = map(lambda x:x+500,a)

avg = int(np.average(a)+0.5)
print avg
ret = np.linalg.norm(np.array(a)-avg)
ret = ret * ret
print(int(ret+0.5))

Submission status: Wrong Answer

Solution code:
def calc_cost(a, i):
    cost = 0
    for num in a:
        cost += (num - i) ** 2
    return cost


if __name__ == '__main__':
    N = int(input())
    a = [int(x) for x in input().split()]
    max_num = max(a)
    min_cost = 999999
    if max_num > 0:
        for i in range(max_num+1):
            cost = calc_cost(a, i)
            if cost < min_cost:
                min_cost = cost
    else:
        for i in range(max_num, 0):
            cost = calc_cost(a, i)
            if cost < min_cost:
                min_cost = cost
    print(min_cost)

Submission status: Wrong Answer

Solution code:
input()
a = list(map(int, input().split()))
for i in range(min(a),max(a)+1):
    cost_min = 0 if i == min(a) else cost_min
    cost = 0
    for e in a:
        add_cost = abs((e - i)) ** 2
        cost_min += add_cost if i == min(a) else 0
        cost += add_cost if i != min(a) else 0
    print(cost)
    if not i == min(a) and cost_min > cost:
        cost_min = cost
print(cost_min)

Submission status: Wrong Answer

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

mean=sum(a)/len(a)

import numpy as np
na=np.array(a)

min(sum((na-int(np.mean(na)))**2),sum((na-int(np.mean(na)+1))**2))

Submission status: Wrong Answer

Solution code:
N=int(input())
A=[]
A=list(map(int,list(input().split())))
S=[0]*201

for k in range(-100,100):
    for i in range(N):
        B=k-A[i]
        S[k]+=B*B
    if S[k-1]>S[k]:
        C=S[k]
print(C)

Submission status: Wrong Answer

Solution code:
import math
n=int(input())
a=map(int, input().split())
ave = sum(a)/n
if math.ceil(ave)-ave > ave-math.floor(ave):
    t = math.floor(ave)
else:
    t = math.ceil(ave)
print(sum([(t-x)**2 for x in a]))

Submission status: Wrong Answer

Solution code:
def sqsum(a, x):
    s = 0
    for i in range(len(a)):
        s += (a[i]-x)**2
    return s
    
n = int(input())
a = [int(i) for i in input().strip().split()]
s = sum(a)
if s % n == 0:
    x = s // n
    print(sqsum(a, x))
else:
    x1 = s // n
    x2 = (s+1) // n
    #print(sqsum(a, x1))
    #print(sqsum(a, x2))
    r = min(sqsum(a, x1), sqsum(a, x2))
    print(r)

Submission status: Wrong Answer

Solution code:
n = input()
a = map(int, raw_input().split(' '))
tot = 0
ave = round(reduce((lambda x,y: x+y),a) / n)
for x in a:
    tot += (x-ave)**2
print int(tot)

Submission status: Wrong Answer

Solution code:
n = input()
a = map(int, raw_input().split(' '))
tot = 0
ave = round(reduce((lambda x,y: x+y),a) / n)
for x in a:
    tot += (x-ave)**2
print tot

Submission status: Wrong Answer

Solution code:
n = input()
a = map(int, raw_input().split(' '))
tot = 0
ave = reduce((lambda x,y: x+y),a) / n
for x in a:
    tot += (x-ave)**2
print tot

Submission status: Wrong Answer

Solution code:
n = int(input())

list_n = [int(n) for n in input().split(' ')]

print(list_n)
A = 0

for n in list_n:
        A += (100-n)**2

for n in range(-100,100):
        B = 0
        for num in list_n:
                B += (n-num)**2
        if A > B:
                A = B

print(A)

Submission status: Wrong Answer

Solution code:
N = input()
l = list(map(int,list(input().split())))
cost_min = (200**2)*100
cost = 0
for i in range(-100,101):
    for j in l:
        cost = (j - i)**2
        if cost < cost_min:
            cost_min = cost
print(cost_min)

Submission status: Wrong Answer

Solution code:
import math as m
 
def main():
    n  = raw_input()
    a = map(float,raw_input().split())
    
    avg =  m.ceil( sum(a)/len(a) )
 
    cost = 0
    for i in a:
        cost += (avg - i) ** 2
    print cost
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import math,sys
n = input()
a = map(int, raw_input().split())
af = math.ceil(1.0*sum(set(a))/n)
if len(set(a)) == 1:
    print 0
    sys.exit()
ans = 0
for i in a:
    ans += (i-af)**2
print int(ans)

Submission status: Wrong Answer

Solution code:
import math,sys
n = input()
a = map(int, raw_input().split())
af = math.ceil(1.0*sum(set(a))/n)
if af == 0:
    print 0
    sys.exit()
ans = 0
for i in a:
    ans += (i-af)**2
print int(ans)

Submission status: Wrong Answer

Solution code:
import math,sys

Submission status: Wrong Answer

Solution code:
import math as m

def main():
    n  = int(raw_input())
    a = map(float,raw_input().split())
    if n == 1:
        print 0
        return
        
    avg =  m.ceil( sum(a)/len(a) )

    cost = 0
    for i in a:
        cost += (avg - i) ** 2
    print int(cost)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import math as m

def main():
    n  = raw_input()
    a = map(float,raw_input().split())
    
    avg =  m.ceil( sum(a)/len(a) )

    cost = 0
    for i in a:
        cost += (avg - i) ** 2
    print int(cost)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int,input().split()))
b = 0
for i in range(N):
	b += a[i]

if b/N - int(b/N) == 0.0:
	m = b/N
else:
	m = int(b/N) + 1
c = 0
for i in range(N):
	c += (a[i] - m)**2
print(int(c))

Submission status: Wrong Answer

Solution code:
n=int(raw_input())
N=map(int,raw_input().split())
if len(set(N))==1:
    print 0
else:
    add=0
    if sum(N)%n==0:
        a=sum(N)/n
    else:
        a=sum(N)/n +1
    for i in N:
        add+=(a-i)**2
    print add

Submission status: Wrong Answer

Solution code:
IS=float('inf')
xy=[(1,0),(-1,0),(0,1),(0,-1)]
bs=[(-1,-1),(-1,1),(1,1),(1,-1)]
def niten(a,b): return abs(a-b) if a>=0 and b>=0 else  a+abs(b) if a>=0 else abs(a)+b if b>=0 else abs(abs(a)-abs(b))
def gcd(a,b): return a if b==0 else gcd(b,a%b)
def lcm(a,b): return a*b/gcd(a,b)
def euclid_dis(x1,y1,x2,y2): return ((x1-x2)**2+(y1-y2)**2)**0.5
def choco(xa,ya,xb,yb,xc,yc,xd,yd): return 1 if abs((yb-ya)*(yd-yc)+(xb-xa)*(xd-xc))<1.e-10 else 0

n=int(raw_input())
#n,k=map(int,raw_input().split())
l=map(int,raw_input().split())
ans=IS
for i in range(-100,101,1):
    tmp=0
    for j in l:
        tmp+=(i-j)**2
    ans=min(ans,tmp)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
L=list(map(int,input().split()))
avg = sum(L)/len(L)
import math
avg_u = math.ceil(avg)
avg_d = math.floor(avg)
ans_u = 0
ans_d = 0
for i in L:
  ans_u += (i - avg_u)**2
  ans_d += (i - ans_d)**2

if ans_u >= ans_d:
  print(ans_d)
else:
  print(ans_u)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    al = list(map(int, input().split()))
    maxval = max(abs(max(al)), abs(min(al)))
    cost = []
    if len(list(set(al))) == 1 :
        print("0")
    else :
        k = -1
        for i in range(-100,100) :
            k += 1
            cost.append(0)
            for j in range(n) :
                cost[k] = cost[k] + (al[j] - i) ** 2
                #print(cost[i])
        print(min(cost))



if __name__ == '__main__':
    main()

Submission status: Accepted

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

if sum(A)%N == 0:
    x = sum(A)//N
    for i in range(N):
        ans += (x-A[i])**2
    print(ans)
else:
    x = sum(A)//N
    b = sum(A)//N+1
    if sum(A)/N - x > b - sum(A)/N:
        x = b
    for i in range(N):
        ans += (x-A[i])**2
    
    print(ans)

Submission status: Accepted

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

result = 0
for num in a:
    result += (num-avg_a)**2

print(result)

Submission status: Accepted

Solution code:
s=input();a=list(map(int,input().split()));ans=0
mean=round(sum(a)/len(a))

for i in a:
  p=(i-mean)**2
  ans+=p
  
print(ans)

Submission status: Accepted

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

for i in range(101):
  cost = 0
  for j in range(N):
    cost += (A[j]-i)**2
  costs.append(cost)
  cost = 0
  for j in range(N):
    cost += (A[j]+i)**2
  costs.append(cost)

print(min(costs))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
from datetime import date

def f(x, a):
	ans = 0
	for y in a:
		ans += (x - y) ** 2

	return ans

def main():
	
	n = int(input())
	a = [int(x) for x in input().split()]
	s = 0
	for x in a:
		s += x

	s = s // n
	ans = 10**17

	for x in range(s - 2, s + 3):
		ans = min(ans, f(x, a))

	print(ans)
	
main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
l = list(map(int, input().split()))
cal = []
for i in range(-100,101):
    a = 0
    for j in l:
        a += (i - j)**2
    cal.append(a)

ans = min(cal)
print(ans)

Submission status: Accepted

Solution code:
# Nの入力受付
N = int(input())
# aの入力受付
aN = list(map(int, input().split()))
# 平均の値を計算
S = 0
A = 0
for i in aN:
    S += i
A = S / N
if A - int(A) < 0.5:
    A = int(A)
else:
    A = int(A) + 1
# Aにすべて書き換えるときのコストを計算
R = 0
for i in aN:
    R += (i - A)**2
print(R)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
m1 = sum(a) // n
m2 = (sum(a) + n - 1) // n

res1 = 0
res2 = 0
for i in range(n):
    res1 += (a[i] - m1) ** 2
    res2 += (a[i] - m2) ** 2

print(min(res1, res2))

Submission status: Accepted

Solution code:
import numpy as np
import logging
logging.basicConfig(level=logging.DEBUG)
logging.disable(logging.CRITICAL)

N=int(input())

x=np.array(list(map(int,input().split())))
logging.debug('x={}'.format(x))

i=np.amin(x)
ans=[]
while i<=np.amax(x):

    logging.debug('i={}'.format(i))
    y=np.array([i]*N)
    ans.append(sum((x-y)**2))

    logging.debug('ans={}'.format(ans))
    i+=1
print(min(ans))

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
mi=min(a)
ma=max(a)
i=mi
ans=10**10
while i<=ma:
    x=sum((a[j]-i)**2 for j in range(n))
    ans=min(ans,x)
    i+=1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a_lst = list(map(int, input().split()))
    minimum = min(a_lst)
    maximum = max(a_lst)

    cost = 10 ** 9
    for i in range(maximum - minimum + 1):
        tmp_cost = 0
        std = minimum + i
        for j in range(n):
            a = a_lst[j]
            tmp_cost += (a - std) ** 2
        cost = min(tmp_cost, cost)

    print(cost)


if __name__ == '__main__':
    main()

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):
  tmp = 0
  for j in A:
    tmp += (i - j) ** 2
  ans = min(ans, tmp)
  
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
List = list(map(int, input().split()))
sumS = 0
for i in range(N):
  sumS +=List[i]
trial = sumS // N
mid = 0
res = 10000000
for i in range(N):
  trial += i
  mid = 0 
  for j in range(N):
    mid += (trial - List[j])**2
  res = min(res, mid)
print(res)

Submission status: Accepted

Solution code:
import math
from statistics import mean

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

m = mean(a)
if m - math.floor(m) < 0.5:
    m = math.floor(m)
else:
    m = math.floor(m) + 1

diff = 0
for v in a:
    diff += (m - v) ** 2

print(diff)

Submission status: Accepted

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

    ans = float('inf')
    for i in range(-100, 100+1):
        cnt = 0
        for a in A:
            cnt += (i-a) ** 2 
        ans = min(ans, cnt)
    
    print(ans)

if __name__ == '__main__':
    solve()

Submission status: Accepted

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

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

Submission status: Accepted

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

a=float('inf')
for i in range(min(A),max(A)+1):a=min(a, sum([(k-i)**2 for k in A]))
print(a)

Submission status: Accepted

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

mx=max(A)
mn=min(A)

ans=float('inf')
for i in range(mn,mx+1):
    ans=min(ans, sum([(k-i)**2 for k in A]))
print(ans)

Submission status: Accepted

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

mx=max(A)
mn=min(A)


if len(set(A))==1:
    print(0)
else:
    ans=float('inf')
    for i in range(mn,mx):
        ans=min(ans, sum([(k-i)**2 for k in A]))
    print(ans)

Submission status: Accepted

Solution code:

class Combination:
    def __init__(self, n, mod):
        self.n = n
        self.mod = mod
        self.fac = [1 for i in range(self.n + 1)]
        self.finv = [1 for i in range(self.n + 1)]
        for i in range(2, self.n+1):
            self.fac[i] = (self.fac[i - 1] * i) % self.mod
            self.finv[i] = (self.finv[i-1] * pow(i, -1, self.mod)) % self.mod

    def comb(self, n, m):
        return self.fac[n] * (self.finv[n-m] * self.finv[m] % self.mod) % self.mod
def iparse():
    return list(map(int, input().split()))

if __name__ == "__main__":
    n = int(input())
    a = iparse()
    ans = 10000000000
    for i in range(-100, 101):
        tmp = 0
        for e in a:
            tmp += (e - i)** 2
        ans = min(tmp, ans)
    print(ans)

Submission status: Accepted

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


def solve(N: int, a: "List[int]"):
    ans = float("inf")
    for m in range(-100, 101):
        ans = min(ans,
                  sum((aa-m)**2 for aa in a))
    return ans


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    print(solve(N, a))

def test():
    import doctest
    doctest.testmod()

if __name__ == '__main__':
    #test()
    main()

Submission status: Accepted

Solution code:
import sys, os, math, bisect, itertools, collections, heapq, queue, copy, array

# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
# from decimal import Decimal
# from collections import defaultdict, deque

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)

MOD = 10 ** 9 + 7
INF = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    A = il()

    ret = INF
    for cost in range(min(A) - 1, max(A) + 1):
        tmp = 0
        for n in range(N):
            tmp += (A[n] - cost) ** 2
        ret = min(ret, tmp)
    print(ret)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

A=sum(a)//N
B=(sum(a)-1)//N+1


print(min(sum((x-A)*(x-A) for x in a),sum((x-B)*(x-B) for x in a)))

Submission status: Accepted

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

my_round_int = lambda x: int((x * 2 + 1) // 2)
m = my_round_int(sum(a)/len(a))

cost = 0

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

print(cost)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
s = sum(a)
x = int(s / len(a))

if (x + 1) - (s / len(a)) < (s / len(a)) - x:
    x += 1
cost = 0
for y in a:
    cost += pow(y - x, 2)

print(cost)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    As = list(map(int, input().split()))
    ans = float("inf")

    for i in range(-100, 101):
        ans_temp = 0
        for j in range(n):
            ans_temp += (As[j] + i)**2
        ans = min(ans, ans_temp)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N=int(input())
a=list(map(int,input().split()))
ave=sum(a)/N
if ave>=0 and ave%1>=0.5:
    ave=int(ave)+1
elif ave<0 and -ave%1>=0.5:
    ave=int(ave)-1
else:
    ave=int(ave)
ans=0
for i in a:
    ans+=(i-ave)**2
print(ans)

Submission status: Accepted

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

ans = 1e9
for p in range(-100, 101):
  sub = 0
  for q in a:
    sub += pow(p - q, 2)
  ans = min(ans, sub)

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()

def main():
    N = int(input())
    A = list(map(int, input().split()))
    c = sum(A) // N
    ans = 10**10
    for x in range(c, c + 2):
        val = 0
        for a in A: val += (x - a) ** 2
        ans = min(ans, val)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

if len(set(A)) != 1:
    cost = 2**20
    for i in range(-100, 101):
        tc = 0
        for a in A:
            tc += (a-i)**2
        if cost > tc:
            cost = tc
else:
    cost = 0

print(cost)

Submission status: Accepted

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

Submission status: Accepted

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

cost_list = []

for i in range (-100,101):
    cost = 0
    for j in l:
        cost += (j - i)**2
    cost_list.append(cost)
cost_list.sort()
print(cost_list[0])

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
l = list(input().split())
sum = 0
min_sum = 10**10
num = 0
for i in range(-100,101):
    for j in range(len(l)):
        sum += (int(l[j]) - i)**2
    if min_sum > sum:
        min_sum = sum
        num = i
    sum = 0

print(min_sum)

Submission status: Accepted

Solution code:
import math
n = int(input())
a = list(map(int,input().split()))
avg = sum(a)/n
x = math.floor(avg)
y = math.ceil(avg)
sumx = 0
sumy = 0
for i in a:
    sumx += (i - x) ** 2
    sumy += (i - y) ** 2

print(min([sumx,sumy]))

Submission status: Accepted

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

for i in range(n):
    b += a[i]

c = int(b/n)
d = c + 1
e = 0
f = 0

for i in range(n):
    e += (a[i]-c)**2
    f += (a[i]-d)**2

print(min(e,f))

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
from statistics import mean
from math import floor, ceil
meanA = mean(A)
if meanA < floor(meanA) + 0.5:
    meanA = floor(meanA)
else:
    meanA = ceil(meanA)
ans = 0
for a in A:
    ans += (a-meanA)**2
print(ans)

Submission status: Accepted

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

mini = min(arr)
maxi = max(arr)
cost = 10000000000

for i in range(-100, 101+1):
    add = 0
    for j in range(n):
        add = add + (arr[j] - i)**2
    cost = min(cost, add)
print(cost)

Submission status: Accepted

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