#include "bits/stdc++.h"
    using namespace std;
    ///////////////////////////////////////////
    const long long int INF = 1LL<<60;
    const long long int Mod = 1000000007;
    using ll = long long int; using ci = const int;
    using vi = vector<int>;  using Vi = vector<long long int>;
    using P = pair<int, int>;  using PLL = pair<ll, ll>;
    using matrix = vector<vector<ll>>;
    #define pb(x) push_back(x)
    #define mp(x,y) make_pair(x,y)
    #define all(x) (x).begin(),(x).end()
    #define rep(i,N) for(ll i = 0; i < (ll)N; i++)
    #define repi(i,a,b) for(ll i = ll(a); i < ll(b); ++i)
    template<class T>bool chmax(T &former, const T &b) { if (former<b) { former=b; return true; } return false; }
    template<class T>bool chmin(T &former, const T &b) { if (b<former) { former=b; return true; } return false; }
    template<class T>T sqar(T x){ return x*x; }//sqrt(x)は平方根;
    #define Sort(v) std::sort(v.begin(), v.end(), std::greater<decltype(v[0])>()) //降順でVをソート
    #define p_queue(v) priority_queue<v, vector<v>, greater<v> >
    template<class T> inline void princ(T x){cout<<x<<" ";}; 
    template<class T> inline void print(T x){cout<<x<<"\n";};
    template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; }
    template<class T> inline void YES(T condition){ if(condition) cout << "YES" << endl; else cout << "NO" << endl; }
    ///////////////////////////////////////////////////////////////////////////////////
     
    ll n;
    Vi a;
    Vi sum;
    bool ck(ll l,ll r,ll mid){//[l,mid),[mid,r)
        ll tmp = sum[mid] - sum[l] - sum[r] + sum[mid];
        if(tmp>0)return true;
        else return false;
    }
     
     
    int main(){
        cin.tie(0);ios::sync_with_stdio(false);
        std::cout<<std::fixed<<std::setprecision(30);
        cin>>n;
        a.resize(n);
        sum.resize(n+1,0);
        rep(i,n){
            cin>>a[i];
            sum[i+1] += sum[i] + a[i];
        }
        ll ans = INF;
        Vi v;
        repi(i,2,n-1){//[0,i),[i,n)
            v.clear();
            ll mid, ok = i, ng = 0;
            while(abs(ok-ng)>1){
                mid = (ok+ng)/2;
                if(ck(0,i,mid))ok = mid;
                else ng = mid;
            }//[~,mid) > 0
            mid = ok;
            if(abs(sum[ok]-sum[0]-sum[i]+sum[mid])>abs(sum[mid-1]-sum[0]-sum[i]+sum[mid-1])){
                v.pb(sum[mid-1]-sum[0]);
                v.pb(sum[i]-sum[mid-1]);
            }else{
                v.pb(sum[mid]-sum[0]);
                v.pb(sum[i]-sum[mid]);
            }
            ok = n, ng = i;
            while(abs(ok-ng)>1){
                mid = (ok+ng)/2;
                if(ck(i,n,mid))ok = mid;
                else ng = mid;
            }
            mid = ok;
            if(abs(sum[mid]-sum[i]-sum[n]+sum[mid])>abs(sum[mid-1]-sum[i]-sum[n]+sum[mid-1])){
                v.pb(sum[mid-1]-sum[i]);
                v.pb(sum[n]-sum[mid-1]);
            }else{
                v.pb(sum[mid]-sum[i]);
                v.pb(sum[n]-sum[mid]);
            }
            //rep(i,4)princ(v[i]);
            sort(all(v));
            //print("");
            chmin(ans,abs(v[3]-v[0]));
        }
        print(ans);
        return 0;
    }

Measured memory_bytes: 3328