Problem:


Score : 300 points

Problem Statement

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.

Constraints

1 \leq N, M \leq 200000

1 \leq K \leq 10^9

1 \leq A_i, B_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M K
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_M

Output

Print an integer representing the maximum number of books that can be read.

Sample Input 1

3 4 240
60 90 120
80 150 80 150

Sample Output 1

3

In this case, it takes us 60, 90, 120 minutes to read the 1-st, 2-nd, 3-rd books from the top on Desk A, and 80, 150, 80, 150 minutes to read the 1-st, 2-nd, 3-rd, 4-th books from the top on Desk B, respectively.

We can read three books in 230 minutes, as shown below, and this is the maximum number of books we can read within 240 minutes.

Read the topmost book on Desk A in 60 minutes, and remove that book from the desk.

Read the topmost book on Desk B in 80 minutes, and remove that book from the desk.

Read the topmost book on Desk A in 90 minutes, and remove that book from the desk.

Sample Input 2

3 4 730
60 90 120
80 150 80 150

Sample Output 2

7

Sample Input 3

5 4 1
1000000000 1000000000 1000000000 1000000000 1000000000
1000000000 1000000000 1000000000 1000000000

Sample Output 3

0

Watch out for integer overflows.



Sample input:
3 4 240
60 90 120
80 150 80 150


Sample output:
3

Submission status: Wrong Answer

Solution code:
# Did not get in the contest. The key insight is in the end, the optimal solution involves taking X books from the front of A and Y books from the front of B. The intermediate transitions from one stack to another do not matter in the big picture.

from sys import stdin
n, m, k = map(int, stdin.readline().split())
astack = list(map(int, stdin.readline().split()))
bstack = list(map(int, stdin.readline().split()))

def correctApproach_naiveImplementation(n, m, k, astack, bstack):
    # TLE. Time complexity O(N^2). N<2*10^5.

    # max books from A upto which total time <= k.
    tot_time = 0
    maxbooks = 0
    prefix_tot_time_a = [0]

    # O(n) to calculate prefix total time for stack A.
    for i in astack:
        if tot_time + i <= k:
            tot_time += i
            maxbooks += 1
            prefix_tot_time_a.append(tot_time)
        else:
            break

    # remove one book at a time from maxbooks of A. And checking how many more books of B we can take. Find the max configuration of taking X books from front of A and Y books from front of B.

    # O(n) to calculate prefix total time for stack B.
    ans = maxbooks
    prefix_tot_time_b = [bstack[0]]
    for i in bstack[1:]:
        prefix_tot_time_b.append(prefix_tot_time_b[-1] + i)

    # ind1 is the number of books taken from A. This is O(n^2).
    for ind1 in range(maxbooks, -1, -1):
        currmax = ind1
        tot_time = prefix_tot_time_a[ind1]

        # this inner loop is O(N) we are trying to find the max number of books we can take from B linearly. We can use binary search instead.
        for ind2 in range(m):
            if tot_time + prefix_tot_time_b[ind2] <= k:
                currmax += 1
            else:
                break
        ans = max(ans, currmax)
    print(ans)
            
def correctApproach_binarySearch(n, m, k, astack, bstack):
    # Accepted. O(N log M + M)
    from bisect import bisect_right
    tot_time = 0
    maxbooks = 0
    prefix_tot_time_a = [0]

    # O(n) to calculate prefix total time for stack A.
    for i in astack:
        if tot_time + i <= k:
            tot_time += i
            maxbooks += 1
            prefix_tot_time_a.append(tot_time)
        else:
            break

    # O(n) to calculate prefix total time for stack B.
    ans = maxbooks
    prefix_tot_time_b = [bstack[0]]
    for i in bstack[1:]:
        prefix_tot_time_b.append(prefix_tot_time_b[-1] + i)

    # ind1 is the number of books taken from A. This is O(n log m).
    for ind1 in range(maxbooks, -1, -1):
        currmax = ind1
        tot_time = prefix_tot_time_a[ind1]

        # remaining time; Binary search on prefix total time on B.
        rem = k - tot_time 
        ind2 = bisect_right(prefix_tot_time_b, x = rem)
        currmax += ind2

        ans = max(ans, currmax)
    print(ans)

