Problem:


Score : 300 points

Problem Statement

There are N people living on a number line.

The i-th person lives at coordinate X_i.

You are going to hold a meeting that all N people have to attend.

The meeting can be held at any integer coordinate. If you choose to hold the meeting at coordinate P, the i-th person will spend (X_i - P)^2 points of stamina to attend the meeting.

Find the minimum total points of stamina the N people have to spend.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq X_i \leq 100

Input

Input is given from Standard Input in the following format:

N
X_1 X_2 ... X_N

Output

Print the minimum total stamina the N people have to spend.

Sample Input 1

2
1 4

Sample Output 1

5

Assume the meeting is held at coordinate 2. In this case, the first person will spend (1 - 2)^2 points of stamina, and the second person will spend (4 - 2)^2 = 4 points of stamina, for a total of 5 points of stamina. This is the minimum total stamina that the 2 people have to spend.

Note that you can hold the meeting only at an integer coordinate.

Sample Input 2

7
14 14 2 13 56 2 37

Sample Output 2

2354



Sample input:
2
1 4


Sample output:
5

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
sum= 1000000000000000
for p in range(1,101):

    tmp=0
    # print("p",p)
    for i in range(len(x)):
        tmp += (x[i] - p)**2
        # print("tmp",tmp)
    sum = min(sum,tmp)
    # print("su",sum)
print(sum)

Submission status: Accepted

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

lst.sort()
M = lst[-1]
m = lst[0]
total = float('inf')

for i in range(m,M+1):
  s = sum([(x-i)**2 for x in lst])
  if s < total:
    total = s
    
print(total)

Submission status: Accepted

Solution code:
N = int(input())
X_ls = list(map(int, input().split(' ')))
rst = -1
for i in range(min(X_ls), max(X_ls) + 1):
    val = 0
    for j in X_ls:
        val += (i - j) ** 2
    if rst == -1:
        rst = val
    else:
        rst = min(rst, val)
print(rst)

Submission status: Accepted

Solution code:
def stamina(x, p):
    return (x - p) ** 2


def resolve():
    N = int(input())
    Xs = list(map(int, input().split()))

    min_sum = 10 ** 10
    for p in range(1, 101):
        s_sum = 0
        for p_x in Xs:
            s_sum += stamina(p_x, p)

        min_sum = min(min_sum, s_sum)

    print(min_sum)
resolve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int, input().split()))
_min = min(X)
_max = max(X)
_sum = 1000000000
for i in range(_min, _max+1):
    temp = 0
    for x in X:
        temp += (x - i) * (x - i)
    if temp < _sum:
        _sum = temp
