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:
# Code for C - Rally
# Use input() to fetch data from STDIN
n = int(input())
x = [int(x) for x in input().split()]

h = []
for s in range(1, 101):
    total = 0
    for t in x:
        total += (s - t) * (s - t)
    h.append(total)
print(min(h))

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
x.sort()
ans = 10**9+7
for i in range(x[0], x[n-1]+1):
    res = []
    for j in range(n):
        res.append((i-x[j])**2)
    ans = min(sum(res), ans)
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
X = np.array(list(map(int, input().split())))
ans = 1000000000

for P in range(1, 101):
    temp = np.sum((X - P)**2)
    if ans > temp:
        ans = temp
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
X = [int(i) for i in input().split()]
mini = 10**6
for p in range(1,100):
	HP = sum(list(map(lambda x:(x-p)**2,X)))
	mini = min(mini,HP)
print(mini)

Submission status: Accepted

Solution code:
N = input()
list_X =  list(map(float,input().split()))

mean_dai = int(sum(list_X)/len(list_X)//1 +1)
mean_syo = int(sum(list_X)/len(list_X)//1)

l_dai = [(list_X[i] - mean_dai)**2 for i in range(len(list_X)) ]
l_syo = [(list_X[i] - mean_syo)**2 for i in range(len(list_X)) ]

ans = int(min(sum(l_dai),sum(l_syo)))
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int, input().split()))
X.sort()
ans = 999999999
for i in range(1,100):
    tmp = 0
    for j in X:
        tmp += (i-j)**2
    if tmp < ans:
        ans = tmp
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

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

point1 = min(X)
point2 = max(X)
Ans = 100000000000

for i in range(point1, point2+1):
    tmp = sum((X_arr - i) ** 2)
    if tmp < Ans:
        Ans = tmp

