Problem:


Score : 300 points

Problem Statement

There are N people living on a number line.

The i-th person lives at coordinate X_i.

You are going to hold a meeting that all N people have to attend.

The meeting can be held at any integer coordinate. If you choose to hold the meeting at coordinate P, the i-th person will spend (X_i - P)^2 points of stamina to attend the meeting.

Find the minimum total points of stamina the N people have to spend.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq X_i \leq 100

Input

Input is given from Standard Input in the following format:

N
X_1 X_2 ... X_N

Output

Print the minimum total stamina the N people have to spend.

Sample Input 1

2
1 4

Sample Output 1

5

Assume the meeting is held at coordinate 2. In this case, the first person will spend (1 - 2)^2 points of stamina, and the second person will spend (4 - 2)^2 = 4 points of stamina, for a total of 5 points of stamina. This is the minimum total stamina that the 2 people have to spend.

Note that you can hold the meeting only at an integer coordinate.

Sample Input 2

7
14 14 2 13 56 2 37

Sample Output 2

2354



Sample input:
2
1 4


Sample output:
5

Submission status: Accepted

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

p = round(sum(x) / n)
print(sum(list(map(lambda xi: (xi - p)**2, x))))

Submission status: Accepted

Solution code:
import math

def calc(N, X):
    P = round(sum(X) / N)
    res = 0
    for x in X:
        res += (P - x) ** 2
    return math.ceil(res)

if __name__ == '__main__':
    N = int(input())
    X = list(map(int, input().split()))
    print(calc(N, X))

Submission status: Accepted

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

pmin = 99999999999999999

bsum = []
for count1 in range(0,N):
    asum = []
    for count2 in range(1,100):
        asum.append(abs(X[count1]-count2)**2)
    bsum.append(asum)


for count3 in range(0,99):
    asum = 0
    for count4 in bsum:
        asum += count4[count3]
    pmin = min(pmin,asum)

print(pmin)

Submission status: Accepted

Solution code:
from collections import defaultdict,deque,Counter
import sys,heapq,bisect,math,itertools,string,queue,copy,time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

N = inp()
xx = inpl()


ans = INF
for a in range(102):
    tmp = 0
    for x in xx:
        tmp += (a-x)**2
    ans = min(ans,tmp)

print(ans)

Submission status: Accepted

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

a_min = min(a)


for i in range(100):
    z = i + a_min
    hp = 0
    for x in a:
        hp += (x - z) ** 2
    if i == 0:
        ans = hp
    if ans > hp:
        ans = hp

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
X = sorted([int(x) for x in input().split()])
min_cost = 0
is_first = True
for p in range(X[0]+1, X[-1]):
    cost_sum = 0
    for x in X:
        cost_sum += (x-p)**2
    if is_first:
        min_cost = cost_sum
        is_first = False
    else:
        min_cost = min(min_cost, cost_sum)

print(min_cost)

Submission status: Accepted

Solution code:
N = int(input())

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

x.sort()

s = 0
t = 0
for i in range(N):
    s += (x[i]-100)**2
    t += (x[i]-1)**2

s = max(s,t)

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

Submission status: Accepted

Solution code:
import random
n = int(input())
l = input().split()
l = list(map(int, l))
print(min([sum([(i-j)**2 for i in l]) for j in range(1, 10000)]))

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

sum=0
A=[]

for i in range(-10,int(X[N-1]+100)):
    for j in range(N):
        sum+=(X[j]-i)**2
    A.append(sum)
    sum=0

print(min(A))

Submission status: Accepted

Solution code:
b = int(input())
a = input().split()
for i in range(len(a)):
    a[i] = int(a[i])

a2 = a.copy()

best = None

for i in range(len(a)):
    a[i] = a[i] + 1

for i in range(100):
    x = [0] * b
    for j in range(len(a)):
        a[j] = a[j] - 1
        x[j] = a[j]**2
    s = sum(x)
    if best is None or s < best:
        best = s

