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:
import sys
N=input()
X=map(int, sys.stdin.readline().split())
X_MAX=max(X)

ans=float("inf")

for p in range(1,X_MAX+1):
    ans_kouho=0
    for x in X:
        ans_kouho+=(x-p)**2
    ans=min(ans,ans_kouho)    
print ans

Submission status: Accepted

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

num = 0
min_num = 1000000000000000000000000

for p in range(1,max(a)+1):
  for i in a:
    num += (i-p)**2
    
  if num < min_num:
    min_num = num
  
  num = 0

print(min_num)

Submission status: Accepted

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

costs = list()

for i in range(100):
    cost = 0
    for v in x:
        l = abs(v-i)
        cost += l ** 2
    costs.append(cost)
        
print(min(costs))

Submission status: Accepted

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

XM = max(X)
Xm = min(X)
ans = []
res = 0
for x in range(Xm, XM+1):
    for i in range(n):
        res += (X[i]-x)**2
    ans.append(res)
    res = 0
    
print(min(ans))

Submission status: Accepted

Solution code:
N = int(input())
X = [int(x) for x in input().split(' ')]
Pa = sum(X) // N
Pb = Pa + 1
Ca = Cb = 0
for i in range(N):
  Ca += (X[i] - Pa)**2
  Cb += (X[i] - Pb)**2
print(min(Ca, Cb))

Submission status: Accepted

Solution code:
import itertools
import sys

n = int(input())
x = list(map(int,input().split()))
ans = float('inf')
for i in range(min(x),max(x)+1):
    v = 0
    for j in x:
        v += (i-j)**2
    if v < ans:
        ans = v
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
X=list(map(int,input().split()))
S=(sum(X))/N
e=S-int(S)
if e<=0.5:
  p=int(S)
  t=0
  for i in range(N):
    t=t+(X[i]-p)**2
  print(t)
else:
  p=int(S)+1
  t=0
  for i in range(N):
    t=t+(X[i]-p)**2
  print(t)

Submission status: Accepted

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

ans = -1
for i in range(max(x)):
    tmp_ans = 0
    for j in range(n):
        tmp_ans += (x[j]-(i+1))**2
    if ans == -1:
        ans = tmp_ans
    else:
        ans = min(ans, tmp_ans)
print(ans)

Submission status: Accepted

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

X.sort()
mi = X[0]
ma = X[N-1]

ans = 10**9
for i in range(mi,ma+1):
  sum = 0
  for j in range(N):
    sum += abs(X[j]-i)**2
  ans = min(ans, sum)

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
x=[int(x) for x in input().split()]
ave=sum(x)/len(x)
if ave>=int(ave)+0.5:
    ave=int(ave)+1
else:
    ave=int(ave)
val=0
for i in x:
    val+=(ave-i)**2
print(val)

Submission status: Accepted

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


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

print(ans)

Submission status: Accepted

Solution code:
# C
N = int(input())
A = list(map(int,input().split()))
mi = min(A)
ma = max(A)
ans = 10 ** 12
for i in range(mi,ma+1):
    su = 0
    for j in A:
        su += (i-j)**2
    ans = min(ans,su)
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
sum=0
for i in range(N):
    sum = sum+A[i]
