Problem:


Score : 300 points

Problem Statement

There are N people living on a number line.

The i-th person lives at coordinate X_i.

You are going to hold a meeting that all N people have to attend.

The meeting can be held at any integer coordinate. If you choose to hold the meeting at coordinate P, the i-th person will spend (X_i - P)^2 points of stamina to attend the meeting.

Find the minimum total points of stamina the N people have to spend.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq X_i \leq 100

Input

Input is given from Standard Input in the following format:

N
X_1 X_2 ... X_N

Output

Print the minimum total stamina the N people have to spend.

Sample Input 1

2
1 4

Sample Output 1

5

Assume the meeting is held at coordinate 2. In this case, the first person will spend (1 - 2)^2 points of stamina, and the second person will spend (4 - 2)^2 = 4 points of stamina, for a total of 5 points of stamina. This is the minimum total stamina that the 2 people have to spend.

Note that you can hold the meeting only at an integer coordinate.

Sample Input 2

7
14 14 2 13 56 2 37

Sample Output 2

2354



Sample input:
2
1 4


Sample output:
5

Submission status: Accepted

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

min_P = 0
min_cost = float('inf')
for P in range(1, 101):
    cost = 0
    for Xi in X:
        cost += (Xi-P)**2
    if min_cost > cost:
        min_cost = cost
        min_P = P
print(min_cost)

Submission status: Accepted

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

hp = 0
hp2 = 0

p = sum(x)//n
for i in range(n):
    hp += (x[i-1]-p)**2
    hp2 += (x[i-1]-(p+1))**2

print(min(hp,hp2))

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int, input().split()))
m = 10**10
for p in range(1, max(l)+1):
    tmp = 0
    for x in l:
        tmp += (x-p)**2
    m = min(m, tmp)

print(m)

Submission status: Accepted

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

min = 1000001

for i in range(1,101):
    damage = 0
    for j in x_list:
        damage += (j - i)**2
    if damage < min:
        min = damage
print(min)

Submission status: Accepted

Solution code:
N, *X = map(int, open(0).read().split())

m = sum(X) // N
v1 = sum((v - m) ** 2 for v in X)
v2 = sum((v - m - 1) ** 2 for v in X)
print(min(v1, v2))

Submission status: Accepted

Solution code:
n=int(input())
x=list(map(int,input().split()))
ans=9999999999
for i in range(min(x),max(x)+1):
    xi=list(map(lambda x:(x-i)**2,x))
    ans=min(ans,sum(xi))
print(ans)

Submission status: Accepted

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

head = min(X)
tail = max(X)

min_score = float('inf')
for p in range(head, tail+1):
    min_score = min(sum(map(lambda x: (x - p)**2, X)), min_score)

print(min_score)

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
x = np.array(input().split(),dtype=int)

ave = 0
for i in range(n):
    ave += x[i]
ave /= n
ave = int(ave+0.5)

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

print(ans)

Submission status: Accepted

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

line = [0]*(100)

for i in s:
    for j in range(1,101):
        line[j-1] += (i-j)**2

print(min(line))

Submission status: Accepted

Solution code:
#https://atcoder.jp/contests/abc156/tasks/abc156_c

n=int(input())
x=list(map(int, input().split()))
x.sort()
minimum=10**23
for i in range(x[0],x[-1]+1):
    sum=0
    for l in x:
        sum+=(l-i)**2
    minimum=min(sum,minimum)
print(minimum)

Submission status: Accepted

Solution code:
N = int(input())
alist = list(map(int,input().split()))
ans = 10**8
for i in range(101):
    q = 0
    for j in range(N):
        q = q + (i - alist[j])**2
    ans = min(q,ans)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

r = 0
a = round(sum(X)/N)
for x in X:
    r += (x - a)**2

print(r)

Submission status: Accepted

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

N = int(input())
X = list(map(int, input().split()))
X_max = max(X)
X_min = min(X)
tes_min = 10000000

for i in range(X_min, X_max + 1):
    tes = 0
    for j in X:
        tes += (j - i)**2
    if tes_min > tes:
        tes_min = tes
print(tes_min)

Submission status: Accepted

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

# Sigma_n (Xn-P)**2 = Sigma_n (Xn**2 + P**2 -2*P*Xn)
# diff => Sigma_n (2P - 2Xn)

su = 0
P = round(sum(Xn) / N)
for x in Xn:
  su += (x-P) ** 2
print(int(su))

