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:
A,B, K=map(int,input().split())
List_A = list(map(int, input().split()))
List_B = list(map(int, input().split()))
res = 0
a=0
b=0
inter = []
inter2 = 0
n = A+B
for i in range(n):
  if a ==A and b !=B:
    inter.append(List_B[b])
    b +=1
  elif a !=A and b ==B:
    inter.append(List_A[a])
    a +=1
  else:
    if List_A[a]>List_B[b]:
      inter.append(List_B[b])
      b +=1
    elif List_A[a] < List_B[b]:
      inter.append(List_A[a])
      a +=1
    else:
      k = min(A-a, B-b)
      for j in range(1,k):
        if List_B[b+j] > List_A[a+j]:
          inter.append(List_A[a])
          a +=1
          break
        elif List_A[a+j] > List_B[b+j]:
          inter.append(List_B[b])
          b +=1
          break
        else:
          if j == k-1:
            if B-b > A-a:
              inter.append(List_A[a])
              a +=1
              break
            elif A-a > B-b:
              inter.append(List_B[b])
              b += 1
              break
          else:
            pass
for i in range(n):
  inter2 += inter[i]
  if K < inter2:
    break
  else:
    res +=1
print(res)

Submission status: Runtime Error

Solution code:
A=[]
B=[]
books = 0
time = 0
n, m, k = map(int,input().split())
i = n-1
j = m - 1
for i in range(0,n):
    A.append(int(input()))
for i in range(0,m):
    B.append(int(input()))
while not(time > k) :
    if (i> -1 and j> -1 and A[i]<B[j]) or (i>-1 and not(j>-1)):
        time = time+A[i]
        i=i-1
        books=books+1
    elif (i> -1 and j> -1 and not(A[i]<B[j])) or (j>-1 and not(i>-1)):
            time = time+A[i]
            i=i-1
            books=books+1
print(books)

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()))
 
T_A = [0, A[0]]
T_B = [0, B[0]]
for i in range(1, N):
  T_A.append(T_A[i] + A[i])
for j in range(1, M):
  T_B.append(T_B[i] + B[i])
      
book = 0
for i in range(len(T_A)):
  for j in range(len(T_B)):
    if K < T_A[i] + T_B[j]:
      break
    else:
      if i + j  > book:
        book = i + j
      else:
        pass
    
print(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()))

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(1000000):
                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:
def ABC172_C():
    n,m,k = map(int,input().split())
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))

    if k < a[0] or k < b[0]:
        print(0)
        return

    minamo = []
    def saiki(i,j,sum):
        if sum > k:
            minamo.append(i+j)
            return

        if sum == k:
            minamo.append(i+j+1)
            return

        if i+1 <= n:
            saiki(i+1,j,sum+a[i])
        if j+1 <= m:
            saiki(i,j+1,sum+b[j])

    saiki(0,0,0)
    print(max(minamo)-1)

ABC172_C()

Submission status: Runtime Error

Solution code:
N,M,K=map(int,input().split())
booka=list(map(int,input().split()))
bookb=list(map(int,input().split()))
answer=0
while len(booka)*len(bookb)>0 and K-booka[0]>=0 and K-bookb[0]>=0:
  if booka[0]<bookb[0]:
    K-=booka[0]
    answer+=1
    booka=booka.pop(0)
  elif booka[0]==bookb[0]:
    n=1
    while booka[n]=bookb[n]=booka[0]:
      n+=1
      if n>len(booka) 
        listp=[2*len(booka),math.floor(K/booka[0])]
        answer+=min(listp)
        break
      print(answer)
      elif booka[n]<bookb[n]:
        K-=booka[0] 
        answer+=1
        booka=booka.pop(0)
      else:
        K-=bookb[0]
        answer+=1
        bookb=bookb.pop(0)
  else:
    K-=bookb[0]
    answer+=1
    bookb=bookb.pop(0)

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

read = 0

i = 0
j = 0

while K - A[i] >= 0 or K - B[j] >= 0:
    if N > 0 and M > 0:
        if A[i] < B[j]:
            N -= 1
            read += 1
            K -= A[i]
            i += 1
        elif A[i] > B[j]:
            M -= 1
            read += 1
            K -= B[j]
            j += 1
        else:
            i2 = i
            j2 = j
            while A[i2] == B[j2]:
                if A[i2] < B[j2]:
                   N -= 1
                   read += 1
                   K -= A[i]
                   i += 1
                   break
                elif A[i2] > B[j2]:
                   M -= 1
                   read += 1
                   K -= B[j]
                   j += 1
                   break
                else:
                   i2 += 1
                   j2 += 1
    elif N <= 0 and M > 0:
        M -= 1
        read += 1
        K -= B[j]
        j += 1
    elif N > 0 and M <= 0:
        N -= 1
        read += 1
        K -= A[i]
        i += 1
    else:
        break

