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:
Data = []

def calc(a):
    sum1 = 0
    for d in Data:
        sum1 += (a-d)*(a-d)
    return sum1

N = int(input())
Data = list(map(int,input().split()))
sum1 = sum(Data)
if sum1 % N == 0:
    ave1 = sum1 // N
    ans = calc(ave1)
else:
    ave1 = int(sum1 // N)
    ave2 = ave1-1
    ave3 = ave1+1
    ans = min(min(calc(ave1),calc(ave2)),calc(ave3))
print(ans)

Submission status: Accepted

Solution code:
mini=10**29 
n=int(input())
l=[int(i) for i in input().split()]
i = round(sum(l) / n )
for z in range(1):
    sm=0  
    for j in l:
        sm+=(i-j)*(i-j)
    mini=min(mini,sm)
print(mini)

Submission status: Accepted

Solution code:
mini=10**29 
n=int(input())
l=[int(i) for i in input().split()]
for i in range(min(l)-19,max(l)+3):
    sm=0  
    for j in l:
        sm+=(i-j)*(i-j)
    mini=min(mini,sm)
print(mini)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
cons = round(sum(A) / N )
ans = 0 
for i in range(N):
  ans += (A[i] - cons)**2 
 
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):
    ttl = 0
    for j in range(n):
        ttl += (a[j]-i)**2
    ans = min(ans,ttl)

print(ans)

Submission status: Accepted

Solution code:
import math

from decimal import Decimal, ROUND_HALF_UP


def quantize(f):
    return int(Decimal(str(f)).quantize(Decimal('0'), rounding=ROUND_HALF_UP))


def check(n, numbers):
    average = sum(numbers) / n
    average = quantize(average)
    costs = [int(math.pow(n - average, 2)) for n in numbers]
    return sum(costs)


def main():
    N = int(input())
    numbers = list(map(int, input().split()))
    print(check(n=N, numbers=numbers))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math

from functools import lru_cache


@lru_cache(maxsize=4096)
def get_cost(x, y):
    return int(math.pow((x - y), 2))


def get_costs(target, numbers):
    return sum([get_cost(target, n) for n in numbers])


def check(n, numbers):
    n_max = max(numbers)
    n_min = min(numbers)
    costs = []
    for target in range(n_min, n_max + 1):
        costs.append(get_costs(target=target, numbers=numbers))

    return min(costs)


def main():
    N = int(input())
    numbers = list(map(int, input().split()))
    print(check(n=N, numbers=numbers))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:

def f(A):
    s = sum(A)
    c = len(A)
    r = s / float(c)
    r = int(round(r))

    cost = 0
    for j in range(len(A)):
        diff = A[j] - r
        cost += (diff * diff)
    return cost

f([4, 2, 5]) == 5
f([1, 1, 3]) == 3

n = input()
A = list(map(int, input().split()))
ans = f(A)
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
li=list(map(int,input().split()))
ave1=sum(li)//N
ave2=sum(li)//N+1
ans1=0
ans2=0
for n in range(len(li)):
  ans1+=(li[n]-ave1)**2
  ans2+=(li[n]-ave2)**2
print(min(ans1,ans2))

Submission status: Accepted

Solution code:
N=int(input())
li=list(map(int,input().split()))
ave1=sum(li)//N
ave2=sum(li)//N+1
ave3=sum(li)//N-1
ans1=0
ans2=0
ans3=0
for n in range(len(li)):
  ans1+=(li[n]-ave1)**2
  ans2+=(li[n]-ave2)**2
  ans3+=(li[n]-ave3)**2
print(min(ans1,ans2,ans3))

Submission status: Accepted

Solution code:
N = int(input())
a = [int(i) for i in input().split()]
avg = int(((sum(a) / N)*2+1) //2)
print(sum([(i - avg)**2 for i in a]))

Submission status: Accepted

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

print(min([sum([(a - x) ** 2 for a in A]) for x in range(-100, 100+1)]))

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int, input().split()))
MIN = min(l)
MAX = max(l)
ans = float('inf')
for i in range(MIN, MAX + 1):
    count = 0
    for j in l:
        count += (i - j) ** 2
    ans = min(ans, count)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict

if __name__ == "__main__":
    n = int(input())
    mean = 0
    numbers = defaultdict(int)
    for i in map(int, input().split()):
        numbers[i] += 1
        mean += i
    mean = round(mean / n)
    res = 0
    for number in numbers:
        res += numbers[number] * (mean - number) ** 2

    print(res)

