open Batteries

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

   - edge case
        P, 住所:
                - 1
                - 100
        住所リスト:
                - 全要素が同じ値

   amount_of_calculation
        upper_limit: 
                O(N)
        actual     : 
                O(N)
*)
(* - function type
        solver: int -> int list -> int
*)
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

   - edge case
        住所:
                - 1
                - 100
        住所リスト:
                - 全要素が同じ値

   - amount_of_calculation 
        upper_limit: 
                O(N^2)
        actual     : 
                O(N^2)
*)
(* - 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"
(*
let rec solver3 n m lst = 
        if m = (n + 1) || n = m 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
                                solver3 n p lst 
                        else
                                solver3 (p + 1) m lst 

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

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