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

l = stdin.readline().rstrip().split(' ')
a = stdin.readline().rstrip().split(' ')
b = stdin.readline().rstrip().split(' ')
k = int(l[2])
n = int(l[0])
m = int(l[1])
ia = 0
ib = 0
sum = 0
count = 0
a_sum = 0
res = []
b_list = []
for bt in b:
    sum += int(bt)
    if sum <= k:
        count += 1
        b_list.append(sum)
    else:
        break
#print(b_list)
res.append(count)
count = 0
ia = 1
for at in a:
    a_sum += int(at)
    if k < a_sum:
        print(max(res))
        sys.exit()
    count = ia
    sum = a_sum
    for bsum in b_list:
        sum = a_sum + bsum
        if k < sum:
            break
        else:
            count += 1
    ia += 1
    res.append(count)
print(max(res))


Fixed submission:
from sys import stdin
import sys

l = stdin.readline().rstrip().split(' ')
A = list(map(int, input().split()))
B = list(map(int, input().split()))
k = int(l[2])
n = int(l[0])
m = int(l[1])

sum = 0
a = [0]
for i in range(n):
    sum += A[i]
    a.append(sum)
sum = 0

b = [0]
for i in range(m):
    sum += B[i]
    b.append(sum)
#print(b)

ans = 0
j = m
for i in range(n+1):
    if a[i] > k:
        break
    while b[j] > k - a[i]:
        j -= 1
    ans = max(ans, i+j)
print(ans)

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

l = stdin.readline().rstrip().split(' ')
a = stdin.readline().rstrip().split(' ')
b = stdin.readline().rstrip().split(' ')
k = int(l[2])
n = int(l[0])
m = int(l[1])
ia = 0
ib = 0
b_sum = 0
count = 0
a_sum = 0
res = []

for bt in b:
    b_sum += int(bt)
    if b_sum <= k:
        count += 1
    else:
        break
res.append(count)
count = 0
for at in a:
    a_sum = a_sum + int(at)
    if k < a_sum:
        #res.append(count)
        print(max(res))
        sys.exit()
    else:
        count = ia + 1
    b_sum = a_sum
    for bt in b:
        b_sum += int(bt)
        if b_sum <= k:
            count += 1
        else:
            break
    ia += 1
    res.append(count)
print(max(res))

Fixed submission:
from sys import stdin
import sys

l = stdin.readline().rstrip().split(' ')
A = list(map(int, input().split()))
B = list(map(int, input().split()))
k = int(l[2])
n = int(l[0])
m = int(l[1])

sum = 0
a = [0]
for i in range(n):
    sum += A[i]
    a.append(sum)
sum = 0

b = [0]
for i in range(m):
    sum += B[i]
    b.append(sum)
#print(b)

ans = 0
j = m
for i in range(n+1):
    if a[i] > k:
        break
    while b[j] > k - a[i]:
        j -= 1
    ans = max(ans, i+j)
print(ans)

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

l = stdin.readline().rstrip().split(' ')
a = stdin.readline().rstrip().split(' ')
b = stdin.readline().rstrip().split(' ')
k = int(l[2])
n = int(l[0])
m = int(l[1])
ia = 0
ib = 0
b_sum = 0
count = 0
a_sum = 0
res = []

for ib in range(m):
    b_sum += int(b[ib])
    if b_sum <= k:
        count += 1
    else:
        break
res.append(count)
count = 0
for ia in range(n):
    a_sum = a_sum + int(a[ia])
    if k < a_sum:
        #print("ok")
        res.append(count)
        continue
    else:
        count = ia + 1
    b_sum = a_sum
    for ib in range(m):
        b_sum += int(b[ib])
        if b_sum <= k:
            count += 1
        else:
            break
    res.append(count)
print(max(res))

Fixed submission:
from sys import stdin
import sys

l = stdin.readline().rstrip().split(' ')
A = list(map(int, input().split()))
B = list(map(int, input().split()))
k = int(l[2])
n = int(l[0])
m = int(l[1])

sum = 0
a = [0]
for i in range(n):
    sum += A[i]
    a.append(sum)
sum = 0

b = [0]
for i in range(m):
    sum += B[i]
    b.append(sum)
#print(b)

ans = 0
j = m
for i in range(n+1):
    if a[i] > k:
        break
    while b[j] > k - a[i]:
        j -= 1
    ans = max(ans, i+j)
print(ans)