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())))

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

X_max = max(X)

answer = 99999999999
for P in range(1, X_max + 1):
    total = 0
    for Xi in X:
        total += distance(Xi, P)
    answer = min(total, answer)

print(answer)

Submission status: Accepted

Solution code:
N = int(input())

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

min_ans = 100000000

for p in range(101):
    ans_cand = 0
    for i in range(N):
        ans_cand += (p-X_l[i])**2
    if ans_cand < min_ans:
        min_ans = ans_cand

print(min_ans)

Submission status: Accepted

Solution code:

def test_coordinates(arr, coord):
    return sum([(x - coord) * (x - coord) for x in arr])


N = int(input())

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

for i in range(1, max(coordinates)+1):
    costs.append(test_coordinates(coordinates, i))
print(min(costs))

Submission status: Accepted

Solution code:

#S = (input())
N = int(input())
#N, M = map(int, input().split())
#A = list(map(lambda x: int(x)*(-1), input().split()))
X = list(map(int, input().split()))
#C = [list(map(int, input().split())) for i in range()]

final_ans = 100000000000000000000
for p in range(1,101):
    ans = 0
    for x in X:
        ans += (x-p)**2
    if final_ans > ans:
        final_ans = ans
print(final_ans)

Submission status: Accepted

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

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

  
  A.append(ans)
  ans = 0
  
print(min(A))

Submission status: Accepted

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

sum = 0
ans = 0

round=lambda x:(x*2+1)//2

for i in range(n):
    sum += x[i]

avg = round(sum/n)

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

print(int(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int, input().split()))
min = float('inf') 
sum = 0
for i in range(1, 100):
    for j in range(n):
        sum += (l[j] - i) * (l[j] - i)
    if min > sum:
        min = sum
    sum = 0
print(min)

Submission status: Accepted

Solution code:
n= int(input())
spot = input().split()
ans = []
for i in spot:
    spot.append(int(spot[0]))
    del spot[0]
if n == 1:
    print(0)
else:
    if max(spot)==min(spot):
        ans.append(0)
    for s in range(min(spot),max(spot)):
        x = 0
        for t in spot:
            x += (s-t)**2
        ans.append(x)
if n != 1:
    ans.sort(reverse=False)
    print(ans[0])

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    n=int(input())
    X=[int(_) for _ in input().split()]
    sum_x=sum(X)
    ans=0
    if sum_x/n-sum_x//n<0.5:p=sum_x//n
    else:p=sum_x//n+1
    for i in range(n):
        ans+=(X[i]-p)**2
    print(ans)

if __name__=='__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
a=int(input())
b=list(map(int,input().split()))
min,max=1000000,0
for i in range(101):
  for ii in range(a):
    max+=(b[ii]-i)*(b[ii]-i)
  if(max<min):
    min=max
    max=0
print(min)

Submission status: Accepted

Solution code:
import numpy as np
n=int(input())
x=list(map(int, input().split()))
x=np.array(x)
fatigue=[]
for i in range(1,max(x)+1):
    y=(x-i)**2
    fatigue.append(sum(y))

print(min(fatigue))

Submission status: Accepted

Solution code:
#C

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

import numpy as np

X = np.array(list(X))

min_cost = np.inf
for n in range(1, 101):
  cost = np.sum(np.square(X-n))
  if cost < min_cost:
    min_cost = cost
print(min_cost)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

N = int(readline().rstrip())
X = list(map(int, readline().split()))

lower = min(X)
upper = max(X)

ans = float("inf")
for P in range(lower, upper+1):
    dis = 0
    for x in X:
        dis += abs(P - x) ** 2
    ans = min(ans, dis)
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
X=list(map(int,input().split()))
ave=sum(X)//N
a=[(X[i]-ave)**2 for i in range(N)]
b=[(X[i]-ave-1)**2 for i in range(N)]
print(min(sum(a),sum(b)))

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
import numpy as np

N = INT()
X = LIST()
SUM = np.zeros(max(X)-min(X)+1)
SUM = list(SUM)

for i in range(N):
  for j in range(min(X), max(X)+1):
    SUM[j-min(X)] += (j-X[i])**2
    
print(int(min(SUM)))

Submission status: Accepted

Solution code:
n=int(input())
a=input().split(" ")
num=[]
sum_list=[]
for i in range(n):
    num.append(int(a[i]))

num=sorted(num)

