from itertools import accumulate

n=int(eval(input()))

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

acc=list(accumulate(a))

sumA=acc[-1]

cut=[0, 1, 2, len(a)-1]

score=[acc[cut[0]], acc[cut[1]]-acc[cut[0]], acc[cut[2]]-acc[cut[1]], acc[cut[3]]-acc[cut[2]]]

ans=max(score)-min(score)

for i in range(1,len(a)-1):

    cut[1]=i    

    while abs(acc[cut[1]]-acc[cut[0]]-acc[cut[0]]) > abs(acc[cut[1]]-acc[cut[0]+1]-acc[cut[0]+1]):

        cut[0]+=1 

    while abs(acc[cut[3]]-acc[cut[2]]-(acc[cut[2]]-acc[cut[1]])) > abs(acc[cut[3]]-acc[cut[2]+1]-(acc[cut[2]+1]-acc[cut[1]])):

        cut[2]+=1

    score=[acc[cut[0]], acc[cut[1]]-acc[cut[0]], acc[cut[2]]-acc[cut[1]], acc[cut[3]]-acc[cut[2]]]

    ans=min(ans, max(score)-min(score))



print(ans)

from itertools import accumulate

n=int(eval(input()))

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

acc=list(accumulate(a))

left=0; right=2

ans=float('inf')

for center in range(1,n-1):

    while abs(acc[center]-acc[left]-acc[left]) > abs(acc[center]-acc[left+1]-acc[left+1]):

        left+=1 

    while abs(acc[n-1]-acc[right]-(acc[right]-acc[center])) > abs(acc[n-1]-acc[right+1]-(acc[right+1]-acc[center])):

        right+=1

    score=acc[left], acc[center]-acc[left], acc[right]-acc[center], acc[n-1]-acc[right]

    ans=min(ans, max(score)-min(score))



print(ans)

from itertools import accumulate

n = int(eval(input()))
a = list(map(int, input().split()))
acc = list(accumulate(a))
sumA = acc[-1]
cut = [0, 1, 2, len(a) - 1]
score = [
    acc[cut[0]],
    acc[cut[1]] - acc[cut[0]],
    acc[cut[2]] - acc[cut[1]],
    acc[cut[3]] - acc[cut[2]],
]
ans = max(score) - min(score)
for i in range(1, len(a) - 1):
    cut[1] = i
    while abs(acc[cut[1]] - acc[cut[0]] - acc[cut[0]]) > abs(
        acc[cut[1]] - acc[cut[0] + 1] - acc[cut[0] + 1]
    ):
        cut[0] += 1
    while abs(acc[cut[3]] - acc[cut[2]] - (acc[cut[2]] - acc[cut[1]])) > abs(
        acc[cut[3]] - acc[cut[2] + 1] - (acc[cut[2] + 1] - acc[cut[1]])
    ):
        cut[2] += 1
    score = [
        acc[cut[0]],
        acc[cut[1]] - acc[cut[0]],
        acc[cut[2]] - acc[cut[1]],
        acc[cut[3]] - acc[cut[2]],
    ]
    ans = min(ans, max(score) - min(score))
print(ans)

from itertools import accumulate

n = int(eval(input()))
a = list(map(int, input().split()))
acc = list(accumulate(a))
left = 0
right = 2
ans = float("inf")
for center in range(1, n - 1):
    while abs(acc[center] - acc[left] - acc[left]) > abs(
        acc[center] - acc[left + 1] - acc[left + 1]
    ):
        left += 1
    while abs(acc[n - 1] - acc[right] - (acc[right] - acc[center])) > abs(
        acc[n - 1] - acc[right + 1] - (acc[right + 1] - acc[center])
    ):
        right += 1
    score = (
        acc[left],
        acc[center] - acc[left],
        acc[right] - acc[center],
        acc[n - 1] - acc[right],
    )
    ans = min(ans, max(score) - min(score))
print(ans)

-sumA = acc[-1] -cut = [0, 1, 2, len(a) - 1] -score = [ -    acc[cut[0]], -    acc[cut[1]] - acc[cut[0]], -    acc[cut[2]] - acc[cut[1]], -    acc[cut[3]] - acc[cut[2]], -] -ans = max(score) - min(score) -for i in range(1, len(a) - 1): -    cut[1] = i -    while abs(acc[cut[1]] - acc[cut[0]] - acc[cut[0]]) > abs( -        acc[cut[1]] - acc[cut[0] + 1] - acc[cut[0] + 1] +left = 0 +right = 2 +ans = float("inf") +for center in range(1, n - 1): +    while abs(acc[center] - acc[left] - acc[left]) > abs( +        acc[center] - acc[left + 1] - acc[left + 1] -        cut[0] += 1 -    while abs(acc[cut[3]] - acc[cut[2]] - (acc[cut[2]] - acc[cut[1]])) > abs( -        acc[cut[3]] - acc[cut[2] + 1] - (acc[cut[2] + 1] - acc[cut[1]]) +        left += 1 +    while abs(acc[n - 1] - acc[right] - (acc[right] - acc[center])) > abs( +        acc[n - 1] - acc[right + 1] - (acc[right + 1] - acc[center]) -        cut[2] += 1 -    score = [ -        acc[cut[0]], -        acc[cut[1]] - acc[cut[0]], -        acc[cut[2]] - acc[cut[1]], -        acc[cut[3]] - acc[cut[2]], -    ] +        right += 1 +    score = ( +        acc[left], +        acc[center] - acc[left], +        acc[right] - acc[center], +        acc[n - 1] - acc[right], +    )