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: Wrong Answer

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

t = int(mean(a))
ans = 100*len(a)**2
for i in range(t-1, t+2):
    sum = 0
    for j in a:
        sum += (i-j)**2
    ans = min(ans, sum)

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math
n = int(input())
a = [int(_) for _ in input().split()]
b =[]
for i in range(n):
  ave = math.ceil((max(a)+min(a))/2)
  c =( a[i] - ave)**2
  b.append(c)
print(sum(b))

Submission status: Wrong Answer

Solution code:
# C - いっしょ / Be Together
N = int(input())
A = list(map(int, input().split()))
ans = 1000

for X in range(-100,101):
    tmp = 0
    for i in range(N):
        tmp += (A[i]-X)**2
    if ans >= tmp:
        ans = tmp
    else:
        pass

print(ans)

Submission status: Wrong Answer

Solution code:
# C - いっしょ / Be Together
N = int(input())
A = list(map(int, input().split()))
ans = 1000

for X in range(-100,100):
    tmp = 0
    for i in range(N):
        tmp += (A[i]-X)**2
    if ans >= tmp:
        ans = tmp
    else:
        pass
print(ans)

Submission status: Wrong Answer

Solution code:
# C - いっしょ / Be Together
N = int(input())
A = list(map(int, input().split()))
A.sort()
A_s = set(A)

