fun main(args: Array<String>) {
    val N = readLine()!!.toInt()
    val arr = readLine()!!.split(" ").map { it.toInt() }
    val sum = arr.sum()
    val cost = arrayOf(sum / arr.size, sum / arr.size + 1)
            .map {
                arr.map { x -> val d = x - it; d * d }.sum()
            }
            .min()
    println(cost)
}