def correctApproach_twoPointer(n, m, k, astack, bstack):
    tot = 0
    pa = []
    for i in astack:
        if tot + i <= k:
            tot += i
            pa += [tot]
        else: break
    
    tot = 0
    pb = []
    for i in bstack:
        if tot + i <= k:
            tot += i
            pb += [tot]
        else: break
    
    l1, l2 = len(pa), len(pb)
    if l1 == 0: print(l2)
    elif l2 == 0: print(l1)
    else:
        i, j = l1-1, 0
        ans = max(l1, l2)
        tot = pa[-1]
        while i > -1 and j < l2:
            currmax = (i+1) + (j)
            if tot + pb[j] <= k:
                currmax += 1
                j += 1
            else:
                i -= 1
            ans = max(ans, currmax)
        print(ans)
            
# correctApproach_binarySearch(n, m, k, astack, bstack)
correctApproach_twoPointer(n, m, k, astack, bstack)























########## MY FAILED ATTEMPT IN CONTEST; COULD ONLY GET 10 AC AND 10 WA ################
# Tried greedy approach in various ways which were wrong.


# from sys import stdin
# from collections import deque as dq
# from itertools import zip_longest
# n, m, k = map(int, input().split())
# a = dq(map(int, stdin.readline().split()))
# b = dq(map(int, stdin.readline().split()))

# tot = 0
# count = 0

# i = j = 0
# while i < n and j < m:
#     # pick one line
#     if tot + a[i] + b[j] > k:
#         count1 = 0
#         ssf = tot
#         for ind1 in range(i, n):
#             if a[ind1] + ssf > k:
#                 break
#             else:
#                 ssf += a[ind1]
#                 count1 += 1

#         count2 = 0
#         ssf = tot
#         for ind2 in range(j, m):
#             if b[ind2] + ssf > k:
#                 break
#             else:
#                 ssf += b[ind2]
#                 count2 += 1

#         if count1 > count2:
#             print(count + count1)
#         else:
#             print(count + count2) 
#         exit()

#     # pick from any line 
#     else:
#         if tot + min(a[i], b[j]) > k:
#             break
#         elif a[i] < b[j]:
#             tot += a[i]
#             count += 1
#             i += 1
#         elif b[j] < a[i]:
#             tot += b[j]
#             count += 1
#             j += 1
#         else:
#             count1 = 0
#             for ind1 in range(i, n):
#                 if a[ind1] <= a[i]:
#                     count1 += 1
#                 else:
#                     break
#             count2 = 0
#             for ind2 in range(j, m):
#                 if a[ind2] <= b[j]:
#                     count2 += 1
#                 else:
#                     break
            
#             if count1>count2:
#                 tot += a[i]
#                 i += 1
#             elif count2<count1:
#                 tot += b[j]
#                 j += 1
#             else:
#                 if sum(a[i: min(i + count1 + 1, n)]) < sum(b[j: min(j + count2 + 1, m)]):
#                     tot += a[i]
#                     i += 1
#                 else:
#                     tot += b[j]
#                     j += 1
#             count += 1


# if i == n and j == m:
#     print(count)
# elif i < n:
#     for ind in range(i, n):
#         if tot + a[ind] <= k:
#             count += 1
#             tot += a[ind]
#     print(count)
# elif j < m:
#     for ind in range(j, m):
#         if tot + b[ind] <=k:
#             count += 1
#             tot += b[ind]
#     print(count)




# while tot < k:
#     atop = a[0] if a else 10**10
#     btop = b[0] if b else 10**10

#     # both empty
#     if atop == 10**10 and btop == 10**10:
#         break
    
#     # if adding the minimum crosses k.
#     if tot + min(atop, btop) > k:
#         break
    
#     if tot + atop + b
#     if atop < btop:
#         tot += a.popleft()
#         count += 1
#     elif btop < atop:
#         tot += b.popleft()
#         count += 1
#     else:
#         rema = True
#         for i, j in zip(a, b):
#             if i < j:
#                 rema = True
#                 break
#             elif j < i:
#                 rema = False
#                 break
#             else:
#                 pass
#         if rema:
#             tot += a.popleft()
#             count += 1
#         else:
#             tot += b.popleft()
#             count += 1

Submission status: Wrong Answer

Solution code:
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
for i in range(1, N + M + 1):
    flag = 0
    for j in range(i + 1):
        if sum(A[0: j]) + sum(A[0: i - j]) <= K:
            flag = 1
            break
    if flag == 0:
        i -= 1
        break
