#define _CRT_SECURE_NO_WARNINGS
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
#include <bits/stdc++.h>
using namespace std;
#define CRP(t, x) const t &x
#define OPL(t, x) bool operator<(CRP(t, x)) const
#define FIL(x, v) memset(x, v, sizeof(x))
#define CLR(x) FIL(x, 0)
#define NE1(x) FIL(x, -1)
#define INF(x) FIL(x, 0x3f)
#ifndef _DEBUG
#define _DEBUG 0
#endif // !_DEBUG
#define IFD if (_DEBUG)
typedef long long ll, i64;
const int N = 2e5 + 60;
ll a[N];
int main()
{
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%lld", a + i), a[i] += a[i - 1];
    ll ans = numeric_limits<decltype(ans)>::max();
    for (int i = 2; i < n - 1; i++)
    {
        int sp1 = lower_bound(a, a + n + 1, a[i] >> 1) - a;
        int sp2 = lower_bound(a, a + n + 1, (a[n] + a[i]) >> 1) - a;
        for (int j = -1; j <= 1; j++)
            for (int k = -1; k <= 1; k++)
            {
                int sp3 = max(min(sp1 + j, i - 1), 1);
                int sp4 = max(min(sp2 + k, n - 1), i + 1);
                auto res = {a[sp3], a[i] - a[sp3], a[sp4] - a[i], a[n] - a[sp4]};
                //printf("%d %d %d %lld\n", sp3, i, sp4, max(res) - min(res));
                ans = min(ans, max(res) - min(res));
            }
    }
    printf("%lld", ans);
    return 0;
}
