Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3

from itertools import accumulate

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

a_cumsum = list(accumulate(a))
a_cumsum_rev = list(reversed(list(accumulate(reversed(a)))))

# print(a_cumsum)
# print(a_cumsum_rev)

ans = 10**12
div = 1
while(1):
    # print(left, right)
    pq = a_cumsum[0:div+1]
    pq_rev = a_cumsum_rev[0:div+1]
    diff_pq = a_cumsum_rev[div+1]

    rs = a_cumsum[div+1:]
    rs_rev = a_cumsum_rev[div+1:]
    diff_rs = a_cumsum[div]
    # print(pq)
    # print(rs)

    dis = 10**12
    for j in range(len(pq)-1):
        dis_tmp = abs(pq[j]-pq_rev[j+1]+diff_pq)
        if dis_tmp < dis:
            dis = dis_tmp
            p, q = pq[j], pq_rev[j+1]-diff_pq

    dis = 10**12
    for j in range(len(rs)-1):
        dis_tmp = abs(rs[j]-rs_rev[j+1]-diff_rs)
        if dis_tmp < dis:
            dis = dis_tmp
            r, s = rs[j]-diff_rs, rs_rev[j+1]

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

    div += 1
    if div == n-2:
        break
    # print()
print(ans)


Fixed submission:
#!/usr/bin/env python3

from itertools import accumulate, islice
from bisect import bisect_left

n = int(input())
xs = list(map(int, input().split()))

ys = list(accumulate(xs))
zs, total = ys[:-1], ys[-1]

res = max(xs) - min(xs)
for i, z in islice(enumerate(zs), 1, n-1):
    j = bisect_left(zs, z//2)
    splits = ((zs[j+k], z - zs[j+k]) for k in (-1, 0, 1) if 0 <= j+k <= i)
    a, b = min(splits, key=lambda s: abs(s[0]-s[1]))

    j = bisect_left(zs, (total - z)//2 + z)
    splits = ((zs[j+k] - z, total - zs[j+k])
              for k in (-1, 0, 1) if i <= j+k <= n-2)
    c, d = min(splits, key=lambda s: abs(s[0]-s[1]))

    minn, _, _, maxx = sorted((a, b, c, d))
    if maxx - minn < res:
        res = maxx - minn

print(res)

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3

import numpy as np
n = int(input())
a = list(map(int, input().split()))

a_cumsum = np.cumsum(a)
a_cumsum_rev = np.flip(np.cumsum(list(reversed(a))))


ans = 10**12
left = 0
right = -1
while(1):
    # print(left, right)
    p = a_cumsum[left]
    s = a_cumsum_rev[right]

    qr = a_cumsum[left+1:right] - p
    qr_rev = a_cumsum_rev[left+1:right] - s
    # print(qr)
    # print(qr_rev)

    dis = 10**12
    for j in range(len(qr)-1):
        if abs(qr[j]-qr_rev[j+1]) < dis:
            dis = abs(qr[j]-qr_rev[j+1])
            q, r = qr[j], qr_rev[j+1]
    # print(p, q, r, s)
    ans_tmp = max(p, q, r, s) - min(p, q, r, s)
    ans = min(ans, ans_tmp)

    tmp_left = a_cumsum[left+1]
    tmp_right = a_cumsum_rev[right-1]

    rest = n-(left+1-right)
    if rest == 2:
        break

    if abs(a_cumsum[left+1]-s) <= abs(p-a_cumsum_rev[right-1]):
        left += 1
    else:
        right -= 1
    # print()
print(ans)


Fixed submission:
#!/usr/bin/env python3

from itertools import accumulate, islice
from bisect import bisect_left

n = int(input())
xs = list(map(int, input().split()))

ys = list(accumulate(xs))
zs, total = ys[:-1], ys[-1]

res = max(xs) - min(xs)
for i, z in islice(enumerate(zs), 1, n-1):
    j = bisect_left(zs, z//2)
    splits = ((zs[j+k], z - zs[j+k]) for k in (-1, 0, 1) if 0 <= j+k <= i)
    a, b = min(splits, key=lambda s: abs(s[0]-s[1]))

    j = bisect_left(zs, (total - z)//2 + z)
    splits = ((zs[j+k] - z, total - zs[j+k])
              for k in (-1, 0, 1) if i <= j+k <= n-2)
    c, d = min(splits, key=lambda s: abs(s[0]-s[1]))

    minn, _, _, maxx = sorted((a, b, c, d))
    if maxx - minn < res:
        res = maxx - minn

print(res)