Buggy python submission (verdict: Time Limit Exceeded):
#! /usr/bin/env python3

import sys
import numpy as np
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

N, M, K = map(int, readline().split())
A = list(map(int, readline().split()))[::-1]
B = list(map(int, readline().split()))[::-1]

cnt = 0
tmp = 0
res = [0]
for a in A:
    if sum(res) + a <= K:
        res.append(a)
        cnt += 1
    else:
        break
for b in B:
    if sum(res) + b <= K:
        res.append(b)
        cnt += 1
    else:
        break
ans = cnt

cnt = 0
tmp = 0

for b in B:
    if tmp + b <= K:
        tmp += b
        cnt += 1
    else:
        break
for a in A:
    if tmp + a <= K:
        tmp += a
        cnt += 1
    else:
        break
ans = max(ans, cnt)

tot = 0
cnt = 0

while True:
    if len(A) > 0:
        acc_a = A.pop()
    else:
        acc_a = sys.maxsize
    if len(B) > 0:
        acc_b = B.pop()
    else:
        acc_b = sys.maxsize
    if acc_a == acc_b == sys.maxsize:
        break
    acc = min(acc_a, acc_b)

    if tot + acc <= K:
        cnt += 1
    else:
        break

    tot += acc
    if acc_a <= acc_b:
        B.append(acc_b)
    else:
        A.append(acc_a)

ans = max(ans, cnt)

print(ans)


Fixed submission:
#! /usr/bin/env python3

import sys
import numpy as np
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

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

A_cum = [0]
B_cum = [0]

for i in range(N):
    A_cum.append(A_cum[i] + A[i])
for i in range(M):
    B_cum.append(B_cum[i] + B[i])

cnt, j = 0, M

for i in range(N + 1):
    if A_cum[i] > K:
        break
    while B_cum[j] > K - A_cum[i]:
        j -= 1
    cnt = max(cnt, i + j)

print(cnt)

Buggy python submission (verdict: Time Limit Exceeded):
#! /usr/bin/env python3

import sys
import numpy as np
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

N, M, K = map(int, readline().split())
A = list(map(int, readline().split()))[::-1]
B = list(map(int, readline().split()))[::-1]

cnt = 0
tmp = 0
res = [0]
for a in A:
    if sum(res) + a <= K:
        res.append(a)
        cnt += 1
    else:
        break
for b in B:
    if sum(res) + b <= K:
        res.append(b)
        cnt += 1
    else:
        break
ans = cnt

cnt = 0
tmp = 0

for b in B:
    if tmp + b <= K:
        tmp += b
        cnt += 1
    else:
        break
for a in A:
    if tmp + a <= K:
        tmp += a
        cnt += 1
    else:
        break
ans = max(ans, cnt)

tot = 0
cnt = 0

while True:
    if len(A) > 0:
        acc_a = A.pop()
    else:
        acc_a = sys.maxsize
    if len(B) > 0:
        acc_b = B.pop()
    else:
        acc_b = sys.maxsize
    if acc_a == acc_b == sys.maxsize:
        break
    acc = min(acc_a, acc_b)

    if tot + acc <= K:
        cnt += 1
    else:
        break

    tot += acc
    if acc_a <= acc_b:
        B.append(acc_b)
    else:
        A.append(acc_a)

ans = max(ans, cnt)

print(ans)


Fixed submission:
#! /usr/bin/env python3

import sys
import numpy as np
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

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

A_cum = [0]
B_cum = [0]

for i in range(N):
    A_cum.append(A_cum[i] + A[i])
for i in range(M):
    B_cum.append(B_cum[i] + B[i])

cnt, j = 0, M

for i in range(N + 1):
    if A_cum[i] > K:
        break
    while B_cum[j] > K - A_cum[i]:
        j -= 1
    cnt = max(cnt, i + j)

print(cnt)