#include <bits/stdc++.h>
#define FI first
#define SE second
#define EPS 1e-9
#define ALL(a) a.begin(),a.end()
#define SZ(a) int((a).size())
#define MS(s, n) memset(s, n, sizeof(s))
#define FOR(i,a,b) for (int i = (a); i <= (b); i++)
#define FORE(i,a,b) for (int i = (a); i >= (b); i--)
#define FORALL(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
#define WHATIS(x) cout << #x << " is " << x << endl;
#define ERROR(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
//__builtin_ffs(x) return 1 + index of least significant 1-bit of x
//__builtin_clz(x) return number of leading zeros of x
//__builtin_ctz(x) return number of trailing zeros of x

using namespace std;
using ll = long long;
using ld = double;
typedef pair<int, int> II;
typedef pair<II, ll> III;
typedef complex<ld> cd;
typedef vector<cd> vcd;

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cout << *it << " = " << a << endl;
    err(++it, args...);
}

const ll MODBASE = 1000000007LL;
const int MAXN = 200010;
const int MAXM = 100010;
const int MAXK = 510;
const int MAXQ = 200010;

int a[MAXN], n;
ll l[MAXN], r[MAXN];

int main() {
    ios::sync_with_stdio(0);
    cin.tie(nullptr);
    cin >> n;
    FOR(i,1,n) cin >> a[i];
    FOR(i,1,n) l[i] = l[i-1] + a[i];
    FORE(i,n,1) r[i] = r[i+1] + a[i];
    int lefNotch = 1, rigNotch = 3;
    ll res = 1e18;
    FOR(i,2,n-2) {
        ll sumLef = l[i];
        ll sumRig = r[i+1];

        ll sumLefLef = l[lefNotch];
        ll sumLefRig = sumLef - sumLefLef;
        ll diffLef = sumLefLef - sumLefRig;
        while (lefNotch + 1 < i) {
            int newLefNotch = lefNotch + 1;

            ll newSumLL = l[newLefNotch];
            ll newSumLR = sumLef - newSumLL;
            ll newDiffLef = newSumLL - newSumLR;

            if (abs(diffLef) <= abs(newDiffLef)) break;

            diffLef = newDiffLef;
            lefNotch = newLefNotch;
        }

        rigNotch = max(rigNotch, i+1);
        ll sumRigRig = r[rigNotch+1];
        ll sumRigLef = sumRig - sumRigRig;
        // cout << sumRigLef << ' ' << sumRigRig << endl;
        ll diffRig = sumRigLef - sumRigRig;
        while (rigNotch + 1 < n) {
            int newRigNotch = rigNotch + 1;

            ll newSumRR = r[newRigNotch+1];
            ll newSumRL = sumRig - newSumRR;
            ll newDiffRig = newSumRL - newSumRR;

            // cout << newSumRL << ' ' << newSumRR << endl;

            if (abs(diffRig) <= abs(newDiffRig)) break;

            diffRig = newDiffRig;
            rigNotch = newRigNotch;
        }

        sumLefLef = l[lefNotch];
        sumLefRig = sumLef - sumLefLef;
        sumRigRig = r[rigNotch+1];
        sumRigLef = sumRig - sumRigRig;
        // cout << i << ' ' << sumLefLef << ' ' << sumLefRig << ' ' << sumRigLef << ' ' << sumRigRig << "\n";
        ll Min = min(sumLefLef, min(sumLefRig, min(sumRigRig, sumRigLef)));
        ll Max = max(sumLefLef, max(sumLefRig, max(sumRigRig, sumRigLef)));
        res = min(res, Max - Min);
    }
    cout << res;
    return 0;
}

Measured memory_bytes: 4224