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:
p=int(input())
x=[int(x) for x in input().split()]
mi=min(x)
ma=max(x)
op=list()
for i in range(mi,ma+1):
	temp=0
	for j in x:
		temp=temp+(i-j)**2
	op.append(temp)  
print(min(op))

Submission status: Accepted

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

mini = 10**10
for i in range(min(X), max(X)+1):
    sum = 0
    for j in range(N):
        sum += (X[j]-i)**2
    if sum < mini:
        mini = sum
print(mini)

Submission status: Accepted

Solution code:
n = int(input().rstrip())
xs = list(map(int, input().rstrip().split(' ')))

result = float('inf')

i = 0
while True:
    point = i + 1
    current_sum = sum([(x-point) * (x-point) for x in xs])
    if result > current_sum:
        result = current_sum
        i += 1
    else:
        break

print(result)

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int,input().split()))
sum = 0
min_x = 0
min_x1 = 0
for i in x:
    sum += i
ave = sum // n
ave1 = ave + 1
for i in x:
    min_x += (ave - i) * (ave - i)
for i in x:
    min_x1 += (ave1 - i) * (ave1 - i)
if min_x1 >= min_x:
    print(min_x)
else:
    print(min_x1)

Submission status: Accepted

Solution code:
import math

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

ave = sum(lx) / len(lx)
ave = round(ave)

ret = 0
for _x in lx:
    ret = ret + (_x - ave)**2

print("{}".format(ret))

Submission status: Accepted

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

a = sum(X)
b = sum(x**2 for x in X)

energy = [N*p**2 - 2*a*p for p in range(1, 101)]

print(min(energy) + b)

Submission status: Accepted

Solution code:
n = int(input())
x = [int(x) for x in input().split()]
li = list()
for i in range (101):
    total = 0
    for m in x:
        total += (m-i)**2
    li.append(total)
print(min(li))

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int,input().split()))
m_energy = 250000
for i in range(1,101):
  energy = 0
  for k in range(n):
    energy += (x[k]-i)**2
  if energy < m_energy:
    m_energy = energy
print(m_energy)

Submission status: Accepted

Solution code:
N = int(input())
X_s = tuple(map(int,input().split()))
MIN = 10**9
for i in range(1,101):
  SUM = sum(tuple(map(lambda x:(x-i)**2, X_s)))
  if MIN > SUM:
    MIN = SUM
print(MIN)

Submission status: Accepted

Solution code:
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
n = int(input())
xs = list(map(int,input().split()))
min_x = sum(xs) / n
x_z = Decimal(str(min_x)).quantize(Decimal('0'), rounding=ROUND_HALF_UP)
ans = 0
for i in xs:
    ans += (x_z - i)**2
print(ans)

Submission status: Accepted

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

minx = min(X)
maxx = max(X)
min_cost = float('inf')
for p in range(minx, maxx + 1):
  cost = 0
  for xi in X:
    cost += (p - xi) * (p - xi)
  if cost < min_cost:
    min_cost = cost

print(min_cost)

Submission status: Accepted

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

print(min(a))

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int,input().split()))
min = float('inf')
 
for i in range(max(x)):
    s = 0
    for j in range(n):
        s += (x[j]-(i+1))**2
    if s < min:
        min = s
 
print(min)

Submission status: Accepted

Solution code:
n = int(input())
xlist = list(map(int,input().split()))
ans = []
p = 0

for i in range(0,101):
    for k in range(0,n):
        p += (xlist[k] - (i+1))**2
    ans.append(p)
    p = 0

anss = min(ans)

print(anss)

Submission status: Accepted

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

avg = sum([i for i in Pi]) // N

r1 = sum([(avg - i)**2 for i in Pi])
r2 = sum([(avg +1 - i)**2 for i in Pi])

print(min(r1, r2))

Submission status: Accepted

Solution code:
n = int(input())
persons_coordinates = list(map(int, input().split()))
max_coordinate = max(persons_coordinates)
min_coordinate = min(persons_coordinates)
min_stamina = float('inf')

for p in range(min_coordinate, max_coordinate + 1):
    stamina = 0
    for x in persons_coordinates:
        stamina += (x - p) ** 2
    if min_stamina >= stamina:
        min_stamina = stamina

