#include <bits/stdc++.h>
using namespace std;
#define repr(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define reprrev(i,a,b) for(int i=b-1;i>=a;i--) // [a, b)
#define reprev(i,n) reprrev(i,0,n)
typedef long long ll;
typedef unsigned long long ull;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

const ll mod = 1e9+7;

void chmod(ll &M){
    if(M >= mod) M %= mod;
    else if(M < 0){
        M += (abs(M)/mod + 1)*mod;
        M %= mod;
    }
}

ll modpow(ll x, ll n){
    if(n==0) return 1;
    ll res=modpow(x, n/2);

    if(n%2==0) return res*res%mod;
    else return res*res%mod*x%mod;
}

int getl(int i, int N) { return i==0? N-1:i-1; };
int getr(int i, int N) { return i==N-1? 0:i+1; };

// 線分 ab の偏角 返り値は[-π, π]
double argument(const pair<double, double> &a, const pair<double, double> &b){
    double ax=a.first, ay=a.second, bx=b.first, by=b.second;
    return atan2(by-ay, bx-ax);
}

/* <-----------------------------------------------------------------------------------> */
/* <-----------------------------------------------------------------------------------> */
/* <-----------------------------------------------------------------------------------> */
/* <-----------------------------------------------------------------------------------> */
/* <-----------------------------------------------------------------------------------> */
/* <-----------------------------------------------------------------------------------> */
/* <-----------------------------------------------------------------------------------> */
/* <-----------------------------------------------------------------------------------> */

int N;
vector<ll> A;
vector<ll> sum;

ll getsum(int l, int r) { return sum[r] - (l==0? 0:sum[l-1]); }

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    cin >> N;
    A.resize(N+1, 0), sum.resize(N+1, 0);
    repr(i, 1, N+1) cin >> A[i];
    repr(i, 1, N+1) sum[i] = sum[i-1] + A[i];

    ll ans = 1LL<<60;
    repr(i, 2, N-1){
        int low = 0, high = i;
        while(high-low > 1){
            int mid = (high+low) / 2;
            if(getsum(0, mid) < getsum(mid+1, i)) low = mid;
            else high = mid;
        }
        vector<ll> le = {low, high};
        
        //cout << i << ": " << le[0] << " " << le[1] << endl;

        low = i, high = N;
        while(high-low > 1){
            int mid = (high+low) / 2;
            if(getsum(i+1, mid) < getsum(mid+1, N)) low = mid;
            else high = mid;
        }
        vector<ll> ri = {low, high};

        //cout << i << ": " << ri[0] << " " << ri[1] << endl;

        rep(j, 2) rep(k, 2){
            if(le[j] == 0 || le[j] == i) continue;
            if(ri[k] == i || ri[k] == N) continue;
            ll p = getsum(1, le[j]);
            ll q = getsum(le[j]+1, i);
            ll r = getsum(i+1, ri[k]);
            ll s = getsum(ri[k]+1, N);
            ll tmp = max(max(p, q), max(r, s)) - min(min(p, q), min(r, s));
            chmin(ans, tmp);
        }
    }
    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 3456