Buggy python submission (verdict: Time Limit Exceeded):
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

A = [0] * (n + 1)
for i in range(1, n + 1):
    A[i] = A[i - 1] + a[i - 1]

B = [0] * (m + 1)
for i in range(1, m + 1):
    B[i] = B[i - 1] + b[i - 1]

ans = 0
for i in range(n + 1):
    for j in range(m + 1):
        t = A[i] + B[j]
        if t > k:
            break
        else:
            ans = max(ans, i + j)

print(ans)


Fixed submission:
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

A = [0] * (n + 1)
for i in range(1, n + 1):
    A[i] = A[i - 1] + a[i - 1]

B = [0] * (m + 1)
for i in range(1, m + 1):
    B[i] = B[i - 1] + b[i - 1]

ans = 0
best = m
for i in range(n + 1):
    for j in range(best, -1, -1):
        t = A[i] + B[j]
        if t <= k:
            ans = max(ans, i + j)
            best = j
            break
print(ans)

Buggy python submission (verdict: Runtime Error):
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

def dfs(ai, bi, t, c):
    if t > k:
        return c - 1
    elif ai == n and bi == m:
        return c
    elif ai == n:
        return dfs(ai, bi + 1, t + b[bi], c + 1)
    elif bi == m:
        return dfs(ai + 1, bi, t + a[ai], c + 1)
    return max(dfs(ai + 1, bi, t + a[ai], c + 1), dfs(ai, bi + 1, t + b[bi], c + 1))

ans = dfs(0, 0, 0, 0)

print(ans)


Fixed submission:
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

A = [0] * (n + 1)
for i in range(1, n + 1):
    A[i] = A[i - 1] + a[i - 1]

B = [0] * (m + 1)
for i in range(1, m + 1):
    B[i] = B[i - 1] + b[i - 1]

ans = 0
best = m
for i in range(n + 1):
    for j in range(best, -1, -1):
        t = A[i] + B[j]
        if t <= k:
            ans = max(ans, i + j)
            best = j
            break
print(ans)

Buggy python submission (verdict: Runtime Error):
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

def dfs(ai, bi, t, c):
    if t > k:
        return c - 1
    elif ai == n and bi == m:
        return c
    elif ai == n:
        return dfs(ai, bi + 1, t + b[bi], c + 1)
    elif bi == m:
        return dfs(ai + 1, bi, t + a[ai], c + 1)
    return max(dfs(ai + 1, bi, t + a[ai], c + 1), dfs(ai, bi + 1, t + b[bi], c + 1))

ans = dfs(0, 0, 0, 0)

print(ans)


Fixed submission:
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

A = [0] * (n + 1)
for i in range(1, n + 1):
    A[i] = A[i - 1] + a[i - 1]

B = [0] * (m + 1)
for i in range(1, m + 1):
    B[i] = B[i - 1] + b[i - 1]

ans = 0
best = m
for i in range(n + 1):
    for j in range(best, -1, -1):
        t = A[i] + B[j]
        if t <= k:
            ans = max(ans, i + j)
            best = j
            break
print(ans)

Buggy python submission (verdict: Runtime Error):
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

def dfs(ai, bi, t, c):
    if t > k:
        return c - 1
    elif ai == n - 1 and bi == m - 1:
        return c
    elif ai == n - 1:
        return dfs(ai, bi + 1, t + b[bi], c + 1)
    elif bi == m - 1:
        return dfs(ai + 1, bi, t + a[ai], c + 1)
    return max(dfs(ai + 1, bi, t + a[ai], c + 1), dfs(ai, bi + 1, t + b[bi], c + 1))

ans = dfs(0, 0, 0, 0)

print(ans)


Fixed submission:
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

A = [0] * (n + 1)
for i in range(1, n + 1):
    A[i] = A[i - 1] + a[i - 1]

B = [0] * (m + 1)
for i in range(1, m + 1):
    B[i] = B[i - 1] + b[i - 1]

ans = 0
best = m
for i in range(n + 1):
    for j in range(best, -1, -1):
        t = A[i] + B[j]
        if t <= k:
            ans = max(ans, i + j)
            best = j
            break
print(ans)

Buggy python submission (verdict: Runtime Error):
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

def dfs(ai, bi, t, c):
    if t > k:
        return c - 1
    elif ai == n and bi == m:
        return c
    elif ai == n:
        return dfs(ai, bi + 1, t + b[bi], c + 1)
    elif bi == m:
        return dfs(ai + 1, bi, t + a[ai], c + 1)
    return max(dfs(ai + 1, bi, t + a[ai], c + 1), dfs(ai, bi + 1, t + b[bi], c + 1))

ans = dfs(0, 0, 0, 0)

print(ans)


Fixed submission:
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

A = [0] * (n + 1)
for i in range(1, n + 1):
    A[i] = A[i - 1] + a[i - 1]

B = [0] * (m + 1)
for i in range(1, m + 1):
    B[i] = B[i - 1] + b[i - 1]

ans = 0
best = m
for i in range(n + 1):
    for j in range(best, -1, -1):
        t = A[i] + B[j]
        if t <= k:
            ans = max(ans, i + j)
            best = j
            break
print(ans)