A_s_a = int(sum(A_s)//len(A_s))

if N % 2 != 0:
    if A[N//2+1-1] <= A_s_a:
        pass
    else:
        A_s_a = A_s_a + 1
else:
    pass

ans = 0

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

print(ans)

Submission status: Wrong Answer

Solution code:
# C - いっしょ / Be Together
N = int(input())
A = list(map(int, input().split()))
A.sort()
A_s = set(A)

A_s_a = int(sum(A_s)//len(A_s))

if A[N//2+1-1] <= A_s_a:
    pass
else:
    A_s_a = A_s_a + 1

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

print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
a = int(input())
b = list(map(int, input().split()))
b.sort()
c = (b[0] + b[len(b)-1]) // 2
d = c + 1
mc = 0
md = 0
for i in b:
    mc += abs(i - c) ** 2
    md += abs(i - d) ** 2
print(min(mc, md))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
n=int(input())
num=list(map(int,input().split()))
ans=10**9
s,e=min(num),max(num)
for j in range(s,e+1):
  c=0
  for i in range(n):
    c+=(num[i]-j)**2
  if c<ans:
    ans=c
print(c)

Submission status: Wrong Answer

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

h = sum(a) // N

print(sum(((h - ai)**2 for ai in a)))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from numpy import*
n,*a=map(int,open(0).read().split())
t=(sum(a)-1)//n+1
a=array(a)
print(int(sum((a-t)**2)))

Submission status: Wrong Answer

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

m = round(sum(A) / 2)
print(sum((a - m) ** 2 for a in A))

Submission status: Wrong Answer

Solution code:
sn = input()
xyx = xx = -1
for i in range(1, len(sn)):
    if (xx == -1) and (sn[i-1] == sn[i]):
        xx = i-1 if xyx == -1 else i
    if (xyx == -1) and (i >= 2) and (sn[i-2] == sn[i]):
        xyx = i-2 if xx == -1 else i
if (xx == -1) or (xyx == -1):
    print(-1, -1)
else:
    print(min(xx, xyx)+1, max(xx, xyx)+1)

Submission status: Wrong Answer

Solution code:
sn = input()
l = r = -1
for i in range(1, len(sn)):
    if (sn[i-1] == sn[i]):
        l = i
        r = i+1
        break
    if (i >= 2) and (sn[i-2] == sn[i]):
        l = i
        r = i+2
        break
if (r == -1):
    print(-1, -1)
else:
    print(l, r)

Submission status: Wrong Answer

Solution code:
sn = input()
xyx = xx = -1
for i in range(1, len(sn)):
    if (xx == -1) and (sn[i-1] == sn[i]):
        xx = i-1 if xyx == -1 else i
    if (xyx == -1) and (i > 2) and (sn[i-2] == sn[i]):
        xyx = i-2 if xx == -1 else i
if (xx == -1) or (xyx == -1):
    print(-1, -1)
else:
    print(min(xx, xyx)+1, max(xx, xyx)+1)

Submission status: Wrong Answer

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

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

a_sum = sum(a)

flag = True
sum2 = 0
for i in range(n):
    sum2 += a[i] ** 2
    if i >= 1:
        if a[i] != a[i-1]:
            flag = False

cand = []
if flag:
    cand.append(a[i])
else:
    for i in range(min(a),max(a)+1):
        cand.append(sum2 - 2 * i * a_sum + n * (i ** 2))

print(min(cand))

Submission status: Wrong Answer

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

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

a_sum = sum(a)

flag = True
sum2 = 0
for i in range(n):
    sum2 += a[i] ** 2
    if i >= 1:
        if a[i] != a[i-1]:
            flag = False

cand = []
if flag:
    cand.append(a[i])
else:
    for i in range(min(a),max(a)):
        cand.append(sum2 - 2 * i * a_sum + n * (i ** 2))

print(min(cand))

Submission status: Wrong Answer

Solution code:
import math

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

ans = 10**9+7
for n in range(100):
    temp=0
    for i in a:
        temp += int(abs(n - i)) ** 2
    ans = min(ans,temp)

print(ans)

Submission status: Wrong Answer

Solution code:
import math
import statistics

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

med = statistics.median(a)
ans = 0
for i in a:
    ans += abs(med - i) ** 2
    
print(int(ans))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
s = input()
t = " "+s[:-1]
r = "  "+s[:-2]
for n,(i,j,k) in enumerate(zip(s,t,r)):
    if i == j :
        print(n+1,n+2)
        exit()
    elif i == k:
        print(n+1,n+3)
        exit()
print(-1,-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
n = list(map(int,input().split()))
ave = sum(set(n)) // len(set(n))
costave = 0
for i in range(N):
    costave += (n[i]-ave)**2
costlist = []
for i in range(N):
    cost = 0
    for j in range(N):
        cost += (n[j] - n[i] )**2
    costlist.append(cost)
ans = min(min(costlist),costave)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ave = -(-abs(s)//n)

if s < 0:
    ave = -ave

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

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int, input().split()))
mincost = 10000000000000
for i in range(1, 101):
    cost = 0
    for j in a:
        cost += pow(j - i, 2)
    if mincost > cost:
        mincost = cost
print(mincost)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int, input().split()))
mincost = 1000000
for i in range(1, 101):
    cost = 0
    for j in a:
        cost += pow(j - i, 2)
    if mincost > cost:
        mincost = cost
print(mincost)

Submission status: Wrong Answer

Solution code:
import math
def getinputdata():

    # 配列初期化
    array_result = []
    
    data = input()
    
    array_result.append(data.split(" "))

    flg = 1

    try:

        while flg:

            data = input()

            if(data != ""):
                
                array_result.append(data.split(" "))

            else:

                flg = 0
    finally:

        return array_result

arr_data = getinputdata()

n = int(arr_data[0][0])

arr=[int(arr_data[1][x]) for x in range(n)]

#四捨五入
myround= lambda x: float(math.floor(x + math.copysign(0.5, x)))

if len(set(arr))==1:
    
    print(0)

else:

    target=sum(arr)//len(arr) if sum(arr)%len(arr)==0 else myround(sum(arr)/len(arr))

    mysum=0

    for v in arr:

        mysum+=(v-int(target))**2

    print(mysum)

Submission status: Wrong Answer

Solution code:
def getinputdata():

    # 配列初期化
    array_result = []
    
    data = input()
    
    array_result.append(data.split(" "))

    flg = 1

    try:

        while flg:

            data = input()

            if(data != ""):
                
                array_result.append(data.split(" "))

            else:

                flg = 0
    finally:

        return array_result

arr_data = getinputdata()

n = int(arr_data[0][0])

arr=[int(arr_data[1][x]) for x in range(n)]


if len(set(arr))==1:
    
    print(0)

else:

    s1=set(arr)
    
    target=sum(s1)//len(s1) if sum(s1)%len(s1)==0 else (sum(s1)//len(s1))+1

    mysum=0

    for v in arr:

        mysum+=(v-target)**2

    print(mysum)

Submission status: Wrong Answer

Solution code:
def getinputdata():

    # 配列初期化
    array_result = []
    
    data = input()
    
    array_result.append(data.split(" "))

    flg = 1

    try:

        while flg:

            data = input()

            if(data != ""):
                
                array_result.append(data.split(" "))

            else:

                flg = 0
    finally:

        return array_result

arr_data = getinputdata()

n = int(arr_data[0][0])

arr=[int(arr_data[1][x]) for x in range(n)]


if len(set(arr))==1:
    
    print(0)

else:

    s1=set(arr)

#    s1=arr.copy()
    
    target=sum(s1)//len(s1) if sum(s1)%len(s1)==0 else (sum(s1)//len(s1))+1

    mysum=0

    for v in s1:

        mysum+=(v-target)**2

    print(mysum)

Submission status: Wrong Answer

Solution code:
def getinputdata():

    # 配列初期化
    array_result = []
    
    data = input()
    
    array_result.append(data.split(" "))

    flg = 1

    try:

        while flg:

            data = input()

            if(data != ""):
                
                array_result.append(data.split(" "))

            else:

                flg = 0
    finally:

        return array_result

arr_data = getinputdata()

n = int(arr_data[0][0])

arr=[int(arr_data[1][x]) for x in range(n)]


if len(set(arr))==1:
    
    print(0)

else:

    s1=arr.copy()
    
    target=sum(s1)//len(s1) if sum(s1)%len(s1)==0 else (sum(s1)//len(s1))+1

    mysum=0

    for v in s1:

        mysum+=(v-target)**2

    print(mysum)

Submission status: Wrong Answer

Solution code:
def getinputdata():

    # 配列初期化
    array_result = []
    
    data = input()
    
    array_result.append(data.split(" "))

    flg = 1

    try:

        while flg:

            data = input()

            if(data != ""):
                
                array_result.append(data.split(" "))

            else:

                flg = 0
    finally:

        return array_result

arr_data = getinputdata()

n = int(arr_data[0][0])

arr=[int(arr_data[1][x]) for x in range(n)]

target=sum(arr)//len(arr) if sum(arr)%len(arr)==0 else (sum(arr)//len(arr))+1

mysum=0

for v in arr:
    
    mysum+=(v-target)**2
    
print(mysum)

Submission status: Wrong Answer

Solution code:
import math
N = int(input())
a = list(map(int, input().split()))
a.sort()
ans = 0
median = 0
a_max = max(a)
a_min = min(a)
middle = math.ceil((a_max + a_min) / 2)
for i in range(len(a)):
	ans += pow(middle - a[i], 2)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int, input().split()))
