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

ma, mi = max(l), min(l)
if ma == mi:
  sl.append(0)

for x in range(mi, ma):
  s = 0
  for i in l:
    s += (i - x)**2 
  sl.append(s)

print(min(sl))

Submission status: Accepted

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

P_list = []
for P in range(1, max(X)+1):
    P_list.append(P)

HP_min =10**8#取りうるHPの最大値    
for i in range(len(P_list)):#Pに関する繰り返し
    HP = 0
    for j in range(N):#Xに関する繰り返し
        HP += (X[j] - P_list[i])**2#Pごとに終わらせたい
    #あるPに対しての処理 
    if HP < HP_min:
        HP_min = HP
print(HP_min)

Submission status: Accepted

Solution code:
import sys

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

if len(list(set(x))) == 1:
    print(0)
    sys.exit()

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

x_min = x[0]
x_max = x[n-1]
min_sum = 10000000
for p in range(x_min, x_max):

    sum = 0
    for e in x:
        len = (e - p)**2
        sum += len
    # print("p", p, "sum", sum, "min_sum", min_sum)
    if sum <= min_sum:
        min_sum = sum
print(min_sum)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int,input().split()))
val1 = sum(A)//n
val2 = val1+1
ans1 = 0
ans2 = 0
for a in A:
    ans1 += (a-val1)**2
    ans2 += (a-val2)**2
print(min(ans1,ans2))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


def sum_of_arithmetic_progression(s, d, n):
    return n * (2 * s + (n - 1) * d) // 2


def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


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


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

    ans = INF
    for p in range(0, 102):
        t = 0
        for x in X:
            t += (x - p) ** 2
        ans = min(ans, t)
    print(ans)

def main():
    solve()


if __name__ == '__main__':
    main()

Submission status: Accepted

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

ans = 1000000000

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

print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    X = [int(x) for x in input().split()]
    ave_x = int(sum(X) / len(X))
    error_1 = 0
    error_2 = 0
    for x in X:
        error_1 += (x-ave_x) ** 2
        error_2 += (x-(ave_x+1)) ** 2
        min_e = min(error_1, error_2)
    print(min_e)
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    X = [int(x) for x in input().split()]
    min_x = min(X)
    max_x = max(X)
    min_e = 10**6
    for i in range(min_x, max_x+1):
        error = 0
        for x in X:
            error += (x-i) ** 2
        min_e = min(min_e, error)
    print(min_e)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
x = list(map(int, input().split()))
hp_min = 10000 * n
for i in range(1, 101):
    hp = sum([(x[j] - i) ** 2 for j in range(len(x))])
    if hp < hp_min:
        hp_min = hp
print(hp_min)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
p=round(sum(A)/N)
print(sum(map(lambda x: (x-p)**2, A)))

Submission status: Accepted

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

HP = []
for p in range(1, 101, 1):
    P = [p] * len(X)
    delta = sum([(i - j) ** 2 for (i, j) in zip(X, P)])
    HP.append(delta)

print(min(HP))

Submission status: Accepted

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


