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 = 1000000

for i in range(1, 100):
    T = 0
    for j in range(N):
        T += (X[j]-i) ** 2
    if T < min:
        min = T

print(min)

Submission status: Accepted

Solution code:
import numpy as np
 
def main():
    N = int(input())
    A = sorted(map(int, input().split()))
    mean = int(round(np.mean(A)))
    x = np.array(A) - mean
    print(np.dot(x, x))
 
main()

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
T = []
for p in range(100):
    sum = 0
    for i in range(n):
        sum += (x[i] - p+1) *  (x[i] - p+1)
    T.append(sum)

print(min(T))

Submission status: Accepted

Solution code:
n = int(input())
li = list(map(int, input().split()))
max_num = float('inf')
li.sort()
for i in range(1, 101):
    temp_sum = 0
    for j in li:
        temp_sum = temp_sum + ((j - i) ** 2)

    if temp_sum < max_num:
        max_num = temp_sum

print(max_num)

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int, input().split()))
B = []
for j in range(1, 101):
    A = [(i - j)**2 for i in X]
    B.append(sum(A))
print(min(B))

Submission status: Accepted

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

MX = max(X)

TP = []

for i in range(1, MX+1):
  P = 0
  for j in range(N):
    P += (i - X[j])**2
  TP.append(P)
  
print(min(TP))

Submission status: Accepted

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

b = max(K)
s = min(K)
INF = 10**18
t = [INF]*(b+1)
for i in range(s,b+1):
  a = 0
  for j in range(len(K)):
	  a += (K[j] - i)**2
  t[i] = a
print(min(t))

Submission status: Accepted

Solution code:
from statistics import mean
n = int(input())
a = list(map(int, input().strip().split()))

m = round(mean(a))


data = list(map(lambda x: (x-m)**2, a))

print(sum(data))

Submission status: Accepted

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

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

ans = 1 << 60
for i in range(0,101):
    ans = min(ans, cost(i))

print(ans)

Submission status: Accepted

Solution code:
#python3
INF = int(1e9) 
def main():
    n = int(input())
    xs = [int(i) for i in input().split()] 
    ans = INF
    for p in range(0, 101):
        sum = 0
        for i in xs:
            sum += (i-p)**2
        ans = min(ans, sum)
    print(ans)
main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int, input().split()))
p_list = [0]
for P in range(1,101):
    power = 0
    for i in range(N):
        power += (X[i] - P)**2
    p_list.append(power)
sp_list = sorted(p_list)
print(sp_list[1])

Submission status: Accepted

Solution code:
import statistics as stat

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

Xm = int(stat.mean(X)+0.5)
power = 0
for i in range(N):
  power += (X[i]-Xm)**2
print(power)

Submission status: Accepted

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

min_ = min(list)
max_ = max(list)

total = []
if min_ == max_:
  print(0)
else:
  for i in range(min_, max_):
    length = 0
    for j in range(n):
  	  length += (list[j] - i)**2
    total.append(length)
  print(min(total))

Submission status: Accepted

Solution code:
def resolve():
    n = int(input())
    x = list(map(int, input().split()))
    x.sort()
    xx = [0 for i in range(n)]
    for i,j in enumerate(x):
        xx[i] = j**2
    minp = 100
    for i in range(200):
        k = i*(n*i - 2*sum(x))
        if k < minp:
            minp = k
    print(sum(xx)+ minp)
if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
import math
n=int(input())
x=list(map(int,input().split()))
s=sum(x)
st=s/n
if (st-math.floor(st))>0.5:
  p=math.ceil(st)
else:
  p=math.floor(st)
t=0
for m in x:
  t=t+((m-p)**2)
print(t)

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())
x = list( map(int,input().split()) )

#print(max(x),min(x))

min_hp = 1000000000000000
for p in range(min(x),max(x)+1):
    hp = 0
    for i in x:
        hp +=(i-p)**2
        # print(p,i,hp)
    if min_hp > hp:
        min_hp = hp