for i in range(num[0],num[n-1]+1):
    sum=0
    for j in range(n):
        dis=(num[j]-i)**2
        sum+=dis
    sum_list.append(sum)
sum_list=sorted(sum_list)
print(sum_list[0])

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def debug(*args):
    if debugmode:
        print(*args)
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 0, 1, -1, 1, -1, -1, 1]
dy = [1, -1, 0, 0, 1, -1, 1, -1]
debugmode = True

n = INT()
x = LIST()
ans = inf
for p in range(1, 101):
    tmp2 = 0
    for xx in x:
        tmp2 += (xx - p) ** 2
    if ans > tmp2:
        ans = tmp2
print(ans)

Submission status: Accepted

Solution code:
from math import floor,ceil,sqrt,factorial,log
from collections import Counter, deque
def S(): return input()
def I(): return int(input())
def MS(): return map(str,input().split())
def MI(): return map(int,input().split())
def LS(): return list(MS())
def LI(): return list(MI())
def LLS(): return [list(map(str, l.split() )) for l in input()]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def LLSN(n: int): return [LS() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]

N = I()
X = LI()

ma = max(X)
mi = min(X)

ans = 10000000000
for i in range(mi, ma+1):
    p = i

    sum = 0
    for x in X:
        sum += pow(x-p, 2)

    if ans > sum:
        ans = sum

print(ans)

Submission status: Accepted

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

print(min(p))

Submission status: Accepted

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

from statistics import mean
M = round(mean(X))

ans = 0
for i in range(len(X)):
    ans += (X[i]-M)**2
    
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 10 ** 19
for i in range(1, 101):
  ans = min(ans, sum((x - i) ** 2 for x in X))

print(ans)

Submission status: Accepted

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


for i in range(101):
  sumX = 0
  for x in XList:
    sumX = sumX + (x - i)**2
 
  sumList.append(sumX)

print(min(sumList))

Submission status: Accepted

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

z=[]
for i in range(10**6):
    y=0
    for j in range(n):
        y+=abs(x[j]-i)**2
    z.append(y)

z=sorted(z)
print(z[0])

Submission status: Accepted

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

Submission status: Accepted

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

ans = 10**10
for i in range(input_x[0],input_x[-1]+1):
    cost_x = sum([(x-i)**2 for x in input_x])
    if cost_x < ans:
        ans = cost_x
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import numpy as np
import math

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

p1 = math.floor(np.mean(x))
p2 = math.ceil(np.mean(x))
ans1 = 0 
ans2 = 0 
for i in x:
    ans1 += (i-p1)**2
    ans2 += (i-p2)**2

print(min(ans1, ans2))

Submission status: Accepted

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

Submission status: Accepted

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

minAns=100*100*100+1

for p in range(100+1):
    tmpAns=0
    for i in range(N):
        tmpAns+=(X[i]-p)**2
    minAns=min(minAns,tmpAns)

print(minAns)

Submission status: Accepted

Solution code:
import os
import math

def main():
    num = int(input())
    x_list = list(map(int, input().split()))
    min_num = 10**10    
    for i in range(100):
        result = 0  
        for j in x_list:
            result += pow((i - j), 2)
        
        if result < min_num:
            min_num = result
    print(min_num)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

min = 10**8

for i in range(100):
    de = 0
    for j in X:
        de += (j-i)**2
    if de < min:
        min = de
print(min)

Submission status: Accepted

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

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

Submission status: Accepted

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

for i in range(100):
    for j in range(N):
        power[i] += (X[j]-i)**2

print(min(power))

Submission status: Accepted

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

mean_a = int(sum(h)/len(h))
mean_b = int(sum(h)/len(h))+1
#print(mean)
dist_a = 0
dist_b = 0
for i in range(len(h)):
    dist_a += (h[i]-mean_a)**2
    dist_b += (h[i]-mean_b)**2

print(min(dist_a,dist_b))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

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

ans = float('inf')
for p in range(1, 101):
    tairyoku = 0
    for i in range(N):
        tairyoku += (p-X[i])**2
    ans = min(tairyoku, ans)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

for j in range(min(x), max(x)+1):
    temp = 0
    for t in x:
        temp += (j - t)**2
    P.append(temp)

print(min(P))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = input()
ls = list(map(int, input().split()))
mean = round(sum(ls)/len(ls))
cost = 0
for i in ls:
        cost += (abs(mean - i) ** 2)
print(cost)

Submission status: Accepted

Solution code:
# coding: utf-8
 