n = int(input())
x = [int(i) for i in input().split()]
index = len(x) // 2
new_x = sorted(x)
p1 = - (- sum(x) // len(x))
p2 = sum(x) // len(x)
ans = [0, 0]
for i in new_x:
    ans[0] += dist(i, p1)
    ans[1] += dist(i, p2)
print(min(ans))

Submission status: Accepted

Solution code:
input()
*X,=sorted(map(int,input().split()))
print(min(sum((n-p)**2 for n in X) for p in range(X[0], X[-1]+1)))

Submission status: Accepted

Solution code:
N=int(input())
*X,=sorted(map(int,input().split()))
a=10**9
for p in range(X[0], X[-1]+1):
 a=min(a, sum((n-p)**2 for n in X))
print(a)

Submission status: Accepted

Solution code:
import math
from math import gcd
INF = float("inf")

import sys
input=sys.stdin.readline
import itertools

def main():
    n = int(input())
    l = list(map(int, input().split()))
    ans = INF
    for i in range(1,101):
        trial = 0
        for k in l:
            trial += (k-i)**2
        ans = min(ans, trial)
    print(ans)

if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
input()
l = list(map(int,input().split()))
l.sort()
ans = 1000000
n_ans = 0
for i in range(l[0],l[len(l)-1]+1):
  for j in range(len(l)):
    n_ans += (l[j]-i) ** 2
  if n_ans <= ans:
    ans = n_ans
    n_ans = 0
  else:
    n_ans = 0
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
x=list(map(int, input().strip().split()))
X=0
for i in range(n):
    X+=x[i]
XX=0
for i in range(n):
    XX+=(x[i])**2
s=X//n
t=s+1
S=n*(s**2)-2*s*X+XX
T=n*(t**2)-2*t*X+XX
print(min(S,T))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = [int(x) for x in input().split()]
print(sum([(x - round(sum(a) / n))**2 for x in a]))

Submission status: Accepted

Solution code:
from decimal import Decimal, ROUND_HALF_UP

n = int(input())
x = list(map(int,input().split()))
a = Decimal(sum(x) / n).quantize(Decimal('0'), rounding=ROUND_HALF_UP)
ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
X = sorted(list(map(int,input().split())))
ans = 10**10

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

print(ans)

Submission status: Accepted

Solution code:
# import itertools
# import math
# import sys
# import numpy as np

N = int(input())
# S = input()
# n, *a = map(int, open(0))
# N, K = map(int, input().split())
# A = list(map(int, input().split()))
# Q = list(map(int, input().split()))
# S = input()

# d = sorted(d.items(), key=lambda x:x[0]) # keyでsort

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

tots = []
for i in range(1, 101):
    tot = 0
    for j in X:
        tot += (j - i) ** 2
    tots.append(tot)

print(min(tots))

Submission status: Accepted

Solution code:
#template
def inputlist(): return [int(j) for j in input().split()]
def listinput(): return input().split()
#template
N = int(input())
X = inputlist()
ans = 10**9+7
for i in range(101):
    tmp = 0
    for j in range(N):
        tmp += (X[j]- i)**2
    ans = min(ans,tmp)
print(ans)

Submission status: Accepted

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

for i in X:
  count += (i - ave)**2

print(count)

Submission status: Accepted

Solution code:

N = int(input())
X = list(map(int, input().split()))
ans = 10e9
for i in range(0,max(X)+1):
    tmp = 0
    for j in range(N):
        tmp += (X[j]-i)**2
    if tmp < ans:
        ans = tmp
#    print(tmp)
print(ans)

Submission status: Accepted

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

X_mean = round(sum(X_list) / N)

physical_sum = sum([(i - X_mean)**2 for i in X_list])
print(physical_sum)

Submission status: Accepted

Solution code:
import sys, bisect, math, itertools, string, queue, copy
import numpy as np
import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
from fractions import gcd
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

n = inp()
x = sorted(inpl())

ans = 10**100
for p in range(1,101):
    s = 0
    for i in range(n):
        s += (x[i] - p)**2
    ans = min(ans,s)

print(ans)

Submission status: Accepted

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

for i in range(1,101):
    y=0

    for j in range(n):
        y+=(x[j]-i)**2
    ans.append(y)
print(min(ans))

Submission status: Accepted

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

P = round(sum(numbers)/N)

sum = 0
for i in range(N):
  sum += (numbers[i] - P)**2

print(sum)

Submission status: Accepted

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

X=[0]
for i in array:
    X.append(i)

INF=10000000

dp=[[INF for j in range(100+1)] for j in range(N+1)]

for i in range(100+1):
    dp[0][i]=0

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

print(ans)

Submission status: Accepted

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

for i in range(min(x),max(x)+1):
	result = 0
	for j in range(n):
		result += (x[j] - i) ** 2
	if answer == 0 or answer > result:
		answer = result
		number = i

print(answer)

Submission status: Accepted

Solution code:
import sys


inint = lambda: int(sys.stdin.readline())
inintm = lambda: map(int, sys.stdin.readline().split())
inintl = lambda: list(inintm())
instr = lambda: sys.stdin.readline()
instrm = lambda: map(str, sys.stdin.readline().split())
instrl = lambda: list(instrm())

n = inint()
X = inintl()

p = 0
min_d = 10**10
ans = 0

for i in range(1,max(X)+1):
    t_d = 0
    for x in X:
        t_d += (x-i)**2
    if t_d < min_d:
        min_d = t_d
        p = i

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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


pdlist=[]

for i in range(101):
    pd=0
    for j in X:
        pd+=(i-j)**2
    pdlist.append(pd)
    
pdlist.sort()
print(pdlist[0])

Submission status: Accepted

Solution code:
n=int(input())
man=list(map(int,input().split()))
can=[]
for i in range(1,101):
	pp=0
	for j in range(len(man)):
		bp=(man[j]-i)**2
		pp+=bp
	can.append(pp)
can.sort(key=int)
print(can[0])

Submission status: Accepted

Solution code:
a=int(input())
b=list(map(int,input().split()))
bmax=max(b)+1
bmin=min(b)
ans=1000000
for i in range(bmin,bmax,1):
  e=0
  for j in b:
    e+=(i-j)**2
  ans=min(ans,e)
print(ans)

Submission status: Accepted

Solution code:
import math

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

#  O(100N)とかになりそうだし全探索で良さげ

xmin = 10 ** 7

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

print(xmin)

Submission status: Accepted

Solution code:
N=int(input())
List = list(map(int, input().split()))
res = 1000000000000
mid = 0
for i in range(101):
  for j in range(N):
    mid += (i-List[j])**2
  res = min(res,mid)
  mid = 0
print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int,input().split()))
min_sum = 10**9
for i in range(min(l),max(l)+1):
    min_sum = min(min_sum,sum([(j-i)**2 for j in l]))

