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:
import sys
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
prog=0
a_idx=0
b_idx=0

while a_idx<n:
    if a[a_idx]+prog>k:
        break
    prog+=a[a_idx]
    a_idx+=1

if a_idx==n:
    while b_idx<m:
        if b[b_idx]+prog>k:
            break
        prog+=b[b_idx]
        b_idx+=1

mx=a_idx+b_idx

if mx==n+m:
    print(mx)
    sys.exit()

for i in range(a_idx):
    while b[b_idx]+prog<=k or b_idx<m:
        prog+=b[b_idx]
        b_idx+=1
    if a_idx+b_idx>mx:
        mx=a_idx+b_idx
    if b_idx==m:
        break
    a_idx-=1
    prog-=a[a_idx]

print(mx)

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

i = 0
j = 0
c = []

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

if k < a[0] and k >= b[0]:
    for i in range(len(b)):
        k -= b[i]
        if k < 0:
            print(i)
            exit()
        if i == len(b)-1:
            print(m)
            exit()

if k >= a[0] and k < b[0]:
    for i in range(len(a)):
        k -= a[i]
        if k < 0:
            print(i)
            exit()
        if i == len(a)-1:
            print(n)
            exit()


while True:
    if a[i] <= b[j]:
        c.append(a[i])
        i += 1
        if i == n -1:
            if j == m -1:
                break
            else:
                for l in range(j, m):
                    c.append(b[l])
                break
    else:
        c.append(b[j])
        j += 1
        if j == m -1:
            if i == n -1:
                break
            else:
                for l in range(i, m):
                    c.append(a[l])
                break

for i in range(len(c)):
    k -= c[i]
    if k < 0:
        print(i)
        exit()
    if i == len(c)-1:
        print(n+m)
        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
for i in range(n+m):
    if a == [] or a[0] > b[0]:
        k -= b[0]
        del b[0]
    else:
        k -= a[0]
        del a[0]
    if k < 0:
        break
    else:
        count += 1
print(count)

Submission status: Runtime Error

Solution code:
# Problem C - Tsundoku

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

# initialization
a_start = -1
b_start = -1
max_count = 0
cur_count = 0

# dfs search
def dfs(a_i, b_i):
    global max_count
    global cur_count
    global K
    global N
    global M

    if a_i>=N or b_i>=M:
        if cur_count>max_count:
            max_count = cur_count
        return

    # right check
    if a_i+1<N:
        if K - a_nums[a_i+1]>=0:
            K -= a_nums[a_i + 1]
            cur_count += 1
            dfs(a_i+1, b_i)
            cur_count -= 1
            K += a_nums[a_i + 1]
        else:
            if cur_count>max_count:
                max_count = cur_count
    else:
        if cur_count>max_count:
            max_count = cur_count

    # left check
    if b_i + 1<M:
        if K - b_nums[b_i + 1]>=0:
            K -=b_nums[b_i + 1]
            cur_count += 1
            dfs(a_i, b_i+1)
            cur_count -= 1
            K += b_nums[b_i + 1]
        else:
            if cur_count>max_count:
                max_count = cur_count
    else:
        if cur_count>max_count:
            max_count = cur_count

dfs(a_start, b_start)

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

ai=0
bi=0
time=0

def equalpat(A,B):
  if a[A]>b[B]:return True
  elif a[A]<b[B]:return False
  else:equalpat(A+1,B+1)

while ai < n and bi < m:
  if a[ai]<b[bi]:time+=a[ai];ai+=1
  elif a[ai]>b[bi] :time+=b[bi];bi+=1
  else:
    result = equalpat(ai,bi)
    if result == True:time+=b[bi] ;bi+=1
    else:time+=a[ai] ;ai+=1

  
  
  if time >= k:break
    
if n > m:
  while ai < n:
    time+=a[ai];ai+=1
    if time >= k:break

if n < m:
  while bi < m:
    time+=b[bi];bi+=1
    if time >= k:break
      
print(ai+bi)

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


while True:
    if len(Alist) != 0 and len(Blist) != 0:
        if Alist[0] < Blist[0]:
            readtime = Alist.pop(0)
        elif Alist[0] > Blist[0]:
            readtime = Blist.pop(0)
    elif len(Alist) != 0:
        readtime = Alist.pop(0)
    elif len(Blist) != 0:
        readtime = Blist.pop(0)
    else: 
        break

    if K < readtime:
        break
    else:
        count+=1
        K = K -readtime
