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:
# 2020/04/25
# AtCoder Beginner Contest 059 - C

# Input
n = int(input())
a = list(map(int,input().split()))

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

# Output
print(ans)

Submission status: Accepted

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

reg = 10**12
ans = 0
for i in range(min(A), max(A)+1):
    tmp = 0
    for a in A:
        tmp += (a-i)**2
    reg = min(reg, tmp)
    if reg==tmp:
        ans = reg
print(ans)

Submission status: Accepted

Solution code:
import sys

import math

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

if all(a == A[0] for a in A):
    print(0)
    sys.exit()

cm = math.ceil(sum(A) / len(A))
fm = math.floor(sum(A) / len(A))

kouho1, kouho2 = 0, 0
for a in A:
    kouho1 += (a - cm)**2
    kouho2 += (a - fm)**2
print(min(kouho1, kouho2))

Submission status: Accepted

Solution code:
# ARC 059: C – いっしょ / Be Together
N = int(input())
a = [int(s) for s in input().split()]

costs = 1000000

for i in range(-100, 101):
    costs_tmp = 0

    for j in range(N):
        costs_tmp += (a[j] - i) ** 2
    
    costs = min(costs, costs_tmp)

print(costs)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 10**6
n = min(A)
M = max(A)
while n <= M:
  ans = min(ans, sum([(a-n)**2 for a in A]))
  n += 1
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
L=list(map(int,input().split()))
M=sum(L)//N
ans=float("inf")
tmp=0
for i in range(N):
	tmp+=(L[i]-M)**2
ans=min(ans,tmp)
tmp=0
for i in range(N):
	tmp+=(L[i]-(M+1))**2
ans=min(ans,tmp)
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
x=sum(a)//n
ans1=sum([(ai-x)**2 for ai in a])
ans2=sum([(ai-x-1)**2 for ai in a])
print(min(ans1,ans2))

Submission status: Accepted

Solution code:

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


ave=round(sum(a)*1.0/N)

cost=0


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

print(int(cost))

Submission status: Accepted

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

ans = 1000000005

for i in range(-100, 101):
    res = 0
    for a in A:
        res += (a - i) * (a - i)
    ans = min(ans, res)

print(ans)

Submission status: Accepted

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

    mi = 200 ** 2 * 100
    for y in range(-100, 100 + 1):
        t = 0
        for x in a:
            t += (x - y) ** 2
        mi = min(mi, t)

    print(mi)


if __name__ == '__main__':
    main()

# import sys
#
# sys.setrecursionlimit(10 ** 7)
#
# input = sys.stdin.readline
# rstrip()
# int(input())
# map(int, input().split())

Submission status: Accepted

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


def calc(A, i):
    res = 0
    for a in A:
        res += (a - i) ** 2
    return res

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

    ans = 10 ** 7
    for i in range(-100 ,101):
        ans = min(ans, calc(A, i))
    
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n = inp()
a = inpl()
res = INF
for i in range(-100,101):
    cnt = sum((i-x)**2 for x in a)
    res = min(res, cnt)
print(res)

Submission status: Accepted

Solution code:
# AtCoder Regular Contest 059
# C - いっしょ
# https://atcoder.jp/contests/arc059/tasks/arc059_a
N = int(input())
*A, = map(int, input().split())

mincost = 10**10
for i in range(-100, 101):
    cost = 0
    for a in A:
        cost += (a-i)*(a-i)

    if cost < mincost:
        mincost = cost

print(mincost)

Submission status: Accepted

Solution code:
from math import ceil,floor
n=int(input())
a=list(map(int,input().split()))
ave=sum(a)/n
ave1=ceil(ave)
ave2=floor(ave)
inf=10**10
ans1,ans2=0,0
for aa in a:
  ans1+=(aa-ave1)**2
  ans2+=(aa-ave2)**2
print(min(ans1,ans2))

Submission status: Accepted

Solution code:
import sys
n, *a = map(int, sys.stdin.read().split())

def main():
  x = round(sum(a) / n)
  s = sum([(x - i) ** 2 for i in a])
  print(s)
  
if __name__ == '__main__':
  main()

Submission status: Accepted

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

mean = round(statistics.mean(a))

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

print(ans)

Submission status: Accepted

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

ave = sum(a)//len(a)
ans = int(1000000000)
for i in range(ave-2, ave +3):
    cost = 0
    for j in range(n):
        cost += (a[j]-i)*(a[j]-i)
    if cost < ans:
        ans = cost
print(ans)

Submission status: Accepted

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

