open Batteries

(* purpose
        ある地点の住所Pと全住人の住所リストから全住人の住所からpにかけての距離の総和を求める
   - detail of data
        P:
                detail: 全住人からの距離が最小と成る点
                condition: 1 <= P <= 100
        N, M:
                detail: Pが存在する範囲の始点と終点
                condition: 1 <= N <= 100
                           1 <= M <= 100
                           N <= M

   amount_of_calculation
        upper_limit: O(N)
        actual     : O(N)
*)
(* - function type
        solver: int -> int list -> int

   - I/O example
        input    -> output
        1 [1; 2] -> 1
        1 [1]    -> 0
        1 []     -> 0
*)
let rec solver p lst =
        match lst with
                  [] -> 0
                | first :: rest -> 
                        (Int.pow (first - p) 2) + (solver p rest)

(* - purpose
       任意の住所のリストと全住人の住所のリストから住人の住所からリストに含まれる任意の住所までの距離の全住人についての総和の最小値を求める

   - detail of data
        住所:
                detail: 全住人からの距離が最小と成る点
                condition: 1 <= 住所 <= 100

   - amount_of_calculation 
        upper_limit: O(N^2)
        actual     : O(N^2)
*)
(* - purpose
        detail

   - detail of data
        data1:
                detail: detail
                condition: cond

   - amount of calculation
        upper_limit: O(order)
        actual     : O(order)
*)
(* - function type
        solver2: int list -> int list -> int(acm) -> int

   - accumulator(option)
   	minsum: (調べ終わった総和のうち最小の値を格納) 
*)
let rec solver2 rlst lst minsum = 
        match rlst with
                  [] -> minsum
                | first :: rest -> 
                        let sum = solver first lst in (* O(N) *)
                                if sum < minsum then
                                        solver2 rest lst sum
                                else
                                        solver2 rest lst minsum

let test1 = (
        solver2 [] [] max_int
        =
        max_int
)
let test2 = (
        solver2 [1] [1] max_int
        =
        0
)
let test3 = (
        solver2 [1; 2] [1; 2] max_int
        =
        1
        ||
        solver2 [1; 2] [1; 2] max_int
        =
        2
)

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


let () =
        let maxval = List.max lst in
        let minval = List.min lst in
                solver2 (List.range minval `To maxval) lst max_int
                |> Printf.printf "%d\n"