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 = list(map(int, input().split()))

x_ave = round(sum(x_list) / len(x_list))
diff_sum = 0
for x in x_list:
    diff = abs(x - x_ave)**2
    diff_sum += diff
print(diff_sum)

Submission status: Accepted

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

for i in range(n):
  a += k[i]
  
a = a / n 

for i in range(n):
  b += (round(a) - k[i])**2 
  
print(b)

Submission status: Accepted

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

place = round(sum(cords)/n)

score = sum([(i-place)**2 for i in cords])

print(score)

Submission status: Accepted

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

a=0
ls=[]

for i in range(1,101):
  a=0
  for j in range(n):
    a+=(x[j]-i)**2
  ls.append(a)
  
ans=min(ls)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int,input().split()))
x.sort()
y = []
minimum=x[0]
maximum=x[n-1]
s = 0
for i in range(minimum,maximum+1):
  for j in range(0,n):
    s = s + (x[j]-i)**2
  y.append(s)
  s = 0
  
lenlen = len(y)  
for k in range(1,lenlen):
  if y[k-1]<=y[k]:
    y[k] = y[k-1]

print(y[lenlen-1])

Submission status: Accepted

Solution code:
import math
import itertools
import statistics
n = int(input())
#n, d = list(input().split())
x = list(map(int, input().split()))
x.sort()
m = max(x)
total = [0]*m


for i in range(n):
    for p in range(1, m+1):
        total[p-1]+=abs(x[i]-p)*abs(x[i]-(p))
     
            
print(min(total))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import statistics

if __name__ == "__main__":
    N = int(input())
    X = list(map(int, input().split()))
    X_cp = X.copy()
    P = round(statistics.mean(X_cp))
    ret = 0
    for i in X:
        ret += (i - P)**2
        # print((i - P)**2)
    print(ret)

Submission status: Accepted

Solution code:
from math import *

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

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

ave = int(round(sum / len(X), 0))

ans = 0
for i in range(len(X)):
    ans += int(pow(X[i] - ave, 2))

print(ans)

Submission status: Accepted

Solution code:
from sys import stdin


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


def min_cost(n, data):
    cost = 1000000
    for p in range(1, 100 + 1):
        tmp_cost = sum([calc_cost(p, x) for x in data])
        if tmp_cost < cost:
            cost = tmp_cost
    return cost


def main():
    N = [int(x) for x in stdin.readline().rstrip()]
    CORP = [int(x) for x in stdin.readline().rstrip().split()]
    cost = min_cost(N, CORP)
    print(cost)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

total = 0
p=round(sum(x)/len(x))
for ele in x:
    total += (ele-p)**2

print(total)

Submission status: Accepted

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

min = 100
max = 0
for i in range(n):
  if min > x[i]:
    min = x[i]
  if max < x[i]:
    max = x[i]

best = sys.maxsize
for i in range(min, max+1, 1):
  now = 0
  for j in range(n):
    now = now + (x[j] - i)**2
  if best > now:
    best = now
print(best)

Submission status: Accepted

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

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

if len(ans) == 0:
    print(0)
    exit()
print(min(ans))

Submission status: Accepted

Solution code:
from statistics import mean
import math
N = int(input())
X = list(map(int, input().split()))
P = round(mean(X))
ans = 0
for x in X:
    ans += (x-P)**2
print(ans)

Submission status: Accepted

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

distance_list = []
for i in range (1, 101):
    distance_list.append(sum(map(lambda x : (x - i) ** 2, Xs)))
print(min(distance_list))

Submission status: Accepted

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

result=10**18

for i in range(1,101):
    tmp=0
    for j in range(1,n+1):
        tmp+=(x[j-1]-(i))**2
    else:
        if result>tmp:
            result=tmp
            
print(result)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

