#include<cstdio>
#include<algorithm>
#define LL long long 
using namespace std;
LL a[200010],t1,t2,t3,t4,t,ans;
int n,l,r;
void pan(LL x,LL y,LL x1,LL y1)
{
	if (x<y) swap(x,y);
	if (x1<y1) swap(x1,y1);
	t=min(t,max(max(x-y,x1-y1),max(abs(x-y1),abs(x1-y))));
}
int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
	{
		scanf("%lld",&a[i]);
		a[i]=a[i-1]+a[i];
	}
	l=1;
	r=3;
	ans=10000000000000000;
	for (int i=2;i<=n-2;i++)
	{
		while ((a[i]-a[l+1])>=a[l+1]&&l<i) l++;
		if (r<=i) r=i+1;
		while ((a[r+1]-a[i])<=a[n]-a[r+1]&&r<n) r++; 
		t1=a[i]-a[l];
		t2=a[l];
		t3=a[n]-a[r];
		t4=a[r]-a[i];
		t=100000000000000000;
		pan(t1,t2,t3,t4);
		if (l<i-1) pan(t2+a[l+1]-a[l],t1-(a[l+1]-a[l]),t3,t4);
		if (l<i-1&&r<n-1) pan(t2+a[l+1]-a[l],t1-(a[l+1]-a[l]),t4+a[r+1]-a[r],t3-(a[r+1]-a[r]));
		if (r<n-1) pan(t1,t2,t4+a[r+1]-a[r],t3-(a[r+1]-a[r]));
		ans=min(ans,t);
	}
	printf("%lld",ans);
}