// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

const int maxn=2e5+25;

#define ll long long int

#define INF 0x3f3f3f3f

int n;

int a[maxn];

ll sum[maxn];

ll b[5];

int main()

{

    int n;

    scanf("%d",&n);

    memset(sum,0,sizeof sum);

    for(int i=1;i<=n;i++){

        scanf("%d",&a[i]);

        sum[i]=sum[i-1]+a[i];

    }

    ll ans=-1;

    for(int i=2;i<=n-1;i++){

        ll s1=sum[i];

        ll s2=sum[n]-sum[i];

        ll x1,x2,y1,y2;

        ll av1=s1/2;ll av2=s2/2;

        // 1.0

        x1=lower_bound(sum+1,sum+i+1,av1)-sum;

        y1=lower_bound(sum+i+2,sum+1+n,s1+av2)-sum;

       // cout<<x1<<" "<<y1<<endl;

        b[1]=sum[x1];

        b[2]=s1-sum[x1];

        b[3]=sum[y1]-s1;

        b[4]=s2-b[3];

        //cout<<b[1]<<" "<<b[2]<<" "<<b[3]<<" "<<b[4]<<endl;

        sort(b+1,b+5);

        //cout<<b[1]<<" "<<b[2]<<" "<<b[3]<<" "<<b[4]<<endl;

        if(ans==-1){

            ans=abs(b[4]-b[1]);

        }

        ans=min(ans,abs(b[4]-b[1]));

        //cout<<ans<<endl;





        x1=lower_bound(sum+1,sum+i+1,av1)-sum;

        y1=lower_bound(sum+i+2,sum+1+n,s1+av2)-sum;

       // cout<<x1<<" "<<y1<<endl;

        x1--;y1--;

        b[1]=sum[x1];

        b[2]=s1-sum[x1];

        b[3]=sum[y1]-s1;

        b[4]=s2-b[3];

        //cout<<b[1]<<" "<<b[2]<<" "<<b[3]<<" "<<b[4]<<endl;

        sort(b+1,b+5);

        //cout<<b[1]<<" "<<b[2]<<" "<<b[3]<<" "<<b[4]<<endl;

        if(ans==-1){

            ans=abs(b[4]-b[1]);

        }

        ans=min(ans,abs(b[4]-b[1]));







        x1=lower_bound(sum+1,sum+i+1,av1)-sum;

        y1=lower_bound(sum+i+2,sum+1+n,s1+av2)-sum;

       // cout<<x1<<" "<<y1<<endl;

        x1--;

        b[1]=sum[x1];

        b[2]=s1-sum[x1];

        b[3]=sum[y1]-s1;

        b[4]=s2-b[3];

        //cout<<b[1]<<" "<<b[2]<<" "<<b[3]<<" "<<b[4]<<endl;

        sort(b+1,b+5);

        //cout<<b[1]<<" "<<b[2]<<" "<<b[3]<<" "<<b[4]<<endl;

        if(ans==-1){

            ans=abs(b[4]-b[1]);

        }

        ans=min(ans,abs(b[4]-b[1]));





        x1=lower_bound(sum+1,sum+i+1,av1)-sum;

        y1=lower_bound(sum+i+2,sum+1+n,s1+av2)-sum;

       // cout<<x1<<" "<<y1<<endl;

        y1--;

        b[1]=sum[x1];

        b[2]=s1-sum[x1];

        b[3]=sum[y1]-s1;

        b[4]=s2-b[3];

        //cout<<b[1]<<" "<<b[2]<<" "<<b[3]<<" "<<b[4]<<endl;

        sort(b+1,b+5);

        //cout<<b[1]<<" "<<b[2]<<" "<<b[3]<<" "<<b[4]<<endl;

        if(ans==-1){

            ans=abs(b[4]-b[1]);

        }

        ans=min(ans,abs(b[4]-b[1]));



        //2.0

        av1=s1/2;av2=s2/2;

        x1=upper_bound(sum+1,sum+i+1,av1)-sum;

        y1=lower_bound(sum+i+2,sum+1+n,s1+av2)-sum;

       // cout<<x1<<" "<<y1<<endl;

        b[1]=sum[x1];

        b[2]=s1-sum[x1];

        b[3]=sum[y1]-s1;

        b[4]=s2-b[3];

        //cout<<b[1]<<" "<<b[2]<<" "<<b[3]<<" "<<b[4]<<endl;

        sort(b+1,b+5);

        //cout<<b[1]<<" "<<b[2]<<" "<<b[3]<<" "<<b[4]<<endl;

        if(ans==-1){

            ans=abs(b[4]-b[1]);

        }

        ans=min(ans,abs(b[4]-b[1]));



        //3.0

        av1=s1/2;av2=s2/2;

        x1=lower_bound(sum+1,sum+i+1,av1)-sum;

        y1=upper_bound(sum+i+2,sum+1+n,s1+av2)-sum;

       // cout<<x1<<" "<<y1<<endl;

        b[1]=sum[x1];

        b[2]=s1-sum[x1];

        b[3]=sum[y1]-s1;

        b[4]=s2-b[3];

        //cout<<b[1]<<" "<<b[2]<<" "<<b[3]<<" "<<b[4]<<endl;

        sort(b+1,b+5);

        //cout<<b[1]<<" "<<b[2]<<" "<<b[3]<<" "<<b[4]<<endl;

        if(ans==-1){

            ans=abs(b[4]-b[1]);

        }

        ans=min(ans,abs(b[4]-b[1]));



        //4.0

        av1=s1/2;av2=s2/2;

        x1=upper_bound(sum+1,sum+i+1,av1)-sum;

        y1=upper_bound(sum+i+2,sum+1+n,s1+av2)-sum;

       // cout<<x1<<" "<<y1<<endl;

        b[1]=sum[x1];

        b[2]=s1-sum[x1];

        b[3]=sum[y1]-s1;

        b[4]=s2-b[3];

        //cout<<b[1]<<" "<<b[2]<<" "<<b[3]<<" "<<b[4]<<endl;

        sort(b+1,b+5);

        //cout<<b[1]<<" "<<b[2]<<" "<<b[3]<<" "<<b[4]<<endl;

        if(ans==-1){

            ans=abs(b[4]-b[1]);

        }

        ans=min(ans,abs(b[4]-b[1]));



    }

    printf("%lld\n",ans);

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 2.586709260007364x):
#include<map>

