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()))
sum = 1000000
for i in range(1, 101):
  tmp = 0
  for x in X:
    tmp += (x - i) ** 2
  sum = min(sum, tmp)
print(sum)

Submission status: Accepted

Solution code:
N = int(input())
Xs = list(map(int, input().split()))
s = sum(Xs)
s2 = sum([X*X for X in Xs])
rs = []
for P in range(1, 100+1):
  r = N*P*P-2*s*P+s2
  rs.append(r)
r = min(rs)
print(r)

Submission status: Accepted

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

s = sum(x)//n
s1 = s+1
tairyoku1=0
tairyoku2=0

for i in range(n):
  tairyoku1 += (x[i]-s)**2
  tairyoku2 += (x[i]-s1)**2
  
print(min(tairyoku1,tairyoku2))

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    X = list(map(int, input().split()))
    Xmin = min(X)
    Xmax = max(X)
    ans = 1000000000000
    for i in range(Xmin-1, Xmax+2):
        tmp = 0
        for p in X:
            tmp += (p-i)*(p-i)
        ans = min(ans, tmp)
    print(ans)

main()

Submission status: Accepted

Solution code:
n=int(input())
li = list(map(int,input().split()))
ans = [-1,10**7]

for i in range(1,101):
    a = 0
    for j in range (len(li)):
        a += (li[j]-i)**2
    if a <= ans[1]:
        ans[0] = i
        ans[1] = a
    
print(ans[1])

Submission status: Accepted

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

m = min(s)
M = max(s)

lis = []
ans = 0

for i in range(m,M + 1):
  for j in range(N):
  	ans += (s[j] - i) ** 2
  lis.append(ans)
  ans = 0
  
print(min(lis))

Submission status: Accepted

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

m = s[0]
M = s[N-1]

lis = []
ans = 0

for i in range(1000):
  for j in range(N):
  	ans += (s[j] - i) ** 2
  lis.append(ans)
  ans = 0
  
print(min(lis))

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    x = list(map(int, input().split()))
    
    t = []
    for p in range(max(x)+1):
        t.append(sum((i-p)**2 for i in x))
    print(min(t))
    
if __name__ == '__main__':
    main()

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

center = round(sum(x) / len(x))
results = [(hp - center) ** 2 for hp in x]
print(sum(results))

Submission status: Accepted

Solution code:
def resolve():
    import numpy as np
    n=int(input())
    x_base=list(map(int,input().split()))
    x_base.sort()
    x=np.array(x_base)
    ans=10**10
    for i in range(x[0],x[-1]+1):
        if sum((x-i)**2) <=ans:
            ans=sum((x-i)**2)
    print(ans)
resolve()

Submission status: Accepted

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

li = []
p = 0
for i in range(1, 101):
    for j in range(n):
        p += (x[j] - i) ** 2

    li.append(p)
    p = 0

print(min(li))

Submission status: Accepted

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

ans = 100000000000000000

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

Submission status: Accepted

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

p = sum(x) // len(x)

for i in x:
    h[0] += (i - p)**2
    h[1] += (i - (p + 1))**2

print(min(h))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n = int(input())
xs = list(map(int, input().split()))
ans = 10 ** 9
for p in range(101):
    s = 0
    for x in xs:
        s += (x - p) ** 2
    ans = min(ans, s)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
X = list(map(int, input().split()))
X.sort()
p = X[0]
min_power = float('inf')
while p <= X[-1]:
  power = 0
  for x in X:
    power += pow((x-p),2)
  min_power = min(min_power, power)
  p += 1
print(min_power)

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
ans1 = 0
ans2 = 0
xa = sum(x) // n
for i in x:
    ans1 += (abs(i-xa))**2

for i in x:
    ans2 += (abs(i-xa-1))**2

print(min(ans1, ans2))

Submission status: Accepted

Solution code:
n = int(input())
li = list(map(int,input().split()))
o1 = (sum(li))//n
o2 = o1 + 1
li1 = []
li2 = []
for i in range(n):
    li1.append((int(li[i]-o1))**2)
for j in range(n):
    li2.append((int(li[j]-o2))**2)
if sum(li1) >= sum(li2):
    print(sum(li2))
