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

void main(){
    readln();
    auto A = readln().strip().split().map!(to!int)().array;

    auto ave = A.reduce!"a+b".to!float / 4;

    int mn = int.max, mx;
    ulong pos;

    foreach(i; 0..4) {
        int t;
        for (size_t j = pos; j < A.length; ++j) {
            t += A[j];
            //writeln(t);
            if((j-pos!=0 && t > ave) || A.length - j + i < 4) {
                //writeln("-------------");
                auto tt = t - A[j];
                if(i == 3 || abs(t - ave) < abs(tt - ave)) {
                    tt = t;
                    pos = j+1;
                } else {
                    pos = j;
                }

                mn = min(mn, tt);
                mx = max(mx, tt);
                break;
            }
        }
    }

    //writeln("min:", mn, " mx:", mx);
    writeln(mx-mn);
}