from itertools import accumulate

from bisect import bisect_left, bisect_right



n = int(eval(input()))

a = list(map(int, input().split()))



ts = list(accumulate(a))+[0]

r = 0



def calc(st, ed):

    t0 = ts[ed-1] - ts[st-1]

    i1 = bisect_left(ts, (ts[ed-1]+ts[st-1])/2, st+1, ed-1)

    t11 = ts[i1-1] - ts[st-1]

    t12 = t0 - t11

    t21 = ts[i1] - ts[st-1]

    t22 = t0 - t21

    if abs(t22-t21) > abs(t12-t11):

        return t11, t12

    else:

        return t21, t22



ans = 10**18

for i in range(2, n-1):

    r1, r2 = calc(0, i)

    r3, r4 = calc(i, n)

    # print(r1, r2, r3, r4)

    diff = max([r1, r2, r3, r4]) - min([r1, r2, r3, r4])

    ans = min(ans, diff)



print(ans)

    



n = int(eval(input()))

a = list(map(int, input().split()))



from itertools import accumulate

accum = list(accumulate(a))



i_left = 0

i_right = 2

ans = 10**18

for i in range(1, n-2):

    while True:

        p = accum[i_left]

        q = accum[i] - p

        p_new = accum[i_left+1]

        q_new = accum[i] - p_new

        if abs(p_new - q_new) < abs(p - q):

            i_left += 1

        else:

            break

    while True:

        r = accum[i_right] - accum[i]

        s = accum[-1] - accum[i_right]

        r_new = accum[i_right+1] - accum[i]

        s_new = accum[-1] - accum[i_right+1]

        if abs(r_new - s_new) < abs(r - s):

            i_right += 1

        else:

            break



    # print(p, q, r, s)

    diff = max([p, q, r, s]) - min([p, q, r, s])

    ans = min(ans, diff)



print(ans)

from itertools import accumulate
from bisect import bisect_left, bisect_right

n = int(eval(input()))
a = list(map(int, input().split()))
ts = list(accumulate(a)) + [0]
r = 0


def calc(st, ed):
    t0 = ts[ed - 1] - ts[st - 1]
    i1 = bisect_left(ts, (ts[ed - 1] + ts[st - 1]) / 2, st + 1, ed - 1)
    t11 = ts[i1 - 1] - ts[st - 1]
    t12 = t0 - t11
    t21 = ts[i1] - ts[st - 1]
    t22 = t0 - t21
    if abs(t22 - t21) > abs(t12 - t11):
        return t11, t12
    else:
        return t21, t22


ans = 10**18
for i in range(2, n - 1):
    r1, r2 = calc(0, i)
    r3, r4 = calc(i, n)
    # print(r1, r2, r3, r4)
    diff = max([r1, r2, r3, r4]) - min([r1, r2, r3, r4])
    ans = min(ans, diff)
print(ans)

n = int(eval(input()))
a = list(map(int, input().split()))
from itertools import accumulate

accum = list(accumulate(a))
i_left = 0
i_right = 2
ans = 10**18
for i in range(1, n - 2):
    while True:
        p = accum[i_left]
        q = accum[i] - p
        p_new = accum[i_left + 1]
        q_new = accum[i] - p_new
        if abs(p_new - q_new) < abs(p - q):
            i_left += 1
        else:
            break
    while True:
        r = accum[i_right] - accum[i]
        s = accum[-1] - accum[i_right]
        r_new = accum[i_right + 1] - accum[i]
        s_new = accum[-1] - accum[i_right + 1]
        if abs(r_new - s_new) < abs(r - s):
            i_right += 1
        else:
            break
    # print(p, q, r, s)
    diff = max([p, q, r, s]) - min([p, q, r, s])
    ans = min(ans, diff)
print(ans)

-from itertools import accumulate -from bisect import bisect_left, bisect_right - -ts = list(accumulate(a)) + [0] -r = 0 +from itertools import accumulate - -def calc(st, ed): -    t0 = ts[ed - 1] - ts[st - 1] -    i1 = bisect_left(ts, (ts[ed - 1] + ts[st - 1]) / 2, st + 1, ed - 1) -    t11 = ts[i1 - 1] - ts[st - 1] -    t12 = t0 - t11 -    t21 = ts[i1] - ts[st - 1] -    t22 = t0 - t21 -    if abs(t22 - t21) > abs(t12 - t11): -        return t11, t12 -    else: -        return t21, t22 - - +accum = list(accumulate(a)) +i_left = 0 +i_right = 2 -for i in range(2, n - 1): -    r1, r2 = calc(0, i) -    r3, r4 = calc(i, n) -    # print(r1, r2, r3, r4) -    diff = max([r1, r2, r3, r4]) - min([r1, r2, r3, r4]) +for i in range(1, n - 2): +    while True: +        p = accum[i_left] +        q = accum[i] - p +        p_new = accum[i_left + 1] +        q_new = accum[i] - p_new +        if abs(p_new - q_new) < abs(p - q): +            i_left += 1 +        else: +            break +    while True: +        r = accum[i_right] - accum[i] +        s = accum[-1] - accum[i_right] +        r_new = accum[i_right + 1] - accum[i] +        s_new = accum[-1] - accum[i_right + 1] +        if abs(r_new - s_new) < abs(r - s): +            i_right += 1 +        else: +            break +    # print(p, q, r, s) +    diff = max([p, q, r, s]) - min([p, q, r, s])