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 numpy

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

A_time = []
B_time = []

if sum(A) + sum(B) <= K:
    print(len(A) + len(B))
elif K < min(A) and K < min(B):
    print(0)
else:
    A_res = numpy.cumsum(A)
    B_res = numpy.cumsum(B)

    books = []

    for i in range(len(A_res)):
        if A[i] > K:
            break
        res = K - A[i]
        tmp = 0
        for j in range(1,len(B_res)):
            if B[j] > res:
                books.append(i + tmp + 1)
                break
            tmp = j
    print(max(books))

Submission status: Runtime Error

Solution code:
na,nb,k = map(int, input().split())
ta = list(map(int, input().split()))
tb = list(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 = [ i+1 + get_bcnt( k - sum( ta[:i] ) ) ) for i in range(na) if sum( ta[:i] ) <= k ]
readbook.append( get_bcnt(k) )

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()))
if sum(A) + sum(B) <= K:
    print(len(A)+len(B))
elif min(A) > K or min(B) > K:
    print(0)
else:
    d = 0
    while sum(A) + sum(B) > K:
        A = deque(A)
        B = deque(B)
        if A[-1] <= B[-1]:
            B.pop()
            d += 1
        else:
            A.pop()
            d += 1
    print(N + M - d +1)

Submission status: Runtime Error

Solution code:
def ABC172_C():
    import sys
    sys.setrecursionlimit(4100000)

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

    minamo = []
    def saiki(i,j,sum,mx):

        if a[i] > k:
            return mx

        if b[i] > k:
            return mx
        
        if sum >= k:
            mx = min(i+j,mx)
            return mx

        if i+j >= mx:
            return mx

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

        return mx

    mls = saiki(0,0,0,pow(10,5))
    print(mls)

ABC172_C()

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
            elif A[i] < B[i] and k-A[i] >= 0:
                k -= A[i]
                ans += 1
                A = deque(list(A)[i+1:])
            elif A[i] > B[i] and k-B[i]>=0:
                k -= B[i]
                ans += 1
                B = deque(list(B)[i+1:])
            else: ok = True
    else: break
print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque


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

A = deque(int(a) for a in input().split())
B = deque(int(b) for b in input().split())

ans = 0
while (len(A) or len(B)) and k:
    a = A[0] if len(A) else k + 1
    b = B[0] if len(B) else K + 1
    if a >= b:
        if k - b >= 0:
            k -= B.popleft()
            ans += 1
            continue
        else:
            break
    else:
        if k - a >= 0:
            k -= A.popleft()
            ans += 1
            continue
        else:
            break

print(ans)

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 = 1 #Aの掘り具合
j = 1 #Bの掘り具合
ans = 0 #読んだ本の冊数
while k > 0:
    if (A[i] > B[j] or i == len(A)) and B[j] <= k:
        k = k - B[j]
        ans = ans + 1
        j = j + 1
    elif (A[i] < B[j] or j == len(B)) and A[i] <= k:
        k = k - A[i]
        ans = ans + 1
        i = i + 1
    if i  == len(A) and j  == 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()))
import numpy as np

read = 0

f = np.zeros((n+1, m+1))
f[0][0] = k
for i in range(0, n):
    f[i+1][0] = f[i][0]-a[i]
    if f[i+1][0]<0:
        break
    read = read if read > i+1 else i+1
    for j in range(0, m):
        f[i+1][j+1] = f[i+1][j]-b[j]
        if f[i+1][j+1]<0:
            break
        read = read if read > i+j+2 else i+j+2

print(read)

Submission status: Runtime Error

Solution code:
import numpy as np

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

a_sum = a.cumsum()
b_sum = b.cumsum()
b_li = np.array([0] * k)
for i,book in enumerate(b_sum):
  b_li[book-1:] = i+1

#print(b_li)

ans = 0
for i,book in enumerate(a_sum):
  if book <= k:
    if i+1+b_li[k-book] > ans:
      ans = i+1+b_li[k-book]

print(ans)

Submission status: Runtime Error

