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

cost_min = 10**20

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

print(cost_min)

Submission status: Accepted

Solution code:
import math

if __name__ == "__main__":
    n = input()
    X = [int(i) for i in input().split(" ")]
    X.sort()
    
    left = X[0]
    right = X[-1]
    right_p_before = 0
    left_p_before  = 0
    while True:
        left_p  = 0
        right_p = 0
        for i in X:
            left_p  += (i - left )**2
        for i in X:
            right_p += (i - right)**2
        
        if right_p == right_p_before and left_p == left_p_before:
            if right_p > left_p:
                print(left_p_before)
            else:
                print(right_p_before)
            break
        else:
            right_p_before = right_p
            left_p_before  = left_p
            if left_p < right_p:
                right = math.floor(right - (right-left)/2)
            else:
                left  = math.floor(left  + (right-left)/2)

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int,input().rstrip().split(" ")))
ans = 0
ansb = 0
for i in range(100):
  ansb = 0
  for i2 in range(N):
    ansb += (X[i2] - i)**2
  if i == 0 or ans > ansb:
    ans = ansb
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
xl=list(map(int,input().split()))
xl.sort()
ans=[]
for p in range(xl[-1]-xl[0]+1):
    sum1=0
    for xi in xl:
        sum1+=(xi-xl[0]-p)**2
    ans.append(sum1)
print(min(ans))

Submission status: Accepted

Solution code:
import math

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

num = math.ceil(sum(x)/len(x))
num2 = int(sum(x)/len(x))
res = 0
res2 = 0

for i in x:
    res += (i - num)**2

for i in x:
    res2 += (i - num2)**2

if res < res2:
    print(res)
else:
    print(res2)

Submission status: Accepted

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

  ANS = 1000000000000000000000000000000000

  for i in range(1,101):
    C_ANS = 0
    for j in X:
      C_ANS += (i-j)**2
    if C_ANS < ANS:
      ANS = C_ANS
  
  print(ANS)

if __name__ == '__main__':
  main()

Submission status: Accepted

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

answer = (max(x)-min(x))**2 * n
for p in range(min(x), max(x)+1):
    count = 0
    for i in x:
        count += (i-p)**2
    answer = min(answer,count)
print(answer)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#C

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

y = [i ** 2 for i in x]

if sum(x) % n == 0:
    
    print(sum(y)-((sum(x) ** 2) // n))
else:
    t = sum(x) // n
    #a = n * p ** 2 - 2 * p * sum(x) + sum(y)
    print(min(n * (t ** 2) - 2 * t * sum(x) + sum(y),n * ((t + 1) ** 2) - 2 * (t + 1) * sum(x) + sum(y)))

Submission status: Accepted

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

minx = min(a)
maxx = max(a)
ans = 2 * 10 ** 18

for i in range(minx,maxx+1):
  judge = 0
  for j in a:
    temp = (j - i) ** 2
    judge += temp
  if judge < ans:
    ans = judge

print(ans)

Submission status: Accepted

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

P = round(sum(X_list)/N)

l = 0
for i in X_list:
    l = l + (i - P)**2

print(l)

Submission status: Accepted

Solution code:
from functools import lru_cache
def resolve():
    n = int(input())
    x = list(map(int, input().split()))

    x.sort()

    l = 0
    r = max(x)
    
    @lru_cache()
    def cost(p):
        s = 0
        for i in x:
            s += (i - p)**2
        return s

    while l + 1 < r:
        m = (l + r) // 2 
        l_s = cost(l)
        r_s = cost(r)
        if l_s > r_s:
            l = m
        else:
            r = m
    print(min(cost(l), cost(r)))


if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
n = int(input())
x_list = list(map(int, input().split()))
ans = (100 ** 2) * 100
for p in range(1, 101):
    y = sum([(x - p) ** 2 for x in x_list])
    ans = min(ans, y)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int, input().split()))
X.sort()
r = None
for i in range(X[0], X[-1]+1):
    c = sum((m - i)**2 for m in X)
    if r is None or r > c:
        r = c

print(r)

Submission status: Accepted

