program c
implicit none
integer(8) :: n,i,cut2,b1,c1,b2,c2,d1,e1,d2,e2,tmp1,tmp2,tmp3,tmp4
integer(8),parameter :: ten = 10
integer(8) :: ans = ten ** 15.
integer(8) :: a(200000)=0,s(200000)=0
integer(8) :: low,high,mid
double precision :: target

read(*,*) n
read(*,*) (a(i),i=1,n)

!まず累計和をとる
do i=1,n
	if(i == 1) then
		s(i) = a(i)
	else
		s(i) = s(i-1) + a(i)
	endif
enddo

!部分列Cの末尾のiをcut2とする
do cut2 = 2,n-2

	!部分列Bの末尾を2分探査する
	low = 1
	high = cut2-1
	target = s(cut2)/2.
	
	do while(low <= high)
		mid = (low + high) / 2
		if (s(mid) > target) then
			high = mid - 1
		else
			low = mid + 1
		endif
	enddo
	
	if(s(mid) > target) then
		mid = mid - 1
	endif
	
	b1 = s(max(mid,1))
	c1 = s(cut2)-b1
	b2 = s(min(mid+1,cut2-1))
	c2 = s(cut2)-b2

	!同様に部分列Dの末尾を2分探査する
	!部分列Bの末尾を2分探査する
	low = cut2+1
	high = n-1
	target = (s(n)-s(cut2))/2.
	
	do while(low <= high)
		mid = (low + high) / 2
		if (s(mid)-s(cut2) > target) then
			high = mid - 1
		else
			low = mid + 1
		endif
	enddo
	
	if(s(mid)-s(cut2) > target) then
		mid = mid - 1
	endif
	
	d1 = s(max(mid,cut2+1))-s(cut2)
	e1 = s(n)-s(cut2)-d1
	d2 = s(min(mid+1,n-1))-s(cut2)
	e2 = s(n)-s(cut2)-d2
	tmp1 = max(b1,c1,d1,e1)-min(b1,c1,d1,e1)
	tmp2 = max(b2,c2,d2,e2)-min(b2,c2,d2,e2)
	tmp3 = max(b1,c1,d2,e2)-min(b1,c1,d2,e2)
	tmp4 = max(b2,c2,d1,e1)-min(b2,c2,d1,e1)
	ans = min(ans,tmp1,tmp2,tmp3,tmp4)
	
enddo

	write(*,*) ans

end program