print(min_hp)

Submission status: Accepted

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

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

min = 100*100*100
for i in range(1, 101):
    sum = 0
    for j in range(N):
        val = (X[j]-i)*(X[j]-i)
        sum += val
    if min > sum:
        min = sum
print(min)

Submission status: Accepted

Solution code:
N=int(input())
X=input().split()
for i in range(len(X)):
    X[i]=int(X[i])
Xmin=min(X)
Xmax=max(X)
for P in range(Xmin,Xmax+1):
    p=0
    for xi in X:
        p+=(xi-P)**2
    if P==Xmin:
        m=p
    elif p<m:
        m=p
print(m)

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int, input().split()))
maxX =max(X)
ansmin = float('INF')
HP = 0
for i in range(1,maxX+1):
    for j in range(N):
        HP += (X[j]-i)**2
    ansmin = min(ansmin, HP)
    HP = 0
print(ansmin)

Submission status: Accepted

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

x.sort()

minD = 0
for x_i in x:
    minD += (x_i-1) ** 2
# print(minD)

for i in range(2, 101):
    nowD = 0
    for x_j in x:
        nowD += (abs(x_j-i)) ** 2
    minD = min(nowD, minD)
print(minD)

Submission status: Accepted

Solution code:
def main():
    a = int(input())
    l = list(map(int, input().split()))
    temp = [0]*100

    #集会の場所
    for p in range(1,101):
        #各人の消費体力
        for score in l:
            temp[p-1] += (score-p)**2
    print(min(temp))
            

if __name__ == '__main__' :
    main()

Submission status: Accepted

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

import math

n = int(input())
l = list(input().split(" "))
l = [int(element) for element in l]

end = max(l)

dist = []

for i in range(1,end+1):
  s = 0
  for e in l:
    s += (e - i)**2
  dist.append(s)

print(min(dist))

Submission status: Accepted

Solution code:

N=int(input())
Y=0
s=0
X=list(map(int,input().split()))
P=0
for i in range(N):
  P+=X[i]

P/=N
D=int(P)
if (P-D)*10>=5:
  Y=int(P)+1
else:
  Y=int(P)
  


for i in range(N):
  s+=(X[i]-Y)**2
  
print(s)

Submission status: Accepted

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

sum = 0
for i in list:
  sum += i
average = sum // len(list)


ans1 = 0
ans2 = 0
ans3 = 0
ans4 = 0
ans5 = 0
for i in list:
  ans1 += (i-average+1)*(i-average+2)
  ans2 += (i-average+1)*(i-average+1)
  ans3 += (i-average)*(i-average)
  ans4 += (i-average-1)*(i-average-1)
  ans5 += (i-average-2)*(i-average-2)
answer = min(ans1,min(ans2,min(ans3,min(ans4,ans5))))
print(answer)

Submission status: Accepted

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

a=0
b=0

for i in range(N):
  b+=2*line[i]

j=round(b/(2*N))

for l in range(N):
  a+=(line[l]-j)**2

print(a)

Submission status: Accepted

Solution code:

N=int(input())
X=list(map(int,input().split()))
X_sum=sum(X)
ans=0
ans_ant=0
P=int(X_sum/N)
P_ant=int(X_sum/N)+1
for i in range(N):
    ans+=(X[i]-P)**2
    ans_ant+=(X[i]-P_ant)**2
print(min(ans,ans_ant))

Submission status: Accepted

Solution code:
import numpy as np
import math
N=int(input())
X=list(map(float,input().split()))
Xmin=int(math.floor(np.min(X)));Xmax=int(math.ceil(np.max(X)))
P=np.array([i for i in range (Xmin,Xmax+1)])
Sum=np.zeros(len(P),dtype=float)
for i in range(len(P)):
    Sum[i]=np.sum((np.array(X)-P[i])*(np.array(X)-P[i]))
print(int(np.min(Sum)))

Submission status: Accepted

Solution code:
from sys import stdin