min_sum = 10**9
ans = 1000
for i in range(min(a), max(a)+1):
    sum = 0
    for j in a:
        sum += (j-i)**2
    if sum < min_sum:
        min_sum = sum

print(min_sum)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
y1 = sum(a) // n
y2 = y1 + 1
y3 = y1 - 1

ans = 200 ** 2 * 100
tmp1 = 0
tmp2 = 0
tmp3 = 0
for x in a:
    tmp1 += (x - y1) ** 2
    tmp2 += (x - y2) ** 2
    tmp3 += (x - y3) ** 2
ans = min(ans, tmp1, tmp2, tmp3)

print(ans)

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split())
av=sum(a)/n
ans=9e9

for i in range(int(av)-5,int(av)+5):
  s=0
  for j in a:
    s+=(j-i)**2
  ans=min(ans,s)
  
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

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


ans=np.inf
for i in range(-100,101):
  now=0
  for a in A:
    now+=(i-a)**2
  ans=min(now,ans)

print(ans)

Submission status: Accepted

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

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 ai in a:
        tmp += (ai - i) ** 2
    ans = min(ans, tmp)

print(ans)

Submission status: Accepted

Solution code:
def resolve():
    import math
    n = int(input())
    a = list(map(int, input().split()))
    ava = sum(a)/len(a)
    floor =math.floor(ava)
    ceil = math.ceil(ava)
    if abs(floor-ava) <= abs(ceil-ava):
        ava = floor
    else:
        ava = ceil
    ans = []
    for i in range(len(a)):
        ans.append((a[i]-ava)**2)
    print(sum(ans))
resolve()

Submission status: Accepted

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

mi = 10**10

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

Submission status: Accepted

Solution code:
N = int(input())
a = [int(i) for i in input().split()]
mn = min(a)
mx = max(a)
ans = 10**8
for j in range(mn,mx+1):
    costs = 0
    for i in a:
        cost = (i-j)**2
        costs += cost
    if ans>costs:
        ans = costs
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

m_a = sum(l) // N
m_b = m_a + 1

ansa = 0
for i in range(N):
  ansa += (l[i]-m_a)**2
ansb = 0
for i in range(N):
  ansb += (l[i]-m_b)**2
print(min(ansa,ansb))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import floor, ceil


def main():
    n = int(input())
    a = [int(x) for x in input().split()]
    average = sum(a) / n
    answer = [0, 0]
    for i in range(n):
        answer[0] += (a[i] - floor(average)) ** 2
        answer[1] += (a[i] - ceil(average)) ** 2
    print(min(answer))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

ans = 10 ** 18
for const in range(min(a), max(a) + 1):
    tmp = 0
    for num in a:
        tmp += (num - const) ** 2
    ans = min(ans, tmp)
    
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, *a = map(int, open(0).read().split())
c = float("inf")
for i in range(-100, 101):
  c = min(c, sum(map(lambda x: (x-i)**2, a)))
print(c)

Submission status: Accepted

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

mi=min(a)
ma=max(a)

ans=[]
for i in range(mi,ma+1):
    sum=0
    for aa in a:
        sum+=(i-aa)**2
    ans.append(sum)

print(min(ans))

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, log
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
from decimal import Decimal, ROUND_HALF_UP
import numpy as np
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 = np.array(LIST(), np.int64)
tmp = Decimal(sum(a)/N).quantize(Decimal("1"), rounding=ROUND_HALF_UP)
print(((a-tmp)**2).sum())

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
cost = float("inf")
for y in range(-100,101):
    c = 0
    for a in A:
        c += pow(y-a,2)
    cost = min(cost,c)
print(cost)

Submission status: Accepted

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

ans = 1000000000

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

for i in range(a_min,a_max+1):
    S = 0
    for j in range(N):
        S += (a[j]-i)**2
    ans = min(S,ans)

print(ans)

Submission status: Accepted

Solution code:
import math
def average(dat):
    ans = 0
    for i in range(len(dat)):
        ans += dat[i]
    return ans/len(dat)
N = int(input())
A = list(map(int,input().split()))
K = average(A)
if K%1 == 0:
    B = int(K)
    ans_2 = 0
    for i in range(N):
        ans_2 += (A[i]-B)**2
    print(ans_2)
else:
    B = math.ceil(K)
    C = math.floor(K)
    ans_2 = 0
    ans_3 = 0
    for i in range(N):
        ans_2 += (A[i]-B)**2
        ans_3 += (A[i]-C)**2
    print(min(ans_2,ans_3))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin

n = int(stdin.readline().rstrip())
a = list(map(int, stdin.readline().rstrip().split()))
amax = max(a)
amin = min(a)
ans = 10 ** 9 + 7
for i in range(amin, amax + 1):
    tmp = 0
    for ai in a:
        tmp += (ai - i) ** 2
    if tmp < ans:
        ans = tmp
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8

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

mean = int(sum(A)/N)

for i in range(-100,101):
    cost = 0
    for j in range(N):
        cost += (i - A[j]) ** 2
    ans = min(ans, cost)

if ans == float('inf'):
    ans = 0
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8

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

mean = int(sum(A)/N)
mean_ceil = mean + 1

ans = 0
tmp = 0
for i in range(N):
    ans += (mean - A[i]) ** 2
for i in range(N):
    tmp += (mean_ceil - A[i]) ** 2
print(min(ans,tmp))

Submission status: Accepted

Solution code:

def resolve():

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

    minans=10**10

    for i in range(-100,101):
        val=0
        for j in A:
            val+=(j-i)**2
        minans=min(minans,val)
    print(minans)



resolve()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = 4000000
cost = 0
for i in range(-100, 101):
    for j in a:
        if j!=i:
            cost += (j-i)**2
    ans = min(ans, cost)
    cost = 0

print(ans)

Submission status: Accepted

Solution code:

N = int(input())
A = list(map(int, input().split()))
 
ans = 10**9

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

Submission status: Accepted

Solution code:
import sys

rl = sys.stdin.readline


def solve():
    _ = int(rl())
    a = list(map(int, rl().split()))
    
    ans = 10 ** 10
    for i in range(-100, 101):
        tmp = 0
        for ai in a:
            tmp += (ai - i) ** 2
        ans = min(ans, tmp)
    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
def main():
    import numpy as np

    n = int(input())
    a = list(map(int, input().split()))
    b = np.array(a)

    cost = np.sum(b ** 2)

    for i in range(-100, 101, 1):
        cost = min(cost, np.sum((b - i) ** 2))
    print(cost)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

ave = round(sum(a)/n)
ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
"""
1 2 3 4 5
1 1 1 1 9   4 4 4 4 36=53 | 1 1 1 1 49
"""
import math
n = int(input())
a = list(map(int, input().split()))

ave1 = math.ceil(sum(a)/n)
ave2 = math.floor(sum(a)/n)
ans1 = 0
ans2 = 0

for _a in a:
    ans1 += (_a - ave1)**2

for _a in a:
    ans2 += (_a - ave2)**2

print(min(ans1, ans2))

Submission status: Accepted

Solution code:
N=int(input())
a=list(map(int, input().split()))
a.sort()
buf=0
mi=float("inf")
for i in range(a[0],a[N-1]+1):
    for j in range(N):
        buf=buf+pow(a[j]-i,2)
    if buf<mi:
        mi=buf
    buf=0
print(int(mi))

Submission status: Accepted

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

ans = 10**9
for a in range(min(A), max(A)+1):
    s = 0
    for b in A: s += (a-b)**2
    if ans > s: ans = s
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 += (a[j] - i)**2
         if ans > tmp:
                  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(min(A), max(A)+1):
  cost = 0
  for a in A:
    cost += (a - i) ** 2
  ans = min(cost, ans)

print(ans)

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7

n = INT()
a = LIST()
ans = inf
for i in range(min(a), max(a) + 1):
    tmp = 0
    for j in range(n):
        tmp += (a[j] - i) ** 2
    ans = min(ans, tmp)
print(ans)

Submission status: Accepted

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

min_val = 99999999
for i in range(-100, 101):
  sum_val = 0
  for j in range(n):
    sum_val += (i - a[j])**2
  if min_val > sum_val:
    min_val = sum_val
print(min_val)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
 
#整数input
def ii(): return int(sys.stdin.readline().rstrip()) #int(input())
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) #list(map(int,input().split()))
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]
#文字列input
def ss(): return sys.stdin.readline().rstrip() #input()
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) #list(input().split())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]

#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？

n=ii()
arr=limii()

ans=10**100
sumarr=sum(arr)
sum2arr=sum(list(map(lambda  x:x**2,arr)))


for i in range(-100,101):
    b=n*(i**2)-2*sumarr*i+sum2arr
    ans=min(b,ans)
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())
a = np.array(list(map(int, input().split())))

def calc(mid,a):
    return ((a - mid) ** 2).sum()


for i in range(a.min(), a.max() + 1, 1):
    tmp = calc(i, a)
    if i == a.min():
        ans = calc(i, a)
    elif tmp < ans:
        ans = tmp
    else:
        pass

