Buggy python submission (verdict: Time Limit Exceeded):
def main():
    from itertools import accumulate, product

    def binary_search(a, x, lo, hi):
        while lo < hi:
            mid = (lo + hi) // 2
            if a[mid] < x:
                lo = mid + 1
            else:
                hi = mid
        return lo

    n = int(input())
    *a, = map(int, input().split())
    acc = (0,) + tuple(accumulate(a))

    # [0,i), [i,j), [j,k), [k, n)

    ret = acc[-1]
    for j in range(2, n - 1):
        left = acc[j]
        right = acc[-1] - left

        i = binary_search(acc, left // 2, 2, j + 2)
        k = binary_search(acc, left + (right // 2), j + 2, n + 1)

        li = i - 1, i
        rk = k - 1, k

        for i, k in product(li, rk):
            if i <= 0 or k <= j:
                continue
            pqrs = acc[i], left - acc[i], acc[k] - left, acc[-1] - acc[k]
            ret = min(
                ret,
                max(pqrs) - min(pqrs)
            )

    print(ret)


if __name__ == '__main__':
    main()


Fixed submission:
from bisect import bisect_left
from itertools import accumulate

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

cumsum = (0,) + tuple(accumulate(a))
# print(cumsum)

# PQ := [0, cut)
# RS = [cut, n)

ans = cumsum[-1]
for cut in range(2, n - 1):
    # cumsum[cut] : PQ区間和
    # cumsum[lc], cumsum[lc_1] : P区間和の候補
    # cumsum[n] - cumsum[cut] : RS区間和
    # cumsum[rc], cumsum[rc_1] : R区間和 + sPQの候補

    sPQ = cumsum[cut]
    sRS = cumsum[n] - cumsum[cut]

    lc = bisect_left(cumsum, sPQ // 2)
    # x以上で最小, 累積和のindexは、aの1-indexedに対応
    lc_1 = lc - 1
    # x未満で最大

    if abs((sPQ - cumsum[lc]) - cumsum[lc]) < abs((sPQ - cumsum[lc_1]) - cumsum[lc_1]):
        sP = cumsum[lc]
        # print('sP', sP, lc, cumsum)
    else:
        sP = cumsum[lc_1]
        # print('sP', sP, lc_1, cumsum)
    sQ = sPQ - sP
    # print('sQ', sQ)

    rc = bisect_left(cumsum, sPQ + (sRS // 2))
    # x以上で最小, 累積和のindexは、aの1-indexedに対応
    rc_1 = rc - 1
    # x未満で最大

    if abs((sRS - (cumsum[rc] - sPQ)) - (cumsum[rc] - sPQ)) < abs((sRS - (cumsum[rc_1] - sPQ)) - (cumsum[rc_1] - sPQ)):
        sR = cumsum[rc] - sPQ
        # print('sR', sR, rc, cumsum)
    else:
        sR = cumsum[rc_1] - sPQ
        # print('sR', sR, rc_1, cumsum)
    sS = sRS - sR
    # print('sS', sS)

    t = (sP, sQ, sR, sS)

    ans = min(ans, max(t) - min(t))
    # print(t)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    from itertools import accumulate, product

    def binary_search(a, x, lo, hi):
        while abs(hi - lo) > 0:
            mid = (lo + hi) // 2
            if a[mid] < x:
                lo = mid + 1
            else:
                hi = mid
        return hi

    n = int(input())
    *a, = map(int, input().split())
    acc = (0,) + tuple(accumulate(a))

    # [0,i), [i,j), [j,k), [k, n)

    ret = acc[-1]
    for j in range(2, n - 1):
        left = acc[j]
        right = acc[-1] - left

        i = binary_search(acc, left // 2, 2, j + 2)
        k = binary_search(acc, left + (right // 2), j + 2, n + 1)

        li = i - 1, i
        rk = k - 1, k

        for i, k in product(li, rk):
            if i <= 0 or k <= j:
                continue
            pqrs = acc[i], left - acc[i], acc[k] - left, acc[-1] - acc[k]
            ret = min(
                ret,
                max(pqrs) - min(pqrs)
            )

    print(ret)


if __name__ == '__main__':
    main()


Fixed submission:
from bisect import bisect_left
from itertools import accumulate

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

cumsum = (0,) + tuple(accumulate(a))
# print(cumsum)

# PQ := [0, cut)
# RS = [cut, n)

ans = cumsum[-1]
for cut in range(2, n - 1):
    # cumsum[cut] : PQ区間和
    # cumsum[lc], cumsum[lc_1] : P区間和の候補
    # cumsum[n] - cumsum[cut] : RS区間和
    # cumsum[rc], cumsum[rc_1] : R区間和 + sPQの候補

    sPQ = cumsum[cut]
    sRS = cumsum[n] - cumsum[cut]

    lc = bisect_left(cumsum, sPQ // 2)
    # x以上で最小, 累積和のindexは、aの1-indexedに対応
    lc_1 = lc - 1
    # x未満で最大

    if abs((sPQ - cumsum[lc]) - cumsum[lc]) < abs((sPQ - cumsum[lc_1]) - cumsum[lc_1]):
        sP = cumsum[lc]
        # print('sP', sP, lc, cumsum)
    else:
        sP = cumsum[lc_1]
        # print('sP', sP, lc_1, cumsum)
    sQ = sPQ - sP
    # print('sQ', sQ)

    rc = bisect_left(cumsum, sPQ + (sRS // 2))
    # x以上で最小, 累積和のindexは、aの1-indexedに対応
    rc_1 = rc - 1
    # x未満で最大

    if abs((sRS - (cumsum[rc] - sPQ)) - (cumsum[rc] - sPQ)) < abs((sRS - (cumsum[rc_1] - sPQ)) - (cumsum[rc_1] - sPQ)):
        sR = cumsum[rc] - sPQ
        # print('sR', sR, rc, cumsum)
    else:
        sR = cumsum[rc_1] - sPQ
        # print('sR', sR, rc_1, cumsum)
    sS = sRS - sR
    # print('sS', sS)

    t = (sP, sQ, sR, sS)

    ans = min(ans, max(t) - min(t))
    # print(t)
print(ans)