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:
from math import ceil
n = int(input())
a = list(map(int, input().split()))

ans = 10**10

m1 = a[int(n/2)]
m2 = int(sum(a) / n)
m3 = ceil(sum(a) / n)

for v in [m1, m2, m3]:
    cost = 0
    for i in range(n):
        cost += (a[i] - v) ** 2
    ans = min(cost, ans)
print(ans)

Submission status: Accepted

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

ave=sum(a)/n
ave_upper=math.ceil(ave)
ave_lower=math.floor(ave)

ans_lower=0
ans_upper=0
for i in a:
    ans_lower+=(ave_lower-i)**2
    ans_upper+=(ave_upper-i)**2

print(min(ans_lower, ans_upper))

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort()
cost=10000000
for i in range(a[0],a[-1]+1):
  wa=0
  for j in range(n):
    wa+=(a[j]-i)**2
  if wa<cost:
    cost=wa
print(cost)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = [int(a) for a in input().split()]
    ave = sum(A) // N
    min_cost = float('inf')
    for v in [ave, ave+1]:
        cost = 0
        for a in A:
            cost += pow(a-v, 2)
        min_cost = min(min_cost, cost)

    return min_cost

if __name__ == '__main__':

    print(main())

Submission status: Accepted

Solution code:
# ABC043C - いっしょ / Be Together (ARC059C)
# exhaustive search
def main():
    # find the min cost to rewrite all to i (min(A) <= i <= max(A))
    N = int(input())
    A = tuple(map(int, input().split()))
    ans = []
    for i in range(min(A), max(A) + 1):
        cnt = sum((i - a) ** 2 for a in A)
        ans += [cnt]
    print(min(ans))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ABC043C - いっしょ / Be Together (ARC059C)
def main():
    N = int(input())
    A = tuple(map(int, input().split()))
    ans = []
    for i in range(min(A), max(A) + 1):
        cnt = sum((i - a) ** 2 for a in A)
        ans += [cnt]
    print(min(ans))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict
from heapq import heappush, heappop
import math
import bisect
import random

def LI(): return list(map(int, input().split()))
def I(): return int(input())
def LIM(): return list(map(lambda x:int(x) - 1, input().split()))
def LS(): return input().split()
def S(): return input()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def LIRM(n): return [LIM() 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)]
mod = 1000000007



def main():
    n = I()
    n_list =  sorted(LI())
    ans = float('inf')
    for i in range(n_list[0], n_list[-1] + 1):
        ret = 0
        for j in n_list:
            ret += (i - j) ** 2
        ans = min(ans, ret)


    return ans








print(main())

Submission status: Accepted

Solution code:

import copy
n = int(input())
a = list(map(int, input().split()))
a_avg = round(sum(a)/len(a))
b = copy.copy(a)
while(len(set(b)) > 1):
    for i in range(len(a)):
        if b[i] > a_avg:
            b[i] -= 1
        if b[i] < a_avg:
            b[i] += 1
s = 0
for i in range(len(a)):
    s += (a[i]-b[i])**2
print(s)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(12345678)
import itertools
from collections import Counter
from collections import defaultdict
from collections import deque
import bisect
from heapq import heappush, heappop


def main():
    n = int(input())
    al = list(map(int, input().split()))
    ans = 10**15
    for i in range(-100,101):
        y = i
        cost = 0
        for x in al:
            cost += (x - y) ** 2
        ans = min(ans, cost)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())

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

l_min = min(lst)
l_max = max(lst)

m = 10**8

for i in range(l_min,l_max+1):
    c = 0
    
    for j in range(n):
        c += (lst[j]-i)**2
    
    if c < m:
        m = c

print(m)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
ave=sum(a)/n
x=sum(a)//n
ans=0
if((ave-x)<=0.5):
  for i in a:
    ans += (i-x)**2
  print(ans)
else:
  for i in a:
    ans+=(i-x-1)**2
  print(ans)