print(i)

Submission status: Wrong Answer

Solution code:
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A.reverse()
B.reverse()
time = []
count = int()

while K - sum(time) >= 0:
    if sum(A) == 0:
        if sum(B) == 0:
            break
        if K - sum(time) >= min(B):
            #print('B:', B)
            time.append(min(B))
            B.pop()
            count += 1
            #print('K - sum(time)b:', K - sum(time))
            print('最終結果：A', A, '最終結果：B', B, '残時間：', K - sum(time))
            continue


    elif sum(B) == 0:
        if sum(A) == 0:
            break
        if K - sum(time) >= min(A):
            #print('A:', A)
            time.append(min(A))
            A.pop()
            count += 1
            #print('K - sum(time)a:', K - sum(time))
            print('最終結果：A', A, '最終結果：B', B, '残時間：', K - sum(time))
            continue

    else:
        if min(A) >= min(B):
            if K - sum(time) >= min(B):
                # print('B:', B)
                time.append(min(B))
                B.pop()
                count += 1
                # print('K - sum(time)b:', K - sum(time))
                print('最終結果：A', A, '最終結果：B', B, '残時間：', K - sum(time))
                continue
            else:
                break
        else:
            if K - sum(time) >= min(A):
                # print('A:', A)
                time.append(min(A))
                A.pop()
                count += 1
                # print('K - sum(time)a:', K - sum(time))
                print('最終結果：A', A, '最終結果：B', B, '残時間：', K - sum(time))
                continue
            else:
                break

print(count)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
input = sys.stdin.readline

N, M, K = map(int, input().split())
A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))


#N, M, K = 3, 4, 730
#A_list = [60, 90, 120]
#B_list = [80, 150, 80, 150]


A_list.insert(0, 0)
B_list.insert(0, 0)
B_cumsum = np.cumsum(B_list)

time = B_cumsum[-1]
ans, j = 0, M

for i in range(N+1):
    while (j > 0 and time > K):
        j -= 1
        time -= B_cumsum[j]
    if (time > K):
        break
    ans = max(ans, i+j)
    time += A_list[i]

print(ans)

Submission status: Wrong Answer

Solution code:
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
time, count = 0, 0
for i in range(N + M):
    if len(A) > 0 and len(B) > 0:
        if A[0] <= B[0]:
            time += A[0]
            A = A[1:]
        else:
            time += B[0]
            B = B[1:]
    elif len(A) > 0 and len(B) == 0:
        time += A[0]
        A = A[1:]
    elif len(B) > 0 and len(A) == 0:
        time += B[0]
        B = B[1:]
    else:
        break
    if time <= K:
        count += 1
    else:
        break
print(count)

Submission status: Wrong Answer

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

a, b = iter(A), iter(B)
now_a, now_b = next(a), next(b)
ans, cnt_time = 0, 0
ex_a = True
ex_b = True
this_time_cnt_time = 0

while cnt_time < K:
    #print('\n------',cnt_time)
    this_time_cnt_time = cnt_time
    if now_a > now_b and ex_b:
        try:
            ans += 1
            cnt_time += now_b
            #print('b', now_b, cnt_time, ans)
            now_b = next(b)
        except StopIteration:
            #print('b--')
            ex_b = False
            continue
    elif now_a <= now_b and ex_a:
        try:
            ans += 1
            cnt_time += now_a
            #print('a', now_a, cnt_time, ans)
            now_a = next(a)
        except StopIteration:
            #print('a--')        
            ex_a = False
            continue
    elif ex_b:
        try:
            ans += 1
            cnt_time += now_b
            #print('b2', now_b, cnt_time, ans)
            now_b = next(b)
        except StopIteration:
            #print('b2--')
            ex_b = False
            continue
    elif ex_a:
        try:
            ans += 1
            cnt_time += now_a
            #print('a2', now_a, cnt_time, ans)
            now_a = next(a)
        except StopIteration:
            #print('a2--')
            ex_a = False
            continue
    if this_time_cnt_time == cnt_time:
        ans+=1
        #print('aaaaaaa')
        break
if not ex_a and not ex_b:
    #print('***', ans, ex_a, ex_b)
    ans+=1
print(ans-1)

Submission status: Wrong Answer

Solution code:
from sys import stdin
n, m, k = map(int, stdin.readline().split())
astack = list(map(int, stdin.readline().split()))
bstack = list(map(int, stdin.readline().split()))

