Problem:


Score : 200 points

Problem Statement

Evi has N integers a_1,a_2,..,a_N. His objective is to have N equal integers by transforming some of them.

He may transform each integer at most once. Transforming an integer x into another integer y costs him (x-y)^2 dollars. Even if a_i=a_j (i≠j), he has to pay the cost separately for transforming each of them (See Sample 2).

Find the minimum total cost to achieve his objective.

Constraints

1≦N≦100

-100≦a_i≦100

Input

The input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the minimum total cost to achieve Evi's objective.

Sample Input 1

2
4 8

Sample Output 1

8

Transforming the both into 6s will cost (4-6)^2+(8-6)^2=8 dollars, which is the minimum.

Sample Input 2

3
1 1 3

Sample Output 2

3

Transforming the all into 2s will cost (1-2)^2+(1-2)^2+(3-2)^2=3 dollars. Note that Evi has to pay (1-2)^2 dollar separately for transforming each of the two 1s.

Sample Input 3

3
4 2 5

Sample Output 3

5

Leaving the 4 as it is and transforming the 2 and the 5 into 4s will achieve the total cost of (2-4)^2+(5-4)^2=5 dollars, which is the minimum.

Sample Input 4

4
-100 -100 -100 -100

Sample Output 4

0

Without transforming anything, Evi's objective is already achieved. Thus, the necessary cost is 0.



Sample input:
2
4 8


Sample output:
8

Submission status: Accepted

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

ans = 10**18

for y in range(-100, 101):
    ans = min(ans, sum([(x - y) ** 2 for x in arr]))
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
a=list(map(int,input().split()))
ans=10**9
for i in range(-100,101):
  bns=0
  for j in range(N):
    bns+=(abs(a[j]-i)**2)
  ans=min(ans,bns)
print(ans)

Submission status: Accepted

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

print(min(sum(map(lambda x: (x - i) * (x - i), a)) for i in range(-100, 101)))

Submission status: Accepted

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

ans = float("INF")
for i in range(min(A), max(A)+1):
    tmp = 0
    for a in A:
        tmp += (i - a) ** 2
    ans = min(tmp, ans)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
x = 100
count1 = 0
for i in a:
    count1 += (i - x)**2

for X in range(-100, 100):
    count = 0
    for i in a:
        count += (i - X)**2
    if count < count1:
        count1 = count

print(count1)

Submission status: Accepted

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

mn = min(a)
mx = max(a)

ans = 10000000
for i in range(mn, mx+1):
    p = 0
    for j in a:
        p += (j-i) * (j-i)
    if ans > p:
        ans = p
print(ans)

Submission status: Accepted

Solution code:
#入力
N = int(input())
lst = input().split()

#整数値化
for i in range(len(lst)):
   lst[i] = int(lst[i])

#ソート
lst.sort()

#コスト計算関数
def get_cost(x, y):
   return (x - y) ** 2

costs = []

#コスト計算
for i in range(lst[0], lst[-1]): #入力値の最小値から最大値まで
   cost = 0
   for j in lst:
      cost = cost + get_cost(j, i)
   costs.append(cost)

#ソート
costs.sort()

#最小コストを出力
#入力値が全て同じとき最小も最大も同じなのでfor文の中のやつは1回も実行されない
#つまりcostsリストが空なのでcosts[0]が存在しない
if costs != []:
   print(costs[0])
else:
   print(0)

Submission status: Accepted

Solution code:
import sys

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


def main():
    n = int(readline())
    a = list(map(int, readline().split()))

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = list(map(int, input().split()))
    n_min, n_max = min(a), max(a)
    ans = []

    if n_min == n_max:
        return 0

    for y in range(n_min, n_max+1):
        cost = 0
        for x in a:
            cost += (x-y)**2
        ans.append(cost)

    return min(ans)


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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().rstrip().split(" ")))
ave = 0
ans = 0
for i in A:
    ave += i
ave /= N
ave = (ave + 0.5) // 1
for i in A:
    ans += (i - ave) ** 2
print(int(ans))

Submission status: Accepted

Solution code:
N = int(input())
a = sorted(list(map(int, input().split())))
answer = 10000000
for i in range(a[0], a[-1] + 1):
    cost = 0
    for num in a:
        cost += ((num - i) ** 2)
    answer = min(answer, cost)
print(answer)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=10**18
for num in range(-100,101):
    cnt=0
    for i in range(n):
        cnt+=(num-a[i])**2
    ans=min(ans,cnt)