Submission status: Accepted

Solution code:
import numpy as np
import math
n = int(input())
x = np.array(list(map(int, input().split())))

p0 = math.floor(x.sum()/n)
p1 = math.ceil(x.sum()/n)
cost0 = np.sum((x-p0)**2)
cost1 = np.sum((x-p1)**2)
print(min(cost0, cost1))

Submission status: Accepted

Solution code:
n=int(input())
x=list(map(int, input().split()))
min_=10**9
for i in range(min(x),max(x)+1,1):
  #print(i)
  sum_=0
  for j in range(n):
    sum_+=(x[j]-i)**2
  if sum_<min_: min_=sum_
  #print(min_)
print(int(min_))

Submission status: Accepted

Solution code:
import numpy as np

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

mean_x = int(sum(x_s)/n)
a_s = [(x - mean_x)**2 for x in x_s]
a = sum(a_s)
b_s = [(x - mean_x - 1)**2 for x in x_s]
b = sum(b_s)

if a < b:
    print(a)
else:
    print(b)

Submission status: Accepted

Solution code:
import math
N = int(input())
X = list(map(int,input().split()))
A = math.ceil(sum(X)/N)
B = math.floor(sum(X)/N)
ans_1 = 0
ans_2 = 0
for i in range(N):
    ans_1 += (X[i]-A)**2
for j in range(N):
    ans_2 += (X[j]-B)**2
print(min(ans_1,ans_2))

Submission status: Accepted

Solution code:
n= int(input())

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

p=round(sum(X)/n)
ans=0
for i in range(n):
	ans+=(X[i]-p)**2

print(ans)

Submission status: Accepted

Solution code:
import math

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

ave = sum(X) / len(X)

m = math.floor(ave)
M = math.ceil(ave)

def f(x):
  ans = 0
  for i in X:
    ans += (x - i)**2
  return ans  

print(min(f(m), f(M)))

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int,input().split()))
M = max(x)
s = [0]*(10*M)

for j in range(10*M):
	for i in range(n):
		s[j] += pow(x[i]-(j-5*M),2)
print(min(s))

Submission status: Accepted

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

mid = round(sum(X)/N)
ans = 0
for i in range(N):
    Q = X[i] - mid
    ans += Q*Q
print(ans)

Submission status: Accepted

Solution code:
import math
n = int(input())
data = input().split(' ')
x_list = list(map(lambda x: int(x), data))
opt_p_center = sum(x_list) / n  # least squares
opt_p_left = math.floor(opt_p_center)
opt_p_right = math.ceil(opt_p_center)
opt_p = opt_p_left if opt_p_center < (opt_p_left + opt_p_right) / 2 else opt_p_right
ans = sum(list(map(lambda x: (x - opt_p)**2, x_list)))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
X=list(map(int,input().split()))
# 真ん中が最適の点, round関数を使って四捨五入する
P = int(round(sum(X)/N,0))
ans = 0
for x in X:
    ans += (x-P)**2
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
x = np.array(list(map(int, input().split())))
k = round(sum(x) / n)
print(int(sum((x - k)**2)))

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
x = np.array(list(map(int, input().split())))
k = sum(x) // n
x1 = (x - k) ** 2
x2 = (x - k - 1) ** 2
print(min(sum(x1), sum(x2)))

Submission status: Accepted

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

mean = np.mean(X)
p = int(mean) if mean % 1 < 0.5 else int(mean) + 1
stamina = 0
for x in X:
    stamina += (x - p)**2
print(stamina)

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
x = np.array([int(i) for i in input().split()])
p = min(sum((x - np.floor(x.mean())) ** 2), sum((x - np.ceil(x.mean())) ** 2))
print(int(p))

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
pdlist = []
for i in range(max(x) + 1):
  p = i
  dlist = []
  for j in x:
    dlist.append(int((int(j) - int(p)) ** 2))
  sumd = sum(dlist)
  pdlist.append(sumd)
minpdlist = min(pdlist)
print(minpdlist)

Submission status: Accepted

Solution code:
N=int(input())
X=[int(x) for x in input().split()]
ans=10**9
for i in range(1,101):
    kari=0
    for j in range(N):
        kari+=abs(i-X[j])**2
    if kari<ans:
        ans=kari
print(ans)

Submission status: Accepted

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

# calc
P = 0.0
for xn in X:
    P += xn/N
P = round(P)
    
ans = 0
for xn in X:
    ans += (xn - P)**2
    
