Problem:
Score : 300 points

Problem Statement

There are N people living on a number line.

The i-th person lives at coordinate X_i.

You are going to hold a meeting that all N people have to attend.

The meeting can be held at any integer coordinate. If you choose to hold the meeting at coordinate P, the i-th person will spend (X_i - P)^2 points of stamina to attend the meeting.

Find the minimum total points of stamina the N people have to spend.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq X_i \leq 100

Input

Input is given from Standard Input in the following format:

N
X_1 X_2 ... X_N

Output

Print the minimum total stamina the N people have to spend.

Sample Input 1

2
1 4

Sample Output 1

5

Assume the meeting is held at coordinate 2. In this case, the first person will spend (1 - 2)^2 points of stamina, and the second person will spend (4 - 2)^2 = 4 points of stamina, for a total of 5 points of stamina. This is the minimum total stamina that the 2 people have to spend.

Note that you can hold the meeting only at an integer coordinate.

Sample Input 2

7
14 14 2 13 56 2 37

Sample Output 2

2354

Solution in OCaml (4.02.3) (judge verdict: Accepted)

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"
*)