#include <bits/stdc++.h>
#define fst first
#define snd second
#define rep(n) for(lint I = 0; (I) < (lint)(n); ++(I))
#define repeat(i, n) for(lint i = 0; (i) < (lint)(n); ++(i))
#define repeat_to(i, n) for(lint i = 0; (i) <= (lint)(n); ++(i))
#define repeat_from(i, m, n) for(lint i = (m); (i) < (lint)(n); ++(i))
#define repeat_from_to(i, m, n) for(lint i = (m); (i) <= (lint)(n); ++(i))
#define repeat_reverse_from_to(i, m, n) for(lint i = (m); (i) >= (lint)(n); --(i))
#define el cout<<endl
#define dump(x) cout<<" "<<#x<<"="<<x
#define vdump(v) for(size_t I=0; I<v.size(); ++I){cout<<" "<<#v<<"["<<I<<"]="<<v[I];} cout<<endl
using namespace std;
using lint = long long;
using ld = long double;

int main(void) {
    lint n;
    cin >> n;
    vector<lint> a(n + 1);
    a[0] = 0;
    repeat (i, n) cin >> a[i + 1];
    
    vector<lint> s(n + 1);
    s[0] = 0;
    repeat (i, n) s[i+1] = s[i] + a[i+1];
    
    auto f = [&s](lint lb, lint rb, lint p) -> lint {
        if (p < lb)  return -(s[rb] - s[lb-1]);
        if (rb <= p) return +(s[rb] - s[lb-1]);
        lint x = s[p] - s[lb-1];
        lint y = s[rb] - s[p];
        return x - y;
    };
    
    
    auto negmax_search = [&s, &f](lint lb, lint rb) -> pair<lint, lint> {
        lint lpos = lb - 1;
        lint rpos = rb;
        assert (f(lb, rb, lpos) < 0 and f(lb, rb, rpos) > 0);
        lint mpos = (lpos + rpos) / 2;
        rep (100) {
            if (f(lb, rb, mpos) < 0) lpos = mpos;
            else rpos = mpos;
            mpos = (lpos + rpos) / 2;
            //dump(lpos); dump(mpos); dump(rpos); el;
        }
        lint x = s[mpos] - s[lb-1];
        lint y = s[rb] - s[mpos];
        assert (f(lb, rb, mpos) == x - y);
        if (x == 0) {
            x = s[mpos+1] -s[lb-1];
            y = s[rb] - s[mpos+1];
        }
        return make_pair(x, y);
    };
    
    auto posmin_search = [&s, &f, &negmax_search](lint lb, lint rb) -> pair<lint, lint> {
        lint lpos = lb - 1;
        lint rpos = rb;
        assert (f(lb, rb, lpos) < 0 and f(lb, rb, rpos) > 0);
        lint mpos = (lpos + rpos) / 2;
        rep (100) {
            if (f(lb, rb, mpos) <= 0) lpos = mpos;
            else rpos = mpos;
            mpos = (lpos + rpos) / 2;
            //dump(lpos); dump(mpos); dump(rpos); el;
        }
        lint x = s[mpos+1] - s[lb-1];
        lint y = s[rb] - s[mpos+1];
        assert (f(lb, rb, mpos+1) == x - y);
        if (y == 0) {
            x = s[mpos] - s[lb-1];
            y = s[rb] - s[mpos];
        }
        return make_pair(x, y);
    };

    lint ans = 1e18;
    //1
    repeat_from_to (p, 2, n-2) {
        {
            vector<lint> x(4);
            tie(x[0], x[1]) = negmax_search(1, p);
            tie(x[2], x[3]) = negmax_search(p+1, n);
            //vdump(x);
            sort(x.begin(), x.end());
            ans = min(ans, x[3] - x[0]);
        }
    }
    //2
    repeat_from_to (p, 2, n-2) {
        {
            vector<lint> x(4);
            tie(x[0], x[1]) = posmin_search(1, p);
            tie(x[2], x[3]) = negmax_search(p+1, n);
            //vdump(x);
            sort(x.begin(), x.end());
            ans = min(ans, x[3] - x[0]);
        }
    }
    //3
    repeat_from_to (p, 2, n-2) {
        {
            vector<lint> x(4);
            tie(x[0], x[1]) = negmax_search(1, p);
            tie(x[2], x[3]) = posmin_search(p+1, n);
            //vdump(x);
            sort(x.begin(), x.end());
            ans = min(ans, x[3] - x[0]);
        }
    }
    //4
    repeat_from_to (p, 2, n-2) {
        {
            vector<lint> x(4);
            tie(x[0], x[1]) = posmin_search(1, p);
            tie(x[2], x[3]) = posmin_search(p+1, n);
            //vdump(x);
            sort(x.begin(), x.end());
            ans = min(ans, x[3] - x[0]);
        }
    }
    cout << ans << endl;
        
}

Measured memory_bytes: 3328