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
from io import StringIO
import unittest

def yn(b):
    print("Yes" if b==1 else "No")
    return

def resolve():
    readline=sys.stdin.readline

    n,m,k=map(int, readline().rstrip().split())
    aa=list(map(int, readline().rstrip().split()))
    bb=list(map(int, readline().rstrip().split()))
    asum=[0]*n
    bsum=[0]*m
    memo=[0]*n
    asum[0]=aa[0]
    bsum[0]=bb[0]
    mp=n-1
    ans=0
    for i in range(1,n):
        asum[i]=(aa[i]+asum[i-1])
    for i in range(1,m):
        bsum[i]=(bb[i]+asum[i-1])

    for i in range(m+1):
        if i==0:
            x=0
        else:
            x = bsum[i-1]
        while mp>=0:
            if asum[mp]+x <= k:
                break
            else:
                mp-=1
        if mp>=0:
            if asum[mp]+x <= k:
                ans = max(i + mp+1,ans)
        else:
            if x <= k:
                ans = max(i,ans)
    print(ans)

    return

if 'doTest' not in globals():
    resolve()
    sys.exit()

### ----------------
### ここまで 
### ----------------

Submission status: Runtime Error

Solution code:
a = input()
n = input()
m = input()
first = a.split(" ")
N = n.split(" ")
M = m.split(" ")

overflow = 0
for i in N:
    if int(i) > 10**9:
        overflow = 1
        break
        
for i in M:
    if int(i) > 10**9:
        overflow = 1
        break


if overflow == 0:
    totaltime = 0
    count = 1
    for i in range(int(first[1]) + int(first[0])):
      if totaltime + int(N[0]) > int(first[2]) or totaltime + int(M[0]) > int(first[2]):
        break
      if int(N[0]) < int(M[0]):
        totaltime += int(N[0])
        N.remove(N[0])
        count += 1
      elif int(M[0]) < int(N[0]):
        totaltime += int(M[0])
        M.remove(M[0])
        count += 1
    print(count)
else:
    print(0)

Submission status: Runtime Error

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

from collections import deque
q = deque([[0, 0, 0]])
ans = 0
while q:
    t, ai, bi = q.pop()
    if t > k:
        continue
    ans = max(ans, ai+bi)

    if ai < n:
        q.append([t + A[ai], ai+1, bi])
    if bi < m:
        q.append([t + B[ai], ai, bi+1])

print(ans)

Submission status: Runtime Error

Solution code:
import sys
from collections import deque

readline = sys.stdin.readline
readlines = sys.stdin.readlines
ns = lambda: readline().rstrip() # input string
ni = lambda: int(readline().rstrip()) # input int
nm = lambda: map(int, readline().split()) # input multiple int 
nl = lambda: list(map(int, readline().split())) # input multiple int to list

n, m, k = nm()
A = nl()
B = nl()
a = deque(A)
b = deque(B)
cnt = 0
time = 0
while True:
    if a[0] < b[0]:
        tmp= a.popleft()
    else:
        tmp = b.popleft()
    
    if tmp + time <= k:
        cnt += 1
        time += tmp
    else:
        break
print(cnt)

Submission status: Runtime Error

Solution code:
def main():
    n, m, k = map(int, input().split())
    alis = list(map(int, input().split()))
    blis = list(map(int, input().split()))
    adic = {}
    bdic = {}
    for i in range(len(alis)):
        adic[i+1] = alis[i]
    for i in range(len(blis)):
        bdic[i+1] = blis[i]

    time = 0
    ans = 0
    a = 1
    b = 1
    while time< k:
        #print(alis, blis, len(alis), k, time)
        if a > len(adic) and b > len(bdic):
            break
        elif a > len(adic) and bdic[b] <= k-time:
            time += bdic[b]
            b += 1
            ans += 1
        elif b > len(bdic) and adic[a] <= k-time:
            time += adic[a]
            a += 1
            ans += 1
        else:
            #print(alis, blis)
            if adic[a] <= bdic[b] and time + adic[a] <= k:
                #print(time, alis[0], blis[0])
                time += adic[a]
                a += 1
                ans += 1
            elif adic[a] > bdic[b] and time + bdic[b] <= k:
                #print(time)
                time += bdic[b]
                b += 1
                ans += 1
            else:
                break


    print(ans)

    
