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: Runtime Error

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+M):
    if A[a] < B[b]:
        if A[a] <= K:
            K -= A[a]
            a += 1
            if a >= N:
                A.append(10**100)
        elif B[b] <= K:
            K -= B[b]
            b +=1
            if b >= N:
                B.append(10**100)
    else:
        if B[b] <= K:
            K -= B[b]
            b +=1
            if b >= N:
                B.append(10**100)
        elif A[a] <= K:
            K -= A[a]
            a += 1
            if a >= N:
                A.append(10**100)
              
            
print(a+b)

Submission status: Runtime Error

Solution code:
x,y ,z= map(int,input().split())
left = list(map(int,input().split()))
right = list(map(int,input().split()))
array=[]
while x != 0 and y != 0:   # line7
    if left[0] < right[0]:                  # line8
        array.append(left.pop(0))           # line9
    else:
        array.append(right.pop(0))    
    if sum(array) > z:
        array.pop(-1)
        break
print(len(array))

Submission status: Runtime Error

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

if K >= sum(A) + sum(B):
    print(N + M)
    exit()

n = []


def read(k, n_books, a, b):
    # print(k, n_books, a, b)
    if k - a[0] < 0 and k - b[0] < 0:
        n.append(n_books)
    else:
        if len(a) > 0 and k - a[0] >= 0:
            read(k - a[0], n_books+1, a[1:], b)
        if len(b) > 0 and k - b[0] >= 0:
            read(k - b[0], n_books+1, a, b[1:])


read(K, 0, A, B)
print(max(n))

Submission status: Runtime Error

Solution code:
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
time = 0
co =0
flag = True
while time <=k:
    if len(a) ==0 and len(b) ==0:
        co +=1
        Flag = False
        break
    elif a[0] >= b[0] or len(a) ==0:
        time += b[0]
        co += 1
        p = b[0]
        b.remove(p)
    else :
        time += a[0]
        co +=1
        q = a[0]
        a.remove(q)
if flag:
    print(co - 1)
else :
    print(co)

Submission status: Runtime Error

Solution code:
if __name__ == '__main__':
    n, m, k = map(int, input().strip().split())
    a = list(map(int, input().strip().split()))
    b = list(map(int, input().strip().split()))

    min = 0
    a_count = 0
    for i, a_min in enumerate(a):
        if a_min + min <= k:
            min += a_min
            a_count += 1
        else:
            break
    b_count = 0

    for i, b_min in enumerate(b):
        if b_min + min <= k:
            min += b_min
            b_count += 1
        else:
            break

    max_count = a_count + b_count
    if not max_count == n+m:
        for i in range(a_count, 0, -1):
            min -= a[i - 1]
            a_count -= 1
            while min + b[b_count] < k:
                min += b[b_count]
                b_count += 1
            if a_count + b_count > max_count:
                max_count = a_count + b_count
    print(max_count)

Submission status: Runtime Error

Solution code:
def calculate(A,B, K, ai, bi, book_count, times):
	m = float('-INF')
	if sum(times) > K:
		times.pop()
		return book_count-1
	if sum(times) == K or (ai == len(A)-1 and bi == len(B)-1):
		return book_count
	
	# select A
	if ai < len(A):
		c = calculate(A,B, K, ai+1, bi, book_count+1, times.copy() + [A[ai]])

		if c > m:
			m = c
		

	# select B
	if bi < len(B):
		c = calculate(A,B, K, ai, bi+1, book_count+1, times.copy() + [B[bi]])

		# backtrack
		if c > m:
			m = c
	return m

N, M, K = map(int, input().split())
A, B = [], []
if N:
	A = list(map(int, input().split()))
if M:
	B = list(map(int, input().split()))

print(calculate(A,B, K, 0, 0, 0, []))

Submission status: Runtime Error

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

A = list(map(int,input().split()))
B = list(map(int,input().split()))

