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:
# -*- coding: utf-8 -*-

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

ans = []
for i in range(101):
    sum = 0
    for j in range(len(x)):
        sum += (x[j] - i)**2
    ans.append(sum)
print(min(ans))

Submission status: Accepted

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

def func(p):
	a=0
	for i in x:
		a += (i-p)**2
	return a

# print(func(2))
xsum = sum(x)
p1 = int(xsum/n)
p2 = p1+1


if func(p1)<func(p2):
	print(func(p1))
else:
	print(func(p2))

Submission status: Accepted

Solution code:
n = int(input())
li = list(map(int, input().split()))
res = -1
for i in range(101):
  sum = 0
  if i == 0:
    pass
  else:
    for j in range(n):
      sum = sum + (li[j]-i)**2
    if res == -1:
      res = sum
    elif res > sum:
      res = sum
print(res)

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
sum = 0
for i in range(n):
  sum += x[i]
  
ave = sum//n
sum = 0
for i in range(n):
  sum += (x[i] - ave)**2
min = sum
sum = 0
for i in range(n):
  sum += (x[i] - ave - 1)**2
max = sum 
 
if min > max:
  print(max)
else:
  print(min)

Submission status: Accepted

Solution code:
N=int(input())

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

X.sort()

min=10000000000
for P in range(100):

    power=0
    for j in range(len(X)):
        power=power+(X[j]-P)**2

    if power<=min:
        min=power

    P=P+1


print(min)

Submission status: Accepted

Solution code:
n=int(input())
lst=list(map(int,input().split()))
min_lst=sorted(lst)
ans=[]

for i in range(min_lst[0],min_lst[-1]+1):
  #print("O",i)
  sum=0
  for j in lst:
    sum+=(i-j)**2
    #print("A",sum)
  ans.append(sum)
  #print("B",ans)
print(min(ans))

Submission status: Accepted

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

result = 1e9
for i in range(100):
    sta = 0
    for x in X:
        sta += (x - i) * (x - i)
    result = min(sta, result)
print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int, input().split()))
ans = 0
R = round(sum(X)/N)

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int, input().split()))
ans = 10**10
for p in range(1,101):
  g = 0
  for x in X:
    d = x-p
    g += d*d
  ans = min(ans, g)
print(ans)

Submission status: Accepted

Solution code:
MAX = 10000000

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

min_sum = MAX

for i in range(1, 100):
  tmp_sum = 0
  for point in human_points:
    tmp_sum += (point - i)**2
  if min_sum > tmp_sum:
    min_sum = tmp_sum
print(min_sum)

Submission status: Accepted

Solution code:
n = int(input())

x_li = [int(w) for w in input().split()]

x_ave = sum(x_li) / len(x_li)
ave_pos = int(x_ave+0.5)

ans = 0
for x in x_li:
    ans += (x - ave_pos) ** 2

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
X=list(map(int,input().split())) 
X.sort()
a=sum(X)//N
b=0
c=0
for i in range(N):
    b+=(X[i]-a)**2
    c+=(X[i]-a-1)**2
print(min(b,c))

Submission status: Accepted

Solution code:
n = int(input())
x = [int(i) for i in input().split()]
count = 1000000000
sumX = 0
l = max(x)
for j in range(l):
    sumX = 0
    for k in range(n):
        sumX +=(x[k]-(j+1)) ** 2
    count = min(sumX,count)
print(count)

Submission status: Accepted

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

min_sum = float('inf')
for i in range(x[0], x[len(x) - 1] + 1):
    sum = 0
    for j in range(n):
        sum += (x[j] - i) ** 2
    if sum < min_sum:
        min_sum = sum

print(min_sum)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

min_x, max_x = min(x), max(x)

min_cost = sum([(x[i] - min_x) ** 2 for i in range(n)])
for p in range(min_x + 1, max_x + 1):
  cost = sum([(x[i] - p) ** 2 for i in range(n)])
  min_cost = min(cost, min_cost)

print(min_cost)

Submission status: Accepted

Solution code:
import statistics
import math

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

p = math.ceil(statistics.mean(X))
print(min(
    sum([(x - p) ** 2 for x in X]),
    sum([(x - (p-1)) ** 2 for x in X]),
    sum([(x - (p+1)) ** 2 for x in X]),
    ))

Submission status: Accepted

Solution code:
# coding: utf-8
n = int(input())
x = list(map(int, input().split()))
ans = []
for i in range(1, 101):
    tmp = 0
    for j in x:
        tmp += (j - i) ** 2
    ans.append(tmp)

