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()))

ans = 0
read_time = 0
while 1:
    if len(a) == 0 and len(b) == 0:
        break
    if len(a) == 0:
        read_time += b.pop(0)
        if read_time > K:
            break
        else:
            ans += 1
            continue
    if len(b) == 0:
        read_time += a.pop(0)
        if read_time > K:
            break
        else:
            ans += 1
            continue
    # aとbのどちらを読むか判定
    if a[0] > b[0]:
        read_time += b.pop(0)
        if read_time > K:
            break
        else:
            ans += 1
    if a[0] <= b[0]:
        read_time += a.pop(0)
        if read_time > K:
            break
        else:
            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]
    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:
# 別変数の整数複数個
M, M, K = [int(x) for x in input().strip().split(" ")]
A = [int(x) for x in input().strip().split(" ")]
B = [int(x) for x in input().strip().split(" ")]

def pick(A, B):
    # print(A, B)
    if len(A) == 0 and len(B) > 0:
        return False
    if len(B) == 0 and len(A) > 0:
        return True
    if A[0] < B[0]:
        return True
    elif A[0] > B[0]:
        return False
    return pick(A[1:], B[1:])

total = 0
cur = 0
while True:
    if pick(A, B):
        total += A.pop(0)
    else:
        total += B.pop(0)
    if total > K:
        break
    cur += 1
    if len(A) + len(B) == 0:
        break
print(cur)

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()))
 
cnt = 0

while K >= 0:
    if K < min(a[0], b[0]):
        cnt += 1
        break
        
    if len(a)==0 or len(b)==0:
        if len(a) == 0:
            K -= b.pop(0)
        elif len(b) == 0:
            K -= a.pop(0)
        else:
            cnt += 1
            break
    else:
        if a[0] <= b[0]:
            K -= a.pop(0)
        elif a[0] > b[0]:
            K -= b.pop(0)
    cnt += 1
print(cnt-1)

Submission status: Runtime Error

Solution code:
from collections import deque

n,m,k = map(int,input().split())
A = deque(map(int,input().split()))
B = deque(map(int,input().split()))
ans = 0

while len(A) > 0 and len(B) >0 and k>0:
  if A[0] < B[0]:
    k -= A[0]
    A.popleft()
    ans += 1
  elif A[0] > B[0]:
    k -= B[0]
    B.popleft()
    ans += 1
  elif A[0] == B[0]:
    if len(A) == 1 and B[1] <= B[0]:
      k -= B[0]
      B.popleft()
      ans += 1
    elif len(A) == 1 and B[1] > B[0]:
      k -= A[0]
      A.popleft()
      ans += 1
    elif len(B) == 1 and A[1] <= A[0]:
      k -= A[0]
      A.popleft()
      ans += 1
    elif len(B) == 1 and A[1] > A[0]:
      k -= B[0]
      B.popleft()
      ans += 1
    else:
      if A[1] < B[1]:
        k -= A[0]
        A.popleft()
        ans += 1
      elif A[1] >= B[1]:
        k -= B[0]
        B.popleft()
        ans += 1
while len(A) > 0 and k > 0:
  k -= A[0]
  A.popleft()
  ans += 1
while len(B) > 0 and k > 0:
  k -= B[0]
  B.popleft()
  ans += 1
if k == 0:
  print(ans)
else:
  print(ans-1)

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()))

ans = 0

if ab_min > K:
    ans = 0
else:
    while True:
        K = K - min(A[0],B[0])
        if K < 0:
            break
        ans += 1
        if min(A[0],B[0])==A[0]:
            A.pop(0)
            if A == []:
                A.append(10**9+1)
        else:
            B.pop(0)
            if B == []:
                B.append(10**9+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()))

def doku(li, K, time):
  cnt = 0
  tmp = 0
  for i in range(len(li)):
    if time+li[i] <= K:
      tmp += li[i]
      cnt += 1
    else:
      return cnt
  return cnt

ans = 0
time = 0
while time+min(A[0], B[0]) <= K:
  plus = min(A[0], B[0])
  time += plus
  ans += 1
  if A[0] <= B[0]:
    A.pop(0)
    if not A:
      ans += doku(B, K, time)
      print('A')
      break;
  else:
    B.pop(0)
    if not B:
      ans += doku(A, K, time)
      print('B')
      break;
  print(A, B, time, ans)
    
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から選んでみる
minutes_a = 0
count_a = 0
for i in range(N):
    if minutes_a + A[i] > K:
        break
    minutes_a += A[i]
    count_a += 1