else:
    print(sum(li1))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math


def inp():
    return(int(input()))


def invr():
    return(map(int, input().split()))


n = inp()
arr = invr()
arr = sorted(arr)
max = arr[n-1]
min = arr[0]

cost = 100000000000
for p in range(min, max+1):
    temp_cost = 0
    for xi in arr:
        temp_cost += (xi-p)**2
    if temp_cost < cost:
        cost = temp_cost
print(cost)

Submission status: Accepted

Solution code:
import statistics
import math
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN

n = int(input())

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

mean = statistics.mean(x)

syukai = Decimal(str(mean)).quantize(Decimal('0'), rounding=ROUND_HALF_UP)

#print(syukai)

sum = 0

for i in range(len(x)):
    sum += (x[i] - syukai)**2

print(sum)

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
l.sort()
ans=10e8
for j in range(l[0],l[-1]+1):
    hp=0
    for i in l:
        hp+=(i-j)**2
    if ans>hp:
        ans=hp
print(ans)

Submission status: Accepted

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

t = round(sum(x)/n)
ans = 0
for i in x:
  ans += abs((i-t))**2
print(ans)

Submission status: Accepted

Solution code:
N =int(input())
X = [int(i) for i in input().split()]
m = sum(X)//len(X)
mm = m+1

suma1 = 0
suma2 = 0
for i in X:
  suma1 += (i-m)**2
  suma2 += (i-mm)**2
print(min(suma1,suma2))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
num = int(input())
lst = sorted(list(map(int, input().split())))
result = float('inf')
for i in range(lst[0], lst[-1] + 1):
    tmp = 0
    for v in lst:
        tmp += (v - i) ** 2
    if tmp < result:
        result = tmp
print(result)

Submission status: Accepted

Solution code:
from sys import stdin

n = int(stdin.readline().strip())
x_lst = [int(x) for x in stdin.readline().strip().split()]

p1 = sum(x_lst) // n
p2 = sum(x_lst) // n + 1

p1_sum = sum([(x-p1) ** 2 for x in x_lst])
p2_sum = sum([(x-p2) ** 2 for x in x_lst])

print(min(p1_sum, p2_sum))

Submission status: Accepted

Solution code:
N = int(input())

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

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

ans = 1000000

for i in range(L, R+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()))
X2SUM = sum([i**2 for i in X])
XSUM = sum([i for i in X])
minn = float('inf')
for i in range(1,101):
    minn = min(minn, X2SUM+-2*XSUM*i+N*(i**2))
print(minn)

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
minx = min(x)
maxx = max(x)
list = []
for i in range(minx,maxx+1):
  klist = []
  for j in x:
    k = (j-i)*(j-i)
    klist.append(k)
  #print(klist)
  list.append(sum(klist))
print(min(list))

Submission status: Accepted

Solution code:
import sys
import math
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
from collections import deque
from bisect import bisect_left
from itertools import product
def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
#文字列を一文字ずつ数字に変換、'5678'を[5,6,7,8]とできる
def LSI(): return list(map(int, list(sys.stdin.readline().rstrip())))
def LSI2(N): return [list(map(int, list(sys.stdin.readline().rstrip()))) for i in range(N)]
#文字列として取得
def ST(): return sys.stdin.readline().rstrip()
def LST(): return sys.stdin.readline().rstrip().split()
def LST2(N): return [sys.stdin.readline().rstrip().split() for i in range(N)]
def FILL(i,h): return [i for j in range(h)]
def FILL2(i,h,w): return [FILL(i,w) for j in range(h)]
def FILL3(i,h,w,d): return [FILL2(i,w,d) for j in range(h)]
def FILL4(i,h,w,d,d2): return [FILL3(i,w,d,d2) for j in range(h)]
def sisha(num,digit): return Decimal(str(num)).quantize(Decimal(digit),rounding=ROUND_HALF_UP)
#'0.01'や'1E1'などで指定、整数に戻すならintをかます
MOD = 1000000007
INF = float("inf")
sys.setrecursionlimit(10**6+10)

