var toInt = x=>parseInt(x,10);
var toIntArr = arr=>arr.map(x=>toInt(x));
var abs = x=>x>0?x:-x;
function min(){var rest = arguments,val=rest[0];for(var i=1;i<rest.length;i++)if(rest[i]<val)val = rest[i];return val}
function max(){var rest = arguments,val=rest[0];for(var i=1;i<rest.length;i++)if(rest[i]>val)val = rest[i];return val}
Main(require("fs").readFileSync("/dev/stdin", "utf8"));
 
function Main(input){
	var input = input.split('\n');
	var n = toInt(input[0]);
	var as = toIntArr(input[1].split(' '));
	var ss = []; //累積和
	ss[-1] = 0;
	for(var i = 0; i < n; i++){
		ss[i] = ss[i-1]+as[i];
	}
	var l = 1;
	var r = n-1;
	var sumL;
	var _ans = [];
	var pqrs = [];
	var ls = [];
	var rs = [];
	for(var j = 2; j < n-1; j++){
		//L
		while(true){
			if(l === j-1){
				break;
			}
			if(abs(ss[j-1]-2*ss[l-1])<abs(ss[j-1]-2*ss[l])){
				break;
			}
			l++;
		}
		ls[j] = l;
	}
	for(var k = n-2; k > 1; k--){
		while(true){
			if(r === k+1){
				break;
			}
			if(abs((ss[n-1]-ss[k-1])-2*(ss[r-1]-ss[k-1]))<abs((ss[n-1]-ss[k-1])-2*(ss[r-2]-ss[k-1]))){
				break;
			}
			r--;
		}
		rs[k] = r;
	}
	var ans = 0,_ans = 0;
	for(var x = 2; x < n-1; x++){
		pqrs = [ss[ls[x]-1],ss[x-1]-ss[ls[x]-1],ss[rs[x]-1]-ss[x-1],ss[n-1]-ss[rs[x]-1]]; 
		_ans = max.apply(this,pqrs) - min.apply(this,pqrs);
		if(x===2){
			ans = _ans;
		}else if(ans > _ans){
			ans = _ans;
		}
	}
	console.log(ans);
}