print(Ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    xn = list(map(int,input().split()[:n]))

    minsum = 0
    for point in range(min(xn),max(xn)):
        tmp = 0
        for xi in xn:
            tmp += (xi - point)**2
        if tmp < minsum or minsum == 0:
            minsum = tmp

    print(minsum)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys

def solve(N, X):
    ans = float("inf")
    for i in range(max(X)+1):
        cost = 0
        for x_i in X:
            cost +=  (x_i - i) ** 2
        ans = min(ans, cost)
    print(ans)

if __name__ == '__main__':
    N = int(sys.stdin.readline().strip())
    X = list(map(int,sys.stdin.readline().strip().split(" ")))
    
    solve(N, X)

Submission status: Accepted

Solution code:


def pleaseAC(ary):
    for i in range(100):
        ans = 0
        for j in ary:
            ans += (j - i)**2

        yield ans

def main():
    input()
    x = sorted(map(int, input().split()))

    print(min(pleaseAC(x)))




main()

Submission status: Accepted

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

Submission status: Accepted

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

minhp = float('inf')


for p in range(min(X),max(X)+1):
    hp = 0
    for x in X:
         hp += (x-p)**2
    
    if hp < minhp:
        minhp = hp

print(minhp)

Submission status: Accepted

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

ma = max(X)
mi = min(X)

ans = 10**9

for n in range(mi, ma+1):
  count = 0
  for x in X:
    count += abs(x-n)**2

  if ans > count:
    ans = count

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
m=float("inf")
for x in range(1,101):
    p=0
    for y in a:
        p+=(y-x)**2
    m=min(m,p)
print(int(m))

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(1, max(X)+1):
    tmp_ans = 0
    for xi in X:
        tmp_ans += (xi - i) ** 2
    ans = min(ans, tmp_ans)
    
print(ans)

Submission status: Accepted

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

ans1=0
for i in x:
    ans1+=(i-average-1)**2

print(min(ans,ans1))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import numpy as np


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

m = 1000000000
for i in range(1,101):
    d = x - i
    h = d*d
    m = min(sum(h), m)

print(m)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
min1 = 1000000000
for i in range(max(A)+1):
  sum1 = 0
  for j in range(N):
    sum1 += ((i - A[j]) ** 2)
  if(sum1 < min1):
    min1 = sum1
    
print(min1)

Submission status: Accepted

Solution code:
def main():
    n=int(input())
    X=list(map(int,input().split()))
    m=sum(X)/n
    if 2*(m//1-m)+1<0:
      m=int(m//1+1)
    else:
      m=int(m//1)
    ans=0
    for i in range(n):
        ans+=(X[i]-m)**2
    print(ans)   
if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:

def solve(N, Xs):
    low = min(Xs)
    high = max(Xs)
    ans = sum([x**2 for x in Xs])

    for p in range(low, high + 1):
        cost = 0
        for x in Xs:
            cost += (x - p) ** 2
        ans = min(ans, cost)
    return ans

if __name__ == "__main__":
    N = int(input())
    Xs = list(map(int, input().split(" ")))
    print(solve(N, Xs))

Submission status: Accepted

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

total = 0

for X in Xs:
    total += (int)(X)

P = (int)(total/N+0.5)

ans = 0

for X in Xs:
    ans += ((int)(X)-P)**2

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
#n,k = map(int,input().split())
L= list(map(int, input().split()))
min1 = min(L)
max1 = max(L)
ans = 10**10

for i in range(min1,max1+1):
    sum1 =0
    for j in range(n):
        sum1 += (L[j]-i)**2
        #print(sum1)
    ans = min(ans,sum1)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=input()
X=list(map(int,input().split()))
P=9999999999
for i in range(1,101):
  tmp=0
  for j in X:
    tmp += (j-i)**2
  if(P>tmp):
    P=tmp

print(P)

Submission status: Accepted

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

X.sort()

Xmin = min(X)
Xmax = max(X)

#print(Xmin,Xmax)

T =[]

for i in range(1,101):
    HP = 0
    for j in X:
        HP = HP + (i-j)**2
    T.append(HP)
print(min(T))

Submission status: Accepted

Solution code:
n=int(input())
x=[int(x) for x in input().split()]
big=max(x)
small=min(x)
ans=10000000000000000
for i in range(small,big+1):
  samu=0
  for j in range(len(x)):
    samu+=(x[j]-i)*(x[j]-i)
  if samu<=ans:
    ans=samu
print(ans)

Submission status: Accepted

Solution code:
import sys

def main():
    n = int(input().replace('\n',''))
    x = input().replace('\n','').split(' ')
    x = [int(i) for i in x]
    x_sort = sorted(x)
    min = -1
    for i in range(x_sort[len(x_sort)-1]+1):
      res=0
      for j in x_sort:
        res += (j-i) ** 2
      if min > res or min == -1:
        min = res
    print(min)
if __name__ == '__main__':
    input = sys.stdin.readline
    main()

Submission status: Accepted

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

p_min = min(data)
p_max = max(data)

ans_min = 100000000000000

for i in range(p_min, p_max+2):
  ans = 0

  for d in data:
    ans += (i-d)**2

  if ans < ans_min:
    ans_min = ans

print(ans_min)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
X = np.fromstring(input(), np.int64, sep=' ')
ans = 1000000
for p in range(1, 100):
    ans = min(ans, np.sum((X - p) ** 2))
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
s = [int(x) for x in input().split()]
def count(P):
    a = 0
    for i in range(N):
        b = (s[i]-P)**2
        a = a + b
    return a

c = count(1)
for i in range(100):
    if count(i+1) <= c:
        c = count(i+1)
print(c)

Submission status: Accepted

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

mn = 10**9
count = 0

for i in range(-110,110):
    for j in range(len(x)):
        count += (x[j] - i)**2
    if count <= mn:
        mn = count
        count = 0

print(mn)

Submission status: Accepted

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

#print(N)
#print(list)

SU = sum(list)/N
SR = round(SU)

#print(SU)
#print(SR)

ANS = 0
for i in range(N):
  temp = (list[i] - SR)
  ANS += temp*temp

print(ANS)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
X=list(map(int,input().split()))
a=sum(X)//N
b=sum(X)//N+1
ans_a=sum(abs(i-a)**2 for i in X)
ans_b=sum(abs(i-b)**2 for i in X)
print(min(ans_a,ans_b))

Submission status: Accepted

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

ans = float("inf")
for p in range(1, 101):
    ans = min(ans, sum((x - p) ** 2 for x in X))

print(ans)

Submission status: Accepted

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

a = min(X)
b = max(X)
l = [0]*(b-a+1)
for i, p in enumerate(range(a, b+1)):
  for j in range(N):
    l[i] += (p - X[j])**2
print(min(l))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# N人住んでいる
N = int(input())

# Xiが住んでいる場所
X = list(map(int, input().split()))

ans = 10**9
# 開催場所は0~一番遠い人のところまで
for p in range(max(X)+1):
    tmp = 0
    for x in X:
        #print(x,p,(x-p)**2)
        tmp += (x-p)**2
    # 最小値を更新する
    ans = min(ans, tmp)
    
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
pos_l = list(map(int,input().split()))
MAX = 100
ans = 10**6
for i in range(1,MAX+1):
    tmpans = 0
    for pos in pos_l:
        tmpans += (pos - i)**2
    
    if(tmpans < ans):
        ans = tmpans

print(ans)

Submission status: Accepted

Solution code:
N = input()
N = int(N)
X = input().split()
A = []

min = 100
max = 0
for x in X:
  x = int(x)
  A.append(x)
  if max < x:
    max = x
  if x < min:
    min = x

ans = 0
count = 1
for p in range(min, max):
  sum = 0
  for a in A:
    sum += (a - p) * (a - p) 
  if ans > sum:
    ans = sum
  if count == 1:
    ans = sum
  count += 1

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int,input().split()))
tmp = 0
mi =100000000000
for i in range(1,101):
  for k in range(N):
    tmp += (X[k] - i)**2
  if tmp < mi:
    mi = tmp
  tmp = 0
print(mi)

Submission status: Accepted

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

cost = float("inf")

for p in range(1,101):
    total=0
    for x in xl:
        total += (p-x)**2
    cost = min(cost,total)
print(cost)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
#sys.setrecursionlimit(150000)


def solve(N: int, X: "List[int]"):
    st=min(X)
    end=max(X)
    costmin=1000000
    for p in range(st,end+1):
        cost=0
        for i in range(N):
            cost+=(p-X[i])**2
        if cost<costmin: costmin=cost
    print(costmin)
    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()))

