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

Solution (Python):
n = int(input())
za = list(map(int,input().split()))
za_max = max(za)
za_min = min(za)
kouho = 0
kouho_sum = 0
ans = 9999999999999999999999999999999999999999999999999999999999999999999999999999
for zahyou in range(za_min,za_max+1):
    for i in za:
        kouho = (i - zahyou)*(i - zahyou)
        kouho_sum = kouho_sum + kouho
    if(ans > kouho_sum):
        ans = kouho_sum
        kouho_sum = 0
print(ans)

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

e = 0
ave = 0
for i in range(0, len(X)):
    ave += X[i]/len(X)

ave = int(ave+0.5)

for i in range(0,len(X)):
    energy = X[i] - ave
    e += energy ** 2

print(e)

Solution (Python):
N = int(input())
X = tuple(map(int, input().split()))

m = 10 ** 100
for i in range(1, 101):
  t = 0
  for x in X:
    t += (x - i) ** 2
  if m > t:
    m = t

print(m)

Solution (Python):

import sys

def solve(N: int, X: "List[int]"):
    min = None
    for p in range(1, 101):
        r = 0
        for x in X:
            r += (x - p)**2
        if min is None or min > r:
            min = r
    return min


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    X = [int(next(tokens)) for _ in range(N)]  
    print(solve(N, X))

if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
X = list(map(int, input().split()))
M = round(sum(X)/N)
answer = 0
for i in X:
    answer += (i-M)**2
print(answer)

Solution (Python):
import math
n=int(input())
ls=list(map(int,input().split()))

s=sum(ls)/n

if abs(math.ceil(s)-s)<0.5:
    t=math.ceil(s)
else:
    t=math.floor(s)


ans=0
for i in ls:
    ans+=(t-i)**2
print(ans)

Solution (Python):
n = int(input())
x = list(map(int, input().split()))
x_square = [num ** 2 for num in x]
sum_x = sum(x)
sum_x_square = sum(x_square)
minVal = 1000000
for p in range(101):
    minVal = min(minVal, sum_x_square - 2 * p * sum_x + n * p ** 2)
print(minVal)

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

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

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

print(ans)

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

maximum = max(x)
ans = 1000000000
for i in range(1, maximum + 1):
  total = 0
  for j in x:
    total += (j - i)** 2

  if total < ans:
    ans = total

print(ans)

Solution (Python):
def return_distance(X,p):
    A=list(map(lambda a:(a-p)**2,X))
    return sum(A)

N=int(input())
X=list(map(int,input().split()))
min_p=min(X)
max_p=max(X)

ans=return_distance(X,max_p)
for p in range(min_p,max_p):
    if ans>return_distance(X,p):
        ans=return_distance(X,p)
    else:
        pass

print(ans)

Solution (Python):
N = int(input())
X = list(map(int, input().split()))
max_raw = max(X)

ans_raw = []
count = 0

for i in range(1, max_raw + 1):
    count = 0
    for j in range(0, len(X)):
        count += (X[j] - i) ** 2
    ans_raw.append(count)

print(min(ans_raw))

Solution (Python):
N = int(input())
X = list(map(int, input().split()))
M = round(sum(X)/N)
answer = 0
for i in X:
    answer += (i-M)**2
print(answer)

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

p = sum(x)//n
ans1 = 0
ans2 = 0
for i in range(n):
    ans1 += (x[i]-p) ** 2

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

print(min(ans1, ans2))

Solution (Python):
N = int(input())
X = list(map(int, input().split()))
M = round(sum(X)/N)
answer = 0
for i in X:
    answer += (i-M)**2
print(answer)

Solution (Python):
N = int(input())
X = list(map(int, input().split()))
M = round(sum(X)/N)
ans = 0
for i in X:
    ans+=(i-M)**2
print(ans)

Solution (Python):
n = int(input())
*x, = map(int, input().split())
x.sort()
mid = (x[0] + x[n-1]) // 2
ans = float('inf')

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

Solution (Python):
import numpy as np
N = int(input())
A = np.array(list(map(int, input().split(" "))))

M = np.zeros((100, A.shape[0]))