Submission status: Accepted

Solution code:
N = int(input())
As = list(map(int, input().split()))
sAs = sum(As)
s2As = sum(map(lambda a: a*a, As))
#print(sAs, s2As)
cs = []
for x in range(min(As), max(As)+1):
  c = N*x*x-2*x*sAs+s2As
  cs.append(c)
r = min(cs)
print(r)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
from math import floor, ceil

def cost(x):
    return sum((x-a)**2 for a in A)

print(min(
  cost(ceil(sum(A)/N)),
  cost(floor(sum(A)/N))
         )
     )

Submission status: Accepted

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

a.sort()

d=[[0]*N for i in range(max(a)-min(a)+1)]
for i in range(max(a)-min(a)+1):
  for j in range(N):
      d[i][j]=(a[j]-(min(a)+i))**2

print(min([sum(dd) for dd in d]))

Submission status: Accepted

Solution code:
def C_BeTogether():
    N = int(input())
    a = list(map(int,input().split()))
    average = round(sum(a) / N)
    ans = 0
    for x in range(N):
        ans += pow(a[x] - average, 2)
    print(ans)
    
if __name__ == "__main__":
    C_BeTogether()

Submission status: Accepted

Solution code:
n = int(input())
a_list = [int(col) for col in input().split(' ')]

min_score = None
for target in range(-100, 101):
  score = 0
  for a in a_list:
    score += (target - a) * (target - a)
  if min_score is None or min_score > score:
    min_score = score
print(min_score)

Submission status: Accepted

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

ans = 100100100
for i in range(-100, 101):
    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 = []
a = input().split()
Sum = 0

for i in range(n):
  Sum = Sum + int(a[i])

average = int(Sum / n)

answer_1 = 0
answer_2 = 0
for i in range(n):
  answer_1 = answer_1 + (int(a[i]) - average) * (int(a[i]) - average)
  answer_2 = answer_2 + (int(a[i]) - average - 1) * (int(a[i]) - average - 1)

  answer = min(answer_1, answer_2)
print(answer)

Submission status: Accepted

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

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

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

print(sum)

Submission status: Accepted

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

Submission status: Accepted

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

N = int(input())
a = list( map(int, input().split()) )
b = np.asarray(a, np.float)

ave = np.average(b)
ave_floor = np.floor(ave)
ave_ceil = np.ceil(ave)

cost1 = np.sum((b-ave_floor)**2)
cost2 = np.sum((b-ave_ceil)**2)
min_cost = int( np.min([cost1, cost2]) )