time = 0
ai = 0
bi = 0
while (time < K):
  if (ai < N and bi < M): #AにもBにも本がまだのこっているとき
    if (A[ai] < B[bi]):　#Aのほうが時間が短い
      if(time + A[ai] <= K):
        time += A[ai]
        ai += 1
      else: 
        break
    else: #Bのほうが時間が短い
      if (time + B[bi] <= K):
        time += B[bi]
        bi += 1
      else:
        break
  elif (ai == N and bi < M): #Aに本がなくなってBに本があるとき
    if (time + B[bi] <= K):
      time += B[bi]
      bi += 1
    else:
      break
  elif (ai < N and bi == M): #Bに本がなくなってAに本があるとき
    if (time + A[ai] <= K):
      time += A[ai]
      ai += 1
    else:
      break
  else: #AにもBにも本がないとき
    break
print(ai+bi)

Submission status: Runtime Error

Solution code:
import sys

N,M,K = map(int, sys.stdin.readline().strip().split())
A = list(map(int, sys.stdin.readline().strip().split()))
B = list(map(int, sys.stdin.readline().strip().split()))

count = 0
k = K

if K < A[0] and K < B[0]:
    print(0)
    sys.exit()

if sum(A) + sum(B) <= K:
    print(N + M)
    sys.exit()

a = 0
b = 0
while k >= 0:
    if A[a] > B[b]:
        k -= B[b]
        b += 1
        count += 1
    elif A[a] < B[b]:
        k -= A[a]
        a += 1
        count += 1
    else:
        k -= A[a]
        a += 1
        count += 1

print(count - 1)

Submission status: Runtime Error

Solution code:
na,nb,k = map(int, input().split())
ta = map(int, input().split())
tb = map(int, input().split())

def get_bcnt( usek,tb=tb ):
    t = 0
    cnt = 0
    try:
        while True:
            if t + tb[cnt] > usek:
                return cnt
            else:
                t += tb[cnt]
                cnt += 1
    except:
        return len(tb)

sumta = 0
readbook = [get_bcnt(k)] 
for i in range(na):
    sumta += ta[i]
    if sumta > k:
        break
    if ta[i] <= k:
        readbook.append( i+1 + get_bcnt( k - sumta ) )

print( max(readbook) )

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import numpy as np

N, M, K = map(int, input().split())
A = input().split()
B = input().split()
for i in range(len(A)):
  A[i] = int(A[i])
for i in range(len(B)):
  B[i] = int(B[i])
A_cumsum = np.array(A).cumsum()
B_cumsum = np.array(B).cumsum()
max_count = 0

for i in range(N):
  if A_cumsum[i] > K:
    break
  for j in range(M):
    if A_cumsum[i] + B_cumsum[j] <= K and max_count < A_cumsum[i] + B_cumsum[j]:
      max_count = i + j + 2
    else:
      break
print(max_count)

Submission status: Runtime Error

Solution code:
n, m, k = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
total = 0
count = 0
if n < m:
    for _ in range(m - n):
        A.append(10 ** 9 + 1)

else:
    for _ in range(n - m):
        B.append(10 ** 9 + 1)

for i in range(n + m):
    if A[0] == min(A[0], B[0]):
        total += A.pop(0)
    elif B[0] == min(A[0], B[0]):
        total += B.pop(0)
    if total > k:
        break
    count += 1
print(count)

Submission status: Runtime Error

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():
	a, b, k = read(); a_ = read(); b_ = read()
	for i in range(1, a): a_[i] += a_[i-1]
	for i in range(1, b): b_[i] += b_[i-1]
	ans = 0
	while k > 0:
		inda = bisect(a_, k); indb = bisect(b_, k)
		# print(k, inda, indb, a_[inda-1], b_[indb-1])
		if inda > indb:
			k -= a_[inda-1];
			ans += inda
			a_ = a_[inda:]
		elif inda == indb:
			if a_[inda-1] < b_[indb-1]:
				k -= a_[inda-1];
				ans += inda
				a_ = a_[inda:]
			else:
				k -= b_[indb-1];
				ans += indb
				b_ = b_[indb:]
		else:
			k -= b_[indb-1];
			ans += indb
			b_ = b_[indb:]


	print(ans)





		

if __name__ == "__main__":
	solve()

Submission status: Runtime Error

Solution code:
def A(x):
  return x + x**2 + x**3


def B(s, t):
  ans = 0
  for i in range(len(s)):
    if s[i] != t[i]:
      ans += 1
  return ans


def C(n, m, k, a, b):
  pa = 0
  pb = 0
  ans = 0
  while k > 0:
    if pa < len(a) and a[pa] < b[pb]:
      k -= a[pa]
      pa += 1
    elif pb < len(b):
      k -= b[pb]
      pb += 1
    else:
      break
    if k >= 0:
      ans += 1
  return ans