a.sort()
ans = 0
median = 0
if N % 2 == 0:
	median = int((a[int(N/2) - 1] + a[int(N/2)]) / 2)
else:
	median = a[int(N/2)]
for i in range(len(a)):
	ans += pow(median - a[i], 2)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int, input().split()))
a.sort()
ans = 0
median = 0
if N % 2 == 0:
	median = int((a[int(N/2) - 1] + a[int(N/2)]) / 2)
else:
	median = a[int(N/2)]
print(median)
for i in range(len(a)):
	ans += pow(median - a[i], 2)
print(ans)

Submission status: Wrong Answer

Solution code:
import math
import numpy as np
N = int(input())
Ary = np.array(list(map(int, input().split(' '))))
goal = math.ceil(np.mean(Ary))
Ary = np.power(Ary - goal,2)
print(np.sum(Ary))

Submission status: Wrong Answer

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

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

print(cost)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
ave=sum(a)//n
mi=10**15
c=0
for i in a:
  c+=(i-ave)**2
mi=min(mi,c)
ave+=1
for i in a:
  c+=(i-ave)**2
mi=min(mi,c)
ave-=2
for i in a:
  c+=(i-ave)**2
mi=min(mi,c)
print(mi)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def solve():
    N = int(input())
    a = list(map(int, input().split()))
    ans = 100000
    for i in range(min(a),max(a)+1):
        cost = 0
        for a_i in a:
            cost += abs(i-a_i)**2
        ans = min(ans, cost)
    print(ans)
    