print(min_cost)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
s = sum(a)
m = s/n
if s%n==0:
    cost = 0
    for i in range(n):
        cost += (a[i]-s//n)**2
else:
    m1 = m//1
    m2 = m//1+1
    cost1 = 0
    cost2 = 0
    for i in range(n):
        cost1 += (a[i]-m1)**2
        cost2 += (a[i]-m2)**2
    cost = min(cost1,cost2)
print(int(cost))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
ans = []
for i in range(-100,101):
    s = 0
    for v in a:
        need = abs(v-i)
        s+= need*need
    ans.append(s)
print(min(ans))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
a.sort()
c = 10 ** 9
for i in range(a[0], a[-1] + 1):
    tc = 0
    for j in a:
        tc += (j - i) ** 2
    c = min(c, tc)
print(c)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import statistics
import math
# 文字列の入力
N= int(input())
# 数値用のリスト　インプットをブランクで区切りリスト化
k = input()
l = [int(j) for j in k.split(" ")]
w = 0
# 算術平均値の取得
mean = round(statistics.mean(l))
for i in range(len(l)):
	w += (l[i] - mean) ** 2
print(w)

Submission status: Accepted

Solution code:

import collections
import itertools
import sys
import time

def getint(): return int(input())
def getints(): return list(map(int, input().split()))

n = getint()
xs= getints()

res = 100**2 * n

for g in range(-100,101):
    tmp = 0
    for x in xs:
        tmp += (x-g)**2
    res = min(res, tmp)

print(res)

Submission status: Accepted

Solution code:
n = int(input())
*l, = map(int,input().split())
ans = 10**6
for i in range(min(l),max(l)+1):
  tmp = sum([(y-i)**2 for y in l])
  ans = min(tmp,ans)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
*l, = map(int,input().split())
ans = 10**6
for i in range(-100,101):
  tmp = sum([(y-i)**2 for y in l])
  ans = min(tmp,ans)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
a.sort()
search = [i for i in range(a[0], a[-1]+1)]
ans = 100000

cost = [0] * len(search)
for i in range(len(search)):
    for j in range(len(a)):
        cost[i] += (search[i] - a[j])**2


print(min(cost))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
n = float(input())
a = list(map(float,input().split()))
x =round(sum(a)/n)
s = 0
for i in a:
    s += (x-i)**2
print(int(s))

Submission status: Accepted

Solution code:
import math
n = float(input())
a = list(map(float,input().split()))
x =round(sum(a)/n)
s = 0
for i in a:
    s += (x-i)**2
print(int(s))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
a=[int(x) for x in input().split()]
print(min([sum([(x-item)**2 for x in a]
              )
           for item in list(range(-100,101
                                 )
                           )
          ]
         )
     )

Submission status: Accepted

Solution code:
n=int(input())
a=[int(x) for x in input().split()]
print(min([sum([(x-item)**2 for x in a]) for item in list(range(-100,101)
                                                         )
          ]
         )
     )

Submission status: Accepted

Solution code:
n=input()
kazu=list(map(int, input().split()))
kotae=9999999999
for i in range(-100,101):
	kei=0
	for j in kazu:
		jijo=(i-j)**2
		kei+=jijo
	if kei<kotae:
		kotae=kei
print(kotae)

Submission status: Accepted

Solution code:
N = int(input())
nums = list(map(int, input().split()))
min_cost = 4*100**3
for i in range(-100, 101):
  cost = 0
  for num in nums:
    cost += (i-num)**2
  min_cost = min(min_cost, cost)
print(min_cost)

Submission status: Accepted

Solution code:
N = raw_input()
a = map(int,raw_input().split())
max_num = max(a)
min_num = min(a)

ans = []
b = 0
for i in range(min_num,max_num+1):
    for j in range(0,int(N)):
        b +=(a[j]-i)*(a[j]-i) 
    ans.append(b)
    b = 0
print min(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys, math, fractions, itertools


def solve(N: int, a: "List[int]"):
    s = sum(a)
    k1 = math.floor(s / N)
    k2 = math.ceil(s / N)

    d1 = 0
    d2 = 0
    for i in range(N):
        d1 += (a[i] - k1) ** 2
        d2 += (a[i] - k2) ** 2
    print(min(d1, d2))
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    solve(N, a)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

sumA = sum(A)
obj = round(sumA/num)
ans = 0
for i in range(num):
    ans += (A[i]-obj) ** 2
print(ans)

Submission status: Accepted

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

sumA = sum(A)
obj = round(sumA/num)
sum2A = 0
for i in range(num):
    sum2A += A[i] ** 2
print(sum2A - (2*obj*sumA) + (num*obj**2))

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 += (i-mean) ** 2

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

min_sum = 100000000
temp_sum = 0
ans = 0
for i in range(-100,101):
    for j in range(N):
        temp_sum += pow(i-a_list[j],2)
        if j==N-1:
            if temp_sum < min_sum:
                min_sum = temp_sum
            temp_sum = 0
print(min_sum)

Submission status: Accepted

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

ave = sum(numbers) / N
#print( ave )
ave_int = round( ave )
#print( ave_int )

ans = 0
for number in numbers :
  ans += (number - ave_int ) **2
print( ans )

Submission status: Accepted

Solution code:
n = int(input())
arr = list(map(int,input().split()))
s = []
for i in range(min(arr), max(arr)+1):
  cost = 0
  for j in arr:
    cost += (j-i)**2
  s.append(cost)
print(min(s))

Submission status: Accepted

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

for i in range(-100, 101):
    tmp = 0
    for j in range(N):
        tmp += (i-a[j])**2
    ans_lsit.append(tmp)

ans = min(ans_lsit)
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(300000)


def solve(N: int, a: "List[int]"):
    ret = float('inf')
    mn = min(a)
    mx = max(a)
    for v in range(mn, mx + 1):
        tmp = 0
        for x in a:
            tmp += (x - v) ** 2
        ret = min(ret, tmp)
    print(ret)
    return


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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

ave = sum(a)/n
s_ave = math.ceil(ave)
l_ave = math.floor(ave)
s_cost = 0
l_cost = 0
for i in range(n):
  s_cost += (a[i]-s_ave)**2
  l_cost += (a[i]-l_ave)**2

print(min(s_cost, l_cost))

Submission status: Accepted

Solution code:

import sys


def main():
    num = int(input())
    data = list(map(int, input().split()))
    half1, half2 = sum(data) // num, sum(data) // num + 1

    ans1 = 0
    for i in range(num):
        ans1 += (data[i] - half1) ** 2

    ans2 = 0
    for i in range(num):
        ans2 += (data[i] - half2) ** 2

    print(min(ans1, ans2))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

    # 全探索余裕
    ans = float("inf")
    for i in range(-100, 100+1):
        cost = 0
        for j in range(N):
            # 全てiに書き換える
            cost += (i - A[j])**2
        ans = min(ans, cost)
    
    print(ans)


if __name__ == "__main__":
    solve()

Submission status: Accepted

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

d = round(sum(a) / len(a))
cost = 0
for i in a:
    cost += (i - d) ** 2

print(cost)

Submission status: Accepted

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

ans = 200**2*1000
for k in range(-100, 101):
    l = 0
    for a in A:
        l += abs(a-k)**2
    ans = min(ans, l)

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N=int(input())
An=[int(x) for x in input().split()]
ans=(200**2)*N
for i in range(-100,101):
	ans=min(ans,sum([(a-i)**2 for a in An]))
print(ans)

Submission status: Accepted

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

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(min(a),max(a)+1)]))