def main():
    N = int(stdin.readline().rstrip())
    l = list(map(int, stdin.readline().rstrip().split()))
    result = 1000000
    for i in range(0, max(l)+1):
        sum = 0
        for x in l:
            sum += (x - i)**2
        if result > sum:
            result = sum
    print(result)

if __name__ == '__main__':
    main()

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())
X = list(map(int,input().split()))
m = sum(X)//N
def fun(X,m):
  s = 0
  for i in X:
    s += (i-m)**2
  return s

print(min(fun(X,m),fun(X,m+1)))

Submission status: Accepted

Solution code:
import numpy as np
n=int(input())
x=list(map(int,input().split()))
p=[0]*100
y=[0]*n
i=0
j=0
while i<100:
  def ppp(q):
    return (q-i)*(q-i)
  y=list(map(ppp,x))
  p[i]=np.sum(y)   
  i=i+1
r=min(p)
print(r)

Submission status: Accepted

Solution code:
N = int(input())
Xs = list(map(int, input().split()))
 
loss = 0
 
sum = 0
for X in Xs:
  sum += X
  
ave = round(sum/len(Xs))
 
for X in Xs:
  loss += (X-ave)**2
 
print(loss)

Submission status: Accepted

Solution code:
import math

n = int(input())
x = list(map(int, input().split()))
p1 = math.floor(sum(x) / n)
p2 = math.ceil(sum(x) / n)

result1 = 0
result2 = 0
for i in x:
  result1 += ((i - p1) * (i - p1))
  result2 += ((i - p2) * (i - p2))

if result1 <= result2:
  print(result1)
else:
  print(result2)

Submission status: Accepted

Solution code:
N=number=int(input())
X_list=list(map(int, input().split()))
min_pow=0
if N==1:
    print("0")
else:
    X_min=min(X_list)
    X_max=max(X_list)
    for i in range(X_min,X_max):
        sum_pow=0
        for j in range(N):
            sum_pow+=(X_list[j]-i)**2
        if i==X_min:
            min_pow=sum_pow
        if min_pow>sum_pow:
            min_pow=sum_pow
    print(min_pow)

Submission status: Accepted

Solution code:
N = int(input())
X = [int(x) for x in input().split()]
p = round(sum(X)/N)
total = 0
for i in X:
    total += (i - p)**2
print(total)

Submission status: Accepted

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

for i in range(1, 101):
  sum=0
  for j in range(N):
    sum+=(L[j]-i)**2
  if min > sum: min=sum

print(min)

Submission status: Accepted

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

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

ans1 = 0
ans2 = 0
for i in range(n):
    ans1 += (av1 - a[i]) ** 2
    ans2 += (av2 - a[i]) ** 2

print(min(ans1, ans2))

Submission status: Accepted

Solution code:
import statistics
N = int(input())
X = [int(i) for i in input().split()] 
C=0
D=int(format((statistics.mean(X)), '.0f'))
for i in range(N):
	C+=abs(D-X[i])**2
print(C)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce
import pprint
sys.setrecursionlimit(10 ** 9)


INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007



n = I()
X = LI()
ans = INF
for i in range(100):
    ret = 0
    for x in X:
        ret += (i - x) ** 2
    ans = min(ans, ret)


print(ans)

Submission status: Accepted

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


def short(P):
    remain = []
    for i in X:
        remain.append((P-i)**2)
    return sum(remain)

remainder = []
for i in range(1,101):
    remainder.append(short(i))

print(min(remainder))

Submission status: Accepted

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

ans = 100 ** 2 * N
for i in range(min(X), max(X)+1):
  a = 0
  for j in range(N):
    a += (X[j] - i) ** 2
  if ans > a:
    ans = a
print(ans)

Submission status: Accepted

Solution code:
# C - Rally

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

ct = sum(x) / n
ct = int(ct + 0.5)

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
X = array = list(map(int, input().strip().split()))
num = 1
bor = 0
par = 0
score = 0
while par <= N-1:
        score = score + (X[par]-num)**2
        par = par +1
