// 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.560590151690898x):
#include <bits/stdc++.h>

using namespace std;



const int N = 220000;



int a[N], mid[2][N];

long long sum[N];



int main() {

	

	ios::sync_with_stdio(0);

	cin.tie(0), cout.tie(0);

	

	int n; cin >> n;

	for (int i = 1; i <= n; i++) cin >> a[i], sum[i] = sum[i - 1] + a[i];

	sum[n + 1] = sum[n];

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

		mid[0][i] = mid[0][i - 1];

		while (mid[0][i] <= n && sum[mid[0][i]] * 2 < sum[i]) mid[0][i]++;

	}

	mid[1][n + 1] = n + 1;

	for (int i = n; i >= 1; i--) {

		mid[1][i] = mid[1][i + 1];

		while (mid[1][i] >= 1 && (sum[n + 1] - sum[mid[1][i]]) * 2 < (sum[n + 1] - sum[i])) mid[1][i]--;

	}

	long long tot = sum[n + 1];

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

		auto Max = max(max(sum[mid[0][i]], sum[i] - sum[mid[0][i]]), max(sum[n + 1] - sum[mid[1][i]], sum[mid[1][i]] - sum[i]));

		auto Min = min(min(sum[mid[0][i]], sum[i] - sum[mid[0][i]]), min(sum[n + 1] - sum[mid[1][i]], sum[mid[1][i]] - sum[i]));

		tot = min(tot, Max - Min);

		

		mid[0][i]--;

		Max = max(max(sum[mid[0][i]], sum[i] - sum[mid[0][i]]), max(sum[n + 1] - sum[mid[1][i]], sum[mid[1][i]] - sum[i]));

		Min = min(min(sum[mid[0][i]], sum[i] - sum[mid[0][i]]), min(sum[n + 1] - sum[mid[1][i]], sum[mid[1][i]] - sum[i]));

		tot = min(tot, Max - Min);

		mid[0][i]++;

		

		mid[1][i]++;

		Max = max(max(sum[mid[0][i]], sum[i] - sum[mid[0][i]]), max(sum[n + 1] - sum[mid[1][i]], sum[mid[1][i]] - sum[i]));

		Min = min(min(sum[mid[0][i]], sum[i] - sum[mid[0][i]]), min(sum[n + 1] - sum[mid[1][i]], sum[mid[1][i]] - sum[i]));

		tot = min(tot, Max - Min);

		mid[1][i]--;

		

		mid[0][i]--, mid[1][i]++;

		Max = max(max(sum[mid[0][i]], sum[i] - sum[mid[0][i]]), max(sum[n + 1] - sum[mid[1][i]], sum[mid[1][i]] - sum[i]));

		Min = min(min(sum[mid[0][i]], sum[i] - sum[mid[0][i]]), min(sum[n + 1] - sum[mid[1][i]], sum[mid[1][i]] - sum[i]));

		tot = min(tot, Max - Min);

		mid[0][i]++, mid[1][i]--;

	}

	

	cout << tot << "\n";

	

	return 0;

}