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:
import math
from math import gcd,pi,sqrt
INF = float("inf")

import sys
sys.setrecursionlimit(10**6)
import itertools
from collections import Counter,deque
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]


def main():
    n = i_input()
    a = i_list()
    a_min = min(a)
    a_max = max(a)
    ans = INF
    for i in range(a_min,a_max+1):
        trial = 0
        for j in a:
            trial += (i-j)**2
        ans = min(ans,trial)
    print(ans)



if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
mn,mx=min(a),max(a)
m=10**9
for i in range(mn,mx+1):
	m=min(m,sum([(t-i)**2 for t in a]))
print(m)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
Sum =0
for i in range(len(A)):
  Sum += (A[0]-A[i])**2
Answer = Sum
for j in range(-100,101):
  Sum =0
  for k in range(len(A)):
    Sum+= (A[k]-j)**2
  Answer =min(Answer, Sum)
print(Answer)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

ave = sum(a) // n
ans1 = sum((ai - ave)** 2 for ai in a)
ans2 = sum((ai - ave - 1)** 2 for ai in a)
print(min(ans1,ans2))

Submission status: Accepted

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

from collections import Counter
c=Counter(a)
avr=sum(a)//n
c[avr]=0
if sum(a)%n!=0:
	c[-(-sum(a)//n)]=0
ans = abs(max(a)-min(a))**n
for key in c:
	res=0
	for aa in a:
		res+=(aa-key)**2
	ans=min(ans,res)
print(ans)

Submission status: Accepted

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

def cost(number):
    res = 0
    for x in a:
        res += (x - number) ** 2
    return res

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

print(ans)

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/arc059/tasks/arc059_a

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

ans = 10**9 + 7
for i in range(-100, 101):
    tmp = 0
    for a in A:
        tmp += (i - a)**2
    ans = min(tmp, ans)

print(ans)

Submission status: Accepted

Solution code:
def cost(x,a,b):
    cost=0
    for i in range(b):
        cost+=(x-a[i])**2
    return cost

n=int(input())
a=list(map(int,input().split()))
ans=0
for i in range(n):
    ans+=a[i]
ans/=n
ans=int(ans)
print(min(cost(ans,a,n),cost(ans+1,a,n)))

Submission status: Accepted

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

print(sum([(i - t) ** 2 for i in a]))

Submission status: Accepted

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

x = round(sum(a) / n)

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
mi = min(a)
ma = max(a)
if mi == ma:
    print(0)
    exit()
ans = 0
for i in a:
    ans += (i - mi)**2
for i in range(mi + 1, ma + 1):
    val = 0
    for j in a:
        val += (i - j)**2
    ans = min(ans, val)
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
from math import ceil

n = int(input())
a = list(map(int, input().split()))
a = np.array(a)
ans = 10**9
for i in range(-100,101):
    ans = min(ans,(np.abs(a-i)**2).sum())
print(ans)

Submission status: Accepted

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

ans = []
for num in range(101):
    cost1, cost2 = 0, 0
    for a in A:
        cost1 += (a-num)**2
        cost2 += (a-(-num))**2
    ans.append(cost1)
    ans.append(cost2)
print(min(ans))

Submission status: Accepted

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

mean = round(sum(a)/n)

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

print(cnt)

Submission status: Accepted

Solution code:
a = lambda :map(int,input().split())
lma = lambda :list(map(int,input().split()))
tma = lambda :tuple(map(int,input().split()))
ni = lambda:int(input())
yn = lambda fl:print("Yes") if fl else print("No")
import collections
import math
import itertools
import heapq as hq

n = ni()
A = lma()
ans = 10**9
for i in range(-100,101):
    tmp=0
    for a1 in A:
        tmp+=(i-a1)**2
    ans = min(tmp,ans)
print(ans)

Submission status: Accepted

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

ans = 10**10
for i in range(-200, 201, 1):
    tmp = 0
    for a in A:
        tmp += (a-i)**2
    ans = min(ans, tmp)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
ans = float('inf')
check = 0
for i in range(-100,100+1):
    for j in A:
        check +=(j-i)**2
    ans = min(ans,check)
    check=0
print(ans)

Submission status: Accepted

Solution code:
import sys

n = int(input())
a = list(map(int,input().split()))
s = ((sum(a))//n)+1
t = sum(a)//n
u = ((sum(a))//n)-1
l = []
l.append(s)
l.append(t)
l.append(u)
ans = []
ans2 = []
aa = set(a)

if len(aa) == 1:
    print(0)
    sys.exit()

for j in range(len(l)):
    ans = []
    for i in range(n):
        if a[i] == l[j]:
            continue
        else:
            b = (a[i]-l[j])**2
            ans.append(b)
    ans2.append(sum(ans))


print(min(ans2))

Submission status: Accepted

Solution code:
N = int(input())
AAA = map(int,input().split())
AA = list(AAA)
AA.sort()
mini = 10**7
x = AA[-1]
for i in range(AA[0],x+1):
  total = 0
  for j in AA:
    total += (j-i)**2
  if total < mini:
    mini = total
print(mini)

Submission status: Accepted

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

Submission status: Accepted

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

mu=sum(a)//n

print(min(sum([(aa-mu)**2 for aa in a]),sum([(aa-mu-1)**2 for aa in a])))

Submission status: Accepted

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

ans = float("INF")
for k in range(-100, 100+1):
    tmp = 0
    for a in A:
        tmp += pow(a - k, 2)
    ans = min(ans, tmp)

print(ans)

Submission status: Accepted

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

# from numba import njit

# input = stdin.readline

INF = 10**9

# @njit
def solve(n,a):
  ans = INF
  k = min(a)
  while k <= max(a):
    ans = min(ans,sum((x-k)**2 for x in a))
    k += 1
  return ans



def main():
  N = int(input())
  # N,M = map(int,input().split())
  a = list(map(int,input().split()))
  print(solve(N,a))
  return

if __name__ == '__main__':
  main()

Submission status: Accepted

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

ans=1000000
for i in range(min(A),max(A)+1):
    cost=0
    for x in range(len(A)):
        cost+=(A[x]-i)**2
    ans=min(ans,cost)

print(ans)

Submission status: Accepted

Solution code:
import sys

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

tin=lambda : map(int, input().split())
lin=lambda : list(tin())
mod=1000000007

#+++++

def main():
	n = int(input())
	#b , c = tin()
	#s = input()
	al = lin()
	ret = mod
	for i in range(-102, 102):
		v = sum([(x-i)**2 for x in al])
		ret = min(v, ret)
	print(ret)
	
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Submission status: Accepted

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

ave = sum(al)//n
ans = 10**9
for y in range(n-5000,n+5000+1):
    cost = 0
    for x in al:
        cost += (x - y)**2
    ans = min(ans,cost)
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
av=sum(l)//n
sm=0
sm1=0
for m in l:
  sm+=(m-av)**2
  sm1+=(m-av-1)**2
print(min(sm,sm1))

Submission status: Accepted

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

Submission status: Accepted

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

res = 10**10
for i in range(min(A), max(A)+1):
    x = 0
    for a in A:
        x += abs(a-i)**2
    res = min(x, res)

print(res)

Submission status: Accepted

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

M=float("inf")
for x in range(-100,101):
    K=0
    for a in A:
        K+=(a-x)*(a-x)
    M=min(M,K)
print(M)

Submission status: Accepted

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

ans = float('inf')
for i in range(-100, 101):
    temp = sum([(a[j] -i)**2 for j in range(n)])
    ans = min(ans, temp)
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n, *a = map(int, open(0).read().split())
p = round(sum(a) / n)
print(sum((i - p)**2 for i in a))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n, *a = map(int, open(0).read().split())
p = sum(a) // n
q = -(-sum(a) // n)
print(min(sum((i - p)**2 for i in a), sum((i - q)**2 for i in a)))

Submission status: Accepted

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

s = math.ceil(sum(A)/N)
t = math.floor(sum(A)/N)

ans_s = 0
ans_t = 0
for a in A:
    ans_s += (a-s)**2
    ans_t += (a-t)**2
print(min(ans_s, ans_t))

Submission status: Accepted

Solution code:
import sys, math
from itertools import combinations
sys.setrecursionlimit(500000)
MOD = 10**9+7

def input():
    return sys.stdin.readline()[:-1]

def mi():
    return map(int, input().split())

def ii():
    return int(input())

def i2(n):
    tmp = [list(mi()) for i in range(n)]
    return [list(i) for i in zip(*tmp)]

def main():
    N = ii()
    a = list(mi())
    m = 10**100
    for K in range(-100, 101):
        tmp = sum((a[i]-K)**2 for i in range(N))
        m = min(m, tmp)
    print(m)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
amax=max(a)
amin=min(a)
ans=1000000000000
for i in range(amin,amax+1):
  t=0
  for j in range(n):
    t=t+(a[j]-i)**2
  ans=min(t,ans)
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
ar=list(map(int,input().split()))
def f(i):
    ans=0
    for e in ar:
        ans+=(e-i)**2
    return ans
cur=float('inf')
for i in range(-200,200):
    cur=min(cur,f(i))
print(cur)

Submission status: Accepted

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

# In[10]:


from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN


# In[25]:


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


# In[26]:


x = int(Decimal(str(sum(a)/N)).quantize(Decimal('0'), rounding=ROUND_HALF_UP))
ans = 0
for i in range(N):
    ans += (x-a[i])**2
print(ans)


# In[ ]:

Submission status: Accepted

Solution code:
INF = 10 ** 18
N = int(input())
A = list(map(int, input().split()))
up = max(A)
low = min(A)
res = INF
for i in range(low, up + 1):
    t = 0
    for a in A:
        t += (i - a) ** 2
    res = min(res, t)
print(res)

Submission status: Accepted

Solution code:
import math
n = int(input())
a = list(map(int,input().split()))
p = 0
if sum(a) % n == 0:
    b = sum(a) // n
    for i in range(n):
        p += abs(a[i]-b) ** 2
    print(p)
else:
    b = sum(a) / n
    x = math.floor(b)
    y = math.ceil(b)
    q = 0
    for i in range(n):
        p += abs(a[i]-x) ** 2
        q += abs(a[i]-y) ** 2
    print(min(p,q))

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
def resolve():
    '''
    code here
    '''
    import math
    N = int(input())
    As = [int(item) for item in input().split()]

    
    res = 2000**2

    for mid in range(min(As), max(As)+1):
        temp_res = 0
        for item in As:
            temp_res += (item - mid)**2
        res = min(temp_res, res)
    print(res)

if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

#%%
ans = float('inf')
# 直感的にはAの平均周辺が最小値っぽい
mean = sum(A)//n
means = [mean-1, mean, mean+1]
for m in means:
    tmp = 0
    for a in A:
        tmp += (m - a)**2
    ans = min(ans, tmp)

print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    A = list(map(int, input().split()))
    m = int(sum(A) / len(A) + 0.5)
    ans = float('inf')
    for i in range(m - 1, m + 2):
        ans = min(ans, sum([(a - i) ** 2 for a in A]))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math

N = int(input())
a_li = list(map(int, input().split()))
mid_1 = math.ceil(sum(a_li) / N)
mid_2 = sum(a_li) // N
ans_1 = 0
ans_2 = 0
for a in a_li:
    ans_1 += (a - mid_1) ** 2
for a in a_li:
    ans_2 += (a - mid_2) ** 2
print(min(ans_1, ans_2))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque, defaultdict
from math import sqrt, factorial, gcd, ceil
# def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline().strip() # warning bytes
# def input(): return sys.stdin.buffer.readline().decode('utf-8')
import string
# string.ascii_lowercase
from bisect import bisect_left

def solve():
    n = int(input())
    a = [int(x) for x in input().split()]
    ans = float('inf')
    for y in range(min(a), max(a) + 1):
        sm = 0
        for x in a:
            sm += (x - y) ** 2
        ans = min(ans, sm)
    print(ans)

t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()


"""

azyxwvutsrqponmlkjihgfedcb

"""

Submission status: Accepted

Solution code:
import sys,math
import numpy as np
input = sys.stdin.readline

N = int(input())
a = np.array(list(map(int,input().split())))
c = int(round(sum(a)/N))

print( sum((a-c)**2))

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

Submission status: Accepted

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

x = sum(a) // n
count1 = 0
count2 = 0
for i in range(n):
    count1 += (a[i] - x)**2
    count2 += (a[i] - (x + 1))**2

print(min(count1, count2))

Submission status: Accepted

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

Submission status: Accepted

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

ave = sum(a)//n
ans1 = sum((ai - ave) ** 2 for ai in a)
ans2 = sum((ai - ave - 1) ** 2 for ai in a)
print(min(ans1,ans2))

Submission status: Accepted

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

ans1 = sum((ai - sum(a) // n) ** 2 for ai in a)
ans2 = sum((ai - sum(a) // n - 1) ** 2 for ai in a)
print(min(ans1,ans2))

Submission status: Accepted

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

ave = sum(a) // n
ans = sum((ai - ave)** 2 for ai in a)
ans = min(ans, sum((ai - ave - 1)** 2 for ai in a))
print(ans)

Submission status: Accepted

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

def main():
    N = int(input())
    a = list(map(int, input().split()))
    arg = round(sum(a) / len(a))
    ans = 0
    for i in range(N):
        ans += (a[i] - arg)**2
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import bisect,copy,heapq,string
from collections import *
from math import *
from itertools import *
import sys
sys.setrecursionlimit(10 ** 6)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n = INT()
a = LIST()

ans = inf
for i in range(-100,101):
    tmp = 0
    for j in range(len(a)):
        tmp += (i-a[j])**2
    ans = min(ans, tmp)

print(ans)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    n = int (input())
    a = [int(i) for i in input().split()]
    minN = min(a)
    maxN = max(a)
    cost = -1
    for i in range(minN-1,maxN+1):
        temp =0
        for j in a:
            temp+=(i-j)*(i-j)
        if cost == -1 or ( cost > temp):
            cost = temp
    print(cost)

Submission status: Accepted

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

INF = 10**18
ans = INF
for X in range(-100, 101):
    cost = sum(map(lambda a: (a-X)**2, A))
    if cost < ans:
        ans = cost
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):
  t = 0
  for a in A:
    t += (a - x) ** 2
  ans = min(ans,t)
  
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()


INF = float('inf')

n = I()
a = LI()
ans = INF
for i in range(-100, 101):
    cost = 0
    for j in a:
        cost += (i - j) ** 2
    ans = min(ans, cost)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
ps = list(map(int, input().split()))
 
p_avg = round(sum(ps)/len(ps))
 
print(sum([abs(p-p_avg)**2 for p in ps]))

Submission status: Accepted

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

p_min = min(ps)
p_max = max(ps)

a = 999999999999999999999

for i in range(p_min, p_max+1):
    b = sum([abs(p-i)**2 for p in ps])
    a = min(a, b)

print(a)

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/arc059/tasks/arc059_a

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

ans = float('inf')
for i in range(-100, 101):
    t = 0
    for num in nums:
        t += (num - i) ** 2
    ans = min(ans, t)
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import math

n = int(input())
l = list(map(int, input().split()))
avg1 = sum(l)//n
avg2 = math.ceil(sum(l)/n)
# print(avg1, avg2)
res1 = 0
res2 = 0
for i in range(n):
    res1 += abs(l[i] - avg1)*abs(l[i] - avg1)
    res2 += abs(l[i] - avg2)*abs(l[i] - avg2)
print(min(res1, res2))

Submission status: Accepted

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


def log(*args):
    print(*args, file=sys.stderr)


def main():
    ans = 1000000000
    _n = int(input())
    a = list(map(int, input().rstrip().split()))
    for i in range(-100, 101):
        tmp = 0
        for v in a:
            tmp += pow((v - i), 2)
        ans = min(tmp, ans)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/arc059/tasks/arc059_a
# 全探索してもいいし、二乗誤差の最小化から平均値を求めても良い

N = int(input())
A = list(map(int, input().split()))
candi1 = sum(A) // N
candi2 = candi1 + 1

ans1 = ans2 = 0
for a in A:
    ans1 += (a - candi1)**2
    ans2 += (a - candi2) ** 2
print(min(ans1, ans2))

Submission status: Accepted

Solution code:
n = int(input())
a_li = list(map(int,input().split()))
a_min = min(a_li)
a_max = max(a_li)
a_average = round(sum(a_li)/n)
ans1 = 0
ans2 = 0
ans3 = 0
for i in range(n):
    ans1 += (a_min-a_li[i])**2
    ans2 += (a_max-a_li[i])**2
    ans3 += (a_average-a_li[i])**2
print(min(ans1,ans2,ans3))

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=10**9+7
for tar in range(-100,101):
    cost=0
    for i in a:
        cost+=(tar-i)**2
    if cost<ans: ans=cost
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
import sys
read = sys.stdin.read
readline = sys.stdin.readline

n,*a = map(int,read().split())

ans = 1<<60

for i in range(-101,101):
    v = 0
    for ai in a:
        v += (ai-i)**2
    #print(v,i)
    ans = min(ans,v)


print(ans)

Submission status: Accepted

Solution code:
import numpy as np
n=int(input())
a=list(map(int,input().split()))
a=np.array(a)
all=np.sum(a)
c=int(all/n+0.5)
d=int(all/n)
ans1=a-c
ans1=ans1**2
ans2=a-d
ans2=ans2**2
ans1=np.sum(ans1)
ans2=np.sum(ans2)
if ans1<=ans2:
    print(ans1)
else:
    print(ans2)

Submission status: Accepted

Solution code:
import math

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

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

print(b)

Submission status: Accepted

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

ans=1001001001001
cost_sum=0

for i in range(min(a),max(a)+1):
    for j in range(n):
        cost=(a[j]-i)**2
        cost_sum+=cost
        if ans < cost_sum:
            continue
    if ans > cost_sum:
        ans = cost_sum
    cost_sum=0
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin, setrecursionlimit


def main():
    input = stdin.buffer.readline
    n = int(input())
    a = list(map(int, input().split()))
    x1 = sum(a) // n
    x2 = x1 + 1
    ans1 = 0
    ans2 = 0
    for ai in a:
        ans1 += (ai - x1) ** 2
        ans2 += (ai - x2) ** 2

    print(min(ans1, ans2))


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Submission status: Accepted

Solution code:
n = int(input())
alst = list(map(int, input().split()))
ans = 10 ** 10
min_a = min(alst)
max_a = max(alst)
for y in range(min_a, max_a + 1):
    tmp = 0
    for num in alst:
        tmp += (num - y) ** 2
    ans = min(ans, tmp)
print(ans)

Submission status: Accepted

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

med=sum(a)/n

med_1=int(med)
med_2=int(med)+1

ans_1=0
ans_2=0
for i in range(n):
    ans_1+=(a[i]-med_1)**2
    ans_2+=(a[i]-med_2)**2
print(min(ans_1,ans_2))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# input()
# int(input())
# map(int, input().split())
# list(map(int, input().split()))
# list(map(int, list(input()))) # スペースがない数字リストを読み込み
import math
import fractions
import sys
import bisect
import heapq  # 優先度付きキュー(最小値取り出し)
import collections
from collections import Counter
from collections import deque
import pprint

sr = lambda: input()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

"""nを素因数分解"""
"""2以上の整数n => [[素因数, 指数], ...]の2次元リスト"""
def factorization(n):
    arr = []
    temp = n
    if n == 1:
        return arr

    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

# a^n
def power(a,n,mod):
    x = 1
    while n:
        if n & 1:
            x *= a % mod
        n >>= 1
        a *= a % mod
    return x % mod

# n*(n-1)*...*(l+1)*l
def kaijo(n, l, mod):
    if n == 0:
        return 1
    a = n
    tmp = n-1
    while(tmp >= l):
        a = a*tmp%mod
        tmp -= 1
    return a

inf = 10**18
mod = 10**9+7

n = ir()
a = lr()

total = 0
for num in a:
    total+=num
mean_min = total//n

ans = [0,0]
for i in range(mean_min, mean_min+2):
    ind = i%2
    for num in a:
        ans[ind]+=(num-i)*(num-i)
print(min(ans))

Submission status: Accepted

Solution code:
# ARC057A

import numpy as np

def resid(a, mean):
    res = 0
    for i in a:
        res += (i - mean)**2
    return int(res)

def f(n, a):
    mean = sum(a) / n
    mean_ceil = np.ceil(mean)
    mean_floor = np.floor(mean)
    print(min(resid(a, mean_ceil), resid(a, mean_floor)))

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

Submission status: Accepted

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

ans = float("inf")

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

Submission status: Accepted

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

#整数にする
y=round(sum(A)/N)
#print(y)
hituyou_min_cost=sum([(x-y)**2 for x in A])
print(hituyou_min_cost)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int, input().split()))
cost = 10**9
for i in range(-100,101):
    temp = 0
    for j in range(N):
        temp += (i-A[j])**2
    cost = min(cost,temp)


print(cost)

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())
a = np.array(list(map(int,input().split())))
ans = np.array([])

max_num = max(a)
min_num = min(a)

for i in range(min_num,max_num+1):
  ans = np.append(ans, sum((a-i)**2))

print(int(min(ans)))

Submission status: Accepted

Solution code:
from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
#from fractions import gcd
#from itertools import combinations # (string,3) 3回
#from collections import deque
#from collections import deque,defaultdict
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#

import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
n = I()
A = readInts()
medi = sum(A)/n
#print(medi)
medi = int(medi) if medi - int(medi) < 0.5 else int(medi) + 1
ans = 0
#print(medi)
for i in range(n):
    ans += abs(A[i] - medi)**2
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int,input().split()))
ans = 10**9
for k in range(-100,101):
    temp = 0
    for e in a:
        temp += (e-k)**2
    ans = min(temp,ans)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
a = 0
s = sum(A)
sn = s/N
A.sort()
m = A[0]
M = A[0]
if len(A) > 1:
  M = A[-1]
c = 1000000
for i in range(m, M+1):
  if abs(i - sn) < c:
    a = i
    c = abs(i-sn)
ans = 0
for i in range(N):
  ans += (a-A[i])**2
print(ans)

Submission status: Accepted

Solution code:
from statistics import mean
N = int(input())
A = list(map(int, input().split()))
m = int((mean(A)+0.5)//1)
ans = 0
for i in range(N):
    ans += (A[i]-m)**2
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
cmin = 10**7
for a in range(-100,101):
    cnt = 0
    for i in range(N):
        cnt += (A[i]-a)**2
    cmin = min(cmin,cnt)
print(cmin)

Submission status: Accepted

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

Submission status: Accepted

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

N = int(input())
a = list(map(int, input().split()))
l = min(a)
r = max(a)
ans = 10**18

for i in range(l, r+1):
    cnt = 0
    
    for ai in a:
        cnt += (ai-i)**2
    
    ans = min(ans, cnt)

print(ans)

Submission status: Accepted

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

ans = 10**18
for i in range(-101, 102):
    temp = 0
    for a in A:
        temp += (a-i)**2
    ans = min(temp, ans)
print(ans)

Submission status: Accepted

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