output_description: Print the minimum total cost to achieve Evi's objective.
  
  * * *
input_description: The input is given from Standard Input in the following format:
  
      
      
      N
      a_1 a_2 ... a_N
attempt: #
  # Written by NoKnowledgeGG @YlePhan
  # ('ω')
  #
  # import math
  # mod = 10**9+7
  # import itertools
  # import fractions
  # import numpy as np
  # mod = 10**4 + 7
  def kiri(n, m):
      r_ = n / m
      if (r_ - (n // m)) > 0:
          return (n // m) + 1
      else:
          return n // m
  
  
  """ n! mod m 階乗
  mod = 1e9 + 7
  N = 10000000
  fac = [0] * N
  def ini():
    fac[0] = 1 % mod
    for i in range(1,N):
      fac[i] = fac[i-1] * i % mod"""
  
  """mod = 1e9+7
  N = 10000000
  pw = [0] * N
  def ini(c):
    pw[0] = 1 % mod
    for i in range(1,N):
      pw[i] = pw[i-1] * c % mod"""
  
  """
  def YEILD():
    yield 'one'
    yield 'two'
    yield 'three'
  generator = YEILD()
  print(next(generator))
  print(next(generator))
  print(next(generator))
  """
  """def gcd_(a,b):
    if b == 0:#結局はc,0の最大公約数はcなのに
      return a
    return gcd_(a,a % b) # a = p * b + q"""
  """def extgcd(a,b,x,y):
    d = a
    if b!=0:
      d = extgcd(b,a%b,y,x)
      y -= (a//b) * x
      print(x,y)
    else:
      x = 1
      y = 0
    return d"""
  
  
  def readInts():
      return list(map(int, input().split()))
  
  
  def main():
      n = int(input())
      A = readInts()
  
      SPLIT = kiri(sum(A), n)
  
      ANS = 0
      # print(SPLIT)
      for i in range(n):
          ANS += abs(SPLIT - A[i]) ** 2
      print(ANS)
  
  
  if __name__ == "__main__":
      main()
  
problem_description: 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.
samples: [{"input": "2\n    4 8", "output": "8\n    \n\nTransforming the both into 6s will cost (4-6)^2+(8-6)^2=8 dollars, which is\nthe minimum.\n\n* * *"}, {"input": "3\n    1 1 3", "output": "3\n    \n\nTransforming the all into 2s will cost (1-2)^2+(1-2)^2+(3-2)^2=3 dollars. Note\nthat Evi has to pay (1-2)^2 dollar separately for transforming each of the two\n1s.\n\n* * *"}, {"input": "3\n    4 2 5", "output": "5\n    \n\nLeaving the 4 as it is and transforming the 2 and the 5 into 4s will achieve\nthe total cost of (2-4)^2+(5-4)^2=5 dollars, which is the minimum.\n\n* * *"}, {"input": "4\n    -100 -100 -100 -100", "output": "0\n    \n\nWithout transforming anything, Evi's objective is already achieved. Thus, the\nnecessary cost is 0."}]