#include <bits/stdc++.h>
using namespace std;

using int64 = long long;
using uint64 = unsigned long long;

#define repeat(i, x) for (int64 i = 0; (i) < (x); (i)++)
#define rev_repeat(i, x) for (int64 i = (x) - 1; (i) >= 0; (i)--)
#define countup(i, n) for (int64 i = 0; (i) <= (n); (i)++)
#define countdown(i, n) for (int64 i = (n); (i) >= 0; (i)--)
#define range(i, a, b) for (int64 i = (a); (i) < (b); (i)++)
#define traverse(it, ctn) for (auto it = begin(ctn); (it) != end(ctn); (it)++)
#define rev_traverse(it, ctn) for (auto it = rbegin(ctn); (it) != rend(ctn); (it)++)

template<typename T>
ostream& operator<<(ostream& os, vector<T>& vec)
{
    os << "[";
    repeat (i, vec.size())
    {
        os << (i == 0 ? "" : ", ") << vec[i];
    }
    os << "]";
    return os;
}

template<typename T, typename U>
ostream& operator<<(ostream& os, pair<T, U>& p)
{
    os << "(" << p.first << ", " << p.second << ")";
    return os;
}

template<typename T>
ostream& operator<<(ostream& os, set<T>& s)
{
    os << "{";
    traverse (it, s)
    {
        if (it != s.begin()) os << ", ";
        os << *it;
    }
    os << "}";
    return os;
}

template<typename K, typename V>
ostream& operator<<(ostream& os, map<K, V>& m) 
{
    os << "{";
    traverse (it, m)
    {
        if (it != m.begin()) os << ", ";
        os << it->first << ": " << it->second;
    }
    os << "}";
    return os;
}

//
// _______  _____  _______  _____  _______ _______ _____ _______ _____ _    _ _______ 
// |       |     | |  |  | |_____] |______    |      |      |      |    \  /  |______ 
// |_____  |_____| |  |  | |       |______    |    __|__    |    __|__   \/   |______ 
//
//  _____   ______  _____   ______  ______ _______ _______ _______ _____ __   _  ______   /
// |_____] |_____/ |     | |  ____ |_____/ |_____| |  |  | |  |  |   |   | \  | |  ____  / 
// |       |    \_ |_____| |_____| |    \_ |     | |  |  | |  |  | __|__ |  \_| |_____| .  
//

const int MAX_N = 200005;

int64 A[MAX_N], cumA[MAX_N];

int main()
{
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int N;
    cin >> N;

    repeat (i, N)
    {
        cin >> A[i];
        cumA[i] = A[i];

        if (i > 0)
        {
            cumA[i] += cumA[i - 1];
        }
    }

    repeat (i, N)
    {
        cerr << cumA[i] << " ";
    }
    cerr << endl;

    int64 ans = (1LL << 60);

    range (anchor, 2, N - 1)
    {
        int mid1 = lower_bound(cumA, cumA + anchor, cumA[anchor - 1] / 2) - cumA,
            mid2 = lower_bound(cumA + anchor, cumA + N, (cumA[anchor - 1] + cumA[N - 1]) / 2) - cumA;
        
        // cumA[l, r]
        auto getSum = [&](int l, int r) {
            int64 offset = (l > 0) ? cumA[l - 1] : 0;
            return cumA[r] - offset;
        };

        // cerr << "anchor: " << anchor << " mid1: " << mid1 << " mid2: " << mid2 << endl;

        int64 maxA1 = max(getSum(0, mid1), getSum(mid1 + 1, anchor - 1)),
              minA1 = min(getSum(0, mid1), getSum(mid1 + 1, anchor - 1));
        if (mid1 - 1 >= 0)
        {
            maxA1 = min(maxA1, max(getSum(0, mid1 - 1), getSum(mid1, anchor - 1)));
            minA1 = max(minA1, min(getSum(0, mid1 - 1), getSum(mid1, anchor - 1)));
        }
        int64 maxA2 = max(getSum(anchor, mid2), getSum(mid2 + 1, N - 1)),
              minA2 = min(getSum(anchor, mid2), getSum(mid2 + 1, N - 1));
        if (mid2 - 1 >= anchor)
        {
            maxA2 = min(maxA2, max(getSum(anchor, mid2 - 1), getSum(mid2, N - 1)));
            minA2 = max(minA2, min(getSum(anchor, mid2 - 1), getSum(mid2, N - 1)));
        }

        ans = min(ans, max(maxA1, maxA2) - min(minA1, minA2));
    }

    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 3328