  // C - いっしょ / Be Together https://atcoder.jp/contests/abc043/tasks/arc059_a

  import io.Source

  object Main extends App {
    val xs = Source.stdin.getLines()
    val n = xs.next().toInt
    val ns = xs.next().split(' ').map(_.toInt)

    println(solve(ns))
    
    def solve(ns: Seq[Int]): String = {
      // 平均かその前後の値に書き換えるのが一番いいはず
      val nss = ns.sorted
      val min = nss.min
      val max = nss.max
      // 平均で計算
      val a = (min + max) / 2
      var ans = cost(a, nss)
      // 1つ前の値で計算
      val pi = nss.lastIndexWhere(_ < a)
      if (pi >= 0) {
        ans = Math.min(cost(ns(pi), nss), ans)
      }
      // 1つ後の値で計算
      val ni = nss.indexWhere(_ > a)
      if (ni >= 0) {
        ans = Math.min(cost(ns(ni), nss), ans)
      }
      ans.toString
    }

    def cost(n: Int, ns: Seq[Int]): Int = {
      ns.map(m => Math.pow(m - n, 2).toInt).sum
    }
  }