Solution code:
N = int(input())
List = list(map(int,input().split()))
cost = 10**36
for i in range(101):
  temp = 0
  for X in List:
    temp += (X - i)**2
  if temp <= cost:
    cost = temp
print(cost)

Submission status: Accepted

Solution code:
inp = int(input())
s = 0
l = input().split(" ")
for i in l:
  s += int(i)

# print(s)
a = 0

# print(s/inp)
m =int(round(s/inp, 0))

for i in l:
  a += (m - int(i))**2
print(a)

Submission status: Accepted

Solution code:
n=int(input())
x=list(map(int,input().split()))
mn=999999999999999
for i in range(100):
    ans=0
    for j in range(n):
        ans+=(x[j]-(i+1))**2
    if mn>ans:
        mn=ans
print(mn)

Submission status: Accepted

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

X = np.zeros((100,n))
Y = np.zeros((100,n))
for i in range(100):
    X[i,:] = x
    Y[i,:] = np.full(n,i+1)
#print(X)
#print(Y)

Z = X - Y
#print(Z)
Z = np.power(Z,2)
W = np.sum(Z,axis=1)
print(int(np.min(W)))

Submission status: Accepted

Solution code:
def calc(s,t):
       return (t-s)**2

n=int(input())
X=sorted(list(map(int,input().split())))
inf=float("inf")

ans=inf
for i in range(X[0],X[-1]+1):
       u=0
       for j in X:
              u+=calc(i,j)
       ans=min(ans,u)
print(ans)

Submission status: Accepted

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

average = round(sum(X)/N)
min_s = 0
for i in range(len(X)):
    min_s += (X[i] - average) * (X[i] - average)
print(min_s)

Submission status: Accepted

Solution code:
# coding:utf-8

import sys
import math
import time
#import numpy as np
import collections
from collections import deque
import queue
import copy


#X = str(input()).split()
#a = [int(x) for x in input().split()]


N = int(input())
X = [int(x) for x in input().split()]
ans = 9999999999
su = 0

X.sort()

xm = max(X)

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


print(ans)

Submission status: Accepted

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

M = max(X)
m = min(X)
total = [0] * (M - m + 1)
for i in range(m, M + 1, 1):
    for j in range(N):
        total[i - m] += (X[j] - i) ** 2

print(min(total))

Submission status: Accepted

Solution code:
import math

N = int(raw_input())
X = map(int, raw_input().split())

sx = 0
sx2 = 0
for i in range(N):
    sx += X[i]
    sx2 += X[i]*X[i]

#print sx, sx2, sx*sx-N*sx2

p = int(sx/N)

print min(N*p*p-2*p*sx+sx2,  N*(p+1)*(p+1)-2*(p+1)*sx+sx2)

Submission status: Accepted

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

ans = 10 * 10 ** 6
for i in range(101):
    t = 0
    for j in x:
        t += (j - i) ** 2
    
    ans = min(ans, t)

print(ans)

Submission status: Accepted

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

res = 100000000
for p in range(1, 101):
    tmp = 0
    for i in x:
        tmp += (i-p)**2
    if tmp < res:
        res = tmp

print(res)

Submission status: Accepted

Solution code:
n=int(input())
x=list(map(int,input().split()))
ans=10**100
for i in range(101):
  k=0
  for a in x:
    k+=(a-i)**2
  if k<ans:
    ans=k
print(ans)

Submission status: Accepted

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

z = sum(x)//n
z2 = z + 1
ans1 = 0
ans2 = 0
for i in range(n):
	ans1 += (x[i] - z)**2
for i in range(n):
	ans2 += (x[i] - z2)**2
	
print(min(ans1,ans2))

Submission status: Accepted

Solution code:
def func(n, x, i):
    sum = 0
    for j in range(n):
        sum = sum + (x[j] - i) * (x[j] - i)
    return sum


n = int(input())
x = list(map(int, input().split()))
minnum = 9999999
for i in range(min(x), max(x) + 1):
    f = func(n, x, i)
    if f < minnum:
        minnum = f
print(minnum)

Submission status: Accepted

