program main
	implicit none
    integer N, i, j, s(0:3), sb(0:3)
	integer(8) P(1:4), old, Pb(1:4), oldb, Sa(1:4), aa
	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
	
	
	sb(0) = N+1
	Pb(1:4) = 0
	do j = 1, 3
		oldb = 0
		do i = sb(j-1)-1, 5 - j, -1
			Pb(j) =Pb(j) + A(i)
			sb(j) = i
			if(i /= sb(j-1)-1) then
				if(abs(Pb(j) - ave) > abs(oldb - ave)) then
					Pb(j) = oldb
					sb(j) = i+1
					exit
				end if
			end if
			oldb = Pb(j)
		end do
	end do
	do i = sb(3)-1, 1, -1
		Pb(4) = Pb(4) + A(i)
	end do
	
	
	do i = 1, 4
		do j = 1, N
			Sa(i) = Sa(i) + A(j)
			if(Sa(i) <= i * ave) then
				exit
			end if
		end do
	end do
	aa = max(Sa(4)-Sa(3), Sa(3)-Sa(2),Sa(2)-Sa(1),Sa(1))- min(Sa(4)-Sa(3), Sa(3)-Sa(2),Sa(2)-Sa(1),Sa(1))

	write(*, *) min(maxval(P) -minval(P),maxval(Pb) -minval(Pb), aa)
end program main