from itertools import accumulate

N=int(eval(input()))

s=list(accumulate(list(map(int,input().split()))))

ans=s[-1]

p=0

q=2

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

    while abs(s[i-1]-2*s[p])>abs(s[i-1]-2*s[p+1]):

        p+=1

    while abs(s[q]*2-s[i-1]-s[-1])>abs(s[q+1]*2-s[i-1]-s[-1]):

        q+=1

    t=[s[i-1]-s[p],s[p],s[q]-s[i-1],s[-1]-s[q]]

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

print(ans)    
from itertools import accumulate

N=int(eval(input()))

s=list(accumulate(list(map(int,input().split()))))

def f(N,s):

    ans=s[-1]

    p=0

    q=2

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

        while abs(s[i-1]-2*s[p])>abs(s[i-1]-2*s[p+1]):

            p+=1

        while abs(s[q]*2-s[i-1]-s[-1])>abs(s[q+1]*2-s[i-1]-s[-1]):

            q+=1

        t=[s[i-1]-s[p],s[p],s[q]-s[i-1],s[-1]-s[q]]

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

    print(ans)

    return 

f(N,s)
from itertools import accumulate

N = int(eval(input()))
s = list(accumulate(list(map(int, input().split()))))
ans = s[-1]
p = 0
q = 2
for i in range(2, N - 1):
    while abs(s[i - 1] - 2 * s[p]) > abs(s[i - 1] - 2 * s[p + 1]):
        p += 1
    while abs(s[q] * 2 - s[i - 1] - s[-1]) > abs(s[q + 1] * 2 - s[i - 1] - s[-1]):
        q += 1
    t = [s[i - 1] - s[p], s[p], s[q] - s[i - 1], s[-1] - s[q]]
    ans = min(ans, max(t) - min(t))
print(ans)

from itertools import accumulate

N = int(eval(input()))
s = list(accumulate(list(map(int, input().split()))))


def f(N, s):
    ans = s[-1]
    p = 0
    q = 2
    for i in range(2, N - 1):
        while abs(s[i - 1] - 2 * s[p]) > abs(s[i - 1] - 2 * s[p + 1]):
            p += 1
        while abs(s[q] * 2 - s[i - 1] - s[-1]) > abs(s[q + 1] * 2 - s[i - 1] - s[-1]):
            q += 1
        t = [s[i - 1] - s[p], s[p], s[q] - s[i - 1], s[-1] - s[q]]
        ans = min(ans, max(t) - min(t))
    print(ans)
    return


f(N, s)

-ans = s[-1] -p = 0 -q = 2 -for i in range(2, N - 1): -    while abs(s[i - 1] - 2 * s[p]) > abs(s[i - 1] - 2 * s[p + 1]): -        p += 1 -    while abs(s[q] * 2 - s[i - 1] - s[-1]) > abs(s[q + 1] * 2 - s[i - 1] - s[-1]): -        q += 1 -    t = [s[i - 1] - s[p], s[p], s[q] - s[i - 1], s[-1] - s[q]] -    ans = min(ans, max(t) - min(t)) -print(ans) + + +def f(N, s): +    ans = s[-1] +    p = 0 +    q = 2 +    for i in range(2, N - 1): +        while abs(s[i - 1] - 2 * s[p]) > abs(s[i - 1] - 2 * s[p + 1]): +            p += 1 +        while abs(s[q] * 2 - s[i - 1] - s[-1]) > abs(s[q + 1] * 2 - s[i - 1] - s[-1]): +            q += 1 +        t = [s[i - 1] - s[p], s[p], s[q] - s[i - 1], s[-1] - s[q]] +        ans = min(ans, max(t) - min(t)) +    print(ans) +    return + + +f(N, s)