Buggy python submission (verdict: Runtime Error):
# -*- coding: utf-8 -*-
from scratch import two_pointers
from operator import add, lt
from itertools import accumulate

n = int(input())
a = [int(_) for _ in input().split()]
cul_ary = [0] + list(accumulate(a, add))

l_split = 1
r_split = 3
griefs = []

for i in range(2, n - 1):
    l_split, l_diff, p, q = two_pointers(cul_ary,
                                         0,
                                         i,
                                         l_split,
                                         lambda ary, l, r: ary[r] - ary[l],
                                         lambda left, right: abs(left - right),
                                         lambda val1, val2: True if val1 > val2 else False)

    r_split, r_diff, r, s = two_pointers(cul_ary,
                                         i,
                                         n,
                                         r_split,
                                         lambda ary, l, r: ary[r] - ary[l],
                                         lambda left, right: abs(left - right),
                                         lambda val1, val2: True if val1 > val2 else False)

    griefs.append(max(p, q, r, s) - min(p, q, r, s))

print(min(griefs))


Fixed submission:
# -*- coding: utf-8 -*-
n = int(input())
a = [int(_) for _ in input().split()]

# my initial trial below ##############################
# from itertools import combinations
# combs = combinations(range(1, n), 3)
# grief = 10**10
# for comb in combs:
#     p = sum(a[:comb[0]])
#     q = sum(a[comb[0]:comb[1]])
#     r = sum(a[comb[1]:comb[2]])
#     s = sum(a[comb[2]:])

#     tmp = max(p, q, r, s) - min(p, q, r, s)
#     if tmp < grief:
#         grief = tmp

# print(grief)  # running time limit error

# solution below ######################################

cumul_sum = [0]
for i, ai in enumerate(a):
    cumul_sum.append(ai + cumul_sum[i])


def cal_sum(left, right):
    return cumul_sum[right] - cumul_sum[left]


def two_pointers(l, r, split, diff):
    for i in range(split, r):
        tmp_diff = abs(cal_sum(l, i) - cal_sum(i, r))
        if diff >= tmp_diff:
            diff = tmp_diff
        else:
            return i - 1, diff
    else:
        return r - 1, diff


l_split = 1
r_split = 3
griefs = []
for i in range(2, n - 1):
    # left
    l_split, l_diff = two_pointers(0, i, l_split, 10**12)
    # right
    r_split, r_diff = two_pointers(i, n, r_split, 10**12)

    p, q, r, s = cal_sum(0, l_split), cal_sum(l_split,
                                              i), cal_sum(i, r_split), cal_sum(
                                                  r_split, n)
    griefs.append(max(p, q, r, s) - min(p, q, r, s))


print(min(griefs))

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
from itertools import combinations

n = int(input())
a = [int(_) for _ in input().split()]

combs = combinations(range(1, n), 3)

grief = 10**10
for comb in combs:
    p = sum(a[:comb[0]])
    q = sum(a[comb[0]:comb[1]])
    r = sum(a[comb[1]:comb[2]])
    s = sum(a[comb[2]:])

    tmp = max(p, q, r, s) - min(p, q, r, s)
    if tmp < grief:
        grief = tmp

print(grief)


Fixed submission:
# -*- coding: utf-8 -*-
n = int(input())
a = [int(_) for _ in input().split()]

# my initial trial below ##############################
# from itertools import combinations
# combs = combinations(range(1, n), 3)
# grief = 10**10
# for comb in combs:
#     p = sum(a[:comb[0]])
#     q = sum(a[comb[0]:comb[1]])
#     r = sum(a[comb[1]:comb[2]])
#     s = sum(a[comb[2]:])

#     tmp = max(p, q, r, s) - min(p, q, r, s)
#     if tmp < grief:
#         grief = tmp

# print(grief)  # running time limit error

# solution below ######################################

cumul_sum = [0]
for i, ai in enumerate(a):
    cumul_sum.append(ai + cumul_sum[i])


def cal_sum(left, right):
    return cumul_sum[right] - cumul_sum[left]


def two_pointers(l, r, split, diff):
    for i in range(split, r):
        tmp_diff = abs(cal_sum(l, i) - cal_sum(i, r))
        if diff >= tmp_diff:
            diff = tmp_diff
        else:
            return i - 1, diff
    else:
        return r - 1, diff


l_split = 1
r_split = 3
griefs = []
for i in range(2, n - 1):
    # left
    l_split, l_diff = two_pointers(0, i, l_split, 10**12)
    # right
    r_split, r_diff = two_pointers(i, n, r_split, 10**12)

    p, q, r, s = cal_sum(0, l_split), cal_sum(l_split,
                                              i), cal_sum(i, r_split), cal_sum(
                                                  r_split, n)
    griefs.append(max(p, q, r, s) - min(p, q, r, s))


print(min(griefs))