print(min_stamina)

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int, input().split()))
P = sum(X)//len(X)
Q = P+1
ans_p = 0
ans_q = 0
for x in X:
  ans_p += (x-P)**2
  ans_q += (x-Q)**2
print(min(ans_p, ans_q))

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
b=[i**2 for i in a]
#sum_=sum(b)-2*p*sum(a)+n*p**2
c=sum(a)
def tai(p):
    return n*p**2-2*p*c
d=[]
for i in range(min(a)-2,max(a)+2):
    d.append(tai(i+1))
print(min(d)+sum(b))

Submission status: Accepted

Solution code:
number = int(input())
coordinates = list(map(int, input().split()))
coordinates.sort()
coordinates.reverse()
#print(coordinates)
list = []
for i in range(coordinates[0]+1):
    total=0
    for j in range(number):
        total += (coordinates[j]-i)**2
    list.insert(0, total)
list.sort()
print(list[0])

Submission status: Accepted

Solution code:
N = int(input())
Xinput = input().split()


X = [int(s) for s in Xinput]

Xmean = sum(X) / N
Xmean = round(Xmean)

Xout = [(x - Xmean) * (x - Xmean) for x in X]

print(sum(Xout))

Submission status: Accepted

Solution code:
INFTY = 9999999999999999999

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

X.sort()
left = X[0]
right = X[-1]

score = INFTY
for P in range(left, right+1):
    tmp_score = 0
    for i in range(N):
        tmp_score += ((X[i] - P) * (X[i] - P))
    if (tmp_score < score):
        score = tmp_score

print(score)

Submission status: Accepted

Solution code:

def calculate_hp(n,x):
  minimum_hp = 0
  xlist = [int(s) for s in x]
  for n in range(min(xlist), max(xlist)):
    total_hp = 0
    for i in xlist:
      hp = (i-n) ** 2
      total_hp+=hp
    if minimum_hp == 0 or minimum_hp > total_hp :
      minimum_hp = total_hp
  return minimum_hp

def main():
  n = int(input())
  x = input().split()
  minimum_hp=calculate_hp(n, x)
  print(str(minimum_hp))

main()

Submission status: Accepted

Solution code:
import sys

n=int(input())
li=list(map(int,input().split()))
li.sort()

mi,mx=li[0],li[-1]
ans=10**9
t=10**9
if mi==mx:
    print(0)
    sys.exit()

for i in range(mi,mx):
    ans=min(ans,t)
    t=0
    for j ,item in enumerate(li):
       t+=(li[j]-i)**2
       
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

N = int(input())
huga = tuple(map(int,input().split())) 

MinLen = 999999999999999
for i in range(min(huga),max(huga)+1):
    Len = 0
    for j in huga:
        Len += (j-i) ** 2
    if Len < MinLen:
      MinLen = Len
print( MinLen )

Submission status: Accepted

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

n = int(input())
x = [int(val) for val in input().split()]

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

Submission status: Accepted

Solution code:
N=int(input())
X=list(map(int,input().split()))
dispersion=[]
cnt=0
for i in range(100):
    for j in range(N):
        cnt+=abs(X[j]-i)**2
    dispersion.append(cnt)
    cnt=0
print(min(dispersion))

Submission status: Accepted

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

P = round(sum(X_list) / N)
ans = 0
for i in range(N):
    ans += (X_list[i] - P) ** 2
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()))
gt = int(Decimal(str(sum(X) / N)).quantize(Decimal('0'), rounding=ROUND_HALF_UP))
print(sum([(x-gt)**2 for x in X]))

Submission status: Accepted

Solution code:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
from queue import PriorityQueue
from fractions import gcd
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def _I(): return int(sys.stdin.readline())
def _F(): return float(sys.stdin.readline())
def _pf(s): return print(s, flush=True)

N = _I()
X = LI()

# 距離の2乗の体力を消費
# 100ループすれば求まる

min_x = min(X)
max_x = max(X)

min_XP = 10**9
for p in range(min_x, max_x+1):
    XP = 0
    for x in X:
        XP += (x-p)**2
    if min_XP > XP:
        min_XP = XP
print(min_XP)

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
hel = []
cnt = 0
for i in range(100):
    for j in range(n):
        cnt += abs(x[j] - i) ** 2
    hel.append(cnt)
    cnt = 0
print(min(hel))

Submission status: Accepted

