import java.util.Scanner;

fun main (args : Array<String>) {
  val input = Scanner(System.`in`)
  
  while (input.hasNext()) {
    val N = input.nextInt()
    val a = IntArray(N)
    var sum = 0
    
    for (i in a.indices) {
      a[i] = input.nextInt()
      sum += a[i]
    }
    
    val avg = a.average()
    val avgCeil = Math.ceil(avg).toInt()
    val avgFloor = Math.floor(avg).toInt()
    var totalCostCeil = 0
    var totalCostFloor = 0
    
    for (i in a.indices) {
      var dist = avgCeil - a[i]
      totalCostCeil += dist * dist
      
      dist = avgFloor - a[i]
      totalCostFloor += dist * dist
    }
    
    println(Math.min(totalCostCeil, totalCostFloor))
  }
}