print(_sum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect, copy, heapq, math, sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
def celi(a,b):
    return -(-a//b)
sys.setrecursionlimit(5000000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

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

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

Submission status: Accepted

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

Submission status: Accepted

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

for p in range(X[N-1] + 1):
    sum = 0
    for i in range(len(X)):
         sum += (X[i]-p)**2
         ans = sum
    p += 1
    data.append(ans)

print(min(data))

Submission status: Accepted

Solution code:
# import math
# import statistics
a=int(input())
#b,c=int(input()),int(input())
# c=[]
# for i in a:
#     c.append(i)
#e1,e2 = map(int,input().split())
f = list(map(int,input().split()))
#g = [input() for _ in range(a)]
f.sort()
ans=0
ma = []
for i in range(f[0],f[-1]+1):
    for k in f:
        ans+=((k-i)**2)
    ma.append(ans)
    ans=0
print(min(ma))

Submission status: Accepted

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

tairyoku = []
for x in range(min(p),max(p)+1):
  a = 0
  for y in p:
    a += (x-y)**2
  tairyoku.append(a)
  
print(min(tairyoku))

Submission status: Accepted

Solution code:
N = int(input())
X_ls = list(map(int, input().split(' ')))
rst = -1
for i in range(min(X_ls), max(X_ls) + 1):
    val = 0
    for j in X_ls:
        val +=  (i - j) ** 2
    if rst == -1:
        rst = val
    else:
        rst = min(rst, val)
print(rst)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
Xlist=list(map(int,input().split()))
ans=10**18
for p in range(1,101):
    ans=min(ans,sum(list(map(lambda x:(p-x)**2,Xlist))))
print(ans)

Submission status: Accepted

Solution code:
import sys
N = int(input())
X = [int(c) for c in input().split()]
l = [10**10]
for i in range(1,101):
    l.append(0)
    for j in range(len(X)):
        l[i] += (X[j]-i)**2
    
    if l[i-1] < l[i]:
        print(l[i-1])
        sys.exit(0)

Submission status: Accepted

Solution code:
# coding: utf-8

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

    for i in range(1, 101):
        tmp = sum([(x - i) ** 2 for x in X])
        if tmp < ans:
            ans = tmp

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int, input().split()))
SUM = 0
DIF = 0
for i in range(N):
    SUM = SUM + X[i]

AVG = round((SUM / N))



for i in range(N):
    DIF = DIF + (abs(X[i] - AVG))**2
print(DIF)

Submission status: Accepted

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

ans = float("inf")

for p in range(min(x),max(x)+1):
	t = 0
	for i in x:
		t+=(i-p)**2
	ans = min(ans, t)

print(ans)

Submission status: Accepted

Solution code:
# C - Rally

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

c = []
x_ave = int(sum(x)/n)
s = 0
for i in range(n):
    s += (x[i]-x_ave)**2
c.append(s)
s = 0
for i in range(n):
    s += (x[i]-x_ave-1)**2
c.append(s)


print(min(c))

Submission status: Accepted

Solution code:
n=int(input())
x=list(map(int,input().split()))
x.sort()
m=10000000000
for i in range(x[0],x[n-1]+1):
  ans=0
  for j in range(n):
    ans+=(i-x[j])**2
  m=min(m,ans)

print(m)

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int,input().split()))
ans = 10**36
S = 0

for p in range(100):
    S =0
    for x in X:
        S += (x-p)**2
    ans = min(ans,S)

print(ans)

Submission status: Accepted

Solution code:
import sys

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

# i = min(x)
# while i <= max(x):
for i in range(min(x),max(x)+1,1):
    tmp = sum(map(lambda p: (p-i)**2,x))
    if ans > tmp:
        ans = tmp
    # i += 1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

print(min(ans))

Submission status: Accepted

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

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

ans = sum((x-G)**2 for x in X)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
s = list(map(int , input().split()))
list.sort(s , reverse = True)
L = [10**10]
for p in range(min(s) , max(s) + 1):
  a = 0
  b = L[0]
  for i in range(N):
    if a < b:
      a += (s[i] - p) ** 2
  if a < b:
    L[0] = a
print(L[0])

Submission status: Accepted

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

a = min(X)
b = max(X)

s = 10**6
for i in range(a, b+1):
    t = 0
    for j in range(N):
        t += (X[j] - i)**2
    if t < s:
        s = t

print(s)

Submission status: Accepted

Solution code:
n = int(input())
x = [int(x) for x in input().split()]
res = 10 ** 10
for i in range(101):
  c = 0
  for j in range(n):
    c += (i - x[j]) ** 2
  res = min(res,c)
print(res)

Submission status: Accepted

Solution code:
#-*-coding:utf-8-*-
import sys
import math
input=sys.stdin.readline

def main():
    maps=[]
    n = int(input())
    maps=list(map(int,input().split()))
    ans=0
    answers=[]

    for i in range(1,101):
        for data in maps:
            ans+=abs(i-data)**2
        answers.append(ans)
        ans=0
    print(min(answers))

if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
#import numpy as np
#n = 100
#a = np.random.randint(1, 100, n)
a = sorted(a)
minsum = float('inf')
for i in range(a[0], a[-1]+1):
  cnt = 0
  for ai in a:
    cnt += (ai - i)**2
  if cnt <= minsum:
    minsum = cnt
print(minsum)

Submission status: Accepted

Solution code:

def main():
    n = int(input())
    x = list(map(int, input().split(" ")))
    min = 1e9
    for i in range(1,101):
        na = 0
        for j in x:
            na += (j - i)**2
        if na < min:
            min = na
    print(min)
        



if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
num = int(input())
pos = list(map(int,input().split()))
maxpos = max(pos)
minhealth = 1e9
count = 0
for i in range(maxpos+1):
    for j in pos:
        count += (j-i)**2
    minhealth = min(minhealth, count)
    count = 0
print(minhealth)

Submission status: Accepted

Solution code:
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

from  collections import defaultdict
import bisect
def main():
    N = I()
    X = LI()
    Ans_list = []
    for i in range(1, 101):
        cnt = 0
        for x in X:
            cnt += (x - i) ** 2
        Ans_list.append(cnt)
    print(min(Ans_list))

if __name__ == "__main__":
    main()

Submission status: Accepted

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

x.sort()

ans = 1000000000

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

Submission status: Accepted

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

for i in range(100):
	P=i+1
	a=0
	for x in X:
		a+=(x-P)**2
	ans[i]=a

print(min(ans))

Submission status: Accepted

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

m = sum(x)/len(x)
m = int(round(m))
power = sum([(i - m)**2 for i in x])
print(power)

Submission status: Accepted

Solution code:
N = int(input())
nums = list(map(int, input().split()))
answer = 10**8
for p in range(1, 100):
  power = 0
  for j in range(N):
    power += (p-nums[j])**2
  answer = min(answer, power)
print(answer)

Submission status: Accepted

Solution code:
# C - Rally
N = int(input())
X = list(map(int,input().split()))
ans = 10**10
for p in range(1,101):
    tmp = 0
    for x in X:
        tmp += (x-p)**2
    ans = min(ans,tmp)
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
x = list(map(int,input().split()))
y =[]
b = sum(x)
for i in range(n):
        y.append((x[i])**2)
a = sum(y)

p = round(b/(n))
ans = (n)*p**2-2*p*b+a
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
import numpy as np
def main():
    n = int(input())
    p = np.array(read().split(), np.int32)
    r = float('inf')
    for i1 in range(101):
        r = min(r, sum((p - i1)**2))
    print(r)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

mid = sum(x)//len(x)

ans = float('inf')
for i in [-1, 0, 1]:
    temp = sum([(i+j-mid)**2 for j in x])
    ans = min(temp, ans)
print(ans)

Submission status: Accepted

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

ans = 999999

for i in range(100):
    x = 0
    for a in A:
        x += (a-i)**2
    ans = min(ans,x)

print(ans)

Submission status: Accepted

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

X.sort()
dist = float("inf")
for i in range(X[0],X[-1]+1):
  d = 0
  for j in range(N):
    d += (i - X[j]) ** 2
  dist = min(dist,d)
  
print(dist)

Submission status: Accepted

Solution code:
from sys import exit
import math
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

n = ii()
x = li()

ave = sum(x) //n

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

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int, input().split()))
 
def calc_power(p):
  P = list(map(lambda x: (x-p)**2, X))
  return sum(P)
 
power = []
_min = min(X)
_max = max(X)

if _min != _max:
  for p in range(_min, _max):
    power.append(calc_power(p))
  print(min(power))
  
else:
  power = calc_power(_min)
  print(power)

Submission status: Accepted

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

avg1 = sum(x)//n
avg2 = avg1+1

ans1 = int(sum([(i - avg1)**2 for i in x]))
ans2 = int(sum([(i - avg2)**2 for i in x]))

print(min(ans1,ans2))

Submission status: Accepted

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

P=round(sum(X)/N,0)
vital_sum=0
for i in X:
    vital_sum+=(i-P)**2
print(int(vital_sum))

Submission status: Accepted

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

m = min(x)
M = max(x)
ans = 10**9
for i in range(m,M+1):
  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()))
 
ans = 10**9
m = min(X)
M = max(X)
for i in range(m, M+1):
  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()))
 