def main():
    N, *X = map(int, read().split())
    s = round(sum(X)/len(X))
    res = [(x - s) ** 2 for x in X]
    print(sum(res))
    
    return

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
c = int(input())
arr = list(map(int,input().split()))
median = round(sum(arr)/c)
product = temp = 0
for i in arr:
   temp = (i - median) ** 2
   product += temp
   temp = 0
print(product)

Submission status: Accepted

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


max_x = max(x)
min_x = min(x)
sum_a = [0]*(max_x+1)


for i in range(0,max_x+1):
    for j in range(0,N):
        sum_a[i] += (x[j]-i)**2

#print(sum_a)
min_dis = min(sum_a)

check = min(sum_a)
ans = sum_a.index(check)
a = 0
for i in range(0,N):
    a += (x[i] - ans)**2

print(a)

Submission status: Accepted

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

num=sum(X)/N
num1=math.floor(num)
num2=math.ceil(num)

R1=[(X[i]-num1)**2 for i in range(N)]
R2=[(X[i]-num2)**2 for i in range(N)]
print(min(sum(R1),sum(R2)))

Submission status: Accepted

Solution code:

s = [input() for i in range(2)]

n = int(s[0])
x = [int(s) for s in s[1].split()]

mina = 0
a = max(x)
b = min(x)
for i in range(b,a+1):
    tem = 0
    for j in range(n):
        tem += (x[j]-i)*(x[j]-i)
    if(i==b or tem<mina):
        mina = tem
print(mina)

Submission status: Accepted

Solution code:
N=int(input())
X=list(map(int, input().split()))
"""
minX=min(X)
maxX=max(X)
"""
minE = 1000000000
for P in range(101):
  sum = 0
  for Xi in X:
    sum = sum + (Xi-P)**2
  if minE>sum:
    minE=sum
print(minE)

Submission status: Accepted

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

res = 10**10

for x in range(1,101):
    cnt = 0
    for e in a:
        cnt += (e-x)**2
    res = min(res,cnt)
print(res)

Submission status: Accepted

Solution code:
def main():
  from statistics import mean
  from math import floor,ceil

  n = int(input())
  x = list(map(int, input().split()))
  x_mean = mean(x) 
  if x_mean == 0.5:
      greater_than_x = len([i for i in x if i > x_mean])
      if greater_than_x * 2 >= n:
          x_mean = ceil(x_mean)
      else:
          x_mean = floor(x_mean)
  elif x_mean % 1 > 0.5:
      x_mean = ceil(x_mean)
  else:
      x_mean = floor(x_mean)
  x2 = [(num - x_mean)**2 for num in x]
  print(sum(x2))

main()

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
souwa = 0
kazu = len(x)
souwalist=[]
saidai = max(x)

for j in range(saidai):
	j += 1
	for i in range(kazu):
		tem = x[i]
		souwa = (tem - j) ** 2 + souwa
	souwalist.append(souwa)
	souwa = 0

print(min(souwalist))

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int,input().split()))
X.sort()
min = 10**100

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

Submission status: Accepted

Solution code:

n=int(input())
x=list(map(int,input().split()))
    
x=sorted(x)
m=999999999999
for j in range(x[0],x[n-1]+1):
    s=0
    for i in range(n):
        s+=(x[i]-j)**2
    m=min(m,s)
    
print(m)

Submission status: Accepted

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

print(int(np.sum((X-np.round(sum(X)/len(X)))**2)))

Submission status: Accepted

Solution code:
# coding: utf-8


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

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

    print(min(min_sums))

    return 0


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
ls=input().split(' ')
mp=[]
for i in range(int(min(ls)),int(max(ls))+1):
    hp=0
    for j in range(n):
        hp+=(i-int(ls[j]))**2
    mp.append(hp)
print(min(mp))

Submission status: Accepted

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

min = 10000*N

lst.sort()

for i in range(lst[0],lst[-1]+1):
  x = 0
  for j in lst:
    x += (j-i)*(j-i)
  if x < min:
    min = x

print(min)

