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:
import os
import sys

import numpy as np
from scipy.optimize import fmin

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

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


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

X = np.array(X, dtype=int)


def calc(p):
    return ((X - p) ** 2).sum()


ans = INF
for i in range(200):
    ans = min(ans, calc(i))
# print(ans)

y, = fmin(calc, x0=0, disp=False)
print(int(calc(round(y))))

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 = k + (j-a)**2
print(k)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
x=list(map(int,input().split()))
imax=int(max(x))
imin=int(min(x))
lis=[]
re=0
for k in range(imax+1):
  for i in x:
    re=re+(i-k)**2
  lis.append(re)
  re=0
print(min(lis))

Submission status: Accepted

Solution code:
n=int(input())
x=list(map(int,input().split()))
imax=int(max(x))
imin=int(min(x))
lis=[]
re=0
for k in range(imin,imax+1):
  for i in x:
    re=re+(i-k)**2
  lis.append(re)
  re=0
print(min(lis))

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int,input().split()))
x.sort()
z = 10**100
for p in range(x[0],x[-1]+1):
    temp = 0
    for xx in x:
        temp += (xx-p)**2
    z = min(temp,z)
print(z)

Submission status: Accepted

Solution code:
import math
from statistics import mean, median,variance,stdev
N = int(input())
X = list(map(int,input().split()))
a = sum(X)
ave = round(a/N)
b = 0
for i in range(N):
    b +=(X[i]-ave)**2
print(b)

Submission status: Accepted

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

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

ans = 0
ans_sum = 0

for P in range(Xmin,Xmax+1):
  for i in range(N):
    cal = (X[i] - P)**2
    ans_sum = ans_sum + cal
  if P == Xmin:
    ans = ans_sum
    ans_sum = 0
  elif ans >= ans_sum:
    ans = ans_sum
    ans_sum = 0
  else :
    ans_sum = 0
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
people = list(int(i) for i in input().split())
min_s = 0
av = int(round(sum(people) / n))
for s in people: 
  min_s = min_s + (s - av)**2
print(min_s)

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())
lst = [int(x) for x in input().split()]

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

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

Submission status: Accepted

Solution code:

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

    ans.append(a)
    a=0
print(min(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input(""))
l=input("").split()
L=[int(n) for n in l]
max=max(L)
min=min(L)
min_shohi=1000000
for P in range(min,max+1):
    shohi=0
    for x in L:
        shohi+=(x-P)**2
    if shohi<min_shohi:
        min_shohi=shohi
print(min_shohi)

Submission status: Accepted

Solution code:
import numpy as np
n=int(input())
x=list(map(int,input().split()))
x=np.array(x)
ans=10**100
for i in range(-150,150,1):
  d=np.sum( (x-i)**2)
  ans=min(ans,d)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int, input().split()))
mean_val = int(round(sum(X)/len(X)))
total_stamina = sum((val-mean_val)**2 for val in X)
print(total_stamina)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

p = 0
sum = 0

for i in range(n):
    p += x[i]

p = round(p/n)

for j in range(n):
    sum += (x[j] - p)**2

print(sum)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

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

sig=0
for i in range(N):
    sig+=X[i]

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

min=10**10
med=X[0]
while True:
    sum=N*med**2-2*med*sig+sig_2

    if sum<min:
        min=sum
        prev=sum
        med+=1
    else:
        break

print(prev)

Submission status: Accepted

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

ave = int(sum(A) / len(A))
tmp = sum(A) / len(A)
if(tmp > ave + 0.5):
    ave += 1
ans = 0
for i in range(N):
    ans += (A[i] - ave)**2
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
l1 = min(x)
l2 = max(x)
ans = 100000000
for i in range(l1, l2+1):
    sum = 0
    for j in x:
        sum += (i-j)**2
    if sum < ans:
        ans = sum
print(ans)

Submission status: Accepted

Solution code:
import sys
from functools import reduce

