#include "bits/stdc++.h"
// Begin Header {{{
#define all(x)          (x).begin(), (x).end()
#define rep(i, n)       for (i64 i = 0, i##_limit = (n); i < i##_limit; ++i)
#define reps(i, s, t)   for (i64 i = (s), i##_limit = (t); i <= i##_limit; ++i)
#define repr(i, s, t)   for (i64 i = (s), i##_limit = (t); i >= i##_limit; --i)
#ifndef DBG
#define trace(...)
#endif
using namespace std;
using i64 = int_fast64_t;
using pii = pair<i64, i64>;
template <class T, class U> inline bool chmax(T &a, const U &b) { return b > a && (a = b, true); }
template <class T, class U> inline bool chmin(T &a, const U &b) { return b < a && (a = b, true); }
constexpr int INF  = 0x3f3f3f3f;
constexpr i64 LINF = 0x3f3f3f3f3f3f3f3fLL;

template <class T>
inline vector<T> make_v(const T &initValue, size_t sz) {
    return vector<T>(sz, initValue);
}

template <class T, class... Args>
inline auto make_v(const T &initValue, size_t sz, Args... args) {
    return vector<decltype(make_v<T>(initValue, args...))>(sz, make_v<T>(initValue, args...));
}

inline void read() {}

template <class Head, class... Tail>
inline void read(Head &head, Tail&... tail) { cin >> head; read(tail...); }

inline void print() { cout << "\n"; }

template <class Head, class... Tail>
inline void print(Head &&head, Tail&&... tail) {
    cout << head;
    if (sizeof...(tail)) cout << ' ';
    print(forward<Tail>(tail)...);
}

template <class T>
inline ostream& operator<< (ostream &out, const vector<T> &vec) {
    static constexpr const char *delim[] = { " ", "" };
    for (const auto &e : vec) out << e << delim[&e == &vec.back()];
    return out;
}
// }}} End Header

class CumulativeSum { // {{{{
    vector<i64> cum;

public:
    CumulativeSum() : cum(1) {}

    CumulativeSum(int sz) : cum(sz + 1) {}

    template<class T>
        CumulativeSum(initializer_list<T> il) { assign(il.begin(), il.end()); }

    template<class Iterator>
        CumulativeSum(Iterator begin, Iterator end) { assign(begin, end); }

    template<class Iterator>
        inline void assign(Iterator begin, Iterator end) {
            cum.assign(distance(begin, end) + 1, 0);
            std::copy(begin, end, cum.begin() + 1);
        }

    inline void build() {
        for (int i = 1; i < cum.size(); ++i) {
            cum[i] += cum[i-1];
        }
    }

    // "半開区間" [0, r) までの総和
    inline i64 sum(int r) const {
        return cum[r];
    }

    // "半開区間" [l, r) までの総和
    inline i64 sum(int l, int r) const {
        return sum(r) - sum(l);
    }

    ///////////////////////////////////////////////////////////////////////////
    // vector like methods
    inline void resize(int sz) {
        cum.resize(sz + 1);
    }
    template<class... Args>
        inline void emplace_back(Args&&... args) {
            cum.emplace_back(args...);
        }
    inline i64 size() const noexcept {
        return static_cast<i64>(cum.size()) - 1;
    }
    inline const auto& data() const noexcept {
        return cum;
    }
    inline auto begin() const noexcept {
        return cum.begin() + 1;
    }
    inline auto end() const noexcept {
        return cum.end();
    }
    inline i64 operator[](int i) const {
        return cum[i + 1];
    }
    inline i64& operator[](int i) {
        return cum[i + 1];
    }
};
// }}}

signed main() {
    ios_base::sync_with_stdio(false); cin.tie(nullptr);

    int N;
    read(N);

    vector<i64> a(N);
    CumulativeSum cum(N);

    rep(i, N) {
        read(a[i]);
        cum[i] = a[i];
    }

    cum.build();
    trace(cum.sum(0, N));

    i64 ans = LINF;
    reps(mid, 1, N-2) {
        const i64 lmidsum = cum.sum(0, mid) / 2;
        const i64 rmidsum = cum.sum(0, mid) + cum.sum(mid, N) / 2;
        const int aa = lower_bound(all(cum), lmidsum) - begin(cum);
        const int cc = lower_bound(all(cum), rmidsum) - begin(cum);
        trace(aa, mid, cc);

        reps(i, max(0, aa-6), min(N-1, aa+6)) {
            reps(k, max(0, cc-6), min(N-1, cc+6)) {
                if (i >= mid || mid >= k) continue;
                const i64 a = cum.sum(0, i);
                const i64 b = cum.sum(i, mid);
                const i64 c = cum.sum(mid, k);
                const i64 d = cum.sum(k, N);
                const pii mm = minmax({ a, b, c, d });
                chmin(ans, abs(mm.first - mm.second));
            }
        }
    }

    print(ans);
    return 0;
}


Measured memory_bytes: 3328