print(read)

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
t = 0
if k<a[0] and k<b[0]:
  print(0)
else:
  while t < k:
    ans += 1
    if a==[] and b==[]:
      print(n+m)
      exit()
    elif a==[] and b!=[]:
      t += b[0]
      b.pop()
    elif b==[] and a!=[]:
      t += a[0]
      a.pop()
    else:
      if a[0] > b[0]:
        t += a[0]
        a.pop()
      elif a[0] < b[0]:
        t += b[0]
        b.pop()
      elif a[0] == b[0]:
        if a[1] <= b[0]:
          t += a[0]
          a.pop()
        elif b[1] <= a[0]:
          t += b[0]
          b.pop()
  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
count = 0
i = 0
j = 0
for _ in range(n + m):
    if i > len(a) - 1:
        time += b[j]
        j += 1
    elif j > len(b) -1 :
        time += a[i]
        i += 1
    elif i > len(a)-1 and j > len(b)-1:
        break
    else:
        if a[i] < b[j]:
            time += a[i]
            i +=1
        elif a[i] > b[j]:
            time += b[j]
            j +=1
        else:
            time += a[i]
            if a[i + 1] < b[j+1]:
                i += 1
            else:
                j += 1
    if time > k:
        break
    else:
        count += 1
print(count)

Submission status: Runtime Error

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

time = 0
count = 0
while len(a) > 0 or len(b) > 0:
    if len(a) ==  0:
        time += b.popleft()
        count += 1
    elif len(b) == 0:
        time += a.popleft()
        count+= 1
    elif a[0] > b[0]:
        time += b.popleft()
        count += 1
    elif a[0] == b[0]:
        if a[1] > b[1]:
            time += b.popleft()
        else:
            time += a.popleft()
        count += 1            
    else:
        time += a.popleft()
        count += 1
    if time > k:
        count -= 1
        break

    
print(count)

Submission status: Runtime Error

Solution code:
i = list(map(int, input().split()))
n=i[0]
m=i[1]
k=i[2]
A = list(map(int, input().split()))
B = list(map(int, input().split()))
a=b=0 #それぞれの読んだ数
cal=0 #読んだ時間
if max(a)>1000000000 or max(b)>1000000000:
    print(0)
else:
    while True:
        if a!=n and b!=m:
            if A[a]<B[b]:
                cal+=A[a]
                a+=1
                if cal>k:
                    a-=1
                    break
            elif B[b]<A[a]:
                cal+=B[b]
                b+=1
                if cal>k:
                    b-=1
                    break
        elif a==n and b==m:
            break
        elif a==n:
            cal+=B[b]
            b+=1
            if cal>k:
                b-=1
                break
        elif b==m:
            cal+=A[a]
            a+=1
            if cal>k:
                a-=1
                break
    print(a+b)

Submission status: Runtime Error

Solution code:
from collections import deque
N, M, K = map(int, input().split())

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

ans = 0
dic = {}
def dfs(i, j, cost):
    global ans
    if cost > K:
        ans = max(ans, i+j-1)
        return i+j-1
    if i == N and j==M:
        ans = j+i
        return i+j

    if i==N and j<M:
        if (i, j+1, cost+B[j]) in dic:
            m = dic[(i, j+1, cost+B[j])]
        else:
            m = dfs(i, j+1, cost+B[j])
            dic[(i, j+1, cost+B[j])] = m
        return m

    if j==M and i<N:
        if (i+1, j, cost+A[i]) in dic:
            n = dic[(i+1, j, cost+A[i])]
        else:
            n = dfs(i+1, j, cost+A[i])
            dic[(i+1, j, cost+A[i])] = n
        return n

    else:
        if (i+1, j, cost+A[i]) in dic:
            n = dic[(i+1, j, cost+A[i])]
        else:
            n = dfs(i+1, j, cost+A[i])
            dic[(i+1, j, cost+A[i])] = n

        if (i, j+1, cost+B[j]) in dic:
            m = dic[(i, j+1, cost+B[j])]
        else:
            m = dfs(i, j+1, cost+B[j])
            dic[(i, j+1, cost+B[j])] = m
    return max(n, m)
