function Main(s) {
  var a = s[1].split(" ").map(a => parseInt(a));
  var min = Math.min(...a);
  var max = Math.max(...a);
  var ans = 1e9;
  for(var i = min; i <= max; i++){
    var cost = 0;
    for(var j = 0; j < a.length; j++){
      var n = i - a[j];
      cost += n * n;
    }
    if(ans > cost) ans = cost;
    else break;
  }
	console.log(ans);
}

Main(require("fs").readFileSync("/dev/stdin", "utf8").split("\n"));