import math
n = input()
arr = list(map(int, input().split()))
a = sum(arr) // len(arr)
b = a + 1
'''
arr.sort()
if len(arr) % 2 == 0:
    mid = len(arr) // 2
    avg = (arr[mid] + arr[mid-1]) // 2
else:
    mid = len(arr) // 2
    avg = arr[mid]

print(avg)
'''
ans = 0
ans2 = 0
for p in arr:
    ans += (a - p)**2
    ans2  += (b - p)**2
print(min(ans, ans2)) import math math n = input() n input() input arr = list(map(int, input().split())) arr list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split a = sum(arr) // len(arr) a sum(arr) // len(arr) sum(arr) sum arr len(arr) len arr b = a + 1 b a + 1 a 1 '''
arr.sort()
if len(arr) % 2 == 0:
    mid = len(arr) // 2
    avg = (arr[mid] + arr[mid-1]) // 2
else:
    mid = len(arr) // 2
    avg = arr[mid]

print(avg)
''' ans = 0 ans 0 ans2 = 0 ans2 0 for p in arr:
    ans += (a - p)**2
    ans2  += (b - p)**2 p arr ans += (a - p)**2 ans (a - p)**2 a - p a p 2 ans2  += (b - p)**2 ans2 (b - p)**2 b - p b p 2 print(min(ans, ans2)) print min(ans, ans2) min ans ans2 n = input() input() n ans2 = 0 0 ans2 arr = list(map(int, input().split())) list(map(int, input().split())) arr b = a + 1 a + 1 b ans2  += (b - p)**2 (b - p)**2 ans2 ans += (a - p)**2 (a - p)**2 ans a = sum(arr) // len(arr) sum(arr) // len(arr) a ans = 0 0 ans
import math
n = input()
arr = list(map(int, input().split()))
a = sum(arr) // len(arr)
b = a + 1
'''
arr.sort()
if len(arr) % 2 == 0:
    mid = len(arr) // 2
    avg = (arr[mid] + arr[mid-1]) // 2
else:
    mid = len(arr) // 2
    avg = arr[mid]

print(avg)
'''
ans = 0
ans2 = 0
for p in arr:
    ans += (a - p)**2
    ans2  += (b - p)**2
print(min(ans, ans2))
