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 = str(input())
x = list(x.split())

s = 0

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

    
p = round(s/n)

en = 0
for i in range(len(x)):
    en += (int(x[i])-p)**2

print(en)

Submission status: Accepted

Solution code:
INF = 1001001001

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

ans = INF
for i in range(1, 101):
    stamina = 0
    for j in range(1, N+1):
        stamina += (X[j-1] - i)**2
    ans = min(ans, stamina)

print(ans)

Submission status: Accepted

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

p=sum(x)/n
import math
p1=math.floor(p)
p2=math.ceil(p)
ans1=0
ans2=0

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import math
import bisect

def query_value(A, m):
    ans = 0
    for a in A:
        ans += (m - a) ** 2
    return ans

def main():
    n = int(input())
    A = list(map(int, input().split()))
    min_val = 10 ** 18
    for i in range(1, 101):
        val = query_value(A, i)
        min_val = min(min_val, val)
    print(min_val)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

hp = []
for p in range(min(X),max(X)+1):
  h = 0
  for x in X:
    h += (p-x)*(p-x)
  hp.append(h)
print(min(hp))

Submission status: Accepted

Solution code:
n=int(input())
xs=list(map(int,input().split()))
p=sum(xs)//n
hp,hq=0,0
for i in xs:
    hp+=(i-p)**2
    hq+=(i-p-1)**2
print(min(hp,hq))

Submission status: Accepted

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

a = round(sum(X)/N)
c = 0

for i in range(N):
  c += (X[i] - a)**2
  
print(c)

Submission status: Accepted

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

Submission status: Accepted

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