Submission status: Accepted

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

    mn = float('inf')
    for i in range(1, 101):
        mn = min(mn, calc(X, i))
    print(mn)


def calc(X, P):
    retval = 0
    for x in X:
        retval += (x - P) ** 2 
    return retval
    
if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import math

def calcEnergy(goal, curr):
  return (curr - goal) * (curr - goal)

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

xs = x.copy()
xl = x.copy()

av = (int(sum(x)/n))

for i in range(n):
  xs[i] = calcEnergy(av, xs[i])
  xl[i] = calcEnergy(av + 1, xl[i])

small = sum(xs)
large = sum(xl)
if(small < large):
  print(small)
else:
  print(large)

Submission status: Accepted

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

l = min(x)
r = max(x)
ans = 100 ** 3
for i in range(l, r + 1):
    vit = sum(abs(xi - i) ** 2 for xi in x)
    ans = min(ans, vit)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
if max(x)-min(x)==0:
    print(0)
else:
    k=max(x)-min(x)
    box=[0]*k

    for i in range(0,k):
        aa=0
        for j in range(0,n):
           aa = aa+(x[j]-min(x)-i)**2
        box[i]=aa


    print(min(box))

Submission status: Accepted

Solution code:
import math
n = int(input())
lx = list(map(int, input().split()))
if sum(lx)%n == 0:
  mx = int(sum(lx)/n)
  print(sum([(x - mx)**2 for x in lx]))
else:
  mx = float(sum(lx)/n)
  mxc = sum([(x - math.ceil(mx))**2 for x in lx])
  mxf = sum([(x - math.floor(mx))**2 for x in lx])
  if mxc < mxf:
    print(int(mxc))
  else:
    print(int(mxf))

Submission status: Accepted

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

P = round(sum(X) / N)

ans = 0
for x in X :
    ans += (P - x) ** 2

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
n = int(input())
x = list(map(int, input().split()))
sum = res = 0

for i in range(n):
    sum += x[i]
    
p = sum / n
p = Decimal(str(p)).quantize(Decimal('0'), rounding=ROUND_HALF_UP)
for i in range(n):
    res += (x[i] - Decimal(p)) ** 2
    
print(int(res))

Submission status: Accepted

Solution code:
N = int(input())
X = [int(i) for i in input().split(" ")]
XP2 = [i**2 for i in X]
sum_x = sum(X)
sum_xp2 = sum(XP2)

Min = 10000000000

for p in range(1,max(X)+1):
    M = sum_xp2 - 2*p*sum_x + (p**2)*N
    if  M < Min:
        Min = M
print(Min)

Submission status: Accepted

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

for i in range(max(x)+1):
    for j in range(n):
        sum+=(x[j]-i)**2
    hp.append(sum)
    sum=0

print(min(hp))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
 
#整数input
def ii(): return int(sys.stdin.readline().rstrip()) #int(input())
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) #list(map(int,input().split()))
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]
#文字列input
def ss(): return sys.stdin.readline().rstrip() #input()
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) #list(input().split())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]

#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？

n=ii()
arr=limii()

if n==1:
    print(0)
    exit()

ans = Decimal(str(sum(arr)/n)).quantize(Decimal("0"),rounding=ROUND_HALF_UP)
cnt=0
for i in range(n):
    cnt+=(ans-arr[i])**2

print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
m = 100**5
s = 0
for item in x:
    s += item

s = round(s/n)

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

print(result)

Submission status: Accepted

Solution code:
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN

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

ave = sum(x) / len(x)
p = Decimal(str(ave)).quantize(Decimal('0'), rounding=ROUND_HALF_UP)
squared = sum([i*i for i in x])

print(n*p*p - 2*p*sum(x) + squared)

Submission status: Accepted

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

Submission status: Accepted

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

p = round(sum(xi) / n)
hp = 0
for x in xi:
    hp += (x - p) ** 2

