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
#sys.setrecursionlimit(450000)
input = sys.stdin.readline

from functools import lru_cache

@lru_cache(maxsize=None)
def Tsundoku(i,j):
    global k
    if dp[i][j]!=None:
        return dp[i][j]
    ans1,ans2 = 0,0
    if i<n and k-a[i]>=0:
        k-=a[i]
        ans1 = 1+Tsundoku(i+1,j)
        k+=a[i]
    if j<m and k-b[j]>=0:
        k-=b[j]
        ans2 = 1+Tsundoku(i,j+1)
        k+=b[j]
    dp[i][j] = max(ans1,ans2)
    return dp[i][j]

n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
dp = [[None]*(m+1) for _ in range(n+1)]
ans = Tsundoku(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()))
i=j=0
limit=k

while i<n and limit>=a[i]:
   
    limit-=a[i]
    i+=1
    
limit2=k
while j<m and limit2>=b[j]:
  
   
    limit2-=b[j]
    j+=1

if(i>j)or(i==j and limit>=limit2):
    j=0
    while j<m and limit>=b[j]:
        j+=1
        limit-=b[j]
else:
    i=0
    while i<n and limit2>=a[i]:
        i+=1
        limit2-=a[i]
print(i+j)

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:
        return
    count += 1
    if max_num < count:
        max_num = count
    print(max_num)
    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:
"""everything has a cost!"""

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

__author__ = 'p7x'


# sys.stdin = open('in.txt', 'r')
# sys.stdout = open('out.txt', 'w')
# sys.stderr = open('err.txt', 'w')

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)

Submission status: Runtime Error

Solution code:
def whichDesk(a, b, i, j):
    if i < len(a) and j >= len(b):
        return 1
    elif j < len(b) and i >= len(a):
        return 2
    elif a[i] < b[j]:
        return 1
    elif a[i] > b[j]:
        return 2
    else:
        return whichDesk(a, b, i+1, j+1)

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

t = 0
cnt = 0

i = 0
j = 0

wd = 0

while t <= k and (i < len(a) or j < len(b)):
    wd = whichDesk(a, b, i, j)
    if wd == 2:
        t += b[j]
        j += 1
    else:
        t += a[i]
        i += 1

    if t <= k:
        cnt += 1
        
print(cnt)

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

num = 0
for i in range(n + m):
    x = a_list[0]
    y = b_list[0]
    if x < y:
        k -= x
        a_list.pop(0)
        num += 1
    else:
        k -= y
        b_list.pop(0)
        num += 1
    if k < 0:
        break

print(num - 1)

Submission status: Runtime Error

Solution code:
import numpy as np
N, M, K = map(int, input().split())

A = [0] + list(map(int, input().split()))
B = [0] + list(map(int, input().split()))

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

a = 0
b = 0

flg = False

if cum_A[N] + cum_B[M] <= K:
    ans = N + M
else:
    while True:
        if cum_A[a] <= cum_B[b]:
            if (a < N) & (cum_A[a + 1] + cum_B[b] <= K):
                a += 1
            else:
                flg = True
        elif cum_A[a] > cum_B[b]:
            if (b < M) & (cum_A[a] + cum_B[b + 1] <= K):
                b += 1
            else:
                flg = True
        
        if flg:
            ans = a + 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()))
sum = 0
j = 0
l = 0
count = 0
¥
for i in range(10 ** 9 * 10 ** 9):
    if A == [] and B == []:
        break

    if A == [] or B == []:
        if A == []:
            if B[0] + sum <= K:
                sum += B[0]
                B.pop(0)
                count += 1
                # j += 1
            else:
                break
        elif B == []:
            if A[0] + sum <= K:
                sum += A[0]
                A.pop(0)
                count += 1
                # j += 1
            else:
                break
    else:
        if A[0] > B[0]:
            if B[0] + sum <= K:
                sum += B[0]
                B.pop(0)
                count += 1
                # j += 1
            else:
                break
        else:
            if A[0] + sum <= K:
                sum += A[0]
                A.pop(0)
                count += 1
                # l += 1
            else:
                break
        

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

A = sorted(A)
B = sorted(B)
if n>m:
    
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:
import numpy as np
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A.append(0)
A = np.cumsum(A)
B.append(0)
B = np.cumsum(B)
ans = 0
for i in range(N+1):
  if A[i] > K:
    break
  for j in range(M+1):
    if A[i]+B[i] > K:
      break
    ans = max(ans, i+j)
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.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:
import collections import deque

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