print(min_sum)

Submission status: Accepted

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

ans = 10**10
for P in range(0, 102):
    cost = 0
    for X in Xs:
        cost += (X-P)**2
    ans = min(ans, cost)

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

# from numba import njit

# from collections import Counter
# from itertools import accumulate
# import numpy as np
# from heapq import heappop,heappush
# from bisect import bisect_left

INF = pow(10,6)

# @njit
def solve(n,a):
  res = INF
  for i in range(1,101):
    res = min(res,sum(pow(i-x,2) for x in a))
  return res



def main():
  N = int(input())
  # N,M = map(int,input().split())
  a = list(map(int,input().split()))
  print(solve(N,a))
  return

if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
N = int(input())
x = []
hp = []
xa = 0
hpsum = 0

x = [int(e) for e in input().split()]
for i in range(N):
    xa += x[i]



xa = xa / N

if xa % 1 < 0.5:
    xa = int(xa / 1)
elif xa % 1 >= 0.5:
    xa = int(xa / 1 + 1)

for j in range(N):
    hp.append((x[j]-xa)*(x[j]-xa))
    hpsum += hp[j]

print(int(hpsum))

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 += (x[i]-p)**2
print(ans)

Submission status: Accepted

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

minimum = 10000000

for n in range(1,max(X)+1):
  kyori = 0
  for i in range(N):
    kyori += (X[i]-n)**2
  if kyori < minimum:
    minimum = kyori
print(minimum)

Submission status: Accepted

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

xmin = x[0]
xmax = x[-1]
ans = 10**9
for i in range(xmin, xmax+1):
    d = 0
    for j in x:
        d += (j - i)**2
    ans = min(ans, d)
print(ans)

Submission status: Accepted

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

ans = 10 ** 9
for i in range(1, 101):
    sm = 0
    for v in x:
        sm += (v - i) ** 2
    ans = min(ans, sm)
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# Generated by https://github.com/kyuridenamida/atcoder-tools
from typing import *
import collections
import functools
import itertools
import math
import sys

INF = float('inf')


def solve(N: int, X: "List[int]"):
    return min([sum([(x-i)**2 for x in X]) for i in range(1, 100+1)])


def main():

    sys.setrecursionlimit(10 ** 6)

    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]"
    print(f'{solve(N, X)}')


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

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

X = np.sort(X)
minX = X[0]
maxX = X[-1]
ans = 999999999

for i in range(minX, maxX+1, 1):
    x = (X - i) ** 2
    total = np.sum(x)
    ans = min(ans, total)

print(ans)

Submission status: Accepted

Solution code:
N,*X = map(int, open(0).read().split())
ans = float('inf')
for i in range(1,max(X)+1):
    temp = 0
    for j in range(N):
        temp += (X[j]-i) ** 2
    ans = min(ans,temp)
print(ans)

Submission status: Accepted

Solution code:
# abc156_c.py
# https://atcoder.jp/contests/abc156/tasks/abc156_c

# C - Rally /
# 実行時間制限: 2 sec / メモリ制限: 1024 MB
# 配点 : 300点