Solution code:
N,*X=map(int,open(0).read().split())
X.sort()
_min=10**6
for i in range(1,X[-1]+1):
    _sum=0
    for x in X:
        _sum += (x-i)**2
    if _min > _sum:
        _min = _sum
print(_min)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

temp = []
data.sort()
for i in range(data[0],data[-1]+1):
    temp.append(sum([(x-i)**2 for x in data]))

print(min(temp))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# vim:fenc=utf-8 ff=unix


def dist(X, P):
    d = 0
    for x in X:
        d += (x-P)*(x-P)
    return d


def solv(N, X):
    ans = 100*100*100 + 1
    for P in range(1, 101):
        d = dist(X, P)
        if ans > d:
            ans = d
    return ans


if __name__ == "__main__":
    N = int(input())
    X = list(sorted(map(int, input().split())))
    ans = solv(N, X)
    print(ans)

Submission status: Accepted

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

avg = int(sum(X) / N)
nvA = 0
nvB = 0
for x in X:
    nvA += (avg - x) * (avg - x)
    nvB += (avg + 1 - x) * (avg + 1 - x)
if nvA > nvB:
    print(nvB)
else:
    print(nvA)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int, input().split()))
res=float("inf")
for i in range(max(A)+1):
    res1=0
    for j in range(len(A)):
        res1=res1+(A[j]-i)**2
    res=min(res,res1)
print(res)

Submission status: Accepted

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

#s = float('Inf')
s = float('Inf')
for i in range(min(x), max(x) + 1):
    score = 0
    for j in x:
        score += (j - i)**2 
    s = min(s, score)

print(s)

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
# from fractions import gcd
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from functools import reduce
from bisect import bisect_left, insort_left
from heapq import heapify, heappush, heappop

INPUT = lambda: sys.stdin.readline().rstrip()
INT = lambda: int(INPUT())
MAP = lambda: map(int, INPUT().split())
S_MAP = lambda: map(str, INPUT().split())
LIST = lambda: list(map(int, INPUT().split()))
S_LIST = lambda: list(map(str, INPUT().split()))

sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    N = INT()
    X = LIST()

    a = N
    b = (-2) * sum(X)
    c = 0
    for x in X:
        c += x**2

    l = int((-1)*b / (2*a))
    r = ceil((-1)*b / (2*a))
    print(min(a * l**2 + b * l + c, a * r**2 + b * r + c))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int, input().split()))
a = 0
b = 0
l = []
for j in range(1,101):
    for i in X:
    
        a = abs((i-j)**2)
        b = b + a
    l.append(b)
    b = 0

print(min(l))

Submission status: Accepted

Solution code:
n = int(input())
x = [int(x) for x in input().split()]
check = 0
res = 0
for i in x:
    check += i
check2 = round(check / len(x))
for num in x:
    res += (num-check2)**2
print(res)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import math
import fractions

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors

def ValueToBits(x,digit):
    res = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        res[i]=now%2
        now = now >> 1
    return res

def BitsToValue(arr):
    n = len(arr)
    ans = 0
    for i in range(n):
        ans+= arr[i] * 2**i
    return ans

def ValueToArray10(x, digit):
    ans = [0 for i in range(digit)]
    now = x
    for i in range(digit):
        ans[digit-i-1] = now%10
        now = now //10
    return ans

  
'''
p = 10 ** 9 + 7
N = 10 ** 6 + 2
fact = [1, 1]  # fact[n] = (n! mod p)
factinv = [1, 1]  # factinv[n] = ((n!)^(-1) mod p)
inv = [0, 1]  # factinv 計算用

for i in range(2, N + 1):
    fact.append((fact[-1] * i) % p)
    inv.append((-inv[p % i] * (p // i)) % p)
    factinv.append((factinv[-1] * inv[-1]) % p)
    
def cmb(n, r, p):
    if (r < 0) or (n < r):
        return 0
    r = min(r, n - r)
    return fact[n] * factinv[r] * factinv[n-r] % p
'''


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

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

#hpList = [[] for i in range(n)]

ans = 10**10