# max books from A upto which total time <= k.
tot_time = 0
maxbooks = 0
prefix_tot_time_a = [0]
for i in astack:
    if tot_time + i <= k:
        tot_time += i
        maxbooks += 1
        prefix_tot_time_a.append(tot_time)

# remove one book at a time from maxbooks of A. And checking how many more books of B we can take.
ans = maxbooks

prefix_tot_time_b = [bstack[0]]
for i in bstack[1:]:
    prefix_tot_time_b.append(prefix_tot_time_b[-1] + i)

# maxbooks-1 is also the index position of the last book taken from A.
for ind1 in range(maxbooks, -1, -1):
    currmax = ind1
    tot_time = prefix_tot_time_a[ind1]
    for ind2 in range(m):
        if tot_time + prefix_tot_time_b[ind2] <= k:
            currmax += 1
        else:
            break
    ans = max(ans, currmax)
print(ans)
            


























########## MY ATTEMPT IN CONTEST; COULD ONLY GET 10 AC AND 10 WA ################
# Tried greedy approach in various ways which were wrong.


# from sys import stdin
# from collections import deque as dq
# from itertools import zip_longest
# n, m, k = map(int, input().split())
# a = dq(map(int, stdin.readline().split()))
# b = dq(map(int, stdin.readline().split()))

# tot = 0
# count = 0

# i = j = 0
# while i < n and j < m:
#     # pick one line
#     if tot + a[i] + b[j] > k:
#         count1 = 0
#         ssf = tot
#         for ind1 in range(i, n):
#             if a[ind1] + ssf > k:
#                 break
#             else:
#                 ssf += a[ind1]
#                 count1 += 1

#         count2 = 0
#         ssf = tot
#         for ind2 in range(j, m):
#             if b[ind2] + ssf > k:
#                 break
#             else:
#                 ssf += b[ind2]
#                 count2 += 1

#         if count1 > count2:
#             print(count + count1)
#         else:
#             print(count + count2) 
#         exit()

#     # pick from any line 
#     else:
#         if tot + min(a[i], b[j]) > k:
#             break
#         elif a[i] < b[j]:
#             tot += a[i]
#             count += 1
#             i += 1
#         elif b[j] < a[i]:
#             tot += b[j]
#             count += 1
#             j += 1
#         else:
#             count1 = 0
#             for ind1 in range(i, n):
#                 if a[ind1] <= a[i]:
#                     count1 += 1
#                 else:
#                     break
#             count2 = 0
#             for ind2 in range(j, m):
#                 if a[ind2] <= b[j]:
#                     count2 += 1
#                 else:
#                     break
            
#             if count1>count2:
#                 tot += a[i]
#                 i += 1
#             elif count2<count1:
#                 tot += b[j]
#                 j += 1
#             else:
#                 if sum(a[i: min(i + count1 + 1, n)]) < sum(b[j: min(j + count2 + 1, m)]):
#                     tot += a[i]
#                     i += 1
#                 else:
#                     tot += b[j]
#                     j += 1
#             count += 1


# if i == n and j == m:
#     print(count)
# elif i < n:
#     for ind in range(i, n):
#         if tot + a[ind] <= k:
#             count += 1
#             tot += a[ind]
#     print(count)
# elif j < m:
#     for ind in range(j, m):
#         if tot + b[ind] <=k:
#             count += 1
#             tot += b[ind]
#     print(count)




# while tot < k:
#     atop = a[0] if a else 10**10
#     btop = b[0] if b else 10**10

#     # both empty
#     if atop == 10**10 and btop == 10**10:
#         break
    
#     # if adding the minimum crosses k.
#     if tot + min(atop, btop) > k:
#         break
    
#     if tot + atop + b
#     if atop < btop:
#         tot += a.popleft()
#         count += 1
#     elif btop < atop:
#         tot += b.popleft()
#         count += 1
#     else:
#         rema = True
#         for i, j in zip(a, b):
#             if i < j:
#                 rema = True
#                 break
#             elif j < i:
#                 rema = False
#                 break
#             else:
#                 pass
#         if rema:
#             tot += a.popleft()
#             count += 1
#         else:
#             tot += b.popleft()
#             count += 1

Submission status: Wrong Answer

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


def cumsum(l):
    s = 0
    for v in l:
        s += v
        yield s