print( int(ans) )

Submission status: Accepted

Solution code:
N = int(input())
x = [int(x) for x in input().split()]
ans = 10**6
for i in range(1,101):
    wa = 0
    for j in range(N):
        wa += (i-x[j])**2
    ans = min(ans,wa)
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
n=int(input())
x=list(map(int,input().split()))
s=int(sum(x)/n)
t=s+1
X=np.array(x)
P=np.ones(n)*s
Q=P+np.ones(n)
ans=min(sum((X-Q)**2),sum((X-P)**2))
print(int(ans))

Submission status: Accepted

Solution code:
from functools import reduce
a=int(input())
b=list(map(int,input().split()))
c=reduce(lambda a,b:a+b,b)
d=int(c/a)
def x(a,b,c):
    t=a.copy()
    c=[c]*b
    t=list(map(lambda x,y:x-y,t,c))
    return reduce(lambda a,b:a+b*b,t,0)
ans=min(x(b,a,d),x(b,a,d+1),x(b,a,d-1))
print(ans)

Submission status: Accepted

Solution code:
import math
def main():
  N=int(input())
  X=list(map(int,input().split()))
  x_mean=round(sum(X)/len(X))
  sumX=0
  for i in range(len(X)):
    sumX=sumX+(X[i]-x_mean)**2
  print(sumX)

if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
n = int(input())
x = [int(i) for i in input().split()]
mn = 10**9
for p in range(101):
    sum_mn = 0
    for i in range(n):
        sum_mn += (p-x[i])**2
    if mn > sum_mn:
        mn = sum_mn
print(mn)

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int,input().split()))
A = 0
B = 0
for i in range(N):
	A += (X[i])**2
	B += X[i]
def ans(A,B,n,x):
	return A-2*x*B+n*(x**2)
	
res = 99999999999
for i in range(1,101):
	res = min(res,ans(A,B,N,i))
	
print(res)

Submission status: Accepted

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

ans = 10**20 #答えになるので初期化
for i in range(1,101): #1<=P<=100の範囲について消費する体力の総和を計算
    dis = 0
    for j in x:
        dis += (i-j)**2
    ans = min(ans,dis)

print(ans)

Submission status: Accepted

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

average = sum(A) // N
ans1 = 0
for i in A:
    ans1 += (i-average)**2
ans2 = 0
for i in A:
    ans2 += (i-average-1)**2
ans = min(ans1, ans2)

print(ans)

Submission status: Accepted

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

ave = sum(X) // N
E1 = []
E2 = []

for i in X:
    E1.append((i - ave-1) ** 2)
    E2.append((i - ave) ** 2)
    
print(min(sum(E1), sum(E2)))

Submission status: Accepted

Solution code:
n=int(input())
x=list(map(int,input().split()))
min_x=sum(x)/n
int_x=int(min_x)
if min_x-int_x <= int_x+1-min_x:
    mp=int_x
else:
    mp=int_x+1
ans=0
for i in range(n):
    ans+=(x[i]-mp)**2
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())
x = np.array(list(map(int, input().split())))

small = x.min()
big = x.max()

min_cost = 1e10

for i in range(small, big+1):
    cost = ((x - i)**2).sum()
    min_cost = np.minimum(cost, min_cost)

print(int(min_cost))

Submission status: Accepted

Solution code:
N = int(input())
Xi= list(map(int,input().split()))
ans = 0
for i in range(1,max(Xi)+1):
	total_energy =0
	for n in range(N):
		energy = (Xi[n]-i)**2
		total_energy = total_energy + energy
	if i == 1:
		ans = total_energy
	elif ans > total_energy:
		ans = total_energy
print(ans)

Submission status: Accepted

Solution code:
import statistics

N = int(input())
X = [int(i) for i in input().split()]
X.sort()
mid = statistics.mean(X)
ans = 0
ans1 = 0
ans2 = 0

for i in range(N):
    ans1 += (X[i] - int(mid)) ** 2
    ans2 += (X[i] - int(mid) - 1) ** 2
ans = min(ans1, ans2)

print(ans)

Submission status: Accepted

Solution code:
def calc(a):
    y = 0
    for xx in x:
        y += (a - xx) ** 2
    return y


n = int(input())
x = [int(i) for i in input().split()]
left = min(x)
right = max(x)
while (right - left > 1):
    l = calc(left)
    r = calc(right)
    if (l < r):
        right = (right + left) // 2
    else:
        left = (right + left) // 2