Solution code:
import numpy
n = int(input())
x = [int(i) for i in input().split()]
min_x = round(sum(x) / n)
ans = sum([(min_x - i)**2 for i in x])
print(ans)

Submission status: Accepted

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

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

ans = float("inf")
for P in range(1,101):
    tmp = 0
    for x in X:
        tmp += (x - P) ** 2
    ans = min(ans , tmp)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
x = [int(i) for i in input().split()]
min_x = round(sum(x) / n)
ans = sum([(min_x - i)**2 for i in x])
print(ans)

Submission status: Accepted

Solution code:

if __name__ == '__main__':
    n = int(input())
    x = list(map(int, input().split()))
    p = []

    for i in range(100):
        cnt = 0
        for j in range(n):
            cnt = cnt + (x[j]-i)**2
        p.append(cnt)

    print(sorted(p)[0])

Submission status: Accepted

Solution code:
N=int(input())
numbers =input().rstrip().split(" ")
points=[]
for i in range(1,101):
    summ=0
    for j in range(N):
        summ=summ+(int(numbers[j])-i)*(int(numbers[j])-i)
    points.append(summ)
print(sorted(points)[0])

Submission status: Accepted

Solution code:
from math import floor
N = int(input())
X = list(map(int, input().split()))
Left = floor(sum(X)/N)
Right = Left + 1
candidate1 = 0
candidate2 = 0
for i in range(N):
  candidate1 += (X[i] - Left) ** 2
  candidate2 += (X[i] - Right) ** 2
print(min(candidate1, candidate2))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import numpy as np
N = int(input())
X = np.array(list(map(int, input().split())))
ans = np.inf
for i in range(min(X), max(X) + 1):
    ans = min(ans, sum((X - i)**2))
print(ans)

Submission status: Accepted

Solution code:
import math
import numpy as np

n=int(input())
x=np.array(input().split()).astype(int)
sx=np.tile(np.array(range(1,101)),(n,1))
y=x-sx.T
print(np.min(np.sum(y**2,1)))

Submission status: Accepted

Solution code:
N = int(input())
L = list(map(int,input().split()))
P = round(sum(L) / N)
A = []
for i in L:
  x = (i - P)*(i-P)
  A.append(x)
print(sum(A))

Submission status: Accepted

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

def stamina(x, p):
    return (x-p) ** 2
# x[i] - p^2
l = []
for i in range(min(X),max(X)+1):
    ans = 0
    for j in X:
        ans += stamina(j, i)
    l.append(ans)

print(min(l))

Submission status: Accepted

Solution code:
N = int(input())
Xs = input()
Xlist = Xs.split(' ')
Xlist.sort()
anslist = []
if len(set(Xlist)) != 1:
    for P in range(int(Xlist[0]),int(Xlist[N - 1])+1):
        ans = 0
        for i in range(len(Xlist)):
            ans = ans + (int(Xlist[i]) - P)**2
        anslist.append(ans)
    print(min(anslist))
else:
    print(0)

Submission status: Accepted

Solution code:
t=int(input())
for i in range(1):
    #a=list(map(int,input().split(' ')))
    a=[int(i) for i in input().split()]
    s=0
    for i in a:
        s=s+i 
    s=s//t 
    ans=1e9
    k=0
    for i in range(2):
        k=0
        for j in a:
           k=k+(j-s-i)*(j-s-i)
        ans=min(ans,k)
    print(ans)

Submission status: Accepted

Solution code:
N=int(input())
s = list(map(int,input().split()))
for a in range(100):
  r = 0
  for i in range(N):
    r+=(s[i]-(a+1))**2
  if a == 0:
    x = r
  elif r<x:
    x = r

print(x)

Submission status: Accepted

Solution code:
N=int(input())
a=list(map(int,input().split()))
b=int(sum(a)/len(a))
c=sum(a)
d=sum(map(lambda x:x**2,a))
e=min(d+N*b*b-2*c*b,d+N*(b+1)*(b+1)-2*c*(b+1))
print(e)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n = int(input())
x = list(map(int,input().split()))
print(sum((i-round(sum(x)/n))**2 for i in x))
#x**2 - 2*x + p**2 をp で微分するとp 位置を動かしたときの増加量がわかる
# ２次方程式になっていて minは重心のとき round すれば良い

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n = int(input())
x = list(map(int,input().split()))
ans = 10**6
for i in range(1,101):
  k = 0
  for j in range(n):
    k += (x[j]-i) **2
  ans = min(ans,k)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int, input().split()))