a = int(input())
b=list(map(int,input().split()))
sum = 0
 
for i in range(a):
    sum += b[i]
  
ans_pre = sum/a
ans = round(ans_pre)

sum = 0 

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

print(sum)

Submission status: Accepted

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

S = []
for i in range(max(X)+1) :
    c = 0
    for j in range(N) :
        c +=  (X[j]-i) ** 2
    S.append(c)

print(min(S))

Submission status: Accepted

Solution code:
from statistics import mean
N=int(input())
X=[int(item)for item in input().split()]
meanX = round(mean(X))
print(sum([(X_-meanX)**2 for X_ in X]))

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int, input().split()))
X2 = [i**2 for i in X]
temp1 = sum(X2)
temp2 = sum(X)
result = 0

for P in range(1, max(X) + 1):
    if P == 1:
        rsum = temp1 - 2* temp2 * P + N * P**2
    rsum = min(rsum, temp1 - 2* temp2 * P + N * P**2)

print(rsum)

Submission status: Accepted

Solution code:
from statistics import mean
import math
N=int(input())
X = [int(item) for item in input().split()]
meanX=mean(X)
underX = math.floor(meanX)
upperX = math.ceil(meanX)

underX=sum([(item-underX)**2 for item in X])
upperX=sum([(item-upperX)**2 for item in X])
print(underX if underX<upperX else upperX)

Submission status: Accepted

Solution code:
N = int(input())

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

add = []

for p in range(1000):
    psum = 0
    for i in range(N):
        power = (X[i] - p)**2
        psum = psum + power
        i = i + 1
    add.append(psum)
    
print(min(add))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

x = []

for y in input().split():
  x.append(int(y))
  
res = float('inf')

for i in range(100):
  cur = 0
  
  for y in x:
    cur += (y - i) ** 2
   
  res = min(res, cur)

print(res)

Submission status: Accepted

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


maxi = max(x)
mini = min(x)

tairyoku_1=1000000000000
tairyoku_2=0

for i in range(mini,maxi+1):
    tairyoku_2=0
    for j in x:
        tairyoku_2 += (j-i)**2
        
    tairyoku_1 = min(tairyoku_1,tairyoku_2)

print(tairyoku_1)

Submission status: Accepted

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

res = 10**9
for p in range(1,101):
    s = 0
    for x in xl:
        s += (x-p)**2
    res = min(res,s)

print(res)

Submission status: Accepted

Solution code:
n = int(input())
li = list(map(int,input().split()))
ans = 10**9
for i in range(min(li),max(li)+1):
    temp = 0
    for j in li:
        temp += (j - i)**2
    if temp < ans:
        ans = temp
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

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

minv = 1000000000000

for i in range(100):
    temp = 0

    for j in X:
        temp += (j - i) ** 2

    if minv > temp:
        minv = temp


print(minv)

Submission status: Accepted

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

X.sort()

Ans=N*(X[-1]**2)
for i in range(X[-1]+1):
    Sum=0
    for j in range(N):
        Sum=Sum+(X[j]-i)**2
    if Sum<Ans:
        Ans=Sum

print(Ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
X = np.array(list(map(int, input().split())))
best_score = None
for i in range(np.min(X), np.max(X)+1):
    score = np.sum(np.power(X - i, 2))
    if best_score is None:
        best_score = score
    elif best_score > score:
        best_score = score
print(best_score)

Submission status: Accepted

Solution code:
a = int(input())
b = map(int, input().split())
ans_list =[]
list_b =list(b)
if a == 0:
    ans_list.append(0)
else:
    for i in range(max(list_b)+1):
        ene_list =map(lambda x: (x-i)**2, list_b)
        sum_ene= sum(list(ene_list))
        ans_list.append(sum_ene)


print(min(ans_list))

Submission status: Accepted

Solution code:
n=int(input())
x=tuple(map(int,input().split()))
s=sum(x)
ave=s//n+(1 if s/n>s//n+0.5 else 0) 
print(sum([pow(ave-i,2) for i in x]))

Submission status: Accepted

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

mean_X = int(round(sum(X) / N))
ans = 0
for x in X:
    ans += int((x - mean_X) ** 2)
print(ans)

Submission status: Accepted

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

p = sum(x)//n
p2 = round(sum(x)/n)
ans = 0
ans2 = 0
for i in range(n):
    ans += (x[i]-p)**2
for i in range(n):
    ans2 += (x[i]-p2)**2
print(min(ans, ans2))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math 

n=int(input())
x=list(map(int,input().split()))
a1=a2=0
g1,g2=math.ceil(sum(x)/n),math.floor(sum(x)/n)
for i in x:
    a1+=(i-g1)**2
    a2+=(i-g2)**2
print (min(a1,a2))

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):
    cost = 0
    for x in X:
        cost += (x - p) * (x - p)
    ans = min(ans, cost)