if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
from collections import deque

input_data = [input().split() for l in range(3)]

limit = int(input_data[0][2])
deskA = deque([int(m) for m in input_data[1]])
deskB = deque([int(m) for m in input_data[2]])
num = 0

while True:
  if not deskA and not deskB:
    break
  if not deskA:
    limit -= deskB.popleft()
  elif not deskB:
    limit -= deskA.popleft()
  elif deskA[0] > deskB[0]:
    limit -= deskB.popleft()
  elif deskA[0] < deskB[0]:
    limit -= deskA.popleft()
  else:
    a = deskA[0]+deskA[1]
    b = deskB[0]+deskB[1]
    if a > b:
      limit -= deskB.popleft()
    else:
      limit -= deskA.popleft()
      
  if limit < 0:
    break
  num += 1

print(num)

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
while K>=total:
  i=0
  
  if K<b_book[0] or K<a_book[0]:
    count = 0
    break
  elif sum(a_book)+sum(b_book)<=K:
    count = N + M
    break
  elif a_book[i] <= b_book[i]:
    total += a_book[i]
    count+=1
    a_book.pop(i)
  else:
    total += b_book[i]
    count+=1
    b_book.pop(i)
    
if count==0:
  print(count)
elif count<N+M:
  print(count-1)
else:
  print(count)

Submission status: Runtime Error

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

n,m,k = map(int, input().split())
a = list(map(int, input().split()))
a.append(10**9+1)
b = list(map(int, input().split()))
b.append(10**9+1)
ans = 0
count = 0
while(ans<=k or count>=n+m):
    print(ans)
    count+=1
    if a[0] > b[0]:
        ans+=b[0]
        b.pop(0)
    elif a[0] < b[0]:
        ans+=a[0]
        a.pop(0)
    else:
        for i in range(1,3):
            if a[i] > b[i]:
                ans+=b[0]
                b.pop(0)
                break
            elif a[i]<b[i]:
                ans+=a[0]
                a.pop(0)
                break
print(count)

Submission status: Runtime Error

Solution code:
n, m, k = map(int, input().split(' '))
list_a = list(map(int, input().split(' ')))
list_b = list(map(int, input().split(' ')))

a_sum=[list_a[0]]
for i in range(1, n):
  sum=list_a[i]+a_sum[i-1]
  a_sum.append(sum)
  
b_sum=[list_b[0]]
for i in range(1, m):
  sum=list_b[i]+b_sum[i-1]
  b_sum.append(sum)
  
ans=[]
if (a_sum[0]>k) & (b_sum[0]>k):
  print(0)
else:
  for i in range(n):
    for j in range(m):
      if a_sum[i]+b_sum[j] <= k:
        key=i+j+2
        if key not in ans:
          ans.append(key)

print(max(ans))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

    total_time = 0
    count = 0
    pos = 0

    for i in range(len(A)):
        if total_time + A[i] <= K:
            total_time += A[i]
            count += 1
        else:
            pos = i
            break
    
    for j in range(len(B)):
        if total_time + B[j] <= K:
            total_time += B[j]
            count += 1
        else:
            break
    
    if pos == 0:
        print(count)
        exit()
    
    temp = count

    for i in range(pos-1, -1, -1):

        while j < len(B) and total_time + B[j] <= K:
            totoal_time += B[j]
            temp += 1
            j += 1
        
        if temp > count:
            count = temp

        if j == len(B):
            break
        
        total_time -= A[i]
        temp -= 1

    print(count)



if __name__ == '__main__':
    main()

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()))
from collections import deque
p=deque(a)
q=deque(b)