solve()

Submission status: Wrong Answer

Solution code:
def solve():
    N = int(input())
    a = list(map(int, input().split()))
    ans = 100000
    for i in range(-100,101):
        cost = 0
        for a_i in a:
            cost += abs(i-a_i)**2
        ans = min(ans, cost)
    print(ans)
    
solve()

Submission status: Wrong Answer

Solution code:
def main(n, ns):
    if len(set(ns)) == 1:
        return 0
    
    min_v  = min(ns)
    max_v  = max(ns)
    answer = pow(200, 2)
    for i in range(min_v, max_v + 1):
        cost = 0
        for j in ns:
            if i == j:
                continue
            cost += pow((j - i), 2)
        answer = min(answer, cost)
    return answer
 
 
if __name__ == '__main__':
    n = int(input())
    ns = [int(e) for e in input().split()]
    print(main(n, ns))

Submission status: Wrong Answer

Solution code:
def main(n, ns):
    if len(set(ns)) == 1:
        return 0
    
    min_v  = min(ns)
    max_v  = max(ns)
    answer = pow(100, 2)
    for i in range(min_v, max_v + 1):
        cost = 0
        for j in ns:
            if i == j:
                continue
            cost += pow((j - i), 2)
        answer = min(answer, cost)
    return answer
 
 
if __name__ == '__main__':
    n = int(input())
    ns = [int(e) for e in input().split()]
    print(main(n, ns))

Submission status: Wrong Answer

Solution code:
def main(n, ns):
    if len(set(ns)) == 1:
        return 0
    
    min_v  = -100
    max_v  = 101
    answer = pow(100, 2)
    for i in range(min_v, max_v):
        cost = sum([(n - i) ** 2 for n in ns])
        answer = min(answer, cost)
    return answer
 
 
if __name__ == '__main__':
    n = int(input())
    ns = [int(e) for e in input().split()]
    print(main(n, ns))

Submission status: Wrong Answer

Solution code:
def main(n, ns):
    if len(set(ns)) == 1:
        return 0
    
    min_v  = min(ns)
    max_v  = max(ns)
    answer = pow(100, 2)
    for i in range(min_v, max_v + 1):
        cost = 0
        for j in ns:
            if i == j:
                continue
            cost += pow((j - i), 2)
        answer = min(answer, cost)
    return answer


if __name__ == '__main__':
    n = int(input())
    ns = [int(e) for e in input().split()]
    print(main(n, ns))

Submission status: Wrong Answer

Solution code:

def main(n, ns):
    if len(set(ns)) == 1:
        return 0
    
    min_v  = min(ns)
    max_v  = max(ns)
    answer = pow(100, 2)
    for i in range(min_v, max_v + 1):
        cost = 0
        for j in ns:
            if i == j:
                continue
            cost += pow((j - i), 2)
        answer = min(answer, cost)
    return answer


if __name__ == '__main__':
    n = int(input())
    ns = [int(e) for e in input().split()]
    print(main(n, ns))