sum_minute = 0
ans = 0
while (sum_minute <= k) and (a or b):
    if a and b:
        if sum_minute + min(a[0], b[0]) <= k:
            if a[0] <= b[0]:
                r = a.popleft()
                sum_minute += r
                ans += 1
            else:
                r = b.popleft()
                sum_minute += r
                ans += 1
        else:
            break
    elif not a:
        if sum_minute + b[0] <= k:
            r = b.popleft()
            sum_minute += r
            ans += 1
        else:
            break
    elif not b:
        if sum_minute + a[0] <= k:
            r = a.popleft()
            sum_minute += r
            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_index = 0
b_index = 0
total_read_time = 0
answer = 0
for i in range(N + M):
    if a_index == len(A) and b_index == len(B):
        break
    elif a_index == len(A):
        if total_read_time + B[a_index] > K:
            break
        else:
            answer += 1
            total_read_time += B[b_index]
            b_index += 1
    elif b_index == len(B):
        if total_read_time + A[a_index] > K:
            break
        else:
            answer += 1
            total_read_time += A[a_index]
            a_index += 1
    else:
        if A[a_index] >= B[b_index]:
            if total_read_time + B[b_index] > K:
                break
            else:
                answer += 1
                total_read_time += B[b_index]
                b_index += 1
        else:
            if total_read_time + A[a_index] > K:
                break
            else:
                answer += 1
                total_read_time += A[a_index]
                a_index += 1
print(answer)

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit(1000000)

def whichDesk(a, b, i, j):
    if i < len(a) and j >= len(b):
        return 1
    elif j < len(b) and i >= len(a):
        return 2
    elif a[i] < b[j]:
        return 1
    elif a[i] > b[j]:
        return 2
    else:
        return whichDesk(a, b, i+1, j+1)

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

t = 0
cnt = 0

i = 0
j = 0

wd = 0

while t <= k and (i < len(a) or j < len(b)):
    wd = whichDesk(a, b, i, j)
    if wd == 2:
        t += b[j]
        j += 1
    else:
        t += a[i]
        i += 1

    if t <= k:
        cnt += 1
        
print(cnt)

Submission status: Runtime Error

Solution code:
import itertools
N, M, K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
Ac = list(itertools.accumulate(A))
Bc = list(itertools.accumulate(B))
amax, bmax = 0, 0
for i in range(len(A)):
    if Ac[i] <= K:
        amax = i + 1
    else: break
if amax == 0:
    At = 0
else:
    At = Ac[amax - 1]
for i in range(len(B)):
    if Bc[i] + At <= K:
        bmax = i + 1
    else: break
ans = amax + bmax
if amax > 0:
    for i in range(1, amax):
        anow = amax - i
        At = Ac[anow - 1]
        for j in range(bmax-1, len(B)):
            if Bc[j] + At <= K:
                bmax = j + 1
            else: break
        ans = max(ans,anow+bmax)
for i in range(len(B)):
    if Bc[i] <= K:
        bmaxr = i + 1
    else: break
ans = max(ans, bmaxr)
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()))

j = 0
ans = 0

for i in range(n):
  if k <= a[i] and k <= b[j]:
    break
  while a[i] > b[j]:
    k = k - b[j]
    j += 1
    ans += 1
    print(k,ans)
  if a[i] < b[j]:
    k = k - a[i]
    ans += 1
    print(k,ans)
    while i == n-1:
      if k >= b[j]:
        k = k - b[j]
        j += 1
        ans += 1
        print(k,ans)
        if j >= m:
          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_cnt = 0
b_cnt = 0
time_cnt = 0

for _ in range(n+m):
    if a[a_cnt] < b[b_cnt]:
        if k - time_cnt < a[a_cnt]:
            break
        time_cnt += a[a_cnt]
        a_cnt += 1
        if a_cnt == n:
            a.append(float('inf')) 
    else:
        if k - time_cnt < b[b_cnt]:
            break
        time_cnt += b[b_cnt]
        b_cnt += 1
        if b_cnt == n:
            b.append(float('inf')) 
print(a_cnt+b_cnt)

Submission status: Runtime Error

Solution code:

#N冊、M冊、時間K
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))