ans = 0
if sum%N <= N//2:
    for i in range(N):
        ans = ans+ (A[i]-(sum//N))**2
    print(ans)
else:
    for i in range(N):
        ans = ans+ (A[i]-1-(sum//N))**2
    print(ans)

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int,input().split()))
sum_x = sum(x)
sum_x2 = sum([i**2 for i in x])
p0 = sum_x//n
p1 = p0+1
r0 = n*p0**2 - 2*sum_x*p0 + sum_x2
r1 = n*p1**2 - 2*sum_x*p1 + sum_x2
print(min(r0,r1))

Submission status: Accepted

Solution code:
import math

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

# max_p = max(x)
# min_p = min(x)

avg = math.ceil(sum(x)/n)
avg2 = sum(x)//n

ans = min(sum([(X-avg)**2 for X in x]), sum([(X-avg2)**2 for X in x]))

print(ans)

Submission status: Accepted

Solution code:
# print('input >>')
N = int(input())
Xs = list(map(int,(input().split())))

ans = 10 ** 10
for i in range(1,101):
    mps = 0
    for X in Xs:
        mps += (X - i)**2
    ans = min(ans, mps)

# print('-----output-----')
print(ans)

Submission status: Accepted

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

ls = sorted(ls)

ans = 10000000000000
x = 0
for i in range(ls[0],ls[-1]+1):
  tmp = 0
  for a in ls:
    tmp += (a-i)**2
  if ans > tmp:
    x = i
    ans = tmp
print(ans)

Submission status: Accepted

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

wa = sum(ls)

wa2 = 0
for i in ls:
    wa2 += i ** 2

p = round(wa / n)

ans = wa2 - 2 * p * wa + n * (p ** 2)

print(ans)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    n = int(input())
    x = [int(i) for i in input().split()]
    min = -1

    for i in range (100):
        sum = 0
        i = i+1
        for j in x:
            sum =sum + (i - j) * (i - j )

        if min == -1 or min > sum:
            min = sum
    print(min)

Submission status: Accepted

Solution code:
import sys

n = int(input())

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

cofg=sum(x)//n
cofg2=cofg+1

sum=0
sum2=0
for i in range(n):
    sum+=(x[i]-cofg)**2
    sum2+=(x[i]-cofg2)**2


ans = sum if sum < sum2 else sum2



print(ans)

Submission status: Accepted

Solution code:
N = int(input())
li = list(map(int,input().split()))	
mi = 10000000000
for i in range(1,100):
    hp = 0
    for j in range(0,len(li)):
        hp += (i-li[j])**2
    if mi >= hp:
        mi = hp
print(mi)

Submission status: Accepted

Solution code:
# /usr/bin/python3
# -*- coding: utf-8 -*-
from queue import Queue
from queue import LifoQueue as Stack
from math import sqrt, floor, ceil, log2
from fractions import gcd
from itertools import permutations, combinations
from operator import itemgetter
from functools import cmp_to_key


yn = 'YNeos'
ynflg = False
cnt = 0
ans = None

def lcm(a, b):
    return (a*b) // gcd(a,b)

def intinput():
    return int(input())

def mulinputs():
    return map(int,input().split())

def lineinputs(func=intinput):
    datas = []
    while True:
        try:
            datas.append(func())
        except EOFError:
            break
    return datas



N = intinput()
xs = list(mulinputs())


l = max(xs)
total = []
m = 10**6
for p in range(l+1):
    t, larr = 0, []
    for x in xs:
        t += abs(x-p)*abs(x-p)
    if t < m:
        m = t


# 出力
print(m)

Submission status: Accepted

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

R=round(sum(X)/N)
ans=0
for i in range(N):
  ans+=(R-X[i])**2
print(ans)

Submission status: Accepted

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

INF = (100 ** 2) * 100

minD = INF

for xi in range(1, 101):
    D = 0
    for n in range(N):
        D += (xi-X[n])**2
    if D < minD:
        minD = D

print(minD)

Submission status: Accepted

Solution code:
def main():
    from sys import stdin, setrecursionlimit
    #setrecursionlimit(10**9)
    #r = input()
    #n = int(input())
    #a, b = map(int, input().split())
    #s, y = input().split()
    #a = input().split()
    #a = [int(input()) for i in range(n)]
    #a = list(map(int, input().split()))
    #a = [list(map(int, input().split())) for i in range(n)]
    #a = [int(s) - 1 for s in input().split()]
    #l=[int(input()) for _ in range(int(input()))]
    #res = 0

    n= int(input())
    x = list(map(int, input().split()))
    avr = sum(x) // len(x)
    avr2 = avr + 1
    x2 = [(i-avr)**2 for i in x]
    x3 = [(i-avr2)**2 for i in x]
    print(min(sum(x2),sum(x3)))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def num():
    return int(input())
def nums():
    return list(map(int,input().split()))
import string
numbers = "0123456789"
alphabets = string.ascii_letters # a-z+A-Zをロード
characters = numbers + alphabets
 
def base_cvt(value, n=2):
    try:
        tmp = int(value)
    except:
        raise ValueError('Invalid value:', value)
 
    if n < 2 or n > len(characters):
        raise ValueError('Invalid n:', value)
 
    result = ''
    tmp = int(value)
    while tmp >= n:
        idx = tmp%n
        result = characters[idx] + result
        tmp = int(tmp / n)
    idx = tmp%n
    result = characters[idx] + result
    return result

N = num()
X = nums()
def get_cost(l,pos):
    cost = 0
    for xi in l:
        cost += (xi - pos)**2
    return cost
costs = set()
for i in range(min(X)-1,max(X)+1):
    costs.add(get_cost(X,i))
print(min(costs))

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
hpmin = (100 ** 2) * 100
for p in range(1, 101):
    hp = 0
    for j in range(n):
        hp += (x[j] - p) ** 2
    if hp < hpmin:
        hpmin = hp
print(hpmin)

Submission status: Accepted

Solution code:
# import numpy as np
# import math
# import copy
# from collections import deque
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(10000)


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

    dp = [0] * 101

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

    res = min(dp)

    print(res)



main()

Submission status: Accepted

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

Submission status: Accepted

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

SINGLE_SUM = sum(X)
DOUBLE_SUM = sum([x ** 2 for x in X])


def f(n, p):
    return n * p ** 2 - 2 * p * SINGLE_SUM + DOUBLE_SUM


min_ = None
for p in range(min(X), max(X) + 1):
    val = f(N, p)
    if min_ is None or val < min_:
        min_ = val

print(min_)

Submission status: Accepted

Solution code:
N = int(input())
X = sorted([int(n) for n in input().split()])
ans = 10**9
for p in range(X[0], X[-1]+1):
    c = 0
    for x in X:
        c += (x-p)**2
    if c < ans:
        ans = c
print(ans)

Submission status: Accepted

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

ans = 10 ** 8
for p in range(1, 101):
    temp = 0
    for i in x:
        temp += (i - p)** 2
    if temp < ans:
        ans = temp
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

import numpy

def main():
  n = int(input())
  xs = numpy.array(list(map(int, input().strip().split())))
  min = 10**9
  for p in range(1, 101):
    cost = numpy.sum((xs - p) ** 2)
    if min > cost:
      min = cost
  print(int(min))

if __name__=='__main__':
  main()

Submission status: Accepted

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

    count=0
    result=10**10
    for p in range(101):
        count=0
        for i in range(N):
            count+=(X[i]-p)**2
        result=min(result,count)
    print(result)
if __name__=="__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

s = 0
for i in x:
    s += i

p = s//n

m = 0
mm = 0
for i in x:
    m += (i - p)**2
    mm += (i - p - 1)**2
    
print(min([m, mm]))

Submission status: Accepted

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

x_min = min(X)
x_max = max(X)

ans = 10000**10

for i in range(x_min, x_max + 1):
    
    sum_ = 0
    for xi in X:
        sum_ += (xi - i) ** 2
    
    ans = min(ans, sum_)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
x_L = list(map(int,input().split()))
x_L = list(sorted(x_L))
start = x_L[0]
end = x_L[-1]
a = 10**20
for i in range(start,end+1):
    sums = 0
    for k,val in enumerate(x_L):
        sums += (x_L[k]-i)**2
    a = min(a,sums)
print(a)

Submission status: Accepted

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

mm = 0
for x in X:
    mm += x**2

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

    if s < mm:
        mm = s

print(mm)

Submission status: Accepted

Solution code:
N=int(input())
X=list(map(int,input().split()))
ans=10000000
for i in range(1,101):
    P=i
    sums=0
    for j in range(N):
        sums+=(X[j]-P)*(X[j]-P)
    if(sums<ans):
        ans=sums
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
*x, = map(int, input().split())
ans = float("inf")
for i in range(max(x)):
    ans = min(ans, sum(map(lambda a: (a-(i+1))**2, x)))

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import numpy as np
N =int(input())
X = np.array(list(map(int,input().split())))

m = np.inf
for i in range(1,101):
  m = min( np.sum((X -i)**2),m)
  
print(int(m))

Submission status: Accepted

Solution code:
import sys


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

temp = sum(X)/N
shukai = round(temp)

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Z = 0
for i in range(N):
    Z += XList[i]

result = 0
if Z % N == 0:
    Z //= N
    for i in range(N):
        result += (XList[i] - Z) ** 2
else:
    Z //= N
    tmp = 0
    for i in range(N):
        tmp += (XList[i] - Z) ** 2
    Z += 1
    for i in range(N):
        result += (XList[i] - Z) ** 2
    if tmp < result:
        result = tmp

print(result)

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split())
b=10**8
for i in range(1,101):
    b=min(b,sum((j-i)**2 for j in a))
print(b)

Submission status: Accepted

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

x_max, x_min = max(X), min(X)

min_cost = float('inf')
for p in range(x_min, x_max+1):
    cost = 0
    for j in range(N):
        cost += (X[j] - p) ** 2
    if cost < min_cost:
        min_cost = cost

print(min_cost)

Submission status: Accepted

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

minX = min(x)
maxX = max(x)

s = [0] * (maxX + 1)
for i in range(minX, maxX+1):
    for _x in x:
        s[i] += (_x - i)**2

print(min(s[minX:]))

Submission status: Accepted

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

f_inf = float("inf")
ans = f_inf
for j in range(s, e + 1):
    res = 0
    for i in range(n):
        res += (x[i] - j) ** 2

    ans = min(ans, res)

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
p1=sum(A)//N
p2=p1+1
s1,s2=0,0
for i in A:
  s1+=(i-p1)**2
  s2+=(i-p2)**2
print(min(s1,s2))

Submission status: Accepted

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

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

Submission status: Accepted

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

ans = 100**3

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

Submission status: Accepted

Solution code:
n = int(input())
X = list(map(int, input().split()))
a = min(X)
b = max(X)

res = 10**8

for i in range(a, b + 1):
    k = 0
    for j in range(n):
        k += (i - X[j])**2
    res = min(res, k)
    
print(res)

Submission status: Accepted

Solution code:
n=int(input())
x=list(map(int,input().split()))
a=max(x)
ans=sum([(1-i)**2 for i in x])

for i in range(1,a+1):
    hp=sum([(i-j)**2 for j in x])
    if ans>=hp:
        ans=hp
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
X_list = [int(item) for item in input().split()]
res = 1000000
next_res = 10000000

for j in range(100):
    res = min(res, next_res)
    next_res = 0
    for i in range(N):
        next_res += (X_list[i] - j)**2

print(res)

Submission status: Accepted

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

ans = 100 * 100 * 100
for i in range(100):
  temp = 0
  for j in x:
    temp += pow(j - i, 2)
  if temp < ans:
    ans = temp

print(ans)

Submission status: Accepted

Solution code:
import math

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


def abs(n):
    if n<0:
        return -1*n
    else:
        return n



def sum(list):
    sum = 0
    for i in list:
        sum = sum + i
    return sum


w = sum(x_n)/n
ceil_w = math.ceil(w)

if abs(ceil_w-w) <= 0.5:
    w =ceil_w
else:
    w = math.floor(w)

energy = 0
for i in x_n:
    energy = energy + (w - i) **2


print(energy)

Submission status: Accepted

Solution code:
# ABC 156 C
from math import ceil,floor
si = lambda: input()
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nms = lambda: map(str, input().split())
nl = lambda: list(map(int, input().split()))
n=ni()
x=nl()
# x.sort()
# max_x=max(x)
# min_x=min(x)
# median_1=(max_x+min_x)//2
# median_2=ceil((max_x+min_x)//2)
# ans=0
ans=10**10
for i in range(1,101):
    t=0
    for j in range(n):
        t+=(x[j]-i)**2
    ans=min(ans,t)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

mi = 10**10
for i in range(1, 101):
    tmp = 0
    for x in xl:
        tmp += (x - i)**2
    if tmp < mi:
        mi = tmp

print(mi)

Submission status: Accepted

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

for x in X:
    s += x
    t += x ** 2

def check(k):
    return t - 2 * s * k + N * (k ** 2)

if check(math.floor(s / N)) < check(math.ceil(s / N)):
    print(check(math.floor(s / N)))
else:
    print(check(math.ceil(s / N)))

Submission status: Accepted

Solution code:
n=int(input())
x=list(map(int,input().split()))
a=sum(x)//n
b=a+1
s=[(i-a)**2 for i in x]
t=[(i-b)**2 for i in x]
print(min(sum(s),sum(t)))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
N = int(input())
X = list(map(int, input().split()))


ave= sum(X) // N
ave2 = ave+1

ans1 = 0
ans2 = 0
for x in X:
    ans1 += pow(x-ave,2)
    ans2 += pow(x-ave2,2)

print(min(ans1, ans2))

Submission status: Accepted

Solution code:
n = int(input())
a = [int(x) for x in input().split()]
mi = min(a)
ma = max(a)

dis = 10 ** 9
for idx in range(mi, ma+1):
  tmp_dis = 0
  for e in a:
    tmp_dis += (e - idx) ** 2
  dis = min(dis, tmp_dis)
print(dis)

Submission status: Accepted

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

#%%
meanX = sum(X)//len(X)

a1 = 0
for x in X:
    a1 += (x -  meanX)**2
a2 = 0
for x in X:
    a2 += (x- meanX + 1) ** 2

a3 = 0
for x in X:
    a3 += (x - meanX - 1) **2

ans = min(a1, a2, a3)
print(ans)

Submission status: Accepted

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

#残差最小化の解
x = sum(a)/N
x_large = math.ceil(x)
x_small = math.floor(x)

#大きい残差と小さい残差を計算
sum_large = 0
sum_small = 0
for i in a:
	sum_large += (x_large-i)**2
	sum_small += (x_small-i)**2
#最小残差
print(min(sum_large,sum_small))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int, input().split()))
X_ = sum(X) // N
HP1 = 0
HP2 = 0
for i in range(N):
  HP1 += (X[i] - X_)**2
  HP2 += (X[i] - X_ -1 )**2
