//#include<Furude_Rika.avi>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <cstdlib>
#include <utility>
#include <cmath>
#include <bitset>
#include <sstream>
using namespace std;
#define ll long long
#define ull unsigned long long
#define all(x) (x).begin(), (x).end() 
#define ayacin ios::sync_with_stdio(false);
#define yukari 100000000000000000LL;
#define M_PI12 3.141592653
#define Parsee long double
#define reimu iterator 
#define x first
#define y second
#define mokou __builtin_popcount
#define __sum accumulate
#define rep(i,a,b) for(i=a;i<=b;i++)
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
#define ls rt<<1
#define rs rt<<1|1 
#define eps 1e-10  
#define zero(a) fabs(a)<eps  
using namespace std;
namespace KMP{
    const int MAXN=1e6+5;
    int M,N,NEXT[MAXN];
    char a[MAXN],b[MAXN];
    void get_NEXT(){
        int i=0,j=NEXT[0]=-1;
        while(i<M){
            if(j==-1||a[i]==a[j]){
                i++,j++;
                NEXT[i]=j;
            }
            else j=NEXT[j];
        }
    }
    int kmp(){
        int i=0,j=0;
        int num=0;
        while(i<N){
            if(j==-1||b[i]==a[j]){
                   i++,j++;
            }
            else j=NEXT[j];
            if(j==M){
                num++;
                    j=NEXT[j];
               }
           }
          return num;
    }
};

namespace HASH{
    const int N=2333;
    char s[N];
    int p[N],A[N],n;
    int P=2342652427;
    void init(int n){
        p[0]=1;
        for(int i=1;i<n+5;i++)p[i]=p[i-1]*P;
        for(int i=1;i<=n;i++)A[i]=A[i-1]*P+(s[i]*s[i]);    
    }     
    inline int hh(int x,int y){
        return A[y]-A[x-1]*p[y-x+1];
    }
};

namespace SUFFIX{
    const int maxn=2333;
    int wa[maxn],wb[maxn],wv[maxn],Ws[maxn];  
    int cmp(int *r,int a,int b,int l)  
        {return r[a]==r[b]&&r[a+l]==r[b+l];}  
    void da(const char *r,int *sa,int n,int m){  
        int i,j,p,*x=wa,*y=wb,*t;   
        for(i=0;i<m;i++) Ws[i]=0;   
        for(i=0;i<n;i++) Ws[x[i]=r[i]]++;   
        for(i=1;i<m;i++) Ws[i]+=Ws[i-1];   
        for(i=n-1;i>=0;i--) sa[--Ws[x[i]]]=i;   
            for(j=1,p=1;p<n;j*=2,m=p){   
            for(p=0,i=n-j;i<n;i++) y[p++]=i;   
            for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;   
            for(i=0;i<n;i++) wv[i]=x[y[i]];   
            for(i=0;i<m;i++) Ws[i]=0;   
            for(i=0;i<n;i++) Ws[wv[i]]++;   
            for(i=1;i<m;i++) Ws[i]+=Ws[i-1];   
            for(i=n-1;i>=0;i--) sa[--Ws[wv[i]]]=y[i];   
            for(t=x,x=y,y=t,p=1,x[sa[0]]=0,i=1;i<n;i++)   
            x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;   
        }   
            return;   
    }  
    int sa[maxn],Rank[maxn],height[maxn];  
    //求height数组  
    void calheight(const char *r,int *sa,int n){  
        int i,j,k=0;  
        for(i=1;i<=n;i++) Rank[sa[i]]=i;  
        for(i=0;i<n;height[Rank[i++]]=k)  
        for(k?k--:0,j=sa[Rank[i]-1];r[i+k]==r[j+k];k++);  
        return;  
    }  
    char str[maxn];  
    int slove(int n){  
        int sum=0;  
        for(int i=1;i<=n;i++)  
            sum+=n-sa[i]-height[i];  
            return sum;  
    }  
};
namespace BIT{
    const int MAXN=1e6+6;
    int bit[MAXN];
    int sum(int i){
        int s=0;
        while(i>0){
            s+=bit[i];
            i-=i&-i;
        }
        return s;
    }
    void add(int i,int x){
        while(i<=1000000){
            bit[i]+=x;
            i+=i&-i;
        }
    }
}
namespace SAM{
	const int CHAR = 26;
	const int MAXN = 250010;
	struct SAM_Node{
    	SAM_Node *fa, *next[CHAR];
    	int len;
    	int id, pos;
    	SAM_Node(){}
    	SAM_Node(int _len){
        	fa = 0;
        	len = _len;
        	memset(next,0,sizeof(next));
    	}
	};
	int oooo;
	SAM_Node SAM_node[MAXN*2], *SAM_root, *SAM_last;
	int SAM_size;
	SAM_Node *newSAM_Node(int len){
    	SAM_node[SAM_size] = SAM_Node(len);
    	SAM_node[SAM_size].id = SAM_size;
    	return &SAM_node[SAM_size++];
	}
	SAM_Node *newSAM_Node(SAM_Node *p){
    	SAM_node[SAM_size] = *p;
    	SAM_node[SAM_size].id = SAM_size;
    	return &SAM_node[SAM_size++];
	}
	void SAM_init(){
    	SAM_size = 0;
    	SAM_root = SAM_last = newSAM_Node(0);
    	SAM_node[0].pos = 0;
	}
	void SAM_add(int x,int len){
    	SAM_Node *p = SAM_last, *np = newSAM_Node(p->len + 1);
    	np->pos = len;
    	SAM_last = np;
    	for(; p && !p->next[x];p = p->fa)
        	p->next[x] = np;
    	if(!p){
        	np->fa = SAM_root;
        	return;
    	}
    	SAM_Node *q = p->next[x];
    	if(q->len == p->len + 1){
        	np->fa = q;
        	return;
    	}
    	SAM_Node *nq = newSAM_Node(q);
    	nq->len = p->len + 1;
    	q->fa = nq;
    	np->fa = nq;
    	for(; p && p->next[x] == q; p = p->fa)
        	p->next[x] = nq;
	}
	void SAM_build(char *s){
    	SAM_init();
    	int len = strlen(s);
    	for(int i = 0;i < len;i++)
        SAM_add(s[i] - 'a', i+1);
	}
};
/*
const int MAXN=2e3+5;
struct sss{
    int l,r,i;
}q[MAXN];
bool comp(const sss &a,const sss &b){
    return a.r<b.r;
}
int dp[MAXN][MAXN],a[MAXN][MAXN];
int last[MAXN*MAXN/2],num[MAXN*MAXN/2];
*/
ll a[1000005],m;
ll calc(ll n){
	ll ans=0;
	for(ll i=1;i<=m;i++) ans+=(a[i]-n)*(a[i]-n);
	return ans;
}
int main(){
	ll n,i,j,k;
	while(cin>>m){
		for(i=1;i<=m;i++) cin>>a[i];
		ll ans=999999999999999LL;
		for(i=-1000;i<=1000;i++) ans=min(ans,calc(i));
		cout<<ans<<endl;
	}
}


Measured memory_bytes: 256