### 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])

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
    r = list()
    result = None
    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
        c0 = None
        for c in range(i):
            B = cumsum(cl,0,c)
            C = cumsum(cl,c+1,i)
            diff = abs(B-C)
            if c0 is None:
                c0 = c
                mb = B
                mc = C
            else:
                if odiff > diff:
                    c0 = c
                    mb = B
                    mc = C
            odiff = diff
            #print("\t\tc0={}, [{}, {}] diff={}".format(c0, B, C, diff))

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

        # evaluate
        diff = abs(max(mb, mc, md, me) - min(mb, mc, md, me))
        if result is None:
            result = diff
        else:
            if result > diff:
                result = diff

    print(result)

### 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)