//Equal Cut
//by ACMonster

#include<bits/stdc++.h>

#define N 200000+5
#define INF 200000000000000

using namespace std;

int n,i,j,k,a[N];
long long ans,pre[N],s1,s2,s3,s4;

int main(){
	scanf("%d",&n);
	for (i=1;i<=n;i++) {
		scanf("%d",&a[i]);
		pre[i]=pre[i-1]+a[i];
	}
	ans=INF;	
	for (i=1,j=2,k=3;j<=n-2;j++) {
		while (abs(pre[j]-2*pre[i+1])<abs(pre[j]-2*pre[i])) i++;
		while (abs(pre[n]+pre[j]-2*pre[k+1])<abs(pre[n]+pre[j]-2*pre[k])) k++;
		s1=pre[i];
		s2=pre[j]-pre[i];
		s3=pre[k]-pre[j];
		s4=pre[n]-pre[k];	
		ans=min(ans,max( max(abs(s2-s3),max(abs(s2-s4),abs(s3-s4))),max( abs(s1-s3) ,max(abs(s1-s4),abs(s1-s2) ) ) ) );
	}
	printf("%lld",ans);
}