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,*X = map(int, open(0).read().split())
p = round(sum(X)/n)
print(sum((x-p)**2 for x in X))

Submission status: Accepted

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

ans = 10 ** 6
for p in range(1, 101):
    s = 0
    for xi in X:
        s += (xi - p) ** 2
    ans = min(ans, s)

print(ans)

Submission status: Accepted

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

p = sum(x)/n

h = 0
for i in x:
  h += abs(i - round(p))**2
  
print(math.ceil(h))

Submission status: Accepted

Solution code:
import math

N = int(input())

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

m1 = math.ceil(sum(X) / N)
m2 = math.floor(sum(X) / N)

res1 = 0
res2 = 0

for i in range(N):
    res1 += (X[i] - m1) ** 2
    res2 += (X[i] - m2) ** 2
    
print(min(res1, res2))

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int, input().split()))
costs = []
for i in range(1,101):
    cost = 0
    for x in X:
        cost += (x-i)**2
    costs.append(cost)
print (min(costs))

Submission status: Accepted

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

ans = 10000000
for p in range(1,101):
    s = 0
    for x in X:
        s += (x - p) ** 2
    ans = min(ans, s)
    
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
X = sorted(list(map(int, input().split())))
if N == 1:
    print(0)
else:
    r = X[-1] - X[0]
    ans = float("inf")
    for i in range(r + 1):
        R = X[0] + i
        hp = 0
        for j in range(N):
            hp = hp + (R - X[j]) ** 2

        ans = min(hp, ans)

    print(ans)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
X = np.array(list(map(int, input().split())))
cost = float("inf")
for i in range(max(X) - min(X) + 1):
    p = min(X) + i
    cost = min(np.sum((X - p) ** 2), cost)
print(cost)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
X = np.array(list(map(int, input().split())))
cost = float("inf")
for i in range(101):
    cost = min(np.sum((X - i) ** 2), cost)
print(cost)

Submission status: Accepted

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

p = round(sum(X)/N)

ans = 0
for x in X :
  ans += (x-p)**2

print(ans)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
X = np.array(list(map(int, input().split())))
costs = []
for i in range(101):
    costs.append(np.sum((X - i) ** 2))
print(min(costs))

Submission status: Accepted

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


_min = pow(10,9)

for p in range(min(x), max(x)+1):
  total = 0
  for xi in x:
    total += pow(xi - p, 2)
  if total < _min:
    _min = total
print(_min)

Submission status: Accepted

Solution code:
# coding: utf-8
# submission # - User: herp_sy
# https://atcoder.jp/contests/
#
# lang: Python3 (3.4.3)

import math
import statistics
import numpy as np
import queue
import itertools

n = int(input())
x = np.array(list(int(i) for i in input().split()))

ans = 0xFFFFFFFF
for p in range(1, 100 + 1):
	ans = min(np.sum((x - p) ** 2), ans)
print(ans)

Submission status: Accepted

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

ans = 1000000000
for p in range(1,101):
  score = 0
  for x in X:
    score += (x-p)**2
  ans = min(ans, score)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
x = [int(i) for i in input().split()]

ans = 100 * 100 * 100
tmp = 0
for i in range(-1000, 1001): #iは集会を開く場所
    for j in range(n):
        tmp += pow(x[j] - i, 2)
    if ans > tmp:
        ans = tmp
    tmp = 0
print(ans)

Submission status: Accepted

Solution code:
import math
import itertools
import fractions
import heapq
import collections
import bisect
import sys
import copy

sys.setrecursionlimit(10**9)
mod = 10**7+9
inf = 10**20

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
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 S(): return input()

n = I()
x = LI()

mx = max(x)
mn = min(x)

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

Submission status: Accepted

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

wk = 0
jyuusin = round(sum(a)/len(a))
for i in a:
    wk += (i-jyuusin)**2
print(wk)

Submission status: Accepted

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import collections

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))

N = int(input())
X = LI()

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

Submission status: Accepted

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

#output
a = 0
for i in range(N):
    a += X[i]

P = round(a/N)
answer = 0
for i in range(N):
    answer += (P-X[i])**2
    
print(answer)

Submission status: Accepted

Solution code:
N = int(input())

X = [int(i) for i in input().split()]
 
s = 10**9  
for i in range(1,100):
  a = []
  for j in range(N):
    a.append((X[j]-i)**2)
     
  s = min(s,sum(a))  

print(s)

Submission status: Accepted

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

arr=[]
for p in range(101):
  distance=0
  for xi in x:
    distance+=(xi-p)**2
  arr.append(distance)

print(min(arr))

Submission status: Accepted

