#include <bits/stdc++.h>
#ifdef __LOCAL
    #define DBG(X) cout << #X << " = " << (X) << endl;
    #define SAY(X) cout << (X) << endl;
#else
    #define DBG(X)
    #define SAY(X)
#endif

#ifdef __LOCAL
    #include <filesystem>
    namespace fs = std::filesystem;
#endif
#define rep(i,n) for (int (i) = 0; (i) < (n); (i)++)

using namespace std;
using ll = long long int;
static mt19937 _g(time(nullptr));
 
inline ll randint(ll a, ll b) { ll w = (_g() << 31LL) ^ _g(); return a + w % (b - a + 1); }
inline void fast_io() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); };
template<typename T, typename S> inline ostream& operator<<(ostream& os, const pair<T, S> p) { cout << "[" << p.first << ";" << p.second << "]"; return os; }
template<typename T, typename S> inline ostream& operator<<(ostream& os, const map<T, S> p) { for (auto el : p) cout << "[" << el.first << ";" << el.second << "]"; return os; }
template<typename T> inline ostream& operator<<(ostream& os, const vector<T>& v) { for (auto el : v) cout << el << " "; return os; }
template<typename T> inline vector<T> fetch_vec(int sz) { vector<T> ret(sz); for (auto& elem : ret) cin >> elem; return ret; }

ll N;
vector<ll> A,AS;
void input(){
    fast_io();
    #ifdef __LOCAL
        fs::path p = __FILE__;
        fs::path input,output;
        input = output = p.parent_path();
        input += string("/input/") + string(p.stem()) + string(".txt");
        output += string("/output/") + string(p.stem()) + string(".txt");
        freopen(input.c_str(), "r", stdin);
        freopen(output.c_str(), "w", stdout);
    #endif
    cin >> N;
    A = fetch_vec<ll>(N);
}

ll calc(int left, int mid, int right){
    ll P,Q,R,S;
    P = AS[left];
    Q = AS[mid]-AS[left];
    R = AS[right]-AS[mid];
    S = AS[N-1]-AS[right];
    // ll ret = max({P,Q,R,S})-min({P,Q,R,S});
    // return ret;
    return max({P,Q,R,S})-min({P,Q,R,S});
}

bool CL(int left, int mid){
    ll bf = abs(AS[mid]-AS[left]-AS[left]);
    ll nt = abs(AS[mid]-AS[left+1]-AS[left+1]);
    return (bf>nt);
}

bool CR(int mid, int right){
    if((right+1)>=N) return false;
    ll bf = abs(AS[N-1]-AS[right]-(AS[right]-AS[mid]));
    ll nt = abs(AS[N-1]-AS[right+1]-(AS[right+1]-AS[mid]));
    return (bf>nt);
}

// しゃくとり法的な。
// しゃくとり法を用いるのはいいが、固定する場所を工夫する必要がある。
// 真ん中の位置を固定したときに左の切り方と右の切り方はmidの単調増加なので
// いいいかんじに求める。
int solve(){
    AS.assign(N,0LL);
    AS[0] = A[0];
    rep(i,N-1) AS[i+1] = AS[i]+A[i+1];
    ll ans=AS[N-1];
    ll r;
    int left=0,right=2;
    // vector<int> L,R;
    for (int mid = 1; mid < N-2; mid++)
    {
        if(left>=mid) left = 0;
        if(right<=mid) right = mid+1;
        while(CL(left,mid) && (left+1)<mid) {left++;}
        while(CR(mid,right) && (right+1)<N) {right++;}
        // L.push_back(left);
        // R.push_back(right);
        r = calc(left,mid,right);
        // printf("0-%d %d %d-%d:%lld\n",left,mid,right,N,r);
        ans = min(ans,r);
    }
    // DBG(L)
    // DBG(R)
    cout << ans << endl;
    return 0;
}   

void maketestcase(){
    fast_io();
    #ifdef __LOCAL
        fs::path p = __FILE__;
        fs::path input,output;
        input = output = p.parent_path();
        input += string("/input/") + string(p.stem()) + string(".txt");
        // output += string("/output/") + string(p.stem()) + string(".txt");
        // freopen(input.c_str(), "r", stdin);
        freopen(input.c_str(), "w", stdout);
    #endif

    int N = 1e5;
    vector<ll> A;
    for (int i = 0; i < N; i++)
    {
        ll a = randint(0,1e9);
        A.push_back(a);
    }
    cout << N << endl;
    cout << A << endl;
    
}

int main()
{
    input();
    solve();
    // maketestcase();
    return 0;
}


Measured memory_bytes: 3456