print(hp)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
A = sorted(map(int, input().split())) 
P=round(sum(A)/len(A))
print(sum((np.array(A)-P)**2))

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int, input().split()))
X.sort()
maxP = X[-1]
#print(maxP)
total = 1000000000000
for P in range(maxP + 1):
    tmpTotal = 0
    for tmpX in X:
#        print("{} {} {}".format(tmpX, N, (N-int(tmpX))**2))
        tmpTotal = tmpTotal + (P - int(tmpX))** 2
    if tmpTotal < total:
        total = tmpTotal

if total == 1000000000000:
    total = 0
print(total)

Submission status: Accepted

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

for p in range(1, max_N+1):
    tmp = sum([(x-p)**2 for x in X])
    if tmp <= ans:
        ans = tmp
    else:
        break
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
x = tuple(map(int,input().split()))
def cost(p):
    s = 0
    for i in range(0,n):
        s = s + (x[i] - p)**2
    return s
mid = round((max(x) + min(x))/2)
if cost(mid) == cost(mid + 1):
    print(cost(mid))
elif cost(mid) > cost(mid + 1):
    para = mid + 1
    for i in range(0,max(x)):
        if cost(para) <= cost(para + 1):
            print(cost(para))
            break
        para = para + 1
else:
    para = mid
    for i in range(0,max(x)):
        if cost(para) <= cost(para - 1):
            print(cost(para))
            break
        para = para - 1

Submission status: Accepted

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

div = round(sum(x)/len(x))
ans = 0
for i in x:
  ans += (div-i)**2
print(ans)

Submission status: Accepted

Solution code:
a = input()
x = input().split()
y = 0
d = 0
a = int(a)

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

for i in range(0,a):
  y = x[i] + y

m = y / a

for i in range(0,a):
  d = (round(m) - x[i])**2 + d
  
print(d)

Submission status: Accepted

Solution code:
import math
n = int(input())
xl = list(map(int, input().split()))
xl_cnt = len(xl)
xl_sum = sum(xl)
xl_avg = xl_sum / xl_cnt
xl_diff = map(lambda x: x - int(format(xl_avg, '.0f')), xl)
xl_double = map(lambda x: x * x, xl_diff)
print(sum(xl_double))

Submission status: Accepted

Solution code:
import statistics
import math

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

ave = round(statistics.mean(x))
sum = 0
for i in range(n):
    sum += (x[i] - ave) ** 2

print(sum)

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int, input().split()))
print(min(map(lambda p: sum((x - p) ** 2 for x in X), range(min(X), max(X)+1))))

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int, input().split()))
ans = 10**8
for i in [int(sum(X)/len(X)), int(sum(X)/len(X)+0.5)]:
  ans = min(ans, sum([(j-i)*(j-i) for j in X]))
print(ans)

Submission status: Accepted

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

mid = round(sum(coordinates)/len(coordinates))

stamina = 0
for c in coordinates:
  stamina += (c - mid)**2
print(stamina)

Submission status: Accepted

Solution code:
from sys import stdin
n = int(stdin.readline().rstrip())
s = stdin.readline().rstrip().split()
x = [None]*n
x = [int(s[i]) for i in range(0, n)]

ave = 0
for i in range(0, n):
   ave += x[i]
p1 = ave//n + 1
p2 = ave//n

sum1 = 0
sum2 = 0
for i in range(0, n):
   sum1 += pow(x[i]-p1, 2)
   sum2 += pow(x[i]-p2, 2)

if sum1 < sum2:
    print(sum1)
else:
    print(sum2)

Submission status: Accepted

Solution code:
n = int(input())
li = list(map(int, input().split()))
import statistics
a = round(statistics.mean(li))

c = 0
for i in li:
    c += ((i - a) ** 2)
print(c)

Submission status: Accepted

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

m = 100000000
ans = 0

for p in range(min(x), max(x)+1):
    ans = 0

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

    if ans < m:
        m = ans


print(m)

Submission status: Accepted

Solution code:
import math


