import random

from itertools import accumulate

N = int(eval(input()))

As = [int(i) for i in input().split(' ')]



#N = pow(10, 4)

#As = [random.randint(0,1000) for r in range(N)] 

As = list(accumulate(As))





def bsearch(L, l, r, ls, rs):

    if r - l == 0:

        l_temp = L[l-1] if l!=0 else 0

        lsum = l_temp - ls

        return [max(lsum, rs - L[l]), min(lsum, rs - L[l]) + L[l] - l_temp]

    n = int((r-l)/2)

    lsum = L[l+n] - ls

    rsum = rs - L[l+n]

    if lsum > rsum:

        return bsearch(L, l, l+n, ls, rs)

    else:

        return bsearch(L, l+n+1, r, ls, rs)



for i in range(N-3):

    cs = bsearch(As, 0, i+1, 0, As[i+1]) + bsearch(As ,i+2, N-1, As[i+1], As[-1])

    if i == 0:

        result = max(cs) - min(cs)

    else:

        if max(cs) - min(cs) < result:

            result = max(cs) - min(cs)

print(result)



def bsearch(mn, mx, func):

    #func(i)=False を満たす最大のi

    idx = (mx + mn)//2

    while mx-mn>1:

        if func(idx):

            idx, mx = (idx + mn)//2, idx

            continue

        idx, mn = (idx + mx)//2, idx

    return idx

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

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

X = [0]*(N+2)

for i in range(N):

    X[i+1] = X[i] + A[i]



