#include <algorithm>
#include <iterator>
#include <iostream>
#include <sstream>
#include <bitset>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <list>
#include <map>
#include <set>
#include <time.h>
#include <limits>
#include <math.h>
#include <climits>
#include <numeric>
#include <utility>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <functional>
#include<unordered_map>
//#include<bits/stdc++.h>
//#include <ext/pb_ds/detail/standard_policies.hpp>
 
const double pi=acos(-1.0);
//memset ( a , 0 , n * sizeof(ll) ) ;
using namespace std;
//using namespace __gnu_pbds; 
#define   endl    '\n'
#define   sl(n)     scanf("%lld",&n)
#define   mp      make_pair
#define   pb      push_back
#define   ppb     pop_back
#define   fi      first
#define   se      second
#define   ll      long long
#define   ull     unsigned long long
#define   pii     pair<int, int>
#define   f(i,a,b)  for(ll i = (ll)(a); i < (ll)(b); i++)
#define   rf(i,a,b)   for(ll i = (ll)(a); i > (ll)(b); i--)
#define   ms(a,b)   memset((a),(b),sizeof(a))
#define   vec(g1)   int temp;cin>>temp;g1,push_back(temp);
#define   abs(x)    ((x<0)?(-(x)):(x))
#define   MAX     1000005
#define   inf     LLONG_MAX
#define   MIN     INT_MIN
#define   yeet    return 0;
#define fast_io ios_base::sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0) ;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
// Use cout.flush() for interactive problems.
// Use this for increased stack size: g++ -o a.exe -Wl,--stack=256000000 -O2 source.cpp
inline long long  MAX2(long long  a, long long  b){return (a)>(b)?(a):(b);}
inline long long  MAX3(long long  a, long long  b,long long  c){return (a)>(b)?((a)>(c)?(a):(c)):((b)>(c)?(b):(c));}
inline long long  MIN2(long long  a, long long  b){return (a)<(b)?(a):(b);}
inline long long  MIN3(long long  a, long long b,long long c){return (a)<(b)?((a)<(c)?(a):(c)):((b)<(c)?(b):(c));}
 
//typedef
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef map<int,int> MPII;
typedef set<int> SETI;
typedef multiset<int> MSETI;
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int  uint64;
 
 
int mod = 1e9 +9 ;
int64_t extGcd(int64_t a, int64_t b, int64_t& x, int64_t& y) {if (!a) {x = 0;y = 1;return b;}int64_t x1, y1;int64_t d = extGcd(b % a, a, x1, y1);x = y1 - (b / a) * x1;y = x1;return d;}
const int factRange = 1000000;
int fact[factRange];
inline int addMod(int a, int b, int m = mod) {return ((int64_t)a + b) % m;}
inline int mulMod(int a, int b, int m = mod) {  return ((int64_t)a * b) % m;}
inline int divMod(int a, int b, int m = mod) {int64_t x, y;int64_t g = extGcd(b, m, x, y);if (g != 1) {cerr << "Bad gcd!" << endl;for (;;);}x = (x % m + m) % m;return mulMod(a, x, m);}
inline void precalcFactorials() {fact[0] = 1;for (int i = 1; i < factRange; i++) {fact[i] = mulMod(fact[i-1], i);}}
inline int F(int n) {return (n < 0) ? 0 : fact[n];}
inline int C(int k, int n) {return divMod(F(n), mulMod(F(n-k), F(k)));}
inline ll exp(ll a,ll b){if(a==0)return 0ll;ll r=1LL;while(b>0){if(b&1){r=r*(a%mod);r=(r+mod)%mod;}b/=2;a=(a%mod)*(a%mod);a=(a+mod)%mod;}return (r+mod)%mod;}
ll gcd(ll a,ll b){if(b==0)return a;if(a==0)return b;return gcd(b,a%b);}
inline ll poww(ll a,ll b){ll r=1LL;while(b>0){if(b&1)r=r*a;b/=2;a=a*a;}return (ll)r;}
 uint32 setbits(ll n){uint32 count=0;while (n){n&=(n-1);count++;}return count; } 
//const int N= 10000000;int lp[N+1];vector<int> pr;void seive(){for (int i=2; i<=N; ++i) {if (lp[i] == 0) {lp[i] = i;pr.push_back (i);}for (int j=0; j<(int)pr.size() && pr[j]<=lp[i] && i*pr[j]<=N; ++j) lp[i * pr[j]] = pr[j]; }}
////****************************************************************************************************************************************************************************************************************////
 int main(){ 
 	  int  n;
 	  cin>>n;
 	  int a[n];
 	  ll sum=0;
 	  f(i,0,n){cin>>a[i];sum+=a[i];}
 	   int p= sum/n;
 	   int p2=p+1;
 	   int p3=p-1;
 	   ll ans1=0;
 	   ll ans2=0;
 	   ll ans3=0;
 	   f(i,0,n){
 	   	 ans1+= abs((p-a[i])*(p-a[i]));
 	   	 ans2+=abs((p2-a[i])*(p2-a[i]));
 	   	 ans3+=abs((p3-a[i])*(p3-a[i]));
 	   }
        ans1= MIN3(ans1,ans2,ans3);
 	   cout<<ans1<<endl;
 	return 0;
 }

Measured memory_bytes: 256