def main():
    n = int(input())
    x = list(map(int, input().split()))
    ans = 10**10
    for p in range(101):
        total = 0
        for i in range(n):
            total += (x[i]-p)**2
        ans = min(total, ans)
    print(ans)


main()

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:
import numpy as np
import math

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

mn = np.mean(lst)
avg = np.ceil(mn)
avg2 = np.floor(mn)

ans = 0
ans2 = 0
for _ in lst:
    ans2 += (_ - avg2) ** 2
    ans += (_ - avg) ** 2
print(int(min(ans, ans2)))

Submission status: Accepted

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


res = 0

p = round(sum(L)/N)

for i in L:
    res += (i-p)**2

print(res)

Submission status: Accepted

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

def wa(avg):
    max=0
    for i in range(len(x)):
        s=x[i-1]-avg
        max=max+s**2
    return max


maxj=1000000
max=wa(1)
avg=2
while maxj>max:
    maxj=max
    max=wa(avg)
    avg=avg+1
print(maxj)

Submission status: Accepted

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

d_min = 10**18
for i in range(min(x), max(x)+1):
  d = 0
  for j in range(len(x)):
    d += (i-x[j])**2
    
  if d<d_min:
    d_min = d
    
print(d_min)

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int, input().split()))
minvalue = 0
list1 = []
for i in range(1, 101):
    for j in range(len(X)):
        minvalue += (X[j]-i)**2
    list1.append(minvalue)
    minvalue = 0
minvalue1 = 10**200
for x in range(len(list1)):
    if minvalue1 > list1[x]:
        minvalue1 = list1[x]
print(minvalue1)

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
x = list(map(int, input().split())) # list()でリスト化、それを変数に代入。
#一行で入力された数値をリストに
p = 0
x2 = np.power(x,2)

xsum = sum(x)
xsum2 = sum(x2)

p = -(-xsum//n)
p2 = xsum//n

print(min(xsum2 - 2*p*xsum + n*p*p,xsum2 - 2*p2*xsum + n*p2*p2))

Submission status: Accepted

Solution code:
N=int(input())
mylist=list(map(int,input().split()))
#print(mylist)
sq_mylist=[i*i for i in mylist]
#print(sq_mylist)

coord_max=max(mylist)
sum_list=sum(mylist)
point=sum_list//N
over_point=point+1

def fun1(p):
    total=p*p*N+sum(sq_mylist)-2*p*sum_list
    return total

print(min(fun1(point),fun1(over_point)))

Submission status: Accepted

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

mi = min(x)
ma = max(x)

a = 0
for i in range(mi, ma+1):
    s = 0
    for j in x:
        s += pow(j - i, 2)
    if a == 0:
        a = s
    else:
        a = min(a, s)
print(a)

Submission status: Accepted

Solution code:
N=int(input())
X= list(map(int, input().split())) 
K=max(X)
Y=0
for j in range(N):
    Y=Y+X[j]**2
for i in range(K+1):
    S=0
    for l in range(N):
        S=S+(X[l]-i)**2
    if S<Y:
        Y=S
print(Y)

Submission status: Accepted

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

spot = round(sum(X) / len(X))
stamina = 0
for x in X:
    stamina += pow(x - spot, 2)

print(stamina)

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int, input().split()))
min_total = 0
X_ = sum(X) // len(X)

for i in range(2):
    total = 0
    for j in range(N):
        total += (X[j] - (X_ + i)) ** 2
    if i == 0 or min_total >= total:
        min_total = total
print(min_total)

Submission status: Accepted

Solution code:

N = int(input())
x_list = list(map(int, input().split()))
maxValue = max(x_list)
minValue = 1000000
for i in range(1,100):
    ans = 0
    for j in range(N):
        ans += ( x_list[j] - i ) **2

    if(ans < minValue):
        minValue = ans