Solution code:
    s = [input() for i in range(3)]
    list1 = list(map(int, s[0].split()))
    n = list1[0]
    m = list1[1]
    k = list1[2]
     
    list2 = list(map(int, s[1].split()))
    list3 = list(map(int, s[2].split()))
     
    counter_l1 = 0
    counter_l2 = 0
     
    for i in range(n+m):
        if counter_l1 < n and counter_l2 < m:
            if list2[counter_l1] < list3[counter_l2]:
                k = k - list2[counter_l1]
                if k < 0:
                    break
                if k == 0:
                    counter_l1 +=1
                    break
                counter_l1 +=1
                continue
            if list2[counter_l1] >= list3[counter_l2]:
                k = k - list3[counter_l2]
                if k < 0:
                    break
                if k == 0:
                    counter_l1 +=1
                    break
                counter_l2 +=1
                continue
        else:
            if counter_l1 >= n:
                k = k - list3[counter_l2]
                if k < 0:
                    break
                if k == 0:
                    counter_l1 +=1
                    break
                counter_l2 +=1
            if counter_l2 >= m:
                k = k - list2[counter_l1]
                if k < 0:
                    break
                if k == 0:
                    counter_l1 +=1
                    break
                counter_l1 +=1
    print(counter_l1+counter_l2)

Submission status: Runtime Error

Solution code:
import collections
import sys
sys.setrecursionlimit(200001)
def C():
    N, M, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    memo = {}
    def dfs(i, j, k):
        if (i, j, k) in memo:
            return memo[(i, j, k)]
        if i >= N and j >= M:
            return 0
        if i >= N or k < A[i]:
            ret = 0
            while j < M and k >= B[j]:
                ret += 1
                k -= B[j]
                j += 1
            memo[(i, j, k)] = ret
            return ret
        if j >= M or k < B[j]:
            ret = 0
            while i < N and k >= A[i]:
                ret += 1
                k -= A[i]
                i += 1
            memo[(i, j, k)] = ret
            return ret
        ret = max(1+dfs(i+1, j, k-A[i]), 1+dfs(i, j+1, k-B[j]))
        memo[(i, j, k)] = ret
        return ret
    print(dfs(0, 0, K))
        
C()

Submission status: Runtime Error

Solution code:
n,m,k = map(int, input().split())
A = input().split()
B = input().split()
A = [int(x) for x in A]
B = [int(x) for x in B]
ans = 0
x = 1
y = 1
if A[0] > B[0]:
  sub = B[0]
elif A[0] == B[0]:
  while True:
    if len(A) <= x:
      sub = A[0]
      break
    if len(B)<=x:
      sub = B[0]
      break
    if A[x] > B[x]:
      sub = B[0]
      break
    elif A[x] == B[x]:
      x +=1
      continue
    else:
      sub = A[0]
      break
else:
  sub = A[0]
while k-sub >= 0:
  ans +=1
  if (len(A)==0):
      A = [10**10]
  if (len(B)==0):
      B=[10*10]
  if A[0] > B[0]:
  	sub = B[0]
  	B.remove(sub)
  elif A[0]==B[0]:
    while True:
      if len(A) <= y:
        sub = A[0]
        A.remove(A)
        break
      if len(B)<=y:
        sub = B[0]
        B.remove(sub)
      if A[y] > B[y]:
        sub = B[0]
        B.remove(sub)
        break
      elif A[y] == B[y]:
        y +=1
        continue
      else:
        sub = A[0]
        A.remove(sub)
        break
  else:
  	sub = A[0]
  	A.remove(sub)
  k -= sub
print(ans)

Submission status: Runtime Error

Solution code:
import sys
import functools
from collections import deque

readline = sys.stdin.buffer.readline
n, m, k = map(int, readline().split())
a = list(map(int, readline().split()))
b = list(map(int, readline().split()))

a_len = len(a)
b_len = len(b)


ans = [0, 0, 0]

L = []
d = deque()
d.append((0, 0, 0))
while d:
    a_readed, b_readed, t = d.popleft()
    if (a_len <= a_readed or t + a[a_readed] >= k) and (b_len <= b_readed or t + b[b_readed] >= k):
        L.append(a_readed + b_readed)
    else:
        if a_len > a_readed and t + a[a_readed] <= k:
            d.append((a_readed + 1, b_readed, t + a[a_readed]))
        if b_len > b_readed and t + b[b_readed] <= k:
            d.append((a_readed, b_readed + 1, t + b[a_readed]))