if HP1>=HP2:
  print(HP2)
else:
  print(HP1)

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int, input().split()))
min = 99999999
tai = 0

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

print(min)

Submission status: Accepted

Solution code:
n=int(input())
x= list(map(int, input().split()))
ans=float('inf')
for i in range(1,101):
    cnt=0
    for j in range(n):
        cnt+=(x[j]-i)**2
    ans=min(cnt,ans)
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):
    sum = 0
    for j in x:
        sum += abs(j - i)**2
    ans = min(ans,sum)
    
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
x=list(map(int,input().split()))
ave1=int(sum(x)//n)
ave2=int((sum(x)//n)+1)
tol1=0
tol2=0
for i in range(n):
  tol1+=(x[i]-ave1)**2
  tol2+=(x[i]-ave2)**2
print(min(tol1,tol2))

Submission status: Accepted

Solution code:
N = int(input())
x_list = list(map(int,input().split()))
Min_pow = 100*100*100
for i in range (1,101):
    pow_sum = 0
    for j in range(N):
        pow_sum += (x_list[j]-i)*(x_list[j]-i)
    if pow_sum < Min_pow:
        Min_pow = pow_sum

print(Min_pow)

Submission status: Accepted

Solution code:
import itertools
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush,heapify
from fractions import gcd
from copy import deepcopy
import math
import queue
#import numpy as np
#import sympy as syp(素因数分解とか)
Mod = 1000000007
import sys
sys.setrecursionlimit(1000000) 
  
def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))+1):
        if prime[i] == 1:
            for j in range(2*i,n+1):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n+1):
        if prime[i] == 1:
            res.append(i)
    return res