print(count)

Submission status: Runtime Error

Solution code:
def recurse(A,B,i,j,suma,count):
    if(suma>K):
        return count
    elif(i==N):
        temp=suma+B[j]
        if(temp>K):
            return count
        else:
            return recurse(A,B,i,j+1,suma+B[j],count+1)
    elif(j==M):
        temp=suma+A[i]
        if(temp>K):
            return count
        else:
            return recurse(A,B,i+1,j,suma+A[i],count+1)
    elif(A[i]<B[j]):
        temp=suma+A[i]
        if(temp>K):
            return count
        else:
            suma+=A[i]
            count+=1
            return recurse(A,B,i+1,j,suma,count)
    elif(A[i]==B[j]):
        temp=suma+A[i]
        if(temp>K):
            return count
        else:
            return max(recurse(A,B,i+1,j,suma+A[i],count+1),recurse(A,B,i,j+1,suma+B[j],count+1))
    else:
        temp=suma+B[i]
        if(temp>K):
            return count
        else:
            return recurse(A,B,i,j+1,suma+B[j],count+1)

N,M,K=list(map(int,input().split()))
A=list(map(int,input().split()))
B=list(map(int,input().split()))
print(recurse(A,B,0,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()))

i = 0
for s in range(n+m):
    if i>k:
        if a[0]>=b[0]:
            i = i + a[0]
            del a[0]
        else:
            i = i + b[0]
            del a[0]
    elif i=k:
        print(s+1)
    else:
        print(s)

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

import numpy as np

a =[]
flag = True
while flag:
    if A[0] >= K or B[0] >= K:
        flag = False
    elif not A and not B:
        flag = False
    elif not A:
        a.append(B[0])
        B = B[1:]
    elif not B:
        a.append(A[0])
        A = A[1:]
    elif A[0] <= B[0]:
        a.append(A[0])
        A = A[1:]
    else:
        a.append(B[0])
        B = B[1:]

res = np.cumsum(a)
r = list(res)

cnt = 0
for i in r:
    if i <= K:
        cnt += 1
    else:
        break
print(cnt)

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()))
tt = 0
a = 0
b = 0
if (K >= (sum(A)+sum(B))):
    print(N+M)
elif (A[a]>K)and(B[b]>K):

    print(0)
else:
    while (tt <= K ):
        print(tt)

        if (a < N)and (b < M):
            if A[a]>B[b]:
                tt += B[b]
                b += 1
                if tt > K:
                    b -= 1
                    break

                elif tt == K:
                    break
            elif A[a]<B[b]:
                tt += A[a]
                a += 1
                if tt > K:
                    a -= 1
                    break

                elif tt == K:
                    break
            elif (A[a]==B[b])and(a < N-1)and (b < M-1):
                if A[a+1]>B[b+1]:
                    tt += B[b]
                    b += 1
                    if tt > K:

                        b -= 1
                        break
                    elif tt == K:
                        break
            elif (A[a]==B[b]and(a == N-1)and (b < M-1):
                tt += B[b]
                b += 1
                if tt > K:

                    b -= 1
                    break
                elif tt == K:
                    break
            else:
                tt += A[a]
                b += 1
                if tt > K:

                    a -= 1
                    break
                elif tt == K:
                    break



        elif (a == N)and (b < M):
            tt += B[b]
            b += 1
            if tt > K:
                b -= 1
                break

            elif tt == K:
                break
        elif (a < N)and (b == M):
            tt += A[a]
            a += 1
            if tt > K:
                a -= 1
                break

            elif tt == K:
                break
        else:
            break
    print(a + b)

Submission status: Runtime Error

Solution code:
from collections import deque

N, M, K = map(int, input().split())
A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))
A_deque = deque(A_list)
B_deque = deque(B_list)
time = 0
book = []

if A_deque[0] > K and B_deque[0] > K:
    pass
