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
def main():
    n, m, k = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    # A.reverse()
    # B.reverse()

    q_a = deque(A)
    q_b = deque(B)
    cnt = 0
    time = 0
    for _ in range(n + m):
        if q_a:
            a = q_a.popleft()
        else:
            a = None

        if q_b:
            b = q_b.popleft()
        else:
            b = None

        print(a,b)

        if a and b:
            
            if a < b:
                q_b.appendleft(b)
                time += a

            
            if b <= a:
                q_a.appendleft(a)
                time += b
        else:
            if a:
                time += a
            if b:
                time += b

        #print(q_a,q_b,time)
        
        if time > k:
            break

        cnt += 1

    print(cnt)














if __name__ == "__main__":
    main()


# from collections import deque
import numpy as np
def main():
    n, m, k = map(int, input().split())
    A = np.array([0] + list(map(int, input().split())))
    B = np.array([0] + list(map(int, input().split())))
    X = np.cumsum(A)
    Y = np.cumsum(B)
    P = X[X <= k]
    Q = Y[Y <= k]
    n = len(P)
    m = len(Q)
    # print(P,Q)

    cnt = 0
    if P[-1] + Q[-1] >= k:
        print(n + m - 2)
        exit()

    
    for i in range(n)[::-1]:
        if i + m <= cnt:
            break
        else:
            for j in range(m)[::-1]:
                #print(i,j,X[i],Y[j])
                if X[i] + Y[j] <= k:
                    cnt = max(cnt, i + j)
                    break
    print(cnt)



if __name__ == "__main__":
    main()


# from collections import deque
import numpy as np
def main():
    n, m, k = map(int, input().split())
    A = np.array([0] + list(map(int, input().split())))
    B = np.array([0] + list(map(int, input().split())))
    X = np.cumsum(A)
    Y = np.cumsum(B)
    P = X[X <= k]
    Q = Y[Y <= k]
    n = len(P)
    m = len(Q)
    # print(P,Q)

    cnt = 0
    
    for i in range(n):
        for j in range(m)[::-1]:
            print(i,j,X[i],Y[j])
            if X[i] + Y[j] <= k:
                cnt = max(cnt, i + j)
                break
    print(cnt)



if __name__ == "__main__":
    main()


# from collections import deque
import numpy as np
def main():
    n, m, k = map(int, input().split())
    A = np.array([0] + list(map(int, input().split())))
    B = np.array([0] + list(map(int, input().split())))
    X = np.cumsum(A)
    Y = np.cumsum(B)
    # print(P,Q)

    cnt = 0
    j = m
    
    for i, x in enumerate(X):
        if x > k:
            break

        while x + Y[j] > k:
            j -= 1
        cnt = max(cnt, i + j)

    print(cnt)

if __name__ == "__main__":
    main()


Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Accepted