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):
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
ans = 0
sum_a = 0
for i in range(-1, len(A)):
    if i == -1:
        sum_a = 0
    else:
        sum_a += A[i]
        if sum_a > K:
            break
        sum_b = 0
    for j in range(len(B)):
        sum_b += B[j]
        if sum_b > K - sum_a:
            ans = max(ans, i+1+j)
            print("i = {0}, j = {1}, i+j +1 = {2}".format(i, j, i+j+1))
            break
        if j == len(B)-1: 
            ans = max(ans, i+j+2)
print(ans)

N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
ans = 0
sum_a = 0
for i in range(-1, len(A)):
    if i == -1:
        sum_a = 0
    else:
        sum_a += A[i]
        if sum_a > K:
            break
        sum_b = 0
    for j in range(len(B)):
        sum_b += B[j]
        if sum_b > K - sum_a:
            ans = max(ans, i+1+j)
            break
        if j == len(B)-1: 
            ans = max(ans, i+j+2)
print(ans)

N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
ans = 0
sum_a = 0
for i in range(len(A)+1):
    if i == 0:
        sum_a = 0
    else:
        sum_a += A[i-1]
        if sum_a > K:
            break
        sum_b = 0
    for j in range(len(B)):
        sum_b += B[j]
        if sum_b > K - sum_a:
            ans = max(ans, i+j)
            break
        if j == len(B)-1:
            ans = max(ans, i+j+1)
print(ans)

N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
ans = 0
sum_a = 0
for i in range(len(A)+1):
    if i == 0:
        sum_a = 0
    else:
        sum_a += A[i-1]
        if sum_a > K:
            break
        sum_b = 0
    for j in range(len(B)):
        sum_b += B[j]
        if sum_b > K - sum_a:
            ans = max(ans, i+j)
            break
        if j == len(B)-1: 
            ans = max(ans, i+j+1)
print(ans)

import bisect
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
a = [0]*(N+1)
b = [0]*(M+1)


for i in range(1, N+1):
    a[i] = a[i-1] + A[i-1]
for j in range(1, M+1):
    b[j] = b[j-1] + B[j-1]

ans = 0
# j = M
for i in range(N+1):
#     left = 0
#     right = j
    if K - a[i] < 0:
        break
#     while left < right:
#         mid = (left + right)//2
#         if b[mid] < K - a[i]:
#             left = mid + 1
#         elif b[mid] > K - a[i]:
#             right = mid - 1
#     if b[left] <= K - a[i]:
#         j = left
#     else:
#         j = left - 1
    j = bisect.bisect_right(b, K - a[i]) - 1
    ans = max(ans, i + j)
print(ans)

Verdicts: Runtime Error

Runtime Error

Runtime Error

Runtime Error

Accepted