program main
     integer(8) n,i,j,k
     integer(8) p(4),sumv,msumv
     integer(8),allocatable,dimension(:):: a
     read(*,*)n
     allocate(a(n))
     read(*,*)a
     msumv=200000_8*1000000000_8
     do i=1,n-3
          do j=i+1,n-2
               do k=j+1,n-1
                   p(1)=sum(a(1:i))
                   p(2)=sum(a(i+1:j))
                   p(3)=sum(a(j+1:k))
                   p(4)=sum(a(k+1:n))
                   sumv=abs(minval(p)-maxval(p))
                   if(msumv>sumv)msumv=sumv
               enddo
          enddo
     enddo
     write(*,*)msumv
end program main