else:
    while time < K:
        if len(A_deque) == 0 and len(B_deque) == 0:
            break
        elif len(A_deque) == 0:
            time += B_deque[0]
            book.append(B_deque[0])
            B_deque.popleft()
            # print(B_deque)
        elif len(B_deque) == 0:
            time += A_deque[0]
            book.append(A_deque[0])
            A_deque.popleft()
            # print(A_deque)
        elif (len(A_deque) == 0) and len(B_deque) == 0:
            break
        elif A_deque[0] < B_deque[0]:
            time += A_deque[0]
            book.append(A_deque[0])
            A_deque.popleft()
            # print(A_deque)
        elif A_deque[0] > B_deque[0]:
            time += B_deque[0]
            book.append(B_deque[0])
            B_deque.popleft()
        elif A_deque[0] == B_deque[0]:
            #print(min(len(A_deque), len(B_deque)))
            min_len = min(len(A_deque), len(B_deque))
            for i in range(0, min_len):
                #print(A_deque, B_deque)
                if A_deque[i] == B_deque[i]:
                    continue
                elif not (A_deque[i] == B_deque[i]):
                    if A_deque[i] > B_deque[i]:
                        time += B_deque[0]
                        book.append(B_deque[0])
                        B_deque.popleft()
                        # break
                    else:
                        time += A_deque[0]
                        book.append(A_deque[0])
                        A_deque.popleft()
                        # break
                # elif A_deque[i] == None or B_deque[i] == None:  # どちらかがなくなる
            if len(A_deque) > len(B_deque):
                time += A_deque[0]
                book.append(A_deque[0])
                A_deque.popleft()
                # break
            else:
                time += B_deque[0]
                book.append(B_deque[0])
                B_deque.popleft()
                # break


if time > K:
    book.pop(-1)
else:
    pass
print(len(book))

Submission status: Runtime Error

Solution code:
from collections import deque

n, m, k = map(int, input().split())
aa = tuple(map(int, input().split()))
bb = tuple(map(int, input().split()))

s = set()
maxn = 0
def dfs(a, b, t):
    global maxn
    global s
    
    if (a, b) in s:
        return
    s.add((a, b))
    maxn = max(maxn, a + b)
    if a < n and t + aa[a] <= k:
        dfs(a + 1, b, t + aa[a])
    if b < m and t + bb[b] <= k:
        dfs(a, b + 1, t + bb[b])
dfs(0, 0, 0)
print(maxn)

Submission status: Runtime Error

Solution code:
deta = list(input().split(" "))
ahon = int(deta[0])
bhon = int(deta[1])
time = int(deta[2])

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

book = 0
a = 0
b = 0
while time > 0:
    for i in range(200000):
        if int(alist[a+i]) == int(blist[b+i]):
            continue
        else:
            break
    if int(alist[a+i]) < int(blist[b+i]):
        if int(alist[a]) > time:
            break
        else:
            time -= int(alist[a])
            book += 1
            a += 1
    elif int(alist[a+i]) > int(blist[b+i]):
        if int(blist[b]) > time:
            break
        else:
            time -= int(blist[b])
            book += 1
            b += 1

print(book)

Submission status: Runtime Error

Solution code:
mnk = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
m = mnk[0]
n = mnk[1]
k = mnk[2]

# m, n, k = 3, 4, 240
# a = [60, 90, 120]
# b = [80, 150, 80, 150]

# m, n, k = 3, 4, 730
# a = [60, 90, 120]
# b = [80, 150, 80, 150]

# 組み合わせを考えること
# aだけをk分読むと何冊か
# bだけをk分読むと何冊か
# 上記で、組み合わせの数を限定することができる

# aの限界
max_a = 0
for i, t in enumerate(a):
  if max_a + a[i] <= k:
    max_a += a[i]
  else:
    break

# bの限界
max_b = 0
for j, t in enumerate(b):
  if max_b + b[j] <= k:
    max_b += b[j]
  else:
    break

# print(max_a)
# print(i)

# print(max_b)
# print(j)

if i-1 == 0 and j-1 == 0:
  print(0)
elif i-1 == 0:
  print(j-1)
elif j-1 == 0:
  print(i-1)
else:
  # i-1とj-1が1より大きいと、
  # 限定されたところまで組み合わせの数を求める
  combis = []
  count = []
  sum_a = 0
  sum_i = 0
  for l, t in enumerate(a[:i+1]):
    if sum_a + t > k:
      break
    else:
      sum_a += t
      sum_i += 1

      sum_b = 0
      sum_j = 0
      for m, u in enumerate(b[:j+1]):
        if sum_a + sum_b + u > k:
          break
        else:
          sum_b += u
          sum_j += 1
          combis.append(sum_a+sum_b)
          count.append(sum_i+sum_j)

  # print(combis)
  # print(count)
  print(max(count))

