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 = 2



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(SD-SE)

  # DEの境界の調整

  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(answer,score)

  

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 = 2
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(SD - SE)
    # DEの境界の調整
    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(answer, score)
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 = 2 -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(SD - SE) -    # DEの境界の調整 -    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(answer, score) +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()