for i in range(100):
    M[i, :] = A - (i + 1)

t = M * M


t1 = np.sum(t, axis=1)


print(int(np.min(t1)))

Solution (Python):
n = int(input())
xlist = list(map(int, input().split()))
s = 0
t = 0
min = 0
ave = int(sum(xlist)/len(xlist))
for j in xlist:
  s += (j-ave)**2
  t += (j-ave-1)**2
if s < t:
  print(s)
else:
  print(t)

Solution (Python):
N = int(input())

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

max_x = max(x_list)
min_x = min(x_list)

each_value_list = []
min_value = 10e1000
for i in range(min_x, max_x + 1):
    tmp_sum = 0
    for x in x_list:
        tmp_sum += (x - i)**2
    if min_value > tmp_sum:
        min_value = tmp_sum

print(min_value)

Solution (Python):
import numpy as np
n=int(input())
i = list(map(int, input().split()))
ia=np.array(i)
ave=np.mean(ia)
ans=0
for k in range(n):
  ans+=(round(ave, 0)-i[k])**2
print(int(ans))

Solution (Python):
n=int(input(''))
x=input('')
x=x.split(' ')
for a in range(n):
    x[a]=int(x[a])

ave=float(0)
for a in x:
    ave=ave+a
ave=ave/n

disa=0
disb=0

for a in x:
    disa=disa+(int(ave)-a)**2
    disb=disb+(int(ave+1)-a)**2

if disa<=disb:
    print(disa)
else:
    print(disb)

Solution (Python):
import math


def main():
    n = int(input())
    array = [int(s) for s in input().split(" ")]
    min_v = min(array)
    max_v = max(array)
    min_ans = float("inf")
    for i in range(min_v, max_v + 1):
        total = 0
        for a in array:
            total += (a - i) ** 2
        if min_ans > total:
            min_ans = total
    print(min_ans)


if __name__ == '__main__':
    main()

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

Solution (Python):
from math import ceil
N = int(input())
X = list(map(int, input().split()))


x = (sum(X)/N)
kouho = [int(x), ceil(x)]
ans = [0,0]
for k in range(len(kouho)):
    for i in range(N):
        ans[k] += (X[i]-kouho[k])**2
print(min(ans))

Solution (Python):
N = int(input())
X = list(map(int, input().split()))
M = round(sum(X)/N)
answer = 0
for i in X:
    answer += (i-M)**2
print(answer)

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

minDis = 0
flag = True

for p in range(1,101):

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

    if flag:
        minDis = dis
        flag = False
        continue

    if dis < minDis:
        minDis = dis

print(minDis)

Solution (Python):
def resolve():
    N = int(input())
    X = list(map(int, input().split()))

    ans = 100000000000

    for p in range(1, 101):
        tmp = 0
        for x in X:
            tmp = tmp + ((x - p) ** 2)

        ans = min(tmp, ans)

    print(ans)
    
resolve()

Solution (Python):
n = int(input());a = input().split();print(min([sum([(int(i)-j)**2 for i in a]) for j in range(1,n+10000)]))

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

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

total_strength = []

for p in range(max(x)+1):
    strength = 0
    for i in range(len(x)):
        strength += (x[i]-p)**2
    total_strength.append(strength)

print(min(total_strength))

Solution (Python):
N=int(input())
X=list(map(int,input().split()))
def tairyoku(n):
    return (n-i)**2
i=1
XT=list(map(tairyoku,X))
ans=sum(XT)
for i in range(2,101):
    XT=list(map(tairyoku,X))
    ans=min(ans,sum(XT))
print(ans)

Solution (Python):

from collections import deque, defaultdict
import bisect


def comb(a, b):
    result = 1
    for i in range(b):
        result *= (a - i) / (i + 1)
    return result


def main():
    N = int(input())
    Xs = list(map(int, input().split()))

    min_load = None
    for p in range(101):
        load = sum([(x - p) ** 2 for x in Xs])
        if min_load is None or min_load > load:
            min_load = load

    print(min_load)


if __name__ == '__main__':
    main()

Solution (Python):

N = int(input())


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

cost_min = 10**20