def main():
  n, m, k = map(int, input().split())
  a = list(map(int, input().split()))
  b = list(map(int, input().split()))
  print(C(n, m, k, a, b))


if __name__ == '__main__':
  main()

Submission status: Runtime Error

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

ans = [0]

def dfs(now_sum,i,j,cnt):
    # print(now_sum)
    # if ans[0] <= cnt:
    #     return
    if now_sum > K:
        # print("a")
        cnt -= 1
        ans[0] = max(ans[0],cnt)
        return
    
    # if i == N or j== M:
        # print("b")
        # print(now_sum,i,j)
    # ans[0] = max(ans[0],cnt)
        # return

    if i+1 < N+1:
        dfs(now_sum + A[i],i+1,j,cnt+1)
    if j+1 < M+1:
        dfs(now_sum + B[j],i,j+1,cnt+1)
    ans[0] = max(ans[0],cnt)
    


dfs(0,0,0,0)

print(ans[0])

Submission status: Runtime Error

Solution code:
N,M,K = map(int, input().split())
S = N + M

A = list(map(int, input().split()))
B = list(map(int, input().split()))

sum = 0

for s in range(S):
    if K <= sum:
        break

    if len(A)==0 or A[0]>B[0]:
        sum=sum+B[0]
        B.pop(0)
    elif len(B)==0 or A[0]<B[0]:
        sum=sum+A[0]
        A.pop(0)
    
else:
    s+=1


print(s)

Submission status: Runtime Error

Solution code:
n,m,k = map(int,input().split());
alist = list(map(int,input().split()));
blist = list(map(int,input().split()));
alist.append(0);
blist.append(0);

book = 0;
time = 0;
aa = 0;
bb = 0;
allread = 0;

while True:
    if aa >= n and bb >= m:
        allread = 1;
        break;
    elif aa >= n and bb < m:
        b = blist[bb];
        a = b + 1;
    elif bb >= m and aa > n:
        a = alist[aa];
        b = a + 1;
    else:
        a = alist[aa]
        b = blist[bb]

    if a == b:
        if a[i+1] >= b[i+1]:
            time += b;
            bb += 1;
        else:
            time += a;
            aa += 1;
    elif a > b:
        time += b;
        bb += 1;
    else:
        time += a;
        aa += 1;
    if time > k:
        break;
    book += 1;

print(book);

Submission status: Runtime Error

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

i_a = 0
i_b = 0

total_time = 0

while True:
    if i_a == N and i_b == M:
        break
    else:
        if i_a < N and A[i_a] < B[i_b]:
            read_a = True
        else:
            read_a = False
    
    if read_a:
        if A[i_a] <= K-total_time:
            total_time += A[i_a]
            i_a += 1
        else:
            break
    else:
        if B[i_b] <= K-total_time:
            total_time += B[i_b]
            i_b += 1
        else:
            break

print(int(i_a+i_b))

Submission status: Runtime Error

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

i, j = 0, 0
readed = 0
count = 0

while True:
    if i >= n:
        if j >= m:
            break
        else:
            readed += b[j]
            j += 1
    elif j >= m:
        readed += a[i]
        i += 1
    elif a[i] < b[j]:
        readed += a[i]
        i += 1
    elif a[i] == b[j]:
        if i+1 <= n and j+1 <= m:
            if a[i+1] <= b[j+1]:
                readed += a[i]
                i += 1
            else:
                readed += b[j]
                j += 1
        elif i+1 <= n:
            readed += a[i]
            i += 1
        elif i+1 <= m:
            readed += b[j]
            j += 1
        else:
            readed += b[j]
            j += 1  

    else:
        readed += b[j]
        j += 1
    if readed > k:
        break
    count += 1
print(count)

Submission status: Runtime Error

Solution code:
k=int(input().split(-1))
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=[]
while len(a+b)>0:
	if len(b)==0 or len(a)>0 and a[0]<b[0]:
		c+=[a.pop(0)]
	else:
		c+=[b.pop(0)]
	if sum(c)>k:
		c.pop();break
print(len(c))

Submission status: Runtime Error

