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 itertools
import functools
import math
from collections import Counter
from itertools import combinations
import re



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

    A_len = len(A)
    maxB = len(B)

    Asum = [0] * (200000 + 1)
    Bsum = [0] * (200000 + 1)

    maxA = 0

    add = 0
    for i in range(1,A_len+1):
        add += A[i-1]
        Asum[i] = add
        if add <= K:
            maxA = i + 1
        else:
            break

    ans = maxA
    prevmax = 0
    for i in reversed(range(maxA)):
        for j in range(prevmax,maxB):
            if Bsum[j+1] == 0:
                Bsum[j+1] = (Bsum[j] + B[j])
            if Asum[i] + Bsum[j] <= K:
                if ans < i + (j + 1):
                    ans = i + (j + 1)
                    prevmax = j

                if j == maxB - 1:
                    print(ans)
                    exit()
            else:
                break


    print(ans)

main_sum()


import itertools
import functools
import math
from collections import Counter
from itertools import combinations
import re



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

    A_len = len(A)
    maxB = len(B)

    Asum = [0] * (200000 + 1)

    maxA = 0

    add = 0
    for i in range(1,A_len+1):
        add += A[i-1]
        Asum[i] = add
        if add <= K:
            maxA = i + 1
        else:
            break

    ans = maxA
    prevmax = 0
    Bsum = 0
    for i in reversed(range(maxA)):
        for j in range(prevmax,maxB):
            Bsum += B[j]
            if Asum[i] + Bsum <= K:
                if ans < i + (j + 1):
                    ans = i + (j + 1)
                    prevmax = j

                if j == maxB - 1
                    print(ans)
                    exit()
            else:
                break


    print(ans)

main_sum()


import itertools
import functools
import math
from collections import Counter
from itertools import combinations
import re



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

    A_len = len(A)
    B_len = len(B)

    Asum = [0] * (A_len + 1)
    Bsum = [0] * (B_len + 1)

    maxA = len(A) + 1
    maxB = len(B) + 1

    add = 0
    for i in range(1,A_len+1):
        Asum[i] = A[i-1] + Asum[i-1]

    add = 0
    for i in range(1,B_len+1):
        Bsum[i] = B[i-1] + Bsum[i-1]

    ans = 0
    prev = 0
    for i in reversed(range(maxA)):
        for j in range(prev,maxB):
            if Asum[i] + Bsum[j] <= K:
                prev = j
                if ans < i + j:
                    ans = i + j
                if j == (maxB - 1):
                    print(ans)
                    exit()
            else:
                break

    print(ans)




main_sum()


Verdicts: Wrong Answer

Runtime Error

Accepted