import java.util.*

fun main(args: Array<String>) {
  val sc = Scanner(System.`in`)
  val n = sc.nextInt()
  val a = (1..n).map { sc.nextInt() }

  val min = a.min()!!
  val max = a.max()!!

  val list = mutableListOf<Long>()
  for (i in min..max) {
    var sum = 0L
    for (ai in a) {
      val tmp = (i - ai) * (i - ai)
      sum += tmp.toLong()
    }
    list.add(sum)
  }

  println(list.min())

}