# 問題文
# 数直線上に N人の人が住んでいます。
# i番目の人が住んでいるのは座標 Xiです。
# あなたは N人全員が参加する集会を開くことを考えています。
# 集会は数直線上の任意の 整数値の座標 で開くことができ、座標 Pで集会を開くとき、
# i 番目の人は集会に参加するために (Xi−P)2の体力を消費します。
# N人が消費する体力の総和としてありえる値の最小値を求めてください。

# 制約
#     入力は全て整数である。
#     1≤N≤100
#     1≤Xi≤100

# 入力
# 入力は以下の形式で標準入力から与えられる。
# N
# X1 X2 ... XN

# 出力
# N人が消費する体力の総和としてありえる値の最小値を出力せよ。

# 入力例 1
# 2
# 1 4

# 出力例 1
# 5

# 座標 2で集会を開くとき、1 番目の人が消費する体力は (1−2)2=1、 2 番目の人が消費する体力は (4−2)2=4、よってその総和は 5 です。 
# これが 2人が消費する体力の総和としてありえる値の最小値です。
# 集会を開くことができるのは整数値の座標だけであることに注意してください。

# 入力例 2
# 7
# 14 14 2 13 56 2 37

# 出力例 2
# 2354


global FLAG_LOG
FLAG_LOG = False


def log(value):
    # FLAG_LOG = True
    # FLAG_LOG = False
    if FLAG_LOG:
        print(str(value))


def calculation(lines):
    # S = lines[0]
    N = int(lines[0])
    # N, M = list(map(int, lines[0].split()))
    values = list(map(int, lines[1].split()))
    # values = list(map(int, lines[2].split()))
    # values = list()
    # for i in range(N):
    #     values.append(int(lines[i]))
    # valueses = list()
    # for i in range(N):
    #     valueses.append(list(map(int, lines[i+1].split())))

    result = None
    for p in range(101):
        su = 0
        for n in range(N):
            su += (values[n] - p)**2
        if result is None:
            result = su
        elif result > su:
            result = su

    return [result]


# 引数を取得
def get_input_lines(lines_count):
    lines = list()
    for _ in range(lines_count):
        lines.append(input())
    return lines


# テストデータ
def get_testdata(pattern):
    if pattern == 1:
        lines_input = ['2', '1 4']
        lines_export = [5]
    if pattern == 2:
        lines_input = ['7', '14 14 2 13 56 2 37']
        lines_export = [2354]
    return lines_input, lines_export


# 動作モード判別
def get_mode():
    import sys
    args = sys.argv
    global FLAG_LOG
    if len(args) == 1:
        mode = 0
        FLAG_LOG = False
    else:
        mode = int(args[1])
        FLAG_LOG = True
    return mode


# 主処理
def main():
    import time
    started = time.time()
    mode = get_mode()
    if mode == 0:
        lines_input = get_input_lines(2)
    else:
        lines_input, lines_export = get_testdata(mode)

    lines_result = calculation(lines_input)

    for line_result in lines_result:
        print(line_result)

    # if mode > 0:
    #     print(f'lines_input=[{lines_input}]')
    #     print(f'lines_export=[{lines_export}]')
    #     print(f'lines_result=[{lines_result}]')
    #     if lines_result == lines_export:
    #         print('OK')
    #     else:
    #         print('NG')
    # finished = time.time()
    # duration = finished - started
    # print(f'duration=[{duration}]')


# 起動処理
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def Rally():
    # 入力
    N = int(input())
    X = list(map(int, input().split()))
    # リストの最大値と最小値
    max_num = max(X)
    min_num = min(X)
    # Pの座標を移動させて各座標の体力消耗を調べる
    min_sum = list()
    for P in range(min_num, max_num+1):
        sum = 0
        for i in range(N):
            sum += (X[i] - P)**2
        min_sum.append(sum)
    # 戻り値
    return min(min_sum)

result = Rally()
print(result)

Submission status: Accepted

Solution code:
n, x = int(input()), list(map(int, input().split()))
px = 0
a = 100 ** 100
for i in range(100):
  s = 0
  for j in x:
    s += (i-j) ** 2
  if a > s:
    a = s
print(a)

Submission status: Accepted

Solution code:
n = int(input())
xn = [int(num) for num in input().split()]