for i in range(x[0],x[len(x)-1]+1):
    cost = 0
    for j in x:
        cost += (i-j)**2
    cost_min = min(cost_min,cost)

print(cost_min)

Solution (Python):
import math

if __name__ == "__main__":
    n = input()
    X = [int(i) for i in input().split(" ")]
    X.sort()
    
    left = X[0]
    right = X[-1]
    right_p_before = 0
    left_p_before  = 0
    while True:
        left_p  = 0
        right_p = 0
        for i in X:
            left_p  += (i - left )**2
        for i in X:
            right_p += (i - right)**2
        
        if right_p == right_p_before and left_p == left_p_before:
            if right_p > left_p:
                print(left_p_before)
            else:
                print(right_p_before)
            break
        else:
            right_p_before = right_p
            left_p_before  = left_p
            if left_p < right_p:
                right = math.floor(right - (right-left)/2)
            else:
                left  = math.floor(left  + (right-left)/2)

Solution (Python):
N = int(input())
X = list(map(int,input().rstrip().split(" ")))
ans = 0
ansb = 0
for i in range(100):
  ansb = 0
  for i2 in range(N):
    ansb += (X[i2] - i)**2
  if i == 0 or ans > ansb:
    ans = ansb
print(ans)

Solution (Python):
n=int(input())
xl=list(map(int,input().split()))
xl.sort()
ans=[]
for p in range(xl[-1]-xl[0]+1):
    sum1=0
    for xi in xl:
        sum1+=(xi-xl[0]-p)**2
    ans.append(sum1)
print(min(ans))

Solution (Python):
import math

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

num = math.ceil(sum(x)/len(x))
num2 = int(sum(x)/len(x))
res = 0
res2 = 0

for i in x:
    res += (i - num)**2

for i in x:
    res2 += (i - num2)**2

if res < res2:
    print(res)
else:
    print(res2)

Solution (Python):
def main():
  N = int(input())
  X = list(map(int, input().split()))

  ANS = 1000000000000000000000000000000000

  for i in range(1,101):
    C_ANS = 0
    for j in X:
      C_ANS += (i-j)**2
    if C_ANS < ANS:
      ANS = C_ANS
  
  print(ANS)

if __name__ == '__main__':
  main()

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

answer = (max(x)-min(x))**2 * n
for p in range(min(x), max(x)+1):
    count = 0
    for i in x:
        count += (i-p)**2
    answer = min(answer,count)
print(answer)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
ans = 10000000000000
for i in range(101):
    x = 0
    for j in range(n):
        x += (i - a[j])**2
    if ans >= x:
        ans = x
print(ans)

Solution (Python):


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

y = [i ** 2 for i in x]