for i in range(101):
    now = 0
    for j in range(n):
        now += (a[j]-i)*(a[j]-i)
    if(ans>now):
        ans = now
print(ans)

Submission status: Accepted

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

min = min(x_list)
max = max(x_list)

sum_min = 1000000000000000000
for target_i in range(1, 101):
    sum_tmp = 0
    for i in range(n):
        sum_tmp += (target_i - x_list[i]) ** 2
    if sum_min >= sum_tmp:
        sum_min = sum_tmp

print(sum_min)

Submission status: Accepted

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

import numpy as np

X_array = np.array(X_list)

print(int(np.sum((X_array - round(np.mean(X_array))) ** 2)))

Submission status: Accepted

Solution code:
N=int(input())
X=list(map(int,input().split(' ')))
M=0
T=[]
H=[]
for j in range(1,200):
    for i in range(0,N):
        M=M+(X[i]-j)**2
    T.append(M)
    M=0
print(min(T))

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
x = input().split()
x = np.array(x,dtype='int')
m = x.sum()
v = (x**2).sum()
pm = int(np.round(m/n))
print(n * pm**2 - 2*pm*m + v)

Submission status: Accepted

Solution code:
N = int(input())
S = list(map(int, input().split()))
ans = int(sum(S) /N)
def HP(x):
    A = 0
    for i in S:
        A += (i - x) **2
    return A

mi = False
UP = False
DO = False
while(mi == False):
    if HP(ans) > HP(ans+1):
        ans = ans+1
        UP = False
        DO = False
    elif HP(ans) > HP(ans-1):
        ans = ans -1
        UP = False
        DO = False
    elif HP(ans) == HP(ans+1):
        if DO :
            break
        else:
            UP = True
            ans = ans +1
    elif HP(ans) == HP(ans-1):
        if UP:
            break
        else:
            Do = True
            ans = ans -1  
    else:
        mi = True

print(HP(ans))

Submission status: Accepted

Solution code:
N=int(input())
X = list(map(int,input().split()))
sums = 0
sumsum = 0
sumsumA = 0
P = 0

X.sort()

for i in X:
    sums += i
P = sums // len(X)

for i in X:
    sumsum += (i - P)**2

for i in X:
    sumsumA += (i - (P+1))**2
print(sumsum if sumsum < sumsumA else sumsumA)

Submission status: Accepted

Solution code:
from sys import stdin

def main():

    input = stdin.readline

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

    avg_min = int(sum(X) / len(X))
    avg_max = avg_min+1

    ans_min = [ (x - avg_min) ** 2 for x in X]
    ans_max = [ (x - avg_max) ** 2 for x in X]

    print(min(sum(ans_min),sum(ans_max)))
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())


min_ = 10000000
sum_ = 0

X = input().split()

for t in range(1,101):
  for i in range(N):
    tmp = int(X[i])
    sum_ += (tmp-t)**2
    
  if min_ > sum_:
    min_ = sum_
    
  sum_ = 0  
      
print(min_)

Submission status: Accepted

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

x.sort()

for p in range(x[0],x[len(x)-1]+1):
    psum=0
    for i in range(len(x)):
        psum=psum+(x[0]-x[i])**2
#    print(psum)
    pmin=psum    
    for j in range(x[0]+1,x[len(x)-1]):
        psum=0
        for i in range(len(x)):
            psum=psum+(j-x[i])**2
#        print(psum)
        if pmin>psum:
            pmin=psum
print(pmin)

Submission status: Accepted

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

wa = 0
wawa = 0
ava = int(sum(x) / n)
aa = ava+1
for i in x:
    wa += (i-ava)**2
    wawa += (i - aa)**2

if wa > wawa:
    print(wawa)
else:
    print(wa)

Submission status: Accepted

Solution code:
N=int(input())
n=list(map(float, input().split()))
ave=sum(n)/len(n)
aver=round(ave)
d=[(i-aver)**2 for i in n]
a=sum(d)
print(int(a))

Submission status: Accepted

Solution code:
import math
n = int(input())
x = list(map(int,input().split()))
k = sum(x)/n
h = 0
if (math.ceil(k)-k)**2 <= (math.ceil(k)-k-1)**2:
    h = math.ceil(k)