print(ans)

Submission status: Accepted

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

ave = round(sum(a) / n)

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

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
mi, ma = min(a), max(a)
ans = 10**9
for _ in range(n):
    for s in range(mi-1, ma+1):
        l = []
        for ai in a:
            l.append((s-ai)**2)
        ans = min(ans, sum(l))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
dat = list(map(int, input().split()))
s = sum(dat)
av = round(s/ n)
res = 0
for x in dat:
    res += (x-av) * (x-av)
print(res)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
# coding: utf-8
from fractions import gcd 
from functools import reduce
import sys
sys.setrecursionlimit(200000000)
from inspect import currentframe


# my functions here!
#標準エラー出力
def printargs2err(*args):
    names = {id(v):k for k,v in currentframe().f_back.f_locals.items()}
    print(', '.join(names.get(id(arg),'???')+' : '+repr(arg) for arg in args),file=sys.stderr)
def debug(*args):
    print(*args,file=sys.stderr)

def printglobals():
    for (symbol, value) in globals().items():
        print('symbol="%s"、value=%s' % (symbol, value),file=sys.stderr)
def printlocals():
    for (symbol, value) in locals().items():
        print('symbol="%s"、value=%s' % (symbol, value),file=sys.stderr)
#入力（後でいじる）
def pin(type=int):
    return map(type,input().split())
     
#input

                
def resolve():
    N=int(input())
    b=sorted(list(pin()))
    ans=10000000
    for i in range(-100,101):
        temp=sum(map(lambda x:pow((i-x),2),b))
        ans=min(ans,temp)
    print(ans)
        #print([["NA","YYMM"],["MMYY","AMBIGUOUS"]][cMMYY][cYYMM])
#if __name__=="__main__":resolve()

"""
#printデバッグ消した？
#前の問題の結果見てないのに次の問題に行くの？
"""
"""
お前カッコ閉じるの忘れてるだろ
"""
import sys
from io import StringIO
import unittest

class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """2
4 8"""
        output = """8"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """3
1 1 3"""
        output = """3"""
        self.assertIO(input, output)
    def test_入力例_3(self):
        input = """3
4 2 5"""
        output = """5"""
        self.assertIO(input, output)
    def test_入力例_4(self):
        input = """4