import sys
time=0
ans=0
while(1):
    if len(p)==0 and len(q)==0:
        print(ans)
        sys.exit()
    if len(p)>0 and len(q)>0:
        if p[0]>q[0]:
            time=time+q.popleft()
            if time>k:
                print(ans)
                sys.exit()
            else:
                ans=ans+1
        elif p[0]<q[0]:
            time=time+p.popleft()
            if time>k:
                print(ans)
                sys.exit()
            else:
                ans=ans+1
        else:
            if p[1]>=q[1]:
                la=q.popleft()
                if la==1000000000:
                    print(ans)
                    sys.exit()
                time=time+la
                if time>k:
                    print(ans)
                    sys.exit()
                else:
                    ans=ans+1
            elif p[0]<q[0]:
                la=p.popleft()
                if la==1000000000:
                    print(ans)
                    sys.exit()
                time=time+la
                if time>k:
                    print(ans)
                    sys.exit()
                else:
                    ans=ans+1
    elif len(p)==0:
        la=q.popleft()
        if la==1000000000:
            print(ans)
            sys.exit()
        time=time+la
        if time>k:
            print(ans)
            sys.exit()
        else:
            ans=ans+1
    elif len(q)==0:
        la=p.popleft()
        if la==1000000000:
            print(ans)
            sys.exit()
        time=time+la
        if time>k:
            print(ans)
            sys.exit()
        else:
            ans=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()))


class X:
    def __init__(self, K, A, B, N, M):
        self.A = A
        self.B = B
        self.K = K
        self.N = N
        self.M = M
        self.ret = 0

    def helper(self, i, j, time, num_a, num_b):
        if time > self.K:
            return

        self.ret = max(self.ret, num_a + num_b)

        if num_a < self.N:
            self.helper(i + 1, j, time + self.A[i], num_a + 1, num_b)

        if num_b < self.M:
            self.helper(i, j + 1, time + self.B[j], num_a, num_b + 1)

        return

    def run(self):
        self.helper(0, 0, 0, 0, 0)

        return self.ret


c = X(K, A, B, N, M)
print(c.run())

Submission status: Runtime Error

Solution code:
str2 = input()
lst = str.split(' ')
n,m,k = lst
n = int(n)
m = int(m)
k = int(k)

a = str1.split(' ')
b = str2.split(' ')
i=0
j=0
count = 0
c=0
flaga=0
flagb = 0

while c<=k and flaga==0 and flagb==0:
  
  
  if i<len(a):
    
    c = c+int(a[i])
    if c<=k:
      a.pop(i)
      i = i+1
      count = count+1
    else:
      c = c-int(a[i])
      flaga=1

  if j<len(b):
    c = c+int(b[j])
    if c<=k:
      b.pop(j)
      j = j+1
      count = count+1
    else:
      c = c-int(b[j])
      flagb=1

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()))
ai = -1
bi = -1
answer = 0
t_sum = 0
if sum(a) + sum(b) <= k:
    print(n + m)
elif sum(a) <= k:
    t_sum = sum(a)
    ai = n-1
    while True:
        bi += 1
        if t_sum + b[bi] > k:
            break
        t_sum += b[bi] 
    bi -= 1
    answer = ai + bi + 2
    while ai >= 0:
        t_sum -= a[ai]
        ai -= 1
        while bi <= m-1:
            bi += 1
            if bi <= m-1:
                if t_sum + b[bi] > k:
                    break
            t_sum += b[bi]
        bi -= 1
        if ai + bi +2 > answer:
            answer = ai + bi +2
    print(answer)
else:
    while True:
        ai += 1
        if t_sum + a[ai] > k:
            break
        t_sum += a[ai]
    ai -= 1
    while True:
        bi += 1
        if t_sum + b[bi] > k:
            break
        t_sum += b[bi]
    bi -= 1
    answer = ai + bi + 2
    while ai >= 0:
        t_sum -= a[ai]
        ai -= 1
        while bi <= m-1:
            bi += 1
            if bi <= m-1:
                if t_sum + b[bi] > k:
                    break
            t_sum += b[bi]
        bi -= 1
        if ai + bi +2 > answer:
            answer = ai + bi +2
    print(answer)

Submission status: Runtime Error

Solution code:
import numpy as np
import sys
A, B, t = list(map(int, input().split()))
a = np.array(list(map(int, input().split())))
b = np.array(list(map(int, input().split())))
for i in range(1, A):
    a[i] = a[i]+a[i-1]
for i in range(1, B):
    b[i] = b[i]+b[i-1]
s = np.zeros((A+1, B+1))
s[0, 1:] = b.copy()
s[1:, 0] = a.copy()
c = 0
for i in range(1, A+1):
    for j in range(1, B+1):
        s[i, j] = s[i, 0]+s[0, j]
        if s[i, j] <= t:
            if i+j>c:
                c = i+j
        else:
            break