max_count = count_a
minutes_b = 0
count_b = 0

while count_a > 0:
    for i in range(count_b, M):
        if minutes_a - A[count_a - 1] + minutes_b + B[i] > K:
            break
        minutes_a -= A[count_a - 1]
        count_a -= 1
        minutes_b += B[i]
        count_b += 1

for i in range(count_b, M):
    if minutes_a + minutes_b + B[i] > K:
        minutes_b += B[i]
        count_b += 1


print(count_a + count_b)

Submission status: Runtime Error

Solution code:
global info
global A
global B

def calc(a, b, total):
  if a == info[0]-1 and b == info[1]-1:
    return a+b+2
  if a >= info[0] or b >= info[1]:
    return a + b
  if total + A[a] > info[2] and total + B[b] > info[2]:
    return a + b
  else:
    return max(calc(a+1, b, total+A[a]), calc(a, b+1, total+B[b]))
  
info = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
a, b, total = 0, 0, 0

print(calc(a, b, total))

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)
idx_a = 0
idx_b = 0
c = 0
while k > 0:
    c += 1
    if a[idx_a] > b[idx_b]:
        k -= b[idx_b]
        idx_b += 1
    elif a[idx_a] < b[idx_b]:
        k -= a[idx_a]
        idx_a += 1
    else:
        tmpa = idx_a
        tmpb = idx_b
        while tmpa <= len(a) and a[tmpa] == b[tmpb]:
            tmpa += 1
            tmpb += 1
        if a[tmpa] < b[tmpb]:
            k -= a[idx_a]
            idx_a += 1
        else:
            k -= b[idx_b]
            idx_b += 1
if k < 0:
    c -= 1
print(c)

Submission status: Runtime Error

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

    read = deque()

    while sum(read) <= K:
        if len(A) == 0 and len(B) == 0:
            break

        if len(A) == 0:
            if sum(read) + B[0] <= K:
                read.append(B[0])
            B.pop(0)
            continue

        if len(B) == 0:
            if sum(read) + A[0] <= K:
                read.append(A[0])
            A.pop(0)
            continue

        if A[0] < B[0]:
            if sum(read) + A[0] <= K:
                read.append(A[0])
            A.pop(0)
        else:
            if sum(read) + B[0] <= K:
                read.append(B[0])

            B.pop(0)

    print(len(read))


resolve()

Submission status: Runtime Error

Solution code:
# coding:utf-8
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

ans = 0
time = 0

if sum(a) + sum(b) <= k:
    print(len(a) + len(b))
    exit()

while time <= k:
    if len(a) == 0 and len(b) == 0:
        print(ans)
        exit()
    elif len(a) == 0:
        book = b[0]
    elif len(b) == 0:
        book = a[0]
    else:
        book = min(a[0], b[0])

    time += book
    if time > k:
        print(ans)
        exit()
    ans += 1
    if book == a[0]:
        a.pop(0)
    else:
        b.pop(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()))

ans = 0
k_list = []

flag = True

if sum(a) + sum(b) <= k:
    flag = False
    print(n+m)

while k > 0:
    if len(a) != 0 and a[0] <= b[0]:
        k -= a.pop(0)
        ans += 1
    else:
        k -= b.pop(0)
        ans += 1


if flag:
    print(ans-1)

Submission status: Runtime Error

Solution code:
#ABC172C
n, m, k = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
i = 0 #Aの掘り具合
j = 0 #Bの掘り具合
time = 0
ans = 0 #読んだ本の冊数
while time < k:
    if (i+1 == len(A) or A[i] > B[j]) and time + B[j] <= k:
        time = time +B[j]
        ans = ans + 1
        j = j + 1
    elif (j+1 == len(B) or A[i] < B[j]) and time + A[i] <= k:
        time = time + A[i]
        ans = ans + 1
        i = i + 1
    if i + 1 == len(A) or j + 1 == len(B):
        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()))