print(ans)

Submission status: Accepted

Solution code:
a = int(input())
b = list(map(int,input().split()))
ans = 4000000

for i in range(-100,101):
    ans_sub = 0
    for j in range(a):
        ans_sub += (b[j]-i)**2
    ans = min(ans,ans_sub)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
min_val = None
stacks = 0

for i in range(-100, 101):
    cost_stack = 0
    for j in range(len(a)):
        cost_stack += (a[j] - i) ** 2
        
    if min_val == None:
        min_val = cost_stack
    elif cost_stack < min_val:
        min_val = cost_stack

print(min_val)

Submission status: Accepted

Solution code:
#ABC043C
n=int(input())
a = list(map(int, input().split()))
h=round(sum(a)/len(a))
sum_a=0
for i in range(len(a)):
    sum_a=sum_a+(a[i]-h)**2
print(int(sum_a))

Submission status: Accepted

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

x = sum(a) / n
y = sum(a) // n
if x == y:
   cand = [y]
else:
   cand = [y, y+1]

cost = 10**18
for ave in cand:
   c = 0
   for i in range(n):
      c += (a[i]-ave)**2
   cost = min(cost, c)

print(cost)

Submission status: Accepted

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

b = min(a)
c = max(a)
ans = []
for i in range(b, c+1):
  res = 0
  for j in a:
    res += (j - i)**2
  ans.append(res)

print(min(ans))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
C = 10 ** 9
for i in range(-100, 101):
    c = 0
    for a in A:
        c += (i - a) ** 2
    C = min(C, c)
print(C)

Submission status: Accepted

Solution code:
n=int(input())

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

ans = 10000000000

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

Submission status: Accepted

Solution code:
import math


def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def solve():
    N = read_int()
    A = read_ints()
    avg = sum(A)/N
    cost = lambda M: sum(abs(a-M)**2 for a in A)
    return min(cost(math.floor(avg)), cost(math.ceil(avg)))


if __name__ == '__main__':
    print(solve())

Submission status: Accepted

Solution code:
from statistics import mean

n = int(input())

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

m = mean(num_list)

m = round(m)

cost = 0
for num in num_list:
    cost += (num - m)**2

print(cost)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import copy

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

res = 1e+9
for i in range(min(a),max(a)+1):
    tmp = copy.copy(a)
    res = min(res,sum(map(lambda x:(x-i)**2,tmp)))
print(res)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
l = []
for i in range(min(a), max(a) + 1):
  c = 0
  for e in a:
    c += (e - i) ** 2
  l.append(c)
print(min(l))

Submission status: Accepted

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

ans = 1000000000

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

print(ans)

Submission status: Accepted

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


for i in range(-101, 101):
    ref = 0
    for j in a:
        ref += (i - j)**2
    ans = min(ans, ref)

print(ans)

Submission status: Accepted

Solution code:
import sys
import numpy as np
import random
from decimal import Decimal
import itertools
import re
import bisect
from collections import deque, Counter
from functools import lru_cache

sys.setrecursionlimit(10**9)
INF = 10**13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
def SERIES(n): return np.fromstring(sys.stdin.buffer.read(), dtype=np.int32, sep=' ')
def GRID(h,w): return np.fromstring(sys.stdin.buffer.read(), dtype=np.int32, sep=' ').reshape(h,-1)[:,:w]
def GRIDfromString(h,w): return np.frombuffer(sys.stdin.buffer.read(), 'S1').reshape(h,-1)[:,:w]
MOD = 1000000007

def main():
	n = I()
	a_list = np.array(LI())
	cost = INF
	for a in range(-100,101):
		a_diff = (a - a_list) ** 2
		cost = min(cost, np.sum(a_diff))
	print(cost)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N=int(input())
S=list(map(int,input().split()))
D=[]
for i in range(-101,102):
    goukei=0
    for j in range(N):
        goukei+=(S[j]-i)**2
    D.append(goukei)

print(min(D))

Submission status: Accepted

Solution code:
'''     ##     ##  #######  # #  ######
        ##     ##  ##   ##  ###    ##
        ##     ##  ##    #  # #    ##
        #########  #######  # #    ##    '''

import sys
import math
# sys.setrecursionlimit(10**6)

def get_ints(): return map(int, input().strip().split())
def get_list(): return list(get_ints())