Submission status: Runtime Error

Solution code:
# C - Tsundoku
import sys
sys.setrecursionlimit = 10**18

N, M, K = map(int, input().split())
A = list(int(a) for a in input().split())
B = list(int(b) for b in input().split())
la = len(A)
lb = len(B)
T = 0
r = 0
ans = 0
def dfs(i, j, T, r):
    global ans
    ans = max(ans, r)
    if i < la and T+A[i] <= K:
        dfs(i+1, j, T+A[i], r+1)
    if j < lb and T+B[j] <= K:
        dfs(i, j+1, T+B[j], r+1)

dfs(0, 0, T, 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()))
ans = 0
cnta = 0
cntb = 0
flag = 0
for i in range(M+N):
    if A[cnta] <= B[cntb]:
        K = K - A[cnta]
        cnta += 1

    else:
        K = K - B[cntb]
        cntb += 1
        if cntb == M:
            K = K - B[cntb]
            cnta += 1
            if K >= 0:
                ans += 1
            else:
                break

    if K >= 0:
        ans += 1
    else:
        break

    if cnta == N:
        for _ in range(M+N):
            K = K - B[cntb]
            cntb += 1
            if K >= 0:
                ans += 1
            else:
                flag = 1
                break
            if cntb == M:
                flag = 1
                break
    if flag == 1:
        break

    if cntb == M:
        for _ in range(M+N):
            K = K - A[cnta]
            cnta += 1
            if K >= 0:
                ans += 1
            else:
                flag = 1
                break
            if cnta == N:
                flag = 1
                break

    if flag == 1:
        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()))

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

if(n<m):
    for i in range(m-n):
        a.append(10**10)
if(n>m):
    for i in range(n-m):
        b.append(10**10)

#print(a)
#print(b)
time=0
i=0
j=0
count=0

while(True):
    if(time>k):
        break
    if(i>len(a)-1):
        time+=b[j]
        count+=1
        j+=1
        
    if(j>len(b)-1):
        time+=a[i]
        count+=1
        i+=1
    
    
    if(time>k):
        break
    
    if(a[i]<b[j]):
        time+=a[i]
        count+=1
        i+=1
    elif(a[i]>b[j]):
        time+=b[j]
        count+=1
        j+=1

    
    else:
        ii=i
        jj=j
        while(True):
            if(a[ii]<b[jj]):
                time+=a[i]
                count+=1
                i+=1
                break
            elif(a[ii]>b[jj]):
                time+=b[j]
                count+=1
                j+=1
                break
            else:
                ii+=1
                jj+=1
                if(ii>len(a)-1):
                    time+=b[j]
                    count+=1
                    j+=1
                    break
                if(jj>len(b)-1):
                    time+=a[i]
                    count+=1
                    i+=1
                    break

  

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

a,b = A.popleft(), B.popleft()
ans = 0
ok = False
while k > 0:
    if ok: break
    if a < b and k-a >= 0:
        k -= a
        ans += 1
        if A: a = A.popleft()
        else: a = float("INF")
    elif a > b and k-b >= 0:
        k -= b
        ans += 1
        if B: b = B.popleft()
        else: b = float("INF")
    elif a == b:
        for i in range(min(len(A), len(B))):
            if A[i] == B[i]:
                if k-A[i] >=0: 
                    k-= A[i]
                    ans += 1
                else: 
                    ok = True
                continue
            if A[i] < B[i] and k-A[i] >= 0:
                k -= A[i]
                ans += 1
                A = A[i+1:]
            elif A[i] > B[i] and k-B[i]>=0:
                k -= B[i]
                ans += 1
                B = B[i+1:]
            else: ok = True
    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()))

import numpy as np

a =[]

if K 

flag = True
while flag:
    if A[0] >= K or B[0] >= K:
        flag = False
    elif not A and not B:
        flag = False
    elif not A:
        a.append(B[0])
        B = B[1:]
    elif not B:
        a.append(A[0])
        A = A[1:]
    elif A[0] <= B[0]:
        a.append(A[0])
        A = A[1:]
    #elif A[0] > B[0]:
    else:
        a.append(B[0])
        B = B[1:]