Solution code:
n, *a = map(int, open(0).read().split())

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

Submission status: Accepted

Solution code:
n = int(input())
places = list(map(int, input().split()))
def cost(point):
    res = 0
    for i in places:
        res += ((i-point) * (i-point))
    return res
ans = float('inf')
for i in range(1, 101):
    ans = min(ans, cost(i))
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
xlist = [int(i) for i in input().split()]
k = 0
dm = (100**2)*100#最大値
while k <= max(xlist):
    d = 0
    for j in range(n):
        d += (xlist[j]-k)**2
    k += 1
    if d < dm:
        dm = d
        xm = k
print(dm)

Submission status: Accepted

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

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

d = 0
for p in range(min(x), max(x) + 1):
    c = 0
    for xi in x:
        c += (xi - p) ** 2

    d = c if not d else min(d, c)

print(d)

Submission status: Accepted

Solution code:
# C - Rally
def main():
    N, *X = map(int, open(0).read().split())
    calc = lambda x, p: (x - p) ** 2
    P = sum(X) // N
    ans = min(sum(calc(x, p) for x in X) for p in range(P - 2, P + 2))
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# C - Rally
def main():
    N, *X = map(int, open(0).read().split())
    left, right = min(X), max(X)
    ans = min(sum(map(lambda x: (x - p) ** 2, X)) for p in range(left, right + 1))
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# C - Rally
def main():
    N, *X = map(int, open(0).read().split())
    calc = lambda x, p: (x - p) ** 2
    left, right = min(X), max(X)
    ans = min(sum(calc(x, p) for x in X) for p in range(left, right + 1))
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ANSHUL GAUTAM
# IIIT-D

from math import *
from copy import *
from string import *				# alpha = ascii_lowercase
from random import *				# l.sort(key=lambda l1:l1[0]-l1[1]) => ex: sort on the basis difference
from sys import stdin
from sys import maxsize
from operator import *				# d = sorted(d.items(), key=itemgetter(1))
from itertools import *
from collections import Counter		# d = dict(Counter(l))
from collections import defaultdict # d = defaultdict(list)

'''

'''

def solve(l):
	n = len(l)
	minn = 999999999999
	for spot in range(min(l),max(l)+1):
		curr = 0
		for i in range(n):
			curr += (l[i] - spot)**2
		minn = min(minn,curr)
	return minn


N = int(stdin.readline())
l = list(map(int, stdin.readline().rstrip().split()))
ans = solve(l)
print(ans)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
printV = lambda x: print(*x, sep="\n")
printH = lambda x: print(" ".join(map(str,x)))
def IS(): return sys.stdin.readline()[:-1]
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LII(rows_number): return [II() for _ in range(rows_number)]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]

def main():
	N = II()
	X = LI()
	m = round(sum(X) / N)
	ans = 0
	for x in X:
		ans += ((x-m)**2)
	print(ans)

if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
import sys
import numpy as np

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
printV = lambda x: print(*x, sep="\n")
printH = lambda x: print(" ".join(map(str,x)))
def IS(): return sys.stdin.readline()[:-1]
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LII(rows_number): return [II() for _ in range(rows_number)]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]

def main():
	N = II()
	X = LI()
	X = np.array(X)
	m = round(np.sum(X) / N)
	print(int(np.sum((X-m)**2)))

if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
import sys
import numpy as np

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
printV = lambda x: print(*x, sep="\n")
printH = lambda x: print(" ".join(map(str,x)))
def IS(): return sys.stdin.readline()[:-1]
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LII(rows_number): return [II() for _ in range(rows_number)]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]

def main():
	N = II()
	X = LI()
	X = np.array(X, dtype="int32")
	m = round(np.sum(X) / N)
	print(int(np.sum((X-m)**2)))

if __name__ == '__main__':
	main()

Submission status: Accepted

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

Submission status: Accepted

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

ret = min(
    sum(
        (x - p) * (x - p)
        for x in xs
    ) for p in range(1, 100 + 1)
)

print(ret)

Submission status: Accepted

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

min_n = min(X_list)
max_n = max(X_list)
min_cost = 10**16

for n in range(min_n,max_n+1):
    cost = 0
    for x in X_list:
        cost = cost + (x - n)**2
    min_cost = min(min_cost, cost)

print(min_cost)

Submission status: Accepted

Solution code:
_,x=open(0);print(min(sum((i-int(j))**2for j in x.split())for i in range(99)))

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
m=sum(l)/n
if m-int(m) < 0.5:
  m=int(m)
else:
  m=int(m)+1
print(sum([(k-m)**2 for k in l]))

Submission status: Accepted

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