R = 10**18

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

    # X0, X1, X2 .. Xi

    # Xj Xj+1 (1<= j <= i-1)

    idx1 = bsearch(1, i-1, lambda j:X[i]//2 < X[j])

    idx2 = bsearch(i+1, N, lambda j:(X[N]-X[i])//2 < (X[j]-X[i]))

#    print(X[idx1], X[i]-X[idx1], X[idx2]-X[i], X[N]-X[idx2])

    if idx1+1 <= i-1:

        if abs(2*X[idx1]-X[i]) > abs(2*X[idx1+1]-X[i]):

            idx1+=1

#    print(1,i,idx2)

    if idx2+1 <= N-1:

        if abs(2*X[idx2]-X[i]-X[N]) > abs(2*X[idx2+1]-X[i]-X[N]):

            idx2+=1

    mx = max([X[idx1], X[i]-X[idx1], X[idx2]-X[i], X[N]-X[idx2]])

    mn = min([X[idx1], X[i]-X[idx1], X[idx2]-X[i], X[N]-X[idx2]])

#    print(X[i]-X[idx1], X[idx1], X[idx2]-X[i])

#    print(idx1, i, idx2)

#    print(mx-mn)

    R = min(R, mx-mn)

print(R)



import random
from itertools import accumulate

N = int(eval(input()))
As = [int(i) for i in input().split(" ")]
# N = pow(10, 4)
# As = [random.randint(0,1000) for r in range(N)]
As = list(accumulate(As))


def bsearch(L, l, r, ls, rs):
    if r - l == 0:
        l_temp = L[l - 1] if l != 0 else 0
        lsum = l_temp - ls
        return [max(lsum, rs - L[l]), min(lsum, rs - L[l]) + L[l] - l_temp]
    n = int((r - l) / 2)
    lsum = L[l + n] - ls
    rsum = rs - L[l + n]
    if lsum > rsum:
        return bsearch(L, l, l + n, ls, rs)
    else:
        return bsearch(L, l + n + 1, r, ls, rs)


for i in range(N - 3):
    cs = bsearch(As, 0, i + 1, 0, As[i + 1]) + bsearch(
        As, i + 2, N - 1, As[i + 1], As[-1]
    )
    if i == 0:
        result = max(cs) - min(cs)
    else:
        if max(cs) - min(cs) < result:
            result = max(cs) - min(cs)
print(result)

def bsearch(mn, mx, func):
    # func(i)=False を満たす最大のi
    idx = (mx + mn) // 2
    while mx - mn > 1:
        if func(idx):
            idx, mx = (idx + mn) // 2, idx
            continue
        idx, mn = (idx + mx) // 2, idx
    return idx


(N,) = list(map(int, input().split()))
A = list(map(int, input().split()))
X = [0] * (N + 2)
for i in range(N):
    X[i + 1] = X[i] + A[i]
R = 10**18
for i in range(2, N - 1):
    # X0, X1, X2 .. Xi
    # Xj Xj+1 (1<= j <= i-1)
    idx1 = bsearch(1, i - 1, lambda j: X[i] // 2 < X[j])
    idx2 = bsearch(i + 1, N, lambda j: (X[N] - X[i]) // 2 < (X[j] - X[i]))
    #    print(X[idx1], X[i]-X[idx1], X[idx2]-X[i], X[N]-X[idx2])
    if idx1 + 1 <= i - 1:
        if abs(2 * X[idx1] - X[i]) > abs(2 * X[idx1 + 1] - X[i]):
            idx1 += 1
    #    print(1,i,idx2)
    if idx2 + 1 <= N - 1:
        if abs(2 * X[idx2] - X[i] - X[N]) > abs(2 * X[idx2 + 1] - X[i] - X[N]):
            idx2 += 1
    mx = max([X[idx1], X[i] - X[idx1], X[idx2] - X[i], X[N] - X[idx2]])
    mn = min([X[idx1], X[i] - X[idx1], X[idx2] - X[i], X[N] - X[idx2]])
    #    print(X[i]-X[idx1], X[idx1], X[idx2]-X[i])
    #    print(idx1, i, idx2)
    #    print(mx-mn)
    R = min(R, mx - mn)
print(R)

-import random -from itertools import accumulate - -N = int(eval(input())) -As = [int(i) for i in input().split(" ")] -# N = pow(10, 4) -# As = [random.randint(0,1000) for r in range(N)] -As = list(accumulate(As)) +def bsearch(mn, mx, func): +    # func(i)=False を満たす最大のi +    idx = (mx + mn) // 2 +    while mx - mn > 1: +        if func(idx): +            idx, mx = (idx + mn) // 2, idx +            continue +        idx, mn = (idx + mx) // 2, idx +    return idx -def bsearch(L, l, r, ls, rs): -    if r - l == 0: -        l_temp = L[l - 1] if l != 0 else 0 -        lsum = l_temp - ls -        return [max(lsum, rs - L[l]), min(lsum, rs - L[l]) + L[l] - l_temp] -    n = int((r - l) / 2) -    lsum = L[l + n] - ls -    rsum = rs - L[l + n] -    if lsum > rsum: -        return bsearch(L, l, l + n, ls, rs) -    else: -        return bsearch(L, l + n + 1, r, ls, rs) - - -for i in range(N - 3): -    cs = bsearch(As, 0, i + 1, 0, As[i + 1]) + bsearch( -        As, i + 2, N - 1, As[i + 1], As[-1] -    ) -    if i == 0: -        result = max(cs) - min(cs) -    else: -        if max(cs) - min(cs) < result: -            result = max(cs) - min(cs) -print(result) +(N,) = list(map(int, input().split())) +A = list(map(int, input().split())) +X = [0] * (N + 2) +for i in range(N): +    X[i + 1] = X[i] + A[i] +R = 10**18 +for i in range(2, N - 1): +    # X0, X1, X2 .. Xi +    # Xj Xj+1 (1<= j <= i-1) +    idx1 = bsearch(1, i - 1, lambda j: X[i] // 2 < X[j]) +    idx2 = bsearch(i + 1, N, lambda j: (X[N] - X[i]) // 2 < (X[j] - X[i])) +    #    print(X[idx1], X[i]-X[idx1], X[idx2]-X[i], X[N]-X[idx2]) +    if idx1 + 1 <= i - 1: +        if abs(2 * X[idx1] - X[i]) > abs(2 * X[idx1 + 1] - X[i]): +            idx1 += 1 +    #    print(1,i,idx2) +    if idx2 + 1 <= N - 1: +        if abs(2 * X[idx2] - X[i] - X[N]) > abs(2 * X[idx2 + 1] - X[i] - X[N]): +            idx2 += 1 +    mx = max([X[idx1], X[i] - X[idx1], X[idx2] - X[i], X[N] - X[idx2]]) +    mn = min([X[idx1], X[i] - X[idx1], X[idx2] - X[i], X[N] - X[idx2]]) +    #    print(X[i]-X[idx1], X[idx1], X[idx2]-X[i]) +    #    print(idx1, i, idx2) +    #    print(mx-mn) +    R = min(R, mx - mn) +print(R)