Submission status: Accepted

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

print(min(l))

Submission status: Accepted

Solution code:
def smaller(x, y):
	if x < y:
		return x
	return y

N = int(input())
A = list(map(int, input().split()))
min = 5000000000000000
for i in range(-100, 101):
	sum = 0
	for j in range(0, N):
		sum += (A[j]-i)**2
	min = smaller(min, sum)
print(min)

Submission status: Accepted

Solution code:

N = int(input())


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


ans = float("inf")

for i in range(201):

    i -= 100

    now = 0

    for a in A:

        now += ( a - i ) ** 2

    ans = min (ans,now)

print (ans)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
a = list(map(int, input().split()))
a_np = np.array(a)
b = []
for i in range(a_np.min(), a_np.max()+1):
  c = a_np - i
  d = [j*j for j in c]
  b.append(sum(d))
print(min(b))

Submission status: Accepted

Solution code:
a = int(input())
b = list(map(int, input().split()))
b.sort()
c = []
for i in range(b[0], b[len(b)-1] + 1):
    d = 0
    for j in range(len(b)):
        d += (b[j] - i)**2
    c.append(d)
c.sort()
print(c[0])

Submission status: Accepted

Solution code:
N  = int(input())

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

min = 100*(200)**2+10

for n in range(-100,101):
    tmp = 0
    for a in A:
        if a == n:
            pass
        else:
            tmp += (a - n)**2

    if min > tmp:
        min = tmp

print(min)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int, input().split()))

big = max(A)
sml = min(A)

ans = float('inf')