Solution code:
[n, m, k] = [int(stdin) for stdin in input().split()]
a = [int(stdin) for stdin in input().split()]
b = [int(stdin) for stdin in input().split()]

books_total = 0
possible_combo_c = [(0, 0, 0)]
while True:
    possible_combo_n = []
    for combo in possible_combo_c:
        if(combo[0] < n):
            combo_n_a = (combo[0]+1, combo[1], combo[2]+a[combo[0]])
            if combo_n_a[2] <= k:
                possible_combo_n.append(combo_n_a)
        if(combo[1] < m):
            combo_n_b = (combo[0], combo[1]+1, combo[2]+b[combo[0]])
            if combo_n_b[2] <= k:
                possible_combo_n.append(combo_n_b)
    if len(possible_combo_n) > 0:
        books_total += 1
        possible_combo_c = possible_combo_n[:]
    else:
        break

ans = books_total
print(ans)

Submission status: Runtime Error

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

idxA = idxB = 0
readTime :int = 0
readBooks :int = 0

"""
while readTime < k:
    #if arA[idxA] <= arB[idxB] and idxA < n:
    if idxA < n and arA[idxA] <= arB[idxB]:
        if k < readTime + arA[idxA]:
            break
        readTime += arA[idxA]
        idxA += 1
    else:
        if k < readTime + arB[idxB]:
            break
        readTime += arB[idxB]
        idxB += 1
    readBooks += 1
    if idxA >= n and idxB >= m:
        break
"""

while readTime < k:
    if idxA < n and arA[idxA] <= arB[idxB]:
        readTime += arA[idxA]
        idxA += 1
    else:
        readTime += arB[idxB]
        idxB += 1

    if readTime <= k:
        readBooks += 1
    else:
        break
    if idxA >= n and idxB >= m:
        break

print(readBooks)

Submission status: Runtime Error

Solution code:
N,M,K =map(int,input().split())
A =list(map(int,input().split()))
B =list(map(int,input().split()))
ans = 0
while K>0:
  ans+=1
  if A==[] or B[0]<=A[0] :
    K=K-B[0]
    B.pop(0)
  else:
    K=K-A[0]
    A.pop(0)
    
if K==0:
  print(ans)
else:
  print(ans-1)

Submission status: Runtime Error

Solution code:
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A.append(99999)
B.append(99999)
Sum = 0
i = 0
j = 0
while Sum < K:
  if A[i] <= B[j]:
    Sum += A[i]
    i += 1
  else:
    Sum += B[j]
    j += 1
if Sum >= 1000000000:
  print(0)
else:
  print(i + j)

Submission status: Runtime Error

Solution code:
N,M,K=input().split();
N=int(N);
M=int(M);
K=int(K);
A=input().split();
B=input().split();
A = [int(s) for s in A]
B = [int(s) for s in B]
time=0;
count=0;
while time<=K:
  if 0<len(A) and 0<len(A):
    if A[0]<B[0]:
      time=time+A.pop(0);
    else:
      time=time+B.pop(0);
  elif 0<len(A):
    time=time+A.pop(0);
  elif 0<len(B):
    time=time+B.pop(0);
  else:
    count=count+1;
    break;
  count=count+1;
print(count-1);

Submission status: Runtime Error

Solution code:
import numpy as np
import pandas as pd


N, M, K = map(int, input().split())
A = [int(n) for n in input().split()]
B = [int(n) for n in input().split()]

A = np.array(A)
B = np.array(B)

A = np.cumsum(A)
B = np.cumsum(B)

A1 = np.zeros((len(A),2))
A1[:,0] = A
A1[:,1] = np.arange(1,len(A)+1)

B1 = np.zeros((len(B),2))
B1[:,0] = B
B1[:,1] = np.arange(1,len(B)+1)

C1 = np.zeros((len(A)*len(B),2))
lenB = len(B)
for i in range(len(A)):
    for j in range(len(B)):
        C1[lenB*i+j,:] = A1[i,:] + B1[j,:]
C1 = pd.DataFrame(C1)
C1 = C1[C1[0] <= K]

print(int(C1[1].max()))

Submission status: Runtime Error

Solution code:
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
cum_a = [0]*(n+1)
for i, v in enumerate(a):
    cum_a[i+1] = cum_a[i]+a[i]
