from itertools import accumulate

from bisect import bisect_right

N = int(eval(input()))

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



# 累積和を前計算

cumsum = list(accumulate(A))

cumsum.insert(0, 0)



#print(cumsum)

ans = float('inf')

# 真ん中を全探索

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

    #print(A[:i], A[i:])

    # 左の真ん中

    LC = cumsum[i] / 2



    LX = bisect_right(cumsum, LC)

    #print(LX)

    # 超えるべきか超えないべきか

    if abs(cumsum[LX-1] - LC) < abs(cumsum[LX] - LC):

        LX -= 1



    # 右の真ん中

    RC = (cumsum[N] + cumsum[i]) / 2

    RX = bisect_right(cumsum, RC)

    # 超えるべきか超えないべきか

    if abs(cumsum[RX-1] - RC) < abs(cumsum[RX] - RC):

        RX -= 1



    P, Q, R, S = cumsum[LX], cumsum[i]-cumsum[LX], cumsum[RX]-cumsum[i], cumsum[N]-cumsum[RX]

    ans = min(ans, max(P, Q, R, S) - min(P, Q, R, S))



print(ans)

from itertools import accumulate

from bisect import bisect_right, bisect_left



N = int(eval(input()))

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



# 累積和を前計算

cumsum = list(accumulate(A))

cumsum.insert(0, 0)  # out of index 対策





ans = float('inf')

# 真ん中を全探索

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

    #print(cumsum[:i], cumsum[i:])

    # 左の真ん中

    LC = cumsum[i] / 2



    LX = bisect_left(cumsum, LC)

    #print(LX)

    # 超えるべきか超えないべきか

    if abs(cumsum[LX-1] - LC) < abs(cumsum[LX] - LC):

        LX -= 1



    # 右の真ん中

    RC = (cumsum[N] + cumsum[i]) / 2

    RX = bisect_left(cumsum, RC)

    # 超えるべきか超えないべきか

    if abs(cumsum[RX-1] - RC) < abs(cumsum[RX] - RC):

        RX -= 1



    P, Q, R, S = cumsum[LX], cumsum[i]-cumsum[LX], cumsum[RX]-cumsum[i], cumsum[N]-cumsum[RX]

    ans = min(ans, max(P, Q, R, S) - min(P, Q, R, S))



print(ans)

from itertools import accumulate
from bisect import bisect_right

N = int(eval(input()))
A = list(map(int, input().split()))
# 累積和を前計算
cumsum = list(accumulate(A))
cumsum.insert(0, 0)
# print(cumsum)
ans = float("inf")
# 真ん中を全探索
for i in range(2, N - 1):
    # print(A[:i], A[i:])
    # 左の真ん中
    LC = cumsum[i] / 2
    LX = bisect_right(cumsum, LC)
    # print(LX)
    # 超えるべきか超えないべきか
    if abs(cumsum[LX - 1] - LC) < abs(cumsum[LX] - LC):
        LX -= 1
    # 右の真ん中
    RC = (cumsum[N] + cumsum[i]) / 2
    RX = bisect_right(cumsum, RC)
    # 超えるべきか超えないべきか
    if abs(cumsum[RX - 1] - RC) < abs(cumsum[RX] - RC):
        RX -= 1
    P, Q, R, S = (
        cumsum[LX],
        cumsum[i] - cumsum[LX],
        cumsum[RX] - cumsum[i],
        cumsum[N] - cumsum[RX],
    )
    ans = min(ans, max(P, Q, R, S) - min(P, Q, R, S))
print(ans)

from itertools import accumulate
from bisect import bisect_right, bisect_left

N = int(eval(input()))
A = list(map(int, input().split()))
# 累積和を前計算
cumsum = list(accumulate(A))
cumsum.insert(0, 0)  # out of index 対策
ans = float("inf")
# 真ん中を全探索
for i in range(2, N - 1):
    # print(cumsum[:i], cumsum[i:])
    # 左の真ん中
    LC = cumsum[i] / 2
    LX = bisect_left(cumsum, LC)
    # print(LX)
    # 超えるべきか超えないべきか
    if abs(cumsum[LX - 1] - LC) < abs(cumsum[LX] - LC):
        LX -= 1
    # 右の真ん中
    RC = (cumsum[N] + cumsum[i]) / 2
    RX = bisect_left(cumsum, RC)
    # 超えるべきか超えないべきか
    if abs(cumsum[RX - 1] - RC) < abs(cumsum[RX] - RC):
        RX -= 1
    P, Q, R, S = (
        cumsum[LX],
        cumsum[i] - cumsum[LX],
        cumsum[RX] - cumsum[i],
        cumsum[N] - cumsum[RX],
    )
    ans = min(ans, max(P, Q, R, S) - min(P, Q, R, S))
print(ans)

-from bisect import bisect_right +from bisect import bisect_right, bisect_left -cumsum.insert(0, 0) -# print(cumsum) +cumsum.insert(0, 0)  # out of index 対策 -    # print(A[:i], A[i:]) +    # print(cumsum[:i], cumsum[i:]) -    LX = bisect_right(cumsum, LC) +    LX = bisect_left(cumsum, LC) -    RX = bisect_right(cumsum, RC) +    RX = bisect_left(cumsum, RC)