import java.util.*

val sc = Scanner(System.`in`)
val N = sc.nextInt()

fun main(args: Array<String>) {
    val a = Array(N) { sc.nextInt() }
    var ans = Int.MAX_VALUE
    for (k in a.min()!!..a.max()!!) {
        var cnt = 0
        for (ai in a) {
            cnt += pow((ai - k), 2)
        }
        ans = Math.min(ans, cnt)
    }
    println(ans)
}

fun pow(a: Int, b: Int): Int {
    var result = 1
    for (i in 0 until b) {
        result *= a
    }
    return result
}