print(dfs(0, 0, 0))
#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()))

sum_A = [0] * (N+1)
mark_A = 0
for i,a in enumerate(A, 1):
    tmp =  sum_A[i-1] + a
    if tmp <= K:
        sum_A[i] = tmp
        mark_A = i
    else:
        break

sum_B = [0] * (M+1)
mark_B = 0
for j,b in enumerate(B, 1):
    tmp = sum_B[j-1] + b
    if tmp <= K:
        sum_B[j] = tmp
        mark_B = j
    else:
        break
        
import numpy as np

C = np.tile(sum_A[:mark_A+1], (mark_B+1,1)) + np.array(sum_B[:mark_B+1]).reshape(-1, 1)
print(max(np.where(C <= K)[0] + np.where(C <= K)[1]))

Submission status: Runtime Error

Solution code:
def ABC172_C():


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

    if k < a[0] or k < b[0]:
        print(0)
        return

    minamo = []
    def saiki(i,j,sum):
        print(i,j,sum)
        if sum > k:
            minamo.append(i+j)
            return

        if sum == k:
            minamo.append(i+j+1)
            return

        if i+1 <= n:
            saiki(i+1,j,sum+a[i])
        if j+1 <= m:
            saiki(i,j+1,sum+b[j])
        """
        else:
            print(sum)
            return
        """

    saiki(0,0,0)
    print(minamo)
    print(max(minamo)-1)

ABC172_C()

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

c=list()

K=0
if k<min(a):
    print(0)

else:    
    while k>K:
    
        c.append(min(a))
        a.remove(min(a))
        K += min(a)

        if k>K:
           c.append(min(b))
           b.remove(min(b))
           K += min(b)

    print(len(c))

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 )

def dp( i, j, t ):
    if i == n and j == m:
        return 0
    elif i == n:
        return dp( i, j+1, t+b[j] ) + 1
    elif j == m:
        return dp( i+1, j, t+a[i] ) + 1
    elif a[ i ] + t > k and b[ j ] + t > k:
        return 0
    elif a[ i ] + t > k:
        return dp( i, j+1, t+b[j] ) + 1
    elif b[ j ] + t > k:
        return dp( i+1, j, t+a[i] ) + 1
    else:
        return max( dp( i+1, j, t+a[i] ), dp( i, j+1, t+b[j] ) ) + 1

read = dp( 0, 0, 0 )
    
print( read )

Submission status: Runtime Error

Solution code:
import sys

n, m, k = map(int, input().split())
a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))

ans = []

def hoge(z, c, a, b):
    # 終了条件
    if ((len(a) == 0) and (len(b) == 0)) or (z > k):
        ans.append(c)
        return

    # aから
    if len(a) == 0:
        z += b.pop(0)

    # bから
    elif (len(b) == 0) or (a == b):
        z += a.pop(0)

    # a,b　同じ
    elif a[0] == b[0]:
        z_a = z + a.pop(0)
        z_b = z + b.pop(0)
        return hoge(z_a, c, a, b), hoge(z_b, c, a, b)
    else:
        if a[0] > b[0]:
            z += b.pop(0)
        else:
            z += a.pop(0)

    if z <= k:
        c += 1


    return hoge(z, c, a, b)

hoge(0, 0, a_list, b_list)
print(max(ans))

Submission status: Runtime Error

Solution code:
import sys
import copy
from collections import deque
import collections
stdin = sys.stdin

mod = 10**9+7

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n, m, k = na()

a = na()
b = na()

num_books = 0
sum_time = 0
for i in a:
    if sum_time+i > k:
        break
    num_books += 1
    sum_time += i
# print(num_books)


res = num_books
ia = num_books - 1
ib = 0
if num_books==0 and b[ib] > k:
    print(0)
    exit(0)
while ib < len(b):
    if sum_time < k:
        sum_time += b[ib]
        ib += 1
        num_books += 1
    else:
        sum_time -= a[ia]
        ia -= 1
        num_books -= 1
    if sum_time <= k:
        res = max(res, num_books)

print(res)

Submission status: Runtime Error

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

count = sum(a)
p = n-1
ans = 0
for i in range(m+1):
  flag = False
  count += b[i]
  while count > k:
    count -= a[p]
    p -= 1
    if p == -1:
      if count > k:
        flag = True
      break
  if flag:
    break
  sub = p+1+i
  if ans < sub:
    ans = sub