if sum(x) % n == 0:
    
    print(sum(y)-((sum(x) ** 2) // n))
else:
    t = sum(x) // n
    
    print(min(n * (t ** 2) - 2 * t * sum(x) + sum(y),n * ((t + 1) ** 2) - 2 * (t + 1) * sum(x) + sum(y)))

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

minx = min(a)
maxx = max(a)
ans = 2 * 10 ** 18

for i in range(minx,maxx+1):
  judge = 0
  for j in a:
    temp = (j - i) ** 2
    judge += temp
  if judge < ans:
    ans = judge

print(ans)

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

P = round(sum(X_list)/N)

l = 0
for i in X_list:
    l = l + (i - P)**2

print(l)

Solution (Python):
from functools import lru_cache
def resolve():
    n = int(input())
    x = list(map(int, input().split()))

    x.sort()

    l = 0
    r = max(x)
    
    @lru_cache()
    def cost(p):
        s = 0
        for i in x:
            s += (i - p)**2
        return s

    while l + 1 < r:
        m = (l + r) // 2 
        l_s = cost(l)
        r_s = cost(r)
        if l_s > r_s:
            l = m
        else:
            r = m
    print(min(cost(l), cost(r)))


if __name__ == "__main__":
    resolve()

Solution (Python):
n = int(input())
x_list = list(map(int, input().split()))
ans = (100 ** 2) * 100
for p in range(1, 101):
    y = sum([(x - p) ** 2 for x in x_list])
    ans = min(ans, y)
print(ans)

Solution (Python):
N = int(input())
X = list(map(int, input().split()))
X.sort()
r = None
for i in range(X[0], X[-1]+1):
    c = sum((m - i)**2 for m in X)
    if r is None or r > c:
        r = c

print(r)

Solution (Python):
N = int(input())
List = list(map(int,input().split()))
cost = 10**36
for i in range(101):
  temp = 0
  for X in List:
    temp += (X - i)**2
  if temp <= cost:
    cost = temp
print(cost)

Solution (Python):
inp = int(input())
s = 0
l = input().split(" ")
for i in l:
  s += int(i)


a = 0


m =int(round(s/inp, 0))

for i in l:
  a += (m - int(i))**2
print(a)

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

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

X = np.zeros((100,n))
Y = np.zeros((100,n))
for i in range(100):
    X[i,:] = x
    Y[i,:] = np.full(n,i+1)



Z = X - Y

Z = np.power(Z,2)
W = np.sum(Z,axis=1)
print(int(np.min(W)))

Solution (Python):
def calc(s,t):
       return (t-s)**2

n=int(input())
X=sorted(list(map(int,input().split())))
inf=float("inf")

ans=inf
for i in range(X[0],X[-1]+1):
       u=0
       for j in X:
              u+=calc(i,j)
       ans=min(ans,u)
print(ans)

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

average = round(sum(X)/N)
min_s = 0
for i in range(len(X)):
    min_s += (X[i] - average) * (X[i] - average)
print(min_s)

Solution (Python):


import sys
import math
import time

import collections
from collections import deque
import queue
import copy






N = int(input())
X = [int(x) for x in input().split()]
ans = 9999999999
su = 0

X.sort()

xm = max(X)

for i in range(xm+1):
  for j in range(N):
    su += (X[j]-i)**2
  ans = min(ans, su)
  su = 0


print(ans)

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

M = max(X)
m = min(X)
total = [0] * (M - m + 1)
for i in range(m, M + 1, 1):
    for j in range(N):
        total[i - m] += (X[j] - i) ** 2

print(min(total))

Solution (Python):
import math

N = int(raw_input())
X = map(int, raw_input().split())

sx = 0
sx2 = 0
for i in range(N):
    sx += X[i]
    sx2 += X[i]*X[i]



p = int(sx/N)

print min(N*p*p-2*p*sx+sx2,  N*(p+1)*(p+1)-2*(p+1)*sx+sx2)

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

ans = 10 * 10 ** 6
for i in range(101):
    t = 0
    for j in x:
        t += (j - i) ** 2
    
    ans = min(ans, t)

print(ans)

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

res = 100000000
for p in range(1, 101):
    tmp = 0
    for i in x:
        tmp += (i-p)**2
    if tmp < res:
        res = tmp

print(res)

Solution (Python):
n=int(input())
x=list(map(int,input().split()))
ans=10**100
for i in range(101):
  k=0
  for a in x:
    k+=(a-i)**2
  if k<ans:
    ans=k
print(ans)

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

z = sum(x)//n
z2 = z + 1
ans1 = 0
ans2 = 0
for i in range(n):
	ans1 += (x[i] - z)**2
for i in range(n):
	ans2 += (x[i] - z2)**2
	
print(min(ans1,ans2))

Solution (Python):
def func(n, x, i):
    sum = 0
    for j in range(n):
        sum = sum + (x[j] - i) * (x[j] - i)
    return sum


n = int(input())
x = list(map(int, input().split()))
minnum = 9999999
for i in range(min(x), max(x) + 1):
    f = func(n, x, i)
    if f < minnum:
        minnum = f
print(minnum)

Solution (Python):
N,*X=map(int,open(0).read().split())
X.sort()
_min=10**6
for i in range(1,X[-1]+1):
    _sum=0
    for x in X:
        _sum += (x-i)**2
    if _min > _sum:
        _min = _sum
print(_min)

Solution (Python):
def resolve():
    n = int(input())
    x = list(map(int,input().split()))
    b =  round(sum(x)/n)
    m = 0
    for i in x:
        m += (i-b) * (i-b)
    print(m)
resolve()

Solution (Python):
n=int(input())
x=list(map(int,input().split()))
saisho=999999999
for i in range(min(x),max(x)+1):
  hp=0
  for j in range(n):
    hp+=(x[j]-i)**2
  saisho=min(saisho,hp)
print(saisho)

Solution (Python):
N=int(input())
X=list(map(int,input().split()))
ANS=[0]*100
for i in range(100):
  for x in X:
    ANS[i] += ((x - (i+1)) ** 2)
print(min(ANS))

Solution (Python):
n = int(input())
x = [int(y) for y in input().split()]
ans = []
for i in range(1, 101):
    tmp = 0
    for j in x:
        tmp += (j - i) ** 2
    ans.append(tmp)
print(min(ans))

Solution (Python):
n = int(input())
data = [int(x) for x in input().split()]

temp = []
data.sort()
for i in range(data[0],data[-1]+1):
    temp.append(sum([(x-i)**2 for x in data]))

print(min(temp))

Solution (Python):





def dist(X, P):
    d = 0
    for x in X:
        d += (x-P)*(x-P)
    return d


def solv(N, X):
    ans = 100*100*100 + 1
    for P in range(1, 101):
        d = dist(X, P)
        if ans > d:
            ans = d
    return ans


if __name__ == "__main__":
    N = int(input())
    X = list(sorted(map(int, input().split())))
    ans = solv(N, X)
    print(ans)

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

avg = int(sum(X) / N)
nvA = 0
nvB = 0
for x in X:
    nvA += (avg - x) * (avg - x)
    nvB += (avg + 1 - x) * (avg + 1 - x)
if nvA > nvB:
    print(nvB)
else:
    print(nvA)

Solution (Python):
N=int(input())
A=list(map(int, input().split()))
res=float("inf")
for i in range(max(A)+1):
    res1=0
    for j in range(len(A)):
        res1=res1+(A[j]-i)**2
    res=min(res,res1)
print(res)

Solution (Python):
N = int(input())

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


s = float('Inf')
for i in range(min(x), max(x) + 1):
    score = 0
    for j in x:
        score += (j - i)**2 
    s = min(s, score)

print(s)

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians

from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from functools import reduce
from bisect import bisect_left, insort_left
from heapq import heapify, heappush, heappop

INPUT = lambda: sys.stdin.readline().rstrip()
INT = lambda: int(INPUT())
MAP = lambda: map(int, INPUT().split())
S_MAP = lambda: map(str, INPUT().split())
LIST = lambda: list(map(int, INPUT().split()))
S_LIST = lambda: list(map(str, INPUT().split()))

sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    N = INT()
    X = LIST()

    a = N
    b = (-2) * sum(X)
    c = 0
    for x in X:
        c += x**2

    l = int((-1)*b / (2*a))
    r = ceil((-1)*b / (2*a))
    print(min(a * l**2 + b * l + c, a * r**2 + b * r + c))


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
X = list(map(int, input().split()))
a = 0
b = 0
l = []
for j in range(1,101):
    for i in X:
    
        a = abs((i-j)**2)
        b = b + a
    l.append(b)
    b = 0

print(min(l))

Solution (Python):
n = int(input())
x = [int(x) for x in input().split()]
check = 0
res = 0
for i in x:
    check += i
check2 = round(check / len(x))
for num in x:
    res += (num-check2)**2
print(res)

Solution (Python):
n = int(input())
lst = [int(x) for x in input().split()]

num = sum(lst)//n
sum1 = 0
sum2 = 0

for i in lst:
  sum1 += (i-num)**2
  sum2 += (i-num-1)**2
  
if sum1 < sum2:
  print(sum1)
else:
  print(sum2)

Solution (Python):
import math
import fractions

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    
    return divisors

def ValueToBits(x,digit):
    res = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        res[i]=now%2
        now = now >> 1
    return res

def BitsToValue(arr):
    n = len(arr)
    ans = 0
    for i in range(n):
        ans+= arr[i] * 2**i
    return ans

def ValueToArray10(x, digit):
    ans = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        ans[digit-i-1] = now%10
        now = now //10
    return ans

  
'''
p = 10 ** 9 + 7
N = 10 ** 6 + 2
fact = [1, 1]  
factinv = [1, 1]  
inv = [0, 1]  

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)
    
def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p
'''




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



ans = 10**10

for i in range(101):
    now = 0
    for j in range(n):
        now += (a[j]-i)*(a[j]-i)
    if(ans>now):
        ans = now
print(ans)

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

min = min(x_list)
max = max(x_list)

sum_min = 1000000000000000000
for target_i in range(1, 101):
    sum_tmp = 0
    for i in range(n):
        sum_tmp += (target_i - x_list[i]) ** 2
    if sum_min >= sum_tmp:
        sum_min = sum_tmp

print(sum_min)

Solution (Python):

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

import numpy as np

X_array = np.array(X_list)

print(int(np.sum((X_array - round(np.mean(X_array))) ** 2)))

Solution (Python):
N=int(input())
X=list(map(int,input().split(' ')))
M=0
T=[]
H=[]
for j in range(1,200):
    for i in range(0,N):
        M=M+(X[i]-j)**2
    T.append(M)
    M=0
print(min(T))

Solution (Python):
import numpy as np
n = int(input())
x = input().split()
x = np.array(x,dtype='int')
m = x.sum()
v = (x**2).sum()
pm = int(np.round(m/n))
print(n * pm**2 - 2*pm*m + v)

Solution (Python):
N = int(input())
S = list(map(int, input().split()))
ans = int(sum(S) /N)
def HP(x):
    A = 0
    for i in S:
        A += (i - x) **2
    return A

mi = False
UP = False
DO = False
while(mi == False):
    if HP(ans) > HP(ans+1):
        ans = ans+1
        UP = False
        DO = False
    elif HP(ans) > HP(ans-1):
        ans = ans -1
        UP = False
        DO = False
    elif HP(ans) == HP(ans+1):
        if DO :
            break
        else:
            UP = True
            ans = ans +1
    elif HP(ans) == HP(ans-1):
        if UP:
            break
        else:
            Do = True
            ans = ans -1  
    else:
        mi = True

print(HP(ans))

Solution (Python):
N=int(input())
X = list(map(int,input().split()))
sums = 0
sumsum = 0
sumsumA = 0
P = 0

X.sort()

for i in X:
    sums += i
P = sums // len(X)

for i in X:
    sumsum += (i - P)**2

for i in X:
    sumsumA += (i - (P+1))**2
print(sumsum if sumsum < sumsumA else sumsumA)

Solution (Python):
from sys import stdin

def main():

    input = stdin.readline

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

    avg_min = int(sum(X) / len(X))
    avg_max = avg_min+1

    ans_min = [ (x - avg_min) ** 2 for x in X]
    ans_max = [ (x - avg_max) ** 2 for x in X]

    print(min(sum(ans_min),sum(ans_max)))
    
if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())


min_ = 10000000
sum_ = 0

X = input().split()

for t in range(1,101):
  for i in range(N):
    tmp = int(X[i])
    sum_ += (tmp-t)**2
    
  if min_ > sum_:
    min_ = sum_
    
  sum_ = 0  
      
print(min_)

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

x.sort()

for p in range(x[0],x[len(x)-1]+1):
    psum=0
    for i in range(len(x)):
        psum=psum+(x[0]-x[i])**2

    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

        if pmin>psum:
            pmin=psum
print(pmin)

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

wa = 0
wawa = 0
ava = int(sum(x) / n)
aa = ava+1
for i in x:
    wa += (i-ava)**2
    wawa += (i - aa)**2

if wa > wawa:
    print(wawa)
else:
    print(wa)

Solution (Python):
N=int(input())
n=list(map(float, input().split()))
ave=sum(n)/len(n)
aver=round(ave)
d=[(i-aver)**2 for i in n]
a=sum(d)
print(int(a))

Solution (Python):
import math
n = int(input())
x = list(map(int,input().split()))
k = sum(x)/n
h = 0
if (math.ceil(k)-k)**2 <= (math.ceil(k)-k-1)**2:
    h = math.ceil(k)
else:
    h = math.ceil(k)-1
def m(a,h):
    return((a-h)**2)

y = []
for i in range(n):
    y.append((x[i]-h)**2)

print(sum(y))

Solution (Python):
import sys
import socket

hostname = socket.gethostname()

if hostname == 'F451C':
    sys.stdin = open('c1.in')


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


def read_str_list():
    return input().split()


def read_int():
    return int(input())


def read_str():
    return input()


def main():
    N = read_int()
    X = read_int_list()
    
    p = round(sum(X) / N)
    res = 0
    for i in range(N):
      res += (X[i] - p) ** 2
    print(res)


main()

Solution (Python):
N = int(input())
X = list(map(int, input().split()))
M = round(sum(X)/N)
ans = 0
for i in X:
    ans+=(i-M)**2
print(ans)

Solution (Python):
N = int(input())

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

results = []
for i in range(min(numbers), max(numbers)+1):
    cnt = 0
    for n in numbers:
        cnt += abs(n - i) ** 2
    results.append(cnt)

print(min(results))

Solution (Python):
input = raw_input
n = int(input())

x_list = map(int, input().split(" "))

x_sum = sum(x_list)
x_ave = float(x_sum) / n

import math
m1 = int(math.ceil(x_ave))
m2 = int(math.floor(x_ave))
out1 = sum([(x-m1)**2 for x in x_list])
out2 = sum([(x-m2)**2 for x in x_list])
out = min(out1, out2)
print(out)

Solution (Python):
N = int(input())
X = sorted(list(map(int, input().split())))
p = [[] for _ in range(max(X))]
for i in range(max(X)):
    for j in X:
        p[i].append((j - (i+1)) ** 2)
sums = [sum(pp) for pp in p]
print(min(sums))

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

p_max = max(X)
p_min = min(X)
ans = 0
ans_tmp = 0

for i in range(p_min, p_max):
    for j in range(len(X)):
        ans_tmp += (X[j] - i)**2
    if i == p_min:
        ans = ans_tmp
    elif ans_tmp < ans:
        ans = ans_tmp
    ans_tmp = 0

print(ans)

Solution (Python):
import numpy as np
n = int(input())
X = list(map(int, input().split()))
a = sum(X)
b = sum(X[i]**2 for i in range(n))
m = np.floor(a/n)
if m + 1/2 <= a/n:
  m += 1
s = n*(m**2) - 2*a*m + b
print(int(s))

Solution (Python):
import math

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

g = sum(X)/N

g1 = math.floor(g)
g2 = g1 + 1

ans1 = 0
ans2 = 0

for i in range(N):
    ans1 += (X[i] - g1)**2
    ans2 += (X[i] - g2)**2

print(min(ans1, ans2))

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

x = sorted(x)

sum = 0
for i in x:
    sum += i
p1 = sum // len(x)
p2 = p1+1

ans1 = 0
ans2 = 0
for num in x:
    ans1 += (num-p1)**2
    ans2 += (num-p2)**2
ans = min(ans1,ans2)

print(ans)

Solution (Python):
def main(n,x):
    res = 2**31-1
    for i in range(1,101):
        val = 0
        for j in range(len(x)):
            val += (x[j] - i)**2
        res = min(res, val)
    return res

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

Solution (Python):

 
import math
import itertools
import sys
import copy








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

X.sort()

minN = sys.maxsize
for i in range(100) :
  sum = 0
  for j in range(len(X)) :
    sum += (i-X[j]) ** 2
  minN = min(minN, sum)

print (minN)

Solution (Python):
n = int(input())
x = sorted(map(int,input().split()))
p = sum(x)//len(x)
m = float('inf')
for i in [-1,0,1]:
    a = sum(list(map(lambda x: (x-(p+i))**2, x)))
    m = min(m,a)
print(m)

Solution (Python):
n=int(input())
x=list(map(int,input().split()))
result=0
for p in range(0,max(x)+1):
  s=0
  for i in range(0,n):
    s+=(x[i]-p)**2
  if p==0:
    result=s
  else:
    if s<result:
      result=s
print(result)