program equal_cut
  implicit none
  integer :: n, i, j, k, l
  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
  t = 9000000000000000000_8
  i = 1
  do l = 3, n-1
    t = abs(a(2)+a(n)-2*a(l))
    if (m.ge.t) then
      m = t
      k = l
    end if
  end do
  m = 9000000000000000000_8
  t = 9000000000000000000_8
  do j = 2, n-2
    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
    if (abs(a(j+1)+a(0)-2*a(i+1)).le.abs(a(j+1)+a(0)-2*a(i))) i = i+1
    if (abs(a(j+1)+a(n)-2*a(k+1)).le.abs(a(j+1)+a(n)-2*a(k))) k = k+1
  end do
  write(*,'(i0)') m
  stop
end program equal_cut