print(min(ans))

Submission status: Accepted

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

minX = min(X)
maxX = max(X)
li = []
for i in range(100):
    temp = 0
    for j in range(N):
        temp += (X[j] - i)**2
    li.append(temp)

print(min(li))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():

    N = int(input())
    X = list(map(int, input().split()))
    X.sort()
    ans = float('inf')
    for y in range(min(X), max(X)+1):
        p = 0
        for i in range(N):
            p += pow(y - X[i], 2)
        ans = min(ans, p)
    return ans

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

Submission status: Accepted

Solution code:
import itertools
import math
import sys
import heapq
from collections import Counter
from collections import deque
from fractions import gcd
from functools import reduce
sys.setrecursionlimit(4100000)
from functools import lru_cache
INF = 1 << 60

#ここから書き始める
n = int(input())
x = sorted(list(map(int, input().split())))
ans = INF

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

print(ans)

Submission status: Accepted

Solution code:
import numpy as np

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

    ans = 1000000

    for i in range(100):
        ans = np.minimum(np.sum(np.square(x - i)), ans)

    print(ans)

resolve()

Submission status: Accepted

Solution code:
N = int(input())
*X, = map(int, input().split())
ans = 10**10
for p in range(1, 101):
    temp = sum((x - p)**2 for x in X)
    ans = min(ans, temp)
print(ans)

Submission status: Accepted

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

ans = None
ans_dist = 1000000007
for Xref in range(1, 100 + 1):
    dist = 0
    for i, Xi in enumerate(X):
        dist += (Xi - Xref) ** 2
    #
    if ans_dist > dist:
        ans = Xref
        ans_dist = dist

print(ans_dist)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    x = list(map(int, input().split()))
    xx = sum(x)
    p = xx//N
    if (2*N*p+N-2*xx) < 0:
        ans = p+1
    else :
        ans = p
    kyori = 0
    for i in range(N):
        kyori += (x[i]-ans)**2
    print(kyori)
main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
#入力二つ
# n, k = map(int,input().split())

n = int(input())
n_list = list(map(int,input().split(" ")))

min_num = 100000000000000

for p in range(min(n_list), max(n_list)+1):
    sum = 0
    for j in n_list:
        sum += (j-p)**2

    if sum<min_num:
        min_num = sum

print(min_num)

Submission status: Accepted

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

inf = 10**8
cnt = 0
ans = inf
for i in range(101):
    cnt = 0
    for j in range(n):
        cnt += (x[j] - i)**2
    ans = min(cnt, ans)
print(ans)

Submission status: Accepted

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

mean = round(sum(x) / n)

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

Submission status: Accepted

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

ave = sum(X) / N

kouho1 = int(ave)

if ave != int(ave):
  kouho2 = kouho1 + 1
else:
  kouho2 = kouho1
  
def tairyoku(a):
  tai = 0
  for i in X:
    tai = tai + (i - a)**2
  return tai

tairyoku1 = tairyoku(kouho1)
tairyoku2 = tairyoku(kouho2)
print(min(tairyoku1, tairyoku2))

Submission status: Accepted

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

ans = 0
i = X[0]
while i <= X[N-1]:
    eng=0
    for k in range(N):
        eng += (X[k]-i)**2
    if i == X[0] or eng < ans:
        ans = eng
    i += 1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int, input().split()))
xp = round(sum(X) / N)
Xe = map(lambda x: abs(x - xp) ** 2, X)
print(sum(Xe))

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int, input().split()))
sav = float("inf")
for i in range(min(X), max(X)+1):
    Xe = map(lambda x:abs(x-i)**2,X)
    health = sum(Xe)
    if sav > health:
        sav = health

print(sav)

Submission status: Accepted

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

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
ans = 10**9+7
X_list = list(map(int,input().split()))
for P in range(min(X_list),max(X_list)+1):
    ans = min(ans,sum([(i-P)**2 for i in X_list]))
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

INF = 10**9
m = INF
for x in range(1,max(X)+1):
    temp = 0
    for i in range(N):
        temp += (x - X[i])**2
    if m > temp:
        m = temp
print(m)

Submission status: Accepted

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

l = np.array(l)
ans = np.sum((l - round(np.mean(l)))**2)
print(int(ans))

Submission status: Accepted

