#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define int ll

#define REP(i,n) for(int i=0;i<n;++i)
#define SORT(name) sort(name.begin(), name.end())
#define ZERO(p) memset(p, 0, sizeof(p))
#define MINUS(p) memset(p, -1, sizeof(p))
#if 0
#  define DBG(fmt, ...) printf(fmt, ##__VA_ARGS__)
#else
#  define DBG(fmt, ...)
#endif

const ll LLINF = (1LL<<60);
const int INF = (1LL<<30);
const int MOD = 1000000007;
#define MAX_N 200010

template< typename T >
class CumulativeSum
{
public:
    vector< T > m_data;
    CumulativeSum() {};
    CumulativeSum(int sz) : m_data(sz, 0) {};

    // k に x を加える O(1)
    void Add(int k, int x) {
        assert(m_data.size() >= k);
        if(m_data.size() <= k) { m_data.push_back(x); }
        else { m_data[k] += x; }
    }

    // 累積和の構築 O(m_data.size())
    void Build() { for(int i = 1; i < m_data.size(); i++) { m_data[i] += m_data[i - 1]; } }

    // 区間 [0, k] の和を返す O(1)
    // 閉区間なので k が含まれることに注意
    T Query(int k) {
        if(k < 0) { return 0; }
        return (m_data[min(k, (int) m_data.size() - 1)]);
    }

    // 区間 [l, h] の和を返す O(1)
    // 閉区間なので k が含まれることに注意
    T Query(int l, int h) { return this->Query(h) - this->Query(l-1); }
};


ll N;
ll A[MAX_N] = {};

signed main()
{
    CumulativeSum<ll> cs;

    cin >> N;
    REP(i, N) {
        cin >> A[i];
        cs.Add(i, A[i]);
    }
    cs.Build();

    ll ans = LLINF;
    for(int i = 1; i < N-2; ++i) {
        // 左部分が同じくらいの数になるようにぶたん
        ll lb = 0;
        ll ub = i;
        ll prev = -1;
        ll border1, border2;
        while(ub != lb) {
            ll mid = (ub - lb) / 2 + lb;
            border1 = mid;
            if(mid == prev) { break; }
            ll cand1 = LLINF, cand2 = LLINF, cand3 = LLINF;
            if(mid - 1 >= 0) {
                cand1 = abs(cs.Query(0, mid - 1) - cs.Query(mid, i));
            }
            if(mid + 1 <= i) {
                cand2 = abs(cs.Query(0, mid) - cs.Query(mid + 1, i));
            }
            if(mid + 2 <= i) {
                cand3 = abs(cs.Query(0, mid + 1) - cs.Query(mid + 2, i));
            }
            if(cand2 <= cand1 && cand2 <= cand3) {
                break;
            }
            else if(cand1 <= cand2 && cand1 <= cand3) {
                ub = mid;
            }
            else if(cand3 <= cand2 && cand3 <= cand1) {
                lb = mid;
            }
            prev = mid;
        }
        // 右部分が同じくらいの数になるようにぶたん
        prev = -1;
        lb = i + 1;
        ub = N - 1;
        while(ub != lb) {
            ll mid = (ub - lb) / 2 + lb;
            border2 = mid;
            if(mid == prev) { break; }
            ll cand1 = LLINF, cand2 = LLINF, cand3 = LLINF;
            if(mid - 1 >= i + 1) {
                cand1 = abs(cs.Query(i + 1, mid - 1) - cs.Query(mid, N - 1));
            }
            if(mid + 1 <= N - 1) {
                cand2 = abs(cs.Query(i + 1, mid) - cs.Query(mid + 1, N - 1));
            }
            if(mid + 2 <= N - 1) {
                cand3 = abs(cs.Query(i + 1, mid + 1) - cs.Query(mid + 2, N - 1));
            }
            if(cand2 <= cand1 && cand2 <= cand3) {
                break;
            }
            else if(cand1 <= cand2 && cand1 <= cand3) {
                ub = mid;
            }
            else if(cand3 <= cand2 && cand3 <= cand1) {
                lb = mid;
            }
            prev = mid;
        }
        ll P = cs.Query(0, border1);
        ll Q = cs.Query(border1 + 1, i);
        ll R = cs.Query(i + 1, border2);
        ll S = cs.Query(border2 + 1, N - 1);
        DBG("[%lld, %lld]] %lld\n", 0, border1, P);
        DBG("[%lld, %lld]] %lld\n", border1 + 1, i, Q);
        DBG("[%lld, %lld]] %lld\n", i + 1, border2, R);
        DBG("[%lld, %lld]] %lld\n", border2 + 1, N - 1, S);
        ll max_a = max({P, Q, R, S});
        ll min_a = min({P, Q, R, S});
        DBG("max: %lld min: %lld diff: %lld\n", max_a, min_a, abs(max_a - min_a));
        ans = min(ans, abs(max_a - min_a));
    }

    printf("%lld\n", ans);
    return 0;
}


Measured memory_bytes: 3568