#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define dump(...)
#endif
#define endl '\n'
#define ll long long
#define int long long
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define INF 2147483647
#define LLINF 9223372036854775807LL
#define vi vector<int>
#define vvi vector<vector<int>>
#define pi pair<int, int>
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define BIT(n) (1LL << (n))
#define TOUPPER(s) transform(s.begin(), s.end(), s.begin(), ::toupper)
#define TOLOWER(s) transform(s.begin(), s.end(), s.begin(), ::tolower)
#define FILLZERO(s, n)                      \
    do {                                    \
        ostringstream os;                   \
        os << setw(n) << setfill('0') << s; \
        s = os.str();                       \
    } while (0);
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()) //sortしてからつかうこと
#define INIT                               \
    std::cin.tie(nullptr);                 \
    std::ios_base::sync_with_stdio(false); \
    cout << std::fixed << std::setprecision(10);

template <typename T>
vector<T> make_vec(size_t a, T n) { return vector<T>(a, n); }
template <typename T, typename... Ts>
auto make_vec(size_t a, Ts... ts) { return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...)); }
template <class T>
inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &vec) {
    for (T &x : vec)
        is >> x;
    return is;
}

constexpr ll MOD = 1e9 + 7;
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
ll A, B, C, D, E, F, G, H, N, M, L, K, P, Q, R, W, X, Y, Z;
string S, T;
ll ans = 0;

signed main() {
    INIT;
    cin >> N;
    vector<ll> a(N);
    cin >> a;
    vector<ll> sum(N);
    sum[0] = a[0];
    REP(i, N - 1) {
        sum[i + 1] = sum[i] + a[i + 1];
    }
    dump(sum);
    ans = LLINF;
    FOR(i, 1, N - 1) {
        vector<vector<ll>> x(2, vector<ll>(2)), y(2, vector<ll>(2));
        int pq = lower_bound(sum.begin(), sum.begin() + i, (sum[i] + 1) / 2) - sum.begin();
        x[0][0] = sum[pq];
        x[0][1] = sum[i] - sum[pq];
        x[1][0] = (pq != 0) ? sum[pq - 1] : x[0][0];
        x[1][1] = (pq != 0) ? sum[i] - sum[pq - 1] : x[0][1];
        int rs = lower_bound(sum.begin() + i + 1, sum.end(), (sum[N - 1] - sum[i] + 1) / 2 + sum[i]) - sum.begin();
        y[0][0] = sum[rs] - sum[i];
        y[0][1] = sum[N - 1] - sum[rs];
        y[1][0] = (rs != i + 1) ? sum[rs - 1] - sum[i] : y[0][0];
        y[1][1] = (rs != i + 1) ? sum[N - 1] - sum[rs - 1] : y[0][1];
        dump(pq, rs, x, y);
        REP(bit, 4) {
            int pqmx = max(x[bit & 1][0], x[bit & 1][1]);
            int pqmn = min(x[bit & 1][0], x[bit & 1][1]);
            int rsmx = max(y[bit >> 1 & 1][0], y[bit >> 1 & 1][1]);
            int rsmn = min(y[bit >> 1 & 1][0], y[bit >> 1 & 1][1]);
            chmin(ans, (ll)max(pqmx, rsmx) - min(pqmn, rsmn));
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 6380