ans = max(L)
print(ans)

Submission status: Runtime Error

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

from collections import deque


def book(A2, B2, t, cnt):
    if len(A2) == 0 and len(B2) == 0:
        L.append(cnt)
        return cnt
    elif t == K:
        L.append(cnt)
        return cnt
    elif t > K:
        L.append(cnt-1)
        return cnt - 1

    a_r = 0
    b_r = 0
    if len(A2) != 0:
        a = A2.popleft()
        a_r = book(A2, B2, t+a, cnt+1)
        
    if len(B2) != 0:
        b = B2.popleft()
        b_r = book(A2, B2, t+b, cnt+1)
    L.append(a_r)
    L.append(b_r)
    return max(a_r, b_r)




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

L = []

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

t = 0
ret = 0

ret = book(A, B, 0, 0)

print(max(L))

Submission status: Runtime Error

Solution code:
nums=input()
nums=nums.split()
M,N,K = int(nums[0]),int(nums[1]),int(nums[2])

al=input()
al2=al.split()
for n in range(M):
    al2[n] = int(al2[n])

bl=input()
bl2=bl.split()
for n in range(N):
    bl2[n] = int(bl2[n])

count=0

ai=0
bi=0

aflag,bflag=0,0

while 1:
    if al2[ai] <= bl2[bi] and al2[ai] <= K and aflag != 1:
        K = K-al2[ai]
        if ai == M:
            aflag=1
        else:
            ai+=1
        count+=1
        

    elif al2[ai] > bl2[bi] and bl2[bi] <= K and bflag != 1:
        K = K-bl2[bi]
        if bi == N:
            bflag=1
        else
            bi+=1
        count+=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()))

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 = list(map(int,input().split()))
A=list(map(int,input().split()))
B=list(map(int,input().split()))
a=0
b=0
count=0
time=0
A1=list()
A1.append(0)
B1=list()
B1.append(0)
mA=0
mB=0
flag=0
if A[0]==1000000000 and B[0]==1000000000:
    flag=-3
    ans=0
if A[0]==1000000000:
    flag=-1
    ans=0
    memo=0
    for i in range(len(B)):
        memo+=B[i]
        if memo<=K:
            ans+=1
        else:
            break
if B[0]==1000000000:
    flag=-2
    ans=0
    memo=0
    for i in range(len(A)):
        memo+=A[i]
        if memo<=K:
            ans+=1
        else:
            break
for i in range(len(A)):
    mA+=A[i]
    if mA<1000000000:
        A1.append(mA)
    else:
        break
for i in range(len(B)):
    mB+=B[i]
    if mB<1000000000:
        B1.append(mB)
    else:
        break   
Ak=len(A1)-1
Bk=len(B1)-1

count=Ak+Bk
t=0
f=0
if flag==0:
    while True:
        for i in range(f+1):
            if A1[Ak-i]+B1[count-(Ak-i)]<=K:
                ans=count
                t=1
                break
            B1[count-(Ak-i)]==0:
                break
        
        if t==1:
            break
        count-=1
        if count==0:
            ans=0
            break
        f+=1

print(ans)

Submission status: Runtime Error

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

while True:
    if ans > k:
        print(result-1)
        break
    if a[0] + b[0] == 2 * (10 ** 9 + 1):
        print(result)
        break
    if len(a) == 0 or len(b) == 0:
        a.append(10 ** 9 + 1)
        b.append(10 ** 9 + 1)
    if a[0] > b[0]:
        ans += b.popleft()
        result += 1
    else:
        ans += a.popleft()
        result += 1



#<D>
#import math
##def search_divisor_nu

Submission status: Runtime Error

Solution code:
import numpy as np

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

a_sum = a.cumsum()
b_sum = b.cumsum()
b_li = np.array([0] * k)
for i,book in enumerate(b_sum):
  b_li[book-1:] = i+1

print(b_li)

ans = 0
for i,book in enumerate(a_sum):
  if book <= k:
    if i+1+b_li[k-book] > ans:
      ans = i+1+b_li[k-book]

print(ans)

Submission status: Runtime Error

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

