import io.{StdIn => in}

object Main extends App {
  val n = in.readInt
  val a = in.readLine.split(" ").map(_.toInt)
  val avel = a.sum / n
  val aveh = avel + 1
  val resa = a.map(i => math.pow((i - avel),2).toInt).sum
  val resb = a.map(i => math.pow((i - aveh),2).toInt).sum
  val ans = math.min(resa, resb)
  println(ans)
}
