// 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.7514379720015945x):
#include <iostream>

#include <cstdio>

#include <vector>

#include <algorithm>

#include <cstring>

#include <map>

#include <set>

#include <cstring>

#include <iomanip>

#include <queue>

#include <deque>

#include <cmath>

using namespace std;

#define inf 280201280201280201

#define mn 200005

#define FLN "test"

#define int long long

typedef pair<int, int> ii;

//constance



//common data structures and templates

template<typename T>

struct SegmentTree{

};

//gb

int n;

int s[mn];

//functions here

void input(){

}

vector<ii> solve(int left, int right){

  vector<ii> ans;

  int l = left, r = right-1, lower=left;

  while(l<= r){

    int mid = (l+r)>>1;

    if(s[mid]-s[left-1] <= s[right]-s[mid]){

      lower = mid;

      l = mid+1;

    }

    else r = mid-1;

  }

  ans.push_back({s[lower]-s[left-1], s[right]-s[lower]});

  l = left, r = right-1; 

  int higher=left;

  while(l<= r){

    int mid = (l+r)>>1;

    if(s[mid]-s[left-1] >= s[right]-s[mid]){

      higher = mid;

      r = mid-1;

    }

    else l = mid+1;

  }

  ans.push_back({s[higher]-s[left-1], s[right]-s[higher]});

  return ans;

}



//#define LOCAL 1



signed main(signed argc, char** argv) {

	ios_base::sync_with_stdio(false);

	cin.tie(0);

	cout.tie(0);

#ifdef LOCAL

	   freopen(FLN".inp", "r", stdin);

	   freopen(FLN".out", "w", stdout);

#endif

  cin >> n;

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

    cin >> s[i];

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

  }

  //1- >i && i+1 -> n

  vector<ii> p, q;

  int ans= inf;

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

    //1->i

    p = solve(1, i);

    q = solve(i+1,n);

    for(ii x : p)

      for(ii y : q){

        int mi = min(min(x.first, x.second), min(y.first, y.second));

        int ma = max(max(x.first, x.second), max(y.first, y.second));

        ans = min(ans, ma - mi);

      }

  }

  cout << ans;

	return 0;

}