print(best)

Submission status: Accepted

Solution code:
n = int(input())

s = input().split()

s = [int(x) for x in s]

x_max = max(s)
x_min = min(s)


    
    
def ex(pnt):
    sm = 0
    for j in s:
        sm += (pnt - j)**2
    
    return sm

result = 10 **9

for i in range(x_min,x_max+1):
    koho = ex(i)
    if(result > koho):
        result = koho

print(result)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
N=int(input())

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

temp=sum(P)/len(P)
temp//=1
temp2=temp+1

ans=0
for item in P:
    ans+=(item-temp)**2
    
ans2=0
for item in P:
    ans2+=(item-temp2)**2
    
print(int(min(ans,ans2)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
from collections import deque as deq
import math

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

S = 0
for x in X:
    S += x

S /= len(X)
f, i = math.modf(S)
if f >= 0.5:
    i += 1

S = 0
for x in X:
    S += (i - x) ** 2

print(int(S))

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int, input().split()))
ans = float('inf')

for i in range(100):
  temp = 0
  for j in range(N):
    temp += (X[j] - (i+1))**2
  if ans > temp:
    ans = temp

print(ans)

Submission status: Accepted

Solution code:
def f(p, xs):
    r = 0
    for x in xs:
        r += (x-p)**2
    return r

N, = map(int, input().split())
X = list(map(int, input().split()))
r = 10**18
for i in range(101):
    r = min(r, f(i, X))
print(r)

Submission status: Accepted

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

min_cnt = 10000000

for p in range(1, 100):
    cnt = 0
    for i in range(n):
        cnt += (p - x[i])**2
    if min_cnt > cnt:
        min_cnt = cnt

print(min_cnt)

Submission status: Accepted

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

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

ans = []
for i in range(1, 101):
  a = 0
  for xi in x:
    a += (xi - i)*(xi - i)
  ans.append(a)
  
print(min(ans))

Submission status: Accepted

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

myavg = sum(mylist)//N

mysum1 = 0
for m in mylist:
    mysum1 += (m-myavg)**2
mysum2 = 0
for m in mylist:
    mysum2 += (m-(myavg+1))**2
print(min(mysum1, mysum2))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

minans = 100000000

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

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

for i in range(mi, ma+1):
	ans = np.sum((X-i) ** 2)
	if ans < minans:
		minans = ans

print(minans)

Submission status: Accepted

Solution code:
import math

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

low_center = math.ceil(sum(coods)/n)
hi_center = sum(coods)//n

ans = 0

if low_center == hi_center:
    for c in coods:
        ans += (c-low_center)**2
else:
    aa = 0
    ab = 0
    for c in coods:
        aa += (c-low_center)**2
        ab += (c-hi_center)**2
    ans = min(aa, ab)
print(ans)

Submission status: Accepted

Solution code:
def main():
    _ = int(input())
    X = list(map(int, input().split()))
    min = 10**8
    i = 0
    cnt = 0

    while True:
        sum = 0
        for a in X:
            sum += (a - i) ** 2
        i += 1
        if sum < min:
            min = sum
            cnt += 1
        if i > cnt:
            break
    print(min)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

minSum = 0
for i in range(max):
	sum = 0
	for j in range(N):
		sum += (X[j] - i-1) ** 2
	if (i == 0):
		minSum = sum
	elif (minSum > sum):
		minSum = sum
print(minSum)

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
ans=float('inf')
for i in range(1,101):
    ans=min(ans,sum([(i-j)**2 for j in l]))
print(ans)

Submission status: Accepted

Solution code:
import math
import numpy as np
N = int(input())
Xs = [int(x) for x in input().split()]
best_position_float = np.average(Xs)
best_position1 = math.floor(best_position_float)
best_position2 = math.ceil(best_position_float)
cost1 = sum([(best_position1 - x)**2 for x in Xs])
cost2 = sum([(best_position2 - x)**2 for x in Xs])
best_cost = min(cost1, cost2)
print(best_cost)

