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()))
hp=[]

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

Submission status: Accepted

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

ans=1000000000

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

Submission status: Accepted

Solution code:
N=int(input())
X= list(map(int,input().split()))
cnt = 1000000
for j in range(100):
    t = 0
    for i in X:
        t += (i-(j+1))**2
    if t <= cnt:
        cnt = t
print(cnt)

Submission status: Accepted

Solution code:
a=int(input())
li = list(map(int,input().split()))
#print(li)
cnt = 1000000
for j in range(99):
    t = 0
    for i in li:
        t += (i-(j+1))**2
    if t <= cnt:
        cnt = t
print(cnt)

Submission status: Accepted

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

for p in range(x_list[0],x_list[-1]+1):
    cost_tot = 0
    for x in x_list:
        cost = (x-p)**2
        cost_tot += cost
    if cost_tot < cost_tot_min:
        cost_tot_min = cost_tot

print(cost_tot_min)

Submission status: Accepted

Solution code:
def resolve():
    N = int(input())
    Xlist = [int(i) for i in input().split()]
    # P = sum(Xlist) / N
    print(sum((x - round(sum(Xlist) / N)) ** 2 for x in Xlist))


if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
N = int(input())

X_list = [int(n) for n in input().split()]
total = 0
for X in X_list:
    total += X

avg_left = total // N
avg_right = (total // N) + 1

dist_left = 0
dist_right = 0
for X in X_list:
    dist_left += (X - avg_left)**2
    dist_right += (X - avg_right)**2

print(min(dist_left,dist_right))

Submission status: Accepted

Solution code:
#N人の人がそれぞれXiに住んでいる。Pで集会開く時、(Xi-P)^2の和の最小値

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

Xpwr = list()

for i in range (1, 101):
    Xpsum = list()

    for j in range(N):
        Xpsum.append((X[j] - i) ** 2)

    Xpwr.append(sum(Xpsum))

ans = min(Xpwr)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
x_ave = sum(x) // n
def moment(val):
    list_init = [0]*n
    for i in range(n):
        list_init[i] = (x[i] - val)**2
    return(sum(list_init))

print(min(moment(x_ave), moment(x_ave + 1)))

Submission status: Accepted

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

n = int(sum(aa) / len(aa) + 0.5)
a2sum = 0
for a in aa:
    a2sum += (a-n) **2
print(a2sum)

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int, input().split()))
 
cost = 10**10
 
for i in range(1, 101):
  cand = sum([(x-i)**2 for x in X])
  cost = min(cost, cand)

print(cost)

Submission status: Accepted

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

Submission status: Accepted

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

