N = int(eval(input()))



#累積和で受け取る

A = [0] * N

S = 0

for i, a in enumerate(input().split()):

    S += int(a)

    A[i] = S



answer = S



#indexは最後に回収する番号

B = 0

D = 0



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

    D = max(D, C + 1)

    SB = A[B]

    SC = A[C] - A[B]

    SD = A[D] - A[C]

    SE = S - A[D]

    left_score = abs(SB - SC)

    #BCの境界の調整

    while True:

        B_ = B + 1

        SB_ = A[B_]

        SC_ = A[C] - SB_

        x = abs(SB_  - SC_)

        if left_score > x:

            B, SB, SC, left_score = B_, SB_, SC_, x

            continue

        break



    right_score = abs(SE - SD)

    while True:

        D_ = D + 1

        SD_ = A[D_] - A[C]

        SE_ = S - A[D_]

        x = abs(SD_ - SE_)

        if right_score > x:

            D, SD, SE, right_score = D_, SD_, SE_, x

            continue

        break



    score = max(SB, SC, SD, SE) - min(SB, SC, SD, SE)

    answer = min(score, answer)

print (answer)

import numpy as np



N = int(eval(input()))

A = np.zeros(N + 2, dtype = np.int64)

A[1:-1] = input().split()

np.cumsum(A, out = A)

total = A[-1]

A[-1] = 10 ** 18



left = np.searchsorted(A, A/2)

P1 = A[left - 1]

P2 = A[left]



Q1, Q2 = A - P1, A - P2



np.maximum(P1, Q2, out = P1)

np.minimum(P2, Q1, out = P2)

P, Q = P1, P2

right = np.searchsorted(A, (total + A)/2)

R1 = A[right - 1] - A

R2 = A[right] - A

S1 = total - A[right - 1]

S2 = total - A[right]

np.maximum(R1, S2, out = R1)

np.minimum(R2, S1, out = R2)

R, S = R1, R2



PQRS = np.vstack([P, Q, R, S])

x = np.max(PQRS, axis = 0) - np.min(PQRS, axis = 0)

answer = (x[1: -1].min())

print (answer)
N = int(eval(input()))
# 累積和で受け取る
A = [0] * N
S = 0
for i, a in enumerate(input().split()):
    S += int(a)
    A[i] = S
answer = S
# indexは最後に回収する番号
B = 0
D = 0
for C in range(1, N - 2):
    D = max(D, C + 1)
    SB = A[B]
    SC = A[C] - A[B]
    SD = A[D] - A[C]
    SE = S - A[D]
    left_score = abs(SB - SC)
    # BCの境界の調整
    while True:
        B_ = B + 1
        SB_ = A[B_]
        SC_ = A[C] - SB_
        x = abs(SB_ - SC_)
        if left_score > x:
            B, SB, SC, left_score = B_, SB_, SC_, x
            continue
        break
    right_score = abs(SE - SD)
    while True:
        D_ = D + 1
        SD_ = A[D_] - A[C]
        SE_ = S - A[D_]
        x = abs(SD_ - SE_)
        if right_score > x:
            D, SD, SE, right_score = D_, SD_, SE_, x
            continue
        break
    score = max(SB, SC, SD, SE) - min(SB, SC, SD, SE)
    answer = min(score, answer)
print(answer)

import numpy as np

N = int(eval(input()))
A = np.zeros(N + 2, dtype=np.int64)
A[1:-1] = input().split()
np.cumsum(A, out=A)
total = A[-1]
A[-1] = 10**18
left = np.searchsorted(A, A / 2)
P1 = A[left - 1]
P2 = A[left]
Q1, Q2 = A - P1, A - P2
np.maximum(P1, Q2, out=P1)
np.minimum(P2, Q1, out=P2)
P, Q = P1, P2
right = np.searchsorted(A, (total + A) / 2)
R1 = A[right - 1] - A
R2 = A[right] - A
S1 = total - A[right - 1]
S2 = total - A[right]
np.maximum(R1, S2, out=R1)
np.minimum(R2, S1, out=R2)
R, S = R1, R2
PQRS = np.vstack([P, Q, R, S])
x = np.max(PQRS, axis=0) - np.min(PQRS, axis=0)
answer = x[1:-1].min()
print(answer)

+import numpy as np + -# 累積和で受け取る -A = [0] * N -S = 0 -for i, a in enumerate(input().split()): -    S += int(a) -    A[i] = S -answer = S -# indexは最後に回収する番号 -B = 0 -D = 0 -for C in range(1, N - 2): -    D = max(D, C + 1) -    SB = A[B] -    SC = A[C] - A[B] -    SD = A[D] - A[C] -    SE = S - A[D] -    left_score = abs(SB - SC) -    # BCの境界の調整 -    while True: -        B_ = B + 1 -        SB_ = A[B_] -        SC_ = A[C] - SB_ -        x = abs(SB_ - SC_) -        if left_score > x: -            B, SB, SC, left_score = B_, SB_, SC_, x -            continue -        break -    right_score = abs(SE - SD) -    while True: -        D_ = D + 1 -        SD_ = A[D_] - A[C] -        SE_ = S - A[D_] -        x = abs(SD_ - SE_) -        if right_score > x: -            D, SD, SE, right_score = D_, SD_, SE_, x -            continue -        break -    score = max(SB, SC, SD, SE) - min(SB, SC, SD, SE) -    answer = min(score, answer) +A = np.zeros(N + 2, dtype=np.int64) +A[1:-1] = input().split() +np.cumsum(A, out=A) +total = A[-1] +A[-1] = 10**18 +left = np.searchsorted(A, A / 2) +P1 = A[left - 1] +P2 = A[left] +Q1, Q2 = A - P1, A - P2 +np.maximum(P1, Q2, out=P1) +np.minimum(P2, Q1, out=P2) +P, Q = P1, P2 +right = np.searchsorted(A, (total + A) / 2) +R1 = A[right - 1] - A +R2 = A[right] - A +S1 = total - A[right - 1] +S2 = total - A[right] +np.maximum(R1, S2, out=R1) +np.minimum(R2, S1, out=R2) +R, S = R1, R2 +PQRS = np.vstack([P, Q, R, S]) +x = np.max(PQRS, axis=0) - np.min(PQRS, axis=0) +answer = x[1:-1].min()