import std.algorithm;
import std.array;
import std.conv;
import std.math;
import std.range;
import std.stdio;
import std.string;

void main() {
  int n;
  readf (" %d", &n);
  auto a = new int[n];
  int s;
  long res = long.max;
  int best = -1;
  long check (int p) {
    long r;
    foreach (i; a) {
      r += (i - p) * (i - p);
    }
    if (res > r) {
      res = r;
      best = p;
    }
    debug stderr.writefln ("check(%d) = %d", p, r);
    return r;
  }
  int u = int.max;
  int v = int.min;
  foreach (t; 0 .. n) {
    readf (" %d", &a[t]);
    s += a[t];
    u = min (u, a[t]);
    v = max (v, a[t]);
  }
  s /= n;
  foreach (step; u .. v+1) {
    check (step);
  }
  writeln (res);
}