for i in range(sml, big + 1):
    cnt = 0
    for a in A:
        cnt += (abs(i - a))**2
    ans = min(ans, cnt)
print(ans)

Submission status: Accepted

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

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

mi = float('inf')

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

Submission status: Accepted

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

min_a = min(a)
max_a = max(a)

min_cost = 10000 * 100
for num in range(min_a, max_a + 1):
    cost = 0
    for z in a:
        cost += (z - num) ** 2
    if min_cost > cost:
        min_cost = cost

print(min_cost)

Submission status: Accepted

Solution code:
_ = input()
a = list(map(int, input().split()))
ans = 10 ** 18
for val in range(-200, 200):
    curr = 0
    for it in a:
        curr += (it - val) ** 2
    ans = min(ans, curr)
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline #文字列入力のときは注意

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


m = sum(a)//n

ans = 100000000
for i in {-1,0,1}:
    res = sum((x-m-i)**2 for x in a)
    ans = min(ans,res)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import floor, ceil
from statistics import mean
n = int(input())
a = list(map(int, input().split()))
mc = ceil(mean(a))
mf = floor(mean(a))
dc = sum([(i-mc)**2 for i in a])
df = sum([(i-mf)**2 for i in a])
print(min(dc, df))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
nijo_list = []
for i in range(min(a), max(a)):
    nijo_sum = 0
    for j in range(len(a)):
        nijo = (a[j] - i)**2
        nijo_sum += nijo
    nijo_list.append(nijo_sum)
if nijo_list:
    print(min(nijo_list))
else:
    print(0)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
