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:
mini=10**29 
n=int(input())
l=[int(i) for i in input().split()]
for i in l:
    sm=0  
    for j in l:
        sm+=(i-j)*(i-j)
    mini=min(mini,sm)
print(mini)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
cons = int(sum(A)/N)
ans = 0 
for i in range(N):
  ans += (A[i] - cons)**2 

print(ans)

Submission status: Wrong Answer

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

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 = map(int, input().split())
    print(check(n=N, numbers=numbers))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Q=0
for i in a:
    Q+=(i-math.ceil(sum(a)/len(a)))**2
print(Q)

Submission status: Wrong Answer

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

b = sum(a) // n
if sum(a) % n != 0:
    b += 1
ans = 0
for i in range(n):
    ans += (a[i] - b)**2
print(ans)

Submission status: Wrong Answer

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

Solution code:
def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    ans = 2*(10**4)
    for i in range(-100,101):
        tmp = 0
        for a in A:
            tmp += (a - i)**2
        ans = min(ans,tmp)
    from math import ceil
    avg = ceil(sum(A)/N)
    ans1 = 0
    ans2 = 0
    for a in A:
        ans1 += (a - avg)**2
        ans2 += (a - avg-1)**2
    print(min(ans,ans1,ans2))
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    ans = 2*(10**4)
    for i in range(-100,101):
        tmp = 0
        for a in A:
            tmp += (a - i)**2
        ans = min(ans,tmp)
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    from math import ceil
    avg = ceil(sum(A)/N)
    ans = 0
    ans2 = 0
    for a in A:
        ans += (a - avg)**2
        ans2 += (a - avg-1)**2
    print(min(ans,ans2))
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N = int(input())
    A = [int(i) for i in input().split()]
    from math import ceil
    avg = ceil(sum(A)/N)
    ans = 0
    for a in A:
        ans += (a - avg)**2
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import math

in_kosu=int(input())
in_lis=input().split()
gou = 0
kekka = 0
kekka_1 = 0

for i in range(in_kosu):
    gou = gou + int(in_lis[i])*2
gou = gou / in_kosu
gou_kili = math.ceil(gou)
gou_kili = gou_kili/2

for j in range(in_kosu):
    kekka = (gou_kili - int(in_lis[j]))*(gou_kili - int(in_lis[j]))
    kekka_1 = kekka_1 + kekka
print(str(int(kekka_1)))

Submission status: Wrong Answer

Solution code:
n = int(input())
a = sorted(list(map(int, input().split())))
diff = a[-1] - a[0]
num = diff // 2
target = 0
ans = 0

if diff % 2:
    c = a.count(a[-1] - num)
    d = a.count(a[0] + num)
    if c >= d:
        target = a[-1] - num
    else:
        target = a[0] + num
else:
    target = a[0] + num

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

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = input()

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

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

a2 = list(map(calc_sq, a))

sum_a = sum(a)

sum_a2 = sum(a2)

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

print(min(cost))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:

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


ans = 2**30-1
for a in A:
    tmp = 0
    for b in A:
        tmp += (b-a)*(b-a)
    ans = min(ans,tmp)
print(ans)

Submission status: Wrong Answer

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

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

print(min(cost))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math

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

ave = math.ceil(sum(l) / len(l))
print(sum(list(map(lambda x: (x-ave)**2, l))))

Submission status: Wrong Answer

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, 100):
    temp = sum([f(x, k) for x in A])
    ans = min(ans, temp)
print(ans)

Submission status: Wrong Answer

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

sum = 0
result = 0

for i in A:
    sum+=i
ave = math.ceil(sum/len(A))

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

print(math.ceil(result))

Submission status: Wrong Answer

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

sum = 0
result = 0

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

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

print(round(result))

Submission status: Wrong Answer

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

sum = 0
result = 0

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

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

print(math.ceil(result))

Submission status: Wrong Answer

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

sum = 0
result = 0

for i in A:
    sum+=i
ave = math.ceil(sum/len(A))

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

print(result)

Submission status: Wrong Answer

Solution code:
N = input()
a = list(map(int,input().split()))
cost_sum = 0
print_cost = 20001

for i in range(-100, 101):
    for j in range(len(a)):
        cost_sum += (a[j] - i) ** 2
    if print_cost > cost_sum:
        print_cost = cost_sum
    cost_sum = 0

print(print_cost)

Submission status: Wrong Answer

Solution code:
N = input()
a = list(map(int,input().split()))
cost_sum = 0
print_cost = 20000

for a_i in range(min(a), max(a)+1):
    for a_j in a:
        cost_sum += ((a_j - a_i) ** 2)
    if print_cost > cost_sum:
        print_cost = cost_sum

print(print_cost)

Submission status: Wrong Answer

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