c = round(sum(A)/N)
ans = 0
for i in range(N):
  ans += (A[i] - c) ** 2
print(ans)

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 += round( (x_list[i] - p) ** 2 )
    ans +=  (x_list[i] - p) ** 2 

print(ans)

Submission status: Accepted

Solution code:
import sys
import numpy as np

read = sys.stdin.read

N, *X = map(int, read().split())
X = np.array(X, np.int64)
n = np.arange(X.max() + 1)
answer = (X.reshape(-1, 1) - n) ** 2
answer = answer.sum(axis=0)
print(answer.min())

Submission status: Accepted

Solution code:
N = int(input())
X_list = list(map(int, input().split()))
min1 = 10**9
for i in range(100):
    tmp = 0
    for j in range(N):
        tmp += (X_list[j]-i)**2
    if min1 > tmp:
        min1 = tmp
print(min1)

Submission status: Accepted

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


def solve(N: int, X: "List[int]"):
    p = round(sum(X) / len(X))
    ans = sum([(x - p)**2 for x in X])
    print(ans)

    #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())
x = list(map(int, input().split()))
avg = round(sum(x) / n)
ans = 0
for xi in x:
    ans += (xi - avg) ** 2
print(ans)

Submission status: Accepted

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

p=sum(x)/n
if p-(sum(x)//n)<0.5:
    p=sum(x)//n
else:
    p=(sum(x)//n)+1

ans=[]
for i in range(n):
    ans.append((x[i]-p)**2)
print(sum(ans))

Submission status: Accepted

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

Submission status: Accepted

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

min = 1000000
result = 0
for i in range(100):
    for j in X:
        result += (i + 1 - j) ** 2
    if min > result:
        min = result
    result = 0

print(min)

Submission status: Accepted

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

Submission status: Accepted

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

print(min(sum_dif_ls))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
 
#整数input
def ii(): return int(sys.stdin.readline().rstrip()) #int(input())
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) #list(map(int,input().split()))
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]
#文字列input
def ss(): return sys.stdin.readline().rstrip() #input()
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) #list(input().split())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]

#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？

n=ii()
arr=limii()
ans=10**1000
for i in range(100):
    cnt=0
    for j in range(n):
        cnt+=(arr[j]-(i+1))**2
    ans=min(ans,cnt)
print(ans)

Submission status: Accepted

Solution code:
_ = int(input())
x = list(map(int,input().split()))
def p(m):
  l = [] 
  for i in x:
    s = (i-m)**2
    if s < 0:
      l.append(-s)
    else:
      l.append(s)
  return(sum(l))
a = [p(i) for i in range(100)]
print(min(a))

Submission status: Accepted

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

N = int(input())
X = [int(x) for x in input().split()]
P = int(sum(X) / N + 0.5)
total = 0
for x in X:
  kyori = (x - P) * (x - P)
  total += kyori
print(total)

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int,input().split()))
p = round((1/n) * sum(x))

ans = 0
for i in x:
    ans += (p -i) ** 2

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
x=list(map(int,input().split()))
p=0
for i in range(n):
    p+=2*x[i]
  
p1=p//(2*n)
p2=p1+1

    
ans1=0
ans2=0
for j in range(n):
    ans1+=(x[j]-p1)**2
    ans2+=(x[j]-p2)**2
    
print(min(ans1,ans2))

Submission status: Accepted

Solution code:
N = int(input())  #1行目
X = list(map(int,input().split()))  #2行目

wa=[]
for p in range(1,101):
    T=[]
    for i in range(0,N):
        T.append((X[i]-p)**2)
    wa.append(sum(T))

print(min(wa))

Submission status: Accepted

Solution code:
def slv(x, p):
    return (x-p)**2
n = int(input())
X = list(map(int, input().split()))
m = 10**6
mx = max(X)
for i in range(0, mx+1):
    t = 0
    for j in X:
        t += slv(j, i)
    if t < m:
        m = t

print(m)

Submission status: Accepted

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

N = int(input())
X = [int(x) for x in input().split()]
P = int(sum(X) / N + 0.5)
total = 0
for x in X:
  kyori = (x - P) * (x - P)
  total += kyori
print(total)

Submission status: Accepted

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

X.sort()
top = X[-1]
bot = X[0]
lowest = 1145141919810112231231231231081081081081072072072

for i in range(bot, top+1):
  cal = 0
  for x in range(N):
    cal += (X[x] - i)**2
  
  if cal < lowest:
    lowest = cal
    
print(lowest)

Submission status: Accepted

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

    x_ave = sum(X) // N
    return min(sum([(x - (x_ave + p))**2 for x in X]) for p in (0, 1))

