### BUGGY CODE ###

def cumulative_sum_list(l):
    cl = list()
    cs = 0
    for i in range(len(l)):
        cs += l[i]
        cl.append(cs)
    return cl

def cumsum(l, a, b):
    return l[b] - (0 if a == 0 else l[a-1])

def evaluate(cl, c0, c1, c2):
    b = cumsum(cl, 0, c0)
    c = cumsum(cl, c0+1, c1)
    d = cumsum(cl, c1+1, c2)
    e = cumsum(cl, c2+1, len(cl)-1)
    return (abs(max(b,c,d,e) - min(b,c,d,e)))

if __name__ == '__main__':
    # read input
    n = int(input())
    l = list(map(int, input().split()))
    # create cumlative list first
    cl = cumulative_sum_list(l)
    total = cl[-1]
    clen = len(l)

    #  0  F(i)      i i+1 G(i)      len-1
    #  |------|------|-------|-------|
    #         c0    c1       c2
    #  <-----L------> <------R------->
    # pick up a c1 (i:i+1) and evaluate
    f0 = dict()
    g0 = dict()
    for i in range(1,clen-2):
        #print("i={}\tL{}, R{}".format(i, l[0:i+1], l[i+1:]))
        # find C0 for each i
        best_c0 = None
        for c0 in range(i):
            B = cumsum(cl,0,c0)
            C = cumsum(cl,c0+1,i)
            diff = abs(B-C)
            if best_c0 is None:
                best_c0 = c0
            else:
                if odiff > diff:
                    best_c0 = c0
            odiff = diff
            #print("\t\tc0={}, [{}, {}] diff={}".format(c0, B, C, diff))
        f0[i] = best_c0

        # find C2 for each i
        best_c2 = None
        for c2 in range(i+1,clen-1):
            D = cumsum(cl, i+1, c2)
            E = cumsum(cl, c2+1, clen-1)
            diff = abs(E-D)
            if best_c2 is None:
                best_c2 = c2
            else:
                if odiff > diff:
                    best_c2 = c2
            odiff = diff
            #print("\t\tc2={}, [{}, {}] diff={}".format(c0, D, E, diff))
        g0[i] = best_c2

    #print('best C0:{} C2:{}'.format(f0, g0))

    r = list()
    for i in range(1,clen-2):
        #print("cut: {}/{}/{}".format(f0[i], i, g0[i]))
        #print("{} - {} - {} - {}".format(l[0:f0[i]+1], l[f0[i]+1:i+1], l[i+1:g0[i]+1], l[g0[i]+1:]))
        diff = evaluate(cl, f0[i], i, g0[i])
        r.append(diff)

    print(min(r))

### FIXED CODE ###

from itertools import accumulate

def Sdiff(s, m, e):
    # Sdiff(s, m, e) = [A_s, A_s+1 ..., A_m] [A_m+1 .... A_e]
    #    L = cl[m]-cl[s-1], R = cl[e] - cl[m]
    # diff(L,R) = abs(cl[m] - cl[s-1] - cl[e] + cl[m]) = abs(2*cl[m] - cl[s-1] - cl[e])

    x = 0 if s == 0 else cl[s-1]
    return abs(2*cl[m] - x - cl[e])

def partial(start, end, last):
    #print("partial: {}-{} last:{}".format(start,end, last))
    odiff = Sdiff(start, last, end)

    for c in range(last, end):
        diff = Sdiff(start, c, end)
        #print("partial loop: c:{}, diff:{} -> {}".format(c, odiff, diff))
        if odiff < diff:
            return c-1
        odiff = diff
    return c

if __name__ == '__main__':
    # read input
    N = int(input())
    l = list(map(int, input().split()))
    # create cumlative list first
    cl = list(accumulate(l))

    #  0  F(i)      i i+1 G(i)      len-1
    #  |------|------|-------|-------|
    #         c0    c1       c2
    #  <-----L------> <------R------->
    # pick up a c1 (i:i+1) and evaluate
    result = 9 * 10e9
    c1 = 0
    c3 = 2
    for i in range(1, N-2):
        #print("LEFT:RIGHT {}/={}=/{}".format(l[0:i+1], i, l[i+1: N]))
        # LEFT
        c1 = partial(0, i, c1)
        # RIGHT
        c3 = partial(i+1, N-1, c3)

        # evaluate
        mb = cl[c1]
        mc = cl[i] - mb
        md = cl[c3] - cl[i]
        me = cl[N-1] - cl[c3]
        diff = abs(max(mb, mc, md, me) - min(mb, mc, md, me))
        #print("\t\t{}/{}/{}: [{} {} {} {}]".format(c1, i, c3, mb, mc, md, me))
        #print("\t\tdiff={}".format(diff))
        if diff < result:
            result = diff

    print(result)