Buggy python submission (verdict: Runtime Error):
from itertools import accumulate

# bisectはどっちでも（どちらに等号をつけても）うまくいくっぽい
from bisect import bisect_right, bisect_left

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

# 累積和を前計算
cumsum = list(accumulate(A))
cumsum.insert(0, 0)  # out of index 対策


ans = float('inf')
# 真ん中を全探索
for i in range(2, N-1):  # 左側は最低2つ残さなきゃいけないから2から(0もあるので）、右も同じ理由でN-1
    # 左の真ん中
    Lmid = cumsum[i] / 2
    
    # 左真ん中がどこに入るべきか
    Lx = bisect_left(cumsum, Lx)
    
    # Lxまで取るのか、Lx-1まで取るのか（どこまで取ると望ましい値Lmidに近づくのか）
    # 左側がLmidに近づくと右側もLmidに近づく→差の絶対値が0に近づく
    if abs(cumsum[Lx-1] - Lmid) < abs(cumsum[Lx] - Lmid):
        Lmid -= 1


    # 右側処理（基本は左と同じ）
    Rmid = (cumsum[N] + cumsum[i]) / 2  # 3分探索に要領でcumsum[i]を加えてやる
    Rx = bisect_left(cumsum, Rmid)
    # 超えるべきか超えないべきか
    if abs(cumsum[Rx-1] - Rmid) < abs(cumsum[Rx] - Rmid):
        Rx -= 1
    
    
    # 左からLx個の和
    P = cumsum[Lx]
    
    # Lx番目+1から真ん中区切りiまでの和
    Q = cumsum[i]-cumsum[Lx]
    
    # 真ん中区切りのi+1番目からRx番目までの和
    R = cumsum[Rx]-cumsum[i]
    
    # Rx+1番目から最後まで
    S = cumsum[N]-cumsum[Rx]
    ans = min(ans, max(P, Q, R, S) - min(P, Q, R, S))

print(ans)


Fixed submission:
from itertools import accumulate
from bisect import bisect_right
N = int(input())
A = list(map(int, input().split()))

# 累積和を前計算
cumsum = list(accumulate(A))
cumsum.insert(0, 0)

#print(cumsum)
ans = float('inf')
# 真ん中を全探索
for i in range(2, N-1):
    #print(A[:i], A[i:])
    # 左の真ん中
    LC = cumsum[i] / 2

    LX = bisect_right(cumsum, LC)
    #print(LX)
    # 超えるべきか超えないべきか
    if abs(cumsum[LX-1] - LC) < abs(cumsum[LX] - LC):
        LX -= 1

    # 右の真ん中
    RC = (cumsum[N] + cumsum[i]) / 2
    RX = bisect_right(cumsum, RC)
    # 超えるべきか超えないべきか
    if abs(cumsum[RX-1] - RC) < abs(cumsum[RX] - RC):
        RX -= 1

    P, Q, R, S = cumsum[LX], cumsum[i]-cumsum[LX], cumsum[RX]-cumsum[i], cumsum[N]-cumsum[RX]
    ans = min(ans, max(P, Q, R, S) - min(P, Q, R, S))

print(ans)

Buggy python submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split()))
X = sum(A) / 4

i = 0
tmp = 0
B, C, D, E = [], [], [], []
while True:
    B.append(A[i])
    tmp += A[i]
    i += 1
    if tmp > X:
        #print(B, tmp)
        if abs(X - (tmp - A[i-1])) < (tmp - X) and len(B) > 1:
            del B[-1]
            i -= 1
        break

tmp = 0
while True:
    C.append(A[i])
    tmp += A[i]
    i += 1
    if tmp > X:
        #print(X, tmp, C)
        if abs(X - (tmp - A[i-1])) < (tmp - X) and len(C) > 1:
            del C[-1]
            i -= 1
        break

tmp = 0
while True:
    D.append(A[i])
    tmp += A[i]
    i += 1
    if tmp > X or i == N - 1:
        if abs(X - (tmp - A[i-1])) < (tmp - X) and len(D) > 1:
            del D[-1]
            i -= 1
        break

tmp = 0
while i < N:
    E.append(A[i])
    tmp += A[i]
    i += 1

#print(B, C, D, E)
P, Q, R, S = sum(B), sum(C), sum(D), sum(E)
print(max(P, Q, R, S) - min(P, Q, R, S))


Fixed submission:
from itertools import accumulate
from bisect import bisect_right
N = int(input())
A = list(map(int, input().split()))

# 累積和を前計算
cumsum = list(accumulate(A))
cumsum.insert(0, 0)

#print(cumsum)
ans = float('inf')
# 真ん中を全探索
for i in range(2, N-1):
    #print(A[:i], A[i:])
    # 左の真ん中
    LC = cumsum[i] / 2

    LX = bisect_right(cumsum, LC)
    #print(LX)
    # 超えるべきか超えないべきか
    if abs(cumsum[LX-1] - LC) < abs(cumsum[LX] - LC):
        LX -= 1

    # 右の真ん中
    RC = (cumsum[N] + cumsum[i]) / 2
    RX = bisect_right(cumsum, RC)
    # 超えるべきか超えないべきか
    if abs(cumsum[RX-1] - RC) < abs(cumsum[RX] - RC):
        RX -= 1

    P, Q, R, S = cumsum[LX], cumsum[i]-cumsum[LX], cumsum[RX]-cumsum[i], cumsum[N]-cumsum[RX]
    ans = min(ans, max(P, Q, R, S) - min(P, Q, R, S))

print(ans)