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

void main()
{
    auto N = readln.chomp.to!int;
    long[] as = [0];
    foreach (i, a; readln.split.to!(long[])) as ~= a + as[i];

    auto pq_max = new long[](N+1);
    auto pq_min = new long[](N+1);
    auto rs_max = new long[](N+1);
    auto rs_min = new long[](N+1);

    int i = 2, j = 1;
    while (i <= N) {
        while (j+1 < i && abs(as[j]*2 - as[i]) > abs(as[j+1]*2 - as[i])) ++j;
        pq_max[i] = max(as[j], as[i]-as[j]);
        pq_min[i] = min(as[j], as[i]-as[j]);
        ++i;
    }
    i = N-2, j = N-1;
    while (i >= 0) {
        while (j-1 > i && abs(as[j]*2 - as[i] - as[N]) > abs(as[j-1]*2 - as[i] - as[N])) --j;
        rs_max[i] = max(as[j]-as[i], as[N]-as[j]);
        rs_min[i] = min(as[j]-as[i], as[N]-as[j]);
        --i;
    }

    long r = long.max;
    foreach (k; 2..N-1) {
        r = min(r, max(pq_max[k], rs_max[k]) - min(pq_min[k], rs_min[k]));
    }
    writeln(r);
}