Submission status: Accepted

Solution code:
n = int(input())
k = list(map(int, input().split()))
p = min(k)
counta = 0
min_ = 2147483647
if max(k) == min(k):
    print(0)
    exit()

while p != max(k):
    tmp = 0
    for i in range(0,n):
        tmp += ((k[i]-p)**2)
    if tmp < min_:
        min_ = tmp
    p += 1
print(min_)

Submission status: Accepted

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

count = 0
ll = []

for P in range(1, 101):
    for k in range(N):
        count += (X[k] - P) ** 2
        if k == N-1:
            ll.append(count)
            count = 0
        else:
            pass
print(min(ll))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from statistics import mean
n = input()
x_s = list(map(int,input().split()))
point_x = int(format(mean(x_s), '.0f'))
sum_e = sum([(x - point_x) ** 2 for x in x_s])
print(sum_e)

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int, input().split()))
X_min = min(X)
X_max = max(X)
S = []
if X_min == X_max:
    S.append(0)
for x in range(X_min, X_max):
    s = 0
    for i in range(N):
        s += (X[i]-x)**2
    S.append(s)
print(min(S))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#ABC156 C - Rally

n = int(input())
x = [int(x) for x in input().split(' ')]
    
min_len =  100**2 * n

for i in range(100):
    all_len = 0
    for j in range(n):
        all_len += (x[j] - (i+1))**2

    min_len = min(min_len,all_len)

print(min_len)

Submission status: Accepted

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

x = int(input()) 
san = list(map(int,input().split(" ")))

if x == 1:
    print("0")
    sys.exit()

sta = min(san)
goa = max(san)

if sta == goa:
    print("0")
    sys.exit()

tai = 0
ans = [] 

for i in range(sta,goa):
    tai = 0
    for j in range(x):
        tai += (i - san[j])**2
    ans.append(tai)
    
print(min(ans))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
x = list(map(int, input().split()))

left = min(x)
right = max(x)

min_cost = 100000000000000
min_pos=-1

for i in range(left,right+1):
  cost=0
  for j in range(n):
    cost = cost + (x[j]-i)*(x[j]-i)
  if min_cost>cost:
    min_cost=cost
    min_pos=i

print(min_cost)

Submission status: Accepted

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

Xm = sum(X)/N
Xmi = int(Xm)
Xmc = math.ceil(Xm)

Si = list(map(lambda x:(x-Xmi)**2, X))
Sc = list(map(lambda x:(x-Xmc)**2, X))

ans = min([sum(Si),sum(Sc)])
print(ans)

Submission status: Accepted

Solution code:
n = int(raw_input())
x = raw_input()
ints = []
for i in x.split(" "):
  ints.append(int(i))
  
tmin=922337203685477580
for p in range(1,101):
  t = 0
  for i in ints:
    t += (i - p)**2
  if t<tmin:
    tmin = t

print(tmin)

Submission status: Accepted

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

for i in range(min(x),max(x)+1):
  for j in x:
    total+=(j-i)**2
  else:
    ans.append(total)
    total=0
print(min(ans))

Submission status: Accepted

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

cog = 0
for i in range(0, N):
    cog += int(X[i])
    
cog = round(cog/N)
ans = 0
for i in range(0, N):
    ans += (int(X[i]) - cog)**2
print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    x_s = list(map(int,input().split()))
    mn = 10 ** 14

    for i in range(101):
        #print("ポイント：",i,"のとき")

        sm = sum([((x-i)**2) for x in x_s])
        if sm < mn:
            mn = sm
    print(mn)




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

main()

Submission status: Accepted

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

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

for x in X:
    ans += abs(x-x_mean)**2
print(ans)

Submission status: Accepted

Solution code:

N = int(input())
list_number = list(map(int,input().split()))
m = round(sum(list_number) / N)
ans = 0
for ele in list_number:
    ans += (ele - m) ** 2

print(ans)

Submission status: Accepted

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