C_1 = sum(X)
P_R = C_1 / N
P_int_min = int(P_R)
P_int_max = P_int_min + 1

zahyou = -1
if (P_R - P_int_min) <= (P_int_max - P_R):
# P_R が下側の整数に近い時　<=> 下側の整数との距離の方が小さい時
    zahyou = P_int_min
else:
    zahyou = P_int_max

ans = 0
for i in range(N):
    ans += (X[i] - zahyou)**2

print(ans)

Submission status: Accepted

Solution code:
import math

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

s = sum(X) / N

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

l1 = 0
l2 = 0

for x in X:
    l1 += (x - p1) ** 2
    l2 += (x - p2) ** 2

print(min(l1, l2))

Submission status: Accepted

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

lmax = max(X)
lmin = min(X)

res = 99999999
for i in range(lmin, lmax+1):
    p = 0
    for n in X:
        p += (n-i)**2

    if p < res:
        res = p

print(res)

Submission status: Accepted

Solution code:
s=int(input())
t=list(map(int,input().split()))
a=max(t)
b=len(t)
c=0
d=10**6
for i in range(1,a+1):
  for j in range(b):
    c+=(t[j]-i)**2
  d=min(c,d)
  c=0
print(d)

Submission status: Accepted

Solution code:
import itertools
import sys
import math
import numpy as np
from collections import deque
from itertools import combinations
from functools import reduce
from functools import lru_cache
sys.setrecursionlimit(10**9)


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

    ans = 10**20
    for p in range(101):
        sum = 0
        for i in range(0,N):
            sum += (X[i] - p)**2

        ans = min(ans,sum)

    print(ans)

if __name__ == "__main__":
  main()

Submission status: Accepted

Solution code:
from sys import stdin
N = int(stdin.readline().rstrip())
X = [int(x) for x in stdin.readline().rstrip().split()]
ans = []
for i in range(101):
    s = 0
    for j in X:
        s += (j - i)**2
    ans.append(s)