print(min(calc(left), calc(right)))

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
for p in range(1,101):
    sum = 0
    for x in X:
        sum += (x-p)**2
    if p == 1:
        ans = sum
    elif sum < ans:
        ans = sum

print(ans)

Submission status: Accepted

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

ans = 100000000000000
for i in range(101):
  temp = 0
  for j in range(len(A)):
    temp += (A[j]-i)**2
  if(temp < ans):
    ans = temp
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
lmax=0
for i in range(n):
    if lmax<l[i]:
        lmax=l[i]
sumlist=[]
for i in range(lmax):
    sumlist.append(0)
total=0
for i in range(lmax):
    for j in range(n):
        t=(l[j]-(i+1))*(l[j]-(i+1))
        total+=t
    sumlist[i]=total
    total=0
min=10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
for i in range(lmax):
    if min>sumlist[i]:
        min=sumlist[i]
print(min)

Submission status: Accepted

Solution code:
n = int(input())
ls = list(map(int, input().split()))
ls2 =[]
for x in range(1,101):
    k = 0
    for y in ls:
        k += (x - y) ** 2
    ls2 += [k]
m = min(ls2)
print(m)

Submission status: Accepted

Solution code:
n = int(input())
x = [int(_) for _ in input().split()]

res = 999999999
for i in range(1, 101):
    res = min(res, sum([(i-xi)**2 for xi in x]))
print(res)

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
import numpy as np
x = np.array(x)
mi = 1000000
for i in range(100):
  h = (x-i)**2
  s = h.sum()
  if s < mi:
    mi = s
print(mi)

Submission status: Accepted

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

li = []
for i in range(1, 101):
    total = 0
    for j in range(n):
        total += (x[j] - i)**2
    li.append(total)
print(min(li))

Submission status: Accepted

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

l = []

for p in range(min(X), max(X)+1):
    cnt = 0
    for i in range(N):
        cnt += (X[i] - p) ** 2
    l.append(cnt)
print(min(l))

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int,input().split()))
INF = 114514191981036436433-4