print(ans)

Submission status: Accepted

Solution code:

N= int(input())
X =list(map(int,input().split()))
X.sort()
P = [i for i in range(X[0],X[-1] + 1)]
MIN=-1
for p in P:
    total=0
    for x in X:
        total+=(x - p)**2
    if(MIN == -1):
        MIN=total
    else:
        MIN=min(MIN,total)
print(MIN)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
def list_avr(list):
  total = sum(list)
  avr = round(total / len(list))
  return avr

health = 0
n = int(input())
x = list(map(int,input().split()))
avr = list_avr(x)

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

print(health)

Submission status: Accepted

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

dX = round(sum(X)/N)
e = 0
for x in X:
    e = e + (x - dX)**2

print(e)

Submission status: Accepted

Solution code:
from collections import deque

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


res = 10**100

avg = sum(xs)//N
mod = sum(xs)%N

for d in range(avg, avg+mod+1):
  r = 0
  for x in xs:
    r += (x-d) ** 2
  res = min(res, r)

print(res)

Submission status: Accepted

Solution code:
import sys
N = int(input())

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

if N == 1:
    print(0)
    sys.exit()

a.sort()

line = []

for i in range(a[0],a[N-1]+1):
    line.append(i)

def calculation(hairetu,P): #Pにはline，すなわち数直線の値を入れる
    len_hairetu = len(hairetu)
    cal_result = 0
    for i in range(0,len_hairetu):
        cal_result += (hairetu[i] - P)**2
    return cal_result


def search_area(hairetu,line):
    length = len(line)
    if length < 5:
        return line
    
    half = length//2
    f_N = half//2

    f1 = half-f_N
    f3 = half+f_N
    if calculation(hairetu,f1) < calculation(hairetu,f3):
        line = line[0:half]
        search_area(hairetu,line)
    elif calculation(hairetu,f1) > calculation(hairetu,f3):
        line = line[half:length]
        search_area(hairetu,line)
    
search_area(a,line)

min = 1000000
for i in range(0,len(line)):
    if calculation(a, line[i]) < min:
        min = calculation(a,line[i])

print(min)

Submission status: Accepted

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

for i in range(min(A),max(A)+1):
    c = 0
    for j in range(n):
        c += (A[j]-i)**2
    B.append(c)

print(min(B))

Submission status: Accepted

Solution code:
import math
import sys

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

X.sort()
ans = sys.maxsize

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

print(ans)

Submission status: Accepted

Solution code:
import math
import sys

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

X.sort()
ans = sys.maxsize

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

print(ans)

Submission status: Accepted

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

min=99999999999

for i in range(1,101):
    sum=0
    for j in a:
        sum+=(j-i) ** 2

    if sum < min: min=sum

print(min)

Submission status: Accepted

Solution code:
n = int(input())
xs = list(map(int, input().split()))
ans = 10 ** 9
for p in range(100):
    t = 0
    for x in xs:
        t += (x - p) ** 2
    ans = min(ans, t)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int,input().split()))
X.sort()
min = 10000*100
for t in range(1, 101):
    sum = 0
    for x in X:
        sum += (x - t)**2
    if sum < min: min = sum
print(min)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
printV = lambda x: print(*x, sep="\n")
printH = lambda x: print(" ".join(map(str,x)))
def IS(): return sys.stdin.readline()[:-1]
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LII(rows_number): return [II() for _ in range(rows_number)]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]

def main():
	n = II()
	x = LI()
	ans = 10**9
	for i in range(max(x)+1):
		tot=0
		for j in range(n):
			tot += (x[j]-i)**2
		ans = min(ans, tot)
	print(ans)

if __name__ == '__main__':
	main()

Submission status: Accepted

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

Submission status: Accepted

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

if X_sum % N == 0:
    div = X_sum // N
    ans = 0
    for i in range(N):
        ans += (X[i] - div) ** 2
    print(ans)
else:
    # print(X_sum / N)
    div_1 = X_sum // N
    ans_1 = 0
    for i in range(N):
        ans_1 += (X[i] - div_1) ** 2

    div_2 = X_sum // N + 1
    ans_2 = 0
    for i in range(N):
        ans_2 += (X[i] - div_2) ** 2
    print(min(ans_1, ans_2))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split(' ')))