Submission status: Wrong Answer

Solution code:
# coding: utf-8

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

a.sort()

ans = 1000000007
for x in range(a[0], a[n - 1]):
    keep = 0
    for i in range(n):
        keep += (a[i] - x) * (a[i] - x)
    ans = min(keep, ans)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(1000000)
def i():
    return int(raw_input())
def s():
    return raw_input()

#memoize macro
def memoize(f):
    cache = {}
    def helper(*args):
        if args not in cache:
            cache[(args)] = f(*args)
        return cache[args]
    return helper
 
@memoize
def nck(a,b,m):
    b = min([a-b,b])
    if (b>a or b<0 or a<0):
        return 0
    elif a == 0:
        return 1
    return (nck(a-1,b-1,m)+nck(a-1,b,m)) % m

Q=10**9+7
@memoize
def dp(n,i):
    if n<i:
        return 0
    elif n==1:
        return 1
    if i==0:
        return (dp(n-1,1)*2+dp(n-1,0))%Q
    else:
        return (dp(n-1,i-1)+dp(n-1,i+1)*2)%Q
 
n=i()
s=s()
print dp(n,len(s))

Submission status: Wrong Answer

Solution code:
# coding: utf-8
t = str(raw_input())
def f(t):
    for start in range(0, len(t)-1):
        for end in range(start + 2, len(t)):
            sep_t = t[start: end]
            for value in sep_t:
                count = sep_t.count(value)
                if count>float(len(sep_t))/2:
                    return start, end
    return -1, -1

start, end = f(t)
print start, end

Submission status: Wrong Answer

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

sm,avg,cost=0,0,0

for el in l:
	sm+=el
avg=sm//n
if sm%n!=0:
	avg+=1
# print avg
for el in l:
	cost+=(avg-el)**2

print cost

Submission status: Wrong Answer

Solution code:
#D WA
s = list(input())
n = len(s)

for i in range(n - 1):
    if s[i] == s[i + 1]:
        print(i + 1, i + 2)
        exit()

alp = [chr(i) for i in range(97, 97+26)]

for j in range(26):
    num = [0] * n

    for i in range(n):
        if s[i] == alp[j]:
            num[i] += 1

    start = 0
    end = 3

    while True:
        #print(start, end, n)
        #print(sum(num[start:end]),(end - start) / 2)

        if start >= n or end > n:
            break

        if num[start] == 0:
            start += 1
            end += 1
            continue

        if sum(num[start:end]) > (end - start) / 2:
            print(start + 1, end)
            exit()

        if sum(num[start:end]) > (end - start) / 2 - 1:
            end += 1

        else:
            start += 1
            if end - start < 3:
                end += 1

print(-1, -1)

Submission status: Wrong Answer

Solution code:
# coding: utf-8
def count_char_max(text):

  a = "abcdefghijklmnopqrstuvwxyz"
  l = [0] * 26

  for i in text:

    ascNum = ord(i.lower()) - 97

    if ascNum < 0 or 25 < ascNum:
      continue

    l[ascNum] += 1

  return max(l)

s = raw_input()
t = ""
c = 1

for i in range(len(s)):

  t += s[i]

  if count_char_max(t) > len(t) / 2 + 1:
    print "%d %d", c, c + len(t) - 1
    exit()

print "-1 -1"

Submission status: Wrong Answer

Solution code:
# coding: utf-8

def cost(n, a, m):

  hihumi = 0  #hihumi => 滝本ひふみ

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

  return hihumi

N = int(raw_input())
a = map(int, raw_input().split())

ans = 100

for i in range(min(a), max(a) + 1):
  if (ans > cost(N, a, i)):
    ans = cost(N, a, i)

print ans

Submission status: Wrong Answer

Solution code:
import sys
S = input().strip()

