import sys
import math
input = sys.stdin.readline
ins = lambda: input().rstrip()
ini = lambda: int(input().rstrip())
inm = lambda: map(int, input().split())
inl = lambda: list(map(int, input().split()))

n = ini()
a = inl()
x = sum(a) // n
ans = 0
for i in a:
    ans += (i - x) * (i - x)
x = math.ceil(sum(a) / n)
ans2 = 0
for i in a:
    ans2 += (i - x) * (i - x)
print(min(ans, ans2)) import sys sys import math math input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline ins = lambda: input().rstrip() ins lambda: input().rstrip() input().rstrip() ().rstrip () input rstrip ini = lambda: int(input().rstrip()) ini lambda: int(input().rstrip()) int(input().rstrip()) int input().rstrip() ().rstrip () input rstrip inm = lambda: map(int, input().split()) inm lambda: map(int, input().split()) map(int, input().split()) map int input().split() ().split () input split inl = lambda: list(map(int, input().split())) inl lambda: list(map(int, input().split())) list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split n = ini() n ini() ini a = inl() a inl() inl x = sum(a) // n x sum(a) // n sum(a) sum a n ans = 0 ans 0 for i in a:
    ans += (i - x) * (i - x) i a ans += (i - x) * (i - x) ans (i - x) * (i - x) i - x i x i - x i x x = math.ceil(sum(a) / n) x math.ceil(sum(a) / n) math.ceil math ceil sum(a) / n sum(a) sum a n ans2 = 0 ans2 0 for i in a:
    ans2 += (i - x) * (i - x) i a ans2 += (i - x) * (i - x) ans2 (i - x) * (i - x) i - x i x i - x i x print(min(ans, ans2)) print min(ans, ans2) min ans ans2 ins = lambda: input().rstrip() lambda: input().rstrip() ins ini = lambda: int(input().rstrip()) lambda: int(input().rstrip()) ini input = sys.stdin.readline sys.stdin.readline input x = math.ceil(sum(a) / n) math.ceil(sum(a) / n) x x = sum(a) // n sum(a) // n x a = inl() inl() a ans2 += (i - x) * (i - x) (i - x) * (i - x) ans2 inl = lambda: list(map(int, input().split())) lambda: list(map(int, input().split())) inl ans += (i - x) * (i - x) (i - x) * (i - x) ans ans = 0 0 ans n = ini() ini() n ans2 = 0 0 ans2 inm = lambda: map(int, input().split()) lambda: map(int, input().split()) inm
import sys
import math
input = sys.stdin.readline
ins = lambda: input().rstrip()
ini = lambda: int(input().rstrip())
inm = lambda: map(int, input().split())
inl = lambda: list(map(int, input().split()))

n = ini()
a = inl()
x = sum(a) // n
ans = 0
for i in a:
    ans += (i - x) * (i - x)
x = math.ceil(sum(a) / n)
ans2 = 0
for i in a:
    ans2 += (i - x) * (i - x)
print(min(ans, ans2))