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

# readline = sys.stdin.readline
# generator = (readline().strip() for _ in range(N))

# N, M = map(int, input().split())
# As = list(map(int, input().split()))
# queries = (input() for _ in range(N))


def dfs(n, m, total, count, N, M, K, As, Bs):
    if total > K:
        return count - 1

    if n == N and m == M:
        return count

    if n < N:
        c1 = dfs(n + 1, m, total + As[n], count + 1, N, M, K, As, Bs)
    else:
        c1 = count

    if m < M:
        c2 = dfs(n, m + 1, total + Bs[m], count + 1, N, M, K, As, Bs)
    else:
        c2 = count

    return max(c1, c2)


def solve():
    N, M, K = map(int, input().split())

    As = list(map(int, input().split()))
    Bs = list(map(int, input().split()))

    return dfs(0, 0, 0, 0, N, M, K, As, Bs)


def main():
    print(solve())


if __name__ == "__main__":
    main()


Fixed submission:
from bisect import bisect_right


def solve():
    N, M, K = map(int, input().split())

    A = map(int, input().split())
    B = map(int, input().split())

    sum_as = [0] * (N+1)
    sum_bs = [0] * (M+1)

    for i, a in enumerate(A):
        sum_as[i+1] = sum_as[i] + a

    for i, b in enumerate(B):
        sum_bs[i+1] = sum_bs[i] + b
    sum_bs = sum_bs[1:]

    answer = 0
    for i, sa in enumerate(sum_as):
        remain = K - sa
        if remain < 0:
            continue
        
        idx = bisect_right(sum_bs, remain)
        answer = max(answer, i + idx)

    return answer


def main():
    print(solve())


if __name__ == "__main__":
    main()

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

# readline = sys.stdin.readline
# generator = (readline().strip() for _ in range(N))

# N, M = map(int, input().split())
# As = list(map(int, input().split()))
# queries = (input() for _ in range(N))


def solve():
    N, M, K = map(int, input().split())

    As = list(map(int, input().split()))
    Bs = list(map(int, input().split()))

    def dfs(n, m, total, count):
        if total > K:
            return count - 1

        if n == N and m == M:
            return count

        if n < N:
            c1 = dfs(n+1, m, total+As[n], count+1)
        else:
            c1 = count

        if m < M:
            c2 = dfs(n, m+1, total+Bs[m], count+1)
        else:
            c2 = count

        return max(c1, c2)

    return dfs(0, 0, 0, 0)


def main():
    print(solve())


if __name__ == "__main__":
    main()


Fixed submission:
from bisect import bisect_right


def solve():
    N, M, K = map(int, input().split())

    A = map(int, input().split())
    B = map(int, input().split())

    sum_as = [0] * (N+1)
    sum_bs = [0] * (M+1)

    for i, a in enumerate(A):
        sum_as[i+1] = sum_as[i] + a

    for i, b in enumerate(B):
        sum_bs[i+1] = sum_bs[i] + b
    sum_bs = sum_bs[1:]

    answer = 0
    for i, sa in enumerate(sum_as):
        remain = K - sa
        if remain < 0:
            continue
        
        idx = bisect_right(sum_bs, remain)
        answer = max(answer, i + idx)

    return answer


def main():
    print(solve())


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Runtime Error):
# import sys
# readline = sys.stdin.readline
# generator = (readline().strip() for _ in range(N))

# N, M = map(int, input().split())
# As = list(map(int, input().split()))
# queries = (input() for _ in range(N))


def solve():
    N, M, K = map(int, input().split())

    As = list(map(int, input().split()))
    Bs = list(map(int, input().split()))

    def dfs(n, m, total, count):
        if total > K:
            return count - 1

        if n == N and m == M:
            return count

        if n < N:
            c1 = dfs(n+1, m, total + As[n], count+1)
        else:
            c1 = count

        if m < M:
            c2 = dfs(n, m+1, total + Bs[m], count+1)
        else:
            c2 = count

        return max(c1, c2)

    return dfs(0, 0, 0, 0)


def main():
    print(solve())


if __name__ == "__main__":
    main()


Fixed submission:
from bisect import bisect_right


def solve():
    N, M, K = map(int, input().split())

    A = map(int, input().split())
    B = map(int, input().split())

    sum_as = [0] * (N+1)
    sum_bs = [0] * (M+1)

    for i, a in enumerate(A):
        sum_as[i+1] = sum_as[i] + a

    for i, b in enumerate(B):
        sum_bs[i+1] = sum_bs[i] + b
    sum_bs = sum_bs[1:]

    answer = 0
    for i, sa in enumerate(sum_as):
        remain = K - sa
        if remain < 0:
            continue
        
        idx = bisect_right(sum_bs, remain)
        answer = max(answer, i + idx)

    return answer


def main():
    print(solve())


if __name__ == "__main__":
    main()