ans = 10 ** 7

for i in range(1, 101):#1から100で開催
  ans1 = 0
  for j in range(N):
    ans1 += (X[j] - i) ** 2
  ans = min(ans, ans1)
  
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
xs = [int(elem) for elem in input().split()]
 
min_ = min(xs)
max_ = max(xs)
result = []
for p in range(min_, max_+1):
  res = 0
  for x in xs:
    res += (x - p) ** 2
  result.append(res)
print(min(result))

Submission status: Accepted

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

ave = sum(x)//len(x)
ave_1 = ave+1
ans = 0
ans_1 = 0
for i in range(len(x)):
	ans += (x[i]-ave)**2
	ans_1 += (x[i]-ave_1)**2

print(min(ans,ans_1))

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline

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

    ans = float('inf')
    for i in range(1, 101):
        tmp = 0
        for j in range(N):
            tmp += (X[j] - i)**2
        ans = min(ans, tmp)
    print(ans)
    

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

ave=sum(X_list)//N
av2=ave+1
c1=sum([kyori(x,ave) for x in X_list])
c2=sum([kyori(x,av2) for x in X_list])
print(min([c1,c2]))

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
mi = 10**6
for i in range(min(x), max(x)+1):
    tmp = 0
    for j in x:
        tmp += (i-j)**2
    mi = min(tmp, mi)
print(mi)

Submission status: Accepted

Solution code:
#
N=int(input())
X=list(map(int, input().split()))
Xc=round(sum(X)/N)
tairyoku = 0
for Xi in X:
    tairyoku+=(Xi-Xc)**2
print(tairyoku)

Submission status: Accepted

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

ans = float("inf")

for i in range(1, 100):
    cnt = 0
    for j in X:
        cnt += (i-j)**2

    ans = min(cnt, ans)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
xlist=list(map(int,input().split()))
ber1=sum(xlist)//n
ber2=sum(xlist)//n+1
ans1=0
for i in range(n):
    ans1+=(xlist[i]-ber1)**2
ans2=0
for i in range(n):
    ans2+=(xlist[i]-ber2)**2
if ans1>ans2:
    print(ans2)
else:
    print(ans1)

Submission status: Accepted

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

R = max(X)
L = min(X)

ans = float('inf')

for i in range(L, R+1):
  cost = 0
  for j in X:
    cost += (j - i) * (j - i)
  ans = min(ans, cost)

print(ans)

Submission status: Accepted

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

ave = round(sum(x_list) / n)

ans = 0
for x in x_list:
    ans += (x - ave) ** 2

print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    x = [int(i) for i in input().split()]
    min_x = round(sum(x) / n)
    ans = sum([(min_x - i)**2 for i in x])
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())

raw = input().split()
li = list(map(int, raw))
s = sum(li)
a = round(s / N)
all_tairyoku = 0
for i in li:
    tairyoku = (i - a) ** 2
    all_tairyoku += tairyoku
print(all_tairyoku)

Submission status: Accepted

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

ans=[]
for i in range(l[0], l[-1]+1):
  num = 0
  for nn in l:
    num+=(nn-i)**2
  
  ans.append(num)

print(min(ans))

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int,input().split()))
X.sort()
min_HP = 10**9
for P in range(1,max(X)+1):
    S = 0
    for j in X:
        S += (j-P)**2
    if S<min_HP:
        min_HP = S
print(min_HP)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
x = np.array(list(map(int, input().split())))
s = np.sum(np.square(x))

for i in range(1, 101):
    s = min(s, np.sum(np.square(x-i)))

print(s)

Submission status: Accepted

Solution code:
n = int(input())
x_lis = list(map(int, input().split()))
ans = 0
avg = sum(x_lis)/n
if avg - int(avg) >= 0.5:
    avg = int(avg) + 1
else:
    avg = int(avg)
for x in x_lis:
    ans += (x - avg)**2
print(ans)

Submission status: Accepted

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

ans=10**99
for i in range(1,101):
    w=[(i-z)**2 for z in x] 
    ans=min(ans,sum(w))
print(ans)

Submission status: Accepted

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

for i in range(n):
    s += x[i]