rangeMin=min(X)
rangeMax=max(X)+1

for i in range(rangeMin,rangeMax):
    tmp=0
    for value in X:
        tmp+=(value - i)**2
    if(minValue>tmp or minValue==-1):
        minValue=tmp


print(minValue)

Submission status: Accepted

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

mid = round(sum(X) / N)
hoge = 0
for i in X:
    hoge += (i - mid)** 2
    
print(hoge)

Submission status: Accepted

Solution code:
N = int(input())

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

ans = 0
tmp1 = 0
tmp2  = 0

if sum_X % N == 0:
    mean = sum_X // N
    for i in range(N):
       ans += (mean-X[i]) ** 2

else:
    mean1 = sum_X // N
    mean2 = -(-sum_X//N)
    for i in range(N):
      tmp1 += (mean1-X[i]) ** 2
      tmp2 += (mean2-X[i]) ** 2
    if tmp1 <= tmp2:
        ans = tmp1
    else:
        ans = tmp2  
print(ans)

Submission status: Accepted

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

for i in range(n):
    x[i]-=1

cost=[0]*100

for i in range(100):
    tai=0
    for j in range(n):
        tai+=(i-x[j])**2
    cost[i]=tai

print(min(cost))

Submission status: Accepted

Solution code:
n = int(input())
li = list(map(int,input().split()))
li.sort()
temp = 0
min = 100000000000000000000000000
for i in range(li[0], li[-1]+1):
    for k in li:
        temp += (k - i)**2
    if temp <= min:
        min = temp
    temp = 0
print(str(min))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-


def main():
    N = int(input())
    X = list(map(int, input().split()))
    result = []
    for n in range(1, 100):
        tmp = 0
        for x in X:
            tmp += (x - n)**2
        result.append(tmp)
    print(min(result))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import math

#n
n= int(input())

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

sum = 0

for i in range(len(x)):
    sum += x[i]

#よくわからんが中央値の前後でやってみる
sum1 = math.ceil(sum/n)
sum2 = math.floor(sum/n)

ans1 = 0
ans2 = 0

for i in range(len(x)):
    ans1 += abs(x[i]-sum1)**2
    ans2 += abs(x[i]-sum2)**2

if ans1 <= ans2:
    print(ans1)
else:
    print(ans2)

Submission status: Accepted

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

power = 10**18

for i in range(1, 101):
  power_i = 0
  for x_elm in x:
    power_i += (x_elm - i)**2
  power = min(power, power_i)
  
print(power)

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().strip().split()))
alist=[]
pm=10e9
for p in range (min(x),max(x)+1):
    ans=0
    for xi in x:
        ans += (xi-p)**2

    if(ans<pm):
        pm=ans
print(pm)

Submission status: Accepted

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

# 住人の人数
n = int(input())
# 住んでいる場所
x = list(map(int, input().split()))

print(min([sum([(temp - p) ** 2 for temp in x])for p in range(min(x), max(x) + 1)]))

Submission status: Accepted

Solution code:
N=int(input())
X=list(map(int,input().split()))
S1=0
S2=0
for i in range(N):
  S1+=(X[i])*(X[i])
  S2+=X[i]
mins=S1
for i in range(min(X),max(X)+1):
  if mins>N*i*i-2*S2*i+S1:
    mins=N*i*i-2*S2*i+S1
print(mins)

Submission status: Accepted

Solution code:
n=int(input())
mk=input().split()
t=0
s=0
ans=0
for k in mk:
  pk=int(k)
  t+=pk
  s+=(pk**2)
px=round(t/n)
ans+=s+n*(px**2)-2*px*t
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
xls = list(map(int, input().split()))
mx = max(xls)
res = 0
for i in range(1, mx + 1):
    s = sum((x - i) ** 2 for x in xls)
    if res == 0:
        res = s
    if s < res:
        res = s
print(res)

Submission status: Accepted

Solution code:
from statistics import mean


