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())
m = list(map(int, input().split()))
ans = []
for i in range(-100, 101):
    f = lambda x: (x - i)**2
    ans.append(sum(map(f, m)))
print(min(ans))

Submission status: Accepted

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

Submission status: Accepted

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

min_cos = 10**9
for i in range(-100, 101):
    tmp = 0
    for a in a_list:
        tmp += (a-i)**2
    min_cos = min(min_cos, tmp)
print(min_cos)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
fr = min(a)
to = max(a)
ans = []

for i in range(fr, to+1):
  tmp = 0
  for j in range(n):
    tmp += (i - a[j]) ** 2
  ans.append(tmp)
  
print(min(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

N = int(input())
A = list(map(int, sys.stdin.readline().rsplit()))

res = 10 ** 18
for y in range(-200, 201):
    cnt = 0
    for x in A:
        cnt += (x - y) ** 2
    res = min(res, cnt)

print(res)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
meanAs = int(sum(A)/n)
meanAb = meanAs + 1
anss = 0
ansb = 0
for a in A:
    anss += (a-meanAs)**2
    ansb += (a-meanAb)**2
print(min(anss, ansb))

Submission status: Accepted

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

ans = 10e9
mint = min(A)
maxt = max(A)

for t in range(mint, maxt + 1):
    temp = 0
    for ai in A:
        temp += pow(abs(t - ai), 2)
    if temp < ans:
        ans = temp

print(ans)

Submission status: Accepted

Solution code:
import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import *
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
# sys.setrecursionlimit(int(pow(10, 2)))
# sys.stdin = open("input.txt", "r")
# sys.stdout = open("output.txt", "w")
mod = int(pow(10, 9) + 7)
mod2 = 998244353
def data(): return sys.stdin.readline().strip()
def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var))+end)
def l(): return list(sp())
def sl(): return list(ssp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]
# @lru_cache(None)






N=l()[0]
A=l()
ans=10**10
for i in range(-100,101):
    x=sum([(ele-i)**2 for ele in A])
    ans=min(ans,x)
print(ans)

Submission status: Accepted

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

ave=round(sum(a)/n)

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

print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

avg = round(sum(a)/n)

print(sum([(i-avg)**2 for i in a]))

Submission status: Accepted

Solution code:
def findCost(arr, middle):
    cost = 0
    for i in range(len(arr)):
        cost += (arr[i]-middle)*(arr[i]-middle)
    return cost

def answer(arr):
    middle = sum(arr)//len(arr)
    cost1 = findCost(arr, middle)
    cost2 = findCost(arr, middle+1)
    if cost1 < cost2:
        return cost1
    else:
        return cost2

n = int(input())
arr = list(map(int, input().split()))
print(answer(arr))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
ans = float("inf")
for i in range(-100, 101):
    ans = min(ans, sum(map(lambda x: (x - i) * (x - i), A)))
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):
    ans = min(ans, sum(map(lambda x: (x - i) * (x - i), A)))
print(ans)

Submission status: Accepted

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

a.sort()
ans = 1 << 60
for x in range(min(a), max(a)+1) :
  res = 0
  for j in range(n) :
    res += abs(x - a[j])**2
  ans = min(ans, res)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

a = min(la)
z = max(la)

ld = list()
for i in range(a, z+1):
    d = 0
    for j in range(n):
        d += (la[j] - i)**2
    ld.append(d)

print(min(ld))

Submission status: Accepted

Solution code:
from decimal import Decimal, ROUND_HALF_UP

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

m = Decimal(sum(As)/N).quantize(Decimal('0'), rounding=ROUND_HALF_UP)

rlt = 0

for i in As:
  rlt += (i - m)**2

print(rlt)

Submission status: Accepted

Solution code:
import sys