Submission status: Accepted

Solution code:
from collections import defaultdict

if __name__ == "__main__":
    n = int(input())
    mean = 0
    numbers = defaultdict(int)
    tmp = [int(i) for i in input().split()]
    for i in tmp:
        numbers[i] += 1
        mean += i
    mean = round(mean / n)
    res = 0
    for number in numbers:
        res += numbers[number] * (mean - number) ** 2

    print(res)

Submission status: Accepted

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

start = min(a)
end = max(a)

ans = float('inf')
for i in range(start, end + 1):
    temp = 0
    for j in a:
        temp += (j - i) ** 2
    ans = min(ans, temp)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    avg1 = sum(A)//N
    avg2 = avg1 + 1
    ans1 = 0
    ans2 = 0
    for a in A:
        ans1 += (a - avg1)**2
        ans2 += (a - avg2)**2
    print(min(ans1,ans2))

if __name__ == '__main__':
    main()

Submission status: Accepted

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

b = sum(a) / n
ceil = math.ceil(b)
floor = math.floor(b)

ans1 = 0
ans2 = 0
for i in range(n):
    ans1 += (a[i] - ceil)**2
for i in range(n):
    ans2 += (a[i] - floor)**2
print(min(ans1, ans2))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time
sys.setrecursionlimit(10**7)
inf = 10**20

N = input()
a = list(map(int,input().split()))
answer = inf
for goal in range(-100,101):
    squaredSum = sum(map(lambda x: (x-goal)**2, a))
    answer = min(answer, squaredSum)
print(answer)

Submission status: Accepted

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

ave = sum(A) // N 
if sum(A) % N >= N / 2:
  ave += 1

ans = 0
for i in range(N):
  ans += (A[i] - ave) * (A[i] - ave) 

print(int(ans))

Submission status: Accepted

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

mean = sum(As)/len(As)
if mean - int(mean) > 0.5:
    mean = int(mean)+1
else:
    mean = int(mean)

print(sum([(a-mean)**2 for a in As]))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
ans=0
y=round(sum(A)/n)

for x in A:
  ans+=(x-y)**2
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
ans=1000000000000000
for i in range(min(l),max(l)+1):
    tmp=0
    for j in l:
        tmp+=(i-j)**2
    ans=min(ans,tmp)
print(ans)

Submission status: Accepted

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

tmp = 10 ** 9

for i in range(min(As), max(As)+1):
  tmp = min(tmp, sum(map(lambda x: (x-i) ** 2, As)))
  
print(tmp)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
a = list(map(int, input().split()))
ans = 10 ** 6

for i in range(min(a), max(a) + 1):
    b = np.array(a)
    ans = min(ans, np.sum((b - i) ** 2))

print(ans)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
a = list(map(int, input().split()))
ans = 10 ** 6

for i in range(min(a), max(a) + 1):
    b = np.array(a)
    ans = min(ans, np.sum(abs(b - i) ** 2))

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
A.sort()
ans = float("inf")
for i in range(A[0],A[-1]+1):
  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()))
a.sort()
s=[]
#print(a[-1]-a[0])
for i in range(a[0],a[-1]+1):
    b=0
    #print(i)
    for j in range(len(a)):
        b+=(a[j]-i)**2
    s.append(b)
#print(s)
print(min(s))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import math
import functools

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

mean = math.floor(sum(a) / n + 0.5)

print(functools.reduce(lambda x, y: x + (y - mean) ** 2, a, 0))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = input()

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

#各要素を2乗する関数
def calc_sq(n):
	return n ** 2

a2 = list(map(calc_sq, a))
"""
map関数の戻り値はリストではなく、シーケンスのmapオブジェクト
なので、インデックスでアクセスしたり、ループで回したりできるが、
appendなどのlistメソッドは使用できない。
また、print関数で出力しても、カンマ区切りの要素では表示されず、
オブジェクトの型名が表示されるだけ。

だが、map関数の戻り値はシーケンスなので、
list関数を使ってlist型に変換することができる！！
"""

sum_a = sum(a)

sum_a2 = sum(a2)

cost = []
for x in range(-101, 101):
	cost.append(int(N) * x**2 - 2 * x * int(sum_a) + int(sum_a2))

print(min(cost))

Submission status: Accepted

Solution code:
N = input()

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

#各要素を2乗する関数
def calc_sq(n):
	return n ** 2

