program main
	use subprogram
	implicit none
    integer N, i, j, s(0:3)
	integer(8) P(1:4), old
	real(8) ave
	integer, allocatable :: A(:)
	read(*, *) N
	allocate(A(1:N))
	read(*, *) A(1:N)
	ave = 0
	do i = 1, N
		ave = ave + A(i)
	end do
	ave = 0.25d0 * ave
	s(0) = 0
	P(1:4) = 0
	do j = 1, 3
		old = 0
		do i = s(j-1)+1, N-4+j
			P(j) =P(j) + A(i)
			s(j) = i
			if(i /= s(j-1)+1) then
				if(abs(P(j) - ave) > abs(old - ave)) then
					P(j) = old
					s(j) = i-1
					exit
				end if
			end if
			old = P(j)
		end do
	end do
	do i = s(3)+1, N
		P(4) = P(4) + A(i)
	end do
	write(*, *) maxval(P) -minval(P)
end program main