n = int(input())
a = list(map(int,input().split()))
s = ((sum(a))//n)+1
t = sum(a)//n
u = ((sum(a))//n)-1
l = []
l.append(s)
l.append(t)
l.append(u)
ans = []
ans2 = []
aa = set(a)

if len(aa) == 1:
    print(0)
    sys.exit()

for j in range(len(l)):
    ans = []
    for i in range(n):
        if a[i] == l[j]:
            continue
        else:
            b = (a[i]-l[j])**2
            ans.append(b)
    ans2.append(sum(ans))


print(min(ans2))

Submission status: Accepted

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

c_min = min(A)
c_max = max(A)
ans = []
for i in range(c_min,c_max):
    temp_ans = 0
    for a in A:
        temp_ans += (i-a)**2
    ans.append(temp_ans)

if ans:
    print(min(ans))
else:
    print("0")

Submission status: Accepted

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

ans = 10**9
for i in range(-100, 101):
    cost = sum(map(lambda a: (a - i)**2, A))
    ans = min(cost, ans)
print(ans)

Submission status: Accepted

Solution code:
def myAnswer(N:int,A:list) -> int:
   av = sum(A)//N
   tmp1 = 0
   tmp2 = 0
   tmp3 = 0
   for a in A:
      tmp1 += (a - av)** 2
      tmp2 += (a - (av + 1)) ** 2
      tmp3 += (a - av - 1) ** 2
   return min(tmp1,tmp2,tmp3)

def modelAnswer():
   return
def main():
   N = int(input())
   A = list(map(int,input().split()))
   print(myAnswer(N,A))
if __name__ == '__main__':
   main()

Submission status: Accepted

Solution code:
#3問目
import math
N = int(input())
a = list(map(int, input().split()))
averageCeil = math.ceil(sum(a) / N)
averageFloor = math.floor(sum(a) / N)
costCeil = 0
costFloor = 0
#コスト計算
for i in range(N):
    costCeil += (a[i] - averageCeil) ** 2
    costFloor += (a[i] - averageFloor) ** 2
print(min(costCeil,costFloor))

Submission status: Accepted

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

d = round(sum(A)/n)
s = 0
for a in A:
    s += (a - d)**2
print(s)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

def solve():
    N = int(input())
    a = list(map(int, input().split()))
    
    m = np.mean(a)
    m_ceil = np.ceil(m)
    m_floor = np.floor(m)
    
    ans1 = 0
    ans2 = 0
    for ai in a:
        ans1 += (ai - m_ceil) ** 2
        ans2 += (ai - m_floor) ** 2
    
    return int(min(ans1, ans2))

print(solve())

Submission status: Accepted

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

ans = np.inf
for i in range(-100, 101):
    ans = np.min([ans, ((a - i)**2).sum()])

print(int(ans))

Submission status: Accepted

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

ans = np.sum((a - 100) ** 2)

for c in range(-100, 100):
    ans = min(np.sum((a - c) ** 2), ans)

print(int(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
*A,=map(int,input().split())

ans=[]
i=min(A)
while i<=max(A):
    ans.append(sum((j-i)**2 for j in A))
    i+=1

print(min(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 += (a[j]-i)**2
  ans = min(tmp,ans)
  
print(ans)

Submission status: Accepted

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

num = round(sum(A)/N)

ans = 0

for a in A:
  ans += (a-num)**2

print(ans)

Submission status: Accepted

Solution code:
import sys
## io ##
def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
def MIIZ(): return list(map(lambda x: x-1, MII()))
## dp ##
def DD2(d1,d2,init=0): return [[init]*d2 for _ in range(d1)]
def DD3(d1,d2,d3,init=0): return [DD2(d2,d3,init) for _ in range(d1)]
## math ##
def divc(x,y) -> int: return -(-x//y)
def divf(x,y) -> int: return x//y
def gcd(x,y):
    while y: x,y = y,x%y
    return x
def lcm(x,y): return x*y//gcd(x,y)
def enumerate_divs(n):
    """Return a tuple list of divisor of n"""
    return [(i,n//i) for i in range(1,int(n**0.5)+1) if n%i==0]
def get_primes(n=10**3):
    """Return a list of prime numbers n or less"""
    is_prime = [True]*(n+1)
    is_prime[0] = is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i*2, n+1, i): is_prime[j] = False
    return [i for i in range(n+1) if is_prime[i]]
def prime_factor(n):
    """Return a list of prime factorization numbers of n"""
    res = []
    for i in range(2,int(n**0.5)+1):
        while n%i==0: res.append(i); n //= i
    if n != 1: res.append(n)
    return res
## const ##
MOD=10**9+7
## libs ##
import itertools as it
import functools as ft
from collections import deque, Counter
from heapq import heapify, heappop, heappush
from bisect import bisect_left
#======================================================#
def main():
    import sys
## io ##
def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
def MIIZ(): return list(map(lambda x: x-1, MII()))
## dp ##
def DD2(d1,d2,init=0): return [[init]*d2 for _ in range(d1)]
def DD3(d1,d2,d3,init=0): return [DD2(d2,d3,init) for _ in range(d1)]
## math ##
def divc(x,y) -> int: return -(-x//y)
def divf(x,y) -> int: return x//y
def gcd(x,y):
    while y: x,y = y,x%y
    return x
def lcm(x,y): return x*y//gcd(x,y)
def enumerate_divs(n):
    """Return a tuple list of divisor of n"""
    return [(i,n//i) for i in range(1,int(n**0.5)+1) if n%i==0]
def get_primes(n=10**3):
    """Return a list of prime numbers n or less"""
    is_prime = [True]*(n+1)
    is_prime[0] = is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i*2, n+1, i): is_prime[j] = False
    return [i for i in range(n+1) if is_prime[i]]
def prime_factor(n):
    """Return a list of prime factorization numbers of n"""
    res = []
    for i in range(2,int(n**0.5)+1):
        while n%i==0: res.append(i); n //= i
    if n != 1: res.append(n)
    return res
## const ##
MOD=10**9+7
## libs ##
import itertools as it
import functools as ft
from collections import deque, Counter
from heapq import heapify, heappop, heappush
from bisect import bisect_left
#======================================================#
def main():
    n = II()
    aa = MII()
    minc = 10**9
    for i in range(-100, 101):
        minc = min(minc, (sum((a-i)**2 for a in aa)))
    print(minc)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
S = sum(A)
m = S // N
C = [
  sum((A[i] - m)**2 for i in range(N)), 
  sum((A[i] - m - 1)**2 for i in range(N)),
  sum((A[i] - m + 1)**2 for i in range(N))
]
print(min(C))

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:

def main():
    n = int(input())
    a_s = list(map(int,input().split()))
    heikin = round(sum(a_s) / len(a_s))
    counter = 0
    for a in a_s:
        counter += abs(heikin-a)**2
    print(counter)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
a = int(input())
ar = list(map(int,input().split(" ")))
b = 10 ** 9
for i in range(min(ar),max(ar) + 1):
    count = 0
    for r in ar:
        count += (r - i) ** 2
    if b > count:
        b = count
print(b)

Submission status: Accepted

Solution code:
import numpy as np

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

A_sum = np.inf
if N == 1:
    print(0)
else:
    for x in range(min(A), max(A)+1):
        A_sum_new = sum([(a - x)**2 for a in A])
        if A_sum_new <= A_sum:
            A_sum = A_sum_new

    print(A_sum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
a.sort()
s = sum(a)**2
k = 0
for i in range(a[0], a[-1]+1):
  if (N*i-sum(a))**2 < s:
    s = (N*i-sum(a))**2
    k = i
    
l = 0    
for j in range(N):
  l = l + (k-a[j])**2
      
print(l)

Submission status: Accepted

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

ans = 1e+15
for i in range(min(a), max(a)+1):
  total = [(ai - i)**2 for ai in a]
  ans = min(ans, sum(total))
  
print(ans)

Submission status: Accepted

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

mi = 10**10

for i in range(-100,101):
    point = 0
    for j in li:
        point += (i-j)**2
    mi = min(mi,point)

print(mi)

Submission status: Accepted

Solution code:
from math import ceil, floor
N = int(input())
A = list(map(int, input().split()))
average = sum(A) / N
floor_A = floor(average)
ceil_A = ceil(average)
floor_sum = 0
ceil_sum = 0
for a in A:
    floor_sum += (a - floor_A) ** 2
    ceil_sum += (a - ceil_A) ** 2
print(min(ceil_sum, floor_sum))

Submission status: Accepted

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

ans = 10**18
for a in range(min(A), max(A) + 1):
    cost = 0
    for b in A:
        cost += (a - b)**2
    ans = min(ans, cost)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int,input().split()))
sa = sum(A)
nya = (sa+sa%n)//n
ans = 0
for i in range(n):
    ans += pow(abs(nya - A[i]),2)
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
def main():
    #入力
    readline=stdin.readline
    n=int(readline())
    a=list(map(int,readline().split()))

    ans=float("inf")
    for x in range(-100,101):
        cnt=0
        for i in range(n):
            cnt+=(a[i]-x)**2
        ans=min(ans,cnt)

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

Submission status: Accepted

Solution code:
def cost(x,y):
    return (x-y)**2
N = int(input())
A = sorted(list(map(int,input().split())))
cmin = 10**8
for a in range(A[0],A[-1]+1):
    tot = 0
    for i in range(N):
        tot += cost(a,A[i])
    cmin = min(cmin,tot)
print(cmin)

Submission status: Accepted

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

m = sum(a)/n
if int(m) == m:
  ans = 0
  for v in a:
    ans += (int(m)-v)**2
  print(ans)
else:
  x = int(m)
  y = int(m)+1
  X,Y = 0,0
  for v in a:
    X += (x-v)**2
    Y += (y-v)**2
  print(min(X,Y))

Submission status: Accepted

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

MY_INF = 10 ** 8
ans = MY_INF
for dst in range(min(a), max(a) + 1):
    ans_tmp = 0
    for i, ai in enumerate(a):
        ans_tmp += abs(dst - ai) ** 2
    #
    ans = min(ans, ans_tmp)

print(ans)

Submission status: Accepted

Solution code:
N, *A = map(int, open(0).read().split())

ans = 10**10
for i in range(-100, 101):
    sn = sum((a-i)*(a-i) for a in A)
    if sn < ans:
        ans = sn
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int,input().split()))
ans = 10**12
for k in range(-100,101):
    temp = 0
    for e in a:
        temp += (k-e)**2
    ans = min(ans,temp)
print(ans)

Submission status: Accepted

Solution code:
# ARC059 C いっしょ

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

ave = ((sum(a_list) / len(a_list)) * 2 + 1) // 2

ans = sum([(x-ave)**2 for x in a_list])

print(int(ans))

Submission status: Accepted

Solution code:
from statistics import mean
import math

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

mean_num = mean(A)
Ceil = math.ceil(mean_num)
Floor = math.floor(mean_num)

Ceil_sum = 0
Floor_sum = 0
for i in A:
    Ceil_sum += (Ceil - i) ** 2
for i in A:
    Floor_sum += (Floor - i) ** 2

print(min(Floor_sum, Ceil_sum))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
sum = 0
for k in A:
    sum += k
x = int(sum/N)

var1 = 0
for i in A:
    var1 += (i-x)*(i-x)

var2 =0
for j in A :
    var2 += (j-x-1)*(j-x-1)

print(min(var1,var2))

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 range(n):
        cost += (A[j]-i)**2
    ans = min(ans,cost)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a_list = list(map(int,input().split()))
answers = set()
ans = 0
for i in range(-100,101):
    for j in a_list:
        ans += (i-j)**2
    answers.add(ans)
    ans = 0


print(min(answers))

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    N = int(readline())
    A = list(map(int, readline().split()))
    mean = round(sum(A)/N)
    cost = 0
    for i in range(N):
        cost += (A[i]-mean)**2
    print(cost)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from math import floor, ceil
n = int(input())
data = list(map(int, input().split()))
avg = sum(data)/float(n)
y1 = floor(avg)
y2 = ceil(avg)
f = sum((x-y1)**2 for x in data)
c = sum((x-y2)**2 for x in data)
print(min(f, c))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)
# input = sys.stdin.readline    ####
int1 = lambda x: int(x) - 1
def II(): return int(input())

def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())

def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]

def printlist(lst, k='\n'): print(k.join(list(map(str, lst))))
INF = float('inf')

def round(x,d=0):
    p = 10**d
    return (x * p * 2 + 1) // 2 / p

def solve():
    n = II()
    A = LI()

    ave = int(round(sum(A) / n))

    cost = 0
    for a in A:
        cost += pow(a-ave, 2)
    print(cost)




if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import sys
import re
import math
import collections
import decimal
import bisect
import itertools
import fractions
import functools
import copy
import heapq
import decimal
import statistics

sys.setrecursionlimit(10000001)
INF = sys.maxsize
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===
def main():
    n = ni()
    a = na()

    ave = sum(a) / n
    r = decimal.Decimal(str(ave)).quantize(decimal.Decimal("0"), rounding=decimal.ROUND_HALF_UP)
    ave = int(r)

    ans = 0
    for ai in a:
        ans += (ai - ave) ** 2

    print(int(ans))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import heapq
n=int(input())
a=list(map(int, input().split()))
l=[]
for i in range(-100,101):
    s=0
    for j in range(n):
        s+=(a[j]-i)**2
    heapq.heappush(l,s)
s=heapq.heappop(l)
print(s)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import factorial,sqrt,ceil#,gcd
import numpy as np

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

mean = np.mean(arrA)

targetnum = round(mean)

arrans = (arrA - targetnum)**2
ans = sum(arrans)

print(int(ans))

Submission status: Accepted

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

ans = 10**9

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

print(ans)

Submission status: Accepted

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

m = math.floor(sum(a)/n+0.5)
ans = 0
for i in a:
    ans += (m-i)**2
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())
a = np.array([list(map(int, input().split()))], dtype = np.int32)
b = np.around(np.sum(a) / n)
m = a - b
print(int(np.sum(np.power(m, 2))))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
a = np.array(list(map(int, input().split())))
b = round(sum(a) / n)
m = int(sum((a - b) ** 2))
print(m)

Submission status: Accepted

Solution code:
import numpy as np

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

ans = 10 ** 18

for i in range(-100, 101):
    cost = ((A - i) ** 2).sum()
    # print(A - i, cost)
    ans = min(ans, cost)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

n = int(input())
a = list(map(int, input().split()))
m = sys.maxsize
for x in range(min(a), max(a) + 1):
    b = sum([(x - y) ** 2 for y in a])
    if m > b:
        m = b
print(m)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = 10**9

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

print(ans)

Submission status: Accepted

Solution code:
import sys

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

ans = float("inf")

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

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
S=200**2*n
for i in range(-100,101):
    s=0
    for x in A:
        s+=(x-i)**2
    S=min(s,S)
print(S)

Submission status: Accepted

Solution code:
ans=float("INF")
n=int(input())
l=list(map(int,input().split()))
for aim in range(-100,101):
    p=0
    for i in l:
        p+=(i-aim)**2
    ans=min(ans,p)
print(ans)

Submission status: Accepted

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

ans_li = [0 for i in range(min(a_input),max(a_input)+1)]
ans=float('inf')
for i in range(min(a_input),max(a_input)+1):
    cost=sum([(i-a)**2 for a in a_input])
    if cost<ans:
        ans=cost
print(ans)

Submission status: Accepted

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

print(MIN)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
a = np.array(list(map(int, input().split())))
ans = float('inf')

for x in range(-100, 101):
    temp = np.sum((x - a)**2)
    if ans > temp:
        ans = temp
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = sorted([int(i) for i in (input().split())])
if len(set(A))==1:
    print(0)
    exit()
else:
    ans =sum([(a)**2 for a in A]) 
    for i in range(min(A),max(A)+1):
        if ans > sum([(a-i)**2 for a in A]):
            ans = sum([(a-i)**2 for a in A])
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = 10**10
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:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n, *a = map(int, read().split())
ans = float('inf')
for i in range(-100, 101):
    v = 0
    for aa in a:
        v += pow(aa - i, 2)
    ans = min(ans, v)
print(ans)

Submission status: Accepted

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

temp = sum(a) // n

ans = 10 ** 9 + 7
for i in range(-1, 2):
    target = temp + i
    cost = 0
    for n in a:
        cost += (target - n) ** 2
    ans = min(ans, cost)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
total = 0
ans =float("INF")
n_l = list(map(int,input().split()))
for i in range(-100,101):
    for j in n_l:
        total += abs(i-j) ** 2
    ans = min(ans,total)
    total = 0
print(ans)

Submission status: Accepted

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

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

  m = 201
  n = 0

  ans = 0

  for value in a:
    diff = value - avg

    if(diff < m):
      m = diff
      n = value - diff

  for value in a:
    ans += (n - value) ** 2

  print(ans)


if __name__ == "__main__":
  main()

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = list(map(int,input().split()))
    res = 10**18
    for i in range(-101,101):
        tmp = 0
        for j in a:
            tmp += (j-i)**2
        res = min(res, tmp)
    print(res)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

s = float("inf")
for i in range(min(a), max(a) + 1):
    t = 0
    for j in a:
        t += (j - i) ** 2
    s = min(s, t)
print(s)

Submission status: Accepted

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

ans = 0
for num in a:
    ans += num ** 2

for i in range(-100, 101):
    cnt = 0
    for num in a:
        cnt += (i - num)**2
    
    ans = min(ans, cnt)

print(ans)

Submission status: Accepted

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

minim = 10e999999

for jj in range(201):
    j = -100+jj
    total = 0
    for i in range(n):
        total += (a[i]-j)*(a[i]-j)

    minim = min(minim,total)
print(minim)

Submission status: Accepted

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

def main():
    N = int(input())
    a = list(map(int, input().split()))
    t = round(sum(a)/N)
    ans = 0
    for i in range(N):
        ans += (a[i] - t)**2
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

res = 10**9
for x in range(-100,101):
    res = min(res, sum([(x-a)**2 for a in al]))

print(res)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


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

    ans = float("inf")
    for c in range(-100, 100+1):
        s = 0
        for _a in a:
            s += (c - _a)**2
        if s < ans:
            ans = s

    print(ans)


if __name__ == "__main__":
    main()