print(minValue)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
X = list(map(int, input().split()))
x = np.array(X)
ans = 100000000000
for i in range (0,101):
    b = np.array([i]*N)
    c = x -b
    tem = np.square(c)
    tem_ans = np.sum(tem)
    if ans >= tem_ans:
        ans = tem_ans
print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split())) 
    a=round(sum(A)/N)
    total=0
    for i in range(N):
        total+=(A[i]-a)*(A[i]-a)
    print(total)
main()

Submission status: Accepted

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

MaxCoo = max(X)
MinCoo = min(X)
ave = (MaxCoo + MinCoo)//2
Range = MaxCoo - MinCoo + 2
MinCost = 10**12
for i in range(Range//2):
    pos = ave + i
    cost = 0
    for member in X:
        cost += (member-pos)**2
    if cost < MinCost:
        MinCost = cost

    pos = ave - i
    cost = 0
    for member in X:
        cost += (member-pos)**2
    if cost < MinCost:
        MinCost = cost
print(MinCost)

Submission status: Accepted

Solution code:
N = int(input())
inX = input().split()
X = []
a = 0
for x in inX:
	X.append(int(x))
	a += int(x)
P = round(a / N)

# 算出
sum = 0
for x in X:
	sum += (x - P) * (x - P)

print(sum)

Submission status: Accepted

Solution code:
n=int(input())
ls=input().split(' ')
mp=[]
for i in range(100):
    hp=0
    for j in range(n):
        hp+=(i-int(ls[j]))**2
    mp.append(hp)
print(min(mp))

Submission status: Accepted

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

min_X = min(X_list)
max_X = max(X_list)

min_cost = 0
for p in range(min_X, max_X + 1):

    cost = 0
    for i in range(N):
        cost += (X_list[i] - p) ** 2
    
    if cost < min_cost or min_cost == 0:
        min_cost = cost

print(min_cost)

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int, input().split()))
X = list(sorted(X))
if N == 1:
  print(0)
  exit()
ans = float('inf')
for p in range(X[0], X[N - 1] + 1):
  sum = 0
  for x in X:
    sum += (x - p) ** 2
  ans = min(sum, ans)
print(ans)

Submission status: Accepted

Solution code:
def main():
	n = int(input())
	X=list(map(int, input().split()))
	wa = 10000000000000000
	for i in range(min(X),max(X)+1):
		dmy_wa = sum(list(map(lambda x:(x-i)**2, X)))
		if dmy_wa < wa:
			wa=dmy_wa
	print(wa)
main()

Submission status: Accepted

Solution code:
import math

n = int(input().rstrip())
x = [0] * (n)

line = input().rstrip().split(' ')
sum = 0
for i in range(n):
    x[i] = int(line[i])
    sum += x[i]

ave = int(round((sum / n), 0)) 

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

print(ans)

Submission status: Accepted

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

s = 0
for x in X:
    s += (x-X[0])**2
S = s

if N == 1:
    print(S)
else:
    m = max(X)
    n = min(X)
    for P in range(n,m+1):
        s = 0
        for x in X:
            s += (x-P)**2
        if s < S:
            S = s
    print(S)

Submission status: Accepted

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

physical_min = float('inf')
for p in range(min(X), max(X)+1):
    physical_total = 0
    for x in X:
        physical_total += (x-p)**2
    if physical_total < physical_min:
        physical_min = physical_total
print(physical_min)

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int,input().split()))
X.sort()
m=10**18
for p in range(102):
    s=0
    for i in range(N):
       s+= (X[i]-p)**2
    if(m>s): m=s
print(m)

Submission status: Accepted

Solution code:
import numpy as np

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

m = int(np.median(x))
k = int(np.std(x))
ans = max(x) ** 2 * n

# pos = m + k
# print(pos)
# cost = 0

low = max(1, m - k)
high = min(m + k, np.max(x))

for i in range(low, high+1, 1):
  cost = 0
  for j in range (n):
    cost += (x[j] - i) ** 2
  # print(cost)
  if cost < ans:
    ans = cost