n = int(input())
X = list(map(int, input().split()))
m = int(mean(X))
M = m + 1

def dist(X, mean):
    out = 0
    for x in X:
        out += (x - mean) ** 2
    return out


print(min(dist(X, M), dist(X, m)))

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
x = np.array(list(map(int, input().split())))
p = np.min(x)
e = np.sum((x-p)**2)
for i in range(p+1, np.max(x)):
    tmp = np.sum((x-i)**2)
    if(e > tmp):
        e = tmp
print(e)

Submission status: Accepted

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

s.sort()
if len(set(s))==1:
  print(0)
else:
  minx=s[0]
  maxx=s[-1]
  c=10**9
  for p in range(minx,maxx):
    t=0
    for i in s:
      t=t+(i-p)*(i-p)
    if c>t:
      c=t

  print(c)

Submission status: Accepted

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

p = round(sum(x)/n)

d = sum([(i-p)**2 for i in x])

print(d)

Submission status: Accepted

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

c = 100000000000000000
for p in range(X[0], X[-1] + 1):
    P = sum([pow(x - p, 2) for x in X])
    if P >= c:
        break
    c = P
print(c)

Submission status: Accepted

Solution code:
N=int(input())
X=list(map(int,input().split()))
ans=[]
for i in range(max(X)+1):
    goukei = 0
    for j in range(N):
        if i%1==0:
            goukei+=(X[j]-i)**2
    ans.append(goukei)
kotae=set(ans)
print(min(kotae))

Submission status: Accepted

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

ave1 = -1*(-1*sum(X)//len(X))
ave2 = ave1 - 1
dist1 = sum([(i-ave1)**2 for i in X])
dist2 = sum([(i-ave2)**2 for i in X])

print(min(dist1, dist2))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
list = input().split()
N = int(list[0])
list2 = input().split()
x = 0
for i in range(N):
  x += int(list2[i])
x = x / N
x = round(x)
S = 0
for i in range(N):
  S += (int(list2[i]) - x)**2
print(S)

Submission status: Accepted

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


min_s = 100000000
for i in range(1, 101):
    s = 0
    for v in A:
        s += (v - i) ** 2

    min_s = min(min_s, s)

print(min_s)

Submission status: Accepted

Solution code:
import numpy as np

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

if np.mean(X)-int(np.mean(X))>=0.5:
    count=int(np.mean(X))+1
else:
    count=int(np.mean(X))    

result=0
for i in range(N):
    result+=(X[i]-count)*(X[i]-count)
print(result)

Submission status: Accepted

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

costs = []
for p in range(min(X), max(X)+1):
    cost = 0
    for x in X:
        cost += pow(x - p, 2)
    costs.append(cost)

print(min(costs))

Submission status: Accepted

Solution code:
import math

def round_manual(n):
    if n-int(n) >= 0.5:
        res = math.ceil(n)
    else:
        res = math.floor(n)
    return res

def resolve_c():
    n = input()
    l = list(map(int, input().split()))
    mean = sum(l) / len(l)
    res = round_manual(mean)
    ans = sum([(i-res) ** 2 for i in l])
    print(ans)
resolve_c()

Submission status: Accepted

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

Submission status: Accepted

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

for x in X:
    sumX = sumX + (x - avex) ** 2    
print(sumX)

Submission status: Accepted

Solution code:
n = int(input())
x = [int(i) for i in input().split()]
l = []
for i in range(max(x)+1):
    l.append(sum([(j-i)**2 for j in x]))

print(min(l))

Submission status: Accepted

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

avg = round(sum(x) / len(x))

sum = 0
for i in x :
    sum = sum + ((i - avg) * (i - avg))

print(sum)

Submission status: Accepted

Solution code:
N = int(input())
X = [int(_) for _ in input().split()]
max = 0
min = 100
for i in X:
    if max < i:
        max = i
    if min > i:
        min = i

ans = 0
for p in range(max-min):
    total = 0
    for i in X:
        total += (i-p) ** 2
    if ans == 0:
        ans = total
    if ans > total:
        ans = total
print(ans)

Submission status: Accepted

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

ans = float('inf')

for a in range(1, 1001):
    tmp = 0
    for b in A:
        tmp += (a - b) * (a - b)
    ans = min(ans, tmp)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int, input().split(' ')))