else:
    h = math.ceil(k)-1
def m(a,h):
    return((a-h)**2)

y = []
for i in range(n):
    y.append((x[i]-h)**2)

print(sum(y))

Submission status: Accepted

Solution code:
import sys
import socket

hostname = socket.gethostname()

if hostname == 'F451C':
    sys.stdin = open('c1.in')


def read_int_list():
    return list(map(int, input().split()))


def read_str_list():
    return input().split()


def read_int():
    return int(input())


def read_str():
    return input()


def main():
    N = read_int()
    X = read_int_list()
    # p = sum(X) // N
    p = round(sum(X) / N)
    res = 0
    for i in range(N):
      res += (X[i] - p) ** 2
    print(res)


main()

Submission status: Accepted

Solution code:
N = int(input())

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

results = []
for i in range(min(numbers), max(numbers)+1):
    cnt = 0
    for n in numbers:
        cnt += abs(n - i) ** 2
    results.append(cnt)

print(min(results))

Submission status: Accepted

Solution code:
input = raw_input
n = int(input())

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

x_sum = sum(x_list)
x_ave = float(x_sum) / n

import math
m1 = int(math.ceil(x_ave))
m2 = int(math.floor(x_ave))
out1 = sum([(x-m1)**2 for x in x_list])
out2 = sum([(x-m2)**2 for x in x_list])
out = min(out1, out2)
print(out)

Submission status: Accepted

Solution code:
N = int(input())
X = sorted(list(map(int, input().split())))
p = [[] for _ in range(max(X))]
for i in range(max(X)):
    for j in X:
        p[i].append((j - (i+1)) ** 2)
sums = [sum(pp) for pp in p]
print(min(sums))

Submission status: Accepted

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

p_max = max(X)
p_min = min(X)
ans = 0
ans_tmp = 0

for i in range(p_min, p_max):
    for j in range(len(X)):
        ans_tmp += (X[j] - i)**2
    if i == p_min:
        ans = ans_tmp
    elif ans_tmp < ans:
        ans = ans_tmp
    ans_tmp = 0

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
X = list(map(int, input().split()))
a = sum(X)
b = sum(X[i]**2 for i in range(n))
m = np.floor(a/n)
if m + 1/2 <= a/n:
  m += 1
s = n*(m**2) - 2*a*m + b
print(int(s))

Submission status: Accepted

Solution code:
import math

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

g = sum(X)/N

g1 = math.floor(g)
g2 = g1 + 1

ans1 = 0
ans2 = 0

for i in range(N):
    ans1 += (X[i] - g1)**2
    ans2 += (X[i] - g2)**2

print(min(ans1, ans2))

Submission status: Accepted

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

x = sorted(x)

sum = 0
for i in x:
    sum += i
p1 = sum // len(x)
p2 = p1+1

ans1 = 0
ans2 = 0
for num in x:
    ans1 += (num-p1)**2
    ans2 += (num-p2)**2
ans = min(ans1,ans2)

print(ans)

Submission status: Accepted

Solution code:
def main(n,x):
    res = 2**31-1
    for i in range(1,101):
        val = 0
        for j in range(len(x)):
            val += (x[j] - i)**2
        res = min(res, val)
    return res

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
 
import math
import itertools
import sys
import copy
#import numpy as np

# 入力
#A, B, C, D = map(int, input().split())
#L = list(map(int, input().split()))
#S = list(str(input()))
#N = int(input())
#S = str(input())
N = int(input())
X = list(map(int, input().split()))

X.sort()

minN = sys.maxsize
for i in range(100) :
  sum = 0
  for j in range(len(X)) :
    sum += (i-X[j]) ** 2
  minN = min(minN, sum)

print (minN)

Submission status: Accepted

Solution code:
n = int(input())
x = sorted(map(int,input().split()))
p = sum(x)//len(x)
m = float('inf')
for i in [-1,0,1]:
    a = sum(list(map(lambda x: (x-(p+i))**2, x)))
    m = min(m,a)
print(m)