n = int(input())
x = list(map(int, input().split()))
l = ((i, reduce(lambda a, j: a + (x[j] - i) * (x[j] - i), range(0, n), 0)) for i in range(1, 100 + 1))
print(min(l, key=lambda x: x[1])[1])

Submission status: Accepted

Solution code:
N=int(input())
X=list(map(int,input().split()))
s=0
sum=0
for i in X:
    sum+=i

sum=round(sum/len(X))

for i in X:
    tmp=(i-sum)**2
    s+=tmp
print(s)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

best = float('inf')
for c in range(0, 101):
    cost = 0
    for x in XS:
        cost += (x - c) * (x - c)
    best = min(cost, best)
print(best)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = []
CCC = 0
j = 1
max_A = max(A)
for j in range(101):#部屋の座標最大値までのループ
    if j == 0:
        continue
    CCC = 0
    for k in range(N):#各部屋からの計算
        CCC += (A[k] - j) ** 2
    B.append(CCC)
x = min(B)
print(x)

Submission status: Accepted

Solution code:
import statistics

def cal_hp(p, num_list):
    total = 0
    for num in num_list:
        temp = (num - p) ** 2
        total = total + temp
    return total

line1 = input()
line2 = input()

num_list = line2.split(" ")
num_list = [int(n) for n in num_list]
ans_list = []

for i in range(0, max(num_list)+1):
    ans_list.append(cal_hp(i, num_list))
