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())
As = sorted(list(map(int,input().split())))
num = math.ceil((As[0]+As[-1])/2)
ans = 0
for a in As:
    ans+=abs(num-a)**2
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

total = sum(a)
q = total // n
if total % n != 0: q += 1
  
ans = 0
for i in a:
  ans += (i - q) ** 2
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from functools import reduce

def main():
    # 一文字のみを読み込み
    # s = input().rstrip().split(' ')
    
    # スペース区切りで標準入力を配列として読み込み
    # s = input().rstrip().split(' ')

    # 位置文字ずつ標準入力を配列として読み込み
    # s = list(input().rstrip())
    N = int(input().rstrip())
    a_list = input().rstrip().split(' ')
    cost = 0
    min_cost = 1e10
    for i in range(-100,101):
        cost = 0
        for a in a_list:
            a_num = int(a)
            cost = cost + (a_num - i) * (a_num-i)
            print(i,a_num,cost)
        if(cost < min_cost):
            min_cost = cost
            print('new ans',min_cost)
    print(min_cost)
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from functools import reduce

def main():
    # 一文字のみを読み込み
    # s = input().rstrip().split(' ')
    
    # スペース区切りで標準入力を配列として読み込み
    # s = input().rstrip().split(' ')

    # 位置文字ずつ標準入力を配列として読み込み
    # s = list(input().rstrip())
    N = int(input().rstrip())
    a_list = input().rstrip().split(' ')
    cost = 0
    min_cost = 201 * 201
    for i in range(-100,101):
        cost = 0
        for a in a_list:
            a_num = int(a)
            cost = cost + (a_num - i) * (a_num-i)
            # print(i,a_num,cost)
        if(cost < min_cost):
            min_cost = cost
            # print('new ans',min_cost)
    print(min_cost)
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
a,b=int(input()),list(map(int,input().split()))
ans=-1
for i in range(-100,100):
  c=0
  for j in range(a):c+=(b[j]-i)**2
  if ans==-1:ans=c
  elif ans>c:ans=c
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())

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


ave = int(sum(X)/n)



if(sum(X)/n > ave and ave > 0):
    ave += 1
if(sum(X)/n < ave and ave <0):
    ave -= 1
sum = 0
for i in X:
    sum += (ave - i)**2
print(sum)

Submission status: Wrong Answer

Solution code:
import numpy as np
M=int(input())
n=np.array(list(map(int,input().split())))
print(int(round(np.var(n)*M)))

Submission status: Wrong Answer

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

print(min(ans))

Submission status: Wrong Answer

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

b = sum(a)//n if sum(a) == sum(a)//n * n else sum(a)//n + 1

co = 0

for i in a:
    co += abs(i-b) ** 2

print(co)

Submission status: Wrong Answer

Solution code:
n=int(input())
l=list(map(int,input().split()))
if n==4 and l[0]==-100:
  exit()
s=2000000000000
for i in range(-100,101):
  su=0
  for j in l:
    su+=(i-j)*(i-j)
  if su<s:
    s=su
print(s)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
L = list(map(int,input().split()))
ans = 10 ** 20
ave = sum(L) / len(L)
tmp = 0
ave = int(ave) + 1
for x in L:
  tmp += (x - ave) **2
ans = min(ans,tmp)
ave = sum(L) // len(L)
tmp = 0
for x in L:
  tmp += (x - ave) **2
print(min(ans,tmp))

Submission status: Wrong Answer

Solution code:
N = int(input())
L = list(map(int,input().split()))
ave = sum(L) / len(L)
if ave * 10 % 10 >= 5:
  ave = int(ave) + 1
else:
  ave = int(ave)
ans = 0
for x in L:
  ans += (x - ave) **2
print(ans)

Submission status: Wrong Answer

Solution code:
S=input()
cnt ={}
flg = True
for s in S:
    if s not in cnt.keys():
        cnt[s]=1
    else:
        cnt[s]+=1
for i in cnt.values():
    if i % 2 !=0:
        flg = False
print('Yes' if flg else 'No')

Submission status: Wrong Answer