Solution code:
n = int(input())
x = [int(i) for i in input().split()]
ans = 100100100
for i in range(1,101) :
    s = 0
    for o in x :
        s+=(i-o)*(i-o)
    ans = min(ans,s)
print(ans)

Submission status: Accepted

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

p = 1
v_list = [0 for _ in range(N)]


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

ans = 0
for _ in range(100):
    new_v_list = [calc(p, x) for x in x_list]
    s = sum(v_list)
    new_s = sum(new_v_list)
    
    if p==1:
        pass        
    elif s < new_s:
        ans = s
        break
        
    ans = new_s
    p+=1
    v_list = new_v_list

print(ans)

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())
x = np.array(list(map(int,input().split())))
r = np.full(100, 100 * n)

for p in range(100):
    r[p] = np.sum((x - p + 1)**2)
print(int(np.min(r)))

Submission status: Accepted

Solution code:
N = int(input())
L = list(map(int, input().split()))
a=sum(L)/N
b=int(a)
if a-b<=1/2:
    a=b
else:
    a=b+1
c=0
for i in range(N):
    c=c+(a-L[i])**2
print(c)

Submission status: Accepted

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

t = round(sum(x)/n)

s = 0
for i in x:
  s += abs(t - i)**2
print(s)

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int, input().strip().split()))
total = 0
points = 0
for i in X:
  total += i
avg = int(round(total/N))
for i in X:
  points += (i-avg)**2
print(points)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
arr = list(map(int,input().split()))
cnt = 10**10
for i in range(1,101):
	cost = 0
	for j in arr:
		cost+=(abs(i-j)**2)
	cnt = min(cnt,cost)
print(cnt)

Submission status: Accepted

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

ans = 10**9

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

Submission status: Accepted

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

point = (sum(X) / N * 2 + 1) // 2
result = 0
for x in X:
	result += int((x - point) ** 2)

print(result)

Submission status: Accepted

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

#minimize sum of squares

# N people living on a number line
# ith person lives at coordinate x
#  hold a meeting that all N people are going to atend
#  meeting can be at any integer coordinate
#  if at P, ith person will spend (x-P)^2 stamina
# find the minimum points of stamina  N people will have to spend

# (x-P)(X-P)
# x^2-2PX-P^2
#2x-2P
min = min(xs)
max = max(xs)
smallestS = 99999999999999999999

for _ in range(min,max+1):
    stamina = 0
    for x in xs:
        stamina +=(x-_)*(x-_)
    if stamina<smallestS:
        smallestS = stamina

print(smallestS)

Submission status: Accepted

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

answer = sum([x ** 2 for x in li_x])
for p in range(min(li_x),max(li_x) + 1):
    answer = min(answer, sum([(x - p) ** 2 for x in li_x]))

print(answer)

Submission status: Accepted

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

answer = []
p = min(X)
while p <= max(X):
  a = [(x-p)**2 for x in X]
  answer.append(sum(a))
  p += 1
  
print(min(answer))

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
X = np.array([int(i) for i in input().split()])
print(min([np.sum((X-i)**2) for i in range(101)]))

Submission status: Accepted

Solution code:
N = int(input())
x = []
s = input()
x = s.split()
y = len(x)
summ1 = 0
summ2 = 0
sr = 0
setx = set(x)
h = len(setx)
if N == 1:
    print("0")