average = [sum(X)//N,(sum(X)//N)+1]

ans = 0

check = [[],[]]

for i in range(2):
    for j in X:
        a = (abs(j-average[i]))**2
        check[i].append(a)

if sum(check[0]) <= sum(check[1]):
    print(sum(check[0]))
else:
    print(sum(check[1]))

Submission status: Accepted

Solution code:

n = int(input())
x = list(map(int, input().split(" ")))
print(min([ sum(map(lambda x: (x-p)**2, x)) for p in range(min(x), max(x) + 1)] ))

Submission status: Accepted

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

Submission status: Accepted

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


ans = float("inf")
for i in range(1,101):
    conf_site = i
    total = sum([(x - conf_site)**2 for x in X])
    ans = min(ans, total)

print(ans)

Submission status: Accepted

Solution code:
import math
n = int(input())
A = list(map(int,input().split()))
count = 0
for i in A:
    count +=i
ave = count/n
f,i = math.modf(ave)
answer = 0
if f >=0.5:
    a = i + 1
    for i in A:
        answer += (i-a)**2
else:
    a = i
    for i in A:
        answer +=(i-a)**2
print(int(answer))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
X_a = max(a)
N_a = min(a)
L_a = X_a - N_a + 1
b=[0]*L_a

while True:
  if N_a >= X_a:
    break
  for k in range(1,L_a+1):
    for i in a:      
      b[k-1] += (i-N_a)*(i-N_a)
    N_a +=1
print(min(b))

Submission status: Accepted

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

ave = sum(x) // n

r, l = 0, 0
for a in x:
    r += (a - ave)**2
    l += (a - ave - 1)**2

print(min(r, l))

Submission status: Accepted

Solution code:
n=int(input())
x_list=list(map(int,input().split()))
ans=10000000
for i in range(101):
    tem=0
    for k in x_list:
        tem+=(k-i)**2
    ans=min(tem,ans)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
L = list(map(int,input().split()))
min1 = 10**10

for p in range(101):
    en = 0
    for i in range(n):
        en += (L[i]-p)**2
    min1 = min(min1,en)
print(min1)

Submission status: Accepted

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

L = min(X)
R = max(X)

def ene(p):
  cost = 0
  for x in X:
    cost += (x - p) * (x - p)
  return cost

energy = float('inf')
while (L <= R):
  energy = min([energy, ene(L)])
  L += 1
  
print(energy)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

ans = (100 ** 2) * 100 + 1

_min = min(x)
_max = max(x)

for i in range(_min, _max + 1):
    tmp = 0
    for j in x:
        tmp += (j - i) ** 2
    ans = min(ans, tmp)

print(ans)

Submission status: Accepted

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

avg_x = sum(X)/len(X)
p = round(avg_x)
hp = [abs(x-p)**2 for x in X]
print(sum(hp))

Submission status: Accepted

Solution code:
n = int(input())
x = [int(x) for x in input().split()]
e = round(sum(x)/len(x))
k = 0
for i in range(len(x)):
    k = k + (x[i] - e)*(x[i] - e)
print(k)

Submission status: Accepted

Solution code:
N=int(input())
a=list(map(int,input().split()))
ans=[]
b=0
if N==1:
    print(0)
else:
    for i in range(min(a),max(a)+1):
        for k in range(N):
            b+=(a[k]-i)**2
        ans.append(b)
        b=0
    print(min(ans))

Submission status: Accepted

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

P = int(sum(X)/N+0.5)

print(sum(map(lambda x_i: (x_i-P)**2, X)))

Submission status: Accepted

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

y =[(i-p)**2 for i in x]
z =[(i-p-1)**2 for i in x]
print(min(sum(y),sum(z)))

Submission status: Accepted

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

Submission status: Accepted

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

ans=1145141919
for p in range(max(x)+1):
    tmp_ans=0
    for i in range(n):
        tmp_ans+=(x[i]-p)**2
    ans=min(ans,tmp_ans)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
point_list = list(map(int, input().split()))
hp_list = []
for p in range(1, max(point_list) + 1):
    hp = 0
    for i in range(len(point_list)):
        hp += (point_list[i] - p) ** 2
    hp_list.append(hp)
print(min(hp_list))

Submission status: Accepted

Solution code:
# Template 1.0
import sys, re
from collections import deque, defaultdict, Counter, OrderedDict
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from heapq import heappush, heappop, heapify, nlargest, nsmallest
def STR(): return list(input())
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def list2d(a, b, c): return [[c] * b for i in range(a)]
def sortListWithIndex(listOfTuples, idx):   return (sorted(listOfTuples, key=lambda x: x[idx]))
def sortDictWithVal(passedDic):
    temp = sorted(passedDic.items(), key=lambda kv: (kv[1], kv[0]))
    toret = {}
    for tup in temp:
        toret[tup[0]] = tup[1]
    return toret
def sortDictWithKey(passedDic):
    return dict(OrderedDict(sorted(passedDic.items())))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

n = INT()

x = LIST()

sqsum = 0
sum1 = 0
for el in x:
    sqsum+=el*el
    sum1+=el
z = 10**9
for p in range(0, 101):
    z = min(z, sqsum+p*p*n - 2*p*sum1)

print(z)

Submission status: Accepted

Solution code:
import math
N = int(input())
X = list(map(int,input().split()))
P = math.ceil(sum(X)/N)
P1 = P - 1
hp = 0
hp2 = 0

for x in X:
  hp_x = (P-x)**2
  hp2_x = (P1 - x)**2
  hp = hp + hp_x
  hp2 = hp2 + hp2_x
print(min(hp,hp2))

Submission status: Accepted

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

mn = min(X)
mx = max(X)

ans = []
hps = []
for i in range(mx + 1):
    for x in X:
        hps.append((x - i) ** 2)
    ans.append(sum(hps))
    hps = []

print(min(ans))

Submission status: Accepted

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

if sum(lis)/N - sum(lis)//N > 0.5:
    mid = sum(lis)//N +1
else :
    mid = sum(lis)//N
result = 0
for i in lis:
    result += (i-mid)**2

print(result)

Submission status: Accepted

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

for p in range(1, max(x) + 1):
  point = 0
  for _x in x:
    point += (_x - p) ** 2
    #print(p, _x, point)
  point_list.append(point)
#print(point_list)
print(min(point_list))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
X=list(map(int,input().split()))
ans2=10**100
ans3=0
for i in range(max(X)+1):
    ans=0
    for j in X:
        ans+=(i-j)**2
    ans2=min(ans2,ans)
print(ans2)

Submission status: Accepted

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.readline
read = sys.stdin.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import combinations
def run():
    N, *X = list(map(int, read().split()))
    A = sum(X)
    B = sum([val ** 2 for val in X])
    if A%N == 0:
        print(round(B - A ** 2 / N))
    else:
        p = A // N
        alpha = min(abs(p - A/N), abs(p - A/N + 1)) ** 2 * N + B - A ** 2 / N
        print(round(alpha))


if __name__ == "__main__":
    run()

Submission status: Accepted

Solution code:
import math

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

Xn_2 = [x*x for x in Xn]

S_Xn = sum(Xn)
S_Xn_2 = sum(Xn_2)

P = S_Xn/N
P_min = math.floor(P)
P_max = math.ceil(P)

print(min(S_Xn_2-2*S_Xn*P_max+N*P_max*P_max,S_Xn_2-2*S_Xn*P_min+N*P_min*P_min))

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
ans = 10**9
for i in range(1, 101):
  y = map(lambda x: (x-i)**2, x)
  ans = min(ans, sum(y))
print(ans)

Submission status: Accepted

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

import math, statistics

X_mean = statistics.mean(X)

X_mean_l = math.floor(X_mean)
X_mean_h = math.ceil(X_mean)

ans_l = 0
ans_h = 0

for i in range(N):
    ans_l += (X[i] - X_mean_l)**2
    ans_h += (X[i] - X_mean_h)**2

if ans_l < ans_h:
    print(ans_l)
else:
    print(ans_h)

Submission status: Accepted

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

min_count = 1e10
for i in range(101):
    count = 0
    for x in x_list:
        count += (x - i) * (x - i)
    if count < min_count:
        min_count = count

print(min_count)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Thu May  7 22:03:51 2020

@author: shinba
"""

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

largeX = max(X)
smallX = min(X)

ans = float("inf")

for i in range(smallX,largeX+1):
    S = 0
    for j in X:
        S += (i-j)**2
    ans = min(ans,S)
        
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
x_list = list(map(int, input().split()))
hp_list = []
min_x = min(x_list)
max_x = max(x_list)

for i in range(min_x, max_x+1):
  hp_sum = 0
  for j in x_list:
    hp = (j - i) ** 2
    hp_sum += hp
  hp_list.append(hp_sum)
print(min(hp_list))

Submission status: Accepted

Solution code:
n = int(input())
x = [int(x) for x in 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()))
 

cnt_min = 10000000000000000

for P in range(0, 101):
  cnt = 0
  for x in X:
    cnt += (P-x)**2
  if cnt < cnt_min:
    cnt_min = cnt
  
print(cnt_min)

Submission status: Accepted

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

for p in range(1,100):
  count=0
  for xx in x:
    count+=(xx-p)**2
  sumlist.append(count)
print(min(sumlist))

Submission status: Accepted

Solution code:
from sys import stdin


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

    p = sum(x) / n

    p1 = int(p)
    p2 = int(p + 1)

    s1 = 0
    s2 = 0

    for xi in x:
        s1 += (xi - p1) ** 2
        s2 += (xi - p2) ** 2

    print(min(s1, s2))


if __name__ == "__main__":
    main()

Submission status: Accepted

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

min_effort = 100000000000
for i in range(100):
    accumulated_effort = 0
    for j in range(N):
        accumulated_effort += int(math.pow(i - X[j], 2))
    if min_effort > accumulated_effort:
        min_effort = accumulated_effort

print(min_effort)

Submission status: Accepted

Solution code:
n = int(input())

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

a1 = sum(x) // n
a2 = a1 + 1


ans1 = 0
ans2 = 0
for i in range(n):
    ans1 += (a1 - x[i])**2
    ans2 += (a2 - x[i])**2

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

Submission status: Accepted

Solution code:
def hp(P, X):
    ans = 0
    for x in X:
        ans += pow(x-P,2)
    return ans

N = int(input())
X = list(map(int, input().split()))
ans = float('inf')
for i in range(1, 101):
    ans = min(hp(i, X), ans)
print(ans)

Submission status: Accepted

Solution code:
# C
N = int(input())

X_list = list(map(int, input().split()))
mean_X = round(sum(X_list)/N)

res = 0
for x in X_list:
    res += (x - mean_X)**2

print(res)

Submission status: Accepted

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



sum_power = 999999999999

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

print(sum_power)

Submission status: Accepted

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

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

dis = []

for i in range(Xmax - Xmin + 1):
    tmp =  list(map(lambda x: (x - Xmin - i)**2, X))
    dis.append(sum(tmp))
    
print(min(dis))

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int,input().split()))
ans = 1000000
for i in range(100):
    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(int,input().split()))

if n ==1:
    print(0)
else:
    cnt = 0
    res_ = []
    for i in range(100):
        cnt = 0
        for j in range(len(x_)):
            cnt += (x_[j] - i)**2
        res_.append(cnt)

    print(min(res_))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
X = list(map(int, input().split()))
for p in range(max(X)+1):
    sum = 0
    for x in X:
        sum += ((x - p)**2)
    if p == 0:
        ans = sum
    if ans > sum:
        ans = sum
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
X = list(map(int, input().split()))
for p in range(max(X)+100):
    sum = 0
    for x in X:
        sum += ((x - p)**2)
    if p == 0:
        ans = sum
    if ans > sum:
        ans = sum
print(ans)

Submission status: Accepted

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

ans = float('inf')
for i in range(1, 101):
    cur = 0
    for j in X:
      cur += (j - i ) **2
    ans = min(ans, cur)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int,input().split()))
X.sort()
y = []
for i in range(1,100) :
    ans = 0
    for j in range(N) :
        ans += ((X[j] - i)**2)
    y.append(ans)
print(min(y))

Submission status: Accepted

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

a=10000000
for P in range(1,100+1):
    aP=0
    for i in range(N):
        aP=aP+((X[i]-P)**2)
    if aP<a:
        a=aP
print(a)

Submission status: Accepted

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

pos = round(sum(X) / n)
hp = 0
for x in X:
    hp += (x - pos) * (x - pos)

print(hp)

Submission status: Accepted

Solution code:
input()
list1 = list(map(int,input().split()))
res = 100000000
for i in range(1,101):
  sum1 =0
  for num in list1:
    sum1 += (num-i)**2
  res = min(res,sum1)
print(res)

Submission status: Accepted

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

ans = 10**6
for p in range(min(x),max(x)+1,1):
    power = 0
    for i in x:
        power += (i-p)**2
    if power < ans:
        ans =  power

print(ans)

Submission status: Accepted

Solution code:
# C - Rally

N = int(input())
X = [int(x) for x in input().split()]
total = float('inf')
for p in range(101):
    tmp = 0
    for x in X:
        tmp += (x - p) ** 2
    total = min(total, tmp)
print(total)

Submission status: Accepted

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

A = math.ceil(sum(X)/N)
B = math.floor(sum(X)/N)

a = 0
b = 0

for i in X:
  a += (i-A)**2
  
for _ in X:
  b += (_-B)**2
  
print(min(a,b))

Submission status: Accepted

Solution code:
N = int(input())
X = sorted(map(int,input().split()))
countermin = 0
for j in range(N):
        countermin += (X[j]-X[0])**2
        
for i in range(X[0],X[-1]+1):
    counter = 0
    for j in range(N):
        counter += (X[j]-i)**2
    if counter < countermin:
        countermin = counter
print(countermin)

Submission status: Accepted

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

Submission status: Accepted

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

min_num = 99999999999999999999

for i in range(1, 101):
    sum = 0
    for num in x:
        sum += (num - i) ** 2
    min_num = min(min_num, sum)
print(min_num)

Submission status: Accepted

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

total = 0

for i in range(100):
    for j in range(n):
        total += (x[j]-i)**2
    if i == 0:
        ans = total
    elif total < ans:
        ans = total
    total = 0

print(ans)

Submission status: Accepted

Solution code:
import math
N=int(input())
X=list(map(int,input().split()))
Prange= range(math.floor(min(X))-1,math.ceil(max(X))+1)
summin=100000000000
for P in Prange:
    sum=0
    for x in X:
        sum+=(P-x)**2
    if summin > sum:
        summin=sum
print(summin)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
x = [int(i) for i in input().split() ]

each = 0
loss = 0
ll = []

for k in range(min(x), max(x)+1):
      for j in range(len(x)):
            each = np.power((x[j]-k), 2)
            loss += each
      ll.append(loss)
      loss = 0
  
print(min(ll))

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    import numpy as np

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

    X_arr = np.array(X)
    mean = X_arr.mean()

    x1 = int(mean)
    x2 = int(mean)+1

    v1 = np.sum((X_arr-x1)**2)
    v2 = np.sum((X_arr-x2)**2)
    print(min(v1, v2))


if __name__ == "__main__":
    main()

Submission status: Accepted

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

mean = round(sum(l)/N)
stamina = 0

for p in l:
    stamina += (p - mean)**2

print(stamina)

Submission status: Accepted

Solution code:
import math
import numpy as np

N = int(input())

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

max_num = max(A)
min_num = min(A)

ans = 100101101

for i in range(min_num, max_num + 1):
    cost = 0
    for j in range(len(A)):
        cost += (A[j] - i) * (A[j] - i)
    ans = min(ans, cost)

print(ans)

Submission status: Accepted

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

ans = sum([x ** 2 for x in X])
for p in range(1,101):
  tmp = 0
  for x in X:
    tmp += (x - p) ** 2
  if ans > tmp:
    ans = tmp
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n=int(input())
x=list(map(int,input().split()))
power=[]
x.sort()
for i in range(x[0],x[n-1]+1):
  sum=0
  for j in range(n):
    sum+=(x[j]-i)**2
  power.append(sum)
power.sort()
print(power[0])

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop
from itertools import permutations, accumulate
from math import sqrt, ceil, floor, factorial
from bisect import bisect_left, bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
import sys
def I(): return int(input())  # '123' -> 123
def Is(): return map(int, input().split())  # '123 456' -> 123, 456
def LI(): return list(map(int, input().split()))  # '123 456' -> [123, 456]
def TI(): return tuple(map(int, input().split()))  # '123 456' -> (123, 456)
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def TIR(n): return [TI() for _ in range(n)]
def S(): return input()
def Ss(): return input().split()  # 'aa bb' -> 'aa','bb' or ['aa','bb']
def LS(): return list(input())  # 'abc123' -> ['a','b','c','1','2','3']
def SR(n): return [S() for _ in range(n)]
def SsR(n): return [Ss() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
sys.setrecursionlimit(1000000)
MOD = 1000000007
INF = float('inf')

N = I()
X = LI()
p = round(sum(X)/N)
ans = 0
for x in X:
    ans += (x-p)*(x-p)
print(ans)

Submission status: Accepted

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


while True:
  ans2=0
  
  for x in X:
    ans2 += (x-p)**2
   
   
  if ans2 > ans1:
    print(ans1)
    break
   
  ans1=ans2
  p +=1

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
import numpy as np
import math

def solve(N: int, X: "List[int]"):
    place = np.mean(X)
    place = round(place)
    result = 0
    for i in range(N):
        result += (X[i]-place) ** 2
    print(int(result))
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    X = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, X)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def my_round(x, d=0):
    import math
    p = 10 ** d
    return float(math.floor((x * p) + math.copysign(0.5, x)))/p

def resolve():
    import statistics
    n = int(input())
    x = [int(i) for i in input().split()]
    median = my_round(statistics.mean(x))
    res = sum([(i-median)**2 for i in x])
    print(int(res))
    
resolve()

Submission status: Accepted

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

ans = float('inf')

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline
n = int(readline())
x = [int(i) for i in readline().split()]

p1 = sum(x)//n
p2 = sum(x)//n+1
ans1 = 0
ans2 = 0
for i in x:
    ans1 += (i-p1)**2
    ans2 += (i-p2)**2
print(min(ans1, ans2))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

n = int(input())
x = list(map(int, input().split()))
a = sum(x) / n
ans = min(f(math.floor(a) , x), f(math.ceil(a) , x))
print(ans)

Submission status: Accepted

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

sum = 0
for i in range(len(Xs)):
    sum += Xs[i]
average = sum / n
surpls = sum % n

ans = 0
if surpls == 0:
    for i in range(len(Xs)):
        ans += (Xs[i]-int(average))**2
else:
    diff1 = 0
    diff2 = 0
    for i in range(len(Xs)):
        diff1 += (Xs[i]-int(average))**2
    for i in range(len(Xs)):
        diff2 += (Xs[i]-int(average)-1)**2
    if diff1 >= diff2:
        ans = diff2
    else:
        ans = diff1
print(ans)

Submission status: Accepted

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

min_val = -1
for i in range(100):
    pos = i+1
    tmp = 0
    for x in x_list:
        tmp += (x - pos)**2
    if tmp < min_val or min_val == -1:
        min_val = tmp
print(min_val)

Submission status: Accepted

Solution code:
N = int(input())
Xs = list(map(int,input().split()))
ans = 0
ansCandidate = 0
for j in range(min(Xs),max(Xs)):
    ansCandidate = 0
    for i in range(N):
        ansCandidate += (Xs[i] - j)**2
    if j == min(Xs):
        ans = ansCandidate
    ans = min(ans,ansCandidate)
print(ans)

Submission status: Accepted

Solution code:
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
n = int(input())
nums = [int(i) for i in input().split()]
avg = sum(nums)/len(nums)
avg = Decimal(str(avg)).quantize(Decimal('0'), rounding=ROUND_HALF_UP)
print(sum([(num-int(avg))**2 for num in nums]))

Submission status: Accepted

Solution code:
N = int(input())  #数字
A = list(map(int, input().split()))  #リスト

B = sum(A) / len(A)
B = round(B)

C = 0
for i in A:
    C += (i - B) ** 2

print(C)

Submission status: Accepted

Solution code:
from sys import stdin

if __name__ == "__main__":
  N = int(input())
  X = [int(i) for i in input().split()]
  
  min_total_cost = 10**7
  for p in range(1, 100+1):
    total_cost = 0
    for x in X:
      total_cost += (p-x)**2
    
    min_total_cost = min(total_cost, min_total_cost)

  print(min_total_cost)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
N=int(input())
List=list(map(int, input().split()))
MinList=[]
ansList=[]

zahyou=1
while zahyou<=100:
    MinList=[]
    hito=0
    while hito<N:
        #print((List[hito-1]-zahyou)**2)
        MinList.append((List[hito]-zahyou)**2)
        hito+=1
    ansList.append(sum(MinList))
    #print(sum(MinList))
    zahyou+=1
#print(ansList)
print(min(ansList))

Submission status: Accepted

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

Submission status: Accepted

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

ind = range(1, max(X)+1)
K = [0]*(len(ind)+1)

for p in ind:
    K[p] = sum(map(lambda x: (x - p)**2, X))
#    print(K)
print(min(K[1:]))

Submission status: Accepted

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

ans = []
for i in range(1,101):
    k = 0
    for j in X:
        k += Dis[abs(i-j)]
    ans.append(k)
print(min(ans))

Submission status: Accepted

Solution code:
import numpy as np

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

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

sum_min = 1e+9
for p in range(min_x, max_x+1, 1):
    sum = np.sum((x_list-p)**2)
    sum_min = min(sum_min, sum)
print(sum_min)

Submission status: Accepted

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

ans_list = [0] * (max(num_list)+1)
for i in range(max(num_list)+1):
    for j in range(n):
        ans_list[i] += ((num_list[j] - i)**2)

print(min(ans_list))

Submission status: Accepted

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

ans = 10 ** 99
ansp = 0

for i in range(100):
    for j in range(n):
        ansp += abs(x[j] - i)**2
    if ans > ansp:
         ans = ansp
    ansp = 0 
    
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

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

X.sort()
X = np.array(X)
mn = np.inf
for i in range(X[0], X[-1]+1):
    mn = min(mn, sum((X - i)**2))
    
print(mn)

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split(' ')))
a = 0
b = 10**99
for i in range(100):
	for j in range(len(x)):
		a += (i-x[j])**2
	if b > a:
		b = a
	a = 0
print(b)

Submission status: Accepted

Solution code:
N = int(input())
X_list = list(map(int,input().split()))
sum_list = []
for P in range(min(X_list),max(X_list)+1):
  sum = 0
  for i in range(N):
    hp = (X_list[i]-P)**2
    sum += hp
  sum_list.append(sum)

print(min(sum_list))

Submission status: Accepted

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

T = [(X[i]-P)*(X[i]-P) for i in range(0,N,1)]
print(sum(T))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict
import numpy as np

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

memo = defaultdict(int)

for i in range(1, 101):
    tmp = ((x - i)**2).sum()
    memo[tmp] += 1

print(min(memo.keys()))