Problem p03310 (python)

Fixed submission:
import bisect
INF = 10**16
n=int(input())
a=list(map(int,input().split()))
la=[0]
for i in range(n):
    la.append(la[i]+a[i])
la.pop(0)
sa = la[-1]
l=0
r=2
res = INF
for i in range(1,n-2):
    while abs(la[l+1]-(la[i]-la[l+1])) < abs(la[l]-(la[i]-la[l])):
        l += 1

    if r == i:
        r += 1
    while abs((la[r+1] - la[i])-(sa - la[r+1])) < abs((la[r] - la[i])-(sa - la[r])):
        r += 1
    r1 = la[l]
    r2 = la[i] - la[l]
    r3 = la[r] - la[i]
    r4 = sa    - la[r]
    res=min(res,max(r1,r2,r3,r4)-min(r1,r2,r3,r4))
print(res)

Buggy submission (verdict: Runtime Error):
import bisect
n=int(input())
a=list(map(int,input().split()))
la=[0,0,0,0]
for i in range(n):
    la.append(la[i]+a[i])
sa = la[-1]
ma = sa // 2
def ans(r):
    return max(r) - min(r)
def ans2(r1,r2,r3,r4):
    return max(r1,r2,r3,r4) - min(r1,r2,r3,r4)

ca = bisect.bisect_left(la,ma)

ha1 = la[:ca]
ha2 = la[ca:]

cha1 = bisect.bisect_left(ha1,ha1[-1]//2)
r1=[0,0]
r1[0] = ha1[cha1]
r1[1] = ha1[-1] - r1[0]
r2=[0,0]
r2[0] = ha1[cha1-1]
r2[1] = ha1[-1] - r2[0]

tminus = ha1[-1]
for i in range(len(ha2)):
    ha2[i] -= tminus
cha2 = bisect.bisect_left(ha2,ha2[-1]//2)
r3=[0,0]
r3[0] = ha2[cha2]
r3[1] = ha2[-1] - r3[0]
r4=[0,0]
r4[0] = ha2[cha2-1]
r4[1] = ha2[-1] - r4[0]

res1 = min(ans(r1+r3),ans(r1+r4),ans(r2+r3),ans(r2+r4))

ha1 = la[:ca-1]
ha2 = la[ca-1:]

cha1 = bisect.bisect_left(ha1,ha1[-1]//2)
r1=[0,0]
r1[0] = ha1[cha1]
r1[1] = ha1[-1] - r1[0]
r2=[0,0]
r2[0] = ha1[cha1-1]
r2[1] = ha1[-1] - r2[0]

tminus = ha1[-1]
for i in range(len(ha2)):
    ha2[i] -= tminus
cha2 = bisect.bisect_left(ha2,ha2[-1]//2)
r3=[0,0]
r3[0] = ha2[cha2]
r3[1] = ha2[-1] - r3[0]
r4=[0,0]
r4[0] = ha2[cha2-1]
r4[1] = ha2[-1] - r4[0]

res2 = min(ans(r1+r3),ans(r1+r4),ans(r2+r3),ans(r2+r4))

ha1 = la[:ca+1]
ha2 = la[ca+1:]

cha1 = bisect.bisect_left(ha1,ha1[-1]//2)
r1=[0,0]
r1[0] = ha1[cha1]
r1[1] = ha1[-1] - r1[0]
r2=[0,0]
r2[0] = ha1[cha1-1]
r2[1] = ha1[-1] - r2[0]

tminus = ha1[-1]
for i in range(len(ha2)):
    ha2[i] -= tminus
cha2 = bisect.bisect_left(ha2,ha2[-1]//2)
r3=[0,0]
r3[0] = ha2[cha2]
r3[1] = ha2[-1] - r3[0]
r4=[0,0]
r4[0] = ha2[cha2-1]
r4[1] = ha2[-1] - r4[0]

res3 = min(ans(r1+r3),ans(r1+r4),ans(r2+r3),ans(r2+r4))


print(min(res1,res2,res3))

Buggy submission (verdict: Runtime Error):
import bisect
n=int(input())
a=list(map(int,input().split()))
la=[0]
for i in range(n):
    la.append(la[i]+a[i])
sa = la[-1]
ma = sa // 2
def ans(r):
    return max(r) - min(r)
def ans2(r1,r2,r3,r4):
    return max(r1,r2,r3,r4) - min(r1,r2,r3,r4)

ca = bisect.bisect_left(la,ma)

ha1 = la[:ca]
ha2 = la[ca:]

cha1 = bisect.bisect_left(ha1,ha1[-1]//2)
r1=[0,0]
r1[0] = ha1[cha1]
r1[1] = ha1[-1] - r1[0]
r2=[0,0]
r2[0] = ha1[cha1-1]
r2[1] = ha1[-1] - r2[0]

tminus = ha1[-1]
for i in range(len(ha2)):
    ha2[i] -= tminus
cha2 = bisect.bisect_left(ha2,ha2[-1]//2)
r3=[0,0]
r3[0] = ha2[cha2]
r3[1] = ha2[-1] - r3[0]
r4=[0,0]
r4[0] = ha2[cha2-1]
r4[1] = ha2[-1] - r4[0]

res1 = min(ans(r1+r3),ans(r1+r4),ans(r2+r3),ans(r2+r4))

ha1 = la[:ca-1]
ha2 = la[ca-1:]

cha1 = bisect.bisect_left(ha1,ha1[-1]//2)
r1=[0,0]
r1[0] = ha1[cha1]
r1[1] = ha1[-1] - r1[0]
r2=[0,0]
r2[0] = ha1[cha1-1]
r2[1] = ha1[-1] - r2[0]

tminus = ha1[-1]
for i in range(len(ha2)):
    ha2[i] -= tminus
cha2 = bisect.bisect_left(ha2,ha2[-1]//2)
r3=[0,0]
r3[0] = ha2[cha2]
r3[1] = ha2[-1] - r3[0]
r4=[0,0]
r4[0] = ha2[cha2-1]
r4[1] = ha2[-1] - r4[0]

res2 = min(ans(r1+r3),ans(r1+r4),ans(r2+r3),ans(r2+r4))

ha1 = la[:ca+1]
ha2 = la[ca+1:]

cha1 = bisect.bisect_left(ha1,ha1[-1]//2)
r1=[0,0]
r1[0] = ha1[cha1]
r1[1] = ha1[-1] - r1[0]
r2=[0,0]
r2[0] = ha1[cha1-1]
r2[1] = ha1[-1] - r2[0]

tminus = ha1[-1]
for i in range(len(ha2)):
    ha2[i] -= tminus
cha2 = bisect.bisect_left(ha2,ha2[-1]//2)
r3=[0,0]
r3[0] = ha2[cha2]
r3[1] = ha2[-1] - r3[0]
r4=[0,0]
r4[0] = ha2[cha2-1]
r4[1] = ha2[-1] - r4[0]

res3 = min(ans(r1+r3),ans(r1+r4),ans(r2+r3),ans(r2+r4))


print(min(res1,res2,res3))