fun main (args: Array<String>) {
  readLine()!!
  val a = readLine()!!.split(' ').map(String::toInt)
  val min = a.min()!!
  val max = a.max()!!
  val costs = mutableListOf<Int>()
  for (i in min..max) {
    costs.add(a.sumBy { Math.pow((it - i).toDouble(), 2.0).toInt() })
  }
  println(costs.min())
}