p = int(s/n)

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

p += 1
s2 = 0
for i in range(n):
    s2 += (x[i]-p)**2
s = min(s, s2)

print(s)

Submission status: Accepted

Solution code:
n = int(input())
ps = list(map(int,input().split()))
results = []
for i in range(101):
    a = 0
    for p in ps:
        a += (p - i)** 2
    results.append(a)
    
print(min(results))

Submission status: Accepted

Solution code:
N = int(input())

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

cent = int(sum(array)/N+0.5)

ans = 0
for x in array:
  ans+=(cent - x)*(cent - x)
  
print(ans)

Submission status: Accepted

Solution code:
n = input()
x = [int(e) for e in input().split()]
max_cost = float('inf')
for i in range(101):
    cost = 0
    for j in x:
        cost = cost + (i - j)**2
    max_cost = min(cost, max_cost)
print(max_cost)

Submission status: Accepted

Solution code:
N=int(input())
X=list(map(int,input().split()))
max_conf=max(X)
point=[0]*(max_conf+1)
point[0]=10000000000

for i in range(1,max_conf+1):
  for j in X:
    point[i]=point[i]+(j-i)**2
print(min(point))

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
X = np.array(input().split()).astype(int)
P = round(np.average(X))
print(int(np.sum((P-X)**2)))

Submission status: Accepted

Solution code:
import math
n=int(input())
x=list(map(int,input().split()))
sum2 = 0
s=sum(x)
for i in x:
    sum2 += i**2

p1= math.floor(s/n)
p2= math.ceil(s/n)

ans1= sum2 -2*p1*s + n*p1**2
ans2= sum2 -2*p2*s + n*p2**2

print(min(ans1,ans2))

Submission status: Accepted

Solution code:
n=int(input())
X=list(map(int,input().split()))
ans=-1
for i in range(1,101):
    cnt=0
    for x in X:
        cnt+=(x-i)**2
    if ans==-1:
        ans=cnt
    else:
        ans=min(ans,cnt)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from functools import reduce
n = int(input())
p = list(map(int, input().split()))
add_0 = int(reduce(lambda x, y: x+y, p)/n)
add_1 = add_0+1
res_0 = res_1 = 0
for digit in p:
    res_0 = res_0 + (digit-add_0)**2
    res_1 = res_1 + (digit-add_1)**2
print(min(res_0,res_1))

Submission status: Accepted

Solution code:
import math
n = int(input())
X = list(map(int, input().split()))

p1=math.ceil(sum(X)/n)
p2=math.floor(sum(X)/n)
cnt1=0
cnt2=0
for x in X:
  cnt1 += (x-p1)**2
  cnt2 += (x-p2)**2
print(min(cnt1, cnt2))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
read = sys.stdin.buffer.read 
input = sys.stdin.buffer.readline
def inputS(): return input().rstrip().decode()
readlines = sys.stdin.buffer.readlines 

N = int(input())
A = list(map(int, input().split()))
max_A = max(A)
ans = 1<<60
for i in range(1, max_A+1):
  P = sum([(a-i)**2 for a in A])
  ans = min(ans, P)
print(ans)

Submission status: Accepted

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

x.sort()

psum=0
for i in range(len(x)):
    psum=psum+(x[0]-x[i])**2
#    print(psum)
pmin=psum    
for j in range(x[0]+1,x[len(x)-1]):
    psum=0
    for i in range(len(x)):
        psum=psum+(j-x[i])**2
#        print(psum)
    if pmin>psum:
        pmin=psum
print(pmin)

Submission status: Accepted

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

ans = 10 ** 18

for i in range(1,101):
    tmp = 0
    for j in x:
        tmp += (j - i) ** 2
    ans = min(ans, tmp)

print(ans)

Submission status: Accepted

Solution code:
from collections import deque, Counter
from itertools import chain, combinations
import json
import numpy as np
import bisect


def main():
    N = int(input())
    X = [int(a) for a in input().split()]

    def f(n):
        return sum((n - x) ** 2 for x in X)

    res = 1
    v = f(1)
    for i in range(1, 99):
        i += 1
        if v > f(i):
            res = i
            v = f(i)
    print(v)

    # A = [int(a) for a in input().split()]
    #
    # A = sorted(A)
    # A = np.array(A)
    #


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def solve(n, a_list):
    res = 100000000
    for p in range(101):
        r = sum([(a_list[i] - p) ** 2 for i in range(n)])
        if r < res:
            res = r

    return res