a = int(sum(x)//len(x))
b = a + 1
c = 0
d = 0

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

print(int(min(c,d)))

Submission status: Accepted

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

def cost_calc(list_x, p):
  cost = 0
  for x in list_x:
    cost += (x - p)**2
  return cost

cost = 10**20
for _ in range(min(list_x),max(list_x)+1):
  tmp = cost_calc(list_x, _)
  if tmp < cost:
    cost = tmp
print(cost)

Submission status: Accepted

Solution code:
import math
N = int(input())
X = [int(x) for x in input().split()]
s = sum(X)
p = s/N
floor = s//N
if p-floor >= 0.5:
  m = math.ceil(s/N)
else:
  m = math.floor(s/N)
square = [x**2 for x in X]
result = N*m**2 - 2*s*m + sum(square)
print(result)

Submission status: Accepted

Solution code:
import math
import fractions
import sys
import itertools
import numpy as np
from collections import Counter
import datetime
#入力:N(int:整数)
def input1():
	return int(input())

#入力:N,M(int:整数)
def input2():
	return map(int,input().split())

#入力:[n1,n2,...nk](int:整数配列)
def input_array():
	return list(map(int,input().split()))

def keta(N):
	return len(str(N))


def input_daytime(input_time):
	time,distance=input_time.split()
	t=time.split(":")
	se=str(t[-1]).split(".")
	dt1 = datetime.timedelta(hours=int(t[0]),minutes=int(t[1]),seconds=int(se[0]),milliseconds=int(se[1]))
	return dt1


n=input1()
X=input_array()

min_p=min(X)
max_p=max(X)
b_p=10000
p=[]
for i in range(min_p,max_p):
	s=0
	for j in X:
		s+=(j-i)**2
	p.append(s)
	# if s>b_p:
	# 	break
	# else:
	# 	b_p=s

if p==[]:
	print(0)
else:
	print(min(p))

Submission status: Accepted

Solution code:
stdin = open(0).read().split('\n')
N = int(stdin[0])
X = list(map(int,stdin[1].split()))
min=9999999
for i in range(1,101):
    sum=0
    for j in range(len(X)):
        sum+=(i-X[j])**2
    if sum<min:min=sum
print(min)

Submission status: Accepted

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

ans = float('inf')
for i in range(1,101,1):
    tai = 0
    for j in range(len(arr)):
        tai += (arr[j]-i)**2

    if tai <= ans:
        ans =tai

print(ans)

Submission status: Accepted

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

    tmp = (100**2)*100

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

    print(tmp)

main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

avex= sum(x) / N;

p = round(avex);

ans = sum([(a - p) ** 2 for a in x]);

print(ans)

Submission status: Accepted

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list
#from itertools import combinations # (string,3) 3回
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#

mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def main():
    n = int(input())
    X = readInts()
    mid = (sum(X) + (n-1)) // n
    ans = float('inf')
    for k in range(-1,2,1):
        p = 0
        for i in range(n):
            p += (mid + k - X[i]) ** 2
        ans = min(ans,p)
    print(ans)
if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
import math

N=input()
n=int(N)

p=input().split()
for i in range (len(p)):
    p[i]=int(p[i])

sum1=0

q=p.copy()

for i in range (len(q)):
    sum1=sum1+q[i]

avg=sum1/n

if avg-math.floor(avg)>=0.5:
    avg=math.floor(avg)+1
else:
    avg=math.floor(avg)


stamina=0

for i in range(len(p)):
    stamina=stamina+(avg-p[i])**2
    
print(stamina)

Submission status: Accepted

Solution code:
n=int(input())
x=list(map(int,input().split()))
b=[]
M,m=max(x),min(x)
for i in range(m-1,M+1):
  a=0
  for j in x:
    a+=(i-j)**2
  b.append(a)
print(min(b))

Submission status: Accepted

Solution code:
N = int(input())
x = input().split()
avg = 0

for i in range (N) :
    x[i] = int(x[i])
    avg += x[i]

avg1 = avg // N
avg2 = avg // N + 1

ans1 = 0
ans2 = 0

for i in range (N) :
    ans1 += (x[i] - avg1) ** 2
    ans2 += (x[i] - avg2) ** 2

print (int(min(ans1 , ans2)))

Submission status: Accepted

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

ans = int (2e9)
l, r = min (x), max (x)

for p in range (l, r + 1) :
    tmp = 0
    for loc in x:
        tmp += (loc - p) ** 2
    ans = min (ans, tmp)

print (ans)

Submission status: Accepted

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

ans = 10**9
for i in range(1, max(X) + 1):
    z = 0
    for x in X:
        z += (x-i)**2
    ans = min(ans, z)

print(ans)

Submission status: Accepted

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

ans = -1
for i in range(100):
    z = 0
    for x in X:
        z += (x-i) * (x-i)
    if ans == -1 or z < ans:
        ans = z

print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
import re
import math
import itertools
from copy import deepcopy
import random
from heapq import heappop,heappush
import time
import os
import queue
import sys
import datetime
from functools import lru_cache
readline=sys.stdin.readline
sys.setrecursionlimit(2000)
#import numpy as np
alphabet="abcdefghijklmnopqrstuvwxyz"
mod=int(10**9+7)
inf=int(10**20)
week=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
def yn(b):
    if b:
        print("yes")
    else:
        print("no")
def Yn(b):
    if b:
        print("Yes")
    else:
        print("No")
def YN(b):
    if b:
        print("YES")
    else:
        print("NO")
class union_find():
    def __init__(self,n):
        self.n=n
        self.P=[a for a in range(N)]
        self.rank=[0]*n
 
    def find(self,x):
        if(x!=self.P[x]):self.P[x]=self.find(self.P[x])
        return self.P[x]
 
    def same(self,x,y):
        return self.find(x)==self.find(y)
 
    def link(self,x,y):
        if self.rank[x]<self.rank[y]:
            self.P[x]=y
        elif self.rank[y]<self.rank[x]:
            self.P[y]=x
        else:
            self.P[x]=y
            self.rank[y]+=1
 
    def unite(self,x,y):
        self.link(self.find(x),self.find(y))
 
    def size(self):
        S=set()
        for a in range(self.n):
            S.add(self.find(a))
        return len(S)
def is_power(a,b):#aはbの累乗数か
    now=b
    while now<a:
        now*=b
    if now==a:return True
    else:return False
def bin_(num,size):
    A=[0]*size
    for a in range(size):
        if (num>>(size-a-1))&1==1:
            A[a]=1
        else:
            A[a]=0
    return A
def facs(n,mod_=0):
    A=[1]*(n+1)
    for a in range(2,len(A)):
        A[a]=A[a-1]*a
        if(mod_>0):A[a]%=mod_
    return A
def comb(n,r,mod_,fac):
    if(n-r<0):return 0
    return (fac[n]*pow(fac[n-r],mod_-2,mod_)*pow(fac[r],mod_-2,mod_))%mod_
def next_comb(num,size):
    x=num&(-num)
    y=num+x
    z=num&(~y)
    z//=x
    z=z>>1
    num=(y|z)
    if(num>=(1<<size)):return False
    else:
        return num
def get_primes(n,type="int"):
    A=[True]*(n+1)
    A[0]=False
    A[1]=False
    for a in range(2,n+1):
        if A[a]:
            for b in range(a*2,n+1,a):
                A[b]=False
    if(type=="bool"):return A
    B=[]
    for a in range(n+1):
        if(A[a]):B.append(a)
    return B
def is_prime(num):
    if(num<=2):return False
    i=2
    while i*i<=num:
       if(num%i==0):return False
       i+=1
    return True
def ifelse(a,b,c):
    if a:return b
    else:return  c
def join(A,c=" "):
    n=len(A)
    A=list(map(str,A))
    s=""
    for a in range(n):
        s+=A[a]
        if(a<n-1):s+=c
    return s
def factorize(n,type_="dict"):
    b = 2
    list_ = []
    while b * b <= n:
        while n % b == 0:
            n //= b
            list_.append(b)
        b+=1
    if n > 1:list_.append(n)
    if type_=="dict":
        dic={}
        for a in list_:
            if a in dic:
                dic[a]+=1
            else:
                dic[a]=1
        return dic
    elif type_=="list":
        return list_
    else:
        return None
def floor_(n,x=1):
    return x*(n//x)
def ceil_(n,x=1):
    return x*((n+x-1)//x)
#################################### ########
# coding: utf-8
import re
import math
import itertools
from copy import deepcopy
import random
from heapq import heappop,heappush
import time
import os
import queue
import sys
import datetime
from functools import lru_cache
readline=sys.stdin.readline
sys.setrecursionlimit(2000)
#import numpy as np
alphabet="abcdefghijklmnopqrstuvwxyz"
mod=int(10**9+7)
inf=int(10**20)
def yn(b):
    if b:
        print("yes")
    else:
        print("no")
def Yn(b):
    if b:
        print("Yes")
    else:
        print("No")
def YN(b):
    if b:
        print("YES")
    else:
        print("NO")
class union_find():
    def __init__(self,n):
        self.n=n
        self.P=[a for a in range(N)]
        self.rank=[0]*n
 
    def find(self,x):
        if(x!=self.P[x]):self.P[x]=self.find(self.P[x])
        return self.P[x]
 
    def same(self,x,y):
        return self.find(x)==self.find(y)
 
    def link(self,x,y):
        if self.rank[x]<self.rank[y]:
            self.P[x]=y
        elif self.rank[y]<self.rank[x]:
            self.P[y]=x
        else:
            self.P[x]=y
            self.rank[y]+=1
 
    def unite(self,x,y):
        self.link(self.find(x),self.find(y))
 
    def size(self):
        S=set()
        for a in range(self.n):
            S.add(self.find(a))
        return len(S)
def is_power(a,b):#aはbの累乗数か
    now=b
    while now<a:
        now*=b
    if now==a:return True
    else:return False
def bin_(num,size):
    A=[0]*size
    for a in range(size):
        if (num>>(size-a-1))&1==1:
            A[a]=1
        else:
            A[a]=0
    return A
def get_facs(n,mod_=0):
    A=[1]*(n+1)
    for a in range(2,len(A)):
        A[a]=A[a-1]*a
        if(mod>0):A[a]%=mod_
    return A
def comb(n,r,mod,fac):
    if(n-r<0):return 0
    return (fac[n]*pow(fac[n-r],mod-2,mod)*pow(fac[r],mod-2,mod))%mod
def next_comb(num,size):
    x=num&(-num)
    y=num+x
    z=num&(~y)
    z//=x
    z=z>>1
    num=(y|z)
    if(num>=(1<<size)):return False
    else:
        return num
def get_primes(n,type="int"):
    A=[True]*(n+1)
    A[0]=False
    A[1]=False
    for a in range(2,n+1):
        if A[a]:
            for b in range(a*2,n+1,a):
                A[b]=False
    if(type=="bool"):return A
    B=[]
    for a in range(n+1):
        if(A[a]):B.append(a)
    return B
def is_prime(num):
    if(num<=2):return False
    i=2
    while i*i<=num:
       if(num%i==0):return False
       i+=1
    return True
def ifelse(a,b,c):
    if a:return b
    else:return  c
def join(A,c=" "):
    n=len(A)
    A=list(map(str,A))
    s=""
    for a in range(n):
        s+=A[a]
        if(a<n-1):s+=c
    return s
def factorize(n,type_="dict"):
    b = 2
    list_ = []
    while b * b <= n:
        while n % b == 0:
            n //= b
            list_.append(b)
        b+=1
    if n > 1:list_.append(n)
    if type_=="dict":
        dic={}
        for a in list_:
            if a in dic:
                dic[a]+=1
            else:
                dic[a]=1
        return dic
    elif type_=="list":
        return list_
    else:
        return None
def floor_(n,x=1):
    return x*(n//x)
def ceil_(n,x=1):
    return x*((n+x-1)//x)
###################################################
def main():
    n=int(input())
    X=list(map(int,input().split()))
    ans=inf
    for p in range(min(X),max(X)+1):
        ans=min(ans,sum(list(map(lambda x:(x-p)**2,X))))
    print(ans)




main()

Submission status: Accepted

Solution code:
from statistics import mean
N = int(input())
X = [int(x) for x in input().split()]
sum = 0
goal = round(mean(X))
for i in range(len(X)):
    sum += (X[i] - goal) ** 2
print(sum)

Submission status: Accepted

Solution code:
#設定
import sys
input = sys.stdin.buffer.readline

#ライブラリインポート
from collections import defaultdict

con = 10 ** 9 + 7
#入力受け取り
def getlist():
	return list(map(int, input().split()))

#処理内容
def main():
	N = int(input())
	X = getlist()
	ans = 10000000000
	for i in range(1, 101):
		var = 0
		for j in range(N):
			var += (X[j] - i) ** 2

		ans = min(ans, var)

	print(ans)

if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
INT_MAX=10**18+7
x=10**9+7
def INPUT():return list(int(i) for i in input().split())
def LIST_1D_ARRAY(n):return [0 for _ in range(n)]
def LIST_2D_ARRAY(m,n):return [[0 for _ in range(n)]for _ in range(m)]
GETA=123
###############################################################################
import math as M
n=int(input())
A=INPUT()
sum=0
for ele in A:
    sum+=ele
ans=sum/n
#print(ans)
ans1=int(ans)+1
sum1=0
sum2=0
ans2=int(ans)
for ele in A:
    sum1+=(ele-ans1)**2
for ele in A:
    sum2+=(ele-ans2)**2
print(min(sum1,sum2))

Submission status: Accepted

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

import sys
input = sys.stdin.readline

def main():
    N = int(input())
    X = list(map(int, input().split()))
    x2 = sum([i*i for i in X])
    x = sum(X)
    ret = 100100100
    for p in range(min(X), max(X)+1):
        ret = min(ret, x2 - 2*x*p + N*p*p)
    print(ret)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np
ans = 0

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

P = int(round(np.average(X)))

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

print(ans)

Submission status: Accepted

Solution code:
'''
INPUT SHORTCUTS
N, K = map(int,input().split())
N ,A,B = map(int,input().split())
string = str(input())
arr = list(map(int,input().split()))
N = int(input())
'''
import math
N = int(input())
arr = list(map(int,input().split()))
add = sum(arr)
avg = int(round(add/N)) 
ans = 0
for i in range(N):
	ans += (avg-arr[i])*(avg-arr[i])
print(int(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
N = int(input())
X = list(map(int, input().split()))
P1, P2 = math.ceil(sum(X) / N), int(sum(X) / N)
work1, work2 = 0, 0
for x in X:
    work1 += (x - P1)**2
    work2 += (x - P2)**2
print(min(work1, work2))

Submission status: Accepted

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

n=int(input())
nums=list(map(int, input().split()))
ave=round(sum(nums)/n)

ans=0
for i in nums:
    ans+=(i-ave)**2

print(ans)

Submission status: Accepted

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

# 最も体力がいる時でも 100**2(体力) * 100（個数） = 10**6　を超えることはない
hp_old = 10**6

# 全探索
for p in range(1, 101, 1):
    hp_new = 0
    for xi in x:
        # 体力計算
        hp_new += (xi - p) ** 2
    # 体力の最小値を求める
    hp = min(hp_new, hp_old)
    # 上記の体力を現状最小値の体力と定義する
    hp_old = hp

print(hp)

Submission status: Accepted

Solution code:
def custom_round(number, ndigits=0):
    if type(number) == int:
        return number
    d_point = len(str(number).split('.')[1])
    if ndigits >= d_point:
        return number
    c = (10 ** d_point) * 2
    return round((number * c + 1) / c, ndigits)

N=int(input())
A=list(map(int,input().split()))
ave=custom_round(sum(A)/N,0)
total=0
for i in range(N):
    total+=(A[i]-ave)**2
print(int(total))

Submission status: Accepted

Solution code:
import numpy as np

def solve():
    opt = np.round(np.sum(x)/len(x))
    ans = int(np.sum((x-opt)**2))
    return ans


n = int(input())
x = [int(i) for i in input().split(" ")]
print(solve())

Submission status: Accepted

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

Submission status: Accepted

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

ave_down = sum(x) // n #切り捨て平均値
ave_up = -(-sum(x) // n) #切り上げ平均値

distance1 = []
distance2 = []

for i in range(n):
  distance1.append(( ave_down - x[i] ) ** 2)

for i in range(n):
  distance2.append(( ave_up - x[i] ) ** 2)

if sum(distance1) <= sum(distance2):
  print(sum(distance1))
else:
  print(sum(distance2))

Submission status: Accepted

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

dis2 = [0]*100
for p in range(100):
    dis = 0
    for i in range(a):
        dis += (li[i]-(p+1))**2
    dis2[p] = dis
print(min(dis2))

Submission status: Accepted

Solution code:
import sys, math
import collections
input = sys.stdin.readline

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

p = sum(X) / N

def f(p):
	s = 0
	for x in X:
		s += (x - p)**2
	return s

s = min(f(math.floor(p)), f(math.ceil(p)))
print(s)

Submission status: Accepted

Solution code:
import math
import statistics

n = int(input())

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

my_mean = sum(li) / len(li)
ll = math.floor(my_mean)
lll = math.ceil(my_mean)

c = 0
for i in range(len(li)):
  c += (li[i] - ll)**2
  
cc = 0
for i in range(len(li)):
  cc += (li[i] - lll)**2
  
  
if c < cc:
  print(c)
else:
  print(cc)

#20

Submission status: Accepted

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

ans = float("inf")
for i in range(1, 101):
    cost = 0
    for j in range(n):
        cost += (x[j] - i) ** 2
    ans = min(ans, cost)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
X_list = list(map(int, input().split()))
min_strength = sum(list(map(lambda x: (x - max(X_list)) ** 2, X_list)))
for i in range(min(X_list), max(X_list)):
    strength = sum(list(map(lambda x: (x - i) ** 2, X_list)))
    if min_strength > strength:
        min_strength = strength
print(min_strength)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

mi = min(xi)
ma = max(xi)
ans = 10**18
for i in range(mi, ma+1):
    num = 0
    for j in range(n):
        num += (xi[j]-i)**2
    ans = min(ans, num)
print(ans)

Submission status: Accepted

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

average = round(sum(a)/n)

ans=0

for i in a:
  ans += (i - average)**2
  
print(ans)

Submission status: Accepted

Solution code:
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:
import numpy as np
N = int(input())
X = np.array(list(map(int, input().split())))
ans = 10000000
for p in range(min(X), max(X) + 1):
    Y = np.sum((X - p)**2)
    ans = min(ans, Y)

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
X=list(map(int,input().split()))
def D(x,a):
	ans=0
	for i in range(len(a)):
		ans+=(x-a[i])**2
	return ans
for i in range(0,101):
	if D(i,X)<D(i+1,X):
		print(D(i,X))
		break

Submission status: Accepted

Solution code:
n = int(input())
x = [int(x) for x in input().split()]
center = sum(x)//len(x)
center2 = center+1
ans, ans2 =0, 0
for i in x:
    ans += (i-center)**2
    ans2 += (i-center2)**2

print(ans) if ans<ans2 else print(ans2)

Submission status: Accepted

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

ex = sum(xn)/len(xn)
if ex-round(ex) < ex-round(ex)+1:
  ex = round(ex)
else:
  ex = round(ex)+1
varx = [(i-ex)**2 for i in xn]

print(int(sum(varx)))

Submission status: Accepted

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

average = round(sum(X) / N)

ans = 0

for i in X:
    ans += (i-average)**2

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

l = min(x)
r = max(x)

ans = 10000000000

for i in range(l, r+1):
    ansi = 0
    for a in x:
        ansi += abs(a - i) ** 2
    ans = min(ans, ansi)

print(ans)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n = int(readline())
x = list(map(int, readline().split()))
v = [0] * 100
cnt = 0
for i in range(100):
    for xx in x:
        cnt += (xx - i) ** 2
    v[i] = cnt
    cnt = 0
print(min(v))

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int, input().split()))
ans1, ans2 = 0, 0
p1 = -(-sum(X)//N)
p2 = sum(X)//N

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

Submission status: Accepted

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

res = float('inf')

for i in range(min(X), max(X)+1):
    tmp = 0
    for x in X:
        tmp += (i - x) ** 2

    res = min(tmp, res)

print(res)

Submission status: Accepted

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

res = float('inf')

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

    res = min(tmp, res)

print(res)

Submission status: Accepted

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

ans=1000000000
for p in range(1,101):
    temp=0
    for i in range(N):
        temp+=(X[i]-p)**2
    if ans>temp:
        ans=temp

print(ans)

Submission status: Accepted

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

P=sum(X)/N
if P%1 < 0.5:
    P=P//1
else:
    P=P//1+1
L=0
for i in range(N):
    L += X[i]**2
print(int(L-2*P*sum(X)+N*P**2))

Submission status: Accepted

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

    ans = 10 ** 8
    x_min = min(x)
    x_max = max(x)
    for i in range(x_min, x_max+1):
        tmp = 0
        for j in range(n):
            tmp += (x[j] - i) ** 2
        if tmp < ans:
            ans = tmp

    print(ans)

Submission status: Accepted

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

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

  total.append(cost)
print(min(total))

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int,input().split()))
x = sorted(x)
ans = 100**3
for i in range(x[0],x[n-1]+1):
  cost = 0
  for j in range(n):
    cost += pow(x[j]-i,2)
  ans = min(ans,cost)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
xn = list(map(int, input().split()))
xn = sorted(xn)
min_x = xn[0]
max_x = xn[-1]
min_total = float('inf')
for i in range(min_x, max_x+1):
    total = 0
    for x in xn:
        total += abs(x-i)**2
    min_total = min(min_total, total)

print(min_total)

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
cand = []
for i in range(1, 100):
    cand.append(sum([(j-i)**2 for j in x]))

print(min(cand))

Submission status: Accepted

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

MaxX = max(X)
MinX = min(X)

ListP = []

for i in range(MinX, MaxX+1):
    P = 0
    for j in range(N):
        P += (X[j]-i)**2
    
    ListP.append(P)

print(min(ListP))

Submission status: Accepted

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

distance = lambda Xi, P: (Xi - P) ** 2
all_distance = lambda X, P: sum([distance(Xi, P) for Xi in X])

X_min = min(X)
X_max = max(X)

answer = min([all_distance(X, P) for P in range(X_min, X_max + 1)])

print(answer)

Submission status: Accepted

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

distance = lambda Xi, P: (Xi - P) ** 2
all_distance = lambda X, P: sum([distance(Xi, P) for Xi in X])

X_min = min(X)
X_max = max(X)

answer = None
for P in range(X_min, X_max + 1):
    total = all_distance(X, P)
    answer = min(total, answer) if answer is not None else total

print(answer)

Submission status: Accepted

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

distance = lambda Xi, P: (Xi - P) ** 2

X_min = min(X)
X_max = max(X)

answer = None
for P in range(X_min, X_max + 1):
    total = sum([distance(Xi, P) for Xi in X])
    answer = min(total, answer) if answer is not None else total

print(answer)

Submission status: Accepted

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

distance = lambda Xi, P: (Xi - P) ** 2

X_min = min(X)
X_max = max(X)

answer = None
for P in range(X_min, X_max + 1):
    total = 0
    for Xi in X:
        total += distance(Xi, P)
    answer = min(total, answer) if answer is not None else total

print(answer)

Submission status: Accepted

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

distance = lambda Xi, P: (Xi - P) ** 2

X_max = max(X)

answer = None
for P in range(1, X_max + 1):
    total = 0
    for Xi in X:
        total += distance(Xi, P)
    answer = min(total, answer) if answer is not None else total

print(answer)