Buggy python submission (verdict: Time Limit Exceeded):
import math

n = int(input())
a = [int(i) for i in input().split(" ")]
ar = a[::-1]
a = [0] + a
ar = [0] + ar
s = [0] * (n + 1)
sr = [0] * (n + 1)

for i in range(1, n + 1):
    s[i] = a[i] + s[i - 1]
    sr[i] = ar[i] + sr[i - 1]
    
def bs(s, l, r, rr):
    if l == r:
        return l
    else:
        h = math.ceil((l + r) / 2)
        if s[h] * 2 > s[rr]:
            return bs(s, l, h - 1, rr)
        else:
            return bs(s, h, r, rr)
        
def min_2(s, i, rr):
    a = abs(s[i] - (s[rr] - s[i]))
    b = abs(s[i + 1] - (s[rr] - s[i + 1]))
    if a <= b:
        return i
    else:
        return i + 1
    
def f(a):
    return max(a) - min(a)

m_min = 2 * 10**14
for i in range(2, n - 1):
    pq = bs(s, 1, i, i)
    rs = bs(sr, 1, n - i, n - i)
    pq = min_2(s, pq, i)
    rs = min_2(sr, rs, n - i)
    m = f([s[pq], s[i] - s[pq], sr[rs], sr[n - i] - sr[rs]])
    if m < m_min:
        m_min = m
        
print(m_min)

Fixed submission:
import math

n = int(input())

a = [int(i) for i in input().split(" ")]
ar = a[::-1]
a = [0] + a
ar = [0] + ar
s = [0] * (n + 1)
sr = [0] * (n + 1)
li = 0
ri = n

for i in range(1, n + 1):
    s[i] = a[i] + s[i - 1]
    sr[i] = ar[i] + sr[i - 1]
    
def f(a):
    return max(a) - min(a)

def fs(rr):
    global li
    while abs(s[rr] - 2 * s[li]) > abs(s[rr] - 2 * s[li + 1]):
        li += 1
        
def fsr(rr):
    global ri
    while abs(sr[rr] - 2 * sr[ri]) > abs(sr[rr] - 2 * sr[ri - 1]):
        ri -= 1

m_min = 2 * 10**14
for i in range(2, n - 1):
    fs(i)
    fsr(n - i)
    m = f([s[li], s[i] - s[li], sr[ri], sr[n - i] - sr[ri]])
    if m < m_min:
        m_min = m
        
print(m_min)

Buggy python submission (verdict: Time Limit Exceeded):
import math

n = int(input())
a = [int(i) for i in input().split(" ")]
ar = a[::-1]
a = [0] + a
ar = [0] + ar
s = [0] * (n + 1)
sr = [0] * (n + 1)

for i in range(1, n + 1):
    s[i] = a[i] + s[i - 1]
    sr[i] = ar[i] + sr[i - 1]
    
def bs(s, l, r, rr):
    if l == r:
        return l
    else:
        h = math.ceil((l + r) / 2)
        if s[h] * 2 > s[rr]:
            return bs(s, l, h - 1, rr)
        else:
            return bs(s, h, r, rr)
        
def min_2(s, i, rr):
    a = abs(s[i] - (s[rr] - s[i]))
    b = abs(s[i + 1] - (s[rr] - s[i + 1]))
    if a <= b:
        return i
    else:
        return i + 1
    
def f(a):
    return max(a) - min(a)

m_min = 2 * 10**14
for i in range(2, n - 1):
    pq = bs(s, 1, i, i)
    rs = bs(sr, 1, n - i, n - i)
    pq = min_2(s, pq, i)
    rs = min_2(sr, rs, n - i)
    m = f([s[pq], s[i] - s[pq], sr[rs], sr[n - i] - sr[rs]])
    if m < m_min:
        m_min = m
        
print(m_min)

Fixed submission:
import math

n = int(input())

a = [int(i) for i in input().split(" ")]
ar = a[::-1]
a = [0] + a
ar = [0] + ar
s = [0] * (n + 1)
sr = [0] * (n + 1)
li = 0
ri = n

for i in range(1, n + 1):
    s[i] = a[i] + s[i - 1]
    sr[i] = ar[i] + sr[i - 1]
    
def f(a):
    return max(a) - min(a)

def fs(rr):
    global li
    while abs(s[rr] - 2 * s[li]) > abs(s[rr] - 2 * s[li + 1]):
        li += 1
        
def fsr(rr):
    global ri
    while abs(sr[rr] - 2 * sr[ri]) > abs(sr[rr] - 2 * sr[ri - 1]):
        ri -= 1

m_min = 2 * 10**14
for i in range(2, n - 1):
    fs(i)
    fsr(n - i)
    m = f([s[li], s[i] - s[li], sr[ri], sr[n - i] - sr[ri]])
    if m < m_min:
        m_min = m
        
print(m_min)