#include <bits/stdc++.h>
using namespace std;
#define rep(i,N) for(int i=0;i<int(N);++i)
#define rep1(i,N) for(int i=1;i<int(N);++i)
#define all(a) (a).begin(),(a).end()
#define print(v) { cerr<<#v<<": [ "; for(auto _ : v) cerr<<_<<", "; cerr<<"]"<<endl; }
#define printpair(v) { cerr<<#v<<": [ "; for(auto _ : v) cerr<<"{"<<_.first<<","<<_.second<<"}"<<", "; cerr<<"]"<<endl; }
#define dump(x) cerr<<#x<<": "<<x<<endl;
#define bit(k) (1LL<<(k))
typedef long long ll;
typedef pair<int, int> i_i;
typedef pair<ll, ll> l_l;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;
template< typename T1, typename T2 >
ostream &operator<<(ostream &os, const pair< T1, T2 >& p) {
  os << "{" <<p.first << ", " << p.second << "}";
  return os;
}
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 int INF = (ll)1e9;
const ll INFLL = (ll)1e18+1;
const ll MOD = (ll)1e9+7;
const double PI = acos(-1.0);
/*
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const string dir = "DRUL";
*/

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);

    ll N;
    cin >> N;
    vector<ll> A(N);
    rep(i, N)cin >> A[i];
    vector<ll> cum(N+1,0);
    rep(i,N)cum[i+1] = A[i] + cum[i];
    // print(cum);

    // auto check = [&](int l, int mid, int r) -> bool{
    //     // ll sum = cum[r] - cum[l];
    //     ll l_sum = cum[mid] - cum[l];
    //     ll r_sum = cum[r] - cum[mid];
    //     return l_sum < r_sum;
    // };
    // auto check2 = [&](int l, int mid, int r) -> bool{
    //     // ll sum = cum[r] - cum[l];
    //     ll l_sum = cum[mid] - cum[l];
    //     ll r_sum = cum[r] - cum[mid];
    //     return l_sum > r_sum;
    // };
    ll ans = INFLL;
    auto calc = [&](int l, int mid, int r) -> ll{
        ll sums[] = {
            cum[l] - cum[0],
            cum[mid] - cum[l],
            cum[r] - cum[mid],
            cum[N] - cum[r]
        };
        ll maxx = 0;
        ll minn = INFLL;
        rep(j,4){
            chmax(maxx, sums[j]);
            chmin(minn, sums[j]);
        }
        // print(sums);
        return maxx - minn;
    };
    for(int i = 2; i < N-1; i++){
        // int l[2],r[2];
        // // [0,l),[l,i)
        // int ok = 1;
        // int ng = i-1;
        // while(ng - ok > 1){
        //     int mid = (ok+ng)>>1;
        //     if(check(0,mid,i))ok = mid;
        //     else ng = mid;
        // }
        // l[0] = ok;
        // // 
        // ok = 1;
        // ng = i-1;
        // while(ng - ok > 1){
        //     int mid = (ok+ng)>>1;
        //     if(check2(0,mid,i))ok = mid;
        //     else ng = mid;
        // }
        // l[1] = ok;
        // // [i,r), [r,N)
        // ok = i+1;
        // ng = N-1;
        // while(ng - ok > 1){
        //     int mid = (ok + ng)>>1;
        //     if(check(i,mid,N))ok = mid;
        //     else ng = mid;
        // }
        // r[0] = ok;
        // //
        // ok = i+1;
        // ng = N-1;
        // while(ng - ok > 1){
        //     int mid = (ok + ng)>>1;
        //     if(check2(i,mid,N))ok = mid;
        //     else ng = mid;
        // }
        // r[1] = ok;
        // //
        // rep(a,2)rep(b,2){
        //     ll sums[] = {
        //         cum[l[a]] - cum[0],
        //         cum[i] - cum[l[a]],
        //         cum[r[b]] - cum[i],
        //         cum[N] - cum[r[b]],
        //     };
        //     ll maxx = 0;
        //     ll minn = INFLL;
        //     rep(j,4){
        //         chmax(maxx, sums[j]);
        //         chmin(minn, sums[j]);
        //     }
        //     chmin(ans, maxx - minn);
        //     cerr << l[a] << " " << i << " " << r[b] << endl;
        // }
        
        int l[2], r[2];
        // [0, i)
        {
            ll sum = cum[i] - cum[0];
            int ok = 1;
            int ng = i-1;
            while(abs(ok - ng) > 1){
                int mid = (ok + ng) >> 1;
                ll tmp = cum[mid] - cum[0];
                if(2*tmp <= sum)ok = mid;
                else ng = mid;
            }
            l[0] = ok;
        }
        {
            ll sum = cum[i] - cum[0];
            int ng = 1;
            int ok = i-1;
            while(abs(ok - ng) > 1){
                int mid = (ok + ng) >> 1;
                ll tmp = cum[i] - cum[mid];
                if(2*tmp <= sum)ok = mid;
                else ng = mid;
            }
            l[1] = ok;
        }
        // [i, N)
        {
            ll sum = cum[N] - cum[i];
            int ok = i+1;
            int ng = N-1;
            while(abs(ok - ng) > 1){
                int mid = (ok + ng) >> 1;
                ll tmp = cum[mid] - cum[i];
                if(2*tmp < sum)ok = mid;
                else ng = mid;
            }
            r[0] = ok;
        }
        {
            ll sum = cum[N] - cum[i];
            int ng = i+1;
            int ok = N-1;
            while(abs(ok - ng) > 1){
                int mid = (ok + ng) >> 1;
                ll tmp = cum[N] - cum[mid];
                if(2*tmp < sum)ok = mid;
                else ng = mid;
            }
            r[1] = ok;
        }
        rep(j,2)rep(k,2){
            chmin(ans, calc(l[j], i, r[k]));
        }
    }
    cout << ans << endl;
}


Measured memory_bytes: 6352