Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().rstrip().split(" ")))

t = list(0 for i in range(n+1))
for i in range(1, n+1):
    t[i] = t[i-1] + a[i-1]

def sum_a(i, j):
    return t[j]-t[i]

min_dif = sum_a(0, n)

for i in range(2, n-1):
    j0 = 0
    j1 = i
    u = sum_a(0, i) // 2
    while j1 - j0 > 1:
        j = (j0+j1) // 2
        if sum_a(0, j) <= u: 
            j0 = j
        else:
            j1 = j
    if abs(sum_a(0, j0)-sum_a(j0, i)) < abs(sum_a(0, j1) - sum_a(j1, i)): 
        p = sum_a(0, j0)
        q = sum_a(j0, i)
    else:
        p = sum_a(0, j1)
        q = sum_a(j1, i)

    j0 = i
    j1 = n
    u = sum_a(i, n) // 2
    while j1 - j0 > 1:
        j = (j0+j1) // 2
        if sum_a(i, j) <= u: 
            j0 = j
        else:
            j1 = j
    if abs(sum_a(i, j0)-sum_a(j0, n)) < abs(sum_a(i, j1) - sum_a(j1, n)): 
        r = sum_a(i, j0)
        s = sum_a(j0, n)
    else:
        r = sum_a(i, j1)
        s = sum_a(j1, n)

    min_dif = min(min_dif, max(p, q, r, s)-min(p, q, r, s))

print(min_dif)


Fixed submission:
n = int(input())
a = list(map(int, input().rstrip().split(" ")))

t = list(0 for i in range(n+1))
for i in range(1, n+1):
    t[i] = t[i-1] + a[i-1]

def sum_a(i, j):
    return t[j]-t[i]

min_dif = sum_a(0, n)

i = 0
k = 2
for j in range(2, n-1):
    u = sum_a(0, j) // 2
    while sum_a(0, i) < u:
        i = i + 1
    if abs(sum_a(0, i-1)-sum_a(i-1, j)) < abs(sum_a(0, i) - sum_a(i, j)): 
        i = i - 1
    p = sum_a(0, i)
    q = sum_a(i, j)

    u = sum_a(j, n) // 2
    while sum_a(j, k) < u:
        k = k + 1
    if abs(sum_a(j, k-1)-sum_a(k-1, n)) < abs(sum_a(j, k) - sum_a(k, n)): 
        k = k - 1
    r = sum_a(j, k)
    s = sum_a(k, n)

    min_dif = min(min_dif, max(p, q, r, s)-min(p, q, r, s))

print(min_dif)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().rstrip().split(" ")))

sm = 0
for i in range(0, n):
    sm = sm + a[i]

min_dif = sm

p = 0
for i in range(n-3):
    p = p + a[i]
    if 4*p < sm:
        next
    q = 0
    for j in range(i+1, n-2):
        q = q + a[j]
        if 3*q < sm - p:
            next
        r = 0
        for k in range(j+1, n-1):
            r = r + a[k]
            if 2*r < sm - p - q:
                next
            s = 0
            for l in range(k+1, n):
                s = s + a[l]
            d = max([p, q, r, s])-min([p, q, r, s])
            min_dif = min([d, min_dif])

p = 0
for i in range(n-3):
    p = p + a[i]
    if 4*p <= sm - 3*min_dif or 4*p >= sm + 3*min_dif:
        next
    q = 0
    for j in range(i+1, n-2):
        q = q + a[j]
        if abs(p-q) >= min_dif or 4*q <= sm - 3*min_dif or 4*q >= sm + 3*min_dif:
            next
        r = 0
        for k in range(j+1, n-1):
            r = r + a[k]
            if abs(p-r) >= min_dif or abs(q-r) >= min_dif or 4*r <= sm - 3*min_dif or 4*r >= sm + 3*min_dif:
                next
            s = 0
            for l in range(k+1, n):
                s = s + a[l]
            d = max([p, q, r, s])-min([p, q, r, s])
            min_dif = min([d, min_dif])

print(min_dif)


Fixed submission:
n = int(input())
a = list(map(int, input().rstrip().split(" ")))

t = list(0 for i in range(n+1))
for i in range(1, n+1):
    t[i] = t[i-1] + a[i-1]

def sum_a(i, j):
    return t[j]-t[i]

min_dif = sum_a(0, n)

i = 0
k = 2
for j in range(2, n-1):
    u = sum_a(0, j) // 2
    while sum_a(0, i) < u:
        i = i + 1
    if abs(sum_a(0, i-1)-sum_a(i-1, j)) < abs(sum_a(0, i) - sum_a(i, j)): 
        i = i - 1
    p = sum_a(0, i)
    q = sum_a(i, j)

    u = sum_a(j, n) // 2
    while sum_a(j, k) < u:
        k = k + 1
    if abs(sum_a(j, k-1)-sum_a(k-1, n)) < abs(sum_a(j, k) - sum_a(k, n)): 
        k = k - 1
    r = sum_a(j, k)
    s = sum_a(k, n)

    min_dif = min(min_dif, max(p, q, r, s)-min(p, q, r, s))