A = list(map(int, input().split()))
B = list(map(int, input().split()))
maxsum = sum(A)+sum(B)
sumsum=0

if maxsum<K:
    print(S)
elif K < maxsum-K:
    
    for s in range(S):
 
        if len(A)==0 or A[0]>B[0]:
           sumsum+=B[0]
           B.pop(0)
        elif len(B)==0 or A[0]<B[0]:
          sumsum+=A[0]
          A.pop(0)
        if K <= sumsum:
            print(s)
            break
else:
    for s in range(S):
 
        if len(A)==0 or A[len(A)-1]<B[len(B)-1]:
           maxsum-=B[len(B)-1]
           B.pop()
        elif len(B)==0 or A[len(A)-1]>B[len(B)-1]:
          maxsum-=A[len(A)-1]
          A.pop()
        if K >= maxsum:
            print(s)
            break

Submission status: Runtime Error

Solution code:
#C
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
books = 0
timer = 0
judge = True
def read(C):
    books += 1
    timer += C[0]
    try:
        C = C[1:]
    except:
        pass
#全部読む必要があるなら全部読んで終わる
if sum(A) + sum(B) <= K:
    print(len(A)+len(B))
    judge = False
#入力例３対策
elif min(A) > K and min(B) > K:
    print(0)
    judge = False
else:
    while timer <= K and (len(A)>0 or len(B)> 0):
        if len(A) == 0:
            read(B)
        elif len(B) == 0:
            read(A)
        elif A[0] > B[0]:
            read(B)
        elif B[0] > A[0]:
            read(A)
        else:
            read(A)
            if timer > K:break
            read(B)
if judge:
    print(books-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()))
for i in range(1, len(A)):
    A[i] += A[i - 1]
for i in range(1, len(B)):
    B[i] += B[i - 1]
A.insert(0, 0)
B.insert(0, 0)
r = 0
for i, a in enumerate(A):
    min_idx = 0
    max_idx = len(B) - 1
    while min_idx < max_idx:
        j = (min_idx + max_idx + 1) // 2
        k = K - A[i] - B[j]
        if k < 0:
            max_idx = j - 1
        else:
            min_idx = j
    j = min_idx
    while j < len(B) and K - A[i] - B[j + 1] >= 0:
        j += 1
    if K - A[i] - B[j] >= 0:
        r = max(r, i + j)
print(r)

Submission status: Runtime Error

Solution code:
import numpy as np

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

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

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

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

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

print(A1)

C1 = np.zeros((N*M,2))
for i in range(N):
    for j in range(M):
        C1[M*i+j,:] = A1[i,:] + B1[j,:]
C2 = np.array([C[1] for C in C1 if C[0] <= K])

if len(C2):
    print(int(C2.max()))
else:
    print(0)

Submission status: Runtime Error

Solution code:
n,m,k =map(int,input().split())
arra = [int(x) for x in input().split()]
# arra = arra[::-1]

arrb = [int(x) for x in input().split()]
# arrb=arrb[::-1]
topa=0
topb=0
count=0
total = 0
while(total<=k):
    if(arra[0]>k and arrb[0]>k):
        break
    if(topa==n and topb==m):
        break
    if(topa==n):
        total+=arrb[topb]
        if(total>k):
            break
        topb+=1
        count+=1
        continue
    elif(topb==m):
        total+=arra[topa]
        if(total>k):
            break
        topb+=1
        count+=1
        continue
        
    if(arra[topa]<=arrb[topb]):
        total+=arra[topa]
        if(total>k):
            break
        topa+=1
        count+=1
    else:
        total+=arrb[topb]
        if(total>k):
            break
        topb+=1
        count+=1
print(count)

Submission status: Runtime Error

Solution code:
拡げる

Copy
Copy
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()))
A.append(99999)
B.append(99999)
Sum = 0
i = 0
j = 0
while True:
  if A[i] <= B[j] and Sum + A[i] <= K:
    Sum += A[i]
    i += 1
  elif A[i] > B[j] and Sum + B[j] <= K:
    Sum += B[j]
    j += 1
  else:
    break