distlist = [1000000] * 101
for P in range(1, 101):
    dist = 0
    for Xi in X:
        dist += (Xi - P) ** 2
    distlist[P] = dist
print(min(distlist))

Submission status: Accepted

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

ans=10000000
f=0
for p in range(1,101):
  f=0
  for y in x:
    f+=(y-p)*(y-p)
  ans=min(ans,f)



print(ans)

Submission status: Accepted

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

p = round(sum(X)/len(X))
ans = 0
for a in X:
  ans += (a-p)*(a-p)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
x_list = input().split()
avg = 0
for i in range(n):
  x_list[i] = int(x_list[i])
  avg += x_list[i]
avg = round(avg / n)
ans = 0
for i in range(n):
  ans += (x_list[i] - avg) * (x_list[i] - avg)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
za = list(map(int,input().split()))
za_max = max(za)
za_min = min(za)
kouho = 0
kouho_sum = 0
ans = 9999999999999999999999999999999999999999999999999999999999999999999999999999
for zahyou in range(za_min,za_max+1):
    for i in za:
        kouho = (i - zahyou)*(i - zahyou)
        kouho_sum = kouho_sum + kouho
    if(ans > kouho_sum):
        ans = kouho_sum
        kouho_sum = 0
print(ans)

Submission status: Accepted

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

e = 0
ave = 0
for i in range(0, len(X)):
    ave += X[i]/len(X)

ave = int(ave+0.5)

for i in range(0,len(X)):
    energy = X[i] - ave
    e += energy ** 2

print(e)

Submission status: Accepted

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

m = 10 ** 100
for i in range(1, 101):
  t = 0
  for x in X:
    t += (x - i) ** 2
  if m > t:
    m = t

print(m)

Submission status: Accepted

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

def solve(N: int, X: "List[int]"):
    min = None
    for p in range(1, 101):
        r = 0
        for x in X:
            r += (x - p)**2
        if min is None or min > r:
            min = r
    return min

# 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]"
    print(solve(N, X))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

s=sum(ls)/n

if abs(math.ceil(s)-s)<0.5:
    t=math.ceil(s)
else:
    t=math.floor(s)


ans=0
for i in ls:
    ans+=(t-i)**2
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
x_square = [num ** 2 for num in x]
sum_x = sum(x)
sum_x_square = sum(x_square)
minVal = 1000000
for p in range(101):
    minVal = min(minVal, sum_x_square - 2 * p * sum_x + n * p ** 2)
print(minVal)

Submission status: Accepted

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

sum = 0
for i in range(n):
    sum += x[i]
ave = round(sum/n)

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

print(ans)

Submission status: Accepted

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

maximum = max(x)
ans = 1000000000
for i in range(1, maximum + 1):
  total = 0
  for j in x:
    total += (j - i)** 2

  if total < ans:
    ans = total

print(ans)

Submission status: Accepted

Solution code:
def return_distance(X,p):
    A=list(map(lambda a:(a-p)**2,X))
    return sum(A)

N=int(input())
X=list(map(int,input().split()))
min_p=min(X)
max_p=max(X)

ans=return_distance(X,max_p)
for p in range(min_p,max_p):
    if ans>return_distance(X,p):
        ans=return_distance(X,p)
    else:
        pass

print(ans)

Submission status: Accepted

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

ans_raw = []
count = 0

for i in range(1, max_raw + 1):
    count = 0
    for j in range(0, len(X)):
        count += (X[j] - i) ** 2
    ans_raw.append(count)

print(min(ans_raw))

Submission status: Accepted

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

p = sum(x)//n
ans1 = 0
ans2 = 0
for i in range(n):
    ans1 += (x[i]-p) ** 2

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