print(ans)

Submission status: Runtime Error

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

i = 0
j = 0
ans = 0

while k >= 0:
    if i <= n-1 and j <= m-1:
        k -= min(a_lsit[i], b_list[j])
        if a_lsit[i] < b_list[j]:
            i += 1
        else:
            j += 1
    elif i > n-1:
        k -= b_list[j]
        j += 1
    elif j > m-1:
        k -= a_lsit[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()))

max_num = 0


def dfs(curr_a, curr_b, choice, time_required, count):
    global max_num
    if choice == 0:
        if curr_a >= N - 1:
            if max_num < count:
                max_num = count
            return
        else:
            curr_a += 1
            time_required += a[curr_a]
    else:
        if curr_b >= M - 1:
            if max_num < count:
                max_num = count
            return
        else:
            curr_b += 1
            time_required += b[curr_b]
    if time_required > K:
        if max_num < count:
            max_num = count
        return
    count += 1
    if max_num < count:
        max_num = count
    dfs(curr_a, curr_b, 0, time_required, count)
    dfs(curr_a, curr_b, 1, time_required, count)

curr_a = -1
curr_b = -1
time_required = 0
count = 0
dfs(curr_a, curr_b, 0, time_required, count)
curr_a = -1
curr_b = -1
time_required = 0
count = 0
dfs(curr_a, curr_b, 1, time_required, count)

print(max_num)

Submission status: Runtime Error

Solution code:
A=[]
B=[]
books = 0
time = 0
n, m, k = map(int,input().split())
i = n-1
j = m - 1
for i in range(0,len(n)):
    A.append(int(input()))
for i in range(0,len(m)):
    B.append(int(input()))
while not(time > k) :
    if (i> -1 and j> -1 and A[i]<B[j]) or (i>-1 and not(j>-1)):
        time = time+A[i]
        i=i-1
        books=books+1
    elif (i> -1 and j> -1 and not(A[i]<B[j])) or (j>-1 and not(i>-1)):
            time = time+A[i]
            i=i-1
            books=books+1
print(books)

Submission status: Runtime Error

Solution code:
import sys
nums=input().split()
#a=int(nums[0])
b=int(nums[1])
n=int(nums[2])
lista=input().split()
listb=input().split()
listall=[]

#acount=0
bcount=0
count=0
time=0
for i in lista:
  anum=int(i)
  for j in range(bcount,b+1):
    bnum=int(listb[j])
    if j==b:
      listall.append(anum)
      break
    if anum<bnum:
      listall.append(anum)
      break
    else:
      listall.append(bnum)
      bcount+=1

if bcount!=b:
  for i in range(bcount,b):
    listall.append(int(listb[i]))

for i in listall:
  if i+time>n:
    print(count)
    sys.exit()
  else:
    time+=i
    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()))
c = 0
t = 0
ac = 0
bc = 0
lenA = len(A)
lenB = len(B)
sa = max(lenA,lenB) - min(lenA,lenB)
if sa != 0:
  if lenA > lenB:
    for i in range(sa):
      B.append(1000000001)
  if lenA < lenB:
    for i in range(sa):
      A.append(1000000001)
print(A,B)
for i in range(N + M):
  if A[ac] > B[bc]:
    t += B[bc]
    bc += 1
  elif A[ac] < B[bc]:
    t += A[ac]
    ac += 1
  elif A[ac] == B[bc]:
    t += A[ac]
    ac += 1
  if t <= K:
    c += 1
  else:
    break
print(c)

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 = sorted(A)
B = sorted(B)
ans = 0
time = 0
a_i = 0
b_i = 0
for _ in range(n+m):
    a,b = A[a_i], B[b_i]
    if a == b:
        if k >= time +a:
            ans +=1
            time += a
            a_i +=1
            if k >= time +b:
                ans +=1
                time +=b
                b_i +=1
            else:break
        else:break
    else:
        c = min(a,b)
        if k >= time +c:
            ans +=1
            time += c
            if c ==a:
                a_i +=1
            else:
                b_i =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()))
time = 0
ans = 0

while ans < n + m:
  if len(a) == 0:
    time += b[0]
    del b[0]
  elif len(a) == 0:
    time += a[0]
    del a[0]
  elif a[0] < b[0]:
    time += a[0]
    del a[0]
  else:
    time += b[0]
    del b[0]
  if time <= k:
    ans += 1
  else:
    break