a = list(cumsum(A))
b = list(cumsum(B))
ans = 0
k = M - 1
for i in range(N):
    if a[i] > K:
        continue
    tmp = i + 1
    for j in range(k, -1, -1):
        if a[i] + b[j] <= K:
            tmp += j + 1
            k = j
            break
        else:
            continue
    ans = max(ans, tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin
n, m, k = map(int, stdin.readline().split())
astack = list(map(int, stdin.readline().split()))
bstack = list(map(int, stdin.readline().split()))

# max books from A upto which total time <= k.
tot_time = 0
maxbooks = 0
prefix_tot_time_a = [0]
for i in astack:
    if tot_time + i <= k:
        tot_time += i
        maxbooks += 1
        prefix_tot_time_a.append(tot_time)

# remove one book at a time from maxbooks of A. And checking how many more books of B we can take.
ans = maxbooks

prefix_tot_time_b = [bstack[0]]
for i in bstack[1:]:
    prefix_tot_time_b.append(prefix_tot_time_b[-1] + i)

# maxbooks-1 is also the index position of the last book taken from A.
for ind1 in range(maxbooks, -1, -1):
    currmax = ind1
    tot_time = prefix_tot_time_a[ind1]
    for ind2 in range(m):
        if tot_time + prefix_tot_time_b[ind2] <= k:
            tot_time += prefix_tot_time_b[ind2]
            currmax += 1
        else:
            break
    ans = max(ans, currmax)
print(ans)
            


























########## MY ATTEMPT IN CONTEST; COULD ONLY GET 10 AC AND 10 WA ################
# Tried greedy approach in various ways which were wrong.


# from sys import stdin
# from collections import deque as dq
# from itertools import zip_longest
# n, m, k = map(int, input().split())
# a = dq(map(int, stdin.readline().split()))
# b = dq(map(int, stdin.readline().split()))

# tot = 0
# count = 0

# i = j = 0
# while i < n and j < m:
#     # pick one line
#     if tot + a[i] + b[j] > k:
#         count1 = 0
#         ssf = tot
#         for ind1 in range(i, n):
#             if a[ind1] + ssf > k:
#                 break
#             else:
#                 ssf += a[ind1]
#                 count1 += 1

#         count2 = 0
#         ssf = tot
#         for ind2 in range(j, m):
#             if b[ind2] + ssf > k:
#                 break
#             else:
#                 ssf += b[ind2]
#                 count2 += 1

#         if count1 > count2:
#             print(count + count1)
#         else:
#             print(count + count2) 
#         exit()

#     # pick from any line 
#     else:
#         if tot + min(a[i], b[j]) > k:
#             break
#         elif a[i] < b[j]:
#             tot += a[i]
#             count += 1
#             i += 1
#         elif b[j] < a[i]:
#             tot += b[j]
#             count += 1
#             j += 1
#         else:
#             count1 = 0
#             for ind1 in range(i, n):
#                 if a[ind1] <= a[i]:
#                     count1 += 1
#                 else:
#                     break
#             count2 = 0
#             for ind2 in range(j, m):
#                 if a[ind2] <= b[j]:
#                     count2 += 1
#                 else:
#                     break
            
#             if count1>count2:
#                 tot += a[i]
#                 i += 1
#             elif count2<count1:
#                 tot += b[j]
#                 j += 1
#             else:
#                 if sum(a[i: min(i + count1 + 1, n)]) < sum(b[j: min(j + count2 + 1, m)]):
#                     tot += a[i]
#                     i += 1
#                 else:
#                     tot += b[j]
#                     j += 1
#             count += 1


# if i == n and j == m:
#     print(count)
# elif i < n:
#     for ind in range(i, n):
#         if tot + a[ind] <= k:
#             count += 1
#             tot += a[ind]
#     print(count)
# elif j < m:
#     for ind in range(j, m):
#         if tot + b[ind] <=k:
#             count += 1
#             tot += b[ind]
#     print(count)




# while tot < k:
#     atop = a[0] if a else 10**10
#     btop = b[0] if b else 10**10

#     # both empty
#     if atop == 10**10 and btop == 10**10:
#         break
    
#     # if adding the minimum crosses k.
#     if tot + min(atop, btop) > k:
#         break
    
#     if tot + atop + b
#     if atop < btop:
#         tot += a.popleft()
#         count += 1
#     elif btop < atop:
#         tot += b.popleft()
#         count += 1
#     else:
#         rema = True
#         for i, j in zip(a, b):
#             if i < j:
#                 rema = True
#                 break
#             elif j < i:
#                 rema = False
#                 break
#             else:
#                 pass
#         if rema:
#             tot += a.popleft()
#             count += 1
#         else:
#             tot += b.popleft()
#             count += 1

Submission status: Wrong Answer

Solution code:
import sys

input_data = []
input_pointer = 0

output_data = []

def input_scan():
    global input_data
    input_data = sys.stdin.readlines()
    input_data = [x[:-1] for x in input_data]

def next_line():
    global input_pointer
    global input_data
    if input_pointer < len(input_data):
        input_pointer += 1
        return input_data[input_pointer - 1]
    else:
        return ""

def append_output(string):
    global output_data
    output_data.append(string)

def print_output():
    global output_data
    output_data.append("")
    sys.stdout.write("\n".join(output_data))

# problem A
# a = int(input())
# append_output(str(a + a * a + a * a * a))
# print_output()

# problem B
# input_scan()
# s = next_line()
# t = next_line()
# print(sum([1 for x, y in zip(list(s), list(t)) if x != y]))

input_scan()
[n, m, k] = [int(x) for x in next_line().split(" ")]
stack_a = [int(x) for x in next_line().split(" ")]
stack_b = [int(x) for x in next_line().split(" ")]

total_books = 0

remaining_time = k
i = 0
for i, book in enumerate(stack_a):
    if remaining_time - book >= 0:
        remaining_time -= book
        total_books += 1
    else:
        break

max_total_books = total_books
i -= 1
b_pointer = 0

while b_pointer < len(stack_b) and \
      remaining_time - stack_b[b_pointer] >= 0:
    remaining_time -= stack_b[b_pointer]
    b_pointer += 1
    total_books += 1
max_total_books = max(total_books, max_total_books)

while i >= 0:
    total_books -= 1
    remaining_time += stack_a[i]
    i -= 1
    while b_pointer < len(stack_b) and \
       remaining_time - stack_b[b_pointer] >= 0:
        remaining_time -= stack_b[b_pointer]
        b_pointer += 1
        total_books += 1
    max_total_books = max(total_books, max_total_books)

while b_pointer < len(stack_b) and \
      remaining_time - stack_b[b_pointer] >= 0:
    remaining_time -= stack_b[b_pointer]
    b_pointer += 1
    total_books += 1
max_total_books = max(total_books, max_total_books)

append_output(str(max_total_books))
print_output()

Submission status: Wrong Answer

Solution code:
from sys import stdin
n, m, k = map(int, stdin.readline().split())
astack = list(map(int, stdin.readline().split()))
bstack = list(map(int, stdin.readline().split()))

# max books from A upto which total time <= k.
tot_time = 0
maxbooks = 0
prefix_tot_time = [0]
for i in astack:
    if tot_time + i <= k:
        tot_time += i
        maxbooks += 1
        prefix_tot_time.append(tot_time)

# remove one book at a time from maxbooks of A. And checking how many more books of B we can take.
ans = maxbooks

# maxbooks-1 is also the index position of the last book taken from A.
for ind1 in range(maxbooks, -1, -1):
    currmax = ind1
    tot_time = prefix_tot_time[ind1]
    for ind2 in range(m):
        if tot_time + bstack[ind2] <= k:
            tot_time += bstack[ind2]
            currmax += 1
        else:
            break
    ans = max(ans, currmax)
print(ans)

Submission status: Wrong Answer

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

answer = 0
cost = 0
index_a = -1
for i in range(len(A)):
    if i == len(A) or cost + A[i] > K:
        break
    cost += A[i]
    answer += 1
    index_a = i

max_answer = answer
index_b = 0
while (index_b < len(B) and index_a >= 0):    
    if cost + B[index_b] > K:
        if index_a == 0:
            break
        cost -= A[index_a]
        index_a -= 1
        answer -= 1
    else:
        cost += B[index_b]
        index_b += 1
        answer += 1
    if max_answer < answer:
        max_answer = answer
print(max_answer)

Submission status: Wrong Answer

Solution code:
import sys

input_data = []
input_pointer = 0

output_data = []

def input_scan():
    global input_data
    input_data = sys.stdin.readlines()
    input_data = [x[:-1] for x in input_data]

def next_line():
    global input_pointer
    global input_data
    if input_pointer < len(input_data):
        input_pointer += 1
        return input_data[input_pointer - 1]
    else:
        return ""

def append_output(string):
    global output_data
    output_data.append(string)

def print_output():
    global output_data
    output_data.append("")
    sys.stdout.write("\n".join(output_data))

# problem A
# a = int(input())
# append_output(str(a + a * a + a * a * a))
# print_output()

# problem B
# input_scan()
# s = next_line()
# t = next_line()
# print(sum([1 for x, y in zip(list(s), list(t)) if x != y]))

input_scan()
[n, m, k] = [int(x) for x in next_line().split(" ")]
stack_a = [int(x) for x in next_line().split(" ")]
stack_b = [int(x) for x in next_line().split(" ")]

total_books = 0

remaining_time = k
i = 0
for i, book in enumerate(stack_a):
    if remaining_time - book >= 0:
        remaining_time -= book
        total_books += 1
    else:
        break

max_total_books = total_books
i -= 1
b_pointer = 0

while b_pointer < len(stack_b) and \
      remaining_time - stack_b[b_pointer] >= 0:
    remaining_time -= stack_b[b_pointer]
    b_pointer += 1
    total_books += 1
max_total_books = max(total_books, max_total_books)

while i >= 0:
    total_books -= 1
    remaining_time += stack_a[i]
    i -= 1
    while b_pointer < len(stack_b) and \
       remaining_time - stack_b[b_pointer] >= 0:
        remaining_time -= stack_b[b_pointer]
        b_pointer += 1
        total_books += 1
    max_total_books = max(total_books, max_total_books)

append_output(str(max_total_books))
print_output()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
import bisect
import itertools

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

a =[0]+ list(itertools.accumulate(a))
b =[0]+ list(itertools.accumulate(b))


ans=0
for i in range(n+1):
    x=k-a[i]
    if x<=0:
        break
    else:
        v=bisect.bisect_right(b,x)-1
        ans=max(ans,i+v)
print(ans)

Submission status: Wrong Answer

Solution code:
n,m,k=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))