print(ans)

Submission status: Accepted

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

s = sum(X)
p = s // n
# print(p)
ans = 100100100100
for j in range(-5, 6):
    tmp = 0
    for i in range(n):
        tmp += (X[i] - p+j)**2
    if ans > tmp:
        ans = tmp
print(ans)

Submission status: Accepted

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

phy = []
tmp = 0
max1 = 0
count = 0

for i in range(101):
    phy.append(i*i)

for j in range(1,max(x)+1):
    for k in range(n):
        tmp += phy[abs((j-x[k]))]
    if(max1 > tmp or count == 0):
        max1 = tmp
    count += 1
    tmp = 0

print(max1)

Submission status: Accepted

Solution code:
n,*x=map(int,open(0).read().split())

import numpy as np

xar=np.array(x)

ans=float("inf")
for p in range(0,101):
    tmp=sum((xar-p)**2)
    ans=min(ans,tmp)
    
print(int(ans))

Submission status: Accepted

Solution code:
N=int(input())
X=list(map(int,input().split()))
X.sort(key=int)
P=0
out=0
for i in X:
    P+=i
P=round(P/N)

def cost(x,p):
    return (x-p)*(x-p)
for i in X:
    out+=cost(i,P)

print(out)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
import math

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

p1_sum = []
p2_sum = []

p1 = math.floor((1/n) * sum(x))
p2 = math.ceil((1/n) * sum(x))

for i in x:
    p1_sum.append((p1 - i)** 2)
    p2_sum.append((p2 - i)** 2)

print(min(sum(p1_sum),sum(p2_sum)))

Submission status: Accepted

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

mini = (100-1)**2 * n
for p in range(1, 101):
    temp = 0
    for i in x:
        temp += (i-p)**2
    mini = min(mini, temp)

print(mini)

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int,input().split()))
hp = [0]*n
ans = 10 ** 12
for i in range(1,101):
    for j in range(n):
        hp[j] = (i-x[j])**2
    if ans > sum(hp):
        ans = sum(hp)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
x = np.array(sorted(list(map(int, input().split()))),dtype = 'int64')

s = []
for p in range(x[0],x[-1]+1):
    s.append(np.sum((x-p)*(x-p)))
    
print(min(s))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
x = [int(i) for i in input().split()]

ans = 2147483647
tmp = 0
for i in range(-1000, 1001): #iは集会を開く場所
    for j in range(n):
        tmp += pow(x[j] - i, 2)
    if ans > tmp:
        ans = tmp
    tmp = 0
print(ans)

Submission status: Accepted

Solution code:
import statistics
import math

ans1 = 0
ans2 = 0
n = int(input())
m = list(map(int, input().split()))
x = math.floor(statistics.mean(m))
y = math.ceil(statistics.mean(m))
for i in range(n):
    num = (m[i]-x)**2
    ans1 = ans1 + num
for i in range(n):
    num = (m[i]-y)**2
    ans2 = ans2 + num

if ans1 > ans2:
    ans = ans2
else:
    ans = ans1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

ele = sum/N
ele1 = math.ceil(ele)
ele2 = ele1-1

#print(ele1)
#print(ele2)


ans1 = 0
ans2 = 0
for x in X:
    ans1 = ans1 + (x-ele1)**2
    ans2 = ans2 + (x-ele2)**2

print(min(ans1,ans2))

Submission status: Accepted

Solution code:
import itertools

N = input()
# n, r = map(int, input().split())
# p = list(map(int, input().split()))
x = input().split()
# S = [int(input()) for i in range(int(n))]
summ =0
ave =0

for ii in x:
    ave += int(ii)

ave = ave / int(N)
ave = round(ave)
# ave = -(-ave // int(N))  # 問題は切り捨てか切り上げか?配列中でAVEより

for i in x:
    summ+= (int(i) - ave)**2

print(summ)

Submission status: Accepted

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