// Slower C++ solution:
#include <cstdio>

#include <cstring>

#include <algorithm>

using namespace std;

const int maxn=2e5+100;

typedef long long LL;

int n;

LL a[maxn],p[5];



int main(int argc, char const *argv[])

{

    while (scanf("%d",&n)!=EOF)

    {

        memset(a,0,sizeof(a));

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

        {

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

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

        }



        LL ans=3e14;

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

        {

            int x=lower_bound(a+1,a+i+1,a[i]/2)-a;

            int y=lower_bound(a+1,a+n+1,a[i]+(a[n]-a[i])/2)-a;

            if (x!=1) x--;

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



            p[0]=a[x];

            p[1]=a[i]-a[x];

            p[2]=a[y]-a[i];

            p[3]=a[n]-a[y];

            ans=min(ans,abs(max(p[0],max(p[1],max(p[2],p[3])))-min(p[0],min(p[1],min(p[2],p[3])))));



            int x1=x+1,y1=y;

            p[0]=a[x1];

            p[1]=a[i]-a[x1];

            p[2]=a[y1]-a[i];

            p[3]=a[n]-a[y1];

            ans=min(ans,abs(max(p[0],max(p[1],max(p[2],p[3])))-min(p[0],min(p[1],min(p[2],p[3])))));



            int x2=x,y2=y+1;

            p[0]=a[x2];

            p[1]=a[i]-a[x2];

            p[2]=a[y2]-a[i];

            p[3]=a[n]-a[y2];

            ans=min(ans,abs(max(p[0],max(p[1],max(p[2],p[3])))-min(p[0],min(p[1],min(p[2],p[3])))));



            int x3=x+1,y3=y+1;

            p[0]=a[x3];

            p[1]=a[i]-a[x3];

            p[2]=a[y3]-a[i];

            p[3]=a[n]-a[y3];

            ans=min(ans,abs(max(p[0],max(p[1],max(p[2],p[3])))-min(p[0],min(p[1],min(p[2],p[3])))));

        }

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

    }

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.7676193588421458x):
#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;

}