N = I()
X = LI()
max_x = max(X)
ans = INF
for i in range(1,max_x+1):
    temp = 0
    for j in X:
        temp += (j-i)**2
    ans = min(ans,temp)

print(ans)

Submission status: Accepted

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

small = min(x)
large = max(x)

ans = 1000000
for p in range(small,large+1):
    tmp = 0
    for i in x:
        tmp += (i -p)**2
    ans = min(ans, tmp)


print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

for P in range(min(X),max(X)+1):
    tmp = 0
    for j in range(N):
        t = (X[j] - P)**2
        tmp += t
    ans.append(tmp)
print(min(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
import numpy as np
mn=min(a)
mx=max(a)

def lambda_d(ii):
  rr=0
  for jj in range(n):
    rr+=(a[jj]-ii)**2
  return rr

r=lambda_d(mn)
for ii in np.arange(mn,mx+1):
  tempr=lambda_d(ii)
  if tempr<r:
    r=tempr
print(r)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
a = round(sum(x)/n)
ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
import sys

N = int(input())
X = sorted([int(i) for i in input().split()])
ub = X[N - 1]
lb = X[0]

ans = sys.maxsize
for i in range(lb, ub + 1):
    t = 0
    for j in X:
        t += (j - i) ** 2
    if t < ans:
        ans = t

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int, input().split()))
mx = max(X)
ans = float('inf')
for i in range(mx+1):
  tmp = 0
  for x in X:
    tmp += (x-(i+1))**2
  ans = min(ans, tmp)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
inf = 100100100
ans = inf
for i in range(1, 101):
    now = 0
    for j in x:
        now += (j-i)**2
    ans = min(ans, now)
print(ans)

Submission status: Accepted

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

P = round(sum(pos)/N)

print(sum([(i - P)**2 for i in pos]))

Submission status: Accepted

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

ans = 10 ** 9

xmin = min(X)
xmax = max(X)

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

Submission status: Accepted

Solution code:
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

n = ni()
x = nl()

mn = 100100100
for p in range(101):
  cnt = 0
  for xi in x:
    cnt += (p-xi)**2
  mn = min(mn, cnt)
print(mn)

Submission status: Accepted

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

if avg-avg_int <= 0.5:
    m = avg_int
else:
    m = avg_int + 1

cnt = 0
for i in X:
    cnt += (i-m)**2

print(cnt)

Submission status: Accepted

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

min_p = min(X)
max_p = max(X)

ans = float("inf")
for p in range(min_p, max_p+1):
    hp = 0
    for i in range(N):
        hp += (X[i]-p)**2
    ans = min(ans, hp)
    
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from operator import mul
from functools import reduce
from fractions import gcd
import math
import bisect
import itertools
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
INF = float("inf")


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

    ans = INF
    x_min = min(X)
    x_max = max(X)
    for p in range(x_min, x_max + 1):
        tmp = 0
        for x in X:
            tmp += (x - p) ** 2
        ans = min(ans, tmp)
    
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import math
from collections import deque
from collections import defaultdict

def main():
    n = int(sys.stdin.readline())
    X = list(map(int,sys.stdin.readline().split()))
    ans = -1
    for i in range(1,max(X)+1):
        #print(i)
        ans_tmp = 0
        for j in X:
            ans_tmp += (j - i)**2
        #print(ans_tmp)
        if ans > ans_tmp or ans == -1:
            ans = ans_tmp
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

s = sum(x)//n
ss = s+1

ans1 = sum((i-s)**2 for i in x)
ans2 = sum((i-ss)**2 for i in x)
print(min(ans1, ans2))

Submission status: Accepted

Solution code:
import numpy as np

def resolve():
    N = int(input())
    n_array = np.array(list(map(int,input().split())))
    mea = int(np.average(n_array) + 0.5)
    ans = n_array - mea 
    print(sum(ans*ans))
resolve()

Submission status: Accepted

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

# print(max(x))
# print(min(x))

tsukare = [0 for i in range(max(x)-min(x)+1)]

# print(tsukare)
# print(len(x))

for i in range(min(x), max(x)+1):
    for k in range(len(x)):
        tsukare[i-min(x)] += (i-x[k])**2

print(min(tsukare))

Submission status: Accepted