print(min(ans))

Submission status: Accepted

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

m =round( sum(x) / n)

print(sum([(a - m) ** 2 for a in x]))

Submission status: Accepted

Solution code:
import math

def main():
    N = int(input())
    X = list(map(int, input().split()))
    min_tai = 1000000
    min_x = -1
    for x in range(min(X), max(X)+1):
        temp_tai = 0
        for temp_x in X:
            temp_tai += math.pow(x-temp_x, 2)
        if(temp_tai < min_tai):
            min_x = x
            min_tai = temp_tai
    print(int(min_tai))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import numpy as np
n=int(input())
x=np.array(list(map(int, input().split())))
ave=int(np.mean(x)+0.5)
ans=((x-ave)**2).sum()
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def rally(x):
    avg = sum(x) // len(x)
    sumf = sumc = 0
    for i in x:
        sumf += (i - avg) * (i - avg)
        sumc += (i - avg - 1) * (i - avg - 1)
    print(min(sumf, sumc))

if __name__ == "__main__":
    import sys
    in_str = []
    for line in sys.stdin:
        in_str.append(line)
    x = [int(_) for _ in in_str[1].split(' ')]
    rally(x)

Submission status: Accepted

Solution code:
import math

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

P = math.floor(sum(X)/N)

e = 0
for x in X:
    e += abs(x-P)**2

P = math.ceil(sum(X)/N)

ea = 0
for x in X:
    ea += abs(x-P)**2

answer = e if e<ea else ea
print(answer)

Submission status: Accepted

Solution code:
def main(n, x):
    ans = []

    for p in range(1, n + 101):
        t = 0
        for _x in x:
            t += (_x - p) ** 2

        ans.append(t)

    print(min(ans))


if __name__ == "__main__":
    n = int(input())
    x = list(map(int, input().split()))

    main(n, x)

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    N = int(input())
    X = list(map(int,input().split()))
    X.sort()
    min = float("inf")
    s = X[0]
    g = X[N-1]
    for p in range(s,g+1):
        s = 0
        for x in X:
            s += (x-p)**2
        if min >= s:
            min = s
    print(min)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from heapq import heapify, heappop, heappush
from random import randint
from collections import defaultdict
from itertools import accumulate
from fractions import gcd
from collections import defaultdict
import sys
#input = sys.stdin.readline
def inpl(): return list(map(int, input().split()))
MOD = 10**9 + 7

N = int(input())
X = inpl()
mean = sum(X) // N

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

Submission status: Accepted

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

p = [0] * 100
for i in range(100):
    for j in range(N):
        p[i] += (X[j] - (i + 1)) ** 2
ans =min(p)
print(ans)

Submission status: Accepted

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

min_cost = 1e10
for i in range(1, 101):
    cost = 0
    for x in X_list:
        cost += (x - i)**2
    if cost <= min_cost:
        min_cost = cost
print(min_cost)

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int,input().split()))
X.sort()
result = 1000000
A = 0
for i in range(101):
    for j in range(len(X)):
        A += (i-X[j])**2
    result = min(A,result)
    A = 0
print(result)

Submission status: Accepted

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

n = int(input())
x = [int(i) for i in input().split()]
xm = min(x)
xmm = max(x)
min = 9999999999999
for p in range(xm,xmm+1):
    sum = 0
    for xi in x:
        sum += (xi-p)**2
    if sum < min:
        min = sum
print(min)

Submission status: Accepted

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

cand = []
for i in range(min(X), max(X)+1):
    tmp = 0
    for x in X:
        tmp += (x - i) ** 2
    cand.append(tmp)
print(min(cand))

Submission status: Accepted

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

m = sum(a) / len(a)
m = round(m)

cost = 0

for elem in a:
    cost += (elem - m) ** 2

print(cost)

Submission status: Accepted

Solution code:
import sys
import numpy as np

n=int(sys.stdin.readline())
#x=list(map(int,sys.stdin.readline().split()))
x=np.array([sys.stdin.readline().split() for i in range(1)],np.int64)

min=(100**2)*n
for i in range(1,101):
    a=np.sum((i-x)**2)
    if a<min:
        min=a