print(min(ans_list))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int , input().split()))
bb = -(-sum(A)//N)
cc = sum(A)//N
ans = 0
ans_ = 0
for i in range(len(A)):
    ans += (A[i]-bb)**2
for i in range(len(A)):
    ans_ += (A[i]-cc)**2
print(min(ans,ans_))

Submission status: Accepted

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

sum = 0
for i in range(n):
  sum += int(s[i])

a = float(sum)/float(n)
if int(a)+0.5 <= a:
  a = int(a)+1
else:
  a = int(a)
  
ans = 0
for i in range(n):
  ans += (int(s[i])-a)**2

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

minx = min(list_x)
maxx = max(list_x)
sums_list = []

for i in range(minx, maxx+1):
    sums = 0
    for j in list_x:
        sums += (j - i)**2
    sums_list.append(sums)

print(min(sums_list))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import maxsize

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

min = maxsize
for i in range(1, 101):
    hp = sum([(k-i)**2 for k in x])
    if hp < min:
        min = hp

print(min)

Submission status: Accepted

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

sumlist = []

if len(set(x)) == 1:
    print(0)
else:
    for i in range(1, max(max(x), n)):
        s = 0
        for j in range(n):
            s += (x[j] - i) ** 2
        sumlist.append(s)

    if sumlist == []:
        print((x[0] - n) ** 2)
    else:
        print(sorted(sumlist)[0])

Submission status: Accepted

Solution code:
import math

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


def summ(ii):
    xxx = 0
    for x in X:
        xxx += (x - ii) ** 2
    return xxx


i1 = math.ceil(sum(X) / N)
i2 = math.floor(sum(X) / N)

if i1 == i2:
    print(summ(i1))
else:
    print(min(summ(i1), summ(i2)))

Submission status: Accepted

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

xlist.sort()
#max_x = xlist[n - 1]
#min_x = xlist[0]

#p = int((max_x - min_x)/2) + min_x

s = sum(xlist)
p = round(s / n)
add_x = 0
for i in range(n):
    add_x = add_x + (xlist[i] - p) ** 2

print(add_x)

Submission status: Accepted

Solution code:
import math
import decimal
def main():
    n = int(input())
    x = input().split()
    x = [int(t)for t in x]
    
    x_ = sum(x)
    p = round(x_/n)

    s = [(t-p)*(t-p)for t in x]
    print(sum(s))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import numpy as np

def resolve():
    input()
    x = list(map(int, input().split()))
    m = int(round(np.mean(x)))
    sum = 0
    for val in x:
        sum += (val - m) ** 2
    print(sum)

if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
import numpy as np
N=int(input())
X = list(map(int,input().split()))
X.sort()
a=list()
c=N-1
for i in range(X[0],X[c]+1):
    b=0
    for j in range(0,N):
        if i-X[j]>10**5:
            break
        b+=(i-X[j])**2
    a.append(b)
print(min(a))

Submission status: Accepted

Solution code:
num=int(input())
s=input().split()
res=float('inf')
for j in range(1,101):
  temp=0
  for k in s:
    temp+=(int(k)- j)**2
  if temp < res:
    res = temp
print(res)

Submission status: Accepted

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

Submission status: Accepted

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

Physical_strength = 0
flag = 0
n=0
while 1:
    n += 1
    flag=0
    for Xi in X:
        flag += (Xi-n)**2
    if n == 1:
        Physical_strength = flag
    if Physical_strength > flag:
        Physical_strength = flag
    elif Physical_strength < flag and n > N:
        break

print(Physical_strength)

Submission status: Accepted

Solution code:
N = int(input())
x  = [int(x) for x in input().split()]
x.sort()
ans = 10**10
for l in range(100):
  ans1 = 0
  for i in x:
    ans1+=(i-l)**2
  ans = min(ans1,ans)
print(ans)

Submission status: Accepted

Solution code:
import math

N = int(input())
Xs = input().split(' ')
X = []
for i in range(N):
  X.append(int(Xs[i]))
  
 
minimizeP = sum(X)/N
P_floor = math.floor(minimizeP)
P_ceil = math.ceil(minimizeP)
minP1 = 0
minP2 = 0
X.sort()
for i in X:
  minP1 = minP1 + (i - P_floor) ** 2
  minP2 = minP2 + (i - P_ceil) ** 2
  
print(min(minP1,minP2))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

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

ans_1 = 0
ans_2 = 0
x_sum = sum(x_list)

x_sum = x_sum // n

for x in x_list:
    ans_1 += (x-x_sum)**2
    ans_2 += (x-x_sum - 1)**2
    continue

print(min(ans_1,ans_2))

Submission status: Accepted

Solution code:
N = int(input())
P = 0
res = []
stamina = 0

ls = input().split()
for i in range(0, len(ls)):
  ls[i] = int(ls[i])
  
for i in range(0, 100):
  P = P + 1
  for j in range(0, len(ls)):
  	stamina = stamina + (ls[j] - P)**2
  res.append(stamina)
  stamina = 0

print(min(res))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
a=input()
b=input().split()
c=[int(n) for n in b]
d=sum(c)
e=round(d/int(a))

f=[(f-e)*(f-e) for f in c]
g=sum(f)
print(g)

Submission status: Accepted

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

lis_X_sorted = sorted(lis_X)

def calc_dif_sq(a,b):
    return (a-b)**2

if N == 1:
    ans = 0
elif min(lis_X_sorted)==max(lis_X_sorted):
    ans = 0
else :
    res = []
    for p in range(lis_X_sorted[0],lis_X_sorted[-1]):
        tmp = sum(list(map(lambda x:calc_dif_sq(x, p), lis_X_sorted)))
        res.append(tmp)
    ans = min(res)
print(ans)

Submission status: Accepted

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

def damage_all(X,P):
  damage = 0
  for i in X:
    damage += (i - P)**2
  return damage

P = round(sum(X) / len(X))
distance = damage_all(X,P)

print(distance)

Submission status: Accepted

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

Submission status: Accepted

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

def hitpoint(n, xs):
    acc = 0
    for x in xs:
        r = (x - n) ** 2
        acc += r
    return acc

ans = hitpoint(1, XS)
tmp = ans
for i in range(2, 100):
    ans = hitpoint(i, XS)
    if ans >= tmp:
        break
    tmp = ans

print(tmp)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import collections as col
input=sys.stdin.readline

def mainA():
	N,R=map(int,input().split())
	#N=input().rstrip()
	#N=int(input())
	#vA=list(map(int,input().split()))
	
	if N >= 10:
		res = R
	else:
		res = R + 100*(10-N)
	
	print(res)

def mainB():
	N,K=map(int,input().split())
	#N=input().rstrip()
	#N=int(input())
	#vA=list(map(int,input().split()))
	
	res = 0
	while N:
		N //= K
		res += 1
	
	print(res)

def mainC():
	#N,K=map(int,input().split())
	N=input().rstrip()
	#N=int(input())
	vA=list(map(int,input().split()))
	
	res = float("inf")
	for i in range(1, 101):
		t = sum((x-i)**2 for x in vA)
		res = min(res, t)
	
	print(res)


#mainA()
#mainB()
mainC()

Submission status: Accepted

Solution code:
p_num = input()
p_loc = input()
num = int(p_num)
loc_list = [int(i) for i in p_loc.split()]
value_list = []
for i in range(min(loc_list), max(loc_list)+1):
  value = 0
  for loc in loc_list:
    value += (loc-i)**2
  value_list.append(value)
    
print(min(value_list))

Submission status: Accepted

Solution code:
n = int(input())
xList = list(map(int, input().split()))
ans = 1000000
for i in range(1, 100):
    sums = 0
    for j in xList:
        sums += (j - i) ** 2
    if sums < ans:
        ans = sums
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8

n = int(input())
xlist = list(map(int,input().split()))
xlist.sort()
ans = 10**9+7
st = xlist[0]
ed = xlist[-1]
for p in range(st, ed + 1):
  pp = 0
  for i in range(n):
    pp += (xlist[i] - p) ** 2
  ans = min(ans, pp)
print(ans)

Submission status: Accepted

Solution code:

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

y = len(x)
mini = min(x)
maxi = max(x)
x.sort()

ans = 0
p = 0
for valuse in range(mini,maxi):
  p=0
  for j in range(n):
    p += (x[j] -valuse) **2
  if p < ans or ans == 0 :
    ans = p

    ans1 = valuse
  else:
    break

print(ans)

Submission status: Accepted

Solution code:


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

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

    print(int(res))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
# 1 ≤ x ≤ 100 に注目すると, 集合地点を全探索できる
ans = 1e100
for p in range(101):
  cnt = 0
  for i in x:
    cnt += (p - i) ** 2
  if ans > cnt:
    ans = cnt
print(ans)

Submission status: Accepted

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

center = sum(lists)/n
i_part = sum(lists)//n
f_part = center - i_part

if f_part >= 0.5:
    central_int = i_part + 1
else:
    central_int = i_part

stamina = 0
for x in lists:
    stamina += (x-central_int)**2
print(stamina)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from sys import stdin

n = int(input())
x_list_str = stdin.readline().rstrip().split()
x_list = [int(i) for i in x_list_str]

# 体力のリスト
sum_hp_list = []

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

if min_x == max_x:
    print(0)
    exit()

for i in range(min_x, max_x):
    sum_hp = 0
    for x in x_list:
        sum_hp += (x-i)**2
    sum_hp_list.append(sum_hp)

print(min(sum_hp_list))

Submission status: Accepted

Solution code:
import numpy as np

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

P = int(np.round(np.mean(X_array)))

ans = np.sum((X_array - P)**2)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

A_list_str = input()
A_list_strs = A_list_str.split()
X = [int(x) for x in A_list_strs]
HP = 0
HP_b = 0

center = sum(X)//N
center_b = center + 1

for x in X:
    tmp = (x - center) ** 2
    HP = HP + tmp
    tmp1 = (x- center_b) ** 2
    HP_b = HP_b + tmp1

if HP_b < HP:
    print (HP_b)
else:
    print(HP)

Submission status: Accepted

Solution code:
n = int(input())
List = list(map(int, input().split()))
min = 10000000000
sum = 0
# Pを決める
for P in range(1,101):
 # L[i]のループこのループ終わったらsumを初期化しておきたい

    sum = 0
    for L in List:
        sum += pow((P - L), 2)
    if min >= sum:
        min = sum
# 　値の更新がうまくできていない
print(min)

Submission status: Accepted

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

Submission status: Accepted

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

A = []
for P in range(1,101):
    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())
