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 random
n, m, k = map(int, input().split())

a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))


# print(n,m,k)
# print(a_list[0], b_list[0])
a_sum = [0]
b_sum = [0]
max_a = 0
max_b = 0
for i in range(1, max(n+1, m+1)):
    if i < n+1:
        a_sum.append(a_sum[i-1] + a_list[i-1])
        if a_sum[i] < k:
            max_a = i
    if i < m+1:
        b_sum.append(b_sum[i-1] + b_list[i-1])

max_num = max_a
# print(max_a)
while max_a > -1:
    while max_b < m and b_sum[max_b] <= k-a_sum[max_a]:
        max_b += 1
    if max_num < max_a + max_b:
        max_num = max_a + max_b
        # print(max_a, max_b)
    max_a -= 1

print(max_num)


import random
n, m, k = map(int, input().split())

a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))


# print(n,m,k)
# print(a_list[0], b_list[0])
a_sum = [0]
b_sum = [0]
for i in range(1, max(n+1, m+1)):
    if i < n+1:
        a_sum.append(a_sum[i-1] + a_list[i-1])
    if i < m+1:
        b_sum.append(b_sum[i-1] + b_list[i-1])

ans = 0
for i in range(n+1):
    if a_sum[i] > k:
        break
    else:
        j = m
        while b_sum[j] <= k - a_sum[i]:
            j -= 1
    ans = max(ans, i+j)
        
print(ans)
# print(n+m) 

import random

n, m, k = random.randint(1, 200000), random.randint(1, 200000), random.randint(1, 10**9)
# a_list = list(map(int, input().split()))
# b_list = list(map(int, input().split()))
a_list = [random.randint(1, 10**3) for i in range(n)]
b_list = [random.randint(1, 10**3) for i in range(m)]
# print(n,m,k)
# print(a_list[0], b_list[0])
a_sum = [a_list[0]]
b_sum = [b_list[0]]
max_a = 0
max_b = 0
for i in range(1, max(n, m)):
    if i < n:
        a_sum.append(a_sum[i-1] + a_list[i])
        if a_sum[i] < k:
            max_a = i+1
    if i < m:
        b_sum.append(b_sum[i-1] + b_list[i])

max_num = 0
max_a += 1
while max_a > 0:
    max_a -= 1
    while max_b < m and b_sum[max_b] <= k-a_sum[max_a-1]:
        max_b += 1
    if max_num < max_a + max_b:
        max_num = max_a + max_b

print(max_num)
# print(n+m)


import random
n, m, k = map(int, input().split())

a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))


# print(n,m,k)
# print(a_list[0], b_list[0])
a_sum = [0]
b_sum = [0]
max_a = 0
max_b = 0
for i in range(1, max(n+1, m+1)):
    if i < n+1:
        a_sum.append(a_sum[i-1] + a_list[i-1])
        if a_sum[i] < k:
            max_a = i
    if i < m+1:
        b_sum.append(b_sum[i-1] + b_list[i-1])

max_num = max_a
# print(max_a)
while max_a > -1:
    while max_b < m and b_sum[max_b] <= k-a_sum[max_a]:
        max_b += 1
    if max_num < max_a + max_b:
        max_num = max_a + max_b
        print(max_a, max_b)
    max_a -= 1

print(max_num)


n, m, k = map(int, input().split())
a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))
a_index = 0
b_index = 0
count = 0

while k > 0:
    a_min = 1e10
    b_min = 1e10
    if a_index < len(a_list):
        a_min = a_list[a_index]
    if b_index < len(b_list):
        b_min = b_list[b_index]

    if a_min == 1e10 and b_min == 1e10:
        breaka
    elif a_min < b_min:
        count += 1
        k -= a_min
        a_index += 1
    else:
        count += 1
        k -= b_min
        b_index += 1

print(count)


import random
n, m, k = map(int, input().split())

a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))


# print(n,m,k)
# print(a_list[0], b_list[0])
a_sum = [0]
b_sum = [0]
max_a = 0
for i in range(1, max(n+1, m+1)):
    if i < n+1:
        a_sum.append(a_sum[i-1] + a_list[i-1])
        if a_sum[i] <= k:
            max_a = i
    if i < m+1:
        b_sum.append(b_sum[i-1] + b_list[i-1])

ans = 0
j = m
for i in range(0, max_a+1):
    while b_sum[j] > k - a_sum[i]:
        j -= 1
    ans = max(ans, i+j)
        
print(ans)

Verdicts: Wrong Answer

Runtime Error

Wrong Answer

Wrong Answer

Wrong Answer

Accepted