p0 = sum(x)/n
if p0-int(p0) > 0.5:
    p = (int(p0)+1)
else:
    p = (int(p0))
ans = 0
i = 0
while i < n:
    ans += (x[i]-p)*(x[i]-p)
    i += 1
    
print(ans)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
n=int(input())
l=list(map(int,input().split()))
d=sum(l)//n
def dis(x):
    global l;ret=0
    for i in l:
        ret+=(x-i)**2

    return ret
print(min(dis(d),dis(d+1)))

Submission status: Accepted

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

avg_p = int(sum(X) / N)

cost_l = sum(map(lambda x: (x - avg_p)**2, X))
cost_r = sum(map(lambda x: (x - avg_p - 1)**2, X))

print(min(cost_l, cost_r))

Submission status: Accepted

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

p_min = min(X)
p_max = max(X)

ans = p_min
cost_min = N * p_max ** 2
for p in range(p_min, p_max + 1):
  tmp_cost = sum(map(lambda x: (x - p) ** 2, X))
  if tmp_cost < cost_min:
    ans = p
    cost_min = tmp_cost
    
print(cost_min)

Submission status: Accepted

Solution code:
import math
N= int(input())
nums = list(map(int,input().split()))
center = sum(nums) / N
center_down= math.floor(center)
center_up = math.ceil(center)

ans1  =sum([(center_down - i ) ** 2  for i in nums ])

ans2  =sum([(center_up- i ) ** 2  for i in nums ])

print(min(ans1,ans2))

Submission status: Accepted

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

for p in range(100):
  for x in X:
    E[p] += (x - (p + 1)) ** 2

print(min(E))

Submission status: Accepted

Solution code:
import math

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

ave = 0
ans1 = 0
ans2 = 0
for i in range(len(L)):
    ave += L[i]
ave = ave / len(L)
ave_under = math.floor(ave)
ave_up = math.ceil(ave)

for i in range(len(L)):
    ans1 += (L[i] - ave_under) ** 2
    ans2 += (L[i] - ave_up) ** 2
if ans2 > ans1:
    print(ans1)
else:
    print(ans2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
x_li = list(map(int,input().split()))
x_li.sort()
res = []
for x in range(x_li[0],x_li[-1]+1):
    ans = 0
    for y in x_li:
        ans+=(x-y)**2
    res.append(ans)
print(min(res))

Submission status: Accepted

Solution code:
from statistics import mean
import math
N = (map(int, input().split()))
X = list(map(int, input().split()))
A = mean(X)
A = round(A)
list =[]
for i in range(0, len(X)):
    B = X[i] - A
    B =pow(B,2)
    list.append(B)
Sum = sum(list)
print(Sum)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n = int(input()) # 入力が1つ
# map(int, input().split()) # 入力が複数
x = [int(i) for i in input().split()] # 配列で数字

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

Submission status: Accepted

Solution code:
#ABC156 - Virtual
sel='C'

#C
if sel=='C':
    def ceilT(a,b): #ceil(a/b)
        return (a+b-1)//b

    N=int(input())
    X=[int(i) for i in input().split()]
    p=ceilT(sum(X),N)
    ans=sum((x-p)**2 for x in X)
    p-=1
    ans=min(ans,sum((x-p)**2 for x in X))
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

print(min(ans))

Submission status: Accepted

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

X.sort()
s1 = sum([(X[i] - 1)**2 for i in range(N)])

c = 2
while c <= max(X):
    s2 = sum([(X[i] - c)**2 for i in range(N)])
    if s2 < s1:
        s1 = s2
    c += 1
print(s1)

Submission status: Accepted

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

point = sum(x)//n
sum1 = 0;sum2 = 0
for i in range(n):
    sum1 += (point - x[i])**2
    sum2 += (point+1 - x[i])**2
print(min(sum1,sum2))

Submission status: Accepted

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

a = round(sum(X) / len(X))

n=0

for i in X:
    n+=(int(i)-a)**2
print(n)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
max(A)
min(A)
CHE = list(range(min(A), max(A)+1))
#print(CHE)

RES = 0
PP = []

for n in CHE:
    for i in A:
        i = int(i)
        RES += (i-n)**2
    PP.append(RES)
    RES = 0
#print(min(PP))
print(min(PP))