print(c)

Submission status: Runtime Error

Solution code:
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
a_sum = [0]
b_sum = [0]
for i in range(n):
    a_sum.append(a[i]+a_sum[i])
    if a_sum[i]>1e+9:
        break
for i in range(m):
    b_sum.append(b[i]+b_sum[i])
    if b_sum[i]>1e+9:
        break
        
ans = 0
for i in range(len(b_sum)):
    if a_sum[i]>k:
        break
    time = k - a_sum[i]
    for j in range(len(b_sum)):
        if time<b_sum[j+1]:
            break
    if time>=b_sum[m]:
        j = m
    ans = max(ans,i+j)
    
print(ans)

Submission status: Runtime Error

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

    ai = 0
    t = 0
    cnt = 0
    while True:
        if ai < len(A) and t + A[ai] <= K:
            t += A[ai]
            ai += 1
            cnt += 1
            continue
        break

    ans = cnt
    bi = 0
    while True:
        if bi < len(B):
            while ai >= 0 and t + B[bi] > K:
                t -= A[ai]
                ai -= 1
                cnt -= 1
            if t + B[bi] <= K:
                t += B[bi]
                bi += 1
                cnt += 1
                ans = max(ans, cnt)
                continue
        break

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
str2 = input()
lst = str.split(' ')
n,m,k = lst
n = int(n)
m = int(m)
k = int(k)

a = str1.split(' ')
b = str2.split(' ')
i=0
j=0
count = 0
c=0
flaga=0
flagb = 0
print(a)
print(k)
while c<=k and flaga==0 and flagb==0:
  
  
  if i<len(a):
    
    c = c+int(a[i])
    if c<=k:
      a.pop(i)
      i = i+1
      count = count+1
    else:
      c = c-int(a[i])
      flaga=1
  print("c:",c)
  if j<len(b):
    c = c+int(b[j])
    if c<=k:
      b.pop(j)
      j = j+1
      count = count+1
    else:
      c = c-int(b[j])
      flagb=1
    print("c:",c)
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_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(10**10) 
    else:
        if k - time_cnt < b[b_cnt]:
            break
        time_cnt += b[b_cnt]
        b_cnt += 1
        if b_cnt == n:
            b.append(10**10) 
print(a_cnt+b_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()))
cnt=0
def onlyb(y, cnt, s):
  if s+b[y]>k:
    return cnt
  else:
    if y==len(b)-1:
      return cnt+1
    else: 
      return onlyb(y+1, cnt+1, s+b[y])
def onlya(x, cnt, s):
  if s+a[x]>k:
    return cnt
  else:
    if x==len(a)-1:
      return cnt+1
    else:
      return onlya(x+1, cnt+1, s+a[x])
def dfs(x: int, y: int, cnt: int, s:int):
  if s+a[x]>k and s+b[y]>k: # それぞれ残り２個以上で、両方ともNGだった場合
    return cnt  
  else:
    if a[x]>b[y]:  #ｂの方がみじかいばあい
      if y==len(b)-1:    
        return onlya(x, cnt+1, s+b[y])
      else:
        return dfs(x, y+1, cnt+1, s+b[y])
    else:
      if x==len(a)-1:    
        return onlyb(y, cnt+1, s+a[x])
      else:
        return dfs(x+1, y, cnt+1, s+a[x])
print(dfs(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()))

A = sorted(A)
B = sorted(B)
ans = 0
time = 0
i = 1
for _ in range(n+m):
    a,b = A[0], B[0]
    if a == b:
        if k >= time +a:
            ans +=1
            time += a
            A.pop(0)
            if k >= time +b:
                ans +=1
                time +=b
                B.pop(0)
            else:break
        else:break
    else:
        c = min(a,b)
        if k >= time +c:
            ans +=1
            time += c
            if c ==a:
                A.pop(0)
            else:
                B.pop(0)
        else:break
        
print(ans)

Submission status: Runtime Error

Solution code:
a = int(input().split())
x = int(input().split())
y = int(input().split())
z = x.append(y)
t = 0
m = 0
for i in range(len(z)):
  if t > a[2]:
    break
  t += sorted(z)[i]
print(t)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10e9)
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

AA = list(reversed(A))
BB = list(reversed(B))