Solution code:
from sys import stdin, setrecursionlimit


def main():
    input = stdin.buffer.readline
    n = int(input())
    x = list(map(int, input().split()))
    sum = 0
    for xi in x:
        sum += xi
    ans_1 = sum // n
    ans_2 = sum // n + 1
    score_1 = 0
    score_2 = 0
    for xi in x:
        score_1 += (xi - ans_1) ** 2
        score_2 += (xi - ans_2) ** 2
    print(min(score_1, score_2))


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
x_l = [int(x) for x in input().split()]
ans = float('INF')
for p in range(1,101):
    sumn = 0
    for j in range(n):
        sumn += (x_l[j]-p)**2
        if(ans<sumn):
            break
    
    if(ans>sumn):
        ans = sumn

print(ans)

Submission status: Accepted

Solution code:
from statistics import mean

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

check_list = range(1, 101)

life_list = []
for check in check_list:
    life = 0
    for x in X_list:
        life += (x - check)**2
    life_list.append(life)

answer = min(life_list)
print(answer)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin

ni = lambda: int(ns())
ns = lambda: stdin.readline().rstrip()
na = lambda: list(map(int, stdin.readline().split()))

# code here
N = ni()
X = na()

minc = 99999999999999999

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

    minc = min(cost, minc)

print(minc)

Submission status: Accepted

Solution code:
# ABC156C

import numpy as np
def f(n, x):
    mean =  np.mean(x)
    s1 = sum((x-np.floor(mean))**2)
    s2 = sum((x-np.ceil(mean))**2)
    print(int(min(s1, s2)))

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

Submission status: Accepted

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

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

Pmin = 1000000
for i in range(Xmin, Xmax + 1):
    p = 0
    for x in X:
        p += (x - i) ** 2
        
    Pmin = min(Pmin, p)

print(Pmin)

Submission status: Accepted

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

ans = 1000000
for i in range(1,101):
    e = 0
    for j in x:
        e += (i-j)**2
    if ans > e:
        ans = e

print(ans)

Submission status: Accepted

Solution code:
import math

n = int(input())
x = list(map(int, input().split()))
p1 = math.floor(sum(x) / n)
p2 = math.ceil(sum(x) / n)

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

Submission status: Accepted

Solution code:
import math

n=int(input())
x=list(map(int,input().split()))
a=(sum(x)/n)
b1=math.ceil(a)
b2=math.floor(a)

s1=0
s2=0
for i in range(n):
    k1=(x[i]-b1)**2
    k2=(x[i]-b2)**2
    s1+=k1
    s2+=k2
    
if s1<s2:
    print(s1)
else:
    print(s2)

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(0,N):
     t += x[i]/N

s = round(t)
for i in range(0,N):
    min += (x[i] - s) **2

print(min)

Submission status: Accepted

Solution code:
N = int(input())
X = [int(i) for i in input().split()]
P = sum(X)/N
if P - int(P) <= 0.5:
    P = int(P)
else:
    P = int(P)+1