bor = score

while num <= 100:
    par = 0
    score = 0
    while par <= N-1:
        score = score + (X[par]-num)**2
        par = par +1
    if bor > score:
        bor = score
    num = num + 1
print (bor)

Submission status: Accepted

Solution code:
n = int(input())
cor = list(map(int, input().split()))
m = round(sum(cor)/n)
r = 0

for i in cor:
    r = r + (i-m)**2 
    
print(r)

Submission status: Accepted

Solution code:
n=int(input())
x=list(map(int,input().split()))
my_round_int = lambda y: int((y * 2 + 1) // 2)
ans=sum(x)/n
ans=my_round_int(ans)
v=0
for i in x:
    v+=(i-ans)**2
print(v)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
def main():
    N = int(input())
    X = list(map(int,input().split()))
    ans = []
    for i in range(100):
        tmp = 0
        for j in range(N):
            tmp += (X[j]-i)**2
        ans.append(tmp)
    print(min(ans))
if __name__ == '__main__':
    main()

Submission status: Accepted

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

s = sum(x)
p = s / n
q = round(p)

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

print(r)

Submission status: Accepted

Solution code:
from functools import reduce
n = int(input())
X = list(map(int, input().split()))
minv =10000**2
for i in range(min(X),max(X)+1):
    minv = min(minv,sum((j-i)**2 for j in X))
print(minv)

Submission status: Accepted

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

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

X.sort()
start = X[0]
finish = X[-1]

def find_sum(x, A):
    summ = 0
    for a in A:
        summ += (a-x)**2
    return summ

res_len = n*((finish-start)**2)
for j in range(start, finish+1):
    len0 = find_sum(j, X)
    if len0 < res_len:
        res_len = len0

print(res_len)

Submission status: Accepted

Solution code:
N = int(input())
lis = list(map(int, input().split()))
P = []
for p in range(1,101,1):
  sum = 0
  for x in lis:
    sum += pow(x-p,2)
  P.append(sum)
print(min(P))

Submission status: Accepted

Solution code:
N=int(input())
Xs=[int(e) for e in input().split(' ')]

P=int(sum(Xs)/N + 0.5)

sum=0
for X in Xs:
    sum += (X-P)**2

print(sum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8

N = int(input())
X = input().split(" ")
ans = []
for i in range(1,101):
    A = 0
    for j in X:
        x = abs(int(j) - int(i)) 
        A += x * x
    ans.append(A)
ans.sort()
print(ans[0])

Submission status: Accepted

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

res = 0

r = round(sum(x)/len(x))
for i in x:
    res += (i-r)**2
print(res)

Submission status: Accepted

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


def calcCost(P,X):
    sum = 0
    for x in X:
        sum+=(x-P)**2
    return sum

def solve(N: int, X: "List[int]"):
    minCost = 10**16
    m = -1
    for i in range(1,101):
        cost = calcCost(i,X)
        if cost < minCost:
            m = i
            minCost = cost
    print(str(minCost))
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    X = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, X)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

def calc(a,x):
    return(a-x)**2

res = []
for j in range(100):
    for i in range(len(li)):
        tmp = sum(map(calc, [j+1 for x in range(len(li))],li))
        res.append(tmp)

print(min(res))

Submission status: Accepted

Solution code:
import statistics
import math

N = int(input())
input_resident = input()
resident = input_resident.split(" ")
resident_place = []
calc_array = []
result = 0
min = 100
x = 0

for i in resident:
    resident_place.append(int(i))

x = int(round(statistics.mean(resident_place),0))



for R in resident_place:
    result += (R-x)**2

print(result)

Submission status: Accepted

Solution code:
N = int(input().rstrip())
 
A = [int(n) for n in input().rstrip().split(' ')]
 
ans = []
for i in range(1, 100+1):
  temp = 0
  for j in range(len(A)):
    temp += (A[j]-i)**2
  ans.append(temp)
print(min(ans))

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
ans=100000000
for i in range(max(A)-min(A)+1):
  s=0
  for k in range(N):
    s+=(A[k]-(min(A)+i))**2
  if s<ans:
    ans=s
print(ans)

Submission status: Accepted

Solution code:
def f(a,m):
	ans =0
	for x in a:
		ans += abs(m- x)**2
	return ans
		
input()
a =list(map(int, input().split()))
a.sort()
m = (a[0] + a[-1]) //2
ans = f(a,m)
for x in range(a[0],a[-1]+1):
	ans = min(ans,f(a,x))
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int,input().split()))
y = x
y.sort()
mini = 10000000000000
for i in range(y[-1], 0, -1):
    tmp = 0
    for j in x:
        tmp += (j - i)**2
    if mini >= tmp:
        mini = tmp