a2 = list(map(calc_sq, a))
"""
map関数の戻り値はリストではなく、シーケンスのmapオブジェクト
なので、インデックスでアクセスしたり、ループで回したりできるが、
appendなどのlistメソッドは使用できない。
また、print関数で出力しても、カンマ区切りの要素では表示されず、
オブジェクトの型名が表示されるだけ。

だが、map関数の戻り値はシーケンスなので、
list関数を使ってlist型に変換することができる！！
"""

sum_a = sum(a)

sum_a2 = sum(a2)

cost = []
for x in range(-200, 200):
	cost.append(int(N) * x**2 - 2 * x * int(sum_a) + int(sum_a2))

print(min(cost))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

cost_min = float('inf')
for i in range(-100,100+1):
   cost = 0
   for j in range(N):
      cost += (i-A[j])**2
   cost_min = min(cost_min,cost)
  
print(cost_min)

Submission status: Accepted

Solution code:
from sys import stdin
N = int(stdin.readline().rstrip())
a = [int(x) for x in stdin.readline().rstrip().split()]
m = round(sum(a)/N)
ans = sum([(i-m)**2 for i in a])
print(ans)

Submission status: Accepted

Solution code:

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


ans = 2**30-1
X = [i-100 for i in range(201)]
for a in X:
    tmp = 0
    for b in A:
        tmp += (b-a)*(b-a)
    ans = min(ans,tmp)
print(ans)

Submission status: Accepted

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

costs = []
for y in range(-100, 101):
    l = [(x-y)**2 for x in a]
    costs.append(sum(l))
ans = min(costs)
print(ans)

Submission status: Accepted

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

p = min(l)
q = max(l)

ms = []
for i in range(p, q+1):
  m = 0
  for j in range(n):
    m += (l[j]-i)**2
  ms.append(m)
print(min(ms))

Submission status: Accepted

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

ans = float("inf")
for i in range(-100, 101):
    cost = 0
    for ele in a:
        cost += (ele - i) ** 2
    ans = min(ans, cost)

print(ans)

Submission status: Accepted

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

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

print(min(cost))

Submission status: Accepted

Solution code:
n=int(input())
hoge=list(map(int,input().split()))
ans=-1
for i in range(201):
  i=-100+i
  same=0
  for hg in hoge:
    same+=(i-hg)**2
  if(ans==-1):
    ans=same
  else:
    ans=min(same,ans)
print(ans)

Submission status: Accepted

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

ave = round(sum(l) / n)
print(sum(list(map(lambda x: (x-ave)**2, l))))

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict
import sys,heapq,bisect,math,itertools,string,queue
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n = inp()
a = inpl()
min_a = min(a)
max_a = max(a)
res = mod
for i in range(min_a, max_a + 1):
    cost = 0
    for j in a:
        cost += abs(i-j) ** 2
    res = min(res, cost)
print(res)

Submission status: Accepted

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

'''
全探索
'''
cost_tmp = 0
cost_min = 10**8

for i in range(min(a),max(a)+1):
    for j in range(n):
        cost_tmp += pow(a[j]-i,2)
    if cost_tmp < cost_min:
        cost_min = cost_tmp
    cost_tmp=0

print(cost_min)

Submission status: Accepted

Solution code:
def f(x, y):
    return (x - y)**2


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

ans = 10**9
for k in range(-100, 101):
    temp = sum([f(x, k) for x in A])
    ans = min(ans, temp)
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
A = np.array([int(a) for a in input().split()])
ans = 200**2 * 100
for i in range(min(A), max(A)+1):
    a = sum((A-i)**2)
    if a < ans:
        ans = a
print(ans)

Submission status: Accepted

Solution code:
import statistics
N = int(input())
a = list(map(int, input().split()))
s = sorted(a)
ans = 10 ** 9 + 7
for i in range(s[0], s[-1] + 1):
    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(" ")))
s=sum(a)
ave=s/N
ave=round(ave)
b=list(map(lambda x:(x-ave)**2,a))
print(sum(b))

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
y = sum(a) / N
if y < 0:
  y = int(y - 0.5)
else:
  y = int(y + 0.5)
print(sum((x - y) * (x - y) for x in a))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
list_a = list(map(int, input().split(" ")))
target = sum(list_a) / n
if target - int(target) >= 0.5:
  target = int(target) + 1
else:
  target = int(target)
cost = 0
for one_a in list_a:
  cost += (one_a - target) ** 2
print(cost)

Submission status: Accepted

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


if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

ans=10**10
for i in range(-100,101):
  ans=min(sum(list(map(lambda x: (x-i)**2,A))),ans)