Asum=[0]
for i in A:
  if Asum[-1]+i>K:break
  Asum.append(Asum[-1]+i)
    
Bsum=[0]
for i in B:
  if Bsum[-1]+i>K:break
  Bsum.append(Bsum[-1]+i)

maxval=len(Asum)-1

tmp=0
for i in range(len(Asum)):
  for j in range(len(Bsum)):
    j=tmp+j
    if Asum[-i-1]+Bsum[j]>K:
      maxval=max(maxval,len(Asum)-1-i+j-1)
      tmp=j
      break
    elif j==len(Bsum)-1:
      maxval=max(maxval,len(Asum)-1-i+j)
      
print(maxval)

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
count = 0
i = 0
j = 0
while i <len(a) or j <len(b):
    if i < len(a):
        A = a[i]
    else:
        A = 10**10
    if i < len(b):
        B = b[j]
    else:
        B = 10**9
    if A<=B:
        time += A
        i += 1
    else:
        time += B
        j += 1
    if time > k:
        break
    count += 1

print(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()))
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]


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:
N,M,K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
t = 0
ans = 0
a = 0
b = 0
while t<K:
  if a==N and b<M:
    t+=B[b]
    b+=1
    if t>K:
      break
    else:
      ans+=1
    continue
  if b==M and a<N:
    t+=A[a]
    a+=a
    if t>K:
      break
    else:
      ans+=1
    continue

  if A[a] < B[b]:
    t+=A[a]
    a+=1
    if t>K:
      break
    else:
      ans+=1
    continue
  else:
    t+=B[b]
    b+=1
    if t>K:
      break
    else:
      ans+=1
    continue
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()))

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

Submission status: Runtime Error

Solution code:
a.append("1000000001")
b.append("1000000001")
a[n]=int(a[n])
b[m]=int(b[m])

x=0
y=0
ans=0
time=0

for i in range(n+m):
    if a[x]<=b[y]:
        time+=a[x]
        if time>k:
            time-=a[x]
            print(ans)
            sys.exit()
        x+=1
        ans+=1
    else:
        time+=b[y]
        if time>k:
            time-=b[y]
            print(ans)
            sys.exit()   
        y+=1
        ans+=1
print(ans)

Submission status: Runtime Error

Solution code:
[N,M,K]=list(map(int, input().split()))
alist=list(map(int, input().split()))
blist=list(map(int, input().split()))
alist.sort()
blist.sort()
time=0
ans=-1
ai=0
bi=0
while time<=K:
    ans+=1
    if alist==[] and blist==[]:
        break
    elif alist==[]:
        time+=blist[bi]
        bi+=1
    elif blist==[]:
        time+=alist[ai]
        ai+=1
    elif alist[ai]>blist[bi]:
        time+=blist[bi]
        bi+=1
    else:
        time+=alist[ai]
        ai+=1
print(ans)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict

n,m,k = map(int, input().split())
a = list(map(int, input().split()))
a.append(10**9+1)
b = list(map(int, input().split()))
b.append(10**9+1)
ans = 0
count = 0
while(ans<=k):
    #print(ans)
    if count==n+m:
      print(count)
      sys.exit()
    count+=1
    if a[0] > b[0]:
        ans+=b[0]
        b.pop(0)
    elif a[0] < b[0]:
        ans+=a[0]
        a.pop(0)
    else:
        for i in range(1,200000):
            if a[i] > b[i]:
                ans+=b[0]
                b.pop(0)
                break
            elif a[i]<b[i]:
                ans+=a[0]
                a.pop(0)
                break
print(count-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()))
min = 0
count = 0
def set_min():
    global min
    if len(A) == 0 or B[0] < A[0]:
        min = B.pop(0)
    elif len(B) == 0 or A[0] <= B[0]:
        min = A.pop(0)
set_min()
while min <= K:
    K -= min
    count += 1
    if not A and not B:
        print(count)
        exit()
    set_min()
print(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()))

suma = [0]
for i in range(n):
    suma.append(suma[i] + a[i])
    
sumb = [0]
for i in range(m):
    sumb.append(sumb[i] + b[i])
    
ans = 0
for i in range(n + 1):
    for j in range(ans - i + 1, m + 1):
        if suma[i] + sumb[j] <= k:
            ans = i + j