for a_i in range(min(a), max(a)+1):
    cost_sum = 0
    for a_j in a:
        cost_sum += ((a_j - a_i) ** 2)
    if print_cost > cost_sum:
        print_cost = cost_sum

print(print_cost)

Submission status: Wrong Answer

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

for a_i in range(min(a), max(a)+1):
    cost_sum = 0
    for a_j in a:
        cost_sum += ((a_j - a_i) ** 2)

print(cost_sum)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from math import ceil

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

Submission status: Wrong Answer

Solution code:
from math import ceil

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

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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()))

min_a, max_a = min(a_list), max(a_list)
w_list = list(range(min_a, max_a+1))
m1 = statistics.median_low(w_list)
m2 = statistics.median_high(w_list)

cost_1 = 0
cost_2 = 0
for a in a_list:
    cost_1 += (a - m1) ** 2
    cost_2 += (a - m2) ** 2

ans = min(cost_1, cost_2)
print(ans)

Submission status: Wrong Answer

Solution code:
def cul(li,s):
  a = 0
  for i in li:
    a += (i - s)**2
  print(a)
  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: Wrong Answer

Solution code:
N = int(input())
a = list(map(int, input().split()))
al = min(a)
ar = max(a)
ans = 20000
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: Wrong Answer

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(1, max(row)+1):
    data = [(elem - x)**2 for elem in row]
    if sum(data) < min_val:
      min_val = sum(data)
  print(min_val)

main()

Submission status: Wrong Answer

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

print(n)
print(a)

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

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

costs = []

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

print(min(costs))

Submission status: Wrong Answer

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

ans = []
for i in range(-100, 100):
    total = 0
    for ai in a:
        total += (ai - i)**2
    ans.append(total)

print(min(ans))

Submission status: Wrong Answer

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

ans = []
for i in range(-100, 100):
    total = 0
    for ai in a:
        total += (ai - i)**2
    ans.append(total)

print(min(ans))

Submission status: Wrong Answer

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

stack = []

for i in range(n):
    stack.append(a[i])
    stack = stack[::-1]
print(*stack)

Submission status: Wrong Answer

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

stack = []

for i in range(n):
    stack.append(a[i])
    stack.reverse()

st = ""

for i in range(len(stack)):
    st = st + str(stack[i])

print(" ".join(st))

Submission status: Wrong Answer

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

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

min_sum = (max_n - min_n)**2
for y in range(min_n, max_n+1):
    temp_sum = sum([(x - y) ** 2 for x in numbers])
    if temp_sum < min_sum:
        min_sum = temp_sum
print(min_sum)

