N = int(eval(input()))

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



mn = 10**9

for j in range(min(A), max(A)+1):

  cost = 0

  for i in range(N):

    cost += (A[i]-j)**2

  mn = min(cost, mn)

print(mn)
n = int(eval(input()))

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



ans = float('INF')

for i in range(min(a), max(a)+1):

    cost = 0

    for aa in a:

        cost += (i - aa)**2

    if cost < ans :

        ans = cost

print(ans)

N = int(eval(input()))
A = list(map(int, input().split()))
mn = 10**9
for j in range(min(A), max(A) + 1):
    cost = 0
    for i in range(N):
        cost += (A[i] - j) ** 2
    mn = min(cost, mn)
print(mn)

n = int(eval(input()))
a = list(map(int, input().split()))
ans = float("INF")
for i in range(min(a), max(a) + 1):
    cost = 0
    for aa in a:
        cost += (i - aa) ** 2
    if cost < ans:
        ans = cost
print(ans)

-N = int(eval(input())) -A = list(map(int, input().split())) -mn = 10**9 -for j in range(min(A), max(A) + 1): +n = int(eval(input())) +a = list(map(int, input().split())) +ans = float("INF") +for i in range(min(a), max(a) + 1): -    for i in range(N): -        cost += (A[i] - j) ** 2 -    mn = min(cost, mn) -print(mn) +    for aa in a: +        cost += (i - aa) ** 2 +    if cost < ans: +        ans = cost +print(ans)