import java.math.*

fun main(args: Array<String>) {
    val N = readInputLine().toInt()
    
    val aList = readInputLine().split(" ").map { it.toInt() }.toIntArray()
    
    val (ceil, floor) = aList.sum().let {
        Pair(it / N + if (it % N == 0) 0 else 1, it / N)
    }

    println(Math.min(
        aList.sumBy { (it - ceil) * (it - ceil) },
        aList.sumBy { (it - floor) * (it - floor) }
    ))
}

fun readInputLine(): String {
    return readLine()!!
}