def factorial(i):
    if i == 1:
        return 1
    else:
        return i*factorial(i-1)
 
class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[py] = px
        else:
            self.parent[px] = py
 
    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)




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

#endline===============================================
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
p1 = sum(x) // n
p2 = p1 + 1
print(min(sum((xi - p1) ** 2 for xi in x),
          sum((xi - p2) ** 2 for xi in x)))

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
m = 10 ** 8
for i in range(min(x), max(x) + 1):
    c = 0
    for j in range(n):
        c += (x[j] - i) ** 2
    if c < m:
        m = c
print(m)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

IN=lambda : map(int, input().split())
mod=1000000007

#+++++

def main():
	a = int(input())
	al = list(IN())
	
	avg = sum(al) // a
	
	mm= None
	for i in range(3):
		pp=avg + i - 1
		rr = sum([(v-pp)**2 for v in al])
		mm = rr if mm is None or mm > rr else mm
		
	ret=mm
	print(ret)
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Submission status: Accepted

Solution code:
def main():
    import sys
    sys.setrecursionlimit(10000000)
    #input = sys.stdin.readline

    from collections import Counter
    from collections import deque

    #dxy = [[0,1],[0,-1],[1,0],[-1,0]]
    #dxy = [[0,1],[0,-1],[1,0],[-1,0],[1,1],[1,-1],[-1,1],[-1,-1]]
    #MOD = 1000000007
    n = int(input())
    x = list(map(int, input().split()))
    ans = 1000000000000
    for i in range(min(x), max(x)+1):
        total = 0
        for j in x:
            total += (i-j)**2
        if ans>total:
            ans = total
    print(ans)



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# coding: utf-8
import numpy as np