cum_b = [0]*(m+1)
for i, v in enumerate(b):
    cum_b[i+1] = cum_b[i]+b[i]
cum_b = [i for i in cum_b if i <= k]
cum_a = [i for i in cum_a if i <= k]
n = len(cum_a)-1
m = len(cum_b)-1
a_idx = n
b_idx = 0
res = 0
for i in range(n+1):
    current_a_idx = a_idx-i
    sum_a = cum_a[current_a_idx]
    if sum_a > k:
        continue
    sum_b = cum_b[b_idx]
    while sum_a+sum_b <= k and b_idx < m:
        b_idx += 1
        sum_b = cum_b[b_idx]
        if sum_a + sum_b > k:
            b_idx -= 1
            break
    res = max(b_idx+current_a_idx, res)
a_idx = 0
b_idx = m
for i in range(n+1):
    current_b_idx = b_idx-i
    sum_b = cum_b[current_b_idx]
    if sum_b > k:
        continue
    sum_a = cum_a[a_idx]
    while sum_a+sum_b <= k and a_idx < n:
        a_idx += 1
        sum_a = cum_a[a_idx]
        if sum_a + sum_b > k:
            a_idx -= 1
            break
    res = max(current_b_idx+a_idx, res)


print(res)

Submission status: Runtime Error

Solution code:
A = []
B = []
N = int(input())
M = int(input())
K = int(input())

for i in range(N):
  A.append(int(input()))
for j in range(M):
  B.append(int(input()))
           
red_books = 0
red_time = 0
books = sort(A + B)
for book in books:
  if red_time < K:
    red_time += book
    red_book += 1
  else:
    break
    
print(red_book)

Submission status: Runtime Error

Solution code:
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
a.append(10**9+1)
b.append(10**9+1)
rest=k
ans=-1
ai=0
bi=0
while rest>=0 and ai+bi<n+m:
    ans+=1
    if a[ai]<b[bi]:
        rest-=a[ai]
        ai+=1
    elif a[ai]>b[bi]:
        rest-=b[bi]
        bi+=1
    else:
        x=ai
        y=bi
        while a[x]==b[y]:
            x+=1
            y+=1
        if a[x]<b[y]:
            rest-=a[ai]
            ai+=1
        else:
            rest-=b[bi]
            bi+=1
if ai+bi==n+m:
  ans+=1
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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

# まずaを全部読む
# 全部読めたならbも読む
# 最後の本を捨てて、それがなければどこまでいけるかを試す

ans = 0
cost = 0
b_i = 0
while cost <= k:
  ans += 1
  if ans >= n:
    cost += b[n-ans]
    b_i += 1
  else:
    cost += a[ans]

max_ans = ans
if b_i == 0:
  for i in range(ans-1,-1,-1):
    ans -= 1
    cost -= a[i]
    while cost <= k and b_i < m:
      ans += 1
      cost += b[b_i]
      b_i += 1
    max_ans = max(max_ans, ans)
else:
  for i in range(n-1,-1,-1):
    ans -= 1
    cost -= a[i]
    while cost <= k and b_i < m:
      ans += 1
      cost += b[b_i]
      b_i += 1
    max_ans = max(max_ans, ans)
    
if not (a[0] >= k and b[0] >= k):
  print(max_ans)
else:
  print(0)

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit(400001)

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

A=list(map(int,input().split()))
B=list(map(int,input().split()))


def readAB(ia,ib,left_k,nbooks):
    global N,M,nmax

    # print(ia,ib,left_k,N,M,nbooks)

    if nbooks > nmax:
        nmax = nbooks

    if  ia <= N-1:
        if left_k >= A[ia]:
            # print("A")
            readAB(ia+1,ib,left_k-A[ia],nbooks+1)

    if ib <= M-1:
        if left_k >= B[ib]:
            # print("B")
            readAB(ia,ib+1,left_k-B[ib],nbooks+1)

nmax=0
readAB(0,0,K,0)
print(nmax)

Submission status: Runtime Error

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

tmp=0
ca=[0]
for ai in a:
  tmp+=ai
  ca.append(tmp)
tmp=0
cb=[0]
for bi in b:
  tmp+=bi
  cb.append(tmp)
