import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;


void main() {
    int n;
    scan(n);
    auto a = readln.split.to!(int[]);

    auto pf = new long[](n + 1);
    foreach (i ; 1 .. n + 1) {
        pf[i] = pf[i-1] + a[i-1];
    }

    long dif(int k, int btm, int top) {
        return abs(pf[top] - pf[k] - (pf[k] - pf[btm]));
    }

    int l, r;
    long ans = 10L^^18;

    foreach (i ; 0 .. n) {
        if (i < 2 || (n - i) < 2) {
            continue;
        }

        while (dif(l, 0, i) >= dif(l + 1, 0, i)) {
            l++;
        }

        while (r < i || dif(r, i, n) >= dif(r + 1, i, n)) {
            r++;
        }

        long p1 = pf[l] - pf[0];
        long p2 = pf[i] - pf[l];
        long p3 = pf[r] - pf[i];
        long p4 = pf[n] - pf[r];
        long mx = max(p1, p2, p3, p4);
        long mn = min(p1, p2, p3, p4);
        ans = min(ans, mx - mn);
    }

    writeln(ans);
}






































void scan(T...)(ref T args) {
    import std.stdio : readln;
    import std.algorithm : splitter;
    import std.conv : to;
    import std.range.primitives;

    auto line = readln().splitter();
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}



void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}