answer = []
for n in range(1,max(xn)+1):
  sum = 0
  for x in xn:
    sum += (x-n)**2
  answer.append(sum)

print(min(answer))

Submission status: Accepted

Solution code:
from sys import stdin
input = stdin.readline


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

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

  print(min_)


if(__name__ == '__main__'):
  main()

Submission status: Accepted

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

p = int(round(numpy.average(x)))

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

print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
import sys
import math
from functools import reduce
import itertools

n = int(stdin.readline().rstrip())
x = [int(x) for x in stdin.readline().rstrip().split()]

p = int(sum(x)/n)
q = p+1

a = sum([(i-p)**2 for i in x])
b = sum([(i-q)**2 for i in x])

print(min([a,b]))

Submission status: Accepted

Solution code:
import sys


def main():
    input = sys.stdin.buffer.readline
    n = int(input())
    x = list(map(int, input().split()))
    ans = 10 ** 10
    for p in range(1, 101):
        s = 0
        for home in x:
            s += (home - p) ** 2
        ans = min(ans, s)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import numpy as np
from math import ceil, floor
 
n = int(input())
l = [int(x) for x in input().split()]
 
c = ceil(np.array(l).mean())
f = floor(np.array(l).mean())

print(min(sum([(x-c)**2 for x in l]), sum([(x-f)**2 for x in l])))

Submission status: Accepted

Solution code:
import math


def resolve():
    import sys
    input = sys.stdin.readline
    row1 = [int(x) for x in input().rstrip().split(" ")]
    row2 = [int(x) for x in input().rstrip().split(" ")]

    n = row1[0]
    minx = min(row2)
    maxx = max(row2)

    min_sum = float('inf')
    index = -1
    for i in range(minx,maxx+1):
        tmp = sum([(x-i)**2 for x in row2])
        if tmp < min_sum:
            min_sum = tmp
    print(min_sum)







if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
X1 = list(map(int, input().split()))
ans = []
if N == 1:
    print(0)
    exit()
    
if min(X1) == max(X1):
    print(0)
    exit()
    
for p in range(min(X1), max(X1)):
    num = 0
    for i in range(N):
        num += (X1[i] - p)*(X1[i] - p)
        
    ans.append(int(num))

print(min(ans))

Submission status: Accepted

Solution code:
n = int(input())
X = list(map(int, input().split()))
for pos in range(1, 101):
    tmp = 0
    for x in X:
        tmp += pow((x - pos), 2)
    if pos == 1:
        ans = tmp
    else:
        ans = min(tmp, ans)
print(ans)

Submission status: Accepted

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

ave = sum(X) / N
ans1 = int(ave)
ans2 = int(ave) + 1

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

Submission status: Accepted

Solution code:
n = int(input())
list_ = list(map(int,input().split()))
min_ = min(list_)
max_ = max(list_)
num=[]
for a in range(min_,max_):
  _ = []
  for b in range(len(list_)):
    _.append((a - list_[b])**2)
  num.append(sum(_))
if len(list(set(list_))) == 1:
  print(0)
  exit()
print(min(num))

Submission status: Accepted

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

x.sort()

l = x[0]
r = x[-1]

res = int(1e18)
for i in range(l, r+1):
    _res = 0
    for j in x:
        _res += (i - j) * (i - j)
    if _res < res:
        res = _res

print(res)

Submission status: Accepted

Solution code:
N = int(input())
ls = list(map(int,input().split()))
sum0 = 100*100**2
for i in range(1,100):
    sum1 = 0
    for x in ls:
        sum1 += (x-i)**2 
    sum0 = min(sum0,sum1)

print(sum0)

Submission status: Accepted

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

ans = 10 ** 9
for i in range(1, 101):
    for j in range(n):
        ans = min(ans, sum(map(lambda p: (p - i) ** 2, x)))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

res = 10**7

for i in range(100):
  temp = 0
  for j in range(N):
    temp += X[j]**2 - 2*X[j]*i + i**2
  if temp < res:
    res = temp

print(res)

Submission status: Accepted

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

res2 = 10**7
for P in range(100):
    res = 0
    for x in X:
        res += (P-x-1)**2
    if res2 > res:
        res2 = res

print(res2)

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int, input().split()))
ans = float('inf')
for k in range(1, 101):
  a = 0
  for j in range(len(X)):
    a += (X[j]-k)**2
  ans = min(ans, a)
