#include <bits/stdc++.h>

#define mp make_pair
#define pb push_back
#define sz(A) (int)(A.size())

namespace wdnmd {
	inline int read() {
		int x = 0, f = 1; char c = getchar();
		while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); }
		while (c >= '0' && c <= '9') { x = (x << 1) + (x << 3) + (c ^ 48); c = getchar(); }
		return f * x;
	}
	inline void read(int &x) { x = read();}
	inline void read(int &x, int &y) { x = read(), y = read(); }
	inline void read(int &x, int &y, int &z) { x = read(), y = read(), z = read(); }
	inline void read(int &x, int &y, int &z, int &w) { x = read(), y = read(), z = read(), w = read(); }
	
	inline long long readl() {
		long long x = 0, f = 1; char c = getchar();
		while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); }
		while (c >= '0' && c <= '9') { x = (x << 1) + (x << 3) + (c ^ 48); c = getchar(); }
		return f * x;
	}
	inline void read(long long &x) { x = readl();}
	inline void read(long long &x, long long &y) { x = readl(), y = readl(); }
	inline void read(long long &x, long long &y, long long &z) { x = readl(), y = readl(), z = readl(); }
	inline void read(long long &x, long long &y, long long &z, long long &w) { x = readl(), y = readl(), z = readl(), w = readl(); }
	
	inline void writeln(int x) { printf("%d\n", x); }
	inline void writeln(int x, int y) { printf("%d %d\n", x, y); }
	inline void writeln(int x, int y, int z) { printf("%d %d %d\n", x, y, z); }
	inline void writeln(int x, int y, int z, int w) { printf("%d %d %d %d\n", x, y, z, w); }
	inline void writeln(long long x) { printf("%lld\n", x); }
	inline void writeln(long long x, long long y) { printf("%lld %lld\n", x, y); }
	inline void writeln(long long x, long long y, long long z) { printf("%lld %lld %lld\n", x, y, z); }
	inline void writeln(long long x, long long y, long long z, long long w) { printf("%lld %lld %lld %lld\n", x, y, z, w); }
	inline void write(int x) { printf("%d", x); }
	inline void write(int x, int y) { printf("%d %d", x, y); }
	inline void write(int x, int y, int z) { printf("%d %d %d", x, y, z); }
	inline void write(int x, int y, int z, int w) { printf("%d %d %d %d", x, y, z, w); }
	inline void write(long long x) { printf("%lld", x); }
	inline void write(long long x, long long y) { printf("%lld %lld\n", x, y); }
	inline void write(long long x, long long y, long long z) { printf("%lld %lld %lld\n", x, y, z); }
	inline void write(long long x, long long y, long long z, long long w) { printf("%lld %lld %lld %lld\n", x, y, z, w); }
	inline void write_(int x) { printf("%d ", x); }
	inline void write_(long long x) { printf("%lld ", x); }
	
	int gcd(int a, int b) { return b ? gcd(b, a & b) : a; }
	long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
	
	inline int lowbit(int x) { return x & (-x); }
	inline long long lowbit(long long x) { return x & (-x); }
	
	inline long long qmi(int a, int k, int p) {
		long long res = 1 % p, base = a;
		while (k) {
			if (k & 1) res = res * base % p;
			base = base * base % p;
			k >>= 1;
		}
		return res;
	}
	inline long long qmi(int a, int k) {
		long long res = 1, base = a;
		while (k) {
			if (k & 1) res = res * base;
			base = base * base;
			k >>= 1;
		}
		return res;
	}
	
	template <typename T> void chmin(T &x, const T &y) { if (x > y) x = y; }
	template <typename T> void chmin(T &x, const T &y, const T &z) { if (x > y) x = y; if (x > z) x = z; }
	template <typename T> void chmin(T &x, const T &y, const T &z, const T &w) { if (x > y) x = y; if (x > z) x = z; if (x > w) x = w; }
	template <typename T> void chmax(T &x, const T &y) { if (x < y) x = y; }
	template <typename T> void chmax(T &x, const T &y, const T &z) { if (x < y) x = y; if (x < z) x = z; }
	template <typename T> void chmax(T &x, const T &y, const T &z, const T &w) { if (x < y) x = y; if (x < z) x = z; if (x < w) x = w; }
}
using namespace wdnmd;
using namespace std;

typedef pair<int, int> PII;
typedef vector<int> VI;
typedef long long LL;
typedef unsigned long long ULL;

//----------------------------------------------------------------------------------------------------------------------------------------//

const int N = 750000, M = 1000010, INF = 0x3f3f3f3f, MOD = 1000;

int a[N];

int main() {
	//freopen("in.txt", "r", stdin);
	int n = read();
	int s1 = 0, s2 = 0;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		s1 += a[i];
		s2 += a[i] * a[i];
	}
	int x = s1 / n;
	cout << min(n * x * x - 2 * s1 * x + s2, n * (x+1) * (x+1) - 2 * s1 * (x+1) + s2) << endl;	
	return 0; 
} 

Measured memory_bytes: 256