#Aの机の現在の1番上の本の時間とBの机の一番上の本の時間を比較して小さい本を選択して
#本、時間をそれぞれ累積していき、本を削除していく。
#これを繰り返して時間Kより大きい場合になったときに処理を終了させる
#この時、本はカウントしない


aco = 0
bco = 0
book = 0
time = 0

while k > time:
	if a[aco] <= b[bco]:
		book += 1
		time += a[aco]
		aco += 1
	else:
		book += 1
		time += b[bco]
		bco += 1

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

A_ruiseki = []
if A[0]<=k:
    A_ruiseki.append(A[0])
    for i in range(1, n):
        if A_ruiseki[i-1]+A[i] <= k :
            A_ruiseki.append(A_ruiseki[i-1]+A[i])
        else:
            break

B_ruiseki = []
if B[0]<=k:

    B_ruiseki.append(B[0])
    for i in range(1, n):
        if B_ruiseki[i-1]+B[i] <= k :
            B_ruiseki.append(B_ruiseki[i-1]+A[i])
        else:
            break

ans = len(A_ruiseki)

for i in range(len(A_ruiseki)):
    time = A_ruiseki[i]
    expected = max(ans-i-2, 0)

    if time + B_ruiseki[expected] > k:
        continue
    if expected > 0:
        time += B_ruiseki[expected-1]
    for j in range(expected, len(B)):

        if time + B[j] > k:
            break

        time += B[j]

        ans =i+2+j

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 = -1
while time <= K:
    count += 1
    if not A and not B:
        break
    elif not A:
        time += B.pop(0)
    elif not B:
        time += A.pop(0)
    else:
        if A[0] == B[0]:
            if A[1] <= B[1]:
                time += A.pop(0)
            else:
                time += B.pop(0)
        elif A[0] < B[0]:
            time += A.pop(0)
        else:
            time += B.pop(0)
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()))

if k<a[0] and k<b[0]:
  print(0)
else:
  ans = 0
  t = 0
  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[1]:
          t += a[0]
          a.pop()
        elif b[1] < a[1]:
          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()))
copyA = A[:]
copyB = B[:]
max = 0
counter = 0
i = 0
while i < N:
  if max + A[i] > K:
    break
  else:
    max += A[i]
    counter += 1
  i += 1
numA = counter
numB = 0

for i in range(M):
  if max + B[i] <= K:
    max = max + B[i]
    counter += 1
    numB += 1
  else:
    break
while numB < M and numA >=0:
  if max - A[numA] + B[numB+1] <= K:
    max = max -A[numA] + B[numB+1]
    numA -= 1
    numB += 1
    flag=1
    while flag and numB < M-1:
      if max + B[numB+1] <= K:
        counter += 1
        numB +=1
      else:
        flag = 0
  else:
    max -= A[numA]
    numA -= 1

print(counter)

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 calc(k,i,j):
    c1 = 0
    c2 = 0
    global N,M,K,A,B
    if N-1>=i and A[i]+k <= K:
        c1 = 1+calc(A[i]+k,i+1,j)
    if M-1>=j and B[i]+k <= K:
        c2 = 1+calc(B[j]+k,i,j+1)
    return max(c1,c2)
count = calc(0,0,0)
print(count)

Submission status: Runtime Error

Solution code:
N, M, K = [int(x) for x in input().split(' ')]
A = [int(x) for x in input().split(' ')]
B = [int(x) for x in input().split(' ')]

def search(i, j, count, time):
  a = count 
  b = count 
  if i < N and K >= time + A[i]:
    a = search(i + 1, j, count + 1, time + A[i])
  if j < M and K >= time + B[j]:
    b = search(i, j + 1, count + 1, time + B[j])
  return max(a, b)

print(search(0, 0, 0, 0))

Submission status: Runtime Error

Solution code:
a = input().split()
x = input().split()
y = input().split()
z = x + y
zz = []
zz = list(map(lambda i: int(i), z))
t = 0
m = 0
for i in range(len(zz)):
  if t < int(a[2]):
  t += sorted(zz)[i]
  m += 1
  else:
    break
print(m)

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

d=[0]*(k+1)

xx=[0]*(k+1)
yy=[0]*(k+1)

x=0
y=0

cnt=0