print(ans)

Submission status: Accepted

Solution code:
import numpy

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

X2 = X ** 2

S1 = numpy.sum(X)
S2 = numpy.sum(X2)

ans = []
for i in range(-3, 3):
    P = round(S1 / N) + i
    ans.append(S2 - 2 * S1 * P + N * P ** 2)

print(int(min(ans)))

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    X = [int(x) for x in input().split()]
    avgX = round(sum(X) / len(X))
    diff = 0
    for i in range(N):
        diff += (X[i] - avgX)**2
    print(diff)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N=int(input())
L=list(map(int,input().split()))
S=[]
for i in range(100):
  s=0
  for j in L:
    s+=(j-i)**2
  S.append(s)
S=sorted(S)
print(S[0])

Submission status: Accepted

Solution code:
n = int(input())
X = list(map(int, input().split()))
ans = 99999999999
for i in range(1, max(X)+1):
    tmp = sum(list(map(lambda x:(x-i)**2, X)))
    ans = min(tmp, ans)

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

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

Y = [0 for x in range(100+1)]
for i in range(1, 100+1):
    Y[i] = 0
    for x in X:
        Y[i] += (i - x) ** 2

print(min(Y[1:100+1]))

Submission status: Accepted

Solution code:
import numpy as np

def Rally(n, array):
    min_ = max(np.array(array)*10**7)
    for i in range(1,max(np.array(array)+1)):
        if min_ > sum((np.array(array)-i)**2):
            min_ = sum((np.array(array)-i)**2)
    return min_

n = int(input())
array = list(map(int,input().split()))
print(Rally(n, array))

Submission status: Accepted

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

hplist = []

for i in range(min(x),max(x)+1):
    hp = 0
    for j in range(n):
        hp += (x[j] - i)**2
    hplist += [hp]

print(min(hplist))

Submission status: Accepted

Solution code:
import bisect,collections,copy,heapq,itertools,math,string
import numpy as np
import sys
sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())

N = I()
X = LI()

X_np = np.array(X)

costs = []
for p in range(1,101):
    costs.append(np.sum((X_np - p)**2))
ans = min(costs)
print(ans)

Submission status: Accepted

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

if N == 1:
    print(0)
else:
   for i in range(min(li),max(li)+1):
       cost_sum = 0
       for t in range(N):
           cost_sum += (li[t]-i) ** 2
       li_2.append(cost_sum)
   print(min(li_2))

Submission status: Accepted

Solution code:
def my_round(x):
  t=str(int(x*10))
  s=len(t)
  if int(t[s-1])>4:
    return int(x)+1
  else:
    return int(x)
N=int(input())
X=list(map(int,input().split()))
ave=sum(X)/N
P=my_round(ave)
ans=0
for y in range(N):
  ans += (X[y]-P)**2
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import collections as cl
import sys


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 main():
    N = II()
    X = LI()

    ans = 10 ** 9

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

        if sum_temp <= ans:
            ans = sum_temp
    print(ans)


main()

Submission status: Accepted

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

ans = 99999999999

for i in range(100):
    cnt = 0

    for j in range(n):
        cnt += (x[j] - (i + 1)) ** 2

    ans = min(ans, cnt)

print(ans)

Submission status: Accepted

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

p = -1
if S % N == 0:
    p = S // N
    print(sum([(p-i)**2 for i in X]))
else:
    cand1 = (S+N) // N
    cand2 = S // N
    p1 = sum([(cand1-i)**2 for i in X])
    p2 = sum([(cand2-i)**2 for i in X])

    if p1 < p2:
        print(p1)
    else:
        print(p2)

Submission status: Accepted

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

temp = 0
ans = []
for i in range(1,101):
    for j in range(N):
        temp += (X[j] - i) ** 2
        #print(temp)
    ans.append(temp)
    temp = 0
print(min(ans))

Submission status: Accepted

Solution code:
N = int(input())
X = list(map(int,input().split()))
P = int(sum(X) / N + 0.5)
ans = 0

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

print(ans)

Submission status: Accepted

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

ans=100000000000

for x in range(1,101):
  ans1=0
  for y in range(len(a)):
    ans1+=(x-a[y])**2
    
  ans=min(ans,ans1)
  
print(ans)

Submission status: Accepted

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

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

print(min(ans))