import std.functional,
       std.algorithm,
       std.bigint,
       std.string,
       std.traits,
       std.array,
       std.range,
       std.stdio,
       std.conv,
       std.math;

void main() {
  int N = readln.chomp.to!int;
  int[] a = readln.chomp.split.to!(int[]);
  int t = round((a.sum / a.length.to!double)).to!int;
  int ans;

  foreach (e; a) {
    ans += (e - t)^^2;
  }

  writeln(ans);
}