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

    auto left = new long[N];
    foreach (i; 1..N) {
        left[i] = left[i-1];
        while (abs(sum(0,left[i]) - sum(left[i], i)) > abs(sum(0,left[i]+1) - sum(left[i]+1,i))) left[i]++;
    }
    auto right = new long[N];
    foreach (i; 1..N) {
        right[i] = right[i-1];
        while (abs(sum(i,right[i]) - sum(right[i], N)) > abs(sum(i,right[i]+1) - sum(right[i]+1,N))) right[i]++;
    }
    N.iota.map!(i => [sum(0,left[i]), sum(left[i], i), sum(i, right[i]), sum(right[i], N)].pipe!(xs => xs.reduce!max - xs.reduce!min)).reduce!min.writeln;
}
