open Batteries
(* purpose
       集会場所の座標PとN人の人が住んでいる座標リストから距離の2乗の総和を求める 
*)
(* function type
        solver : int -> int list -> int
*)
let rec solver p lst =
        match lst with
                  [] -> 0
                | first :: rest -> 
                        ((first - p) * (first - p)) + (solver p rest)

(*
        
let test1 = (
        solver 0 []
        =
        0
)
let test2 = (
        solver 1 [1; 2]
        =
        1
)
let test3 = (
        solver 2 [1; 4]
        =
        5
)
*)

(* purpose
 * 座標域と住所リストからPまでの距離の総和の最小値を求めるO(n^2)
*)
(* function type
        name : int list -> int list -> int(ac) -> int
*)
let rec solver3 clst mlst min =
        match clst with
                  [] -> min
                | first :: rest -> 
                        let sum = solver first mlst in
                                if sum < min then
                                        solver3 rest mlst sum
                                else
                                        solver3 rest mlst min

(*
        
let test1 = (
        solver3 [1] [1] max_int
        =
        0
)
let test2 = (
        solver3 [1; 2] [1; 2] max_int
        =
        1
)
let test3 = (
        solver3 [1; 2; 3; 4] [1; 4] max_int
        =
        5
)
let test4 = (
        solver3 (List.range 2 `To 56) [14; 14; 2; 13; 56; 2; 37] max_int
        =
        2354
)
*)
(* purpose
 * 距離の総和が最小になるPがnからmまでのどこかにある時の距離の総和の最小値を求める
*)
(* function type
        solver2 : int -> int -> int list -> int(ac) -> int
*)
let rec solver2 n m lst sum = 
                if m = (n + 1) then
                        solver n lst
                else
                        let p = (n + m) / 2 in
                        let lp = (n + p) / 2 in
                        let rp = (p + 1 + m) / 2 in
                        let lsum = solver lp lst in
                        let rsum = solver rp lst in
                                if lsum < rsum then
                                        solver2 n p lst lsum
                                else
                                        solver2 (p + 1) m lst rsum


(*
        
let test1 = (
        solver2 1 2 [] max_int
        =
        0
)
let test2 = (
        solver2 1 2 [1; 2] max_int
        =
        1
)
let test3 = (
        solver2 1 4 [1; 4] max_int
        =
        5
)
let test4 = (
        solver2 2 56 [14; 14; 2; 13; 56; 2; 37] max_int
        =
        2354
)
*)

let lst = Scanf.scanf "%d\n" (
        fun n -> 
                Str.split (Str.regexp " ") (read_line ()) 
                |> List.map int_of_string
)

let () = 
        let minlst = List.min lst in
        let maxlst = List.max lst in
                solver2 minlst maxlst lst max_int
                |> Printf.printf "%d\n"