Submission status: Accepted

Solution code:
n=int(input())
x=list(map(int,input().split()))
result=0
for p in range(0,max(x)+1):
  s=0
  for i in range(0,n):
    s+=(x[i]-p)**2
  if p==0:
    result=s
  else:
    if s<result:
      result=s
print(result)

Submission status: Accepted

Solution code:
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
import itertools
from functools import *
from itertools import permutations,combinations,groupby
import sys
import bisect
import string
import math
import time
import random
def Golf():
    *a,=map(int,open(0))
def S_():
    return input()
def IS():
    return input().split()
def LS():
    return [i for i in input().split()]
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def NI(n):
    return [int(input()) for i in range(n)]
def NI_(n):
    return [int(input())-1 for i in range(n)]
def StoI():
    return [ord(i)-97 for i in input()]
def ItoS(nn):
    return chr(nn+97)
def LtoS(ls):
    return ''.join([chr(i+97) for i in ls])
def GI(V,E,Directed=False,index=0):
    org_inp=[]
    g=[[] for i in range(n)]
    for i in range(E):
        inp=LI()
        org_inp.append(inp)
        if index==0:
            inp[0]-=1
            inp[1]-=1
        if len(inp)==2:
            a,b=inp
            g[a].append(b)
            if not Directed:
                g[b].append(a)
        elif len(inp)==3:
            a,b,c=inp
            aa=(inp[0],inp[2])
            bb=(inp[1],inp[2])
            g[a].append(bb)
            if not Directed:
                g[b].append(aa)
    return g,org_inp
def GGI(h,w,search=None,replacement_of_found='.',mp_def={'#':1,'.':0}):
#h,w,g,sg=GGI(h,w,search=['S','G'],replacement_of_found='.',mp_def={'#':1,'.':0}) # sample usage
    mp=[1]*(w+2)
    found={}
    for i in range(h):
        s=input()
        for char in search:
            if char in s:
                found[char]=((i+1)*(w+2)+s.index(char)+1)
                mp_def[char]=mp_def[replacement_of_found]
        mp+=[1]+[mp_def[j] for j in s]+[1]
    mp+=[1]*(w+2)
    return h+2,w+2,mp,found
def bit_combination(k,n=2):
    rt=[]
    for tb in range(n**k):
        s=[tb//(n**bt)%n for bt in range(k)]
        rt+=[s]
    return rt
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['YES','NO']
Yn=['Yes','No']

mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase
u_alp=string.ascii_uppercase
#ts=time.time()
#sys.setrecursionlimit(10**7)
input=lambda: sys.stdin.readline().rstrip()
 
def ran_input():
    import random
    n=random.randint(4,16)
    rmin,rmax=1,10
    a=[random.randint(rmin,rmax) for _ in range(n)]
    return n,a

class Comb:
    def __init__(self,n,mo=10**9+7):
        self.fac=[0]*(n+1)
        self.inv=[1]*(n+1)
        self.fac[0]=1
        self.fact(n)
        for i in range(1,n+1):
            self.fac[i]=i*self.fac[i-1]%mo
            self.inv[n]*=i
            self.inv[n]%=mo
        self.inv[n]=pow(self.inv[n],mo-2,mo)
        for i in range(1,n):
            self.inv[n-i]=self.inv[n-i+1]*(n-i+1)%mo
        return
    
    def fact(self,n):
        return self.fac[n]
        
    def invf(self,n):
        return self.inv[n]

    def comb(self,x,y):
        if y<0 or y>x:
            return 0
        return self.fac[x]*self.inv[x-y]*self.inv[y]%mo


show_flg=False
show_flg=True

ans=0

n=I()
x=LI()
s,m=0,0
for i in x:
    s+=i**2
    m+=i

#z=int(s-m**2/n)
z=int(m/n)
def en(z):
    return s-2*m*z+n*z**2

ans=min(en(z-1),en(z),en(z+1))
print(ans)

Submission status: Accepted

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

import sys


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

    p_results = []
    for p in range(0, 100):
        p_sum = 0
        for _, x in enumerate(X):
            p_sum += (x - p) ** 2
        p_results.append(p_sum)
    print(min(p_results))


if __name__ == "__main__":
    main()

Submission status: Accepted

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

ave = sum(X)/len(X)
ave_N = sum(X)//len(X)
if( (ave - ave_N) >= (ave_N + 1 - ave)):
  ave = ave_N + 1
else:
  ave = ave_N

total = 0
for i in range(len(X)):
  total += (X[i]-ave)*(X[i]-ave)
print(total)

Submission status: Accepted

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

def cost(p):
    res = 0
    for x in xl:
        res += (x-p)**2
    return res

res = 10**9
for p in range(xl[0],xl[-1]+1):
    res = min(res, cost(p))

print(res)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
import numpy as np

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

n = ni()
x = na()
inf = 10**10
cnt = [0] * 100
for xi in x:
    for p in range(1, 101):
        cnt[p-1] = cnt[p-1] + (xi - p)**2
#print(cnt)
print(min(cnt))

Submission status: Accepted

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

for val in X:
  A = A + val
  
a = int(A / N + 0.5)

for val in X:
  sumA = sumA + (a - val) ** 2
  
print(sumA)

Submission status: Accepted

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

a = min(l)
b = max(l)
res = 250000

for p in range(a,b+1):
    ires = sum([(x-p)**2 for x in l])
    res = min(ires,res)
    
print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
INF = pow(10,10)
_min = INF

for p in range(min(x), max(x) + 1):
  total = 0
  for xi in x:    
    total += pow((xi-p),2)
  if total < _min:
    _min = total
print(_min)

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int,input().split()))
ans = 10000000000000000000000000000000000000
for i in range (-201,202):
  p = 0
  for j in range (0,N):
    p = p + (X[j] - i - 1) ** 2
  ans = min(ans,p)