print(c_rally())

Submission status: Accepted

Solution code:
n = int(input())
index = list(map(int, input().split()))
energy = []
for p in range(min(index), max(index)+1):
    sum = 0
    for xi in index:
        sum += (xi-p)**2
    energy.append(sum)
print(min(energy))

Submission status: Accepted

Solution code:
import sys
import numpy as np
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

N, *X = list(map(int, read().split()))

X = np.array(X)
P = X.sum() // N

ans = 10**8 + 7

for i in range(-1, 2):
    acc = X - (P + i)
    acc = acc**2
    acc = acc.sum()

    ans = min(ans, acc)

print(ans)

Submission status: Accepted

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

lower = min(X)
upper = max(X)

min_shouhi = 0
asn_zahyou = 0
for zahyou in range(lower, upper):
    shouhi = 0
    for i in range(N):
        shouhi += (X[i] - zahyou)**2
    
    if zahyou == lower:
        min_shouhi = shouhi
        asn_zahyou = zahyou
    else:
        if min_shouhi > shouhi:
            min_shouhi = shouhi
            asn_zahyou = zahyou

print(min_shouhi)

Submission status: Accepted

Solution code:
n = int(input())
arr = list(map(int, input().split()))
m = 1000000000000000
for p in range (1, max(arr)+1):
  r = 0
  for i in arr:
    r += (p-i)**2
  m = min(m, r)
print(m)

Submission status: Accepted

Solution code:
n = int(input())
r = list(map(int, input().split()))
old = None
for x in range(min(r), max(r)+1):
  t = sum([(i - x) ** 2 for i in r])
  if not old:
      old = t
      continue
  if t < old:
    old = t
print(old)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int, input().split()))
A = []
for P in range(1, max(X)+1):
    S = 0
    for i in range(N):
        S = S + (X[i]-P)**2
    A.append(S)

print(min(A))

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int, input().split()))
sum = 0
all = True
for a in X:
    sum = sum + a
C = int(sum / N + 0.5)
ans = 0
for b in X:
    ans = ans + (b-C)**2
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
x = [int(i) for i in input().split()]
d = []
for i in range(min(x), max(x)+1):
  a = 0
  for j in x:
    a += (j - i)**2
#    print("({}-{})**2={}".format(j,i,(j - i)**2))
  d.append(a)
print(min(d))

Submission status: Accepted

Solution code:
def rally(n, x):
    if not len(x) == n:
        return "==== pleas input based on n times in one line ===="
    else:
        mini = round(sum(x)/n)
        new_list = []
        for i in x:
            r = (i - mini)**2
            new_list.append(r)
        return (sum(new_list))


n = int(input())
x = list(map(int, input().split())) #input based on n
print(rally(n, x))

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
p = round(sum(x) / len(x))
a = 0

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

Submission status: Accepted

Solution code:
#!python3

iim = lambda: map(int, input().rstrip().split())

def resolve():
    N = int(input())
    X = list(iim())

    av = (2 * sum(X) + N) // N // 2
    ans = 0
    for x in X:
        ans += (x - av)**2
    print(ans)


if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
N = int(input())
X=list(map(int, input().split())) 
X_max = max(X)
X_min = min(X)
 
answerlist = []
for i in range(X_min,X_max+1):
  newX = [(n-i)**2 for n in X]
  total = sum(newX)
  answerlist.append(total)
  
print(min(answerlist))

Submission status: Accepted

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

p = sum(x) / N
p0 = p - int(p)

if p0 <= 0.5:
    p = int(p)
else:
    p = int(p) + 1

ans = 0
for i in x:
    ans += (p - i) ** 2

print(ans)

Submission status: Accepted

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

for p in range(101):
    cost.append(0)
    for person in people:
      cost[-1]+=(person-(p+1))**2

print(min(cost))

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int, input().split()))
A = []
for P in range(1,max(X)+1):
    for i in range(N):
        A.append((X[i]-P)**2)
    if sum(A[0:N]) <= sum(A[N:2*N]) or len(A) == N:
        A = A[0:N]
    else:
        A = A[N:2*N]
print(sum(A))

Submission status: Accepted

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

ans = 100000000
for i in range(100):
  cost = 0
  for j in range(len(A)):
    cost += (A[j] - i) *(A[j] - i)
  ans = min (ans,cost)
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
X = np.array(list(map(int, input().split())))
x = int(np.sum(X) / N + 0.5)
print(np.sum(np.square(X - x)))

Submission status: Accepted

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

Max = max(X)
p_min = 1000000
for i in range(1, 101):
    p = 0
    for j in range(len(X)):
        p += (X[j] - i)**2
    if  p < p_min:
        p_min = p
    else:
        p_min = p_min