import bisect
s=bisect.bisect_right(ca,k)-1
ans=ca[s]
now_a=s
z=k-ca[now_a]
now_b=bisect.bisect_right(cb,z)-1
t=ca[now_a]+cb[now_b]
ans=now_a+now_b
for i in range(s):
  now_a-=1
  t-=a[now_a]
  while t+b[now_b]<=k:
    t+=b[now_b]
    now_b+=1
  ans=max(ans,now_a,now_b)
print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque

N, M, K = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))
# print(N, M, K)
# print(A)
# print(B)
A = deque(A)
B = deque(B)

num_books = 0
remaining_time = K

while True:
    consumption = 0

    if len(A) == 0 and len(B) == 0: break

    if len(A) == 0:
        consumption = B.popleft()
    elif len(B) == 0:
        consumption = A.popleft()
    elif A[0] == B[0]:
        if A[1] < B[1]:
            consumption = A.popleft()
        else: 
            consumption = B.popleft()
    elif A[0] < B[0]:
        consumption = A.popleft()
    else:
        consumption = B.popleft()
    # print(consumption)
    
    remaining_time -= consumption
    if remaining_time < 0: break

    num_books += 1

print(num_books)

Submission status: Runtime Error

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

a.append(10**9+1)
b.append(10**9+1)

i,j,ans=0,0,0
while k>=0:
    if a[i]>10**9:
        k-=b[j]
        j+=1
    elif b[i]>10**9:
        k-=a[i]
        i+=1
    elif a[i]>b[j]:
        k-=b[j]
        j+=1
    else:
        k-=a[i]
        i+=1
    if k<0:
        break
    ans+=1

print(ans)

Submission status: Runtime Error

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

ans = 0
cnt = 0
cost = 0
for i, x in enumerate(a):
    cost += x
    if cost > k:
        cnt = i
        cost -= x
        break
else:
    for j, y in enumerate(b):
        cost += y
        if cost > k:
            cnt = n + j
            cost -= y
            break
    else:
        ans = n + m

if cnt <= n:
    i, j = cnt - 1, 0
else:
    i, j = n - 1, cnt - n + 1

while True:
    if cost == 0: break
    cost += - a[i] + b[j]
    i, j = i - 1, j + 1
    while cost + b[j] <= k:
        j += 1
        cnt += 1
    if cost > k: break
if ans != 0:
    print(ans)
else:
    print(cnt)

Submission status: Runtime Error

Solution code:
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 8)
from itertools import accumulate
from itertools import permutations
from itertools import combinations
from collections import defaultdict
from collections import Counter
import fractions
import math
from collections import deque
from bisect import bisect_left
from bisect import bisect_right
from bisect import insort_left
import itertools
from heapq import heapify
from heapq import heappop
from heapq import heappush
import heapq
from copy import deepcopy
from decimal import Decimal
alf = list("abcdefghijklmnopqrstuvwxyz")
ALF = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
#import numpy as np
INF = float("inf")
#d = defaultdict(int)
#d = defaultdict(list)
N,M,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
a = []
aa = 0
bb = 0
b = []
flag = 0
for i in range(N):
    aa += A[i]
    a.append(aa)
for i in range(M):
    bb += B[i]
    b.append(bb)
MAX = 0
for i in range(N):
    t = a[i]
    if t > K:
        break
    else:
        tt = K-t
        index = bisect_left(b,tt)
        if index == M:
            MAX = max(MAX,i+1+M)
        elif b[index] == tt:
            MAX = max(MAX,i+1+1+index)
        else:
            MAX = max(MAX,i+1+index)
for i in range(M):
    t = b[i]
    if t > K:
        break
    else:
        tt = K-t
        index = bisect_left(a,tt)
        if index == N:
            MAX = max(MAX,i+1+N)
        elif b[index] == tt:
            MAX = max(MAX,i+1+1+index)
        else:
            MAX = max(MAX,i+1+index)


        
print(MAX)

Submission status: Runtime Error

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

i, j = 0, 0
readed = 0
count = 0

while True:
    if i >= n:
        if j >= m:
            break
        else:
            readed += b[j]
            j += 1
    elif j >= m:
        readed += a[i]
        i += 1
    elif a[i] < b[j]:
        readed += a[i]
        i += 1
    elif a[i] == b[j]:
        if i+1 >= n and j+1 >= m:
            if a[i+1] <= b[j+1]:
                readed += a[i]
                i += 1
            else:
                readed += b[j]
                j += 1
        elif i+1 >= n:
            readed += a[i]
            i += 1
        elif i+1 >= m:
            readed += b[j]
            j += 1
        else:
            readed += b[j]
            j += 1  

    else:
        readed += b[j]
        j += 1
    if readed > k:
        break
    count += 1