rest = K
ans = 0

def dfs(cost, aindex, bindex, score):

    if K == cost:
        # print("1:", score, cost)
        return score
    elif K < cost:
        # print("2:", score, cost)
        return score - 1
        # return -1
    
    if aindex == N and bindex == M:
        # print("3:", score, cost)
        return score

    if aindex >= N:
        return dfs(cost + B[bindex], aindex, bindex+1, score + 1)
    
    elif bindex >= M:
        return dfs(cost + A[aindex], aindex+1, bindex, score + 1)

    else:
        return max(dfs(cost + A[aindex], aindex+1, bindex, score + 1), dfs(cost + B[bindex], aindex, bindex + 1, score + 1))

print(max(dfs(A[0], 1, 0, 1), dfs(B[0], 0, 1, 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()))

S = [0]

Sum = 0


for i in range(len(A)):
  K = K - A[i]
  if K > 0:
    Sum += 1
  else:
    K = K + A[i]
    break
S.append(Sum)

for j in range(len(B)):
  K = K - B[j]
  if K > 0:
    Sum += 1
  else:
    if i == 0:
      break
    while(K<0):
      i = i - 1
      K = K + A[i]
      Sum = Sum - 1
    Sum += 1
  S.append(Sum)
print(max(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()))
i,j = 0,0
K = k
a.append(k)
b.append(k)
ans1,ans2 = 0,0
for l in range(n+m):
    if a[i] <= b[j] and a[i] <= k:
        k -= a[i]
        i += 1
        ans1 += 1
    elif a[i] > b[j] and b[j] <= k:
        k -= b[j]
        j += 1
        ans1 += 1
i,j = 0,0
for l in range(n+m):
    if a[i] < b[j] and a[i] <= K:
        k -= a[i]
        i += 1
        ans2 += 1
    elif a[i] >= b[j] and b[j] <= K:
        k -= b[j]
        j += 1
        ans2 += 1
print(ans1) if ans1 >= ans2 else print(ans2)

Submission status: Runtime Error

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

def read(a, b, c, ans):

    tmp = 0

    if len(a) == 0 or len(b) == 0:
        if len(a) == 0 and len(b) == 0:
            return c, ans
        elif len(a) == 0:
            tmp = b[0]
            b = b[1:]
        else:
            tmp = a[0]
            a = a[1:]
    else:
        if a[0] == b[0]:
            aa = read(a[1:], b, c, ans)
            bb = read(a, b[1:], c, ans)
            if aa[0] >= bb[0]:
                a = a[1:]
                tmp = aa[0]
                ans += aa[1]
            else:
                b = b[1:]
                tmp = bb[0]
                ans += bb[1]

        elif a[0] < b[0]:
            tmp = a[0]
            a = a[1:]
        else:
            tmp = b[0]
            b = b[1:]

    if c + tmp > k:
        return c + tmp, ans
    else:
        c += tmp
        ans += 1
        return read(a, b, c, ans)


(_, ans) = read(a, b, 0, 0)

print(ans)

Submission status: Runtime Error

Solution code:
def readBook(i, j, time, cnt):
    if time < 0:
        return cnt - 1
    if i == N and j == M:
        return cnt
    elif i == N:
        res = readBook(i, j + 1, time - B[j], cnt + 1)
    elif j == M:
        res = readBook(i + 1, j, time - A[i], cnt + 1)
    else:
        res = max(readBook(i + 1, j, time - A[i], cnt + 1), readBook(i, j + 1, time - B[j], cnt + 1))
    return res


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

print(readBook(0, 0, K, 0))

Submission status: Runtime Error

Solution code:

line = input().split(" ")
N = int(line[0])
M = int(line[1])
K = int(line[2])

N_arr = input().split(" ")
M_arr = input().split(" ")

cnt = 0
i_N = 0 
i_M = 0

def check_next(i_N, i_M):
    if(i_N+1 < N):
        N_top_next = int(N_arr[i_N+1])
    else:
        N_top_next = 10000000000

    if(i_M+1 < M):
        M_top_next = int(M_arr[i_M+1])
    else:
        M_top_next = 10000000000


    if(N_top_next > M_top_next):
        return "M"
    
    elif(N_top_next == M_top_next):
        return check_next(i_N+1, i_M+1)

    else:
        return "N"

while(True):
    if(i_N < N):
        N_top = int(N_arr[i_N])
    else:
        N_top = 10000000000

    if(i_M < M):
        M_top = int(M_arr[i_M])
    else:
        M_top = 10000000000

    if(N_top <= K or M_top <= K):
        if(N_top > M_top):
            K = K - M_top
            i_M += 1
        elif(N_top == M_top):
            flag = check_next(i_N, i_M)
            if(flag == "N"):
                K = K - N_top
                i_N += 1
            else:
                K = K - M_top
                i_M += 1
        else:
            K = K - N_top
            i_N += 1
        cnt += 1

    else:
        break

print(cnt)

Submission status: Runtime Error

Solution code:
def book(a,b, i, j, read):
    if i >= len(a):
        read = read + b[j]
        j = j + 1

    elif j >= len(b):
        read = read + a[i]
        i = i + 1

    elif a[i] < b[j]:
        read = read + a[i]
        i = i + 1

    elif a[i] > b[j]:
        read = read + b[j]
        j = j + 1
    else:
        return book(a, b, i+1, j+1,read)
    return read

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

read = 0
i = 0
j = 0
while(read < K):
    if i >= len(a):
        read = read + b[j]
        j = j + 1

    elif j >= len(b):
        read = read + a[i]
        i = i + 1

    elif a[i] < b[j]:
        read = read + a[i]
        i = i + 1

    elif a[i] > b[j]:
        read = read + b[j]
        j = j + 1
    else:
        read = book(a,b,i,j, read)
if read==K:
    print(i+j)
elif i==0 and j==0:
    print(0)
else:
    print(i+j-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_sum = [0]
b_sum = [0]
for i in range(n):
    a_sum.append(a[i]+a_sum[i])
    if a_sum[i]>1e+9:
        break
for i in range(m):
    b_sum.append(b[i]+b_sum[i])
    if b_sum[i]>1e+9:
        break
        
ans = 0
for i in range(n+1):
    if a_sum[i]>k:
        break
    time = k - a_sum[i]
    for j in range(m):
        if time<b_sum[j+1]:
            break
    if time>=b_sum[m]:
        j = m
    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()))
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]):
                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+1]<B[b+1]:
                    tt += A[a]
                    a += 1
                    if tt > K:
                        a -= 1
                        break

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

                elif tt == K:
                    break
            elif (A[a]==B[b]) and (a < N)and (b == M):
                tt +=A[a]
                a += 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:
a = input().split()
x = input().split()
y = input().split()
z = x.append(y)
t = 0
m = 0
for i in range(len(z)):
  if t > a[2]:
    break
  t += sorted(z)[i]
print(t)

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

x = [a[0]]
y = [b[0]]
for i in range(1, n):
    x.append(x[i-1] + a[i])
for i in range(1, m):
    y.append(y[i-1] + b[i])
    
t = 0
i = 0
j = 0
ans = 0
l = 0
r = 0
while i < n or j < m:
    if i < n and x[i]-l <= y[j]-r:
        t += a[i]
        l += a[i]
        i += 1
    elif j < m:
        t += b[j]
        r += b[j]
        j += 1
    if t > k:
        break
    ans += 1

print(ans)

Submission status: Runtime Error

Solution code:
NIF = float('-INF')
def calculate(A,B, K, ai, bi, book_count, times):
	m = NIF
	if times > K:
		return book_count-1
	if times == K or (ai >= len(A) and bi >= len(B)):
		return book_count
	
	# select A
	if ai < len(A):
		c = calculate(A,B, K, ai+1, bi, book_count+1, times + A[ai])

		if c > m:
			m = c
		

	# select B
	if bi < len(B):
		c = calculate(A,B, K, ai, bi+1, book_count+1, times + B[bi])

		# backtrack
		if c > m:
			m = c
	return m

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

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

Submission status: Runtime Error

Solution code:
A = []
B = []
N = int(input())
M = int(input())
K = int(input())

for i in range(N):
    A.append(int(input()))
for j in range(M):
    B.append(int(input()))
           
red_book = 0
red_time = 0
books = A + B
books = sorted(books)
for book in books:
    red_time += book
    if red_time < K:
        red_book += 1
    else:
        break
    
print(red_book)