import java.util.Scanner;
public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int [] a = new int [n];
    int ansMin=0;
    int ansMax = 0;
    int sum = 0;
    for(int i = 0; i < n; i++){
      a[i] = sc.nextInt();
      sum += a[i];
    }
    int num = sum / n ;
    for(int i = 0; i < n; i++){
      ansMin += (num-a[i]) * (num-a[i]);
    }
    for(int i = 0; i < n; i++){
      ansMax += (num+1-a[i]) * (num+1-a[i]);
    }
    if(ansMin < ansMax) System.out.println(ansMin);
    else System.out.println(ansMax);
  }
} import java.util.Scanner; Scanner java.util public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int [] a = new int [n];
    int ansMin=0;
    int ansMax = 0;
    int sum = 0;
    for(int i = 0; i < n; i++){
      a[i] = sc.nextInt();
      sum += a[i];
    }
    int num = sum / n ;
    for(int i = 0; i < n; i++){
      ansMin += (num-a[i]) * (num-a[i]);
    }
    for(int i = 0; i < n; i++){
      ansMax += (num+1-a[i]) * (num+1-a[i]);
    }
    if(ansMin < ansMax) System.out.println(ansMin);
    else System.out.println(ansMax);
  }
} Main public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int [] a = new int [n];
    int ansMin=0;
    int ansMax = 0;
    int sum = 0;
    for(int i = 0; i < n; i++){
      a[i] = sc.nextInt();
      sum += a[i];
    }
    int num = sum / n ;
    for(int i = 0; i < n; i++){
      ansMin += (num-a[i]) * (num-a[i]);
    }
    for(int i = 0; i < n; i++){
      ansMax += (num+1-a[i]) * (num+1-a[i]);
    }
    if(ansMin < ansMax) System.out.println(ansMin);
    else System.out.println(ansMax);
  } main {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int [] a = new int [n];
    int ansMin=0;
    int ansMax = 0;
    int sum = 0;
    for(int i = 0; i < n; i++){
      a[i] = sc.nextInt();
      sum += a[i];
    }
    int num = sum / n ;
    for(int i = 0; i < n; i++){
      ansMin += (num-a[i]) * (num-a[i]);
    }
    for(int i = 0; i < n; i++){
      ansMax += (num+1-a[i]) * (num+1-a[i]);
    }
    if(ansMin < ansMax) System.out.println(ansMin);
    else System.out.println(ansMax);
  } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int n = sc.nextInt(); n sc.nextInt() sc.nextInt sc int [] a = new int [n]; a new int [n] n int ansMin=0; ansMin 0 int ansMax = 0; ansMax 0 int sum = 0; sum 0 for(int i = 0; i < n; i++){
      a[i] = sc.nextInt();
      sum += a[i];
    } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
      a[i] = sc.nextInt();
      sum += a[i];
    } {
      a[i] = sc.nextInt();
      sum += a[i];
    } a[i] = sc.nextInt() a[i] a i sc.nextInt() sc.nextInt sc sum += a[i] sum a[i] a i int num = sum / n ; num sum / n sum n for(int i = 0; i < n; i++){
      ansMin += (num-a[i]) * (num-a[i]);
    } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
      ansMin += (num-a[i]) * (num-a[i]);
    } {
      ansMin += (num-a[i]) * (num-a[i]);
    } ansMin += (num-a[i]) * (num-a[i]) ansMin (num-a[i]) * (num-a[i]) (num-a[i]) num a[i] a i (num-a[i]) num a[i] a i for(int i = 0; i < n; i++){
      ansMax += (num+1-a[i]) * (num+1-a[i]);
    } int i = 0; int i = 0; i 0 i < n i n i++ i++ i {
      ansMax += (num+1-a[i]) * (num+1-a[i]);
    } {
      ansMax += (num+1-a[i]) * (num+1-a[i]);
    } ansMax += (num+1-a[i]) * (num+1-a[i]) ansMax (num+1-a[i]) * (num+1-a[i]) (num+1-a[i]) num+1 num 1 a[i] a i (num+1-a[i]) num+1 num 1 a[i] a i if(ansMin < ansMax) System.out.println(ansMin);
    else System.out.println(ansMax); ansMin < ansMax ansMin ansMax System.out.println(ansMin) System.out.println System.out System System.out ansMin System.out.println(ansMax) System.out.println System.out System System.out ansMax String[] args args public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int [] a = new int [n];
    int ansMin=0;
    int ansMax = 0;
    int sum = 0;
    for(int i = 0; i < n; i++){
      a[i] = sc.nextInt();
      sum += a[i];
    }
    int num = sum / n ;
    for(int i = 0; i < n; i++){
      ansMin += (num-a[i]) * (num-a[i]);
    }
    for(int i = 0; i < n; i++){
      ansMax += (num+1-a[i]) * (num+1-a[i]);
    }
    if(ansMin < ansMax) System.out.println(ansMin);
    else System.out.println(ansMax);
  }
} public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int [] a = new int [n];
    int ansMin=0;
    int ansMax = 0;
    int sum = 0;
    for(int i = 0; i < n; i++){
      a[i] = sc.nextInt();
      sum += a[i];
    }
    int num = sum / n ;
    for(int i = 0; i < n; i++){
      ansMin += (num-a[i]) * (num-a[i]);
    }
    for(int i = 0; i < n; i++){
      ansMax += (num+1-a[i]) * (num+1-a[i]);
    }
    if(ansMin < ansMax) System.out.println(ansMin);
    else System.out.println(ansMax);
  }
} Main
import java.util.Scanner;
public class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int [] a = new int [n];
    int ansMin=0;
    int ansMax = 0;
    int sum = 0;
    for(int i = 0; i < n; i++){
      a[i] = sc.nextInt();
      sum += a[i];
    }
    int num = sum / n ;
    for(int i = 0; i < n; i++){
      ansMin += (num-a[i]) * (num-a[i]);
    }
    for(int i = 0; i < n; i++){
      ansMax += (num+1-a[i]) * (num+1-a[i]);
    }
    if(ansMin < ansMax) System.out.println(ansMin);
    else System.out.println(ansMax);
  }
}
