

object Main extends App {
  scala.io.StdIn.readLine
  val a = scala.io.StdIn.readLine.split(" ").map(_.toInt)
  val average = a.sum.toDouble / a.length
  def sumSquare: Int ⇒ List[Int] ⇒ Int = to ⇒ {
    def square: Int ⇒ Int = n ⇒ { n * n }
    list ⇒ {
      list.map { n ⇒ square(to - n) }.sum
    }
  }
  def min(a: Int, b: Int) = if (a < b) a else b
  println(min(sumSquare(average.ceil.toInt)(a.toList), sumSquare(average.floor.toInt)(a.toList)))
}