Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
tmp = 0
lr = [0]
for a in A:
    tmp += a
    lr.append(tmp)

ans = float('inf')


for i in range(2, N - 1):
    f=1
    g=i+1
    while abs(lr[i] - lr[f] - lr[f]) > abs(lr[i] - lr[f + 1] - lr[f + 1]):
        f += 1
    while abs((lr[-1] - lr[g]) - (lr[g] - lr[i])) > abs((lr[-1] - lr[g + 1]) - (lr[g + 1] - lr[i])):
        g += 1
    l = (lr[f], lr[i] - lr[f], lr[-1] - lr[g], lr[g] - lr[i])
    ans = min(ans, max(l) - min(l))

print(ans)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
tmp = 0
lr = [0]
for a in A:
    tmp += a
    lr.append(tmp)

ans = float('inf')

f=1
g=3
for i in range(2, N - 1):

    while abs(lr[i] - lr[f] - lr[f]) > abs(lr[i] - lr[f + 1] - lr[f + 1]):
        f += 1
    while abs((lr[-1] - lr[g]) - (lr[g] - lr[i])) > abs((lr[-1] - lr[g + 1]) - (lr[g + 1] - lr[i])):
        g += 1
    l = (lr[f], lr[i] - lr[f], lr[-1] - lr[g], lr[g] - lr[i])
    ans = min(ans, max(l) - min(l))

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def sum(ar,st,en):
    s=0
    for i in range(st,en+1):
        s+=ar[i]
    return s
max_value=999999999999999999999

def k_part(k,n,ar):
    res=max_value
    for o in range(n):
        l_min=0
        for r_min in range(l_min,n):
            min_s=sum(ar,l_min,r_min)
            dp=[[max_value for i in range(n)] for j in range(k)]
            dp[0][r_min-l_min]=min_s
            for p in range(1,k):
                for l_max in range(r_min,n):
                    for r_max in range(l_max,n):
                        max_s=sum(ar,l_max+1,r_max)
                        if max_s>=min_s:
                            dp[p][r_max]=min(dp[p][r_max],max(max_s,dp[p-1][l_max]))
            if dp[k-1][n-1]==max_value:
                continue
            res=min(res,dp[k-1][n-1]-min_s)
        tmp=[0]*n
        for i in range(n):
            tmp[(i-1+n)%n]=ar[i]
        for i in range(n):
            ar[i]=tmp[i]
    return res

k=4
n=int(input())
deps=[int(i) for i in input().strip().split(" ")]
print(k_part(k,n,deps))

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
tmp = 0
lr = [0]
for a in A:
    tmp += a
    lr.append(tmp)

ans = float('inf')

f=1
g=3
for i in range(2, N - 1):

    while abs(lr[i] - lr[f] - lr[f]) > abs(lr[i] - lr[f + 1] - lr[f + 1]):
        f += 1
    while abs((lr[-1] - lr[g]) - (lr[g] - lr[i])) > abs((lr[-1] - lr[g + 1]) - (lr[g + 1] - lr[i])):
        g += 1
    l = (lr[f], lr[i] - lr[f], lr[-1] - lr[g], lr[g] - lr[i])
    ans = min(ans, max(l) - min(l))

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def sum(ar,st,en):
    s=0
    for i in range(st,en+1):
        s+=ar[i]
    return s
max_value=999999999999999999999

def k_part(k,n,ar):
    res=max_value
    for o in range(n):
        l_min=0
        for r_min in range(l_min,n):
            min_s=sum(ar,l_min,r_min)
            dp=[[max_value for i in range(n)] for j in range(k)]
            dp[0][r_min-l_min]=min_s
            for p in range(1,k):
                for l_max in range(r_min,n):
                    for r_max in range(l_max,n):
                        max_s=sum(ar,l_max+1,r_max)
                        if max_s>=min_s:
                            dp[p][r_max]=min(dp[p][r_max],max(max_s,dp[p-1][l_max]))
            if dp[k-1][n-1]==max_value:
                continue
            res=min(res,dp[k-1][n-1]-min_s)
        tmp=[0]*n
        for i in range(n):
            tmp[(i-1+n)%n]=ar[i]
        for i in range(n):
            ar[i]=tmp[i]
    return res

k=4
n=int(input())
deps=[int(i) for i in input().strip().split(" ")]
print(k_part(k,n,deps))

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
tmp = 0
lr = [0]
for a in A:
    tmp += a
    lr.append(tmp)

ans = float('inf')

f=1
g=3
for i in range(2, N - 1):

    while abs(lr[i] - lr[f] - lr[f]) > abs(lr[i] - lr[f + 1] - lr[f + 1]):
        f += 1
    while abs((lr[-1] - lr[g]) - (lr[g] - lr[i])) > abs((lr[-1] - lr[g + 1]) - (lr[g + 1] - lr[i])):
        g += 1
    l = (lr[f], lr[i] - lr[f], lr[-1] - lr[g], lr[g] - lr[i])
    ans = min(ans, max(l) - min(l))

print(ans)