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!(long[]);
    auto ps = new long[](n + 1);
    foreach (i ; 1 .. n + 1) {
        ps[i] = ps[i-1] + a[i-1];
    }

    long ans = 10L^^18;

    foreach (mid ; 2 .. n - 1) {
        auto left = nibutan(n, ps, 0, mid);
        auto right = nibutan(n, ps, mid, n);
        long p = ps[left] - ps[0];
        long q = ps[mid] - ps[left];
        long r = ps[right] - ps[mid];
        long s = ps[n] - ps[right];
        debug {
            writefln("left = %d, right = %d", left, right);
            writefln("%d, %d, %d, %d", p, q, r, s);
        }
        long mm = max(p, q, r, s);
        long mi = min(p, q, r, s);
        ans = min(ans, mm - mi);
    }

    writeln(ans);
}

int nibutan(int n, long[] ps, int btm, int top) {
    long W = ps[top] - ps[btm];

    long calc(int k) {
        return abs(ps[top] - ps[k] - (ps[k] - ps[btm]));
    }

    bool check(int k) {
        return calc(k + 1) >= calc(k);
    }

    int bb = btm, tt = top;

    while (tt - bb > 1) {
        int mid = (bb + tt) / 2;

        debug {
            writefln("%d, %d", mid, calc(mid));
            writefln("%d, %d", mid + 1, calc(mid + 1));
        }
        if (check(mid)) {
            tt = mid;
        }
        else {
            bb = mid;
        }
    }

    return tt;
}







































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);
        }
    }
}