list = list(map(int, input().split()))
c=100
l=len(list)
m=0
k=0
i=0
while(k<l):
  m+=(list[k]-1)**2
  k+=1
j=1
while(j<c):
  r=0
  i=0
  while(i<l):
    r+=(list[i]-j) ** 2
    i+=1
  if(r<m):
    m=r
  j+=1
print(m)

Submission status: Accepted

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

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

tmpans = 0
ans = float('inf')

for P in range(min(X), max(X) + 1):
    for x in X:
        tmpans += (x-P)**2

    if tmpans < ans:
        ans = tmpans
    tmpans = 0
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
x.sort()
ans = float("inf")

for i in range(x[0], x[-1]+1):
    t = 0
    for q in range(n):
        t += (i - x[q]) ** 2
    else:
        if ans > t:
            ans = t

print(ans)

Submission status: Accepted

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

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

E = [0 for i in range(100)]

for i in range(100):
    E[i] = 0
    for x in K:
        E[i] += (x-i)**2

print(min(E))

Submission status: Accepted

Solution code:
# coding: utf-8


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

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

    print(min(min_sums))

    return 0


if __name__ == '__main__':
    main()

Submission status: Accepted

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

def num(P, X):
    number = 0
    for x in X:
        num = x-P
        #print(str(num))
        num2 = num**2
        number += num2
    return number