a=[0]
b=[0]

for i in range(n):
    a.append(a[i]+A[i])
for i in range(m):
    b.append(b[i]+B[i])
    
c=0
idx=m

for i in range(n+1):
    while 1:
        if idx<0:
            break
        if a[i]+b[idx]>k:
            print(a[i]+b[idx])
            idx-=1
            continue
        else:
            c=max(i+idx,c)
            break
else:
    print(c)

Submission status: Wrong Answer

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

answer = 0
cost = 0
index_a = -1
for i in range(len(A)):
    if i == len(A) or cost + A[i] > K:
        break
    cost += A[i]
    answer += 1
    index_a = i

max_answer = answer
index_b = 0
while (index_b < len(B) and index_a >= 0):
    if cost + B[index_b] > K:
        cost -= A[index_a]
        index_a -= 1
        answer -= 1
    else:
        cost += B[index_b]
        index_b += 1
        answer += 1
    if max_answer < answer:
        max_answer = answer
print(max_answer)

Submission status: Wrong Answer

Solution code:
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = 0

while(True):
    t = None
    
    if not a:
        if not b:
            break
        else:
            t = b.pop(0)
    elif not b:
        t = a.pop(0)
    elif a[0] < b[0]:
        t = a.pop(0)
    else:
        t = b.pop(0)
    
    if k>=t:
        k-=t
        c += 1
    else:
        break
    
print(c)

Submission status: Wrong Answer

Solution code:
import sys
# from math import sqrt, gcd, ceil, log
from bisect import bisect
from collections import defaultdict, Counter
inp = sys.stdin.readline
read = lambda: list(map(int, inp().strip().split()))

# sys.setrecursionlimit(10**6)



def solve():
	n, m, k = read()
	arr1 = [0]+read(); arr2 = [0]+read()
	for i in range(1, n+1):arr1[i] += arr1[i-1]
	for i in range(1, m+1):arr2[i] += arr2[i-1]

	i = 0; ans = 0
	while i < n+1 and arr1[i] < k:
		tem = i + bisect(arr2, k-arr1[i])-1
		# print(tem, i, arr1[i], bisect(arr2, k-arr1[i]))
		ans = max(ans, tem)
		i += 1
	print(ans)

	





		

if __name__ == "__main__":
	solve()