Buggy python submission (verdict: Time Limit Exceeded):
import sys
from functools import lru_cache

sys.setrecursionlimit(10 ** 7)


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


@lru_cache(None)
def dp(i, j, cur):
    if cur > K:
        return 0
    elif i >= N:
        if j < M and cur + B[j] <= K:
            return dp(i, j + 1, cur + B[j]) + 1
        else:
            return 0
    elif j >= M:
        if i < N and cur + A[i] <= K:
            return dp(i + 1, j, cur + A[i]) + 1
        else:
            return 0
    else:
        res = 0
        if cur + A[i] <= K:
            res = max(1 + dp(i + 1, j, cur + A[i]), res)
        if cur + B[j] <= K:
            res = max(1 + dp(i, j + 1, cur + B[j]), res)
        return res


print(dp(0, 0, 0))

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

prefix_A = [0] * (N + 1)
for i, a in enumerate(A, 1):
    prefix_A[i] = prefix_A[i - 1] + a
prefix_B = [0] * (M + 1)
for j, b in enumerate(B, 1):
    prefix_B[j] = prefix_B[j - 1] + b


res = 0
for i in range(N + 1):
    cur = prefix_A[i]
    if cur == K:
        res = max(i, res)
    elif cur < K:
        left = 0
        right = M + 1
        while right - left > 1:
            mid = (left + right) // 2
            if cur + prefix_B[mid] <= K:
                left = mid
            else:
                right = mid
        res = max(res, i + left)


print(res)

Buggy python submission (verdict: Runtime Error):
import sys
from functools import lru_cache

sys.setrecursionlimit(10 ** 5)


N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
n, m = map(len, (A, B))


@lru_cache(None)
def dp(i, j, cur):
    if cur > K:
        return 0
    elif i >= n:
        if j < m and cur + B[j] <= K:
            return dp(i, j + 1, cur + B[j]) + 1
        else:
            return 0
    elif j >= m:
        if i < n and cur + A[i] <= K:
            return dp(i + 1, j, cur + A[i]) + 1
        else:
            return 0
    else:
        res = float('-inf')
        if cur + A[i] <= K:
            res = max(1 + dp(i + 1, j, cur + A[i]), res)
        if cur + B[j] <= K:
            res = max(1 + dp(i, j + 1, cur + B[j]), res)
        if res == float('-inf'):
            return 0
        else:
            return res


print(dp(0, 0, 0))

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

prefix_A = [0] * (N + 1)
for i, a in enumerate(A, 1):
    prefix_A[i] = prefix_A[i - 1] + a
prefix_B = [0] * (M + 1)
for j, b in enumerate(B, 1):
    prefix_B[j] = prefix_B[j - 1] + b


res = 0
for i in range(N + 1):
    cur = prefix_A[i]
    if cur == K:
        res = max(i, res)
    elif cur < K:
        left = 0
        right = M + 1
        while right - left > 1:
            mid = (left + right) // 2
            if cur + prefix_B[mid] <= K:
                left = mid
            else:
                right = mid
        res = max(res, i + left)


print(res)

Buggy python submission (verdict: Runtime Error):
import sys
from functools import lru_cache

sys.setrecursionlimit(10 ** 5)


N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
n, m = map(len, (A, B))


@lru_cache(None)
def dp(i, j, cur):
    if cur > K:
        return 0
    elif i >= n:
        if j < m and cur + B[j] <= K:
            return dp(i, j + 1, cur + B[j]) + 1
        else:
            return 0
    elif j >= m and cur + A[i] <= K:
        if i < n:
            return dp(i + 1, j, cur + A[i]) + 1
        else:
            return 0
    else:
        res = float('-inf')
        if cur + A[i] <= K:
            res = max(1 + dp(i + 1, j, cur + A[i]), res)
        if cur + B[j] <= K:
            res = max(1 + dp(i, j + 1,  cur + B[j]), res)
        if res == float('-inf'):
            return 0
        else:
            return res


print(dp(0, 0, 0))

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

prefix_A = [0] * (N + 1)
for i, a in enumerate(A, 1):
    prefix_A[i] = prefix_A[i - 1] + a
prefix_B = [0] * (M + 1)
for j, b in enumerate(B, 1):
    prefix_B[j] = prefix_B[j - 1] + b


res = 0
for i in range(N + 1):
    cur = prefix_A[i]
    if cur == K:
        res = max(i, res)
    elif cur < K:
        left = 0
        right = M + 1
        while right - left > 1:
            mid = (left + right) // 2
            if cur + prefix_B[mid] <= K:
                left = mid
            else:
                right = mid
        res = max(res, i + left)


print(res)