res = np.cumsum(a)
r = list(res)

cnt = 0
for i in r:
    if i <= K:
        cnt += 1
    else:
        break
print(cnt)

Submission status: Runtime Error

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

time = K
ans = 0
an = 0
bn = 0

while time > 0:
    if A[an] <= B[bn] and time >= A[an]:
        time -= A[an]
        an += 1
        ans += 1
        if an == N:
            while time > 0:
                time -= B[bn]
                bn += 1
                ans += 1
                if bn == M:
                    print(ans)
                    exit()
            print(ans)
            exit()
    elif A[an] >= B[bn] and time >= A[an]:
        time -= B[bn]
        bn += 1
        ans += 1
        if an == M:
            while time > 0:
                time -= A[an]
                an += 1
                ans += 1
                if an == N:
                    print(ans)
                    exit()
            print(ans)
            exit()
    else:
        break
print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque

n, m, k = map(int, input().split())
la = deque(list(map(int, input().split())))
lb = deque(list(map(int, input().split())))

cnt = 0
while k >= 0:
    if la == deque([]):
        if lb == deque([]):
            break
        elif lb[0] <= k:
            k -= lb[0]
            cnt += 1
            lb.popleft()
        else:
            break
    elif lb == deque([]):
        if la[0] <= k:
            k -= la[0]
            cnt += 1
            la.popleft()
        else:
            break
    elif la[0] > lb[0]:
        if lb[0] <= k:
            k -= lb[0]
            cnt += 1
            lb.popleft()
        else:
            break
    elif la[0] < lb[0]:
        if la[0] <= k:
            k -= la[0]
            cnt += 1
            la.popleft()
        else:
            break

    elif la[0] == lb[0]:
        h = 0
        p = 0
        while h < len(la) and h < len(lb) and la[h] == lb[h]:
            h += 1
            if la[h] > lb[h]:
                p = 1
            elif la[h] < lb[h]:
                p = 2
        if p == 1:
            k -= la[0]
            cnt += 1
            la.popleft()
        elif p == 2:
            k -= lb[0]
            cnt += 1
            lb.popleft()
print(cnt)

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit( 10 ** 10 )

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 ):
    next_i = min( i+1, n )
    next_j = min( j+1, m )
    if i == n and j == m:
        return 0
    elif a[ i ] + t > k and b[ j ] + t > k:
        return 0
    elif a[ i ] + t > k:
        return dp( i, next_j, t+b[j] ) + 1
    elif b[ j ] + t > k:
        return dp( next_i, j, t+a[i] ) + 1
    else:
        return max( dp( next_i, j, t+a[i] ), dp( i, next_j, t+b[j] ) ) + 1

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

Submission status: Runtime Error

Solution code:
def check(A,B,a,b):
    if A[a] == B[b]:
        return check(A,B,a+1,b+1)
    elif A[a] > B[b]:
        return True
    else:
        return False

N, M, K = map(int, input().split(" "))
A = list(map(int,input().split()))
B = list(map(int,input().split()))
MAX = 10 ** 9 + 7
A.append(MAX)
B.append(MAX)
a, b, time = 0, 0, 0
for i in range(0,len(A)+len(B)-1):
#     print(A[a],B[b])
    if A[a] == B[b]:
        if A[a] == MAX:
            break
        if check(A,B,a+1,b+1):
            time += A[a]
            a += 1
        else:
            time += B[b]
            b += 1
    elif A[a] <= B[b]:
        time += A[a]
        a += 1
    else:
        time += B[b]
        b += 1
    if time > K:
        break
#     print(time)
print(i)

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)
if n>m:
    B = B + [10**9]*(n-m)
elif m>n:
    A = A + [10**9]*(m-n) 
ans = 0
time = 0
a_i = 0
b_i = 0
for _ in range(n+m):
    a,b = A[a_i], B[b_i]
    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:
# /usr/bin/python3
# -*- coding: utf-8 -*-
from queue import Queue
from queue import LifoQueue as Stack
from math import sqrt, floor, ceil, log2, log10, pi
from fractions import gcd
from itertools import permutations, combinations
from operator import itemgetter
from functools import cmp_to_key


INF=1001001001
__MOD__=(10**9)+7
yn = 'YNeos'
judge = False
cnt = 0
ans = None