def printspx(*args): return print(*args, end="")
def printsp(*args): return print(*args, end=" ")
def printchk(*args): return print(*args, end=" \ ")

MODPRIME = int(1e9+7); BABYMODPR = 998244353;

ONLINE_JUDGE = 1
if not ONLINE_JUDGE:
    sys.stdin  = open("input.txt","r")
    sys.stdout = open("output.txt","w")
    sys.stderr = open("output.txt","w")

# for _testcases_ in range(int(input())):
n = int(input())
li = get_list()
s = sum(li)/n
l = math.floor(s)
r = math.ceil(s)
if l == r:
    s = l
else:
    if s - l < r - s:
        s = l
    else:
        s = r
print(sum([(s-x)**2 for x in li]))

'''
>>> COMMENT THE STDIN!! CHANGE ONLINE JUDGE !!
THE LOGIC AND APPROACH IS BY ME @luctivud ( UDIT GUPTA )
SOME PARTS OF THE CODE HAS BEEN TAKEN FROM WEBSITES LIKE::
(I Own the code if no link is provided here or I may have missed mentioning it)
DO NOT PLAGIARISE.
TESTCASES:
>>> COMMENT THE STDIN!! CHANGE ONLINE JUDGE !!
'''

Submission status: Accepted

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

a_min = min(a)
a_max = max(a)

a_sum = []
for i in range(a_min, a_max + 1):
    num=0
    for j in range(n):
        num += (a[j] - i) ** 2
    a_sum.append(num)
print(min(a_sum))

Submission status: Accepted

Solution code:
n = int(input())
a_lst = list(map(int, input().split()))
max_ = max(a_lst)
min_ = min(a_lst)
res = 0
for num in range(min_, max_+1):
  cost = 0
  for a in a_lst:
    cost += (a - num) ** 2
  if res != 0:
    res = min(res, cost)
  else:
    res = cost
print(res)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Thu Jun 11 17:09:47 2020

