Problem: We have two desks: A and B. Desk A has a vertical stack of N books on it, and Desk B similarly has M books on it.  It takes us A_i minutes to read the i-th book from the top on Desk A (1 \leq i \leq N), and B_i minutes to read the i-th book from the top on Desk B (1 \leq i \leq M).  Consider the following action:    * Choose a desk with a book remaining, read the topmost book on that desk, and remove it from the desk.  How many books can we read at most by repeating this action so that it takes us at most K minutes in total? We ignore the time it takes to do anything other than reading.

Submission attempts (in order):
import sys

readline = sys.stdin.readline
readlines = sys.stdin.readlines
ns = lambda: readline().rstrip() # input string
ni = lambda: int(readline().rstrip()) # input int
nm = lambda: map(int, readline().split()) # input multiple int 
nl = lambda: list(map(int, readline().split())) # input multiple int to list

n, m, k = nm()
a = nl()
b = nl()
ans = 0
sum_a = [0]*(n+1)
sum_b = [0]*(m+1)
for i in range(1, n+1):
    sum_a[i] = sum_a[i-1] + a[i-1]
for i in range(1, m+1):
    sum_b[i] = sum_b[i-1] + b[i-1]
ans = 0
bef_j = m
print(sum_a, sum_b)
for i in range(n+1):
    if sum_a[i] >= k:
        break
    for j in range(bef_j, -1, -1):
        if sum_a[i] + sum_b[j] <= k:
            ans = max(ans, i+j)
            bef_j = j
            break
print(ans)



"""i_n = 0
i_m = 0
while True:
    if i_n >= n and i_m >= m:
        break
    elif i_n >= n:
        tmp = b[i_m]
        i_m += 1
    elif i_m >= m:
        tmp = a[i_n]
        i_n += 1
    else:
        if a[i_n] < b[i_m]:
            tmp = a[i_n]
            i_n += 1
        else:
            tmp = b[i_m]
            i_m += 1

    if tmp + time <= k:
        cnt += 1
        time += tmp
    else:
        break
print(cnt)"""

import sys

readline = sys.stdin.readline
readlines = sys.stdin.readlines
ns = lambda: readline().rstrip() # input string
ni = lambda: int(readline().rstrip()) # input int
nm = lambda: map(int, readline().split()) # input multiple int 
nl = lambda: list(map(int, readline().split())) # input multiple int to list

n, m, k = nm()
a = nl()
b = nl()
ans = 0
sum_a = [0]*(n+1)
sum_b = [0]*(m+1)
for i in range(1, n+1):
    sum_a[i] = sum_a[i-1] + a[i-1]
for i in range(1, m+1):
    sum_b[i] = sum_b[i-1] + b[i-1]
ans = 0
bef_j = m
for i in range(n+1):
    if sum_a[i] >= k:
        break
    for j in range(m+1, -1, -1):
        if sum_a[i] + sum_b[j] <= k:
            ans = max(ans, i+j)
            bef_j = j
            break
print(ans)


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

cs_A = [0] 
for i in A:
    cs_A.append(cs_A[-1] + i)
cs_B = [0] 
for i in B:
    cs_B.append(cs_B[-1] + i)

def is_ok(mid, v):
    if v + cs_B[mid] <= K:
        return True
    else:
        return False

def meguru_bisect(v, ng, ok):
    
    while (abs(ok - ng) > 1):
        mid = (ok + ng) // 2
        if is_ok(mid, v):
            ok = mid
        else:
            ng = mid
    return ok
ans = 0
for i in range(0, N+1):
    v = cs_A[i]
    idx = meguru_bisect(v, M+1, -1)
    if 0 <= idx <= M:
        if v + cs_B[idx] <= K:
            ans = max(ans, i + idx) 
print(ans)

Verdicts: Wrong Answer

Runtime Error

Accepted