#include <bits/stdc++.h>

using namespace std;

#define int long long
#define FOR(i, a, b) for(ll i = (a); i < (b); ++i)
#define FORR(i, a, b) for(ll i = (a); i > (b); --i)
#define REP(i, n) for(ll i = 0; i < (n); ++i)
#define REPR(i, n) for(ll i = n; i >= 0; i--)
#define FOREACH(x, a) for(auto &(x) : (a))
#define VECCIN(x)                                                              \
    for(auto &youso_ : (x)) cin >> youso_
#define bitcnt __builtin_popcount
#define SZ(x) ((ll)(x).size())
#define fi first
#define se second
#define All(a) (a).begin(), (a).end()
template <typename T = long long> inline T IN() {
    T x;
    cin >> x;
    return (x);
}
inline void CIN() {}
template <class Head, class... Tail>
inline void CIN(Head &&head, Tail &&... tail) {
    cin >> head;
    CIN(move(tail)...);
}
#define CINT(...)                                                              \
    int __VA_ARGS__;                                                           \
    CIN(__VA_ARGS__)
#define LCIN(...)                                                              \
    ll __VA_ARGS__;                                                            \
    CIN(__VA_ARGS__)
#define SCIN(...)                                                              \
    string __VA_ARGS__;                                                        \
    CIN(__VA_ARGS__)
#define Yes(a) cout << (a ? "Yes" : "No") << "\n"
#define YES(a) cout << (a ? "YES" : "NO") << "\n"
#define Printv(v)                                                              \
    {                                                                          \
        FOREACH(x, v) { cout << x << " "; }                                    \
        cout << "\n";                                                          \
    }
template <typename T = string> inline void eputs(T s) {
    cout << s << "\n";
    exit(0);
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
    std::fill((T *)array, (T *)(array + N), val);
}

typedef long long ll;
typedef unsigned long long ul;
typedef vector<ll> VL;
typedef pair<ll, ll> PL;
typedef priority_queue<ll> PQ;
typedef priority_queue<ll, VL, greater<ll>> PQG;

const int INF = 1e9;
const int MOD = 1e9 + 7;
// const int MOD = 998244353;
const ll LINF = 1e18 + 1;
const ll dx[] = {1, -1, 0, 0};
const ll dy[] = {0, 0, 1, -1};

ll N;
VL sum;

signed main() {
    cin >> N;
    sum.resize(N + 1);
    REP(i, N) sum[i + 1] += sum[i] + IN();
    ll ans = LINF;
    REP(i, N + 1) {
        ll lb = 0, ub = i + 1;
        while(ub - lb > 1) {
            ll mid = (ub + lb) / 2;
            if(sum[mid] < sum[i] - sum[mid])
                lb = mid;
            else
                ub = mid;
        }
        ll p1 = sum[lb], q1 = sum[i] - sum[lb];
        ll p2 = sum[ub], q2 = sum[i] - sum[ub];
        ll lmin, lmax;
        if(abs(p1 - q1) > abs(p2 - q2))
            lmin = min(p2, q2), lmax = max(p2, q2);
        else
            lmin = min(p1, q1), lmax = max(p1, q1);

        lb = i, ub = N + 1;
        while(ub - lb > 1) {
            ll mid = (ub + lb) / 2;
            if(sum[mid] - sum[i] < sum[N] - sum[mid])
                lb = mid;
            else
                ub = mid;
        }
        ll r1 = sum[lb] - sum[i], s1 = sum[N] - sum[lb];
        ll r2 = sum[ub] - sum[i], s2 = sum[N] - sum[ub];
        ll rmin, rmax;
        if(abs(r1 - s1) > abs(r2 - s2))
            rmin = min(r2, s2), rmax = max(r2, s2);
        else
            rmin = min(r1, s1), rmax = max(r1, s1);

        ans = min(ans, max(lmax, rmax) - min(lmin, rmin));
    }
    cout << ans << "\n";
}


Measured memory_bytes: 1792