def solve(*args: str) -> str:
    n = int(args[0])
    X = np.array(list(map(int, args[1].split())))

    ret = 10**10
    for i in range(1, 101):
        ret = min(ret, np.power((X-i), 2).sum())
    return str(ret)


if __name__ == "__main__":
    print(solve(*(open(0).read().splitlines())))

Submission status: Accepted

Solution code:
words = lambda t : list(map(t, input().split()))
n = int(input())
xs = words(int)
ans = -1
for i in range(1,101):
    ttl = 0
    for x in xs:
        ttl += (i - x)**2
    if ans == -1:
        ans = ttl
    #print(i, ttl)
    ans = min(ans,ttl)
print(ans)

Submission status: Accepted

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

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


M = max(l)

best = float("inf")
for i in range(M+1):
  HP = 0
  for j in l:
    HP += (j-i)**2
  if HP < best:
    best = HP


print(best)

Submission status: Accepted

Solution code:
# coding:utf-8

N = int(input())
X = [el for el in map(int, input().split(' '))]
ans = 2 ** 63 - 1

for point in range(100):
    tmp = 0
    for x in X:
        tmp += (x - point) ** 2
    if ans > tmp:
        ans = tmp

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int,input().split()))
S = [0]*n
ans = 1000000000
for j in range(101):
    for i in range(n):
        S[i] = (x[i]-j)**2
    ans = min(ans, sum(S))