ans = INF
for i in range(1,101):
    a = 0
    for j in range(n):
        a += (x[j] - i) ** 2
        
    ans = min(ans,a)
    
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
n=int(input())
x=list(map(int,input().split()))
b=round(sum(x)/len(x))
x=np.array(x)
x1=abs(x-b)
print(sum(x1*x1))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = sum(A)
C = 0
if(B%N < N/2):
  for i in(A):
    C += (B//N-i)**2
else:
  for i in(A):
    C += (B//N+1-i)**2
print(C)

Submission status: Accepted

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

res = 10**9

for i in range(101):
  res = min(res, sum([(i - x)**2 for x in X]))
  
print(res)

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
if min(x) == max(x):
    print(0)
    exit()

dist_min = 9999999999999
for i in range(min(x),max(x)):
    dist_sum = 0
    for j in range(n):
        dist_sum += (x[j] - i)**2
    if dist_sum < dist_min:
        dist_min = dist_sum

print(dist_min)

Submission status: Accepted

Solution code:
n=int(input())
x=list(map(int, input().split()))
maxn=0
for i in range(n):
    if x[i]>maxn:
        maxn=x[i]
    else:
        maxn=maxn
minp=0
for j in range(n):
    minp=minp+(x[j]-1)**2
for i in range(maxn+1):
    power=0
    for j in range(n):
        power=power+(x[j]-i)**2
    if power<minp:
        minp=power
    else:
        minp=minp
print(minp)

Submission status: Accepted

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

ans = 100**3
for i in range(1, 100):
    tmp = 0
    for j in range(n):
        tmp += (a[j]-i)**2
    if tmp < ans:
        ans = tmp
print(ans)

Submission status: Accepted

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

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

    ans = 0

    for i in range(min(x_li), max(x_li)+1):
        sum_p = sum(list(map(lambda x: (x - i)**2, x_li)))
        if ans == 0:
            ans = sum_p
        elif sum_p < ans:
            ans = sum_p

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
P=(sum(A))//N
P0=P-1
P1=P+1
M,M1,M0=0,0,0
for i in A:
  M+=(i-P)**2
  M1+=(i-P1)**2
  M0+=(i-P0)**2
print(min(M,M1,M0))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
x = int(input())
num = list(map(int, input().split()))
num.sort()
y = round(sum(num) / x)
sum_num = 0

for i in num:
  sum_num += (abs(i - y)) ** 2

print(sum_num)

Submission status: Accepted

Solution code:
people=int(input())
vector=list(map(int,input().split()))
min_cordination=min(vector)
max_cordination=max(vector)
sum_squared_list=[]
for i in range(min_cordination,max_cordination+1):
  tmp_sum_squared=0
  for one_person in vector:
    tmp_sum_squared+=(i-one_person)**2
  sum_squared_list.append(tmp_sum_squared)
print(min(sum_squared_list))

Submission status: Accepted

Solution code:
N = int(input())
X = [int(i) for i in input().split(' ')]
MIN = 10 ** 9
for i in range(101):
    SUM = 0
    for x in X:
        SUM += (x - i) **2
    if SUM < MIN:
        MIN = SUM

print(MIN)

Submission status: Accepted

Solution code:
import math
N = int(input())
X = list(map(int, input().split()))
E1 = 0
E2 = 0
x1 = math.floor(1/N*sum(X))
x2 = math.ceil(1/N*sum(X))
for i in range(N):
  E1 += (X[i]-x1)**2
  E2 += (X[i]-x2)**2
if E1 <= E2:
  print(E1)
else:
  print(E2)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

min_power = 10**10

#どこの座標で集会をするか
for i in range(1,101):
    power = 0
    for j in X:
        power += (j-i)**2
    min_power = min(min_power,power)

print(min_power)

Submission status: Accepted

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

#print(n,x)

def party(person_place,party_place):
    energy=pow(abs(party_place-person_place),2)
    return(energy)

if abs(sum(x)//len(x)-sum(x)/len(x))<0.5:
    party_place=sum(x)//len(x)
else:
    party_place=sum(x)//len(x)+1

#print(party_place)


answer=0    
for i in range(0,len(x)):
    answer+=party(x[i],party_place)
    #print(answer,party(x[i],n))
print(answer)

Submission status: Accepted

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

L = []
for i in range(1, 101):
    s = 0
    for j, v in enumerate(X):
        s = s + (v - i)**2
    L.append(s)
print(min(L))

Submission status: Accepted

Solution code:
n = int(input())
xs = [int(e) for e in input().split(" ")]

print(min([sum([(x - p) ** 2 for x in xs]) for p in range(1, 101)]))

Submission status: Accepted

Solution code:
n = int(input())
*X, = map(int, input().split())
m = 10**10
for i in range(1, 100+1):
    m = min(m, sum([(x-i)**2 for x in X]))
print(m)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    X = [int(a) for a in input().split()]
    mi = 10**9
    for i in range(1, max(X) + 1):
        hp = sum([(X[j] - i)**2 for j in range(N)])
        if hp < mi:
            mi = hp
    print(mi)
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import deque, Counter as cnt
from collections import defaultdict as dd
from operator import itemgetter as ig
from bisect import bisect_right as bsr
from math import factorial, ceil, floor
import sys
sys.setrecursionlimit(1000000)

# お約束
args = None
INF = float("inf")
MOD = int(1e9 + 7)


def input(*ps):
    if type(ps[0]) is list:
        return [input(*ps[0][:-1]) for _ in range(ps[0][-1])]
    elif len(ps) == 1:
        return ps[0](next(args))
    else:
        return [p(next(args)) for p in ps]


def nlist(n, v):
    if not n:
        return [] if type(v) is list else v
    return [nlist(n[1:], v) for _ in range(n[0])]


def yesno(v):
    print(["Yes", "No"][not v])


def main():
    """エントリーポイント"""
    N = input(int)
    X = input([int, N])
    f, c = floor(sum(X) / N), ceil(sum(X) / N)
    ans = [0, 0]
    for x in X:
        ans[0] += (f - x) ** 2
        ans[1] += (c - x) ** 2
    print(min(ans))


if __name__ == '__main__':
    args = iter(sys.stdin.read().split())
    main()

Submission status: Accepted

Solution code:

def atcoder2():
    a = [list(map(int,input().split())) for l in range(2)]

    n= a[0][0]
    x_l = a[1]

    P = sum(x_l)/n

    # print(a,n)
    # print(P)
    if P - int(P) >= 0.5:
        add= 1
    else:
        add = 0

    P = int(P)+add
    
    # print(P)

    result = 0
    for Xi in x_l:
        result = result + (Xi-P)**2 

    print(result)
    return 


atcoder2()

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
x = np.array(list(map(int, input().split())))

ans = 10 ** 9
for i in range(1, 101):
    tmp = np.square(x - i).sum()
    ans = min(ans, tmp)

print(ans)

Submission status: Accepted

Solution code:
import sys

num = []

while True:
    try:
        num.extend([int(n) for n in input().split()])
    except EOFError:
        break
    
s = sum(num[1:num[0]+1])/num[0]
s = round(s)
r = 0
for i in range(1,num[0]+1):
    r += pow(num[i]-s,2)
print(r)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
x=sorted(list(map(int,input().split())))
min=min(x)
max=max(x)
ans=10**9
for p in range(min,max+1):
    sum=0
    for i in range(n):
        sum+=(x[i]-p)**2
    if ans>sum:
        ans=sum
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
X = list(map(int, input().split()))
ans = 10**10
for p in range(1,101):
  total = 0
  for x in X:
    total += pow((x-p),2)
  ans = min(ans, total)
  
print(ans)

Submission status: Accepted

Solution code:

url = "https://atcoder.jp//contests/abc156/tasks/abc156_c"

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

    syukai_pos = max(people_pos) + min(people_pos) // 2
    tmp = 0
    tmp2 = 0
    for pp in people_pos:
        tmp += (pp - syukai_pos) ** 2
        tmp2 += (pp - syukai_pos + 1) ** 2
    print(tmp) if tmp < tmp2 else print(tmp2)


def main2():
    n = int(input())
    people_pos = list(map(int, input().split()))
    people_pos.sort()
    count = 10000000000
    for i in range(people_pos[0] , people_pos[-1] + 1):
        tmp = 0
        for pp in people_pos:
            tmp += (pp - i) ** 2
        count = count if count < tmp else tmp
    print(count)




if __name__ == '__main__':
    main2()

Submission status: Accepted

Solution code:
n=int(input())
li = list(map(int,input().split()))
ans=[]
i=1
while i<=101:
	for j in range(n):
		cost=[(li[j]-i)**2 for j in range(n)]
		ans.append(sum(cost))
	i+=1
print(min(ans))

Submission status: Accepted

Solution code:
n = input()
l = map(int,raw_input().split())
mxs = 10**9


for i in range(max(l)+1):
    sm = 0
    for j in l:
        sm += (j-i)**2
    if sm<mxs:
        mxs = sm
print mxs

Submission status: Accepted

Solution code:
from collections import Counter
n=int(input())
x=list(map(int,input().split()))
max=max(x)
d=dict(Counter(x))
m=1000000
for i in range(1,max+1):
    s=0
    for key,vlaue in d.items():
        s+=(i-key)**2*vlaue
    m=min(s,m)
print(m)

Submission status: Accepted

Solution code:
from decimal import Decimal, ROUND_HALF_UP
n = int(input())
x = list(map(int,input().split()))
a = sum(x)/n
a = int(Decimal(str(a)).quantize(Decimal('0'), rounding=ROUND_HALF_UP))
s=0
for i in range(n):
    s += (x[i]-a)*(x[i]-a)
print(s)

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
x.sort()
sigmaList = list()
for p in range(x[0], x[n - 1] + 1):
    sigma = 0
    for i in range(n):
        sigma += (x[i] - p) ** 2
    sigmaList.append(sigma)
print(min(sigmaList))

Submission status: Accepted

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

total = 100**n
for p in range(min(x), max(x) + 1):
    tmp = 0
    for i in x:
        tmp += (i - p)**2
    if tmp <= total:
        total = tmp

print(total)

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int,input().split()))
C=[]
for p in range(100):
    c=0
    for j in range(len(X)):
        c += (X[j] - p)**2


    C.append(c)

print(min(C))

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int, input().split()))
ptc = 0
tc = 100**2*100
for i in range(min(X), max(X)+1):
    for k in X:
        ptc += (k-i)**2
    tc = min(ptc,tc)
    ptc = 0
