import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons, std.functional;

void main() {
    const N = readln.chomp.to!long;
    const as = readln.split.to!(long[]);
    auto ss = 0 ~ as.dup;
    foreach (i; 0..N) {
        ss[i+1] += ss[i];
    }
    long sum(long begin, long end) {
        return ss[end] - ss[begin];
    }

    cartesianProduct(iota(-1,2), iota(-1,2), iota(-1,2)).map!((t) {
        const i = t[0], j = t[1], k = t[2];
        const c2 = N.iota.reduce!((a,b) => abs(sum(0,a) - sum(a,N)) < abs(sum(0,b) - sum(b,N)) ? a : b) + i;
        const c1 = c2.iota.reduce!((a,b) => abs(sum(0,a) - sum(a,c2)) < abs(sum(0,b) - sum(b,c2)) ? a : b) + j;
        const c3 = iota(c2,N).reduce!((a,b) => abs(sum(c2,a) - sum(a,N)) < abs(sum(c2,b) - sum(b,N)) ? a : b) + k;
        return [c1, c2, c3];
    })
    .filter!(cs => cs.all!(c => 0 <= c && c <= N))
    .map!(c => [sum(0,c[0]), sum(c[0],c[1]), sum(c[1],c[2]), sum(c[2],N)].pipe!(xs => xs.reduce!max - xs.reduce!min))
    .reduce!min.writeln;
}