print(ans)

Submission status: Accepted

Solution code:
n,A=open(0);print(min(sum((x-int(a)-100)**2for a in A.split())for x in range(201)))

Submission status: Accepted

Solution code:
n,A=open(0);print(min(sum((x-int(a))**2for a in A.split())for x in range(-100,101)))

Submission status: Accepted

Solution code:
n,*A=map(int,open(0).read().split());print(min(sum((x-a)**2for a in A)for x in range(-100,101)))

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
cost = 10000000000
for i in range(-100, 101):
    cur_cost = 0
    for e in a:
        cur_cost += (e - i) ** 2
    cost = min(cur_cost, cost)
    
print(cost)

Submission status: Accepted

Solution code:
# C
N = int(input())
lst = list(map(int, input().split()))
high = max(lst)
low = min(lst)
ans = 5000000
for i in range(low, high+1):
    cost = 0
    for j in range(N):
        cost += (i-lst[j])**2
    if (cost <= ans):
        ans = cost
print(ans)

Submission status: Accepted

Solution code:
import sys
import numpy as np
input = sys.stdin.readline

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

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:

import sys
input = sys.stdin.readline
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
##############################

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

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

    ans = min(ans, x)

print(ans)

Submission status: Accepted

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

a1=0
a2=0
for i in range(len(a)):
  m1=sum(a)//n
  m2=-(-sum(a)//n)
  a1+=(a[i]-m1)**2
  a2+=(a[i]-m2)**2
  
print(min(a1,a2))

Submission status: Accepted

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

ans = 10**100

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())
l = list(map(int, input().split()))
c = 0
b = 0
for i in range(N):
    c += l[i] ** 2
    b += -2 * l[i]
x = -b / (2 * N)
x = round(x)
Ans = N * x ** 2 + b * x + c

print(Ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(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 = list(map(int, input().split()))
aSum = 0
for i in a:
    aSum += (i - round(sum(a)/n)) ** 2

print(aSum)

Submission status: Accepted

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

print(aSum)

Submission status: Accepted

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

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

ans = 200**2 * 105
for b in range(a_min, a_max+1):
    cost = 0
    for i in range(n):
        cost += (a[i] - b) ** 2
    ans = min(ans, cost)

print(ans)

Submission status: Accepted

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

for i in range(-100,101):
    total = 0
    for j in range(N):
        total+=(A[j]-i)*(A[j]-i)
    if mini>=total:
        mini=total

print(mini)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
li = list(map(int,input().split()))
s = 0
ans =10**6
a,b = min(li),max(li)
for i in range(a,b+1):
    for j in range(len(li)):
        s += (i-li[j])**2
    ans = min(ans,s)
    s = 0

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = [int(i) for i in input().split()]
num = int(sum(a) / N)
ans = float('inf')

for i in [num - 1, num, num + 1]:
    ans = min(ans, sum([abs(j - i) ** 2 for j in a]))
    
print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

sum = 0
result = 0

for i in A:
    sum+=i
ave = round(sum/len(A))

for i in A:
    result += pow(i-ave,2)

print(round(result))

Submission status: Accepted

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

num.sort()
s = num[0]
e = num[-1] + 1

cost = []
for i in range(s, e):
  cost.append(sum(list(map(lambda x: (x - i)**2, num))))
  
print(min(cost))

Submission status: Accepted

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

set_a = set(a)

import sys

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

totals = []

for i in range(min(a),max(a)+1):
    temp = 0
    for j in range(N):
        temp += (a[j]-i)**2
    totals.append(temp)

res = min(totals)
print(res)

Submission status: Accepted

Solution code:
N = int(input())
A = sorted(list(map(int,input().split())))
z = 10**6

for i in range(A[0],A[-1]+1):
  z = min(z,sum([(a-i)**2 for a in A]))

print(z)

Submission status: Accepted

Solution code:
n = int(input())
L = [int(i) for i in input().split()]
tg = round(sum(L)/n)

cost = 0
for i in range(n):
  cost += pow((L[i]-tg),2)
print(cost)

Submission status: Accepted

Solution code:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools,pdb
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
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())
def _pf(s): return print(s, flush=True)

N = _I()
A = LI()

"""
全数字を例えば1に変える場合など、試していけば良い
"""

ans = []
for i in range(min(A), max(A)+1):
    tmp = 0
    for a in A:
        tmp += abs(a - i) ** 2
    ans.append(tmp)

print(min(ans))

Submission status: Accepted

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

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

print(min(cost))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

check=float('inf')
for i in range(-100,101):
    ans=0
    for j in a:
        ans+=(i-j)**2
    if check>ans:
        check=ans

print(check)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc043/tasks/arc059_a
# C - いっしょ / Be Together
import statistics
N = int(input().split()[0])
a_list = list(map(int, input().split()))

# たかだか200なので、全探索で間に合う
min_cost = float('inf')

for x in range(-100, 101):
    cost = 0
    for a in a_list:
        cost += (a - x) ** 2
    if cost < min_cost:
        min_cost = cost

print(min_cost)

Submission status: Accepted

Solution code:
import sys

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

n = ni()
li = na()
ans = float("inf")
for j in range(-100, 101):
    ct = 0
    for i in range(n):
        ct += (li[i] - j) ** 2
    ans = min(ans, ct)
print(ans)

Submission status: Accepted

Solution code:
def cul(li,s):
  a = 0
  for i in li:
    a += (i - s)**2
  
  return a

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

s1 = sum(li)//len(li)
s2 = s1 + 1

ans1 = cul(li,s1)
ans2 = cul(li,s2)

print(min(ans1,ans2))

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
al = min(a)
ar = max(a)
ans = 1000000000000000
for i in range(al, ar+1):
    tmp = 0
    for j in a:
        tmp += (j-i) ** 2
    if ans > tmp:
        ans = tmp
print(ans)

Submission status: Accepted

Solution code:
from statistics import mean

N = int(input())
a_list = [int(i) for i in input().split()]

ave = mean(a_list)

ave = round(ave)

sum = 0

for x in a_list:
    sum = sum + (x - ave) * (x - ave)

print(sum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
  n = int(raw_input())
  row = [int(x) for x in raw_input().split(' ')]
  min_val = float('inf')
  
  is_eq = True
  for x in row:
    if x != row[0]:
      is_eq = False
  if is_eq:
    print('0')
    return
  
  for x in xrange(-100, 101):
    data = [(elem - x)**2 for elem in row]
    if sum(data) < min_val:
      min_val = sum(data)
  print(min_val)

main()

Submission status: Accepted

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

costs = []

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

print(min(costs))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int,input().split()))
b = sum(a)
c = sum(map(lambda x: x ** 2, a))
d = round(b/N) 
ans = N*d**2 - 2*d*b+c
print(int(ans))