print(ans)

Submission status: Accepted

Solution code:
def near(x):
    if sum(x)/len(x)-sum(x)//len(x)>=0.5:
        return sum(x)//len(x)+1
    else:
        return sum(x)//len(x)

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

s=0
for i in range(len(x)):
   s+=(x[i]-near(x))**2
print(s)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

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

xmin = min(X)
xmax = max(X)

vmin = 1e6
for i in range(xmin, xmax+1):
    v = sum((np.array(X) - i) ** 2)
    if v < vmin:
        vmin = v

print(vmin)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10000000)
INF = 1<<32


def solve(N: int, X: "List[int]"):
    X.sort()
    
    ans = INF
    for p in range(101):
        ans = min(ans, sum([abs(X[i]-p)**2 for i in range(N)]))

    print(ans)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    X = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, X)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
xs = list(map(int, input().split()))
p = round(sum(xs) / n)
print(sum([(x - p)**2 for x in xs]))

Submission status: Accepted

Solution code:
N = int(input())
X_list = list(map(int, input().split()))
res = float('inf')
for i in range(101):
    tmp = sum([(X-i)**2 for X in X_list])
    res = min(res, tmp)
print(res)

Submission status: Accepted

Solution code:
input()
X = [int(x) for x in input().split()]
P = round(sum(X)/len(X))

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

print(S)