#include<stack>

#include<queue>

#include<cmath>

#include<string>

#include<cstdio>

#include<cstring>

#include<iomanip>

#include<iostream>

#include<algorithm>

using namespace std;

#define LL long long int

#define lson rt<<1,l,m

#define rson rt<<1|1,m+1,r

const int inf = 0x3f3f3f3f;

long long a[222222];

long long pre[222222];

long long roo[222222];

int main()

{

	int n;

	while (cin >> n )

	{

		for (int i = 0; i < n; i++)

			cin >> a[i];

		long long ans = -1;

		pre[0] = a[0];

		roo[n - 1] = a[n - 1];

		for (int i = 1; i < n; i++)

			pre[i] = pre[i - 1] + a[i];

		for (int i = n - 2; i >= 0; i--)

			roo[i] = roo[i + 1] + a[i];

		for (int i = 1; i <= n - 3; i++)

		{

			long long ls = pre[i] ;

			long long rs = roo[i + 1];

			long long l1, l2, r1, r2;

			long long lz = lower_bound(pre, pre + i + 1, ls / 2)-pre;

			if (lz != 0)lz--;

			l1 = pre[lz];

			l2 = ls - l1;

			long long rz = lower_bound(pre, pre + n, ls + rs / 2) - pre;

			if (rz != i + 1)rz--;

			r1 = pre[rz] - pre[i];

			r2 = rs - r1;

			long long zs = max(l1, max(l2, max(r1, r2)));

			zs = abs(zs - min(l1, min(l2, min(r1, r2))));

			if (ans == -1 || ans > zs)

				ans=zs;

			l1 = pre[lz + 1];

			l2 = ls - l1;

			r1 = pre[rz] - pre[i];

			r2 = rs - r1;

			zs = max(l1, max(l2, max(r1, r2)));

			zs = abs(zs - min(l1, min(l2, min(r1, r2))));

			if (ans == -1 || ans > zs)

				ans = zs;

			l1 = pre[lz + 1];

			l2 = ls - l1;

			r1 = pre[rz+1] - pre[i];

			r2 = rs - r1;

			zs = max(l1, max(l2, max(r1, r2)));

			zs = abs(zs - min(l1, min(l2, min(r1, r2))));

			if (ans == -1 || ans > zs)

				ans = zs;

			l1 = pre[lz];

			l2 = ls - l1;

			r1 = pre[rz+1] - pre[i];

			r2 = rs - r1;

			zs = max(l1, max(l2, max(r1, r2)));

			zs = abs(zs - min(l1, min(l2, min(r1, r2))));

			if (ans == -1 || ans > zs)

				ans = zs;

		}

		cout << ans << endl;

	}

	return 0;

}