#import <stdio.h>
#import <limits.h>
#define MIN(a,b) ((a)<(b)?(a):(b))
#define MAX(a,b) ((a)>(b)?(a):(b))
typedef long long INT;
int main(){
    int n,a;
    scanf("%d",&n);
    int S=0,T=0;
    int min = INT_MAX;
    int max = -INT_MAX;
    for(int i=0;i<n;i++){
        scanf("%d",&a);
        S+=a*a;
        T+=a;
        min=MIN(min,a);
        max=MAX(max,a);
    }
    INT result=LONG_LONG_MAX;
    for(int x=min;x<=max;x++){
        result=MIN(result,S-2*x*T+x*x*n);
    }
    printf("%lld",result);
}
