Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
import bisect

n = int(input())
a = list(map(int, input().split()))
b = [0] * (n + 1)
for i in range(n):
    b[i + 1] = b[i] + a[i]
l, r = deque([b[1]]), deque(b[3 : n])
lenl, lenr = len(l), len(r)
ans = 1145141919810
for i in range(2, n - 1):
    cl, cr = b[i] / 2, (b[n] - b[i]) / 2
    ml = bisect.bisect_left(l, cl)
    mr = bisect.bisect_left(r, cr + b[i])
    if ml == 0:
        x = b[1]
    elif ml == lenl:
        x = l[-1]
    else:
        if l[ml] - cl <= cl - l[ml - 1]:
            x = l[ml]
        else:
            x = l[ml - 1]
    if mr == 0:
        y = r[0]
    elif mr == lenr:
        y = b[-2]
    else:
        if r[mr] - (cr + b[i]) <= (cr + b[i]) - r[mr - 1]:
            y = r[mr]
        else:
            y = r[mr - 1]
    s = [x, b[i] - x, y - b[i], b[n] - y]
    ans = min(ans, max(s) - min(s))
    l.append(b[i])
    r.popleft()
    lenl += 1
    lenr -= 1
print(ans)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
b = [0] * (n + 1)
for i in range(n):
    b[i + 1] = b[i] + a[i]
x = [[0, 0] for _ in range(n - 3)]
j = 1
for i in range(2, n - 1):
    while True:
        if i == j + 1:
            x[i - 2][0] = b[j]
            break
        if abs(b[i] - 2 * b[j]) > abs(b[i] - 2 * b[j + 1]):
            j += 1
        else:
            x[i - 2][0] = b[j]
            break
j = n - 1
for i in range(n - 2, 1, -1):
    while True:
        if i == j - 1:
            x[i - 2][1] = b[j]
            break
        if abs(b[n] + b[i] - 2 * b[j]) > abs(b[n] + b[i] - 2 * b[j - 1]):
            j -= 1
        else:
            x[i - 2][1] = b[j]
            break
ans = 1145141919810
for i in range(n - 3):
    s = [x[i][0], b[i + 2] - x[i][0], x[i][1] - b[i + 2], b[n] - x[i][1]]
    ans = min(ans, max(s) - min(s))
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque
import bisect

n = int(input())
a = list(map(int, input().split()))
b = [0] * (n + 1)
for i in range(n):
    b[i + 1] = b[i] + a[i]
l, r = deque([b[1]]), deque(b[3 : n])
lenl, lenr = len(l), len(r)
ans = 1145141919810
for i in range(2, n - 1):
    cl, cr = b[i] / 2, (b[n] - b[i]) / 2
    ml = bisect.bisect_left(l, cl)
    mr = bisect.bisect_left(r, cr + b[i])
    if ml == 0:
        x = b[1]
    elif ml == lenl:
        x = l[-1]
    else:
        if l[ml] - cl <= cl - l[ml - 1]:
            x = l[ml]
        else:
            x = l[ml - 1]
    if mr == 0:
        y = r[0]
    elif mr == lenr:
        y = b[-2]
    else:
        if r[mr] - (cr + b[i]) <= (cr + b[i]) - r[mr - 1]:
            y = r[mr]
        else:
            y = r[mr - 1]
    s = [x, b[i] - x, y - b[i], b[n] - y]
    ans = min(ans, max(s) - min(s))
    l.append(b[i])
    r.popleft()
    lenl += 1
    lenr -= 1
print(ans)

Fixed submission:
n = int(input())
a = list(map(int, input().split()))
b = [0] * (n + 1)
for i in range(n):
    b[i + 1] = b[i] + a[i]
x = [[0, 0] for _ in range(n - 3)]
j = 1
for i in range(2, n - 1):
    while True:
        if i == j + 1:
            x[i - 2][0] = b[j]
            break
        if abs(b[i] - 2 * b[j]) > abs(b[i] - 2 * b[j + 1]):
            j += 1
        else:
            x[i - 2][0] = b[j]
            break
j = n - 1
for i in range(n - 2, 1, -1):
    while True:
        if i == j - 1:
            x[i - 2][1] = b[j]
            break
        if abs(b[n] + b[i] - 2 * b[j]) > abs(b[n] + b[i] - 2 * b[j - 1]):
            j -= 1
        else:
            x[i - 2][1] = b[j]
            break
ans = 1145141919810
for i in range(n - 3):
    s = [x[i][0], b[i + 2] - x[i][0], x[i][1] - b[i + 2], b[n] - x[i][1]]
    ans = min(ans, max(s) - min(s))
print(ans)