print(sum([(x-P)**2 for x in X]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
x=list(map(int,input().split()))
mini=999999999
for i in range(100):
    t=0
    for j in range(n):
        t+=pow(x[j]-1-i,2)
        #print(t)
    mini=min(t,mini)
    #print(mini)
    
print(mini)

Submission status: Accepted

Solution code:
import sys
from io import StringIO
import unittest
from decimal import Decimal, ROUND_HALF_UP


def resolve():
    N = int(input())
    X = [int(i) for i in input().split()]
    p = int(Decimal(str(sum(X) / N))
            .quantize(Decimal("0"), rounding=ROUND_HALF_UP))
    sumXP = 0
    for xi in X:
        sumXP += (xi - p)**2
    print(sumXP)


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """2
1 4"""
        output = """5"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """7
14 14 2 13 56 2 37"""
        output = """2354"""
        self.assertIO(input, output)


if __name__ == "__main__":
    # unittest.main()
    resolve()

Submission status: Accepted

Solution code:
# ABC 156 C

N = int(input())
X = [int(j) for j in input().split()]
X = sorted(X)
m = N * (100**2)
for i in range(min(X),max(X)+1):
    tmp = sum([(x-i) ** 2 for x in X])
    if tmp < m:
        m = tmp
print(m)

Submission status: Accepted

Solution code:
#C
n = int(input())
x = list(map(int, input().split()))
s = sum(x)
s = s / n
if s * 10 % 10 >= 5:
    s = int(s) + 1
else:
    s = int(s)

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

Submission status: Accepted

Solution code:

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

zahyo1=sum(x)//n
zahyo2=sum(x)//n+1

tairyoku1=0
tairyoku2=0
for i in range(n):
    tairyoku1+=(x[i]-zahyo1)**2
    tairyoku2+=(x[i]-zahyo2)**2

print(min(tairyoku1,tairyoku2))

Submission status: Accepted

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

for i in range(101):
  for j in X:
    total+=(i-j)**2
  else:
    ans.append(total)
    total=0

print(min(ans))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
chushin = sum(A)//N
chushin_1 = sum(A)//N +1
answer = 0
answer_1 = 0
for a in A:
    answer += abs(a-chushin) ** 2
    answer_1 += abs(a-chushin_1) ** 2
print(min(answer,answer_1))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
answer = 10 ** 9
for i in range(1,101):
    tmp = 0
    for a in A:
        tmp += abs(a-i) ** 2
    answer = min(answer,tmp)
print(answer)

Submission status: Accepted

Solution code:
import itertools
import math
import fractions
import functools
n = int(input())
k = list(map(int, input().split()))

minimum = 1000
maximum = 0

for i in range(n):
    minimum = min(minimum,k[i])
    maximum = max(maximum,k[i])

minimum_ans = 10**9
for i in range(minimum, maximum + 1):
    sum = 0
    for j in range(n):
        sum += (k[j] - i)**2
    minimum_ans = min(minimum_ans,sum)

print(minimum_ans)

Submission status: Accepted

Solution code:
#恐らく平均値に最も近い整数が答えになるかと思われる
n = int(input())
x = list(map(int,input().split()))
Ave = sum(x) / n
point = 0


for i in range(n):
    point += (x[i]-round(Ave))**2 
print(point)

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int,input().split()))
S = sum(x)
Ave = sum(x) // n
m = 10**6
score = 0

for p in range(Ave,Ave+1001): 
    for i in range(n):
        score += (x[i]-p)**2
    if score < m:
        m = score
        score = 0
print(m)

Submission status: Accepted

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

average = sum(x) / n
p = round(average)

exhaust = [(xi - p)**2 for xi in x]
print(sum(exhaust))

Submission status: Accepted

Solution code:
import collections
n,cc = int(raw_input()), collections.Counter(map(int, raw_input().split(' ')))
print min([sum([cc[k]*(k-p)*(k-p) for k in cc]) for p in range(1,100+1)])

Submission status: Accepted

Solution code:
n = int(input())
xs = list(map(int, input().split()))
m = min(xs)
M = max(xs)

ans = float('inf')
for i in range(m, M+1):
  result = 0
  for x in xs:
    result += (x-i) ** 2
  ans = min(ans, result)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = [0]*100
for i in range(100):
  ans = 0
  for j in range(N):
    a = A[j]
    ans += (i-a)**2
  B[i] = ans
print(min(B))

Submission status: Accepted

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


def main():
    N = int(input())
    X = list(map(int, input().split()))
    assert N == len(X)
    P = round(sum(X)/len(X))
    print(sum([(i-P)**2 for i in X]))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys, bisect, math, itertools, heapq, collections
from operator import itemgetter
# a.sort(key=itemgetter(i)) # i番目要素でsort
from functools import lru_cache
# @lru_cache(maxsize=None)
sys.setrecursionlimit(10**8)
input = sys.stdin.readline
INF = float('inf')
mod = 10**9 + 7
eps = 10**-7


def inp():
    '''
    一つの整数
    '''
    return int(input())


def inpl():
    '''
    一行に複数の整数
    '''
    return list(map(int, input().split()))


def comb(n, r):
    if n - r < r:
        r = n - r
    res = 1
    for i in range(r):
        res *= (n - i) % mod
        res %= mod
        res *= pow(r - i, mod - 2, mod)
        res %= mod
    return res


n = inp()
x = inpl()
left = min(x)
right = max(x)
ans = INF
for i in range(left, right + 1):
    tmp = sum([(j - i) * (j - i) for j in x])
    ans = min(ans, tmp)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
X = list(map(int, input().split()))
s = [i ** 2 for i in range(100)]
ans = 1001001
for i in range(1, 101):
    cost = sum([s[abs(x - i)] for x in X])
    ans = min(ans, cost)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
X = list(map(int, input().split()))
r = float('inf')
for p in range(1, 101):
    c = sum([(x - p) ** 2 for x in X])
    if c < r:
        r = c
print(r)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

for i in range(1,100+1):
    cost = 0
    for j in x:
        cost +=(i-j)**a 
        
    ans.append(cost)

print(min(ans))

Submission status: Accepted

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

sum = 0
for x in X:
    sum += x
avg = round(sum / n)

ans = 0
for x in X:
    ans += pow((x-avg), 2)
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(100000000)




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

    sumx = sum(x_ls)
    sumx2 = sum([x**2 for x in x_ls])

    res = 10000000
    for p in range(1, 101):
        z = n* (p**2) -2*sumx*p + sumx2
        if z < res:
            res = z

    print(res)


main()

Submission status: Accepted

Solution code:
N = int(input())
X = [int(hoge) for hoge in input().split()]
curmin = 100000000000000
for x in range(1,101):
    score = sum([(p-x)**2 for p in X])
    curmin = min(score,curmin)
print(curmin)

Submission status: Accepted

Solution code:
import math
import queue
from collections import defaultdict
 
def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()
def factorization(n):
	res = []
	if n%2==0:
		res.append(2)
	for i in range(3,math.floor(n//2)+1,2):
		if n%i==0:
			c = 0
			for j in res:
				if i%j==0:
					c=1
			if c==0:
				res.append(i)
	return res
def fact2(n):
	p = factorization(n)
	res = []
	for i in p:
		c=0
		z=n
		while 1:
			if z%i==0:
				c+=1
				z/=i
			else:
				break
		res.append([i,c])
	return res
def fact(n):#階乗
	ans = 1
	m=n
	for _i in range(n-1):
		ans*=m
		m-=1
	return ans
def comb(n,r):#コンビネーション
	if n<r:
		return 0
	l = min(r,n-r)
	m=n
	u=1
	for _i in range(l):
		u*=m
		m-=1
	return u//fact(l)
def printQueue(q):
	r=qb
	ans=[0]*r.qsize()
	for i in range(r.qsize()-1,-1,-1):
		ans[i] = r.get()
	print(ans)
def dq():
	return queue.deque()
class UnionFind():
	def __init__(self, n):
		self.n = n
		self.parents = [-1]*n

	def find(self, x): # root
		if self.parents[x]<0:
			return x
		else:
			self.parents[x] = self.find(self.parents[x])
			return self.parents[x]

	def union(self,x,y):
		x = self.find(x)
		y = self.find(y)

		if x==y:
			return

		if self.parents[x]>self.parents[y]:
			x,y = y,x

		self.parents[x]+=self.parents[y]
		self.parents[y]=x

	def size(self,x):
		return -1*self.parents[self.find(x)]

	def same(self,x,y):
		return self.find(x)==self.find(y)

	def members(self,x): # much time
		root = self.find(x)
		return [i for i in range(self.n) if self.find(i)==root]

	def roots(self):
		return [i for i,x in enumerate(self.parents) if x<0]

	def group_count(self):
		return len(self.roots())

	def all_group_members(self):
		return {r: self.members(r) for r in self.roots()} # 1~n
def bitArr(n):#ビット全探索
	x = 1
	zero = "0"*n
	ans = []
	ans.append([0]*n)
	for i in range(2**n-1):
		ans.append(list(map(lambda x:int(x),list((zero+bin(x)[2:])[-1*n:]))))
		x+=1
	return ans;
def arrsSum(a1,a2):
	for i in range(len(a1)):
		a1[i]+=a2[i]
	return a1

n = readInt()
x = readInts()
x.sort()
ans = 100000000

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

print(ans)