print(ans)

Submission status: Accepted

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

p = round(sum(X) / N)

print(sum([(X[i] - p) ** 2 for i in range(N)]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
x=sorted(list(map(int,input().split())))
mi=int(x[0])
ma=int(x[n-1])
l=[]
a=0
mp=999999
for i in range(mi,ma+1):
    for j in range(n):
        a+=(x[j]-i)**2
    if a<mp:
        mp=a
        a=0
print(mp)

Submission status: Accepted

Solution code:
n=input()
N=int(n)

X=[int(_) for _ in input().split(" ")]

total=0
p=0
ans=0

for i in range(len(X)):
  total += X[i]

p=round(total/N)

for i in range(len(X)):
  ans += (X[i]-p)*(X[i]-p)


print(ans)

Submission status: Accepted

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

x.sort()
m = 1000000
for i in range(1, 101):
    tm = 0
    for xi in x:
        tm += (xi - i) ** 2
    if tm < m:
        m = tm
print(m)

Submission status: Accepted

Solution code:
#!/use/bin/env python3

from collections import defaultdict

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

s = sorted(x_n)

ans = 1e18
for p in range(s[0], s[-1] + 1):
    l = sum([pow(x - p, 2) for x in s])

    if l < ans:
        ans = l

print(ans)

Submission status: Accepted

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

min_power = 1000001

for p in range(1, 100+1):
    total = 0

    for value in X:
       total += (value - p) ** 2

    if total < min_power:
        min_power = total

print(min_power)

Submission status: Accepted

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

ave = math.ceil(sum(a) / len(a))

print(min(sum([(i-ave)**2 for i in a]),sum([(i-(ave-1))**2 for i in a])))

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int,input().split()))
pmin = min(x)
pmax = max(x)
unchi = []
for t in range(pmax-pmin+1):
	p = t+pmin
	count = 0
	for i in range(n):
		count = count + (x[i]-p)**2
	unchi.append(count)
print(min(unchi))

Submission status: Accepted

Solution code:
n=int(input())
x=[int(i) for i in input().split(" ")]
x=sorted(x)
mind=-1
for i in range(x[0],x[len(x)-1]+1):
    sum=0
    for j in x:
        sum+=(j-i)**2
    if mind>sum or mind==-1:
        mind=sum

print(mind)