print(count)

Submission status: Runtime Error

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

time = 0
count = 0

if((A_list[0]>K)and(B_list[0]>K)):
  print("0")
  exit()
while(time<K):
  if((len(A_list)==0)and(len(B_list)==0)):
    #print("break")
    break
  elif((len(A_list)==0)or(A_list[0]>B_list[0])):
    #print("B")
    time += B_list[0]
    del B_list[:1]
  elif((len(B_list)==0)or(A_list[0]<B_list[0])):
    #print("A")
    time += A_list[0]
    del A_list[:1]
  if(time<=K):
    count += 1
    
print(count)

Submission status: Runtime Error

Solution code:
a,b,K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
total=0
number=0
while K>total:
  if A[a-1]<B[b-1]:
    total=total+A[a-1]
    a=a-1
    number=number+1
  else:
    total=total+B[b-1]
    b=b-1
    number=number+1
print(number)

Submission status: Runtime Error

Solution code:
from collections import deque

N, M, K = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))
# print(N, M, K)
# print(A)
# print(B)
A = deque(A)
B = deque(B)

num_books = 0
remaining_time = K

while True:
    consumption = 0

    if len(A) == 0 and len(B) == 0: break

    if len(A) == 0:
        consumption = B.popleft()
    elif len(B) == 0:
        consumption = A.popleft()
    elif A[0] == B[0]:
        if A[1] < B[1]:
            consumption = A.popleft()
        elif: 
            consumption = B.popleft()
    elif A[0] < B[0]:
        consumption = A.popleft()
    else:
        consumption = B.popleft()
    # print(consumption)
    
    remaining_time -= consumption
    if remaining_time < 0: break

    num_books += 1

print(num_books)

Submission status: Runtime Error

Solution code:
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
time = 0
co =0
flag = True
while time <=k:
    if len(a) ==0 and len(b) ==0:
        co +=1
        Flag = False
        break

    elif len(a) ==0:
        time += b[0]
        co += 1
        p = b[0]
        b.remove(p)

    elif a[0] >= b[0]:
        time += b[0]
        co += 1
        p = b[0]
        b.remove(p)

    elif len(b) ==0:
        time += a[0]
        co += 1
        q = a[0]
        a.remove(q)

    else :
        time += a[0]
        co +=1
        q = a[0]
        a.remove(q)
if flag:
    print(co - 1)
else :
    print(co)

Submission status: Runtime Error

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

a = sum(A)
b = sum(B)
c = min(min(A),min(B))

if c > K:
  print("0")
  exit()
if a + b <= K:
  print(len(A)+len(B))
  exit()
  
q = 0
n = 0
l = 1
while q <= K:
  if A[0] > B[0]:
    q += B[0]
    B.pop(0)
    n += 1
  elif A[0] < B[0]:
    q += A[0]
    A.pop(0)
    n += 1
  elif A[0] == B[0]:
      for i in range(max(len(A),len(B))):
          if A[i] != B[i]:
              break
          l += 1
      if A[l] > B[l]:
          q += B[0]
          B.pop(0)
          n += 1
      elif A[l] < B[l]:
          q += A[0]
          A.pop(0)
          n += 1
print(n-1)

Submission status: Runtime Error

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

while time < K:
    if A[numA] < B[numB]:
        time += A[numA]
        if time <= K:
            numA += 1
        else:
            break
    else:
        time += B[numB]
        if time <= K:
            numB += 1
        else:
            break

print(numA + numB)

Submission status: Runtime Error

Solution code:
N,M,K = map(int, input().split())
S = N + M

A = list(map(int, input().split()))
B = list(map(int, input().split()))

sum = 0

for s in range(S):

    if len(A)==0 or A[0]>B[0]:
        sum=sum+B[0]
        B.pop(0)
    elif len(B)==0 or A[0]<B[0]:
        sum=sum+A[0]
        A.pop(0)
    if K <= sum:
        break
else:
    s+=1