print(ans)

Submission status: Runtime Error

Solution code:
n, m, k=map(int, input().split())
a=list(map(int, input().split()))+[10**10]*(10**10)
b=list(map(int, input().split()))+[10**10]*(10**10)

loop=max(n, m)
i=0
j=0
T=0
ans=0

for t in range(2*loop):
    if a[i]>=b[j]:
        T+=b[j]
        if T<=k:
            ans+=1
            j+=1
        else:
            print(ans)
            exit()
    elif b[j]>a[i]:
        T+=a[i]
        if T<=k:
            ans+=1
            i+=1
        else:
            print(ans)
            exit()
    
    elif i==n-1:
        T+=b[j]
        if T<=k:
            ans+=1
            j+=1
        else:
            print(ans)
            exit()
    
    elif j==m-1:
        T+=a[i]
        if T<=k:
            ans+=1
            i+=1
        else:
            print(ans)
            exit()

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()))
count=0
while True:
    if A[0]<=B[0] or B==[]:
        count+=1
        K-=A.pop(0)
    else:
        count+=1
        K-=B.pop(0)

    if K<0:
        count-=1
        break

    if A==[] and B==[]:
        break
print(count)

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)] #a0のとき
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:
import numpy as np
n,m,k=map(int,input().split())
a=list(map(int, input().split()))
b=list(map(int, input().split()))
A=np.cumsum(np.array(a))
B=np.cumsum(np.array(b))
ans=0
t=0
i=0 #a
j=0 #b
while t<=k:
    if i<n and A[i]<B[j]:
        t+=a[i]
        i+=1
    elif j<m:
        t+=b[j]
        j+=1
    else:
        break
    if t<=k:
        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()))


index = 0
index2 = 0
stackedTime = 0

while(index<N and stackedTime + A[index] <= K):
  stackedTime += A[index]
  index += 1

while(index2 < M and stackedTime + B[index2] <= K):
  stackedTime += B[index2]
  index2 += 1

maxBook = index + index2
for i in range(N):
  if(index == 0 or index2 == M):
    break
  index -= 1
  stackedTime -= A[index]
  while(index2 < M and stackedTime + B[index2] <= K):
    stackedTime += B[index]
    index2 += 1

  maxBook = max(maxBook, index + index2)

print(maxBook)

Submission status: Runtime Error

Solution code:
import numpy as np
def main():
    N, M, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    
    ia = 0
    ib = 0
    cost = 0
    while True:
        if ia < len(A) and ib < len(B):
            if A[ia] < B[ia]:
                if cost + A[ia] > K:
                    break
                ia += 1
                cost += A[ia]
            else:
                if cost + B[ib] > K:
                    break
                ib += 1
                cost += B[ib]
        elif ia < len(A):
            if cost + A[ia] > K:
                break
            ia += 1
            cost += A[ia]
        elif ib < len(B):
            if cost + B[ib] > K:
                break
            ib += 1
            cost += B[ib]
        else:
            break
    
    count = ia + ib
    print(count)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
bhon = int(deta[1])
time = int(deta[2])

alist = list(input().split(" "))
blist = list(input().split(" "))

book = 0
while time > 0:
    if alist == [] and blist == []:
        break
    elif alist == []:
        if int(blist[0]) > time:
            break
        else:
            time -= int(blist[0])
            book += 1
            blist.pop(0)
    elif blist == []:
        if int(alist[0]) > time:
            break
        else:
            time -= int(alist[0])
            book += 1
            alist.pop(0)
    elif int(alist[0]) <= int(blist[0]):
        if int(alist[0]) > time:
            break
        else:
            time -= int(alist[0])
            book += 1
            alist.pop(0)
    elif int(alist[0]) > int(blist[0]):
        if int(blist[0]) > time:
            break
        else:
            time -= int(blist[0])
            book += 1
            blist.pop(0)

print(book)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys

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

R = K
A_index = 0
B_index = 0
ans = 0
while N > 0:
    if R >= A[A_index]:
        R = R - A[A_index]
        ans += 1
        A_index += 1
        N -= 1
    else:
        break

while M > 0:
    if R >= B[B_index]:
        R = R - B[B_index]
        ans += 1
        B_index += 1
        M -= 1
    else:
        break

