Problem p02623 (python)

Fixed submission:
from bisect import *

N,M,K,*f = map(int, open(0).read().split())
A = f[:N]
B = f[N:]
csa = [0]
ans = 0
for i in range(N):
    csa.append(csa[i]+A[i])
csb = [0]
for i in range(M):
    csb.append(csb[i]+B[i])
for i in range(N+1):
    if csa[i] <= K:
        bs = bisect_right(csb,K-csa[i])
        ans = max(ans,i+bs-1)
    else:
        break
print(ans)

Buggy submission (verdict: Runtime Error):
from bisect import *

N,M,K,*f = map(int, open(0).read().split())
A = f[:N]
B = f[N:]
a_read = 0
b_read = 0
csa = [0]
for i in range(N):
    csa.append(csa[i]+A[i])
csb = [0]
for i in range(M):
    csb.append(csb[i]+B[i])
for i in range(N+M):
    if a_read == N:
        bs = bisect_right(csb,K-csa[N])
        b_read = bs - 1
        break
    elif b_read == M:
        bs = bisect_right(csa,K-csb[M])
        b_read = bs - 1
        break
    else:
        if A[a_read] < B[b_read]:
            bs = bisect_right(csa,min(csa[a_read]+B[b_read],K-csb[b_read]))
            if bs <= a_read:
                break
            else:
                a_read = bs - 1
        elif A[a_read] > B[b_read]:
            bs = bisect_right(csb,min(csb[b_read]+A[a_read],K-csa[a_read]))
            if bs <= b_read:
                break
            else:
                b_read = bs - 1
        else:
            if K - csa[a_read] - csb[b_read] >= A[a_read]:
                for j in range(1,N+M):
                    if A[a_read+j] < B[a_read+j]:
                        if K - csa[a_read+j-1] - csb[b_read] >= A[a_read+j]:
                            a_read += j
                            break
                        else:
                            a_read += j - 1
                            break
                    elif A[a_read+j] > B[a_read+j]:
                        if K - csa[a_read] - csb[b_read+j-1] >= B[a_read+j]:
                            b_read += j
                            break
                        else:
                            b_read += j - 1
                            break
                    else:
                        if K - csa[a_read+j-1] - csb[b_read] < A[a_read+j]:
                            a_read += j - 1
                            break
            else:
                break
print(a_read+b_read)

Buggy submission (verdict: Runtime Error):
from bisect import *

N,M,K,*f = map(int, open(0).read().split())
A = f[:N]
B = f[N:]
a_read = 0
b_read = 0
passed = 0
csa = [0]
for i in range(N):
    csa.append(csa[i]+A[i])
csb = [0]
for i in range(M):
    csb.append(csb[i]+B[i])
for i in range(N+M):
    if a_read == N:
        bs = bisect_right(csb,K-csa[N])
        b_read = bs - 1
        break
    elif b_read == M:
        bs = bisect_right(csa,K-csb[M])
        b_read = bs - 1
        break
    else:
        if A[a_read] < B[b_read]:
            bs = bisect_right(csa,min(csa[a_read]+B[b_read],K-csb[b_read]))
            if bs <= a_read:
                break
            else:
                a_read = bs - 1
        elif A[a_read] > B[b_read]:
            bs = bisect_right(csb,min(csb[b_read]+A[a_read],K-csa[a_read]))
            if bs <= b_read:
                break
            else:
                b_read = bs - 1
        else:
            if K - csa[a_read] - csb[b_read] >= A[a_read]:
                for j in range(1,N+M):
                    if A[a_read+j] < B[a_read+j]:
                        if K - csa[a_read+j-1] - csb[b_read] >= A[a_read+j]:
                            a_read += j
                            break
                        else:
                            a_read += j - 1
                            break
                    elif A[a_read+j] > B[a_read+j]:
                        if K - csa[a_read] - csb[b_read+j-1] >= B[a_read+j]:
                            b_read += j
                            break
                        else:
                            b_read += j - 1
                            break
            else:
                break
print(a_read+b_read)