#include<cstdio>
#include<algorithm>
using namespace std;
long long a[200005],s[200005];
int main(){
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;++i){
		scanf("%lld",&a[i]);
		s[i]=s[i-1]+a[i];
	}
	long long l=1,r=3,p,q,u,t,ans=0x7fffffff;
	for(int i=2;i<n-1;++i){
		while(l<i&&abs(s[l]-s[0]-(s[i]-s[l]))>=abs(s[l+1]-s[0]-(s[i]-s[l+1])))
			++l;
		while(r<n&&abs(s[r]-s[i]-(s[n]-s[r]))>=abs(s[r+1]-s[i]-(s[n]-s[r+1])))
			++r;
		p=s[l]-s[0];
		q=s[i]-s[l];
		u=s[r]-s[i];
		t=s[n]-s[r];
		ans=min(ans,max(p,max(q,max(u,t)))-min(p,min(q,min(u,t))));
	}		
	printf("%lld",ans);
}