#include<iostream>
#include<iomanip>
#include<cstdio>

#include<vector>
#include<queue>
#include<set>
#include<map>
#include<string>

#include<algorithm>
#include<cmath>
#include<numeric>

using namespace std;
typedef long long ll;
template <class T> inline bool chmax(T& a, T b) {if (a < b) {a = b; return 1;} return 0;}
template <class T> inline bool chmin(T& a, T b) {if (a > b) {a = b; return 1;} return 0;}
const ll INF = 1e18+1;

int main() {
    int N; cin >> N;
    vector<ll> A(N);
    for (int i = 0; i < N; i++) cin >> A[i];
    vector<ll> S(N,0), F(N,-1), G(N,-1);
    S[0] = A[0];
    for (int i = 1; i < N; i++) S[i] = S[i-1] + A[i];
    //S.insert(S.begin(),0);

    int j = 0;
    for (int i = 0; i < N; i++) {
        ll sum = S[i];
        while (j+1 < i && abs(sum-S[j+1] - S[j+1]) <= abs(sum-S[j] - S[j])) {
            j++;
        }
        F[i] = j;
    }
    j = N-1;
    for (int i = N-1; i >= 0; i--) {
        ll sum = S[N-1] - S[i];
        while (j-1 > i && abs(sum-2*(S[N-1] - S[j-1])) <= abs(sum-2*(S[N-1] - S[j]))) {
            j--;
        }
        G[i] = j;
    }
    ll res = INF;
    for (int i = 0; i < N; i++) {
        ll tmpmax = -1, tmpmin = INF;
        chmax(tmpmax, S[F[i]]); chmax(tmpmax, S[i] - S[F[i]]); chmax(tmpmax, S[G[i]] - S[i]); chmax(tmpmax, S[N-1] - S[G[i]]);
        chmin(tmpmin, S[F[i]]); chmin(tmpmin, S[i] - S[F[i]]); chmin(tmpmin, S[G[i]] - S[i]); chmin(tmpmin, S[N-1] - S[G[i]]);
        chmin(res, tmpmax-tmpmin);
    }
    cout << res << endl;

}



/*

最大値と最小値の差の絶対値x以下は実現できるか?でにぶたん

とりまAの前からと後ろからの累積和をとる

列Bを前から取っていく この和+x以下でにぶたん +x以下でにぶたん +残り これらが実際x以下ならOK  
列Bを前から取っていく この和-x以上でにぶたん +x以下でにぶたん これらが実際x以下ならOK
列Bを前から取っていく この和+x以下でにぶたん -x以上でにぶたん これらが実際x以下ならOK
列Bを前から取っていく この和-x以上でにぶたん -x以上でにぶたん これらが実際x以下ならOK
*/
/*
bool f(ll x, vector<ll> S) {
    ll N = S.size(), B, C, D, E, M = S[N-2];
    for (int i = 1; i < N-4; i++) {
        B = S[i];

        auto cd = lower_bound(S.begin(),S.end(),max(B+B-x, B+1));
        auto de = lower_bound(S.begin(),S.end(),max(*cd+max(B,*cd-B)-x, *cd+1));
        //cout << *cd << " " << *de << endl;
        //cout << abs(M-*de-B) << endl;
        E = M - *de; D = *de - *cd; C = *cd - B;
        if (abs(E - B) <= x && 
            abs(E - C) <= x && 
            abs(E - D) <= x && 
            abs(D - C) <= x &&
            abs(D - B) <= x && 
            abs(C - B) <= x) return true;

        cd = upper_bound(S.begin(),S.end(),B+B+x)-1;
        de = upper_bound(S.begin(),S.end(),*cd+min(B,*cd-B)+x)-1;
        E = M - *de; D = *de - *cd; C = *cd - B;
        if (abs(E - B) <= x && 
            abs(E - C) <= x && 
            abs(E - D) <= x && 
            abs(D - C) <= x &&
            abs(D - B) <= x && 
            abs(C - B) <= x) return true;

        cd = lower_bound(S.begin(),S.end(),max(B+B-x,B+1));
        de = upper_bound(S.begin(),S.end(),*cd+*cd-B+x)-1;
        E = M - *de; D = *de - *cd; C = *cd - B;
        if (abs(E - B) <= x && 
            abs(E - C) <= x && 
            abs(E - D) <= x && 
            abs(D - C) <= x &&
            abs(D - B) <= x && 
            abs(C - B) <= x) return true;
            
        cd = upper_bound(S.begin(),S.end(),B+B+x)-1;
        de = lower_bound(S.begin(),S.end(),max(*cd+*cd-B-x, *cd+1));
        E = M - *de; D = *de - *cd; C = *cd - B;
        if (abs(E - B) <= x && 
            abs(E - C) <= x && 
            abs(E - D) <= x && 
            abs(D - C) <= x &&
            abs(D - B) <= x && 
            abs(C - B) <= x) return true;
    }
    return false;
}

int main() {
    int N; cin >> N;
    vector<ll> A(N);
    for (int i = 0; i < N; i++) cin >> A[i];
    vector<ll> S(N+2,0);
    S[0] = 0; S[1] = A[0]; S[N+1] = INF;
    for (int i = 1; i < N; i++) S[i+1] = S[i] + A[i];

    ll lb = -1, rb = 1e15+1;
    while (rb - lb > 1) {
        ll mid = (rb+lb)/2;
        if (f(mid,S)) rb = mid;
        else lb = mid;
        //cout << lb << " " << rb << endl;
    }
    cout << rb << endl;
}*/

Measured memory_bytes: 6528