Problem:
Score : 600 points

Problem Statement

Snuke has an integer sequence A of length N.

He will make three cuts in A and divide it into four (non-empty) contiguous subsequences B, C, D and E.
The positions of the cuts can be freely chosen.

Let P,Q,R,S be the sums of the elements in B,C,D,E, respectively.
Snuke is happier when the absolute difference of the maximum and the minimum among P,Q,R,S is smaller.
Find the minimum possible absolute difference of the maximum and the minimum among P,Q,R,S.

Constraints

4 \leq N \leq 2 \times 10^5

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Find the minimum possible absolute difference of the maximum and the minimum among P,Q,R,S.

Sample Input 1

5
3 2 4 1 2

Sample Output 1

2

If we divide A as B,C,D,E=(3),(2),(4),(1,2), then P=3,Q=2,R=4,S=1+2=3.
Here, the maximum and the minimum among P,Q,R,S are 4 and 2, with the absolute difference of 2.
We cannot make the absolute difference of the maximum and the minimum less than 2, so the answer is 2.

Sample Input 2

10
10 71 84 33 6 47 23 25 52 64

Sample Output 2

36

Sample Input 3

7
1 2 3 1000000000 4 5 6

Sample Output 3

999999994

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

(*
 * forall l r (p : forall n, l < n <= r -> bool),
 *   ( exists m,
 *     l < m <= r /\ forall n H, p n H = true <-> m <= n ) ->
 *   { m |
 *     l < m <= r /\ forall n H, p n H = true <-> m <= n }
 *)
let rec lower_bound l r p =
  if r <= 1 + l
  then r
  else let m = (l + r) / 2 in
       if p m
       then lower_bound l m p
       else lower_bound m r p

(*
 * forall l r (p : forall n, l <= n < r -> bool),
 *   ( exists m,
 *     l <= m < r /\ forall n H, p n H = true <-> n <= m ) ->
 *   { m |
 *     l <= m < r /\ forall n H, p n H = true <-> n <= m }
 *)
let rec upper_bound l r p =
  if r <= 1 + l
  then l
  else let m = (l + r) / 2 in
       if p m 
       then upper_bound m r p
       else upper_bound l m p

let () = Scanf.scanf "%d\n" @@ fun n ->
  let as_ = Array.init n @@ fun _ ->
    Scanf.scanf "%d " @@ fun a -> a in
  let sum_as = Array.make (n + 1) 0 in
  (* sum_as.(i): 添字の範囲[0; i)におけるas_の要素の総和 *)
  (* 添字の範囲[i; j)の総和が欲しければ sum_as.(j) - sum_as.(i) で手に入る *)
  for i = 0 to n - 1 do
    sum_as.(i + 1) <- as_.(i) + sum_as.(i)
  done;
  begin
    List.concat @@ List.map (fun b1 ->
      List.concat @@ List.map (fun b2 ->
        (* 真ん中の切れ目だけ与えてやる *)
        Array.to_list @@ Array.init (n - 3) (fun j ->
          let j = j + 2 in
          (* B は[0; i)，C は[i; j)，D は[j; k)，E は[k; n) *)
          (* PとQどちらを大きくするかをb1に委ね，にぶたんする *)
          let i =
            if b1 then
              (* P <= Q となる最大の添字 *)
              upper_bound 1 j @@ fun i ->
                sum_as.(i) <= sum_as.(j) - sum_as.(i)
            else 
              (* P >= Q となる最小の添字 *)
              lower_bound 0 (j - 1) @@ fun i ->
                sum_as.(i) >= sum_as.(j) - sum_as.(i) in
          (* RとSどちらを大きくするかをb2に委ね，にぶたんする *)
          let k =
            if b2 then
              (* R <= S となる最大の添字 *)
              upper_bound (j + 1) n @@ fun k ->
                sum_as.(k) - sum_as.(j) <= sum_as.(n) - sum_as.(k)
            else 
              (* R >= S となる最小の添字 *)
              lower_bound j (n - 1) @@ fun k ->
                sum_as.(k) - sum_as.(j) >= sum_as.(n) - sum_as.(k) in
          let pqrs = [ sum_as.(i); sum_as.(j) - sum_as.(i); sum_as.(k) - sum_as.(j); sum_as.(n) - sum_as.(k) ] in
          List.fold_left max min_int pqrs - List.fold_left min max_int pqrs)) [ true; false ]) [ true; false ]
  end
  |> List.fold_left min max_int
  |> Printf.printf "%d\n"