Submission status: Accepted

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

ave = round(sum(A) / N)

var = 0

for a in A:
    var += (a - ave) ** 2

print(var)

Submission status: Accepted

Solution code:
n=int(input())
a=[int(j) for j in input().split()]
ans=10**100

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

Submission status: Accepted

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

squares_sums = []
for y in range(min(numbers), max(numbers)+1):
    squares_sums.append(sum([((x - y) ** 2) for x in numbers]))
print(min(squares_sums))

Submission status: Accepted

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

min_n = min(numbers)
max_n = max(numbers)

squares_sums = []
for y in range(min_n, max_n+1):
    squares_sums.append(sum([((x - y) ** 2) for x in numbers]))
print(min(squares_sums))

Submission status: Accepted

Solution code:
import math
n = int(input())
a = list(map(int, input().split()))
ans1 = 0
ans2 = 0
ave1 = math.floor(sum(a) / n)
ave2 = math.ceil(sum(a) / n)
for i in range(n):
    ans1 += (a[i] - ave1) ** 2
    ans2 += (a[i] - ave2) ** 2
print(min(ans1, ans2))

Submission status: Accepted

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

ave = round(sum(a)/n)
cost=0
for i in range(n):
    cost+=(a[i]-ave)**2
print(cost)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import copy
import collections
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush, heapify
import math
import itertools
import random
 
# NO, PAY-PAY
#import numpy as np
#import statistics
#from statistics import mean, median,variance,stdev
 
INF = float('inf')
def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
 
def main():
    N = inputInt()
    a = inputList()
    
    flg = True
    for i,vol in enumerate(a):
        if i == 0:
            continue
        if vol != a[i-1]:
            flg = False
            break
            
    if flg == True:
        print(0)
        sys.exit()
    
    ans = INF
    for i in range(-100,101):
        cst = 0
        for j,vol in enumerate(a):
            cst += (vol-i)**2
            
        if ans > cst:
            ans = cst
            #print("{} {}".format(cst,ans))
            
    print(ans)
    
if __name__ == "__main__":
	main()