print(min)

Submission status: Accepted

Solution code:
n=int(input())

X=list(map(int,input().split()))
X=sorted(X,reverse=True)
if n==1:
  print(0)
  exit()

A=[]
for i in range(1,100):
  ans=0
  for j  in range(n):
    ans += abs(i-X[j])**2
  A.append(ans)
A = sorted(A)
print(A[0])

Submission status: Accepted

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

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

ans = 10 ** 9
for i in range(min(X), max(X) + 1):
    tmp = 0
    for x in X:
        tmp += (x - i) ** 2
    ans = min(ans, tmp)

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
L=list(map(int,input().split()))
S=sorted(L)
ans=float("inf")
for i in range(S[0],S[-1]+1):
	tmp=0
	for j in range(N):
		tmp+=(S[j]-i)**2
	
	ans=min(ans,tmp)
print(ans)

Submission status: Accepted

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

def main():
    n = int(input())
    x_list = list(map(int, input().split()))
    x_min = min(x_list)
    x_max = max(x_list)
    p_list = list()

    for i in range(x_min, x_max + 1):
        power = sum(map(lambda x: pow(x - i, 2), x_list))
        p_list.append(power)
    
    print(min(p_list))

if __name__ == "__main__":
    main()

Submission status: Accepted

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

cost = [sum([abs(x-i)**2 for x in X]) for i in range (min(X), max(X)+1)]
print(min(cost))

Submission status: Accepted

Solution code:
N = int(input())
X = [int(x) for x in input().split()]
mini = 1000000
for i in range(100) :
    buf = 0
    for j in range(len(X)) :
        buf += (X[j] - i) ** 2
    mini = min(mini,buf)

print(mini)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    n = int(input())
    x = list(map(int, input().split(" ")))

    ave = round(sum(x)/n)
    #print(ave)

    d = 0
    for i in x:
        d += (i - ave)**2
    print(d)

Submission status: Accepted

Solution code:
n = int(input())
x = sorted(list(map(int, input().split())))
# print(x)
# ans = 10 * 18 + 13
ans=[10**9]*101
for i in range(100 + 1):
    cnt = 0
    for j in range(len(x)):
        # print(x[j], i)
        # print(cnt)
        cnt = cnt + (x[j] - i) ** 2
    # ans = min(ans, cnt)
    ans.append(cnt)
print(min(ans))

Submission status: Accepted

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

ans = 10**9
for i in range(100):
  r = 0
  for j in range(100):
    r += ((i-j)**2)*A[j]
  ans = min(r,ans)
print(ans)

Submission status: Accepted

Solution code:
import sys
import socket

hostnames = ['N551J', 'F551C', 'X553M']
input_file = 'c1.in'
if socket.gethostname() in hostnames:
    sys.stdin = open(input_file)


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


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


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


def read_str():
    return input()


def solve():
    inf = 10 ** 20
    n = read_int()
    x = read_int_list()
    res = inf
    for p in range(1, 101):
        s = 0
        for i in range(n):
            s += (p - x[i]) ** 2
        if res > s:
            res = s
    return res


def main():
    res = solve()
    print(res)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#solve
def solve():
    n = II()
    x = LI()
    ans = inf
    for i in range(max(x) + 1):
        tmp = 0
        for xi in x:
            tmp += (xi - i) ** 2
        ans = min(tmp, ans)
    print(ans)
    return


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

Submission status: Accepted

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


ans=10**6

for p in range(1,101):
    _sum=0
    for i in range(0,n):
        _sum += (x[i] - p)**2
    if ans > _sum:
        ans = _sum

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
XX = list(map(int,input().split()))
sumX = sum(XX)
P = round(sumX/N)

ss = [(x-P)**2 for x in XX]
print(sum(ss))

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    x = list(map(int, input().split()))
#     ans = float("inf")
    ave = round(sum(x)/n)
    ans = 0
    for i in range(n):
            ans += (x[i]-ave)**2
    print(ans)
main()

Submission status: Accepted

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

Submission status: Accepted

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

hp_list = []
for p in range(int(min(X_list)), int(max(X_list))):
    hp = 0
    for x in X_list:
        hp += abs(x - p) ** 2
    hp_list.append(hp)

