#include <bits/stdc++.h>

#define ll long long
#define str string
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second

#define vc vector<char>
#define vvc vector<vc>
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vi>
#define vvll vector<vll>
#define vvvll vector<vvll>
#define vs vector<str>
#define vvs vector<vs>
#define vpii vector<pii>
#define vvpii vector<vpii>
#define vpll vector<pll>
#define vvpll vector<vpll>
#define vb vector<bool>
#define vvb vector<vb>
#define rep(i, a, b) for (int i = (a); i < int(b); i++)
#define repi(i, a, b) for (int i = (a); i <= int(b); i++)


using namespace std;
ll INF = LONG_LONG_MAX;
ll mod = 1000000000 + 7;

template <typename T, typename L>
void readVector(vector<T> & _data, L & _size, bool _shift) {
    _data.resize(_size + (ll)_shift);
    for (ll i = (ll)_shift; i < _size + (ll)_shift; i++)
        cin >> _data[i];
}

template <typename T, typename L>
void readMatrix(vector<vector<T>> & _data, L & _rows, L & _cols, bool _shiftRows, bool _shiftCols) {
    _data.resize(_rows + (ll)_shiftRows);
    for (ll i = 0; i < _rows + (ll)_shiftRows; i++)
        _data[i].resize(_cols + (ll)_shiftCols);
    for (ll i = (ll)_shiftRows; i < _rows + (ll)_shiftRows; i++)
        for (ll j = (ll)_shiftCols; j < _cols + (ll)_shiftCols; j++)
            cin >> _data[i][j];
}


//TODO: SOLUTION

ll n;
vll a, pre, f, g;

void computeG() {
    ll j = n - 2;
    g[n - 1] = n;

    while (j >= 1) {
        int k = g[j + 1];
        while (abs(pre[n] - (2*pre[k - 1]) + pre[j - 1]) > abs(pre[n] - (2*pre[k - 2]) + pre[j - 1]))
            k--;
        g[j] = k;
        j--;
    }
}

void computeF() {
    ll j = 3;
    f[2] = 1;

    while (j <= n) {
        int k = f[j - 1];
        while (abs(pre[j] - (2*pre[k])) > abs(pre[j] - (2*pre[k + 1])))
            k++;
        f[j] = k;
        j++;
    }
}

void solve() {
    cin >> n;
    readVector(a, n, 1);
    pre.resize(n + 1, 0);

    repi(i, 1, n) {
        pre[i] = pre[i - 1] + a[i];
    }

    f.resize(n + 1);
    g.resize(n + 1);

    computeF();
    computeG();

    ll ans = INF;
    repi(i, 2, n - 2) {
        ans = min(ans, max(max(max(pre[f[i]], pre[i] - pre[f[i]]), pre[n] - pre[g[i + 1] - 1]), pre[g[i + 1] - 1] - pre[i])
                      -min(min(min(pre[f[i]], pre[i] - pre[f[i]]), pre[n] - pre[g[i + 1] - 1]), pre[g[i + 1] - 1] - pre[i]));
    }

    cout << ans << endl;
}


int main() {

    // TODO: Set value of this variable manually
    bool _multipleTestCases = false;

    if (_multipleTestCases) {
        ll t; cin >> t;
        while (t--)
            solve();
    }
    else {
        solve();
    }

    return 0;
}
#include <iostream>
#include <stdio.h>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cstring>
#include <algorithm>
#include <math.h>

using namespace std;

typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

#define FOR(i,n,m) for(int i=n; i<(int)m; i++)
#define ROF(i,n,m) for(int i=n; i>(int)m; i--)
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define F first
#define S second
#define ri(a) scanf("%d",&a)
#define rii(a,b) ri(a),ri(b)
#define riii(a,b,c) rii(a,b),ri(c)
#define lri(a) scanf("%lld",&a)
#define lrii(a,b) lri(a),lri(b)
#define ms(obj,val) memset(obj,val,sizeof(obj))
#define all(x) x.begin(),x.end()
#define debug true
#define dprintf debug && printf

const int INF = 0x3f3f3f3f;
const ll INFLL = 1e18;
const int MOD = 1e9+7;
const int MAXN = 2e5+5;

ll pref[MAXN];
int n;

ll sum(int l, int r){
    if(l > r) return 0;
    return pref[r] - (l == 0 ? 0 : pref[l-1]);
}

ll dif(int l, int r, int m){
    return abs(sum(l, m-1) - sum(m, r));
}

void mxze(ll &a, ll b){
    a = max(a,b);
}
void mnze(ll &a, ll b){
    a = min(a,b);
}

ll result(int m1, int m, int m2){
    ll mx = sum(0,m1-1);
    mxze(mx, sum(m1,m-1));
    mxze(mx, sum(m,m2-1));
    mxze(mx, sum(m2,n-1));

    ll mn = sum(0,m1-1);
    mnze(mn, sum(m1,m-1));
    mnze(mn, sum(m,m2-1));
    mnze(mn, sum(m2,n-1));

    return mx - mn;
}

bool check(int l, int r, int m){
    return dif(l, r, m) > dif(l,r,m+1);
}

int findBest(int l, int r){
    int lo = l + 1, hi = r;
    while(lo < hi){
        int mid = (lo + hi)>>1;
        if(check(l, r, mid)) lo = mid + 1;
        else hi = mid;
    }
    return hi;
}

int main(){
    //ios_base::sync_with_stdio(false);
    //cin.tie(NULL);

    ri(n);
    FOR(i,0,n) lri(pref[i]);
    FOR(i,1,n) pref[i] += pref[i-1];

    ll ans = INFLL;
    FOR(m,2,n-1){
        mnze(ans, result(findBest(0,m-1), m, findBest(m, n-1)));
    }

    printf("%lld\n", ans);

    return 0;
}
