unittest
{
	assert( [ "5", "3 2 4 1 2" ].solve == 2 );
	assert( [ "10", "10 71 84 33 6 47 23 25 52 64" ].solve == 36 );
	assert( [ "7", "1 2 3 1000000000 4 5 6" ].solve == 999999994 );
}



import std.algorithm;
import std.conv;
import std.math;
import std.range;
import std.stdio;

void main()
{
	stdin.byLineCopy.solve.writeln;
}

auto solve( Range )( Range input )
if( isInputRange!Range && is( ElementType!Range == string ) )
{
	auto n = input.front.to!( long );
	input.popFront;
	auto as = input.front.split.to!( long[] );
	
	// 累積和表を作成
	auto ts = new long[ 1 + as.length ];
	foreach( i, a; as )
	{
		ts[ i + 1 ] = ts[ i ] + a;
	}
	
	// pとrの区切りを全探索する
	auto ans = long.max;
	auto bs = [ 0L, 1L, 2L, 3L, ts.length.signed - 1 ];
	for( ; bs[ 2 ] < ts.length.signed - 2; bs[ 2 ]++ )
	{
		if( bs[ 3 ] <= bs[ 2 ] ) bs[ 3 ] = bs[ 2 ] + 1;
		
		// しゃくとり法でpとqの差を最小にする
		auto p = ts[ bs[ 1 ] ] - ts[ bs[ 0 ] ];
		auto q = ts[ bs[ 2 ] ] - ts[ bs[ 1 ] ];
		auto pq = abs( p - q );
		for( ; bs[ 1 ] + 1 < bs[ 2 ]; bs[ 1 ]++ )
		{
			auto pp = ts[ bs[ 1 ] + 1 ] - ts[ bs[ 0 ] ];
			auto qq = ts[ bs[ 2 ] ] - ts[ bs[ 1 ] + 1 ];
			auto ppqq = abs( pp - qq );
			if( pq <= ppqq ) break;
			p = pp;
			q = qq;
			pq = ppqq;
		}
		
		// しゃくとり法でrとsの差を最小にする
		auto r = ts[ bs[ 3 ] ] - ts[ bs[ 2 ] ];
		auto s = ts[ bs[ 4 ] ] - ts[ bs[ 3 ] ];
		auto rs = abs( r - s );
		for( ; bs[ 3 ] + 1 < bs[ 4 ]; bs[ 3 ]++ )
		{
			auto rr = ts[ bs[ 3 ] + 1 ] - ts[ bs[ 2 ] ];
			auto ss = ts[ bs[ 4 ] ] - ts[ bs[ 3 ] + 1 ];
			auto rrss = abs( rr - ss );
			if( rs <= rrss ) break;
			r = rr;
			s = ss;
			rs = rrss;
		}
		
		// 総和の最大と最小の差を計算
		ans = min( ans, max( p, q, r, s ) - min( p, q, r, s ) );
	}
	return ans;
}