print(min(hp_list) if len(hp_list) > 0 else 0)

Submission status: Accepted

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


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

    if i == min(x):
        max_n = sum

    if sum <= max_n:
        max_n = sum

if len(set(x)) == 1:
    max_n = 0

print(max_n)

Submission status: Accepted

Solution code:
import sys
from io import StringIO
import unittest

def resolve():
    n=int(input().strip())
    xs = list(map(int,input().strip().split()))

    minPw=9999999999999

    for i in range(1,101):
        sumPw=0
        for x in xs:
            sumPw+=(x-i)**2
        if sumPw < minPw:
            minPw = sumPw

    print(str(minPw))

    return

resolve()

Submission status: Accepted

Solution code:
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
X = np.array(lr())

def F(z):
    return ((X-z) ** 2).sum()
left = 0
right = 100
while right > left + 1:
    left_mid = (2*left + right) / 3
    right_mid = (left + 2*right) / 3
    if F(left_mid) < F(right_mid):
        right = right_mid
    else:
        left = left_mid

answer = min(F(int(left)), F(int(left)+1), F(int(left)+2))
print(answer)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
def solve():
    N = int(input())
    X = list(map(int, input().split()))
    res = float('inf')
    for p in range(101):
        res = min(res, sum((x-p)**2 for x in X))
    return str(res)

if __name__ == '__main__':
    print(solve())

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int, input().split()))
s = 1000000
for i in range(1,101):
    t = 0
    for j in range(0,N):
        t = t+(X[j]-i)**2
    if s > t:
        s = t
print (s)

Submission status: Accepted

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

minX=min(X)
maxX=max(X)

ans=float('inf')

for p in range(minX,maxX+1):
    culc=0
    for i in range(N):
        culc+=(X[i]-p)**2
    
    if ans>culc:
        ans=culc

print(ans)

Submission status: Accepted

Solution code:
import sys


def input():
    return sys.stdin.readline()[:-1]


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

ans = 0
mean = round(sum(X)/N)

for i in X:
	ans += (i-mean)**2

print(ans)

Submission status: Accepted

Solution code:
round2 = lambda i:(i*2+1)//2
n = int(input())
plist = [int(i) for i in input().split()]
ass = round2(sum(plist)/n)
power = 0
for j in range(n) :
    power += (plist[j] - ass)**2
print(int(power))

Submission status: Accepted

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


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

Submission status: Accepted

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

P=round(sum(X)/N)

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

print(hp)

Submission status: Accepted

Solution code:
import math

n = int(input())
x = list(map(int, input().split()))
avr = sum(x) / n
avr_min = int(avr)
avr_max = math.ceil(avr)

ans = 0
a = 0
b = 0

if avr_min == avr_max:
  for j in range(n):
    ans += (x[j] - avr_min)** 2
  print(ans)
else:
  for j in range(n):
    a += (x[j] - avr_min)** 2
    b += (x[j] - avr_max)** 2
  print(min(a, b))

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
ans = 10**10
for i in range(101):
    tmp = [abs(i - x[j])**2 for j in range(n)]
    if sum(tmp) < ans:
        ans = sum(tmp)

print(ans)

Submission status: Accepted

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

x_min = min(x_list)
x_max = max(x_list)

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

sum_temp = 10**23
for i in range(x_min, x_max + 1):
    if calc_sum(x_list, i) < sum_temp:
        ans_point = i
        sum_temp = calc_sum(x_list, i)
print(calc_sum(x_list, ans_point))

Submission status: Accepted

Solution code:

N = int(input())
Xs = input().split()

X =[]
for i in Xs:
	X.append(int(i))

minnum = 10000000000

for i in range(101):
	sumnum = 0
	for j in X:
		sumnum += (i-j)*(i-j)
	
	minnum = min(sumnum,minnum)

print(minnum)

Submission status: Accepted

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

X.sort()
min_power = float('inf')
for i in range(X[0], X[-1]+1):
    tmp = 0
    for x in X:
        tmp += pow(x - i, 2)
    min_power = min(min_power, tmp)
    
print(min_power)