Problem p02623 (python)

Fixed submission:
#!usr/bin/env pypy3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)

def main():
    N, M, K = LI()
    A = LI()
    B = LI()

    a = [0]+list(accumulate(A))
    b = [0]+list(accumulate(B))

    ans = 0

    for i in range(N + 1):
        if a[i] > K:
            continue
        rest = K - a[i]
        j = bisect.bisect_right(b, rest)
        ans = max(ans, i + j - 1)

    print(ans)

main()

Buggy submission (verdict: Runtime Error):
#!usr/bin/env pypy3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)

def main():
    N, M, K = LI()
    A = LI()
    B = LI()

    i, j = (0, 0)

    ans = 0
    cum = 0

    while True:
        if i < N and j < M:
            if A[i] <= B[j]:
                if cum + A[i] <= K:
                    cum += A[i]
                    i += 1
                    ans += 1
                else:
                    if cum + B[j] <= K:
                        cum += B[j]
                        j += 1
                        ans += 1
                    else:
                        break
            else:
                if cum + B[j] <= K:
                    cum += B[j]
                    j += 1
                    ans += 1
                else:
                    if cum + A[i] <= K:
                        cum += A[i]
                        i += 1
                        ans += 1
                    else:
                        break
        elif i < N:
            if cum + A[i] <= K:
                cum += A[i]
                i += 1
                ans += 1
            else:
                break
        elif j < M:
            if cum + B[j] <= K:
                cum += B[j]
                j += 1
                ans += 1
            else:
                break
        else:
            break

    print(ans)

main()
#!usr/bin/env pypy3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)

def main():
    N, M, K = LI()
    A = LI()
    B = LI()

    i, j = (0, 0)

    ans = 0
    cum = 0

    while True:
        if i < N and j < M:
            if A[i] <= B[j]:
                if cum + A[i] <= K:
                    cum += A[i]
                    i += 1
                    ans += 1
                else:
                    if cum + B[j] <= K:
                        cum += B[j]
                        j += 1
                        ans += 1
                    else:
                        break
            else:
                if cum + B[j] <= K:
                    cum += B[j]
                    j += 1
                    ans += 1
                else:
                    if cum + A[i] <= K:
                        cum += A[i]
                        i += 1
                        ans += 1
                    else:
                        break
        elif i < N:
            if cum + A[i] <= K:
                cum += A[i]
                i += 1
                ans += 1
            else:
                break
        elif j < M:
            if cum + B[j] <= K:
                cum += B[j]
                j += 1
                ans += 1
            else:
                break
        else:
            break

    print(ans)

main()