if N == 0 and M == 0:
    print(ans)
    sys.exit()

max = ans

while True:
    if A_index >= 0:
        R += A[A_index]
        A_index -= 1
        ans -= 1
        while True:
            if R >= B[B_index]:
                R -= B[B_index]
                B_index += 1
                ans += 1
            else:
                break
        
        if ans > max:
            max = ans
    else:
        break

print(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()))
a = 0
b = 0
ans = 0
if sum(A) + sum(B) <= K:
    print(N+M)
    exit()
A.append(10**9+1)
B.append(10**9+1)
while min(A[a],B[b]) <= K:
    if A[a] == B[b]:
        aa = a
        bb = b
        while A[aa] == B[bb]:
            aa += 1
            bb += 1
        if A[aa] < B[bb]:
            K -= A[a]
            a += 1
        else:
            K -= B[b]
            b += 1
    elif A[a] == min(A[a],B[b]):
        K -= A[a]
        a += 1
    else:
        K -= B[b]
        b += 1
    ans += 1
print(ans)

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()))


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 and left_k >= A[ia]:
        # print("A")
        readAB(ia+1,ib,left_k-A[ia],nbooks+1)

    if ib <= M-1 and 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=[int(x) for x in input().split()]
b=[int(x) for x in input().split()]
t=0
for p in range(n):
    if t+a[p]>k:
        p-=1
        break
    t+=a[p]
for q in range(m):
    if t+b[q]>k:
        q-=1
        break
    t+=b[q]
ans=p+q+2
if q==m-1:
    print(ans)
else:
    z=ans
    r=0
    for r in range(p,-1,-1):
        t-=a[q]
        z-=1
        while r<m and t+b[r]<=k:
            t+=b[r]
            r+=1
            z+=1
        if z>ans:
            ans=z
        if r==m-1:
            break
    print(ans)

Submission status: Runtime Error

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

a = list(map(int,input().split()))[::-1]
b = list(map(int,input().split()))[::-1]

#print(a)
#print(b)
ans = 0
while 1:
    print(k, a,b)
    if b == [] and a == []:
        break
    elif b == [] and a != []:
        if a[-1] <= k:
            k -= a.pop()
            ans += 1
            pass
        else:
            break
    elif a == [] and b != []:
        if b[-1] <= k:
            k -= b.pop()
            ans += 1
            pass
        else:
            break
    if a[-1] > b[-1]:
        if b[-1] <= k:
            k -= b.pop()
            ans += 1
        else:
            break
    else:
        if a[-1] <= k:
            k -= a.pop()
            ans += 1
        else:
            break



print(ans)

Submission status: Runtime Error

Solution code:
def input_int():
    return map(int, input().split())

def one_int():
    return int(input())

def one_str():
    return input()

def many_int():
    return list(map(int, input().split()))

N, M, K = input_int()
A = many_int()
B = many_int()


def dfs(A_index, B_index, K, books):
    
    return_books = books
    return_booksA = books

    if N > A_index and K>=A[A_index]:
        return_books = dfs(A_index+1, B_index, K-A[A_index], books+1)
    if M > B_index and K>=B[B_index]:
        return_booksA = dfs(A_index, B_index+1, K-B[B_index], books+1)

    return max(return_books, return_booksA)

print(dfs(0, 0, K, 0))

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)

n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
def search(x, y, t):
    if t > k:
        return x + y - 1
    elif x == n and y == m:
        return x + y
    elif x == n or a[x] > b[y]:
        return search(x, y + 1, t + b[y])
    elif y == m or a[x] < b[y]:
        return search(x + 1, y, t + a[x])
    return max(search(x + 1, y, t + a[x]), search(x, y + 1, t + b[y]))

ans = search(0, 0, 0)
print(ans)

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()))
count = 0
ai = 0
bi = 0
while True:
  if len(a) != ai and a[ai] <= b[bi]:
    k -= a[ai]
    ai += 1
  elif len(b) != bi:
    k -= b[bi]
    bi += 1
  else:
    break
  if k < 0:
    break
  count += 1
print(count)

Submission status: Runtime Error