print(mini)

Submission status: Accepted

Solution code:
import math
N = int(input())
A = list(map(int, input().split()))
X = sum(A)/N
ans = 0
X = format(X, ".0f")
X = int(X)
for i in A:
  ans += (i-X)**2
print(ans)

Submission status: Accepted

Solution code:
X = []
N = int(input())
X = list(map(int, input().split()))
tsum = 0
tsumMin = 10000000000000000000000000


for zahyou in range(1, 101):
    for i in range(N):
        tairyoku = (X[i] - zahyou)**2
        tsum += tairyoku
    if(tsum < tsumMin):
        tsumMin = tsum
        tsum = 0
    else:
        tsum = 0
        
print(tsumMin)

Submission status: Accepted

Solution code:
a = int(input())
b = list(map(int,input().split()))
h =[]
for z in range(min(b),max(b)+1):
  d = 0
  for x in b:
    d = d + (x - z)**2
  h.append(d)
print(min(h))

Submission status: Accepted

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

xmin = min(x)
xmax = max(x)

ans = sum([(i-xmax)**2 for i in x])

for j in range(xmin,xmax):
  ans = min(ans,sum([(i-j)**2 for i in x]))

print(ans)

Submission status: Accepted

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

for i in range(1, 101):
  goukei = 0
  for j in x:
    basyo = int(j)
    tasu = (i - basyo) ** 2
    goukei += tasu
  if i == 1:
    r = goukei
  if goukei < r:
    r = goukei

print(r)

Submission status: Accepted

Solution code:
N = int(input())
inList = list(map(int, input().split()))
sqList = [item ** 2 for item in inList]

mySum1 = sum(inList)
mySum2 = sum(sqList)

x_p = float(mySum1) / float(N)

if x_p < 1:
  Ans = mySum2
elif x_p > 100:
  Ans = N * (100 ** 2) - 2 * (mySum1 * 100) + mySum2
else:
  x_p_int = int(x_p)
  Ans = min(N * ((x_p_int    ) ** 2) - 2 * (mySum1 * (x_p_int    )) + mySum2, 
            N * ((x_p_int + 1) ** 2) - 2 * (mySum1 * (x_p_int + 1)) + mySum2)

print(Ans)

Submission status: Accepted

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

import sys
import math


debug = False

def log(text):
    if debug:
        print(text)

def parse_input(lines_as_string = None):

    global debug
    lines = []
    if lines_as_string is None:
        debug = False
        # for line in sys.stdin:
        #     lines.append(line)
        lines.append(input())
        lines.append(input())
    else:
        debug = True
        lines = [e for e in lines_as_string.split("\n")][1:-1]
    
    n = int(lines[0])
    x = [int(e) for e in lines[1].split(" ")]

    return (n, x)


def solve(n, x):


    result = float('inf')
    for p in range(1, 101):
        tmp = int(sum([(e - p)**2 for e in x]))
        if tmp < result:
            result = tmp

    return  result


def main():
    # 出力
    result = solve(*parse_input())
    if isinstance(result, list):
        for r in result:
            print("%s" % r, sep='')
    else:
        print("%s" % result, sep='')

if __name__ == '__main__':

    main()

Submission status: Accepted