print(i + j)

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())))
dp = [0] * (len(A)+len(B)+1)
i = j = 0
for idx in range(len(dp)):
    if i >= len(A) and j >= len(B):
        break
    if i >= len(A):
        dp[idx+1] = dp[idx] + B[j]
        j += 1
    elif j >= len(B):
        dp[idx+1]
    elif dp[idx] + A[i] < dp[idx] + B[j]:
        dp[idx+1] = dp[idx] + A[i]
        i += 1
    else:
        dp[idx+1] = dp[idx] + B[j]
        j += 1
    if dp[idx+1] > K:
        break
print(idx)

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

def select_a(k, cnt, a):
    k = k - a[0]
    if a != []:
        a.pop(0)
    if k >= 0:
        cnt = cnt + 1
    return (k, cnt, a)

def select_b(k, cnt, b):
    k = k - b[0]
    if b != []:
        b.pop(0)
    if k >= 0:
        cnt = cnt + 1
    return (k, cnt, b)

def select_even():
    c = a
    d = b
    while c != [] and d != []:
        if c[0] > d[0]:
            select_b()
            break
        elif c[0] < d[0]:
            select_a()
            break
        else:
            c.pop(0)
            d.pop(0)
    
    

while k >= a[0] and k >= b[0]:
    if a[0] > b[0]:
        k, cnt, b = select_b(k, cnt, b)
        if a != []:
            break
    elif a[0] < b[0]:
        k, cnt, a = select_a(k, cnt, a)
        if b != []:
            break
    elif a[0] == b[0]:
        select_even()

while a != [] and k >= a[0]:
    k, cnt, a = select_a(k, cnt, a)

while b != [] and k >= b[0]:
    k, cnt, b = select_b(k, cnt, b)

print (cnt)

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()))
    a.append(10**9+1)
    b.append(10**9+1)
    ans=[]
    def f(i,j,p,ansk):

        if (a[i] > p and b[j]>p):
            ans.append(ansk)

        if a[i]<=p:
            f(i+1,j,p-a[i],ansk+1)

        if b[j]<=p:
            f(i,j+1,p-b[j],ansk+1)
    f(0,0,k,0)
    if len(ans)==0:
        print(0)
    else:
        print(max(ans))
resolve()

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 = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
sum_a={}
tmp=0
for i in range(len(a)):
    tmp+=a[i]
    if tmp>k:
        break
    sum_a.update({i+1:tmp})
if len(sum_a)==0:
    sum_a.update({0:0})
    
sum_b={}
tmp=0
for i in range(len(b)):
    tmp+=b[i]
    if tmp>k:
        break
    sum_b.update({i+1:tmp})
if len(sum_b)==0:
    sum_b.update({0:0})

c_a = max(sum_a.keys())
c_b = max(sum_b.keys())
tmp = sum_a[c_a]+sum_b[c_b]
if tmp<=k:
    print(c_a+c_b)
else:
    for i in range(max(c_a,c_b)+1):
        if sum_a[c_a]>=sum_b[c_b]:
            c_a-=1
        else:
            c_b-=1
        if sum_a[c_a]+sum_a[c_b]<=k:
            print(c_a+c_b)
            break

Submission status: Runtime Error

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


b_book = list(map(int,input().split()))

total = 0
count = 0
A = 0
B = 0
for i in range(N):
  if a_book[i]>=K:
    A = 1
for n in range(M):
  if b_book[n]>=K:
    B = 1

if A==1 or B==1:
  count = 0
elif sum(a_book)+sum(b_book)<=K:
    count = N + M
else:
  s = 0
  while K>=total:
    if a_book[s] <= b_book[s]:
      total += a_book[s]
      count+=1
      a_book.pop(s)
    else:
      total += b_book[s]
      count+=1
      b_book.pop(s)
    
if count==0:
  print(count)
elif count<N+M:
  print(count-1)
else:
  print(count)

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()))
Acount = -1
Bcount = -1
tmpA = []
tmpB = []
tmp = 0
for i in range(N):
    tmp += A[i]
    if tmp > K:
        break
    tmpA.append(tmp)
tmp = 0
for i in range(M):
    tmp += B[i]
    if tmp > K:
        break
    tmpB.append(tmp)