print(min(ans1, ans2))

Submission status: Accepted

Solution code:
n = int(input())
*x, = map(int, input().split())
x.sort()
mid = (x[0] + x[n-1]) // 2
ans = float('inf')

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

Submission status: Accepted

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

M = np.zeros((100, A.shape[0]))

for i in range(100):
    M[i, :] = A - (i + 1)

t = M * M


t1 = np.sum(t, axis=1)


print(int(np.min(t1)))

Submission status: Accepted

Solution code:
n = int(input())
xlist = list(map(int, input().split()))
s = 0
t = 0
min = 0
ave = int(sum(xlist)/len(xlist))
for j in xlist:
  s += (j-ave)**2
  t += (j-ave-1)**2
if s < t:
  print(s)
else:
  print(t)

Submission status: Accepted

Solution code:
N = int(input())

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

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

each_value_list = []
min_value = 10e1000
for i in range(min_x, max_x + 1):
    tmp_sum = 0
    for x in x_list:
        tmp_sum += (x - i)**2
    if min_value > tmp_sum:
        min_value = tmp_sum

print(min_value)

Submission status: Accepted

Solution code:
import numpy as np
n=int(input())
i = list(map(int, input().split()))
ia=np.array(i)
ave=np.mean(ia)
ans=0
for k in range(n):
  ans+=(round(ave, 0)-i[k])**2
print(int(ans))

Submission status: Accepted

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

ave=float(0)
for a in x:
    ave=ave+a
ave=ave/n

disa=0
disb=0

for a in x:
    disa=disa+(int(ave)-a)**2
    disb=disb+(int(ave+1)-a)**2

if disa<=disb:
    print(disa)
else:
    print(disb)

Submission status: Accepted

Solution code:
import math


def main():
    n = int(input())
    array = [int(s) for s in input().split(" ")]
    min_v = min(array)
    max_v = max(array)
    min_ans = float("inf")
    for i in range(min_v, max_v + 1):
        total = 0
        for a in array:
            total += (a - i) ** 2
        if min_ans > total:
            min_ans = total
    print(min_ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import ceil
N = int(input())
X = list(map(int, input().split()))
# N=2
# X=[1,4]
x = (sum(X)/N)
kouho = [int(x), ceil(x)]
ans = [0,0]
for k in range(len(kouho)):
    for i in range(N):
        ans[k] += (X[i]-kouho[k])**2
print(min(ans))

Submission status: Accepted

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

minDis = 0
flag = True

for p in range(1,101):

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

    if flag:
        minDis = dis
        flag = False
        continue

    if dis < minDis:
        minDis = dis

print(minDis)

Submission status: Accepted

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

    ans = 100000000000

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

        ans = min(tmp, ans)

    print(ans)
    
resolve()

Submission status: Accepted

Solution code:
n = int(input());a = input().split();print(min([sum([(int(i)-j)**2 for i in a]) for j in range(1,n+10000)]))

Submission status: Accepted

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

Submission status: Accepted

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

total_strength = []

for p in range(max(x)+1):
    strength = 0
    for i in range(len(x)):
        strength += (x[i]-p)**2
    total_strength.append(strength)

print(min(total_strength))

Submission status: Accepted

Solution code:
N=int(input())
X=list(map(int,input().split()))
def tairyoku(n):
    return (n-i)**2
i=1
XT=list(map(tairyoku,X))
ans=sum(XT)
for i in range(2,101):
    XT=list(map(tairyoku,X))
    ans=min(ans,sum(XT))
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
from collections import deque, defaultdict
import bisect


def comb(a, b):
    result = 1
    for i in range(b):
        result *= (a - i) / (i + 1)
    return result


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

    min_load = None
    for p in range(101):
        load = sum([(x - p) ** 2 for x in Xs])
        if min_load is None or min_load > load:
            min_load = load

    print(min_load)


if __name__ == '__main__':
    main()