ttl1, ttl2, ttl3 = 0, 0, 0
avarage1 = -(-sum(A) // N)
avarage2 = sum(A) // N
for i in A:
    ttl1 += (i - avarage1) ** 2
    ttl2 += (i - avarage2) ** 2
print(min(ttl1, ttl2))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
ttl1, ttl2, ttl3 = 0, 0, 0
avarage1 = -(-sum(A) // N)
avarage2 = sum(A) // N
avarage3 = round(sum(A) / N)
for i in A:
    ttl1 += (i - avarage1) ** 2
    ttl2 += (i - avarage2) ** 2
    ttl3 += (i - avarage3) ** 2
print(min(ttl1, ttl2, ttl3))

Submission status: Accepted

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

r = []
for i in range(-100, 101):
	sum = 0
	for j in range(n):
		sum += (a[j]-i)**2
	r.append(sum)

r.sort()
print(r[0])

Submission status: Accepted

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

def fn(m):
    rtn=0
    for a in al:
        rtn+=(a-m)**2
    return rtn

mean=sum(al)//n
print(min(fn(mean),fn(mean+1)))

Submission status: Accepted

Solution code:
import math
N = int(input())
a = [int(_) for _ in input().split()]

floor = math.floor(sum(a) / len(a))
ceil = math.ceil(sum(a) / len(a))

ceil_sum = sum([(item - ceil) ** 2 for item in a])
floor_sum = sum([(item - floor) ** 2 for item in a])

print(min(ceil_sum, floor_sum))

Submission status: Accepted

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

total_costs = {}
if( len(set(A)) == 1 ):
    print(0)
else:
    for i in range(-100, 101):
        cost = 0
        for a in A:
            cost += (a - i)*(a - i)
        total_costs[i] = cost
    print(min(total_costs.values()))

Submission status: Accepted

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

total_costs = {}
if( len(set(A)) == 1 ):
    print(0)
else:
    for i in range(-100, 101):
        cost = 0
        for a in A:
            cost += (a - i)**2
        total_costs[i] = cost
    print(min(total_costs.values()))

Submission status: Accepted

Solution code:
INF = float("inf")
nl = lambda: list(map(int, input().split()))
sl = lambda: input().split()
n = lambda: int(input())
s = lambda: input()

N = n()
A = nl()

res = INF
for t in range(-100, 101):
    cost = 0
    for i in range(N):
        cost += (A[i] - t) * (A[i] - t)
    if cost < res:
        res = cost

print(res)

Submission status: Accepted

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

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

Submission status: Accepted

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

ans=10**7
for i in range(min(a),max(a)+1):
    c=0
    for j in a:
        c+=(j-i)**2
    ans=min(ans,c)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

d = defaultdict(int)

for i in range(n):
    d[a[i]] += 1

ans = []

for i in range(-100,101):
    cnt = 0
    for j in d.keys():
        cnt += d[j]*(i-j)**2
    ans.append(cnt)

print(min(ans))

Submission status: Accepted

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

s = 0
s_pre = 10000000000

for j in range(-100, 101):
    for i in range(n):
        s += pow((a[i] - j),2)
    if s_pre > s:
        s_pre = s
    s = 0
print(s_pre)

Submission status: Accepted

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

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

print (ans)

Submission status: Accepted

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

mn = 10**9
for j in range(min(A), max(A)+1):
  cost = 0
  for i in range(N):
    cost += (A[i]-j)**2
  mn = min(cost, mn)
print(mn)

Submission status: Accepted

Solution code:
# encoding: utf-8
import sys
# input = sys.stdin.readline

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

ans = 10 ** 9 + 7
for i in range(min(a), max(a) + 1):
    ans_tmp = 0
    for j, aj in enumerate(a):
        ans_tmp += (i - a[j]) ** 2
    if ans > ans_tmp: ans = ans_tmp

print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import Counter, deque, defaultdict
from itertools import accumulate, permutations, combinations, takewhile, compress, cycle
from functools import reduce
from math import ceil, floor, log10, log2, factorial
from pprint import pprint

sys.setrecursionlimit(1000000)
# MOD = 10 ** 9 + 7
# N = int(input())
# A = [int(x) for x in input().split()]
# V = [[0] * 100 for _ in range(100)]
# A = [int(input()) for _ in range(N)]

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

ans = float('inf')
for y in range(-100, 101):
    ans = min(ans, sum([(x - y) ** 2 for x in A]))

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
l=[]
p=input().rstrip().split(' ')
for i in range(-100,101):
    s=0;
    for j in range(0,len(p)):
        t=(int(p[j])-i)**2;
        s=s+t;
    l.append(s)
print(min(l))

Submission status: Accepted

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())


N = int(input())
a = LI()

if len(set(a)) == 1:
    print(0)
    exit()
ans = mod
for i in range(min(a),max(a)):
    tmp = 0
    for j in range(len(a)):
        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()))

mean = int(round(sum(a) / len(a)))

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

Submission status: Accepted

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


def get_cost(now, target):
    return (now - target) * (now - target)


ans = 10000000
for i in range(min(a), max(a) + 1):
    cost_sum = 0
    for j in a:
        cost_sum += get_cost(j, i)
    if cost_sum < ans:
        ans = cost_sum
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int,input().split()))
av = sum(a)//N
ans1 = []
ans2 = []
for i in range(N):
    ans1.append((a[i]-av)**2)
    ans2.append((a[i]-av-1)**2)

print(min(sum(ans1),sum(ans2)))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = float("inf")
m = min(a)
M = max(a)
for i in range(m,M+1):
  s = 0
  for j in a:
    s += (j-i)**2
  ans = min(ans, s)
print(ans)

Submission status: Accepted

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

answer = 10**10
for i in range(min(a), max(a)+1):
  cost = 0
  for j in a:
    cost += (i-j)**2
  answer = min(answer, cost)

print(answer)

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
s=sum(l)
ans=10**7
for i in range(min(l),max(l)+1):
	ans=min(ans, sum([(x-i)**2 for x in l]))
print(ans)

Submission status: Accepted

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

E=round(sum(A)/N)
ans=0
for a in A:
    ans+=(E-a)**2
print(ans)

Submission status: Accepted

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