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;
	}
	
	auto ans = long.max;
	auto ps = [ 0L, 1L, 2L, 3L, as.length.signed ];
	for( ; ps[ 2 ] < as.length.signed - 1; ps[ 2 ]++ )
	{
		if( ps[ 3 ] <= ps[ 2 ] ) ps[ 3 ] = ps[ 2 ] + 1;
		
		auto p = ts[ ps[ 1 ] ] - ts[ ps[ 0 ] ];
		auto q = ts[ ps[ 2 ] ] - ts[ ps[ 1 ] ];
		while( ps[ 1 ] + 1 < ps[ 2 ] )
		{
			auto da = abs( p - q );
			auto db = abs( ( p + as[ ps[ 1 ] ] ) - ( q - as[ ps[ 1 ] ] ) );
			if( da < db ) break;
			p += as[ ps[ 1 ] ];
			q -= as[ ps[ 1 ] ];
			ps[ 1 ]++;
		}
		
		auto r = ts[ ps[ 3 ] ] - ts[ ps[ 2 ] ];
		auto s = ts[ ps[ 4 ] ] - ts[ ps[ 3 ] ];
		while( ps[ 3 ] + 1 < ps[ 4 ] )
		{
			auto da = abs( r - s );
			auto db = abs( ( r + as[ ps[ 3 ] ] ) - ( s - as[ ps[ 3 ] ] ) );
			if( da < db ) break;
			r += as[ ps[ 3 ] ];
			s -= as[ ps[ 3 ] ];
			ps[ 3 ]++;
		}
		
		auto ms = [ p, q, r, s ].reduce!( min, max );
		ans = min( ans, ms[ 1 ] - ms[ 0 ] );
	}
	return ans;
}