print(tc)

Submission status: Accepted

Solution code:
n=int(input())
x= list(map(int, input().split()))
ans=10**9
for i in range(min(x),max(x)+1):
    cost=0
    for j in x:
        cost+=(j-i)**2
    if cost<ans:
        ans=cost
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
X = []
max_ = -1

for i in input().split(" "):
    tmp = int(i)
    if tmp > max_:
        max_ = tmp
    X.append(tmp)

ans = 1e9
for i in range(100):
    tmp = 0
    for j in X:
        tmp += ((j - i) ** 2)
    if ans > tmp:
        ans = tmp

print(ans)

Submission status: Accepted

Solution code:
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
n = int(input())
x = list(map(int, input().split()))

q = sum(x) / n
p = Decimal(str(q)).quantize(Decimal('0'), rounding=ROUND_HALF_UP)

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int,input().split()))
tmp = 0
ans = 10 ** 10
for i in range(1,101):
  tmp = 0
  for a in range(N):
    tmp = tmp + (abs(X[a] - i)) ** 2
  ans = min(ans,tmp)
print(str(ans))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# %%
stdin = open(0)
import numpy as np
 
# %%
data = np.fromstring(stdin.read(), np.int64, sep=' ')
 
# %%
X = data[1:]
costs = []
for p in range(101):
    costs.append(np.sum((X-p)**2))
 
