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):
from itertools import accumulate

n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0

a_accum = [0] + list(accumulate(a))
b_accum = [0] + list(accumulate(b))

for j in range(len(a_accum)):
  for i in range(len(b_accum)):
    t = a_accum[-(j+1)] + b_accum[-(i+1)]
    candi = (n-j) + (m-i)
    print(n-j, m-i, candi)
    if ans >= candi:
      break
    if t <= k:
      ans = candi
      break

print(ans)

from collections import deque

# N, M, K = map(int, input().split())


N, M, K = map(int, "3 4 1000000000".split())
A = deque(map(int, "600000000 90 120".split()))
B = deque(map(int, "800000000 150 80 150".split()))

result = 0

while (A != deque() and K >= A[0]) or (B != deque() and K >= B[0]):
    a, b = 0, 0
    na, nb = 0, 0
    for i in A:
        if a+i <= K:
            a += i
            na += 1
        else:
            break
    for i in B:
        if b+i <= K:
            b += i
            nb += 1
        else:
            break


    if na > nb:
        result += 1
        na -= 1
        K -= A.popleft()
    elif na < nb:
        result += 1
        nb -= 1
        K -= B.popleft()
    elif na == nb:
        result += 1
        if A[0] <= B[0]:
            na -= 1
            K -= A.popleft()
        else:
            nb -= 1
            K -= B.popleft()
print(result)


from collections import deque

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

result = 0

a, b = 0, 0
na, nb = 0, 0
for i in range(len(A)):
    if a+A[i] <= K:
        a += A[i]
        na += 1
    else:
        break
for i in range(len(B)):
    if b+B[i] <= K:
        b += B[i]
        nb += 1
    else:
        break

while A != deque() and K >= A[0]:
    if na > nb:
        result += 1
        na -= 1
        K -= A.popleft()
    elif na == nb:
        result += 1
        if A[0] <= B[0]:
            na -= 1
            K -= A.popleft()

while B != deque() and K >= B[0]:
    if na < nb:
        result += 1
        nb -= 1
        K -= B.popleft()
    elif na == nb:
        result += 1
        if A[0] > B[0]:
            nb -= 1
            K -= B.popleft()

print(result)


from itertools import accumulate

n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0

a_accum = [0] + list(accumulate(a))
b_accum = [0] + list(accumulate(b))

j = m
for i in range(n+1):
  while k < a_accum[i] + b_accum[j] and j >= 1:
    j -= 1
  if k >= a_accum[i] + b_accum[j] and ans < i + j:
    ans = i + j

print(ans)

Verdicts: Wrong Answer

Wrong Answer

Time Limit Exceeded

Accepted