for i in range(k+1):

  if yy[i]<m:
    if (i+b[yy[i]])<=k:
      if d[i+b[yy[i]]]==0 or d[i+b[yy[i]]]<(d[i]+1):
        d[i+b[yy[i]]]=d[i]+1
        yy[i+b[yy[i]]]=yy[i]+1
        xx[i+b[yy[i]]]=xx[i]
        if cnt==0 or cnt<d[i+b[yy[i]]]:
          cnt=d[i+b[yy[i]]]

  if xx[i]<n:
    if (i+a[xx[i]])<=k:
      if d[i+a[xx[i]]]==0 or d[i+a[xx[i]]]<(d[i]+1):
        d[i+a[xx[i]]]=d[i]+1
        xx[i+a[xx[i]]]=xx[i]+1
        yy[i+a[xx[i]]]=yy[i]
        if cnt==0 or cnt<d[i+a[xx[i]]]:
          cnt=d[i+a[xx[i]]]

print(cnt)

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

A = deque(A)
B = deque(B)

ans = 0
left_time = K
while True:
    if len(A) == 0 and len(B) == 0:
        break

    if len(A) != 0 and len(B) == 0:
        val = A.popleft()
    elif len(A) == 0 and len(B) != 0:
        val = B.popleft()
    elif A[0] < B[0]:
        val = A.popleft()
    elif A[0] > B[0]:
        val = B.popleft()
    else:

        def f(n):
            if len(A) == n and len(B) == n:
                val = A.popleft()
                return val
            elif len(A) != n and len(B) == n:
                val = B.popleft()
                return val
            elif len(A) == n and len(B) != n:
                val = A.popleft()
                return val

            elif A[n] < B[n]:
                val = A.popleft()
                return val
            elif A[n] > B[n]:
                val = B.popleft()
                return val
            else:
                val = f(n + 1)
                return val

        val = f(1)

    left_time -= val
    if left_time < 0:
        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()))

def main(A, B, i, j, K):
    if K < 0:
        return -1
    elif i == len(A) and j == len(B):
        return 0
    elif i == len(A):
        return main(A, B, i, j+1, K - B[j]) + 1
    elif j == len(B):
        return main(A, B, i+1, j, K - A[i]) + 1
    with_a = main(A, B, i+1, j, K - A[i])
    with_b = main(A, B, i, j+1, K - B[j])
    return max(with_a, with_b) + 1
print(main(A, B, 0, 0, K))

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 = A+B
a = 0
b = 0
min = 0
cnt= 0

if sum(C) <= K:
  print(len(C))
else:
  while min < K:
    if  len(A) >a and A[a] <= B[b]:
      min += A[a]
      a +=1
      cnt +=1
    
    elif len(B) >b and A[a] > B[b]:
      min +=B[b]
      b +=1
      cnt+=1
    elif len(A) <= a:
      min +=B[b]
      b +=1
      cnt+=1
    elif len(B) <= b:
      min +=A[a]
      a += 1
      cnt+=1
  print(cnt-1)

Submission status: Runtime Error

Solution code:
def main():
    n, m, k = [int(e) for e in input().split()]
    A = [int(e) for e in input().split()]
    B = [int(e) for e in input().split()]

    t = 0
    ca, cb = 0, 0
    for a in A:
        t += a
        if t > k:
            t -= a
            break

        ca += 1

    while True:
        if t > k:
            cb -= 1
            t -= B[cb]
            break

        if cb == len(B):
            break

        t += B[cb]
        cb += 1

    AA = [ca + cb]
    while True:
        if ca == 0 or cb == len(B):
            break

        ca -= 1
        t -= A[ca]
        while True:
            t += B[cb]
            if t > k or cb == len(B):
                break

            cb += 1

        AA.append(ca + cb)

    print(max(AA))


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

books = sorted(A+B)

cnt=0

while K >= 0:
  K-=books[cnt]
  cnt+=1

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

def Tundoku(a,b,count,k,K):
    if k > K :
        count -= 1
        return count
    else:
        if a == N-1 and b == M-1:
            k += min(A[a],B[b])
            if k > K:
                return count
            else:
                k += max(A[a],B[b])
                if k > K:
                    count += 1
                    return count
                else:
                    count = count + 2
                    return count
        if a == N-1 and b != M-1:
            k += B[b]
            count += 1
            return Tundoku(a,b+1,count,k,K)
        elif a != N-1 and b == M-1:
            k += A[a]
            count += 1
            return Tundoku(a+1,b,count,k,K)
        else:
            if A[a] == B[b]:
                k += A[a]
                count += 1
                return min(Tundoku(a+1,b,count,k,K),Tundoku(a,b+1,count,k,K))

            elif A[a] > B[b] :
                k += B[b]
                count += 1
                return Tundoku(a,b+1,count,k,K)
            else:
                k += A[a]
                count += 1
                return Tundoku(a+1,b,count,k,K)