print(ans)

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

a=0
b=0
ans=0
books=len(A)+len(B)
time=0


while(1):
    if a+b==books or time>=K:
        break
    
    elif (a!=N and b!=M):
        if A[a]>B[b]:
            if time+B[b]<=K:
                time+=B[b]
                b+=1
                ans+=1
            else:
                break
        
        elif B[b]>A[a]:
            if time+A[a]<=K:
                time+=A[a]
                a+=1
                ans+=1
            else:
                break
        
        else:
            if time+A[a]>K:
                break
            else:
                tmpa=a
                tmpb=b
                while(1):
                    tmpa+=1
                    tmpb+=1
                    if A[tmpa]>B[tmpb]:
                        time+=B[b]
                        b+=1
                        ans+=1
                        break
                    
                    elif A[tmpa]<B[tmpb]:
                        time+=A[a]
                        a+=1
                        ans+=1
                        break
            
    elif a==N and b!=M:
        time+=B[b]
        b+=1
        ans+=1
        
    elif b==M and a!=N:
        time+=A[a]
        a+=1
        ans+=1
print(ans)

Submission status: Runtime Error

Solution code:
import sys
import numpy as np
import math
import collections
from collections import deque 
from functools import reduce

# input = sys.stdin.readline

n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ai, bi = 0, 0
a.append(10**10)
b.append(10**10)
cnt = 0
tt = 0
cnt = 0
tt = 0
cnt = 0
tt = 0
while True:
    if a[ai] == b[bi]:
        aii = ai + 1
        bii = bi + 1
        while True:
            if a[aii] == b[bii]:
                aii += 1
                bii += 1
                continue
            if a[aii] < b[bii]:
                flg = 'a'
                break
            else:
                flg = 'b'
                break
    elif a[ai] > b[bi]:
        flg = 'b'
    else:
        flg = 'a'

    if flg == 'b':
        if (tt + b[bi] > k):
            break
        cnt += 1
        tt += b[bi]
        bi += 1
    else:
        if (tt + a[ai] > k):
            break
        cnt += 1
        tt += a[ai]
        ai += 1
                
print(cnt)

Submission status: Runtime Error

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

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

if k < a[0] and k < b[0]:
    print(0)
    sys.exit()

count = [[0 for i in range(n + 1)] for j in range(m + 1)]

answer = 0
for i in range(n+2):
    for j in range(m):
        if j != 0:
            count[i][j] = count[i][j-1] + a[j-1]
        if count[i][j] > k:
            break
        if answer < i + j and count[i][j] <= k:
            answer = i + j
    if i == n + 1:
        break
    count[i+1][0] = count[i][0] + b[i]
    if count[i+1][j] > k:
        break 

print(answer)

Submission status: Runtime Error

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

    def dfs(n, i, j, d, k):
        if k >= K or (i == N and j == M):
            return n
        if d == 1 and i < N and k + A[i] <= K:
            k += A[i]
            l = dfs(n + 1, i + 1, j, d, k)
            r = dfs(n + 1, i + 1, j, -d, k)
            return max(l, r)
        elif d == -1 and j < M and k + B[j] <= K:
            k += B[j]
            l = dfs(n + 1, i, j + 1, d, k)
            r = dfs(n + 1, i, j + 1, -d, k)
            return max(l, r)
        else:
            return n

    print(dfs(0, 0, 0, 1, 0))

resolve()

Submission status: Runtime Error

Solution code:
"""everything has a cost!"""

from math import *
import random
import functools
import collections
import sys

__author__ = 'p7x'



n, m = 0, 0


@functools.lru_cache(maxsize=None, typed=False)
def read(ti: int, tj: int, i: int, j: int) -> int:
    if (ti < n and (tj == m or a[ti] < b[tj])):
        return 1
    elif (tj < m and (ti == n or b[tj] < a[ti])):
        return 2
    elif (ti < n and tj < m):
        return read(ti+1, tj+1, i, j)
    else:
        if (ti == i):
            return 3
        else:
            return 1


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

    mins = 0
    i, j = 0, 0
    while (mins < k):
        v = read(i, j, i, j)
        print(i, j, v)
        if (v == 1):
            mins += a[i]
            i += 1
        elif (v == 2):
            mins += b[j]
            j += 1
        else:
            break
    if mins > k:
        print(i+j-1)
    else:
        print(i+j)