@author: sd18016
"""

N = int(input())
a = [int(i) for i in input().split()]
ans = 100010001000

for i in range(-100, 101):
    total = 0
    for j in a:
        total += (j - i)**2
        
    ans = min(ans, total)
    
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

ans = 100010001000

for i in range(-100,101):
    total = 0
    for j in a:
        total += (j-i)**2
    
    ans = min(ans,total)
    
print(ans)

Submission status: Accepted

Solution code:
import os, sys, re, math

N = int(input())
a = [int(s) for s in input().split(' ')]

ret = float('inf')
for i in range(-100,101):
    s = 0
    for aj in a:
        s += (aj - i) ** 2
    ret = min(ret,s)

print(ret)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split(' ')))
ans = []
if A.count(A[0]) == len(A):
    print(0)
    exit(0)
for i in range(-100,101):
    cost = 0
    for j in range(N):
        if A[j] == i:
            continue
        else:
            cost += (A[j] - i)**2
    ans.append(cost)
import heapq
heapq.heapify(ans)
print(heapq.heappop(ans))

Submission status: Accepted

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

def minimize(list):
    min = 1000000
    for i in range(-100,101):
        num = 0
        for j in range(len(list)):
            num = num + (int(list[j]) - i)**2
        if min >= num:
            min = num
    return min

print(minimize(a))

Submission status: Accepted

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

ans = 10 ** 10
for i in range(min(a), max(a) + 1):
    val = sum([(x - i) ** 2 for x in a])
    ans = min(ans, val)

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=10000000
for i in range(-100,101):
    tmp=0
    for j in range(n):
        tmp+=(a[j]-i)**2
    if tmp<ans:
        ans=tmp
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=10000000
for i in range(-101,102):
    tmp=0
    for j in range(n):
        tmp+=(a[j]-i)**2
    if tmp<ans:
        ans=tmp
print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split(' ')))
    sum_A = sum(A)
    left, right = sum_A // N, sum_A // N + 1
    loss_1 = sum([(a - left) ** 2 for a in A])
    loss_2 = sum([(a - right) ** 2 for a in A])
    if loss_1 < loss_2:
        print(loss_1)
    else:
        print(loss_2)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

L.sort()
m = min(L)
M = max(L)

A = []
for i in range(m,M+1):
  k=0
  for j in L:
    k += (j-i)*(j-i)
  A.append(k)
  
print(min(A))

Submission status: Accepted

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

stock =[]

for x in range(-100,101):
    total = 0
    for i in range(n):
        total = total + (x-a[i])**2
    stock.append(total)


print(min(stock))

Submission status: Accepted

Solution code:
from math import ceil,floor
ansc,ansf = 0,0
n = int(input())
a = list(map(int,input().split()))
 
gp = sum(a)/len(a)
c = ceil(gp)
f = floor(gp)

for i in a:
  ansc += (i-c)**2
  ansf += (i-f)**2
  
print(min(ansc,ansf))

Submission status: Accepted

Solution code:
N = int(input())

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

mean = sum(A)//len(A)

def std_div(mean, A):
    total = 0
    for x in A:
        total += (mean-x)**2
    return total

min1 = std_div(mean,A)
min2 = std_div(mean+1,A)

ans = min(min1,min2)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
MIN = float('inf')
for i in range(-100,101):
    tmp = 0
    for a in A:
        tmp += (a-i)**2
    MIN = min(MIN,tmp)
print(MIN)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

n=I()
lis=LI()
U,V=0,0
u=ceil(sum(lis)/n)
v=int(sum(lis)/n)
for i in range(n):
    U+=(lis[i]-u)**2
    V+=(lis[i]-v)**2
print(min(U,V))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

n=I()
lis=LI()
mn=inf
for i in range(-100,100+1):
    sm=0
    for s in lis:
        sm+=(s-i)**2
    if sm<mn:
        mn=sm
print(mn)

Submission status: Accepted

Solution code:
import math

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

b = c = 0
for i in s:
    b += i
    c += i**2

if b%n == 0:
    print(int(c-b**2/n))
else:
    koho1 = math.floor(b/n)
    koho2 = math.ceil(b/n)
    ans = min(n*(koho1**2)-2*b*koho1+c,n*(koho2**2)-2*b*koho2+c)
    print(ans)

Submission status: Accepted

Solution code:
N=int(input())
a=list(map(int,input().split()))
maxi=max(a)
mini=min(a)
ans1=(maxi**2)*N
for i in range(mini,maxi+1):
    ans=0
    for j in a:
        ans+=abs(j-i)**2
    if ans<ans1:
        ans1=ans
print(ans1)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
ans = float('inf')

for target in range(-100, 101):
  cost = 0
  for j, a in enumerate(A):
    cost += (target - a)**2
  ans = min(ans, cost)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

for n in range(min(A),max(A)+1):
  cost.append(sum([(a-n)**2 for a in A]))

print(min(cost))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    A = list(map(int, input().split()))
    round = lambda x: int((x * 2 + 1) // 2)
    m = round(sum(A) / len(A))
    ans = [(a - m) ** 2 for a in A]
    print(sum(ans))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
l = [int(i)for i in input().split()]
r = float("inf")
for i in range(-100, 101):
    r = min(r, sum((n - i)**2 for n in l))
print(r)

Submission status: Accepted

Solution code:
count = int(input())
sum_n = 0
sum_list = []

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

for i in range(min(li),max(li) + 1):
  for j in li:
    sum_n += (i - j)**2
  sum_list.append(sum_n)
  sum_n = 0
    
print(min(sum_list))

Submission status: Accepted

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

val = float("inf")
for i in range(-100,101):
	new = 0
	for j in l:
		new += (i-j)**2	
	val = min(val,new)		
print(val)

Submission status: Accepted

Solution code:
n, *A = map(int, open(0).read().split())
m = round(sum(A) / n)
print(sum(map(lambda x: (x-m)**2, A)))

Submission status: Accepted

Solution code:
N = int(input())
S = list(map(int,input().split()))
ave = round(sum(S) / len(S))
ans = 0
for i in S:
    if i == ave:
        continue
    else:
       ans += (i - ave) ** 2

print(ans)

Submission status: Accepted

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


ans = 1e10
for i in range(-100,101):
    tmp = 0
    for j in a:
        tmp += (i-j)**2
    ans = min(ans,tmp)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
ans = 10**9
for i in range(-100, 101):
  tmp = 0
  for j in range(N):
    tmp += (i-A[j])**2
  ans = min(ans, tmp)
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=float("inf")
for i in range(-100,101):
    k=0
    for j in a:
        k+=(j-i)**2
    ans=min(ans,k)
print(ans)

Submission status: Accepted

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

_max = max(a)
_min = min(a)
result = 20000 * N

for i in range(_min,_max+1,1):
  _result = 0
  for j in a:
    _result += (i - int(j))**2
  if _result <= result:
    result = _result
print(result)

Submission status: Accepted

Solution code:
n = int(input())
a = sorted(list(map(int,input().split())))
sum = 0
List_sum = []
Counter = []
Count_num = (a[-1] - a[0])+1
for i in range(Count_num):
    Counter.append(a[0]+i)
    if Counter[-1] == a[-1]:
        break
if len(set(a)) == 1:
    print(0)
else:
    for i in Counter:
        for j in a:
            sum += (i-j)**2
        else:
            List_sum.append(sum)
            sum = 0
    else:
        print(min(List_sum))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
ave = sum(a)//n
ave1 = ave + 1
ans = 0
ans1 = 0
for i in range(n):
    ans += (a[i] - ave)**2
    ans1 += (a[i] - ave1)**2
print(min(ans, ans1))

Submission status: Accepted

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

min_answer=float("inf")
for i in range(min(alist),max(alist)+1):
  answer=0
  for a in alist:
    answer+=(a-i)**2
    
  min_answer=min(answer,min_answer)
print(min_answer)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()

from collections import defaultdict, deque, Counter
from sys import exit
import heapq
import math
import fractions
import copy
from itertools import permutations
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7

N = getN()
A = getList()

ans = float('inf')
for i in range(-100, 101):
    opt = 0
    for k in range(N):
        opt += (A[k] - i) ** 2
    ans = min(ans, opt)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
ans=10000000
for i in range(-100, 101):
    sum=0
    for a in range(len(A)):
        sum+=(A[a]-i)**2
    ans=min(ans,sum)
print(ans)

Submission status: Accepted

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)


#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####GCD#####
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

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

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i])*n**i for i in range(len(str(X))))

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############
N = I()
A = IL()

ans = []

for i in range(-100,101):
  ans.append(sum((a-i)**2 for a in A))

P(min(ans))

Submission status: Accepted

Solution code:
n = int(input())
an = list(map(int, input().split()))
ans = float('inf')
for i in range(-100, 101):
    check = 0
    for a in an:
        check += (a-i)**2
    ans = min(ans, check)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
mean=sum(a)//n
ans=float('inf')
for i in range(5):
    t=(mean-2)+i
    tmp=0
    for j in range(n):
        tmp+=(a[j]-t)**2
    ans=min(ans,tmp)
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=float('inf')
for i in range(-100, 101):
    tmp=0
    for j in range(n):
        tmp+=(i-a[j])**2
    ans=min(tmp,ans)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int,input().split()))
ans = float("inf")

for i in range(min(A),max(A)+1):
    cost = 0
    
    for j in A:
        cost += (j-i)**2
        
    ans = min(ans,cost)
    
print(ans)

Submission status: Accepted

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

min_cost=10000000000

for x in range(-100,101):
    i=0
    for y in a:
        i+=(y-x)**2
    min_cost=min(min_cost,i)
    
print(min_cost)

Submission status: Accepted

Solution code:

def main():
    n = int(input())
    a_s = list(map(int,input().split()))
    answer = 999999999999999999999999999999999

    for i in range(-100,101):
        temp = sum([(a-i) ** 2 for a in a_s ])
        answer = min(answer,temp)
    print(answer)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

ans = float("inf")
for y in range(-100, 101):
  res = 0
  for x in a:
    res += (x - y)**2
  ans = min(ans, res)

print(ans)

Submission status: Accepted

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


decimal = sum(a) / len(a) - sum(a) // len(a)
if decimal >= 0.5:
    m = sum(a) // len(a) + 1
else:
    m = sum(a) // len(a)

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

print(ans)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    A = list(map(int, input().split()))

    res = f_inf
    for y in range(-100, 101):
        cost = 0
        for i in range(n):
            cost += pow(abs(A[i] - y), 2)
        res = min(res, cost)
    print(res)


if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
import sys, re
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2
from collections import deque, defaultdict, Counter
from itertools import accumulate, permutations, combinations, combinations_with_replacement, 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, heapify
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

N = INT()
a = LIST()

ans = INF
for y in range(-100, 101):
	tmp = 0
	for x in a:
		tmp += (x-y)**2
	ans = min(ans, tmp)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
res = 10**9
for i in range(-100,101):
    SUM = 0
    for j in range(len(a)):
        SUM += (i-a[j])**2
    if SUM < res:
        res = SUM
print(res)