program equal_cut
  implicit none
  integer :: n, i, j, k
  integer(8) :: a(0:200000), m, t
  a = 0_8
  read(*,*) n
  read(*,*) a(1:n)
  do i = 1, n
    a(i) = a(i) + a(i-1)
  end do
  m = 9000000000000000000_8
  do j = 2, n-2
    do i = 1, j-1
      do k = j+1, n-1
        t = max(a(i)-a(0),a(j)-a(i),a(k)-a(j),a(n)-a(k)) &
        & - min(a(i)-a(0),a(j)-a(i),a(k)-a(j),a(n)-a(k))
        if (m.gt.t) m = t
      end do
    end do
  end do
  write(*,'(i0)') m
  stop
end program equal_cut