#include<bits/stdc++.h>
using namespace std;
#define MN 200000
long long s[MN+5],S[4],ans=1e18;
int main()
{
	int n,i,j,k,x,y;
	scanf("%d",&n);
	for(i=1;i<=n;++i)scanf("%lld",&s[i]),s[i]+=s[i-1];
	for(i=2;i<n;++i)
	{
		x=lower_bound(s+1,s+n+1,s[i]/2)-s;
		y=lower_bound(s+1,s+n+1,s[i]+(s[n]-s[i])/2)-s;
		for(j=max(1,x-10);j<=min(x+10,i-1);++j)
			for(k=max(i+1,y-10);k<=min(y+10,n-1);++k)
			{
				S[0]=s[j];
				S[1]=s[i]-s[j];
				S[2]=s[k]-s[i];
				S[3]=s[n]-s[k];
				sort(S,S+4);
				ans=min(ans,S[3]-S[0]);
			}
	}
	printf("%lld",ans);
}