# %%
answer = min(costs)
print(answer)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=1e10
for i in range(1,101):
  sum=0
  for x in a: sum+=pow(abs(x-i),2)
  ans=min(ans,sum)
print(ans)

Submission status: Accepted

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

a=min(X)
b=max(X)

ans=[]
for i in range(a,b+1):
    total=0
    for j in X:
        total+=(i-j)*(i-j)
    ans.append(total)

print(min(ans))

Submission status: Accepted

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

T=[]
for p in range(min(X),max(X)+1):
    t=0
    for x in X:
        t+=(x-p)**2
    T.append(t)
print(min(T))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)


############ ---- Input Functions ---- ############
def in_int():
    return (int(input()))


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


def in_str():
    s = input()
    return (list(s[:len(s) - 1]))


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

mod = 7 + 10**9


n = in_int()

a = in_list()


aa = sum(a)//n

ans1 = 0
ans2 = 0

for xx in a:
    ans1 += (xx-aa)**2
    ans2 += (xx-aa-1)**2


print(min(ans1,ans2))

Submission status: Accepted

Solution code:
N = int(input())
List = list(map(int, input().split()))
s = 1000000000000000000000
t = 0

for i in range(100):
  for j in range(N):
    t += (List[j]-i-1)**2

  if s>t:
    s = t
    t = 0
  else:
    t = 0

print(s)

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())
a = np.array(list(map(int, input().split(' '))))
avg = np.mean(a)
mid_1 = np.ceil(avg)
mid_2 = np.floor(avg)
ans = int(min(((a-mid_1)**2).sum(), ((a-mid_2)**2).sum()))
print(ans)

Submission status: Accepted

Solution code:
def func(x_list, num):
    total = sum([(x - num)**2 for x in x_list])
    return total

N = int(input())
x_list = list(map(int, input().split()))
max_x = max(x_list)
min_num = func(x_list, 1)

for i in range(2, max_x+1):
    tmp = func(x_list, i)
    if min_num > tmp:
        min_num = tmp

print(min_num)

Submission status: Accepted

Solution code:
class atcoder:
    ans=0
    N=int(input())
    X=list(map(int,input().split()))
    ave=round(sum(X)/len(X))
    for i in range(len(X)):
        ans+=(ave-X[i])*(ave-X[i])
    print(ans)

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
x = list(map(int,input().split()))
z = np.array(x)
y = z*z
ans = 10**10
for i in range(1,101):
    ans = min(ans,n*i*i-2*i*sum(x)+sum(y))

print(ans)

Submission status: Accepted

Solution code:
import sys

n, *x = map(int, sys.stdin.read().split())

def main():
    p = round(sum(x) / n)
    res = sum([(p - i) ** 2 for i in x])
    print(res)

if __name__ ==  '__main__':
    main()

Submission status: Accepted

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

ans = 10000000

for i in range(100):
    result = 0
    for j in x:
        result += (j - i )**2
    ans = min(result, ans)
        
print(ans)

Submission status: Accepted

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

import math

c1 = math.ceil(sum(xlst)/n)
c2 = math.floor(sum(xlst)/n)
s1 = [(c1-x)**2 for x in xlst]
s2 = [(c2-x)**2 for x in xlst]

print(min(sum(s1),sum(s2)))

Submission status: Accepted

Solution code:
n=int(input())
position=list(map(int,input().split()))
m=10000000
for i in range(101):
  accum=0
  for pos in position:
    accum+=(pos-i)**2
  m=min(accum,m)
print(m)

Submission status: Accepted

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