nList = []
for p in range(min(X), max(X) + 1):
    nList.append(num(p, X))

n = str(min(nList))
print(n)

Submission status: Accepted

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

result=[]
sum=0
for i in range(min(a),max(a)+1):
    sum=0
    for j in range(len(a)):
        sum += int((a[j]-i)**2)
    result.append(sum)

print(min(result))

Submission status: Accepted

Solution code:
import sys
N = int(input())
X = [int(_) for _ in input().split()]
ans_list = []
if N == 1:
    print('0')
    sys.exit()
for i in range(min(X), max(X) + 1):
    ans = 0
    for j in X:
        ans += (j - i) ** 2
    ans_list.append(ans)

print(min(ans_list))

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())
x = list(map(int,input().split()))
x2 = np.power(x,2)

p1 = sum(x)//n
p2 = -(-sum(x)//n)

w1 = n*(p1**2) -2*sum(x)*p1 +sum(x2)
w2 = n*(p2**2) -2*sum(x)*p2 +sum(x2)

print(min(w1,w2))

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
ans=int(1000000000)
for i in range(1,1000):
    now=int(0)
    for j in range(n):
        now+=(i-A[j])*(i-A[j])
    if now<ans:
        ans=now
        
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
import math
N = int(input())


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

# med = int(math.sqrt((sum(out2)/N)))

cal = [0]*N
r = min(X)
l = max(X)
mid = (l - r) // 2
out = []

for j in range(X[0], X[-1]+1):
    for i in range(N):
        cal[i] = (X[i] - j) ** 2
    out.append(sum(cal))

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

Submission status: Accepted

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

print(min(data))

Submission status: Accepted

Solution code:
import sys

f = sys.stdin
N = int(f.readline()[:-1])
X = list(map(int, f.readline().split(' ')))


def cost(p):
    return sum((x - p)**2 for x in X)


P1 = sum(X) // N
P2 = P1 + 1
result1 = cost(P1)
result2 = cost(P2)
if result1 >= result2:
    print(result2)
else:
    print(result1)

Submission status: Accepted

Solution code:
# Initializing variables
people = int(input())
coordinates = list(map(int, input().split()))
max_ = max(coordinates)
min_ = min(coordinates)
ans = 0

# Calculating the minimum total of stamina
for i in range(min_, max_ + 1):
    currentMin = 0
    for j in range(people):
        currentMin += (coordinates[j] - i) ** 2
    if i == min_:
        ans = currentMin
    elif currentMin < ans:
        ans = currentMin
        
print(ans)

# Check out my Youtube channel! https://www.youtube.com/channel/UCMOKVyAbTZOpKikTrX5dy_A

Submission status: Accepted

Solution code:
a=int,input()
b=list(map(int,input().split()))
c_1=sum(b)//len(b)
c_2=c_1+1
j_1=0
j_2=0
for k in b:
  j_1+=(c_1-k)**2
for k in b:
  j_2+=(c_2-k)**2
if j_1>=j_2:
  print(j_2)
else:
  print(j_1)

Submission status: Accepted

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

l=[]

i=1
for j in range(100):
    B=[n-i for n in A]
    C=(np.array(B))**2
    D=np.sum(C)
    l.append(D)
    i=i+1

print(min(l))

Submission status: Accepted

Solution code:
N = int(input())
x = list(map(int,input().split()))
count =[]
for i in range(100):
    HP = 0
    for j in range(N):
        a = (x[j]-(i+1))**2
        #print(x[j])
        HP +=a
 #   print(HP)
    count.append(HP)
   # print(HP)
print(min(count))
#消費体力を計算する

#前回と比較する
#前回より大きければ、記録する

Submission status: Accepted

Solution code:
line1 = input('')
line2 = input('')
n = int(line1)
lst = []
max_idx = 0
min_idx = 0
for x in line2.split(' '):
  lst.append(int(x))
  if max_idx == 0:
    max_idx = int(x)
  if min_idx == 0:
    min_idx = int(x)
  if max_idx < int(x):
    max_idx = int(x)
  elif min_idx > int(x):
    min_idx = int(x)

min_sum = 0
for p in range(min_idx,max_idx):
  sum = 0
  for x in lst:
    d = (x - p)*(x - p)
    sum += d
  if min_sum == 0:
    min_sum = sum
  elif sum < min_sum:
    min_sum = sum

print(min_sum)

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
s = sum(x)
xx = 0
for i in x:
    xx += i**2
def f(p):
    return n * p**2 - 2 * p * s + xx
ans = f(1)
for i in range(2, 101):
    ans = min(ans, f(i))
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
max = max(x)
min = min(x)
ans = 1000001
for i in range(min, max + 1):
    score = 0
    
    for item in x:
        score = score + ((item - i) ** 2)
    
    if score <= ans:
        ans = score

print(ans)

Submission status: Accepted

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

ans = []
m = max(list_score)

for i in range(1, m+1):
    c = []
    #print(i)
    for j in range(n):
        a = list_score[j] - i
        b = a**2
        c.append(b)
    #print(len(c))
    #print(c)
    ans.append(sum(c))

#print(ans)
ans.sort()

print(ans[0])

Submission status: Accepted

Solution code:
import numpy as np
n = input()
x = input().split()
n = int(n)
x = list(map(int,x))
x = np.array(x)
maxed = np.max(x)
cand = []

for p in range(1,maxed+1):
    xp = (x - p) ** 2
    cand.append(np.sum(xp))
y = np.min(cand)
print(y)

Submission status: Accepted

Solution code:
n = int(input())

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

sum = []
i_sum = 0


for i in range(100):
    
    for j in range(n):
        i_sum += ((address[j] - (i + 1)) ** 2)

    sum.append(i_sum)
    i_sum = 0

print(min(sum))

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    B = list(map(int, input().split()))
    #  [1,2,3]
    # P = range(1, 100)
    l = []

    for P in range(1, 100):
        l2 = []
        for t in B:
            x = (t - P) ** 2
            l2.append(x)

        l.append(sum(l2))
    print(min(l))


main()

Submission status: Accepted

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

ans1 = 0
ans2 = 0
S = sum(X)//N

for x in X:
    ans1 += (x - S) ** 2
    ans2 += (x - (S + 1)) ** 2

print(min(ans1, ans2))

Submission status: Accepted

Solution code:
b=input()
b=int(b)
a = input().split()
a=[int(i) for i in a]
a.sort()
c=0
d=99999999999999999
e=a[0]
f=a[b-1]
if e==f:
    d=0

else:
    for p in range (e,f):
        for x in range (0,b):
            c = c+(a[x]-p)**2
        if d>=c:
            d=c
            c=0

print(d)

Submission status: Accepted

Solution code:
import math

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

min = 0
avg = round(sum(X) / len(X))

for x in X:
    min += (x - avg)**2

print(min)

Submission status: Accepted

Solution code:
def main():
    n = input()
    array = list(map(int, input().split(" ")))
    minX = min(array)
    maxX = max(array)
    p_sum = 0
    power_list = []
    for p in range(minX, maxX + 1):
        for x in array:
            p_sum += power(x, p)
        power_list.append(p_sum)
        p_sum = 0        
    print(min(power_list))

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

if __name__ == "__main__":
    main()

Submission status: Accepted

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


x_sum = sum(x)
# center = int((max_x + min_x)/2)
center = round(x_sum /n)
result = 0
for x_i in x:
    result += (x_i - center)*(x_i - center)

print(result)

Submission status: Accepted

Solution code:
import statistics
n=int(input())
x=[int(_) for _ in input().split()]
mid=int(statistics.mean(x))
mmid=mid-1
pmid=mid+1
cnt=0
cntm=0
cntp=0
for i in range(len(x)):
  cnt+= (x[i]-mid)**2
  cntm+=(x[i]-mmid)**2
  cntp+=(x[i]-pmid)**2
print(min(cnt,cntm,cntp))

Submission status: Accepted

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

Submission status: Accepted

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

temp_sum = 0
ans = 10 ** 9

for p in range(100):
    for x in x_list:
        temp_sum += (x - p) ** 2
    if temp_sum < ans:
        ans = temp_sum
    temp_sum = 0

print(ans)

Submission status: Accepted

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

xp = []
b = 0
for i in range(min(x), max(x) + 1):
  xp.append([])
  for j in x:
    xp[b].append((j ** 2) - (2*j*i) + (i**2))
  b += 1

all_sum = []
for i in xp:
 all_sum.append(sum(i))

print(int(min(all_sum)))

Submission status: Accepted

Solution code:
import math

if __name__=="__main__":
    n = int(input())
    X = [int(a) for a in input().split()]

    sum_x = sum(X)

    sum_x2 = 0
    for x in X:
        sum_x2 += x**2
    
    z = math.floor(sum_x/n)
    r = sum_x/n - z

    if r <= 0.5:
        ans = sum_x2 + n*z**2 - 2*z*sum_x
    else:
        ans = n - 2*(1+z)*sum_x + 2*z*n + z**2*n + sum_x2

    print(ans)

Submission status: Accepted

Solution code:
N = int(input().rstrip())
Xs = [int(x) for x in map(int, input().rstrip().split())]
import sys
sum_val = sys.maxsize
for i in range(min(Xs), max(Xs)+1):
    tmp_sum_val = 0
    for j in Xs:
        tmp_sum_val += (j - i)**2
    if sum_val > tmp_sum_val:
        sum_val = tmp_sum_val
print(sum_val)

Submission status: Accepted

Solution code:
from sys import stdin
 
n = int(input())
k = [int(x) for x in stdin.readline().rstrip().split()]

k.sort()

ave = round(sum(k) / len(k) )

#print(ave)

def calc_pow(n):
    i = pow(n - ave, 2) 
    return i
  
j = map(calc_pow, k)

print(sum(j))