def main():
    n = int(input())
    a_list = list(map(int, input().split()))
    res = solve(n, a_list)
    print(res)


def test():
    assert solve(2, [1, 4]) == 5
    assert solve(7, [14, 14, 2, 13, 56, 2, 37]) == 2354


if __name__ == "__main__":
    test()
    main()

Submission status: Accepted

Solution code:
N=int(input())
inp=input().split()
x=[]
for k in range(N):
  x.append(int(inp[k]))
minim=10**6+1 
for i in range(1,101):
  cost=0
  for j in range(N):
    cost+=(x[j]-i)**2
  if cost<minim:
    minim=cost
print(minim)

Submission status: Accepted

Solution code:
def Stamina(N, X_list, P):
  stamina = 0
  for i in range(N):
    X = X_list[i]
    stamina += X * X - 2 * P * X
  stamina += N * P * P
  return stamina

N = int(input())
X_list = list(map(int, input().split()))
P1 = int(sum(X_list) / N)
P2 = P1 + 1
stamina1 = Stamina(N, X_list, P1)
stamina2 = Stamina(N, X_list, P2)
print (min(stamina1, stamina2))

Submission status: Accepted

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

box = []
d= 0
for i in range(min(x),max(x)+1):
    d = map(lambda y: (y-i)**2, x)
    d_t = sum(d)
    box.append(d_t)
print(min(box))

Submission status: Accepted

Solution code:
import math
n = int(input())
x = list(map(int, input().split()))
def average(line):
    return sum(line)/len(line)
ave_x = average(x)
p_1 = math.floor(ave_x)
p_2 = math.ceil(ave_x)
power_1 = 0
power_2 = 0
for i in range(len(x)):
    power_1 += (x[i]-p_1)**2
    power_2 += (x[i]-p_2)**2
print(min(power_1,power_2))

Submission status: Accepted

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

res=0
for i in Xs:
    res = res + (i-round(sum(Xs)/N))**2

print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
X=list(map(int,input().split()))
p=0
s=0
while abs(p-sum(X)/n)>1/2:
  p+=1
for x in X:
  s+=(x-p)**2
print(s)

Submission status: Accepted

Solution code:
n=int(input())
X=list(map(int,input().split()))
p=0
s=0
m=0
for i in range(n):
  m += X[i-1]/n
while abs(p-m) > 1/2:
  p += 1
for j in range(n):
  s += (X[j-1]-p)**2
print(s)

Submission status: Accepted

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

l,r=x[0],x[-1]
mi=sum([(x[i]-l)**2 for i in range(len(x))])
for i in range(l+1,r+1):
    tmp=sum([(x[k]-i)**2 for k in range(len(x))])
    if tmp<mi: mi=tmp
print(mi)

Submission status: Accepted

Solution code:
N = int(input())
x = list(map(int, input().split()))
 
n = len(x)
 
sum_a = 0
sum_b = 0
 
for xi in x:
  sum_a += (xi*xi)
  sum_b += xi

p = int(sum_b/n+0.5)
ans = sum_a - 2*sum_b*p+n*p*p

min_ans = 10**9
if ans > min_ans:
    ans = min_ans
print(int(ans))

Submission status: Accepted

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

ans=99999999
for i in range(1,101):
  d=0
  for x in a:
    d+=(x-i)*(x-i)
  ans=min(ans,d)
print(ans)

Submission status: Accepted

Solution code:
import unittest


class TestC(unittest.TestCase):
    def test_1(self):
        self.assertEqual(think([1, 4]), 5)

    def test_2(self):
        self.assertEqual(think([14, 14, 2, 13, 56, 2, 37]), 2354)


def solve():
    x = read()
    result = think(x)
    write(result)


def read():
    n = read_int(1)[0]
    return read_int(n)


def read_int(n):
    return read_type(int, n, sep=' ')


def read_float(n):
    return read_type(float, n, sep=' ')


def read_type(t, n, sep):
    return list(map(lambda x: t(x), read_line().split(sep)))[:n]


def read_line(n=0):
    if n == 0:
        return input().rstrip()
    else:
        return input().rstrip()[:n]