Submission status: Wrong Answer

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(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()

Submission status: Wrong Answer

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(max(ans))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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(max(ans))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

num = (sum(a)/n)

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

print(math.ceil(ans))

Submission status: Wrong Answer

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

num = math.ceil(sum(a)/n)

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

print(ans)

Submission status: Wrong Answer

Solution code:
import copy
n = int(input())
a = list(map(int, input().split()))
a_avg = int(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: Wrong Answer

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

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

Submission status: Wrong Answer

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

a.sort()

d=[[0]*N for i in range(N)]
for i in range(N):
  for j in range(1,N):
    if j>i and a[j]!=a[j-1]:
      d[i][j]=(a[i]-a[j])**2
    else:
      d[i][j]=d[j][i]

print(min([sum(d[i]) for i in range(N)]))

Submission status: Wrong Answer

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

ans = 20100
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: Wrong Answer

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

avg = sum(a) // N
ans = 20100
for i in range(avg, avg + 2):
    tmp = 0
    for j in range(N):
        tmp += (a[j] - i) ** 2
    ans = min(ans, tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
N = raw_input()
a = raw_input().split()
ave = 0
for i in a:
    ave += int(i)

ave = -(-ave // int(N))

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

Submission status: Wrong Answer

Solution code:
import math

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

mean = math.ceil(sum(a) / n)

ans = 0

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

print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
mylist=[]
s=0
mylist=[int(x) for x in input().split()] 
for x in range(n):
     s=mylist[x]+s
t=s//n

for y in range(n):
    s=s+(t-mylist[y])**2

print(s)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math
from statistics import median

N = int(input())
a = [int(_) for _ in input().split()]

maxi = max(a)
mini = min(a)
ceiling = math.ceil((maxi + mini) / 2)
floor = math.floor((maxi + mini) / 2)

med = median(a)

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

median_sum = sum([(med - item) ** 2 for item in a])

print(min(ceil_sum, floor_sum, median_sum))

Submission status: Wrong Answer

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

maxi = max(a)
mini = min(a)
ceiling = math.ceil((maxi + mini) / 2)
floor = math.floor((maxi + mini) / 2)

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

print(min(ceil_sum, floor_sum))

Submission status: Wrong Answer

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

print(max(A)+1)

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

Submission status: Wrong Answer

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(0, 100):
        cost = 0
        for a in A:
            cost += (a - i)*(a - i)
        total_costs[i] = cost
    print(min(total_costs.values()))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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(1,101):
    cnt = 0
    for j in d.keys():
        cnt += d[j]*(i-j)**2
    ans.append(cnt)

print(min(ans))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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()

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

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


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


ans = 100000
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: Wrong Answer

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

sol = 10 ** 15
for i in range(-100, 100):
	tmp = 0
	for j in range(n):
		tmp += (num[j] - i) ** 2  
	sol = min(sol, tmp)
print(int(sol))

Submission status: Wrong Answer

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

sol = 10 ** 9
for i in range(-100, 100):
	tmp = 0
	for j in range(n):
		tmp += (num[j] - i) ** 2  
	sol = min(sol, tmp)
print(int(sol))

Submission status: Wrong Answer

Solution code:
from math import floor
n = int(input())
num = list(map(int, input().split()))
set_num = set(num)
mid = floor(sum(set_num) / len(set_num))
sol = 0
for i in num:
	sol += (i - mid) ** 2
print(int(sol))

Submission status: Wrong Answer

Solution code:
n = int(input())
num = list(map(int, input().split()))
set_num = set(num)
mid = sum(set_num) / len(set_num)
sol = 0
for i in num:
	sol += (i - mid) ** 2
print(int(sol))

Submission status: Wrong Answer

Solution code:
from decimal import Decimal, ROUND_HALF_UP
n = int(input())
a = list(map(int, input().split()))
ave = sum(a)/len(a)
s = 0
for i in range(n):
    s += pow(a[i]-ave, 2)

print(Decimal(str(s)).quantize(Decimal('0'), rounding=ROUND_HALF_UP))

Submission status: Wrong Answer

Solution code:
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
n = int(input())
a = list(map(int, input().split()))
ave = sum(a)/len(a)
s = 0
for i in range(n):
    s += pow(a[i]-ave, 2)

print(Decimal(str(s)).quantize(Decimal('0'), rounding=ROUND_HALF_UP))

Submission status: Wrong Answer

Solution code:
N, *a = map(int, open(0).read().split())
if sum(a) >= 0:
    tgt = -(-sum(a) // N)
else:
    tgt = sum(a) // N
print(sum((v - tgt) ** 2 for v in a))

Submission status: Wrong Answer

Solution code:
N, *a = map(int, open(0).read().split())
tgt = -(-sum(a) // N)
print(sum((v - tgt) ** 2 for v in a))

Submission status: Wrong Answer

Solution code:
a=int(input())
b=list(map(int,input().split()))
cost=999999
for i in range(min(b),max(b)+1):
    c=999999
    for j in b:
        c=c+(i-j)**2
    if c<cost:
        cost=c
print(cost)

Submission status: Wrong Answer

Solution code:
a=int(input())
b=list(map(int,input().split()))
cost=999999
for i in range(-100,100):
    c=0
    for j in b:
        c=c+(i-j)**2
    if c<cost:
        cost=c
print(cost)

Submission status: Wrong Answer

Solution code:
a=int(input())
b=list(map(int,input().split()))
cost=999999
for i in range(min(b),max(b)):
    c=0
    for j in b:
        c=c+(i-j)**2
    if c<cost:
        cost=c
print(cost)

Submission status: Wrong Answer

Solution code:
a=int(input())
b=list(map(int,input().split()))
cost=999999
for i in range(min(b),max(b)+1):
    c=0
    for j in b:
        c=c+(i-j)**2
    if c<cost:
        cost=c
print(cost)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[int(i) for i in input().split()]
d=(max(a)+min(a))
t=[d//2,(d+1)//2]
ans=[0]*2
for i in a:
    ans[0]+=(i-t[0])**2
    ans[1]+=(i-t[1])**2
print(min(ans))

Submission status: Wrong Answer

Solution code:
n,a=open(0);*a,=map(int,a.split());print(min(sum((i-j-99)**2for j in a)for i in range(201)))

Submission status: Wrong Answer

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

buf = []
mincost = float('inf')

for i in range(a[0], a[-1]+1):
  buf = list(map(lambda x: abs(x - i), a))
  cost = sum(list(map(lambda x: x ** 2, buf)))
  
  if mincost > cost:
    mincost = cost
  
print(mincost)

Submission status: Wrong Answer

Solution code:
import math
n = int(input())
l = list(map(int, input().split()))
cnt1 = 0
cnt2 = 0
cnt3 = 0
cnt4 = 0
cnt5 = 0
cnt6 = 0
cnt7 = 0

x = math.ceil((max(l) + min(l)) / 2)

for i in l:
	cnt1 += (x - i) * (x - i)

for i in l:
	cnt2 += (x - 1 - i) * (x - 1 - i)

for i in l:
	cnt3 += (x + 1 - i) * (x + 1 - i)

for i in l:
	cnt4 += (x - 2 - i) * (x - 2 - i)

for i in l:
	cnt5 += (x + 2 - i) * (x + 2 - i)

for i in l:
	cnt6 += (x - 3 - i) * (x - 3 - i)

for i in l:
	cnt7 += (x + 3 - i) * (x + 3 - i)

print(min(cnt1, cnt2, cnt3, cnt4, cnt5, cnt6, cnt7))

Submission status: Wrong Answer

Solution code:
import math
n = int(input())
l = list(map(int, input().split()))
cnt1 = 0
cnt2 = 0
cnt3 = 0
x = math.ceil((max(l) + min(l)) / 2)

for i in l:
	cnt1 += (x - i) * (x - i)

for i in l:
	cnt2 += (x - 1 - i) * (x - 1 - i)

for i in l:
	cnt3 += (x + 1 - i) * (x + 1 - i)

print(min(cnt1, cnt2, cnt3))

Submission status: Wrong Answer

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

ans = 0
if min(A)==max(A):
    pass
else:
    import statistics
    th_mean = statistics.mean(A)
    th_median = statistics.median(A)
    if th_mean==th_median:
        for i in range(N):
            ans += (A[i]-th_mean)**2
    else:
        if th_mean>th_median:
            th_max = int((th_mean+0.5)//1)
            th_min = int(th_median)
        else:
            th_max = int((th_median+0.5)//1)
            th_min = int(th_mean)
            
        ans = 10000000
        for i in range(th_min, th_max+1):
            ans_temp = 0
            for j in range(N):
                ans_temp += (A[j]-i)**2
            if ans_temp < ans:
                th = i
                ans = ans_temp
print(ans)

Submission status: Wrong Answer

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

ans = 0
if min(A)==max(A):
    pass
else:
    import statistics
    th_mean = statistics.mean(A)
    th_median = statistics.median(A)
    if th_mean==th_median:
        for i in range(N):
            ans += (A[i]-th_mean)**2
    else:
        if th_mean>th_median:
            th_max = int((th_mean+0.5)//1)
            th_min = int(th_median)
        else:
            th_max = int((th_median+0.5)//1)
            th_min = int(th_mean)
            
        ans = 1000000
        for i in range(th_min, th_max+1):
            ans_temp = 0
            for j in range(N):
                ans_temp += (A[j]-i)**2
            if ans_temp < ans:
                th = i
                ans = ans_temp
print(ans)

Submission status: Wrong Answer

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

sumA = sum(A)
if sumA >= 0:
    mean = (sumA+len(A)-1)//len(A)
else:
    mean = (sumA-len(A)+1)//len(A)
ans = 0
for a in A:
    ans += (mean-a)**2
print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
def main():
	N = int, input().split()
	a = list(map(int, input().split()))
	min_a = a[0]
	max_a = a[0]

	for ai in a:
		min_a = min(min_a, ai)
		max_a = max(min_a, ai)
	ans = 10**9
	for i in range(min_a, max_a+1):
		this_ans = 0
		for ai in a:
			this_ans += (ai-i)*(ai-i)
		ans = min(ans, this_ans)
	print(ans)


if __name__ == '__main__':
	main()

Submission status: Wrong Answer

Solution code:
from math import ceil
n = int(input())
x = list(map(int,input().split()))
# y = set(x)
ans = ceil(sum(x)/n)
g = 0
for i in x:
    g += (i-ans)**2
print(g)

Submission status: Wrong Answer

Solution code:
from math import ceil
n = int(input())
x = list(map(int,input().split()))
y = set(x)
m = len(y)
ans = ceil(sum(y)/m)
g = 0
for i in x:
    g += (i-ans)**2
print(g)

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
L = [int(_) for _ in input().split()]


done = []
total = 100000000000000000
tot2 = 0
for i in range(n):
    if L[i] not in done:
        tot = 0
        goal = L[i]
        for j in range(n):
            tot += abs(L[j] - goal)**2
        if tot < total:
            total = tot
        done.append(L[i])
        
av = sum(done)
for i in range(n):
    tot2 += abs(L[i] - av)**2
    
if len(done) == 1:
    print(0)
else:
    print(min(total, tot2))

Submission status: Wrong Answer

Solution code:
n = int(input())
L = [int(_) for _ in input().split()]
av = sum(L) // 2

done = []
total = 100000000000000000
tot2 = 0
for i in range(n):
    tot2 += abs(L[i] - av)**2
    if L[i] not in done:
        tot = 0
        goal = L[i]
        for j in range(n):
            tot += abs(L[j] - goal)**2
        if tot < total:
            total = tot
        done.append(L[i])

if len(done) == 1:
    print(0)
else:
    print(min(total, tot2))

Submission status: Wrong Answer

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