print(p_min)

Submission status: Accepted

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

Max = max(X)
p_min = 1000000000
for i in range(1, 101):
    p = 0
    for j in range(len(X)):
        p += (X[j] - i)**2
    if  p < p_min:
        p_min = p
    else:
        p_min = p_min
print(p_min)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 100 ** 2 * 100
for i in range(X[0], X[-1] + 1):
    work = 0
    for x in X:
        work += (x - i) ** 2
    ans = min(ans, work)

print(ans)

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split());print(sum((round(sum(a)/n)-i)**2 for i in a))

Submission status: Accepted

Solution code:
input()
l=list(map(int, input().split()))
m=round(sum(l)/len(l))

print(sum((m-i)**2 for i in l))

Submission status: Accepted

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

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

ans = []

for i in range(a,b+1):
  S = 0
  for j in range(N):
    S += (X[j]-i)**2

  ans.append(S)
  
print(min(ans))

Submission status: Accepted

Solution code:
N = int(input())
X = [int(x) for x in input().split()]
X.sort()
P = [[0 for i in range(N)] for _ in range(101)]
for p in range(101):
  for i, x in enumerate(X):
    P[p][i] = (x - p) ** 2
print(min([sum(p) for p in P[1:]]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,*x=map(int,open(0).read().split())
ans=float("inf")
for p in range(0,101):
  s=0
  for xi in x:
    s+=(xi-p)**2
  ans=min(ans,s)
print(int(ans))

Submission status: Accepted

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


def cost(live_pos, held_pos):
    return (live_pos - held_pos) ** 2

mean_pos = round(sum(live_pos_list) / n)
print(sum(map(lambda live_pos: cost(live_pos, mean_pos), live_pos_list)))

Submission status: Accepted

Solution code:

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

a = []
for pp in range(1,101):
    sum = 0
    for xx in x:
        sum += abs((xx-pp) ** 2)
    a.append(sum)

print(min(a))

Submission status: Accepted

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

ans = float('inf')
for i in range(1, 101):
  temp = 0
  for z in x:
    temp += (z - i) ** 2
  ans = min(temp, ans)

print(ans)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

N = int(input())
X = list(map(int, input().split()))
ans = []
for i in range(1, 101):
    cost = 0
    for value in X:
        cost += (value - i)**2
    ans.append(cost)

print(min(ans))

Submission status: Accepted

Solution code:
n = int(input())

l = list(map(int,input().split()))
#n,m = map(int,input().split())
c=0
for i in l:
    c+=i

c=c//n
c1 = c+1 

#print(c,c1)

ans=0
for i in l:
    ans+=(c-i)*(c-i)
    
ans1=0
for i in l:
    ans1+=(c1-i)*(c1-i)
print(min(ans,ans1))

Submission status: Accepted

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

xList = []
for i in XXX:
  xList.append(int(i))

dList = []
for P in range(0,max(xList)+1):
  D = 0
  for xi in xList:
    D += (xi - P)*(xi - P)
  dList.append(D)

result = min(dList)

print(result)

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int,input().split()))
a = sum(x)/n
if a-int(a) < 0.5:
  aa = int(a)
  print(sum([(xi-aa)**2 for xi in x]))
else:
  aa = int(a)+1
  print(sum([(xi-aa)**2 for xi in x]))

Submission status: Accepted

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

    x_sum = sum(X)
    option = (x_sum // N, (x_sum + N - 1) // N)
    return min(sum([(x - p)**2 for x in X]) for p in option)

print(c_rally())

Submission status: Accepted

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

P = float(sum(X) / N)
P = round(P, 0)

T = 0

for i in range(N):
  T = T + (X[i] - P)**2


print(int(T))

#print('N=', N)
#print('X=', X)

Submission status: Accepted

Solution code:
n=int(input())
l= list(int(i) for i in input().split())
a=round(sum(l)/n)
k=0
for j in l:
  k+=(j-a)**2
print(k)

Submission status: Accepted

Solution code:

n = int(input())
x = list(map(int, input().split()))
cost = 10**9
for i in range(1,100):
    now = 0
    for j in x:
        now += (i-j)**2
    if cost > now:
        cost = now
print(cost)

Submission status: Accepted

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

ans = 10 ** 18
ind= -1
for p in range(0, 101):
    tmp = 0
    for num in x:
        tmp += (num - p) ** 2
    if tmp < ans:
        ans = tmp
        ind = p
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
l= list(int(i) for i in input().split())
a=round(sum(l)/n)
k=0
for j in l:
  k+=(j-a)**2
print(k)