Buggy python submission (verdict: Runtime Error):
def main():
    N, M, K = map(int, input().split())
    *A, = map(int, input().split())
    *B, = map(int, input().split())

    ai = 0
    t = 0
    cnt = 0
    while True:
        if ai < len(A) and t + A[ai] <= K:
            t += A[ai]
            ai += 1
            cnt += 1
            continue
        break

    ans = cnt
    bi = 0
    while True:
        if bi < len(B):
            while ai >= 0 and t + B[bi] > K:
                t -= A[ai]
                ai -= 1
                cnt -= 1
            if t + B[bi] <= K:
                t += B[bi]
                bi += 1
                cnt += 1
                ans = max(ans, cnt)
                continue
        break

    print(ans)


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    N, M, K = map(int, input().split())
    *A, = map(int, input().split())
    *B, = map(int, input().split())

    def accumulate(a):
        s = 0
        yield s
        for x in a:
            s += x
            yield s

    *P, = accumulate(A)
    *Q, = accumulate(B)

    ans = 0
    j = len(Q) - 1
    for i, x in enumerate(P):
        while j > 0 and x + Q[j] > K:
            j -= 1
        if x + Q[j] <= K:
            ans = max(ans, i + j)
    print(ans)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import sys
    sys.setrecursionlimit(10 ** 7)

    INF = 10 ** 9 + 10

    N, M, K = map(int, input().split())
    *A, = map(int, input().split())
    *B, = map(int, input().split())

    memo = [[-1] * (len(B) + 1) for _ in range(len(A) + 1)]

    def rec(t, ai, bi):
        if t > K: return -INF
        if ~memo[ai][bi]: return memo[ai][bi]

        ac, bc = INF, INF
        if ai < len(A): ac = A[ai]
        if bi < len(B): bc = B[bi]

        ret = max(
            0,
            rec(t + ac, ai + 1, bi) + 1,
            rec(t + bc, ai, bi + 1) + 1
        )

        memo[ai][bi] = ret
        return ret

    print(rec(0, 0, 0))


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    N, M, K = map(int, input().split())
    *A, = map(int, input().split())
    *B, = map(int, input().split())

    def accumulate(a):
        s = 0
        yield s
        for x in a:
            s += x
            yield s

    *P, = accumulate(A)
    *Q, = accumulate(B)

    ans = 0
    j = len(Q) - 1
    for i, x in enumerate(P):
        while j > 0 and x + Q[j] > K:
            j -= 1
        if x + Q[j] <= K:
            ans = max(ans, i + j)
    print(ans)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import sys
    sys.setrecursionlimit(10 ** 7)

    INF = 10 ** 9 + 10

    N, M, K = map(int, input().split())
    *A, = map(int, input().split())
    *B, = map(int, input().split())

    def rec(t, ai, bi):
        if t > K: return -INF

        ac, bc = INF, INF
        if ai < len(A): ac = A[ai]
        if bi < len(B): bc = B[bi]

        return max(
            0,
            rec(t + ac, ai + 1, bi) + 1,
            rec(t + bc, ai, bi + 1) + 1
        )

    print(rec(0, 0, 0))


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    N, M, K = map(int, input().split())
    *A, = map(int, input().split())
    *B, = map(int, input().split())

    def accumulate(a):
        s = 0
        yield s
        for x in a:
            s += x
            yield s

    *P, = accumulate(A)
    *Q, = accumulate(B)

    ans = 0
    j = len(Q) - 1
    for i, x in enumerate(P):
        while j > 0 and x + Q[j] > K:
            j -= 1
        if x + Q[j] <= K:
            ans = max(ans, i + j)
    print(ans)


if __name__ == '__main__':
    main()