-100 -100 -100 -100"""
        output = """0"""
        self.assertIO(input, output)

if __name__=="__main__":resolve()

Submission status: Accepted

Solution code:
n = int(input())
a_s = list(map(int, input().split()))
mn = (100-(-100))**2*n
for i in range(-100, 101):
  t = []
  for a in a_s:
    t.append((a-i)**2)
  if sum(t) < mn:
    mn = sum(t)
print(mn)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
  n = int(input())
  a = list(input_list())
  ans = 10**9
  a1 = a[0]
  if all([v == a1 for v in a]):
    print(0)
    exit()
  for i in range(-100, 101):
    ta = 0
    for ai in a:
      ta += (ai-i)*(ai-i)
    ans = min(ans, ta)
  print(ans)
  
def input_list():
  return map(int, input().split())

import math
main()

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
a_max=max(a)
a_min=min(a)
if n==1:
    print(0)
else:
    ans=4000000
    for i in range(a_min,a_max+1):
        buf=0
        for j in range(n):
            buf+=(a[j]-i)**2
        ans=min(ans,buf)
    print(ans)

Submission status: Accepted

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

s = sum(a)
m1 = s // n
m2 = -(-s // n)
ans1 = 0
ans2 = 0

for x in a:
    ans1 += (x - m1)**2
    ans2 += (x - m2)**2

print(min(ans1, ans2))

Submission status: Accepted

Solution code:
from math import ceil,floor
n=int(input())
a=list(map(int,input().split()))
ave_h=ceil(sum(a)/n)
ave_l=floor(sum(a)/n)
ans1,ans2=0,0
for aa in a:
    ans1+=(aa-ave_h)**2
for aa in a:
    ans2+=(aa-ave_l)**2
print(min(ans1,ans2))

Submission status: Accepted

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

ans = float('inf')
for i in range(-100,101):
    ans = min(ans, sum((a-i)**2))
print(ans)

Submission status: Accepted

Solution code:
import sys
read=sys.stdin.readline

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

Max=max(A)+1
Min=min(A)

ans=float('inf')

for i in range(Min,Max):
    buf=0
    for a in A:
        buf+=(i-a)**2
    if ans>buf:
        ans=buf

print(ans)

Submission status: Accepted

Solution code:
#coding:utf-8
from statistics import mean
import math
N = int(input())
A = list(map(int, input().split()))
def calc(A,mu):
    return sum([(a-mu)**2 for a in A])
floor = math.floor(mean(A))
ceil = math.ceil(mean(A))
print (min(calc(A,floor),calc(A,ceil)))

Submission status: Accepted

Solution code:
#coding:utf-8
N = int(input())
A = list(map(int, input().split()))

def calc(A,mu):
    return sum([(a-mu)**2 for a in A])
ans = 1e+8
for n in range(-100,201):
    ans = min(ans, calc(A,n))
print (ans)

Submission status: Accepted

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


avg1 = sum(a) // n
avg2 = avg1
if sum(a) % n != 0:
    avg2 = avg1 + 1

ans1 = 0
ans2 = 0 
for i in range(n):
    ans1 += (a[i] - avg1) ** 2
    ans2 += (a[i] - avg2) ** 2

print(min(ans1, ans2))

Submission status: Accepted

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

for i in range(a[0], a[N-1]+1):
    ans = 0
    for j in range(N):
        ans += (a[j]-i)**2
    if(sum_a > ans):
        sum_a = ans
print(sum_a)

Submission status: Accepted

Solution code:
N=int(input())
a=list(map(int, input().split())) 
Min=min(a)
Max=max(a)
M=0
for j in range(N):
    M=M+a[j]**2
for i in range(Min,Max+1):
    S=0
    for k in range(N):
        S=S+(a[k]-i)**2
    if S<M:
        M=S
print(M)

Submission status: Accepted

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

Submission status: Accepted

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

a.sort()


mncost=10**9

for i in range(-100,101):
    cost=0
    for num in a:
        cost+=(num-i)**2
    if cost < mncost:
        mncost = cost

print(mncost)

Submission status: Accepted

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

ans = 10**6
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 = []

for i in range(-100,101):
    s = 0
    for j in a:
        s += (i - j) ** 2
    ans.append(s)

print(min(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
 
import math
import itertools
import sys
import copy
 
# 入力
#A, B, C, D = map(int, input().split())
#L = list(map(int, input().split()))
#S = list(str(input()))
N = int(input())
a = list(map(int, input().split()))

ave = (round(sum(a)/N, 0))

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

print (int(sum))

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
N=int(input())
A=list(map(int,input().split()))

ans=1000
champ=10**9
for i in range(-100,101):
    temp=0
    for item in A:
        temp+=(i-item)**2
    if temp<champ:
        champ=temp

print(champ)

Submission status: Accepted

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


def calc(l, to):
    cost = 0
    for i in l:
        cost += (i-to)**2
    return cost


ans = 10**9
for i in range(-100, 101):
    ans = min(ans, calc(a, i))
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
ans = 1001001001
for i in range(-101, 101):
    res = 0
    for j in A: res += (i - j) * (i - j)
    ans = min(ans, res)
print(ans)

Submission status: Accepted

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

average = int(round(sum(A) / len(A)))
ans = 0

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

print(ans)

Submission status: Accepted

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

b=sum(a)/len(a)
b=round(b)
ans=0

for item in a:
  ans += (item-b)**2
  
print(ans)

Submission status: Accepted

Solution code:
import math
n = int(input())
a = list(map(int, input().split()))
average = sum(a) / n
if average % 1 >= 0.5:
    average = math.ceil(average)
else:
    average = math.floor(average)
ans = 0
for i in range(n):
    ans += (average - a[i]) ** 2
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
S = sum(A)/N
S1 = sum(A)//N
if S - S1 >= 0.5:
  m = S1 + 1
else:
  m = S1
EX = sum((A[i]-m)*(A[i]-m) for i in range(N))
print(EX)

Submission status: Accepted

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

cost=[]

for i in range(min(a),max(a)+1):
    tmp = []
    for j in range(N):
        tmp.append((i-a[j])**2)
    cost.append(sum(tmp))
print(min(cost))

Submission status: Accepted

Solution code:
N, *a = map(int, open(0).read().split())
ave = round(sum(a) / N)
ans = 0
for x in a:
  ans += (x - ave) * (x - ave)
print(ans)

Submission status: Accepted

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

ave=sum(l)/len(l)
if ave==int(ave):
 a=int(ave)
 b=int(ave)
else:
 a=int(ave)
 b=int(ave)+1

cnt1,cnt2=0,0
for i in l:
 cnt1+=(a-i)**2
 cnt2+=(b-i)**2
print(min(cnt1,cnt2))

Submission status: Accepted

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