#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

#include <limits.h>

using namespace std;

typedef long long ll;

template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

const ll INF = LONG_LONG_MAX / 2;

int N;
vector<ll> as;

vector<ll> ss;

int binary_search(int init_left, int init_right) {
    ll s = ss[init_right] - ss[init_left];
    int left = init_left, right = init_right, mid;
    while (right - left > 1) {
        mid = left + (right - left) / 2;
        if ((ss[mid] - ss[init_left]) * 2 < s) {
            left = mid;
        } else {
            right = mid;
        }
    }
    return right;
}

ll solve(const int N) {
    ll ans = INF;

    for (int center = 2; center < N - 1; center++) {
        ll mn = INF;
        ll mx = 0;
        {
            // left
            // ll s = ss[center] / 2;
            // auto it = upper_bound(ss.begin(), ss.end(), s);
            // it--;
            // int left = distance(ss.begin(), it);
            int l = binary_search(0, center);
            vector<int> ls = {l, l - 1};
            pair<ll, ll> res = {0, INF};
            for (auto left : ls) {
                ll s_first = ss[left] - ss[0];
                ll s_second = ss[center] - ss[left];
                // printf("left: %d, center: %d, [0, left): %lld, [left, center): %lld\n",
                //         left, center, s_first, s_second);
                if (abs(s_first - s_second) < abs(res.first - res.second)) {
                    res = {s_first, s_second};
                }
            }
            chmin(mn, res.first);
            chmax(mx, res.first);
            chmin(mn, res.second);
            chmax(mx, res.second);
        }
        {
            // right
            // ll s = (ss[N] - ss[center]) / 2;
            // printf("ss[center]: %lld, s: %lld\n", ss[center], s);
            // auto it = upper_bound(ss.begin(), ss.end(), ss[center] + s);
            // it--;
            int r = binary_search(center, N);
            vector<int> rs = {r, r - 1};
            pair<ll, ll> res = {0, INF};
            for (auto right : rs) {
                ll s_first = ss[right] - ss[center];
                ll s_second = ss[N] - ss[right];
                // printf("center: %d, right: %d, [center, right): %lld, [right, N): %lld\n",
                //         center, right, s_first, s_second);
                if (abs(s_first - s_second) < abs(res.first - res.second)) {
                    res = {s_first, s_second};
                }
            }
            chmin(mn, res.first);
            chmax(mx, res.first);
            chmin(mn, res.second);
            chmax(mx, res.second);
        }

        chmin(ans, mx - mn);
    }

    return ans;
}

int main(void) {
    cin >> N;

    as.resize(N);
    for (int i = 0; i < N; i++) {
        cin >> as[i];
    }

    ss.resize(N + 1, 0);
    for (int i = 0; i < N; i++) {
        ss[i + 1] = ss[i] + as[i];
    }

    cout << solve(N) << endl;

    return 0;
}


Measured memory_bytes: 3456