#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int n;
ll a[200005];
ll sum[200005];

bool C(ll v){
	
	return false;
}

int main(void){
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%lld",&a[i]);
		sum[i+1]+=sum[i];
		sum[i+1]+=a[i];
	}
	ll r=1LL<<60;
	for(int i=2;i<=n-2;i++){
		ll mid=sum[i];
		int v1=lower_bound(sum,sum+i+1,sum[i]/2LL)-sum;
		ll d1=sum[v1];
		if(v1>1 && labs((d1)-(mid-d1))>labs(sum[v1-1]-(mid-sum[v1-1]))){
			d1=sum[v1-1];
		}
		ll mn1=min(d1,mid-d1),mx1=max(d1,mid-d1);
		
		mid=sum[n]-sum[i];
		v1=lower_bound(sum,sum+n+1,sum[i]+mid/2LL)-sum;
		d1=sum[v1]-sum[i];
		if(v1>i+1 && labs((d1)-(mid-d1))>labs(sum[v1-1]-sum[i]-(mid-(sum[v1-1]-sum[i])))){
			d1=sum[v1-1]-sum[i];
		}
		ll mn2=min(d1,mid-d1),mx2=max(d1,mid-d1);
		ll mn=min(mn1,mn2),mx=max(mx1,mx2);
		r=min(r,mx-mn);
	}
	printf("%lld\n",r);
	return 0;
}