for i in range(3,len(S)+1):
    for j in range(0,len(S)+1-i):
        s = S[j:j+i]
        for k in set(s):
            if s.count(k)*2 > len(s):
                print(k)
                print(s)
                print(j+1,j+i)
                sys.exit(0)

print(-1,-1)

Submission status: Wrong Answer

Solution code:
def cost(n, a, m):

  hihumi = 0

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

  return hihumi

N = int(raw_input())
a = map(int, raw_input().split())

ans = max(a)

for i in range(min(a), max(a) + 1):
  if (ans > cost(N, a, i)):
    ans = const(N, a, i)

print ans

Submission status: Wrong Answer

Solution code:
def cost(n, a, m):

  hihumi = 0

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

  return hihumi

N = int(raw_input())
a = map(int, raw_input().split())

ans = max(a)

for i in range(min(a), max(a)):
  if (ans > cost(N, a, i)):
    ans = const(N, a, i)

print ans

Submission status: Wrong Answer

Solution code:
N = int(raw_input())
a = map(int,raw_input().split())
ans =  10**9+7
for i in xrange(-100,100):
	cost = sum((i-a[j])**2 for j in xrange(N))
	if cost < ans:
		ans = cost
print ans

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N = int(input())
m = [ int(i) for i in input().strip().split(" ")]

k_tmp = float(sum(m))/len(m)

k1 = int(np.ceil(k_tmp))
k2 = int(np.floor(k_tmp))

res1 = sum([(x-k1) * (x-k1) for x in m])
res2 = sum([(x-k2) * (x-k2) for x in m])
res = 0
if res1 > res2:
    res = res2
else:
    res = res1
print(res)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split()))
    ans = float('inf')
    for k in range(min(A), max(A)+1):
        cost = 0
        for a in A:
            cost += (a-k)**2
        ans = min(ans, cost)
        if ans == 0:
            break
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
ans = 4000000
for a in range(-100, 101):
  m = 0
  for b in A:
    m += (a - b) ** 2
  if m < ans:
    ans = m
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
avg_a = sum(a) // n
avg_b = avg_a + 1
total_one = 0
total_two = 0
for num in a:
    total_one += (num - avg_a) ** 2
    total_two += (num - avg_b) ** 2
print(min(total_one, total_two))

Submission status: Accepted

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

x = sum(aa) // len(aa)
cost1 = 0
cost2 = 0
for i in range(n):
  cost1 += (x - aa[i])**2
  cost2 += (x + 1 - aa[i])**2
print(min(cost1, cost2))

Submission status: Accepted

Solution code:
n = int(input())
x = sorted(list(map(int, input().split())), reverse = True)
x_2 = list(map(lambda x:x ** 2, x))
ichi = sum(x)
jijou = sum(x_2)
re = 10 ** 100


#def ji(p, x):
#    ans = p * (p - 2 * x)
#    return ans

for p in range(x[-1], x[0] + 1):
    second = n * (p ** 2)
    third = -2 * p * ichi
    summ = jijou + second + third

    if re > summ:
        re = summ

print(re)

#for i in range(x[i]):

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#82 C - いっしょ
N = int(input())
A = list(map(int,input().split()))

X = round(sum(A)/N)

ans = 0
for a in A:
    ans += (X-a)**2
print(ans)

Submission status: Accepted

Solution code:
from decimal import Decimal, ROUND_HALF_UP
N = int(input())
a = list(map(int, input().split()))
f = sum(a)/N
t = Decimal(str(f)).quantize(Decimal('0'), rounding=ROUND_HALF_UP)
c = 0
for i in range(N):
    c += (a[i]-t)**2
print(c)

Submission status: Accepted

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

p = sum(A)//n
q = p+1
r = p-1

ans = float("inf")
for x in [p,q,r]:
    tmp = 0
    for a in A:
        tmp += (a-x)**2
    ans = min(ans, tmp)
    
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
med_l = int(sum(a)/n)
med_h = med_l + 1
sum_l = 0
sum_h = 0