Solution code:
input()
X = tuple(map(int, input().split()))
print(min(sum((x - p) ** 2 for x in X) for p in range(min(X), max(X) + 1)))

Submission status: Accepted

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

Submission status: Accepted

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

if len(ans)==0:
    print(0)
else:
    print(min(ans))

Submission status: Accepted

Solution code:
#C
#入力
N=int(input())
X=list(map(int,input().split()))

#処理
import numpy as np
X=np.array(X)
Z=[i**2 for i in range(101)]
Y=X-1
S=np.array([Z[i] for i in Y])
T=np.sum(S)
for i in range(2,np.max(X)+1):
    Y=X-i
    S=np.array([Z[abs(i)] for i in Y])
    SS=np.sum(S)
    if int(SS)<int(T):
        T=SS
print(T)

Submission status: Accepted

Solution code:

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

s_max = max(s_list)
s_min = min(s_list)

# print('s_max')
# print(s_max)
# print('s_min')
# print(s_min)

j = s_min
k = 0
sum_testcase = 0

kingnum = 1000000

# リスト中の最小値から最大値まで全部回してみる
while j <= s_max :

    # print('[j]')
    # print(j)

    sum_testcase = 0
    k = 0

    # リストの中身を1要素ずつ計算
    while k < len(s_list) :

        # print('s_list[k]')
        # print(s_list[k])

        temp =  (s_list[k] - j) * (s_list[k] - j)
        
        # print('[temp]')
        # print(temp)
        
        k = k + 1
        sum_testcase = sum_testcase + temp

    # 答えが最小なら更新
    if sum_testcase < kingnum :
        kingnum = sum_testcase

    # print('[kingnum]')
    # print(kingnum)

    j = j + 1

print(kingnum)

Submission status: Accepted

Solution code:
n = int(input())

l = [int(i) for i in input().split()]
l.sort()
ans = 1000000000
b = 0
for i in range(1,l[-1]+1):
    b = 0
    for j in l:
        b += (i-j)**2
    if b <ans:
        ans = b
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
xl = list(map(int, input().split()))
ans = 0
for x in xl:
  ans += (x-1)**2

for i in range(2, 101):
  tmp = 0
  for x in xl:
    tmp += (x-i)**2
  if tmp < ans:
    ans = tmp

print(ans)

Submission status: Accepted

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

hp_sums = []

for i in range(max(X)):
    P = i+1
    distance_list = list(map(lambda x: (x-P)**2, X))
    hp_sum = sum(distance_list)
    hp_sums.append(hp_sum)

print(min(hp_sums))

Submission status: Accepted

Solution code:
if __name__ == '__main__':
  num_resident = int(input())
  
  x_list = [int(a) for a in input().split()]
  min_x = min(x_list)
  max_x = max(x_list)
  
  fin_ps = 0
  for i in range(min_x, max_x + 1):
    sum_ps = 0
    for x_i in range(num_resident):
      sum_ps += pow((x_list[x_i] - i), 2)
    
    if(i == min_x):
      fin_ps = sum_ps 
      
    if(sum_ps < fin_ps):
      fin_ps = sum_ps
  
  print(fin_ps)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
A = np.array(list(map(int,input().split())), dtype = 'int64')
a1 = np.min(A)
a2 = np.max(A)

ans = 1000000
for p in range(a1,a2+1):
    B = (A-p)**2
    s = np.sum(B)
    if s < ans:
        ans = s

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
N=int(input())
x=list(input().split())
X = [int(i) for i in x]
X =  np.array(X)
#print(N)
#print(X)

n_max = max(X)
s0 = sum((X)**2)
#print(s0)

for P in range(1,n_max+1):
    #print("P=",P)
    #print((X-P))
    #print((X-P)**2)
    #print(sum((X-P)**2))
    s = sum((X-P)**2)
    if s < s0:
        s0 = s
        P0 = P
print(s0)

Submission status: Accepted

Solution code:
n=int(input())
x=list(input().split())
x=[int(s) for s in x]
if n==1 or len(set(x))==1:
    print(0)
