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 collections import deque
from sys import exit

N, M, K = [int(x) for x in input().split()]

a_book = [0]
for index, i in enumerate(input().split(), 1):
    a_book.append(a_book[index-1] + int(i))

b_book = [0]
for index, i in enumerate(input().split(), 1):
    b_book.append(b_book[index-1] + int(i))

if a_book[1] > K and b_book[1] > K:
    print(0)
    exit()

if a_book[-1] + b_book[-1] < K:
    print(N + M)
    exit()

search = deque()
check = {}


def search_count(v):
    search.appendleft(v)
    while search:
        v = search.popleft()

        if not v in check:
            check[v] = v[0] + v[1]

            if not (v[0] + 1, v[1]) in check:
                if v[0] + 1 <= N:
                    if a_book[v[0] + 1] + b_book[v[1]] <= K:
                        search.appendleft((v[0] + 1, v[1]))

            if not (v[0], v[1] + 1) in check:
                if v[1] + 1 <= M:
                    if a_book[v[0]] + b_book[v[1] + 1] <= K:
                        search.appendleft((v[0], v[1] + 1))


search_count((0, 0))

print(check)

print(max(check.values()))


from collections import deque
from sys import exit

N, M, K = [int(x) for x in input().split()]

a_book = [0]
for index, i in enumerate(input().split(), 1):
    if a_book[index-1] + int(i) <= K:
        a_book.append(a_book[index-1] + int(i))
    else:
        break

b_book = [0]
for index, i in enumerate(input().split(), 1):
    if b_book[index-1] + int(i) <= K:
        b_book.append(b_book[index-1] + int(i))
    else:
        break

search = deque()
check = {}


def search_count(v):
    search.appendleft(v)
    while search:
        v = search.popleft()

        if not v in check:
            check[v] = v[0] + v[1]

            if not (v[0] + 1, v[1]) in check:
                if v[0] + 1 <= N:
                    if a_book[v[0] + 1] + b_book[v[1]] <= K:
                        search.appendleft((v[0] + 1, v[1]))

            if not (v[0], v[1] + 1) in check:
                if v[1] + 1 <= M:
                    if a_book[v[0]] + b_book[v[1] + 1] <= K:
                        search.appendleft((v[0], v[1] + 1))


search_count((0, 0))

print(max(check.values()))


from bisect import bisect_right
from itertools import accumulate

N, M, K = [int(x) for x in input().split()]

a_book = [0] + list(accumulate([int(x) for x in input().split()]))
b_book = [0] + list(accumulate([int(x) for x in input().split()]))

check = {}

max_books = 0

for i, a in enumerate(a_book):
    if a > K:
        break

    b_pos = bisect_right(b_book, K - a)

    if max_books < b_pos - 1 + i:
        max_books = b_pos - 1 + i

print(max_books)


Verdicts: Wrong Answer

Runtime Error

Accepted