Solution code:
for i in range(len(C)):
  countA=0
  countB=0
  ans=0
  answer=0
  for j in range(len(c)):
    if C[i][j]==0:
      ans+=A[countA]
      countA+=1
    else:
      ans+=B[countB]
      countB+=1
    if ans>=K and countA+countB>answer:
      answer=countA+countB
      break
print(answer)

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()))
 
cnt = 0

while K >= 0:
    if len(a)==0 or len(b)==0:
        if len(a) == 0:
            K -= b.pop(0)
        elif len(b) == 0:
            K -= a.pop(0)
        else:
            cnt += 1
            break
    else:
        if a[0] <= b[0]:
            K -= a.pop(0)
        elif a[0] > b[0]:
            K -= b.pop(0)
    cnt += 1
print(cnt-1)

Submission status: Runtime Error

Solution code:
from collections import deque

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

ans = 0
minutes = 0
while True:
    if len(a) == 0 and len(b) == 0:
        break
    elif len(a) > 0 and len(b) > 0:
        if a[0] == b[0]:
            if ans + a[0] <= k:
                if a[1] >= b[1]:
                    minutes += 1
                    ans += b.popleft()
                else:
                    minutes += 1
                    ans += a.popleft()
            else:
                break
        elif a[0] > b[0]:
            if ans + b[0] <= k:
                minutes += 1
                ans += b.popleft()
            else:
                break
        else:
            if ans + a[0] <= k:
                minutes += 1
                ans += a.popleft()
            else:
                break

    elif len(b) == 0 and (a[0] + ans) <= k:
        minutes += 1
        ans += a.popleft()
    elif len(a) == 0 and (b[0] + ans) <= k:
        minutes += 1
        ans += b.popleft()
    else:
        break

print(minutes)
# print(ans)

Submission status: Runtime Error

Solution code:
(n,m,k),*A,=[list(map(int,i.split())) for i in open(0)]
a=A[0]
b=A[1]

time=0
ans=0
anss=[]
i=0
def dfs(time,ans,a,b):
    if time>k:
        anss.append(ans-1)
    elif time==k:
        anss.append(ans)
    else:
        if len(a)>0:
            dfs(time+a[0],ans+1,a[1:],b)
        if len(b)>0:
            dfs(time+b[0],ans+1,a,b[1:])
        if len(a)==0 and len(b)==0:
            anss.append(ans)
dfs(0,0,a,b)
print(max(anss))

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 :
    if k >= a[0] or k >= b[0] :
        if a[0] > b[0] :
            ans += 1
            k = k - b[0]
            del b[0]
            if len(b) == 0 :
                b.append(2000000000)
        elif b[0] > a[0] :
            ans += 1
            k = k - a[0]
            del a[0]
            if len(a) == 0 :
                a.append(2000000000)
        elif a[0] == b[0] :
            ans += 1
            for j in range(100000):
                if a[j] > b[j] :
                    k = k - b[0]
                    del b[0]
                    if len(b) == 0 :
                        b.append(2000000000)
                    break
                elif a[j] < b[j] :
                    k = k - a[0]
                    del a[0]
                    if len(a) == 0 :
                        a.append(2000000000)
                    break
    else :
        break
print(ans)

Submission status: Runtime Error

Solution code:
a=input().split(" ")
b=input().split(" ")
c=input().split(" ")
d=0
e=0
f=0
count=0
while int(a[2])>=e and int(a[2])>=int(b[0]):
    if d<3:
        e=e+int(b[d])
        d=d+1
        count+=1
    elif f<4 and d>=3:
        e=e+int(c[f])
        f=f+1
        count+=1
    else:
        break

print(count)

Submission status: Runtime Error

Solution code:
n,m,k = input().split()
N = int(n)
M = int(m)
K = int(k)
a_t = input().split()
b_t = input().split()
A_Times = list()
B_Times = list()
for i in range(N):
  A_Times.append(int(a_t[i]))
for i in range(M):
  B_Times.append(int(b_t[i]))
total_time = 0
num_books = 0
while total_time<=K:
  min_time = B_Times[0]
  if A_Times[0]<B_Times[0]:
    min_time = A_Times[0]
    del A_Times[0]
  else: del B_Times[0]
  if total_time+min_time<=K:
    total_time += min_time
    num_books+=1
  else:	break
print(num_books)