program main
  implicit none
  integer(8) :: n, i, res, four(4)
  integer(8), allocatable :: a(:)
  read *, n
  allocate (a(0:n))
  a(0) = 0
  read *, a(1:n)
  do i = 1, n
    a(i) = a(i) + a(i - 1)
  end do

  res = 10 ** 18
  do i = 2, n - 2  ! iの後が中央切断
    four(1:2) = optimal_cut(a(1:i), i)
    four(3:4) = optimal_cut(a(i + 1:n) - a(i), n - i)
    res = min(res, maxval(four) - minval(four))
  end do
  print "(i0)", res
  contains

  function optimal_cut(acc, n)
    integer(8) :: optimal_cut(2)
    integer(8), intent(in) :: n, acc(n)
    integer(8) :: ng, ok, md
    ! acc(n)の半分以下の要素のうち最も大きいものを求める
    ng = n
    ok = 1
    do
      md = ok + (ng - ok) / 2
      if (acc(md) * 2 <= acc(n)) then
        ok = md
      else
        ng = md
      end if
      if (ng - ok <= 1) exit
    end do
    if (abs(acc(n) - 2 * acc(ok)) > abs(acc(n) - 2 * acc(ok + 1))) ok = ok + 1
    optimal_cut(1) = acc(ok)
    optimal_cut(2) = acc(n) - acc(ok)
  end function optimal_cut
end program main
