Buggy python submission (verdict: Time Limit Exceeded):

def main():
    N,M,K = map(int, input().split())
    A_list = list(map(int, input().split()))
    B_list = list(map(int, input().split()))

    # cumsum
    A_cum = [0] * (N+1)
    B_cum = [0] * (M+1)
    ap = 0
    for i, a in enumerate(A_list):
        A_cum[i+1] = ap + a
        ap += a
    bp = 0
    for i, b in enumerate(B_list):
        B_cum[i+1] = bp + b
        bp += b

    for bi,b in enumerate(B_cum):
        if b > K:
            break

    max_cnt = bi-1 
    bp = bi

    for i in range(N+1):
        a = A_cum[i]
        if a > K:
            break
        for bi in list(range(bp+1))[::-1]:
            b = B_cum[bi]
            if b+a <= K:
                break
        bp = bi
        max_cnt = max(max_cnt, i+bi)

    print(max_cnt)

if __name__ == "__main__":
    main()

Fixed submission:
import bisect

def main():
    N,M,K = map(int, input().split())
    A_list = list(map(int, input().split()))
    B_list = list(map(int, input().split()))

    # cumsum
    A_cum = [0] * (N+1)
    B_cum = [0] * (M)
    ap = 0
    for i, a in enumerate(A_list):
        A_cum[i+1] = ap + a
        ap += a
    bp = 0
    for i, b in enumerate(B_list):
        B_cum[i] = bp + b
        bp += b

    max_cnt = 0
    for i in range(N+1):
        a = A_cum[i]
        if a > K:
            break
        bi = bisect.bisect_right(B_cum, K-a)
        max_cnt = max(max_cnt, i+bi)

    print(max_cnt)

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):

N,M,K = map(int, input().split())
A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))

# cumsum
A_cum = [0] * (N+1)
B_cum = [0] * (M+1)
ap = 0
for i, a in enumerate(A_list):
    A_cum[i+1] = ap + a
    ap += a
bp = 0
for i, b in enumerate(B_list):
    B_cum[i+1] = bp + b
    bp += b

for bi,b in enumerate(B_cum):
    if b > K:
        break

max_cnt = bi-1 
bp = bi

for i in range(N+1):
    a = A_cum[i]
    if a > K:
        break
    for bi in list(range(bp+1))[::-1]:
        b = B_cum[bi]
        if b+a <= K:
            break
    bp = bi
    max_cnt = max(max_cnt, i+bi)

print(max_cnt)

Fixed submission:
import bisect

def main():
    N,M,K = map(int, input().split())
    A_list = list(map(int, input().split()))
    B_list = list(map(int, input().split()))

    # cumsum
    A_cum = [0] * (N+1)
    B_cum = [0] * (M)
    ap = 0
    for i, a in enumerate(A_list):
        A_cum[i+1] = ap + a
        ap += a
    bp = 0
    for i, b in enumerate(B_list):
        B_cum[i] = bp + b
        bp += b

    max_cnt = 0
    for i in range(N+1):
        a = A_cum[i]
        if a > K:
            break
        bi = bisect.bisect_right(B_cum, K-a)
        max_cnt = max(max_cnt, i+bi)

    print(max_cnt)

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Runtime Error):


N,M,K = map(int, input().split())
A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))

ai = 0
bi = 0
cnt = 0
while True:
    if ai == N and bi == M:
        break
    elif ai == N:
        if B_list[bi] <= K:
            cnt += 1
            K -= B_list[bi]
            bi += 1
        else:
            break
    elif bi == N:
        if A_list[ai] <= K:
            cnt += 1
            K -= A_list[ai]
            ai += 1
        else:
            break

    elif A_list[ai] < B_list[bi]:
        if A_list[ai] <= K:
            cnt += 1
            K -= A_list[ai]
            ai += 1
        else:
            break
    else:
        if B_list[bi] <= K:
            cnt += 1
            K -= B_list[bi]
            bi += 1
        else:
            break
print(cnt)

Fixed submission:
import bisect

def main():
    N,M,K = map(int, input().split())
    A_list = list(map(int, input().split()))
    B_list = list(map(int, input().split()))

    # cumsum
    A_cum = [0] * (N+1)
    B_cum = [0] * (M)
    ap = 0
    for i, a in enumerate(A_list):
        A_cum[i+1] = ap + a
        ap += a
    bp = 0
    for i, b in enumerate(B_list):
        B_cum[i] = bp + b
        bp += b

    max_cnt = 0
    for i in range(N+1):
        a = A_cum[i]
        if a > K:
            break
        bi = bisect.bisect_right(B_cum, K-a)
        max_cnt = max(max_cnt, i+bi)

    print(max_cnt)

if __name__ == "__main__":
    main()