print(min_dif)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().rstrip().split(" ")))

sm = 0
for i in range(0, n):
    sm = sm + a[i]

min_dif = sm

p = 0
for i in range(n-3):
    p = p + a[i]
    if 4*p < sm:
        next
    q = 0
    for j in range(i+1, n-2):
        q = q + a[j]
        if 3*q < sm - p:
            next
        r = 0
        for k in range(j+1, n-1):
            r = r + a[k]
            if 2*r < sm - p - q:
                next
            s = 0
            for l in range(k+1, n):
                s = s + a[l]
            d = max([p, q, r, s])-min([p, q, r, s])
            min_dif = min([d, min_dif])

p = 0
for i in range(n-3):
    p = p + a[i]
    if 4*p <= sm - 3*min_dif or 4*p >= sm + 3*min_dif:
        next
    q = 0
    for j in range(i+1, n-2):
        q = q + a[j]
        if 4*q <= sm - 3*min_dif or 4*q >= sm + 3*min_dif:
            next
        r = 0
        for k in range(j+1, n-1):
            r = r + a[k]
            if 4*r <= sm - 3*min_dif or 4*r >= sm + 3*min_dif:
                next
            s = 0
            for l in range(k+1, n):
                s = s + a[l]
            d = max([p, q, r, s])-min([p, q, r, s])
            min_dif = min([d, min_dif])

print(min_dif)


Fixed submission:
n = int(input())
a = list(map(int, input().rstrip().split(" ")))

t = list(0 for i in range(n+1))
for i in range(1, n+1):
    t[i] = t[i-1] + a[i-1]

def sum_a(i, j):
    return t[j]-t[i]

min_dif = sum_a(0, n)

i = 0
k = 2
for j in range(2, n-1):
    u = sum_a(0, j) // 2
    while sum_a(0, i) < u:
        i = i + 1
    if abs(sum_a(0, i-1)-sum_a(i-1, j)) < abs(sum_a(0, i) - sum_a(i, j)): 
        i = i - 1
    p = sum_a(0, i)
    q = sum_a(i, j)

    u = sum_a(j, n) // 2
    while sum_a(j, k) < u:
        k = k + 1
    if abs(sum_a(j, k-1)-sum_a(k-1, n)) < abs(sum_a(j, k) - sum_a(k, n)): 
        k = k - 1
    r = sum_a(j, k)
    s = sum_a(k, n)

    min_dif = min(min_dif, max(p, q, r, s)-min(p, q, r, s))

print(min_dif)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().rstrip().split(" ")))

sm = 0
for i in range(0, n):
    sm = sm + a[i]

min_dif = sm

p = 0
for i in range(n-3):
    p = p + a[i]
    if 4*p <= sm - 3*min_dif or 4*p >= sm + 3*min_dif:
        next
    q = 0
    for j in range(i+1, n-2):
        q = q + a[j]
        if 4*q <= sm - 3*min_dif or 4*q >= sm + 3*min_dif:
            next
        r = 0
        for k in range(j+1, n-1):
            r = r + a[k]
            if 4*r <= sm - 3*min_dif or 4*r >= sm + 3*min_dif:
                next
            s = 0
            for l in range(k+1, n):
                s = s + a[l]
            d = max([p, q, r, s])-min([p, q, r, s])
            min_dif = min([d, min_dif])

print(min_dif)


Fixed submission:
n = int(input())
a = list(map(int, input().rstrip().split(" ")))

t = list(0 for i in range(n+1))
for i in range(1, n+1):
    t[i] = t[i-1] + a[i-1]

def sum_a(i, j):
    return t[j]-t[i]

min_dif = sum_a(0, n)

i = 0
k = 2
for j in range(2, n-1):
    u = sum_a(0, j) // 2
    while sum_a(0, i) < u:
        i = i + 1
    if abs(sum_a(0, i-1)-sum_a(i-1, j)) < abs(sum_a(0, i) - sum_a(i, j)): 
        i = i - 1
    p = sum_a(0, i)
    q = sum_a(i, j)

    u = sum_a(j, n) // 2
    while sum_a(j, k) < u:
        k = k + 1
    if abs(sum_a(j, k-1)-sum_a(k-1, n)) < abs(sum_a(j, k) - sum_a(k, n)): 
        k = k - 1
    r = sum_a(j, k)
    s = sum_a(k, n)

    min_dif = min(min_dif, max(p, q, r, s)-min(p, q, r, s))

print(min_dif)