else:
    b_li=[]
    for i in range(min(x),max(x)):
        a_li=[]
        for j in range(n):
            a=(x[j]-i)**2
            a_li.append(a)
        b=sum(a_li)
        b_li.append(b)
    print(min(b_li))

Submission status: Accepted

Solution code:
# coding: utf-8


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

    max_number = max(numbers)
    min_number = min(numbers)
    min_sums = []
    for p in range(min_number, max_number + 1):
        sum_ = 0
        for x in numbers:
            sum_ += (x - p) ** 2
        min_sums.append(sum_)

    print(min(min_sums))

    return 0


if __name__ == '__main__':
    main()

Submission status: Accepted

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


print(min(l))

Submission status: Accepted

Solution code:
n, *ps = map(int, open(0).read().split())
pave = round(sum(ps)/n)
psig = 0
for p in ps:
  psig += (p-pave)**2
print(psig)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int, input().split()))
result = -1
for i in range(1,101):
    st = 0
    for item in X:
        st += (item - i) ** 2
    if result == -1 or result > st:
        result = st
print(result)

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int, input().split()))
X.sort(reverse=True)
sum = []
p = 0

for k in range(X[0]):
	sum.append(0)
	p += 1
	for i in range(len(X)):
		sum[k] += abs(X[i] - p) ** 2

sum.sort(reverse=False)        
print(sum[0])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
R = list(map(int, input().split()))
ans = []

for j in range (1, 100):
    diff = 0
    sum = 0
    
    for i in range (N):
        diff = pow(R[i] - j, 2)
        sum += diff
    
    ans.append(sum)

print(min(ans))

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int,input().split(" ")))
x_max = max(x)
x_min = min(x)
l =[]

for i in range(x_min,x_max + 1):
    si = 0
    for j in range(n):
        xj = x[j]
        xj_p = (xj - i) ** 2
        si += xj_p
    l.append(si)

l.sort()
print(l[0])

Submission status: Accepted

Solution code:
num=int(input().rstrip())
#print(num)
position_line = input().rstrip().split(" ")

#print(position_line)

position_line2 = [int(s) for s in position_line]


position_line2.sort()
#print(position_line2)

posiP=0
for i in range(num):
    posiP+=position_line2[i]

posiP=round(posiP/num) 

#posiP=(position_line2[0]+position_line2[num-1])//2
#print(posiP)

sum1=0

for i in range(num):
   a=(position_line2[i]-posiP)**2
   #print(a)
   sum1+=a

print(sum1)

Submission status: Accepted

Solution code:
import math
n = int(input())
X = sorted(list(map(int, input().split())))
start = X[0]
end = X[-1]

ans = 10000000
for i in range(start, end+1):
    tmp = sum([(x-i)**2 for x in X])
    ans = min(tmp,ans) 
print(ans)

Submission status: Accepted

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

cost = 0
sum = 0
for x in X:
    sum += x

P = round(sum/N)

for x in X:
    cost += (x-P)*(x-P)

print(cost)

Submission status: Accepted

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

for i in range(100):
    hp = 0
    point = i
    for j in x:
        hp += (j - point) ** 2
        if loss < hp:
            break
    else:
        loss = hp
print(loss)

Submission status: Accepted

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

ans = []

for i in range(min(x),max(x)+1):#Pは最大から最小までのどこか
    tmp = 0
    for j in x:
        tmp += (i-j)**2
    ans.append(tmp)

print(min(ans))

Submission status: Accepted

Solution code:
import numpy as np

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

ave = 0
for i in range(N):
  ave += X[i]/N
ave = int(np.round(ave))
sum = 0
for i in range(N):
  sum += (X[i]-ave)**2
print(sum)

Submission status: Accepted

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

Submission status: Accepted

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

h=sum(x)//N
s=0
v=0

for i in range(0,N):
  s+=(x[i]-h)**2
  v+=(x[i]-h-1)**2
print(min(s,v))