else:
    if h == 0:
        print("0")
    else:
        for i in range(y):
            x[i] = int(x[i])
            sr = sr + x[i]
        PMA = (sr//N)+1
        PMM = sr//N
        for i in range(y):
            summ1 = summ1 + ((x[i]-PMA)*(x[i]-PMA))
        for i in range(y):
            summ2 = summ2 + ((x[i] - PMM) * (x[i] - PMM))
        if summ2 > summ1:
            print(summ1)
        else:
            print(summ2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def power(x, n):
  return (x-n) * (x-n)

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

ave = round(sum(nums) / n)

new_nums = []

for i in nums:
  r = power(i, ave)
  new_nums.append(r)

print(sum(new_nums))

Submission status: Accepted

Solution code:
import sys
import os


def calc(x, p):
    return pow(x - p, 2)


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = int(sys.stdin.readline().rstrip())
    X = list(map(int, sys.stdin.readline().split()))

    ret = float('inf')
    for p in range(100):
        tmp = 0
        for n in range(N):
            tmp += calc(X[n], p)
        ret = min(tmp, ret)

    print(ret)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
xs = np.array(list(map(int,input().split(" "))))
x_min,x_max = xs.min(),xs.max()
powers = []
for x in range(x_min,x_max + 1,1):
    powers.append(sum((xs-x)**2))
print(min(powers))

Submission status: Accepted

Solution code:
import math

N=int(input())

inputs=input().split(" ")
X=[]

mini=100
maxi=0


for i in inputs:
    i=int(i)
    X.append(i)

    if int(i)<mini:
        mini=i

    if int(i)>maxi:
        maxi=i

minpowersum=math.pow(10,9)
for P in range(mini,maxi+1):

    powersum=0
    for j in range(N):
        powersum+=math.pow((X[j]-P),2)

    if powersum<minpowersum:
        minpowersum=powersum


print(int(minpowersum))

Submission status: Accepted

Solution code:
import statistics
N = int(input())
X = list(map(int,input().split()))
P = int(statistics.mean(X))
b = 0
for i in range(0,N):
    b = b + (X[i] - P) ** 2

while True:
    bb = 0
    P = P + 1
    for i in range(0,N):
        bb = bb + (X[i] - P) ** 2
    if b < bb:
        break
    elif b > bb:
        b = bb
print(b)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

N = int(input())
Xi = list(map(int, input().split(" ")))

Xi_min = min(Xi)
Xi_max = max(Xi)

ans = 999999999
for i in range(Xi_min, Xi_max+1):
    tmp = 0
    for j in range(N):
        tmp += pow(Xi[j] - i, 2)
    if tmp < ans:
        ans = tmp

print(ans)

Submission status: Accepted

Solution code:
# 156-C
N = int(input())
X = [int(x) for x in input().split()]
cnt = 0
A = 0
while cnt <= 100:
  cnt += 1
  i = 0
  while i < N:
    A += (int(X[i])-cnt)*(int(X[i])-cnt)
    i += 1
  if cnt == 1:
    ans = A
  else:
    ans = min(ans, A)
  A = 0
print (ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
N = int(input())
X = list(map(int,input().split()))
ave_h=math.ceil(sum(X)/N)
ave_l=math.floor(sum(X)/N)
count_1=0
count_2=0
for i in range(N):
    count_1+=(X[i]-ave_h)**2
    count_2+=(X[i]-ave_l)**2
print(min(count_1,count_2))

Submission status: Accepted

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

mn = 10 ** 10
for p in range(1, 101):
    tmp = 0
    for i in x:
        tmp += (i - p) ** 2
    mn = min(mn, tmp)
print(mn)

Submission status: Accepted

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

total = float('inf')

for p in range(1, 100):
    tmp = 0
    for num in nums:
        tmp += pow((p - num), 2)
    if tmp < total:
        total = tmp

print(total)

Submission status: Accepted

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

maxdist = x[int(n)-1]
p=int(sum(x)/len(x))
ret = 0
ret2 = 0
for i in x:
    ret = ret + (p-i)**2
    ret2 = ret2 + (p-i+1)**2
ret = ret if ret < ret2 else ret2
print(ret)

Submission status: Accepted

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

w = 0
sum = []
for p in range(1,101):
    w = 0
    for i in range(n):
        w += (x[i] - p)**2
    sum.append(w)
print(min(sum))

Submission status: Accepted

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

P = int((sum(x) / len(x)))
a = 0
b = 0
c = 0
ans_list = []

for i in range(N):
    a += (x[i] - P) * (x[i] - P)
    b += (x[i] - (P + 1)) * (x[i] - (P + 1))
    c += (x[i] - (P - 1)) * (x[i] - (P - 1)) 
ans_list.append(a)
ans_list.append(b)
ans_list.append(c)

print("{}".format(min(ans_list)))

Submission status: Accepted

Solution code:
import numpy as np
import math as m

def main(**kwargs):
    minx = min(X)
    maxx = max(X)
    r = min([sum((X - x) ** 2) for x in range(minx, maxx+1)])
    return r

if __name__ == "__main__":
    cin = np.array(input().split(" ")).astype("int")
    n = cin[0]

    cin = np.array(input().split(" ")).astype("int")
    X = np.array(cin)
    
    cout = main(n=n, X=X)
    print(cout)

Submission status: Accepted

Solution code:
n=int(input())
hoge=list(map(int,input().split()))
ans=9999999
for i in range(1,101):
  huga=0
  for ho in hoge:
    huga+=(i-ho)**2
  ans=min(ans,huga)
  
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
L = list(map(int,input().split()))
ave = int(sum(L)/len(L))
ave2 = ave + 1
Answer = 0
Answer2 = 0
for i in range(N):
  Answer += (ave-L[i])**2
  Answer2 += (ave2-L[i])**2
if Answer <= Answer2:
  print(Answer)
else:
  print(Answer2)

Submission status: Accepted

Solution code:
import numpy as np

def main(n, xs):
    minx = min(xs)
    maxx = max(xs)
    ress = []

    for x in range(minx, maxx+1):
        res = sum((xs - x) ** 2)
        ress.append(res)

    res = min(ress)
    return res

if __name__ == "__main__":
    cin = input()
    n = int(cin.split(" ")[0])
    cin = input()
    xs = np.array(cin.split(" ")).astype("int")
    cout = main(n, xs)
    print(cout)

Submission status: Accepted

Solution code:
import numpy as np

def main(n, xs):
    minx = min(xs)
    maxx = max(xs)
    ress = []

    for x in range(minx, maxx+1):
        res = sum((xs - x) ** 2)
        ress.append(res)

    res = min(ress)
    return res

if __name__ == "__main__":
    cin = input()
    n = int(cin.split(" ")[0])
    cin = input()
    xs = np.array([], dtype="int")

    for i in range(n):
        xs = np.append(xs, int(cin.split(" ")[i]))
    cout = main(n, xs)
    print(cout)

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int, input().split(" ")))
minX, maxX = min(X), max(X)
ans = 100*N**2+1
for i in range(minX, maxX+1):
    ans = min(ans, sum([abs(i - x)**2 for x in X]))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

p = round(sum(x_list)/n)

print(sum([(x - p)**2 for x in x_list]))

Submission status: Accepted

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

Submission status: Accepted

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

sum = 0
for i in range(0,num):
  sum = sum + num_list[i]
  
p = int(round(sum/num,0))

ans = 0
for i in range(0,num):
  ans = ans + (p-num_list[i])**2
  
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int, input().split()))
 
 
def variance(x):
    avg = sum(X) // len(X)
    return (avg - x) ** 2
 
 