def lcm(a, b):
    return (a*b) // gcd(a,b)

def intinput():
    return int(input())

def mulinputs():
    return map(int,input().split())

def lineinputs(func=intinput):
    datas = []
    while True:
        try:
            datas.append(func())
        except EOFError:
            break
    return datas

class ModInt():
    def __init__(self, x):
        self.__x = (x % __MOD__)
    def __add__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x + other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x + other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __radd__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((other.__x + self.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((other.__x + self.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")

    def __sub__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x - other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x - other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __rsub__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((other.__x - self.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((other.__x - self.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")

    def __mul__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x * other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x * other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __rmul__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((other.__x * self.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((other.__x * self.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")

    def __truediv__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x * other.__modinv())%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x * other.__modinv())%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __rtruediv__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((other.__x * self.__modinv())%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((other.__x * self.__modinv())%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")

    def __pow__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__(pow(self.__x, other.__x, __MOD__)))
        elif type(other) == ModInt:
            return self.__class__(pow(self.__x, other.__x, __MOD__))
        else:
            raise Exception("Not Int or Not ModInt")
    def __rpow__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__(pow(other.__x, self.__x, __MOD__)))
        elif type(other) == ModInt:
            return self.__class__(pow(other.__x, self.__x, __MOD__))
        else:
            raise Exception("Not Int or Not ModInt")

    def __modinv(self, m=__MOD__):
        a = self.__x
        if a == 0:
            raise ZeroDivisionError()
        if gcd(a, m) != 1:
            raise Exception("%sの逆数は求まりません。" % a)
        b, u, v = m, 1, 0
        while b != 0:
            t = a//b;
            a -= t*b
            a, b = b, a
            u -= t * v
            u, v = v, u
        u %= m
        if u < 0:
            u += m
        return u
    def __int__(self):
        return self.__x
    def __str__(self):
        return str(self.__x)



if __name__ == '__main__':
    N, M, K = mulinputs()
    an = list(map(int, input().split()))
    bm = list(map(int, input().split()))

    # i, j, la, lb, t = 0, 0, len(an), len(bm), 0
    # l = max(la, lb)
    # while True:
    #     if i < la and j < lb:
    #         a, b = an[i], bm[j]
    #     elif i < la:
    #         a, b = an[i], INF
    #     elif j < lb:
    #         a, b = INF, bm[j]
    #     else:
    #         break
    #     c = 0
    #     if a <= b:
    #         c = a
    #         i += 1
    #     else:
    #         c = b
    #         j += 1
    #     if t + c <= K:
    #         t += c
    #         cnt += 1
    #     else:
    #         break
    # # t = 0
    # # while len(an) != 0 or len(bm) != 0:
    # #     a, b = INF, INF
    # #     if len(an) > 0:
    # #         a = an.pop()
    # #     if len(bm) > 0:
    # #         b = bm.pop()
    # #     if t + min(a, b) > K:
    # #         break
    # #     if a >= b:
    # #         t += a
    # #         cnt += 1
    # #         bm.append(b)
    # #     else:
    # #         t += b
    # #         cnt += 1
    # #         an.append(a)
# 
    # # 出力
    # print(cnt)


    la, lb = len(an), len(bm)
    def dfs(i, j, t, c):
        a = INF if i >= la else an[i]
        b = INF if j >= lb else bm[j]
        if t + min(a,b) > K:
            return c
        return max(dfs(i+1, j, t+a, c+1), dfs(i, j+1, t+b, c+1), c)

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

Submission status: Runtime Error

Solution code:
N,M,K=map(int,input().split())
A=map(int,input().split())
A=list(A)
B=map(int,input().split())
B=list(B)
time=int(0)
book=int(0)
ia=0
jb=0
while time<=K and ia<N and jb<M:
    if A[ia]<=B[jb]:
        time += A[ia]
        book += 1
        ia+=1
        if ia==N:
            break
    else:
        time += B[jb]
        book +=1
        jb -=1
        if jb==M:
            break
if time<=K and ia==N:
    while time<=K and jb<M:
        time+=B[jb]
        book+=1
        jb+=1
        if jb==M:
            break
elif time<=K and jb==M:
    while time<K and ia<N:
        time+=A[ia]
        book+=1
        ia+=1
        if ia==N:
            break
if time<=K:
    print(book)
else:
    print(book-1)