#include <bits/stdc++.h>

#define ll long long
#define mx  200005
#define mod 1000000009
#define lim 10000000
#define N 18
using namespace std;

pair<ll,ll>L[mx],R[mx];

int main(){
	ios_base::sync_with_stdio(false);
	int n;cin>>n;
	int a[n+5];

	for(int i=0;i<n;i++)cin>>a[i];

	ll l1=0,l2=0,r1=0,r2=0;int j=0;

	for(int i=0;i<n;i++)
	{
		l1+=1LL*a[i];
		while(j<=i && abs(l1-l2)>=abs(l1-a[j]-l2-a[j]))
			{
				l1-=a[j],l2+=1LL*a[j];j++;
			}
		L[i]={l1,l2};
	}
	j=n-1;
	for(int i=n-1;i>=0;i--)
	{
		r1+=1LL*a[i];
		while(j>=i && abs(r1-r2)>=abs(r1-a[j]-r2-a[j]))
		{
			r1-=a[j],r2+=1LL*a[j];j--;
		}
		R[i]={r1,r2};
	}
	ll ans=1e16;
	for(int i=1;i<n-2;i++)
	{
		ll v[]={L[i].first,L[i].second,R[i+1].first,R[i+1].second};
		sort(v,v+4);
		ans=min(ans,v[3]-v[0]);
	}
	cout<<ans<<"\n";
     return 0;
}
