#include <iostream>
#include <math.h>

using namespace std;

int main()
{
  int n;
  cin >> n;
  double a[n];
  double sum = 0, ave;

  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  ave = round(sum / n);

  int cost = 0;

  for (int i = 0; i < n; i++) {
    cost += (a[i] - ave) * (a[i] - ave);
  }

  cout << cost << endl;
}