ans = 10**9
m = min(X)
M = max(X)
for i in range(m, M+1):
  s = 0
  for j in range(N):
    s += (X[j]-i)**2
  ans = min(ans, s)
  
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin
from decimal import Decimal, ROUND_HALF_UP

n = int(stdin.readline().rstrip())
data = list(map(int, stdin.readline().rstrip().split()))
p = sum(data) / n

#p should be int
p_int = (Decimal(p).quantize(Decimal('1'), ROUND_HALF_UP))

Sum = 0
for i in data:
	Sum  += (i - p_int) ** 2 
print(Sum)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
 
N = input()
X = list(map(int, input().split()))
ans=9999999
for P in range(0, 101):
  sum = np.sum(list(map(lambda x: (x-P)**2, X)))
  ans = min(ans, sum)
    
print(ans)

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    N = int(readline())
    X = list(map(int, readline().split()))
    ans = INF

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

lc=[]
for i in range(1,101):
    c = 0
    for x in lx:
        c += (i-x)**2
    lc.append(c)

print(min(lc))

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-
n = int(input())

PosList = []
TempSum = 0
ReturnSum = 0

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


for pos in range(1,101):
    TempSum = 0
    for nPos in range(n):
        TempSum += (pos - PosList[nPos])**2
    if pos == 1:
        ReturnSum = TempSum
    elif ReturnSum > TempSum:
        ReturnSum = TempSum
    """
    print("Position = "+str(pos))
    print("TempSum = "+ str(TempSum))
    print("ReturnSum = " + str(ReturnSum))
    print("----------------------------")
    """

print(ReturnSum)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

if n==1:
    print('0')
    sys.exit()

if x.count(x[0])==n:
    print('0')
    sys.exit()

a=sum(x)/len(x)
d=[]
for i in range(min(x),max(x)):
    c=0
    for j in x:
        c+=(i-j)**2
    d.append(c)

print(min(d))

Submission status: Accepted

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

ans = 10**8+1

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

Submission status: Accepted

Solution code:
import math
n = int(input())
xn_list = list(map(int, input().split()))
ans = float('inf')
for i in range(1, 100):
    temp = 0
    for j in xn_list:
        temp += (j-i)**2
    ans = min(ans, temp)
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
X=list(map(int,input().split()))
l=len(X)
s=10**10
for i in range(min(X),max(X)+1):
    x=[]
    for j in range(l):
        x.append((X[j]-i)**2)
    if s>sum(x):
        s=sum(x)
print(s)

Submission status: Accepted