for i in range(n):
  sum_l += (med_l - a[i])**2
  sum_h += (med_h - a[i])**2
  
print(min(sum_l, sum_h))

Submission status: Accepted

Solution code:
import math
n=int(input())
a=list(map(int, input().split(" ")))
ave = round(sum(a)/len(a))
ans = 0
for i in a:
  ans += (ave-i)** 2
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):
  tmp = 0
  for v in a:
    tmp += (i-v)**2
  ans = min(ans,tmp)
print(ans)

Submission status: Accepted

Solution code:
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():
    n = I()
    a_lst = LI()
    ans = float("inf")
    for i in range(-100,100+1):
        cost = 0
        for a in a_lst:
            cost += (a-i)**2
        ans = min(ans, cost)
    print(ans)
main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from sys import exit
from collections import deque
from bisect import bisect_left, bisect_right, insort_left, insort_right #func(リスト,値)
from heapq import heapify, heappop, heappush
from itertools import product, permutations, combinations, combinations_with_replacement
from functools import reduce
from math import sin, cos, tan, asin, acos, atan

sys.setrecursionlimit(10**6)
INF = 10**20
eps = 1.0e-20
MOD = 10**9+7

def lcm(x,y):
    return x*y//gcd(x,y)
def lgcd(l):
    return reduce(gcd,l)
def llcm(l):
    return reduce(lcm,l)
def powmod(n,i,mod):
    return pow(n,mod-1+i,mod) if i<0 else pow(n,i,mod)
def div2(x):
    return x.bit_length()
def div10(x):
    return len(str(x))-(x==0)
def perm(n,mod=None):
    ans = 1
    for i in range(1,n+1):
        ans *= i
        if mod!=None:
            ans %= mod
    return ans
def intput():
    return int(input())
def mint():
    return map(int,input().split())
def lint():
    return list(map(int,input().split()))
def ilint():
    return int(input()), list(map(int,input().split()))
def judge(x, l=['Yes', 'No']):
    print(l[0] if x else l[1])
def lprint(l, sep='\n'):
    for x in l:
        print(x, end=sep)
def ston(c, c0='a'):
    return ord(c)-ord(c0)
def ntos(x, c0='a'):
    return chr(x+ord(c0))
class counter(dict):
    def __init__(self, *args):
        super().__init__(args)
    def add(self,x):
        self.setdefault(x,0)
        self[x] += 1
class comb():
    def __init__(self, n, mod=None):
        self.l = [1]
        self.n = n
        self.mod = mod
    def get(self,k):
        l,n,mod = self.l, self.n, self.mod
        k = n-k if k>n//2 else k
        while len(l)<=k:
            i = len(l)
            l.append(l[i-1]*(n+1-i)//i if mod==None else (l[i-1]*(n+1-i)*powmod(i,-1,mod))%mod)
        return l[k]

N,A = ilint()
ans = INF
for x in range(-100,101):
    tmp = 0
    for y 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()))
ave = round(sum(A) / n)
ans = 0
for a in A:
    ans += (a - ave) ** 2
print(ans)

Submission status: Accepted

Solution code:
n, *a = map(int, open(0).read().split())
if sum(a) % n == 0:
    avg = sum(a)//n
    ans = 0
    for e in a:
        ans += (e - avg)**2
    print(ans)
else:
    avg = sum(a)//n + 1
    avg2 = sum(a)//n - 1
    avg3 = sum(a)//n
    ans2 = 0
    ans = 0
    ans3 = 0
    for e in a:
        ans += (e - avg) ** 2
        ans2 += (e-avg2) ** 2
        ans3 += (e-avg3) ** 2
    print(min(ans, ans2, ans3))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = 10000000000
for num in range(-100, 101):
    kari_ans = 0
    for _a in a:
        kari_ans += (num - _a) ** 2
    ans = min(ans, kari_ans)
print(ans)

Submission status: Accepted

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