ans = Tundoku(0,0,0,0,K)
print(ans)

Submission status: Runtime Error

Solution code:
from sys import exit
from collections import deque
# import copy
# import numpy as np
# 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())))

cnt = 0

while k >= a[0] or k >= b[0]:
    if a[0] < b[0]:
        k -= a.popleft()
        cnt += 1

    elif a[0] > b[0]:
        k -= b.popleft()
        cnt += 1

    else:
        for i in range(1, min(len(a), len(b))+1):
            if a[i] < b[i]:
                for t in range(0, i+1):
                    k -= a.popleft()
                    cnt += 1
                    if k < a[0]:
                        break
                break
            elif a[i] > b[i]:
                for t in range(0, i+1):
                    k -= b.popleft()
                    cnt += 1
                    if k < b[0]:
                        break
                break
        else:
            if len(a) > len(b):
                for i in range(len(a)):
                    k -= a.popleft()
                    if k < a[0]:
                        break
            else:
                for i in range(len(b)):
                    k -= b.popleft()
                    cnt += 1
                    if k < b[0]:
                        break
    if a == deque([]):
        while k > b[0]:
            k -= b.popleft()
            cnt += 1
        break
    if b == deque([]):
        while k > a[0]:
            k -= a.popleft()
            cnt += 1
        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()))


A.append(K)
B.append(K)

sumA = []
sumB = []

sumAi = 0
for i in range(N+1):
    sumAi += A[i]
    if sumAi > K:
        break
    else:
        sumA.append(sumAi)
AA = A[:i]

sumBj = 0
for j in range(M+1):
    sumBj += B[j]
    if sumBj > K:
        break
    else:
        sumB.append(sumBj)
BB = B[:j]

NN = len(AA)
MM = len(BB)

#Ares = [K-x for x in sumA]
#Bres = [K-x for x in sumB]

#invsumA=[]
#invsum=0
#for x in AA[::-1]:
#    invsum += x
#    invsumA.append(invsum)

#ans = NN

if NN == 0:
    ans = MM
else:
    res = K-sumA[-1]
    resA = AA
    kakuB = []
    resN = len(resA)
    
    for j in range(MM):
        if BB[j] <= res:
            kakuB.append(BB[j])
            res-=BB[j]
        else:
            break
    kakuM = len(kakuB)
    
    process=True
    while process:
        invsum = 0
        for i in range(max(resN,kakuM)-1):
            invsum += resA[-i-1]
            if sumB[i+1] - invsum <= res:
                resA = resA[:-i-1]
                kakuB = BB[:i+2]
                resN = len(resA)
                kakuM = len(kakuB)
                break
            if i == max(resN,kakuM)-2:
                process= False
    
    ans = resN+kakuM
print(ans)

Submission status: Runtime Error

Solution code:
def ABC172_C():
    import resource;
    import random
    # 再帰回数上弦を100000回に設定
    sys.setrecursionlimit(100000)
    # スタックサイズを無制限に設定
    resource.setrlimit(resource.RLIMIT_STACK, (-1, -1))
    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())

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]:
                        if time+B[b]<=K:
                            time+=B[b]
                            b+=1
                            ans+=1
                        else:
                            break
                    
                    elif A[tmpa]<B[tmpb]:
                        if time+A[a]<=K:
                            time+=A[a]
                            a+=1
                            ans+=1
                        else:
                            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

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)
        if z_a <= k:
            c_a = c + 1
        z_b = z + b.pop(0)
        if z_b <= k:
            c_b = c + 1
        return hoge(z_a, c_a, a, b), hoge(z_b, c_b, 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:
from collections import deque


N, M, K = map(int, input().split())
A = deque(map(int, input().split()))
B = deque(map(int, input().split()))
now = 0
ans = 0

while A:
    i = 0
    while A[0] > B[i]:
        i += 1
    B.insert(i, A.popleft())

for time in B:
    now += time
    if now > K:
        break
    ans += 1

print(ans)