Solution code:
N = int(input(""))
X = input("").split(" ")
X = [int(b) for b in X]
ans = 0
xb = int(round(sum(X) / len(X)))
for a in X:
    ans += (a-xb)**2
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
s=sum(l)//n
k=1+(sum(l)//n)
c=0
x=0
for i in range(n):
    c+=(l[i]-s)**2
for i in range(n):
    x+=(l[i]-k)**2
print(min(c,x))

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    x_lst = list(map(int, input().split()))
    ans = 10 ** 12

    for p in range(1, 101):
        sum_hp = 0
        for x in x_lst:
            sum_hp += (x - p) ** 2
        if sum_hp < ans:
            ans = sum_hp

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import math
n = input()
arr = list(map(int, input().split()))
a = sum(arr) // len(arr)
b = a + 1
'''
arr.sort()
if len(arr) % 2 == 0:
    mid = len(arr) // 2
    avg = (arr[mid] + arr[mid-1]) // 2
else:
    mid = len(arr) // 2
    avg = arr[mid]

print(avg)
'''
ans = 0
ans2 = 0
for p in arr:
    ans += (a - p)**2
    ans2  += (b - p)**2
print(min(ans, ans2))

Submission status: Accepted

Solution code:
# C - Rally
N = int(input())
X = list(map(int,input().split()))
ans = 1000000000
for p in range(1,101):
    tmp = 0
    for x in X:
        tmp += (x-p)**2
    ans = min(ans,tmp)
print(ans)

Submission status: Accepted

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

ans=float('inf')

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = float('inf')

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

    ans = min(ans, cost)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
x = [int(y.strip()) for y in input().split()]
lower = min(x)
upper = max(x)
i = lower
ans = 10000*100
for i in range(upper+1):
  ttl = 0
  for j in x:
    dist = (j - i)**2
    ttl = ttl + dist
  if ttl < ans:
    ans = ttl
print(ans)

Submission status: Accepted

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

for x in X:
  result1 += (x - P)**2
  result2 += (x - P - 1)**2

if result1 <= result2:
  result = result1
elif result1 > result2:
  result = result2


print(result)

Submission status: Accepted

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

INF = 10**6 + 1
mintotal = INF
for p in range(1, 101):
    total = 0
    for x in X:
        total += (x-p)**2
    mintotal = min(mintotal, total)
print(mintotal)

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque, Counter, OrderedDict
from bisect import bisect_left, bisect_right
from functools import reduce, lru_cache
from heapq import heappush, heappop, heapify

import itertools
import math, fractions
import sys, copy

def L(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline().rstrip())
def SL(): return list(sys.stdin.readline().rstrip())
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI1(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return [list(x) for x in sys.stdin.readline().split()]
def R(n): return [sys.stdin.readline().strip() for _ in range(n)]
def LR(n): return [L() for _ in range(n)]
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def LIR1(n): return [LI1() for _ in range(n)]
def SR(n): return [SL() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]

def perm(n, r): return math.factorial(n) // math.factorial(r)
def comb(n, r): return math.factorial(n) // (math.factorial(r) * math.factorial(n-r))

def make_list(n, *args, default=0): return [make_list(*args, default=default) for _ in range(n)] if len(args) > 0 else [default for _ in range(n)]

sys.setrecursionlimit(1000000)
dire, dire8 = [[1, 0], [0, 1], [-1, 0], [0, -1]], [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]]
alphabets, ALPHABETS = "abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
MOD, INF = 1000000007, float("inf")

def main():
    N = I()
    X = LI()

    ans = INF

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

def average(a):
  return sum(a) / len(a)

avg = average(X)

res = 0
if avg - int(avg) >= 0.5:
  for i in X:
    res += (i - math.ceil(avg)) **2
else:
  for i in X:
    res += (i - math.floor(avg)) ** 2
print(res)

Submission status: Accepted

Solution code:
n= int(input())
x= list(map(int,input().split()))
m= sum(x)/len(x)
g= int(m)
ans=0
if m-g<g+1-m:
  ans=g
else:
  ans=g+1
aaa=0
for i in x:
  aaa+= (ans-i)**2
print(aaa)

Submission status: Accepted

Solution code:
import sys, re
from math import ceil, floor, sqrt, pi, factorial, gcd
from copy import deepcopy
from collections import Counter, deque
from heapq import heapify, heappop, heappush
from itertools import accumulate, product, combinations, combinations_with_replacement
from bisect import bisect, bisect_left, bisect_right
from functools import reduce
from decimal import Decimal, getcontext
# input = sys.stdin.readline 
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 6)
INF = float('inf')
MOD = 10 ** 9 + 7
num_list = []
str_list = []

def main():
    n = i_input()
    xs = i_list()

    min_list = []
    for i in range(1, 101):
        tmp = 0
        for x in xs:
            tmp += (i - x)**2
        min_list.append(tmp)

    print(min(min_list))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int, input().split()))
a = sum(X)
b = sum([x * x for x in X])
if a / N - a // N < 0.5:
  c = a // N
else:
  c = a // N + 1
print(N * c * c - 2 * a * c + b)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

n=int(input())
x=list(map(int,input().split()))
cost_min=1000000000
for i in range(1,101):
    cost=0
    for j in x:
        cost+=(j-i)**2
    if cost>=cost_min:
        continue
    cost_min=cost
print(cost_min)

Submission status: Accepted

Solution code:
from sys import stdin
from math import ceil, floor
n = stdin.readline().rstrip()
data = [int(x) for x in stdin.readline().rstrip().split()]
m = sum(data) / len(data)
cm = ceil(m)
fm = floor(m)
ceil_m = floor_m = 0
for i in data:
    ceil_m += (i-cm)**2
    floor_m += (i-fm)**2