if len(tmpA) > 0 and len(tmpB):
    res = 0
    Bcount = -1
    Acount = len(tmpA) - 1
    while Acount > -1 and Bcount < M:
        for i in range(max(Bcount, 0), M):
            if tmpA[Acount] + tmpB[i] > K:
                break
            Bcount = i
        tmpRes = Acount + Bcount + 2 if Bcount > -1 else Acount + 1
        if res < tmpRes:
            res = tmpRes
        Acount -= 1
    print(res)
elif len(tmpA) > 0:
    print(len(tmpA))
elif len(tmpB) > 0:
    print(len(tmpB))
else:
    print(0)

Submission status: Runtime Error

Solution code:
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
import numpy as np

def binary_search(lis, item):
    low = 0
    high = len(lis) - 1
    inRange = high

    if lis[0] > item:
        return 0

    while low <= high:
        mid = (low + high) //2
        guess1 = lis[mid]
        if mid < inRange:
            guess2 = lis[mid+1]
        else:
            if lis[mid] <= item:
                return len(lis)
            else:
                return len(lis)-1
        if guess1 <= item < guess2:
            return mid+1
        if guess1 > item:
            high = mid-1
        else:
            low = mid+1
    return 0

n,m,k = iim()
a = iil()
cuma = []
if a[0] <= k:
    cuma.append(a[0])
    for i in range(n-1):
        num = cuma[-1]+a[i+1]
        if num > k:
            break
        else:
            cuma.append(num)
b = iil()
cumb = []
if b[0] <= k:
    cumb.append(b[0])
    for i in range(m-1):
        num = cumb[-1]+b[i+1]
        if num > k:
            break
        else:
            cumb.append(num)

if len(cuma) > 0:
    cnt = len(cuma)
    ans = cnt+binary_search(cumb,k-cuma[-1])
    for i,item in enumerate(cuma[::-1]):
        tmp = cnt-i-1
        res = k-item
        plus = binary_search(cumb,res)
        ans = max(ans,tmp+plus)
else:
    ans = binary_search(cumb,k)
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.reverse()
b.reverse()
ans = 0

jikan = k
while jikan > 0 :
    if jikan >= a[-1] or jikan >= b[-1] :
        if a[-1] > b[-1] :
            ans += 1
            jikan -= b[-1]
            del b[-1]
            if len(b) == 0 :
                b.append(2000000000)
        elif b[-1] > a[-1] :
            ans += 1
            jikan -= a[-1]
            del a[-1]
            if len(a) == 0 :
                a.append(2000000000)
        elif a[-1] == b[-1] :
            ans += 1
            for j in range(1,1000000):
                if a[j*(-1)] > b[j*(-1)] :
                    jikan -= b[-1]
                    del b[-1]
                    if len(b) == 0 :
                        b.append(2000000000)
                    break
                elif a[j*(-1)] < b[j*(-1)] :
                    jikan -= a[-1]
                    del a[-1]
                    if len(a) == 0 :
                        a.append(2000000000)
                    break
    else :
        break

print(ans)

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')
    
i, j = 0, 0
n, m = 0, 0
a = []
b = []


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


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

    mins = 0
    while (mins < k):
        v = read(i, j)
        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:
n, m, k = list(map(int,input().split()))
a = list(map(int,input().split()))
b = list(map(int,input().split()))

ans = 0
time = 0
acount = 0
bcount = 0
while time <= k and ans <= m+n:
    if ans == m+n :
        ans += 1
    elif acount == n :
        time += b[bcount]
        bcount += 1
        ans += 1
    elif bcount == m :
        time += a[acount]
        acount += 1
        ans += 1
    elif a[acount] < b[bcount] :
        time += a[acount]
        acount += 1
        ans += 1
    elif a[acount] > b[bcount] :
        time += b[bcount]
        bcount += 1
        ans += 1
    elif a[acount] == b[bcount] :
        if a[acount+1] < b[bcount+1] :
            time += a[acount]
            acount += 1
            ans += 1
        else :
            time += b[bcount]
            bcount += 1
            ans += 1

print(ans-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()))
a_index = 0
b_index = 0
flag = True
while flag:
    if A[a_index] < B[b_index]:
        K -= A[a_index]
        a_index += 1
    else:
        K -= B[b_index]
        b_index += 1
    if K < A[a_index] and K < B[b_index]:
        flag = False
print(a_index + b_index)