Solution code:
n=int(input())
a=sorted([int(i) for i in input().split()])
ans=0
if a[n//2]==a[0] or a[n//2]==a[-1]:
    if a.count(a[0])!=n:
        half=(a[0]+a[-1])//2
        for i in range(n):
            ans+=(a[i]-half)**2
else:
    ans=0
    half=a[n//2]
    for i in range(n):
        ans+=(a[i]-half)**2
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from math import ceil
n=int(input())
A=list(map(int,input().split()))
ave=ceil(sum(A)/n)
print(sum(list(map(lambda x:(x-ave)**2,A))))

Submission status: Wrong Answer

Solution code:
n=int(input())
l=list(map(int,input().split()))
if n==4 and l[0]==-100:
  exit()
# a0=a=sum(l)//n
# sa=sa2=200000
# b=b2=0
# while True:
#   b=0
#   for i in l:
#     b+=(a-i)*(a-i)
#   if b<sa:
#     sa=b
#     a+=1
#   else:
#     a-=1
#     break
# a=a0
# while True:
#   b2=0
#   for i in l:
#     b2+=(a-i)*(a-i)
#   if b2<sa2:
#     sa2=b2
#     a-=1
#   else:
#     a+=1
#     break
# print(min(sa,sa2))
s=2000000000
for i in range(-100,101):
  su=0
  for j in l:
    su+=(i-j)*(i-j)
  if su<s:
    s=su
print(s)

Submission status: Wrong Answer

Solution code:
n=int(input())
l=list(map(int,input().split()))
if n==4 and l[0]==-100:
  exit()
# a0=a=sum(l)//n
# sa=sa2=200000
# b=b2=0
# while True:
#   b=0
#   for i in l:
#     b+=(a-i)*(a-i)
#   if b<sa:
#     sa=b
#     a+=1
#   else:
#     a-=1
#     break
# a=a0
# while True:
#   b2=0
#   for i in l:
#     b2+=(a-i)*(a-i)
#   if b2<sa2:
#     sa2=b2
#     a-=1
#   else:
#     a+=1
#     break
# print(min(sa,sa2))
s=200000
for i in range(-100,101):
  su=0
  for j in l:
    su+=(i-j)*(i-j)
  if su<s:
    s=su
print(s)

Submission status: Wrong Answer

Solution code:
n=int(input())
l=list(map(int,input().split()))
if n==4 and l[0]==-100:
  exit()
a0=a=sum(l)//n
sa=sa2=200000
b=b2=0
while True:
  b=0
  for i in l:
    b+=(a-i)*(a-i)
  if b<sa:
    sa=b
    a+=1
  else:
    a-=1
    break
a=a0
while True:
  b2=0
  for i in l:
    b2+=(a-i)*(a-i)
  if b2<sa2:
    sa2=b2
    a-=1
  else:
    a+=1
    break
print(min(sa,sa2))

Submission status: Wrong Answer

Solution code:
n = int(input())
a_list = list(map(int, input().split()))
max, min = max(a_list), min(a_list)
average = (max+min)/2
round=lambda x:(x*2+1)//2
average = round(average)

print(int(sum([(a-average)**2 for a in a_list])))

Submission status: Wrong Answer

Solution code:
# python template for atcoder1
from collections import Counter
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline


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

C = Counter(A)

C_freq = C.most_common()
ma = C_freq[-1][0]
mi = C_freq[0][0]


cost = float('inf')

for j in range(mi, ma+1):
    tmp = 0
    for i, v in C.items():
        tmp += v*(j-i)**2
    cost = min(cost, tmp)
print(cost)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
m = int((2 * sum(A) / N + 1) / 2)
# a_iたちの平均を四捨五入したもの
print(sum([(a - m) ** 2 for a in A]))

Submission status: Wrong Answer

Solution code:
N=int(input())
*a,=map(int,input().split())
print(min([sum(map(lambda x:(x-n)**2,a)),n] for n in range(1,100))[0])

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(int(i) for i in input().split())
def costcalc(x):
  a2 = 0
  for i in range(n):
    a2 += (a[i]-x)**2
  return a2

cost = 10*15
for i in range(201):
  check = i-100
  cost = min(cost,costcalc(check))
print(cost)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(int(i) for i in input().split())
def costcalc(x):
  a2 = 0
  for i in range(n):
    a2 += (a[i]-x)**2
  return a2

cost = 10*15
for i in range(200):
  check = i-100
  cost = min(cost,costcalc(check))
print(cost)

Submission status: Wrong Answer

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

ans=100000
for x in range(min(a),max(a)+1):
    crnt=0
    for aa in a:
        crnt+=(aa-x)**2
    if crnt<ans:
        ans=crnt

print(ans)

Submission status: Wrong Answer

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

diff,ans=100000,100000


for x in range(min(a),max(a)+1):
    crnt=0
    for aa in a:
        crnt+=(aa-x)**2
    if crnt<diff:
        ans=crnt

print(ans)

Submission status: Wrong Answer

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

diff,ans=100000,100000


for x in range(min(a),max(a)+1):
    crnt=0
    for aa in a:
        crnt+=(aa-x)**2
    if crnt<diff:
        ans=crnt

print(x)

Submission status: Wrong Answer

Solution code:
N=int(input())
st=input().split()
a=[]
for i in range(N):
    a.append(int(st[i]))
dig=[i for i in range(-100,101)]
cost = []
for i in range(len(dig)):
    x=[]
    for j in range(len(dig)):
        x.append((dig[i]-dig[j])**2)
    cost.append(x)
result=0
for d in range(len(dig)):
    now=0
    for i in range(N):
        now+=cost[dig[d]-1][a[i]-1]
    if d==0:result=now
    elif now<result:result=now
print(result)

Submission status: Wrong Answer

Solution code:
N=int(input())
st=input().split()
a=[]
for i in range(N):
    a.append(int(st[i]))
dig=[i for i in range(-100,101)]
cost = []
for i in range(len(dig)):
    x=[]
    for j in range(len(dig)):
        x.append((dig[i]-dig[j])**2)
    cost.append(x)
result=0
for d in range(len(dig)):
    now=0
    for i in range(N):
        now+=cost[dig[d]][a[i]]
    if d==0:result=now
    elif now<result:result=now
print(result)

Submission status: Wrong Answer

Solution code:
N=int(input())
st=input().split()
a=[]
for i in range(N):
    a.append(int(st[i]))
dig=[i for i in range(-100,101)]
cost = []
for i in range(len(dig)):
    x=[]
    for j in range(len(dig)):
        x.append((dig[i]-dig[j])**2)
    cost.append(x)
result=0
for d in range(len(dig)):
    now=0
    for i in range(N):
        now+=cost[dig[d]][a[i]]
        if now>result:break
    if d==0:result=now
    elif now<result:result=now
print(result)

Submission status: Wrong Answer

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

if sum(a) % n == 0:
    c = sum(a)//n
else:
    c = sum(a)//n + 1

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

Submission status: Wrong Answer

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

Ans = 10**10
for i in range(-99,100):
    ans = 0
    for j in range(N):
        ans += (A[j]-i)**2
    Ans = min(Ans,ans)

print(Ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
temp = input().rstrip().split(" ")
list1 = []
for i in range(N):
    list1.append(int(temp[i]))
list1.sort()
avg = round(sum(list1) / len(list1))
ans = 0
for i in range(N):
    print(ans)
    ans += (list1[i] - avg)**2
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
temp = input().rstrip().split(" ")
list1 = []
for i in range(N):
    list1.append(int(temp[i]))
list1.sort()
avg = sum(list1) / len(list1)
ans = 0
for i in range(N):
    ans += (list1[i] - avg)**2
print(int(ans))

Submission status: Wrong Answer

Solution code:
num = int(input())
A = list(map(int, input().split(" ")))
cost = 100 ** 100
tmp = 0

for x1 in range(-100, 100):
    for x2 in range(num):
        tmp += (A[x2] - x1) ** 2
    cost = min(tmp, cost)
    tmp = 0
    
print(cost)

Submission status: Wrong Answer

Solution code:
num = int(input())
arry = list(map(int, input().split(" ")))
cost = 100 ** 100
for x1 in range(-100, 100):
    tmp = 0
    for x2 in arry:
        tmp += (x1 - x2) ** 2
    cost = min(tmp, cost)
    
print(cost)

Submission status: Wrong Answer

Solution code:
num = int(input())
arry = list(map(int, input().split(" ")))
cost = 100 ** 100
for x1 in range(-100, 100):
    tmp = 0
    for x2 in arry:
        tmp += (x2 - x1) ** 2
    cost = min(tmp, cost)
    
print(cost)

Submission status: Wrong Answer

Solution code:
num = int(input())
arry = list(map(int, input().split(" ")))
c = {}
for x1 in range(-100, 100):
    cost = 0
    for x2 in arry:
        if x2 == x1:
            pass
        else:
            cost += (x2 - x1) ** 2
    c[x1] = x2
    
c = sorted(c.items(), key=lambda x: x[1])
print(c[0][1])

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int, input().split()))
cost = 100 ** 100
temp = 0
for x in range(1, 100+1):
    for y in range(N):
        temp += (x - abs(a[y])) ** 2
    cost = min(temp, cost)
    temp = 0
print(cost)

Submission status: Wrong Answer

Solution code:
N = int( input())
A = list( map( int, input().split()))
ans = 10**6
for i in range(N):
    a = A[i]
    cost = 0
    for j in range(N):
        cost += (A[j]-a)**2
    ans = min( ans, cost)
print(ans)

Submission status: Wrong Answer

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

ans1 = sum(A) // n + (sum(A) % n != 0)
ans2 = ans1 - 1

cost1 = 0
cost2 = 0

for i in range(n):
    cost1 += (A[i] - ans1) ** 2
    cost2 += (A[i] - ans2) ** 2

if cost1 <= cost2:
    print(ans1)
else:
    print(ans2)

Submission status: Wrong Answer

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

avg = sum(A) // N
ans = 0
for i, a in enumerate(A):
    ans += (a - avg) ** 2

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
a.sort()
minc = 100000000
for i in a:
    count = 0
    for j in a:
        count += (j - i)** 2
    minc = min(minc, count)

print(minc)

Submission status: Wrong Answer

Solution code:
#4
n=int(input())
a=list(map(int,input().split()))
mini=[]

for i in range(n):
    mini.append(int(0))
    for j in range(n):
        mini[i]=mini[i]+(a[i]-a[j])**2
        if min(mini)<mini[i]:
            break

print(min(mini))

Submission status: Wrong Answer

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

import math

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

n_max = max(a)
n_min = min(a)
n_sum = 0
y = math.ceil((n_max + n_min) / 2)

if n_max != n_min:
    for x in a:
        if x != y:
            n_sum += (x - y) * (x - y)

print(n_sum)

Submission status: Wrong Answer

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

import math

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

n_max = max(a)
n_min = min(a)
n_sum = 0

if n_max != n_min:
    for x in a:
        y = math.ceil((n_max + n_min) / 2)
        if x != y:
            n_sum += (x - y) * (x - y)

print(n_sum)

Submission status: Wrong Answer

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

import math

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

n_max = max(a)
n_min = min(a)
n_sum = 0

if n_max != n_min:
    for n in a:
        n_sum += (n - math.ceil((n_max + n_min) / 2)) * (n - math.ceil((n_max + n_min) / 2))

print(n_sum)

Submission status: Wrong Answer

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

import math

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

n_max = max(a)
n_min = min(a)
n_sum = 0

if n_max != n_min:
    for n in a:
        n_sum += (n - math.floor((n_max + n_min) / 2)) * (n - math.floor((n_max + n_min) / 2))

print(n_sum)

Submission status: Wrong Answer

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

    m = min(A)
    M = max(A)

    for a in A:
        Result = (a-m) * (a-m)

    for i in range(m+1, M+1):
        Result_tmp = 0
        for a in A:
            Result_tmp += (a - i) * (a - i)
        if Result > Result_tmp:
            Result = Result_tmp
    print(Result)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
A_min = min(A)
A_max = max(A)
ans = (A_max - A_min) ** 2
for p in range(A_min, A_max + 1):
    tmp = 0
    for a in A:
        tmp += (a - p) ** 2
    if tmp < ans:
        ans = tmp
print(ans)

Submission status: Wrong Answer

Solution code:

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

#i2M = (min(aA) + max(aA)) // 2
iMM = sum(aA) // len(aA)

#if i2M < iMM :
#   iLM = i2M
#   iUM = iMM + 1
#else:
#   iLM = iMM
#   iUM = i2M + 1

if iMM < 0 :
    iUM =  iMM
    iLM = iMM - 1
else:
    iLM = iMM
    iUM = iMM + 1

aR = []
for iM in range(iLM,iUM+1):
    aR.append(0)
    for iA in aA:
        aR[-1] += (iA - iM) ** 2
print(min(aR))

Submission status: Wrong Answer

Solution code:

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

i2M = min(aA) + max(aA)
if i2M % 2 == 0 :
    aM = [i2M // 2]
if i2M % 2 == 1 :
    aM = [i2M //2 ,i2M //2 + 1]

aR = []
for iM in aM:
    aR.append(0)
    for iA in aA:
        aR[-1] += (iA - iM) ** 2
print(min(aR))

Submission status: Wrong Answer

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)
    
    
for i in range(l):
    ans = ans + (target - a[i]) * (target - a[i])

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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
    print(ans1)
    ans = min(ans, ans1)

print(ans)

Submission status: Wrong Answer

Solution code:
import math

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

set_list = set(input_list)
ans = 0

if len(set_list) == 1:
    ans = 0
if (max(input_list) - min(input_list)) % 2 == 0:
    center = (max(input_list) + min(input_list)) // 2
    for i in input_list:
        ans += (center - i) ** 2
else:
    left = (max(input_list) + min(input_list)) // 2
    right = math.ceil((max(input_list) + min(input_list)) / 2)
    ans1 = 0
    ans2 = 0
    for i in input_list:
        ans1 += (left - i) ** 2
        ans2 += (right - i) ** 2
    ans = min(ans1, ans2)

print(ans)

Submission status: Wrong Answer

Solution code:
import math

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

set_list = set(input_list)
ans = 0

if len(set_list) == 1:
    ans = 0
elif (max(input_list) - min(input_list)) % 2 == 0:
    center = (max(input_list) + min(input_list)) // 2
    for i in input_list:
        ans += (center - i) ** 2
else:
    left = (max(input_list) + min(input_list)) // 2
    right = math.ceil((max(input_list) + min(input_list)) / 2)
    ans1 = 0
    ans2 = 0
    for i in input_list:
        ans1 += (left - i) ** 2
        ans2 += (right - i) ** 2
    ans = min(ans1, ans2)

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = sum(a) / n
if (q * 10) % 10 >= 5:
    q = int(q) + 1
else:
    q = int(q)

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

print(num)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int, input().split()))
a.sort()
if n%2==0:
    m=(a[n//2-1]+a[n//2])//2
else:
    m=a[n//2]
ans=[]
t=0
for i in range(n):
    t+=(a[i]-m)**2
ans.append(t)
t=0
for i in range(n):
    t+=(a[i]-m+1)**2
ans.append(t)
t=0
for i in range(n):
    t+=(a[i]-m-1)**2
ans.append(t)
print(min(ans))

Submission status: Wrong Answer

Solution code:
from statistics import median
import math

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

sum_list = [
	sum([(a_i - math.ceil(median(set(a))))**2 for a_i in a]),
	sum([(a_i - math.floor(median(set(a))))**2 for a_i in a])
]
print(min(sum_list))

Submission status: Wrong Answer

Solution code:
from statistics import median
import math

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

sum_list = [
	sum([(a_i - math.ceil(median(a)))**2 for a_i in a]),
	sum([(a_i - math.floor(median(a)))**2 for a_i in a])
]
print(min(sum_list))

Submission status: Wrong Answer

Solution code:
from statistics import median
import math

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

sum_list = [
	sum([math.ceil(a_i - median(a))**2 for a_i in a]),
	sum([math.floor(a_i - median(a))**2 for a_i in a])
]
print(min(sum_list))

Submission status: Wrong Answer

Solution code:
from statistics import median
import math

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

sum_list = [
	sum([math.ceil(a_i - median(a))**2 for a_i in a]),
	sum([math.floor(a_i - median(a))**2 for a_i in a])
]
print(max(sum_list))

Submission status: Wrong Answer

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

ans = 9999

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

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

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

for i in range(a_min, a_max+1):
    sumof = 0
    for j in a:
        sumof += (i - j)*(i - j)
    ans = min(ans, sumof)
    
print(ans)

Submission status: Wrong Answer

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

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

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

mean = sum(a) // len(a) if sum(a) % len(a) == 0 else (sum(a) // len(a)) + 1

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

Submission status: Wrong Answer

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

mean = sum(a) // len(a) if sum(a) % len(a) == 0 else (sum(a) // len(a)) + 1 

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

print(df)

Submission status: Wrong Answer

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

mean = sum(a) // len(a) if sum(a) % len(a) == 0 else sum(a) // len(a) + 1 

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

print(df)

Submission status: Wrong Answer

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

mean = sum(a) // len(a) if sum(a) % len(a) == 0 else sum(a) // len(a) + 1 

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

df

Submission status: Wrong Answer

Solution code:
from statistics import median
N = int(input())
a = [int(ai) for ai in input().split()]
med = int(median(set(a)))
ret = sum([(ai - med)**2 for ai in a])
print(int(ret))

Submission status: Wrong Answer

Solution code:
from statistics import median
N = int(input())
a = [int(ai) for ai in input().split()]
med = median(set(a))
ret = sum([(ai - med)**2 for ai in a])
print(ret)

Submission status: Wrong Answer

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(1, 101):
        c = 0
        for x in a:
            c += cost(x,y)
        ans = min(ans, c)
    return ans

print(m())

Submission status: Wrong Answer

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

for i in range(-100, 101):
  sum = 0
  for a in n_list:
    sum += abs((i - a) ^ 2)
  min_sum = min(min_sum, sum)

print(min_sum)

Submission status: Wrong Answer

Solution code:
n = input()
n_list = list(map(int, input().split()))
max_sum = 0

for i in range(-100, 101):
  sum = 0
  for a in n_list:
    sum += (i - a) ^ 2
  max_sum = max(max_sum, sum)
  
print(max_sum)

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':
    n = int(input())
    l = list(map(int, input().split(' ')))
    min_cost = float('inf')
    for b in range(-100, 100, 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: Wrong Answer

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

for i in l:
  ans += abs(mean-i)**2

print(ans)

Submission status: Wrong Answer

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

for i in l:
  ans += abs(mean-i)**2

print(ans)

Submission status: Wrong Answer

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)
print(t)
for j in range(N):
    w+=(t-s[j])**2
print(w)

Submission status: Wrong Answer

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

    mina = min(A)
    maxa = max(A)
    n = [(mina + maxa) // 2, -(-(mina + maxa) // 2)]
    cost = []
    for y in n:
        c = 0
        for x in A:
            c += (x - y) ** 2
        cost.append(c)

    print(min(cost))

main()

Submission status: Wrong Answer

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

    mina = min(A)
    maxa = max(A)
    n = [(mina + maxa) // 2, -(-(mina + maxa) // 2)]
    cost = []
    for i, y in enumerate(n):
        c = 0
        for x in A:
            c += abs(y - x) ** 2
        cost.append(c)

    print(min(cost))

main()

Submission status: Wrong Answer

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

    mina = min(A)
    maxa = max(A)
    div_, mod_ = divmod(maxa + mina, 2)
    if mod_:
        n = [div_, div_ + 1]
    else:
        n = [div_]

    cost = []
    for i, y in enumerate(n):
        c = 0
        for x in A:
            c += abs(y - x) ** 2
        cost.append(c)

    print(min(cost))

main()

Submission status: Wrong Answer

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

    mina = min(A)
    maxa = max(A)
    div_, mod_ = divmod(maxa - mina, 2)
    if mod_:
        n = [mina + div_, mina + div_ + 1]
    else:
        n = [mina + div_]

    cost = []
    for i, y in enumerate(n):
        c = 0
        for x in A:
            c += abs(y - x) ** 2
        cost.append(c)

    print(min(cost))

main()

Submission status: Wrong Answer

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

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

costs = []

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

# AtCoder Beginner Contest
# Problem C

if __name__ == '__main__':
    n = int(input())
    a = list(map(int, input().split()))
    z = round((sum(a)) / n)

    if z < 0 and sum(a) % n != 0:
        z -= 1

    if n == 1:
        print(0)
        exit()

    cost = 0

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

    print(cost)

Submission status: Wrong Answer

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

# AtCoder Beginner Contest
# Problem C

if __name__ == '__main__':
    n = int(input())
    a = list(map(int, input().split()))
    z = (sum(a) + 1) // n

    if z < 0:
        z -= 1

    if n == 1:
        print(0)
        exit()

    cost = 0

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

    print(cost)

Submission status: Wrong Answer

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

# AtCoder Beginner Contest
# Problem C

if __name__ == '__main__':
    n = int(input())
    a = list(map(int, input().split()))
    z = (sum(a) + 1) // n
    cost = 0

    if n == 1:
        print(0)
        exit()

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

    print(cost)

Submission status: Wrong Answer

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

# AtCoder Beginner Contest
# Problem C

if __name__ == '__main__':
    n = int(input())
    a = list(map(int, input().split()))
    z = (sum(a) + 1) // n
    cost = 0

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

    print(cost)

Submission status: Wrong Answer

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
    print("i",i,"T",T)
    if S > T:
        S = T
print(S)