print(min(ceil_m, floor_m))

Submission status: Accepted

Solution code:
from sys import stdin
from math import ceil, floor
n = int(stdin.readline().rstrip())
data = [int(x) for x in stdin.readline().rstrip().split()]
m = sum(data) / len(data)
ceil_m = sum([(x-ceil(m))**2 for x in data])
floor_m = sum([(x-floor(m))**2 for x in data])
print(min(ceil_m, floor_m))

Submission status: Accepted

Solution code:
n = int(input())
xi = list(map(int,input().split()))
tairyoku = []
for p in range(100):
  tmp = 0
  for x in xi:
    tmp += (p+1-x)**2
  tairyoku.append(tmp)
print(min(tairyoku))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
def main():
    _ = int(input())
    X = [int(x) for x in input().split()]

    ans = 10 ** 6
    for i in range(1, 101):
        res = 0
        for x in X:
            res += (x - i) ** 2
        ans = min(ans, res)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N=int(input())
#people
li = list(map(int, input().split()))
Ave1=sum(li)//N
Ave2=sum(li)//N+1
S1=0
S2=0
for i in range(N):
  S1=S1+(Ave1-li[i])**2
for i in range(N):
  S2=S2+(Ave2-li[i])**2

if S1>S2:
  print(S2)
else:
  print(S1)

Submission status: Accepted

Solution code:
n=int(input())
x=list(map(int,input().split()))
m=10**15
for i in range(101):
    t=x[:]
    s=sum(list(map(lambda x:(x-i)**2,t)))
    m=min(m,s)
print(m)

Submission status: Accepted

Solution code:
import math

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

m = 9999999999
for i in range(1, 101):
	c = sum([(x - i) ** 2 for x in XS])
	if m > c:
		m = c

print(m)

Submission status: Accepted

Solution code:
n = int(input())
a = sorted(map(int, input().split()))
ans = 10000000000
for p in range(1,101):
  tmp = 0
  for j in a:
    tmp += (p-j)**2
  ans = min(tmp,ans)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
sum1 = 100*100**2
for i in range(101):
    sum2 = 0
    for j in range(len(x)):
        sum2 += (x[j] - i)**2
    if sum2 < sum1:
        sum1 = sum2
print(sum1)

Submission status: Accepted

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

for p in range(1, 100):
    sum_energy = 0
    for i in range(len(x)):
        sum_energy += (x[i] - p) ** 2
    sum_energy_list.append(sum_energy)

answer = min(sum_energy_list)

print(answer)

Submission status: Accepted

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

Submission status: Accepted

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

for x in X:
  ans = ans + (av-x)**2
  
print(ans)

Submission status: Accepted

Solution code:
def abc156c_rally():
    n = int(input())
    x = list(map(int, input().split()))
    min_x, max_x = min(x), max(x)
    best = float('inf')
    for i in range(min_x, max_x + 1):
        total = 0
        for v in x:
            total += (v - i) * (v - i)
        if best > total:
            best = total
    print(best)

abc156c_rally()

Submission status: Accepted

Solution code:
n=int(input())
x=[int(x) for x in input().split()]
ave1=sum(x)//n
ave2=ave1+1
a=0
b=0
for j in x:
  a+=(j-ave1)**2
  b+=(j-ave2)**2
print(min(a,b))

Submission status: Accepted

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

import math

X_min = math.floor(min(X))
X_max = math.ceil(max(X))

c = [0] * (X_max - X_min + 1)

for i in range(X_min, X_max+1):
  for j in range(N):
    c[int(i)-X_min] += (X[j] - i)**2
    
d = min(c)
print(d)

Submission status: Accepted

Solution code:
n = int(input())
X = list(map(int, input().split()))
mean_L = sum(X)//n
mean_R = mean_L+1
ans = min(sum(map(lambda x: (x-mean_L)**2, X)),
          sum(map(lambda x: (x-mean_R)**2, X)))
print(ans)