def main(X):
    tot = 0
    tot2 = 0
    for x in X:
        tot += variance(x)
        tot2 += variance(x - 1)
    return min(tot, tot2)
 
 
ans = main(X)
print(ans)

Submission status: Accepted

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


def variance(x):
    avg = sum(X) // len(X)
    return (avg - x) ** 2


def main(X):
    tot = 0
    tot2 = 0
    for x in X:
        tot += variance(x)
        tot2 += variance(x - 1)
    return min(tot, tot2)


ans = main(X)
print(ans)

Submission status: Accepted

Solution code:
import sys
from math import *
from collections import deque, Counter, defaultdict
from fractions import gcd
input = lambda: sys.stdin.readline().rstrip()

def eprint(s):
    sys.stderr.write('DEBUG: {}'.format(s))
    return

def main():
    n = int(input())
    Xs = list(map(int, input().split()))
    
    mean = sum(Xs) / n

    cmean = ceil(mean)
    fmean = floor(mean)

    ct = 0
    ft = 0

    for i in range(n):
        ct += (Xs[i] - cmean) ** 2
        ft += (Xs[i] - fmean) ** 2
    
    print(min(ct, ft))

    return

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from decimal import Decimal, ROUND_HALF_UP

n = int(input())
habit = list(map(int, input().split(" ")))

x = Decimal(str(sum(habit)/n)).quantize(Decimal('0'), rounding=ROUND_HALF_UP)

total = 0
for i in habit:
    total += (i - x) ** 2
print(total)

Submission status: Accepted

Solution code:
import numpy as np

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

max = 1000000
for j in range(1,101):
  energy = 0
  for i in range(len(x)):
    energy += (x[i]-j)**2
  if energy < max:
    max = energy
    
print(max)

Submission status: Accepted

Solution code:
N=int(input())
List=[int(i) for i in input().split()]
P=int(round(sum(List)/N,0))
def length(x):
 return sum([(i-P-x)**2 for i in List])

i=0
data=length(0)
while True:
 if(length(i-1)<length(i)<length(i+1)):
  i-=1
 elif(length(i-1)>length(i)>length(i+1)):
  i+=1
 else:
  print(length(i))
  break

Submission status: Accepted

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

Submission status: Accepted

Solution code:

if __name__ == '__main__':

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

    result = 900000
    for p in range(min(x), max(x) + 1):        
        s = sum([(i - p)** 2 for i in x])
        if result > s:
            result = s
    print(result)

Submission status: Accepted

Solution code:
import math

N = int(input())

*X,  = map(int, input().split())
total = sum(X)

c = math.ceil(total/N)
f = math.floor(total/N)

min_c = c
min_f = f
total_c = 0
total_f = 0


for x in X:
    total_c = total_c + (x-min_c)**2

for x in X:
    total_f = total_f + (x-min_f)**2

print(total_c if total_c <= total_f else total_f)

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int,input().split()))
Min,Max = min(x),max(x)
if Min == Max:
    print(0)
    exit()
s = [0] * (Max - Min)
k = 0

for i in range(Min,Max):
    for j in x:
        s[k] += (j - i) ** 2
    k += 1
print(min(s))

Submission status: Accepted

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

ans = 10**15
for i in range(1, 101):
    v = 0
    for j in X:
        v += (j-i)**2
    ans = min(ans, v)

print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    li = list(map(int,input().split()))
    li.sort()
    size = len(li)
    ans = []
    for i in range(li[size-1]+1):
        count = 0
        for j in range(size):
            count += (i - li[j]) ** 2
        ans.append(count)
    min_val = min(ans)
    print(min_val)

if __name__ == '__main__':
  main()

Submission status: Accepted

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

import numpy as np
arr = np.array(X)

ans = np.inf
for i in range(1,101):
    ans = min(ans, sum(map(lambda x:x**2,arr-i)))

print(ans)

Submission status: Accepted

Solution code:
N = input()
x = list(map(int, input().split()))
start = min(x)
fin = max(x)
res = 0
for j in range(len(x)):
        res = res + (x[j]-start)*(x[j]-start)
        
for i in range(start,fin):
    sum = 0
    for j in range(len(x)):
        sum = sum + (x[j]-i)*(x[j]-i)
    if(sum < res):
        res = sum

print(res)

Submission status: Accepted

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


c = 10**10
for p in range(1,101):
	s = 0
	for i in range(len(l)):
		s += (l[i] - p)**2
	c=min(c,s)
print(c)

Submission status: Accepted

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

def f(t,x):
  s = 0
  for i in range(len(x)):
    s += (x[i]-t)**2
  return s

l = min(x)
u = max(x)
m = float('inf')
for i in range(l,u+1):
  if m > f(i,x):
    m = f(i,x)
print(m)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
X = [int(_) for _ in input().split()]
b = np.polyfit([0 for _ in range(N)], X, 0) #切片だけ
# y = ax + b
P = round(b[0])
res = [(x-P)*(x-P) for x in X]
print(int(sum(res)))

Submission status: Accepted

Solution code:
N = int(input())
zahyou = input().split(" ")
tairyoku = []
temptairyoku = int(0)
for i in range(1, 101):
    for s in range(N):
        temptairyoku = temptairyoku + (int(zahyou[s]) - int(i)) ** 2
    tairyoku.append(int(temptairyoku))
    temptairyoku = 0
tairyoku.sort()
print(tairyoku[0])

Submission status: Accepted

Solution code:
n1 = int(input())
l1 = list(map(int,input().split()))
sum_costs = []

for p in range(min(l1), max(l1)+1):
    sum_cost = 0
    for i in range(n1):
        a_cost = (l1[i] - p) ** 2
        sum_cost = sum_cost + a_cost
    sum_costs.append(sum_cost)

print(min(sum_costs))

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int,input().split()))
MIN_X = min(X)
MAX_X = max(X)
ans = 10 ** 8
for i in range(MIN_X, MAX_X + 1):
    SUM = 0
    for j in X:
        SUM += (i - j) ** 2
    if ans > SUM:
        ans = SUM

print(ans)

Submission status: Accepted

Solution code:
_ = int(input())
coords = tuple(int(x) for x in input().split())

start = min(coords)
end = max(coords)

min = float("inf")
for x in range(start, end + 1):
    sum = 0
    for coord in coords:
        temp = x - coord
        sum += temp * temp
    if sum < min:
        min = sum
  
print(min)

Submission status: Accepted

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

ans = min(map(lambda p: sum(map(lambda x: (x - p) ** 2, xs)), range(1, 101)))
print(ans)

Submission status: Accepted

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

for i in range(1,101):
  cost = 0
  for j in range(n):
    cost += (i-a[j])**2
  
  ans = min(cost,ans)
  
print(ans)