def think(x):
    left = min(x)
    right = max(x)
    min_cost = 1e20
    for p in range(left, right + 1):
        min_cost = min(min_cost, calc_cost(x, p))
    return min_cost


def write(result):
    print(result)


def calc_cost(x, p):
    return sum(map(lambda x: abs(x - p) ** 2, x))


if __name__ == '__main__':
    # unittest.main()
    solve()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(12345678)
import itertools
from collections import Counter
from collections import defaultdict
from collections import deque
import bisect
from heapq import heappush, heappop


def main():
    n = int(input())
    xl = list(map(int, input().split()))
    min_p = min(xl)
    max_p = max(xl)
    ans = 10**10
    for p in range(min_p, max_p + 1):
        cost = 0
        for x in xl:
            cost += (x - p) ** 2
        ans = min(ans, cost)

    print(ans)
    return 

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def tairyoku(list_x, p):
    sum_t = 0
    for xx in list_x:
        sum_t += (xx - p)**2
    return sum_t

n = int(input())
x = list(map(int, input().split()))
ave = sum(x) / n

print(min(tairyoku(x, int(ave)), tairyoku(x, int(ave)+1)))

Submission status: Accepted

Solution code:
n = int(input())
x = input().split()
min = 100000000
for i in range(1, 101):
    dist = 0
    for j in range(n):
        dist += (int(x[j])-i)**2
    if dist < min:
        min = dist
print(min)

Submission status: Accepted

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

sum_list = []
for p in range(min(X), max(X)+1):
    _sum = 0
    for x in X:
        _sum += abs(p-x) ** 2

    sum_list += [_sum]

print(min(sum_list))

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    X = list(map(int, input().split()))
    ans = float("inf")
    for p in range(1, 101):
        tmp = 0
        for x in X:
            tmp += (x-p)**2
        ans = min(ans, tmp)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
X = [int(x) for x in input().split()]
a = sum(X) // N
b = a + 1
ans1 = 0
ans2 = 0
for x in X:
    ans1 += (x - a) ** 2
    ans2 += (x - b) ** 2
print(min(ans1, ans2))

Submission status: Accepted

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

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

print(score)

Submission status: Accepted

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

m = int(sum(X)/len(X))

score1 = 0
score2 = 0
for x in X:
  score1 += (x-m)**2
  score2 += (x-(m+1))**2

print(min(score1,score2))

Submission status: Accepted

Solution code:
n=int(input())
x=[int(i) for i in input().split()]
ans=10**9
for i in range(1,101):
    res=0
    for j in x:
        res+=(j-i)**2
    ans=min(ans,res)
print(ans,end="")

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
def f(a):
  t = 0
  for i in range(n):
    t += (x[i]-a)**2
  return t
m=float('inf')
for i in range(1, 101):
  if m > f(i):
    m=f(i)
print(m)

Submission status: Accepted

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

min_num = 10**18
for i in range(1, 101):
    m = sum((x-i)**2 for x in X)
    min_num = min(min_num, m)

print(min_num)

Submission status: Accepted

Solution code:
N = int(input())
tmp = input().split(" ")
X = [int(i) for i in tmp]
m = int(10*sum(X)/N)
if m%10<5:
  m = int(m/10)
else:
  m = int(m/10)+1
print(sum([((i-m)**2) for i in X]))

Submission status: Accepted

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

x.sort()
ans = 0
min_ans = 10**9

for i in range(x[0],x[-1]+1):
    for j in range(len(x)):
        ans += (x[j]-i)**2
        
    if min_ans > ans:
        min_ans = ans
    
    ans = 0

print(min_ans)

Submission status: Accepted

Solution code:
from math import ceil, floor

n = int(input())
x = list(map(int, input().split()))
tmin = float('inf')
for i in range(1, max(x) + 1):
	cur = 0
	for j in x:
		cur += (i - j) ** 2
	tmin = min(cur, tmin)
print(tmin)

Submission status: Accepted

Solution code:
# coding: utf-8
import math
import sys

N = int(input())
Xi = list(map(int, input().split()))
Xi.sort()
min = Xi[0]
max = Xi[N-1]
tmp = 0
result = sys.maxsize
for i in range(min,max+1):
    for j in range(0,N):
        tmp += (Xi[j] - i)**2
    if tmp <= result:
        result = tmp
    tmp = 0

print(result)