from itertools import accumulate

import bisect





n = int(eval(input()))

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



arev = list(reversed(a))



front = list(accumulate(a))

back = list(accumulate(arev))



ans = float('inf')

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

    b = bisect.bisect_left(front,front[i]//2,0,i-1)

    c = bisect.bisect_left(back,back[n-i-2]//2,0,n-i-3)



    p = front[b]

    q = front[i]-front[b]

    s = back[c]

    r = back[n-i-2]-back[c]

    ans = min(max(p,q,r,s)-min(p,q,r,s),ans)



    if b!=0:

        p = front[b-1]

        q = front[i] - front[b-1]

        s = back[c]

        r = back[n-i-2] - back[c]

        ans = min(max(p, q, r, s) - min(p, q, r, s), ans)

    if c!=0:

        p = front[b]

        q = front[i] - front[b]

        s = back[c-1]

        r = back[n-i-2] - back[c-1]

        ans = min(max(p, q, r, s) - min(p, q, r, s), ans)

    if b!=0 and c!=0:

        p = front[b-1]

        q = front[i] - front[b-1]

        s = back[c-1]

        r = back[n-i-2] - back[c-1]

        ans = min(max(p, q, r, s) - min(p, q, r, s), ans)



print(ans)



















# 解説の方法



from itertools import accumulate



n = int(eval(input()))

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



acc = list(accumulate(a))



l = 0

r = 2



ans = float('inf')



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

    for x in range(l,y-1):

        if abs(acc[y]-acc[x]*2)>=abs(acc[y]-acc[x+1]*2):

            l = x+1

            continue

        else:

            break

    r = max(y+1,r)

    for z in range(r,n-2):

        if abs(acc[n-1]-acc[z]*2+acc[y])>=abs(acc[n-1]-acc[z+1]*2+acc[y]):

            r = z+1

            continue

        else:

            break

    P = acc[l]

    Q = acc[y] - acc[l]

    R = acc[r] - acc[y]

    S = acc[n-1] - acc[r]

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



print(ans)

from itertools import accumulate
import bisect

n = int(eval(input()))
a = list(map(int, input().split()))
arev = list(reversed(a))
front = list(accumulate(a))
back = list(accumulate(arev))
ans = float("inf")
for i in range(1, n - 2):
    b = bisect.bisect_left(front, front[i] // 2, 0, i - 1)
    c = bisect.bisect_left(back, back[n - i - 2] // 2, 0, n - i - 3)
    p = front[b]
    q = front[i] - front[b]
    s = back[c]
    r = back[n - i - 2] - back[c]
    ans = min(max(p, q, r, s) - min(p, q, r, s), ans)
    if b != 0:
        p = front[b - 1]
        q = front[i] - front[b - 1]
        s = back[c]
        r = back[n - i - 2] - back[c]
        ans = min(max(p, q, r, s) - min(p, q, r, s), ans)
    if c != 0:
        p = front[b]
        q = front[i] - front[b]
        s = back[c - 1]
        r = back[n - i - 2] - back[c - 1]
        ans = min(max(p, q, r, s) - min(p, q, r, s), ans)
    if b != 0 and c != 0:
        p = front[b - 1]
        q = front[i] - front[b - 1]
        s = back[c - 1]
        r = back[n - i - 2] - back[c - 1]
        ans = min(max(p, q, r, s) - min(p, q, r, s), ans)
print(ans)

# 解説の方法
from itertools import accumulate

n = int(eval(input()))
a = list(map(int, input().split()))
acc = list(accumulate(a))
l = 0
r = 2
ans = float("inf")
for y in range(1, n - 2):
    for x in range(l, y - 1):
        if abs(acc[y] - acc[x] * 2) >= abs(acc[y] - acc[x + 1] * 2):
            l = x + 1
            continue
        else:
            break
    r = max(y + 1, r)
    for z in range(r, n - 2):
        if abs(acc[n - 1] - acc[z] * 2 + acc[y]) >= abs(
            acc[n - 1] - acc[z + 1] * 2 + acc[y]
        ):
            r = z + 1
            continue
        else:
            break
    P = acc[l]
    Q = acc[y] - acc[l]
    R = acc[r] - acc[y]
    S = acc[n - 1] - acc[r]
    ans = min(ans, max(P, Q, R, S) - min(P, Q, R, S))
print(ans)

+# 解説の方法 -import bisect -arev = list(reversed(a)) -front = list(accumulate(a)) -back = list(accumulate(arev)) +acc = list(accumulate(a)) +l = 0 +r = 2 -for i in range(1, n - 2): -    b = bisect.bisect_left(front, front[i] // 2, 0, i - 1) -    c = bisect.bisect_left(back, back[n - i - 2] // 2, 0, n - i - 3) -    p = front[b] -    q = front[i] - front[b] -    s = back[c] -    r = back[n - i - 2] - back[c] -    ans = min(max(p, q, r, s) - min(p, q, r, s), ans) -    if b != 0: -        p = front[b - 1] -        q = front[i] - front[b - 1] -        s = back[c] -        r = back[n - i - 2] - back[c] -        ans = min(max(p, q, r, s) - min(p, q, r, s), ans) -    if c != 0: -        p = front[b] -        q = front[i] - front[b] -        s = back[c - 1] -        r = back[n - i - 2] - back[c - 1] -        ans = min(max(p, q, r, s) - min(p, q, r, s), ans) -    if b != 0 and c != 0: -        p = front[b - 1] -        q = front[i] - front[b - 1] -        s = back[c - 1] -        r = back[n - i - 2] - back[c - 1] -        ans = min(max(p, q, r, s) - min(p, q, r, s), ans) +for y in range(1, n - 2): +    for x in range(l, y - 1): +        if abs(acc[y] - acc[x] * 2) >= abs(acc[y] - acc[x + 1] * 2): +            l = x + 1 +            continue +        else: +            break +    r = max(y + 1, r) +    for z in range(r, n - 2): +        if abs(acc[n - 1] - acc[z] * 2 + acc[y]) >= abs( +            acc[n - 1] - acc[z + 1] * 2 + acc[y] +        ): +            r = z + 1 +            continue +        else: +            break +    P = acc[l] +    Q = acc[y] - acc[l] +    R = acc[r] - acc[y] +    S = acc[n - 1] - acc[r] +    ans = min(ans, max(P, Q, R, S) - min(P, Q, R, S))