let rec for_fold a b f v = if a > b then v else for_fold (a+1) b f (f v a)

let () =
  Scanf.scanf "%d" @@ fun n ->
  let a = Array.init n (fun _ -> Scanf.scanf " %d" ((+) 0)) in
  for i = 1 to n-1 do a.(i) <- a.(i) + a.(i-1) done;

  let psum l r =
    if l >= r then 0
    else if l = 0 then a.(r-1) else a.(r-1) - a.(l-1)
  in
  let halve a b =
    let rec loop l r =
      if l >= r then r
      else
        let m = (l+r) / 2 in
        if psum a m >= psum m b then loop l m else loop (l+1) r
    in
    let m = loop a b in
    if abs (psum a m - psum m b) <= abs (psum a (m-1) - psum (m-1) b) then
      psum a m, psum m b
    else
      psum a (m-1), psum (m-1) b
  in

  for_fold 2 (n-3) (fun v i ->
    let a, b = halve 0 i in
    let c, d = halve i n in
    List.(fold_left max a [a;b;c;d] - fold_left min a [a;b;c;d])
    |> min v
  ) a.(n-1) 
  |> Printf.printf "%d\n"
