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:
n,m,k = map(int,input().split())

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

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

aa = a.reverse()
bb = b.reverse()
ans = 0
while k > 0 :
    if k >= a[-1] or k >= b[-1] :
        if a[-1] > b[-1] :
            ans += 1
            k = k - b[-1]
            del b[-1]
            if len(b) == 0 :
                b.append(2000000000)
        elif b[-1] > a[-1] :
            ans += 1
            k = k - a[-1]
            del a[-1]
            if len(a) == 0 :
                a.append(2000000000)
        elif a[-1] == b[-1] :
            ans += 1
            for j in range(1000000):
                if a[j] > b[j] :
                    k = k - b[-1]
                    del b[-1]
                    if len(b) == 0 :
                        b.append(2000000000)
                    break
                elif a[j] < b[j] :
                    k = k - a[-1]
                    del a[-1]
                    if len(a) == 0 :
                        a.append(2000000000)
                    break
    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=0
B=0
ans=0
t=0
for i in range(n+m+1):
    if A==len(a) :
        t+=b[B]
        if k<t:
            break
        else:
            B+=1
            ans+=1
    elif B==len(b):
        t+=a[A]
        if k<t:
        
            break
        else:
            A+=1
            ans+=1
    else:

        if a[A]>b[B]:
            t+=b[B]
            if k<t:
                break
            else:
                B+=1
                ans+=1
        else:
            t+=a[A]
            if k<t:
            
                break
            else:
                A+=1
                ans+=1
print(ans)

Submission status: Runtime Error

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

if n<=m:
	l=m
else:
	l=n


for ind in range(0,l):
	if s>k:break;
	if a[i]<=b[j]:
		s+=a[i]
		i=i+1
	else:
		s+=b[j]
		j=j+1



print(i+j-1)

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

    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 = (int(each) for each in input().split())
alist = [int(each) for each in input().split()]
blist = [int(each) for each in input().split()]

timer = k
numofbooks = 0
l = 1

while timer > 0 and l != 0:
    l = 0
    tmptimer = timer
    for i in range(min(len(alist), len(blist))):
        if alist[i] == blist[i]:
            tmptimer -= alist[i]
            continue
        elif alist[i] < blist[i] or not blist:
            choice = 'a'
            l = i+1
            tmptimer -= alist[i]
            break
        elif alist[i] > blist[i] or not alist:
            choice = 'b'
            l = i+1
            tmptimer -= blist[i]
            break
    if not blist:
        choice = 'a'
        for i in range(len(alist)):
            l = i+1
            tmptimer -= alist[i]
            break
    if not alist:
        choice = 'b'
        for i in range(len(blist)):
            l = i+1
            tmptimer -= blist[i]
            break

    if tmptimer < 0:
        l = i
        break
    # print(i,alist, blist)
    # print(i, alist, blist, choice, l, tmptimer, timer)
    if choice == 'a':
        for i in range(l):
            timer -= alist.pop(0)
        numofbooks += l
    elif choice == 'b':
        for i in range(l):
            timer -= blist.pop(0)
        numofbooks += l
print(numofbooks)

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()))
t = 0
n = 0
a = 0
b = 0

while K > t :
    if a <= N and b <= M :
        if A[a] <= B[b] :
            t += A[a]
            n += 1
            a += 1

        else :
            t +=B[b]
            n += 1
            b += 1
    
    elif a > N and b <= M:
        while b <= M :
            t += B[b]
            n += 1
            b += 1
        
        else :
            break

    else :
        while a <= N :
            t += A[a]
            n += 1
            a += 1
        
        else :
            break

print(n)

Submission status: Runtime Error

Solution code:
import sys
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
prog=0
a_idx=0
b_idx=0

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

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

mx=a_idx+b_idx

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

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

print(mx)

Submission status: Runtime Error

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

A=[int(x) for x in input().split()]
B=[int(x) for x in input().split()]

a=0
b=0
ans=0
books=N+M
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
                            break
                        else:
                            break
                    
                    elif A[tmpa]<B[tmpb]:
                        if time+A[a]<=K:
                            time+=A[a]
                            a+=1
                            ans+=1
                            break
                        else:
                            break
            
    elif a==N and b!=M:
        if time+B[b]<=K:
            time+=B[b]
            b+=1
            ans+=1
        else:
            break
        
    elif b==M and a!=N:
        if time+A[a]<=K:
            time+=A[a]
            a+=1
            ans+=1
        else:
            break
print(ans)

Submission status: Runtime Error

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


cnt = 0
a_max_k = 0
a_cnt = 0
now_k = 0
now_cnt = 0
for A in a:
    if now_k + A <= k:
        now_cnt += 1
        now_k += A
        if now_cnt > cnt:
            cnt = now_cnt
        a_max_k = now_k
a_cnt = cnt
now_k = a_max_k
now_cnt = cnt
for i in range(m):
    while a_cnt >= 0:
        if k - now_k >= b[i]:
            now_cnt += 1
            now_k += b[i]
            if now_cnt > cnt:
                cnt = now_cnt
            break
        else:
            a_cnt += -1
            now_k += -1 * a[-i] 
            now_cnt += -1
        
print(cnt)

Submission status: Runtime Error

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

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

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

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

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

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

# print(max_a)
# print(i)

# print(max_b)
# print(j)

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

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

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

Submission status: Runtime Error

Solution code:
n, m, k = map(int, input().split())
a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))
a_cnt = 0
b_cnt = 0


def haba(cnt, sum, jogen, list1, list2, a, b):
    if len(list1) + len(list2) < a + b + 2:
        return a + b + 1

    if sum == jogen:
        return cnt
    elif sum > jogen:
        return cnt - 1

    max_ = cnt
    if a <= len(list1) - 1:
        t1 = haba(cnt+1, sum+list1[a], jogen, list1, list2, a + 1, b)
        if max_ < t1:
            max_ = t1
    if b <= len(list2) - 1:
        t2 = haba(cnt+1, sum+list2[b], jogen, list1, list2, a, b + 1)
        if max_ < t2:
            max_ = t2
    return max_


print(haba(0, 0, k, a_list, b_list, 0, 0))

Submission status: Runtime Error

Solution code:
def dfs(a, b, k):
    global cnt
    seen[a][b] = True
    na = a + 1
    nb = b + 1
    if na <= N and seen[na][b] is False and k >= book_a[na]:
        k -= book_a[na]
        cnt = max(cnt, na + b)
        dfs(na, b, k)
    if nb <= M and seen[a][nb] is False and k >= book_b[nb]:
        k -= book_b[nb]
        cnt = max(cnt, a + nb)
        dfs(a, nb, k)

N, M, k = map(int, input().split())
book_a = [int(x) for x in input().split()]
book_b = [int(x) for x in input().split()]
book_a.insert(0, 0)
book_b.insert(0, 0)
cnt = 0
seen = [[False] * (M + 1) for _ in range(N + 1)]
dfs(0, 0, k)
print(cnt)

Submission status: Runtime Error

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

count = 0
while True:
    if bool(reading_time_a) is True and bool(reading_time_b) is True:
        if reading_time_a[0] < reading_time_b[0]:
            if k - reading_time_a[0] >= 0:
                k -= reading_time_a[0]
                reading_time_a.pop(0)
                count += 1
            else:
                break
        elif reading_time_a[0] > reading_time_b[0]:
            if k - reading_time_b[0] >= 0:
                k -= reading_time_b[0]
                reading_time_b.pop(0)
                count += 1
            else:
                break
        else:
            if k - reading_time_a[0] >= 0:
                if reading_time_a[1] > reading_time_b[1]:
                    k -= reading_time_b[0]
                    reading_time_b.pop(0)
                    count += 1
                if reading_time_a[1] <= reading_time_b[1]:
                    k -= reading_time_a[0]
                    reading_time_a.pop(0)
                    count += 1
            else:
                break
    elif bool(reading_time_a) is False and bool(reading_time_b) is False:
        break
    elif bool(reading_time_a) is False:
        if k - reading_time_b[0] >= 0:
            k -= reading_time_b[0]
            reading_time_b.pop(0)
            count += 1
        else:
            break
    elif bool(reading_time_b) is False:
        if k - reading_time_a[0] >= 0:
            k -= reading_time_a[0]
            reading_time_a.pop(0)
            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()))
aot = []
i, j = 0, 0
while sum(aot) <= k:


  if len(a) > 0 and len(b) > 0:
    if a[i] >= b[j]:
      aot.append(b[j])
      del b[j]
      #print('aは{}, bは{}'.format(a, b))
    elif a[i] <= b[j]:
      aot.append(a[i])
      del a[i]
      #print('aは{}, bは{}'.format(a, b))
      
  elif len(a) == 0:
    aot.append(b[j])
    del b[j]
  elif len(b) == 0:
    aot.sppend(a[i])
    del a[i]
  
if sum(aot) > k:
  del aot[-1]
print(len(aot))

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()))
minute=0
ans=0
if A[0]>K and B[0]>K: print(0)
while True:
    if not A and not B:
        break
    elif not A:
        x=B[0]
        minute += x
        del B[0]
        if minute > K:
            break
        ans += 1
    elif not B:
        x=A[0]
        minute += x
        del A[0]
        if minute > K:
            break
        ans += 1
    elif A[0]!=B[0]:
        x=min(A[0],B[0])
        if A[0]<B[0]:
            del A[0]
        else:
            del B[0]
        minute+=x
        if minute>K:
            break
        ans += 1
    else:
        x=A[0]
        while True:
            i=1
            if A[i]<B[i]:
                del A[0]
                break
            elif B[i]<A[i]:
                del B[0]
                break
            else:
                i+=1
        minute += x
        if minute > K:
            break
        ans += 1

print(ans)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10000000000) 
n,m,k = list(map(int,input().split()))
a = list(map(int,input().split()))
b = list(map(int,input().split()))
 
count = 0
for i in range(n+m):
    if a == [] or a[0] > b[0]:
        k -= b[0]
        del b[0]
    else:
        k -= a[0]
        del a[0]
    if k < 0:
        break
    else:
        count += 1
print(count)

Submission status: Runtime Error

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

'''
tmp = 0
count = 0
sum = 0
while 1:
    if n  == 0 and m == 0:
        break
    elif n == 0:
        tmp = b.pop(0)
        m -= 1
    elif m == 0:
        tmp = a.pop(0)
        n -= 1
    elif a[0] > b[0]:
        tmp = b.pop(0)
        m -= 1
    elif a[0] <= b[0]:
        tmp = a.pop(0)
        n -= 1

    sum += tmp
    if sum > k:
        break
    count += 1
print(count
'''
_n = 0
_m = 0
sum = 0
count = 0
while 1:
    if _n >= n and _m >= m:
        break
    elif _n >= n:
        sk -= b[_m]
        _m += 1
    elif _m >= m:
        k -= a[_n]
        _n += 1
    elif a[_n] > b[_m]:
        k -= b[_m]
        _m += 1
    elif a[_n] <= b[_m]:
        k -= a[_n]
        _n += 1
    
    if (k < 0):
        break
    count += 1
print(count)

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

ab_list = a_list + b_list
ab_list.sort()

dp = [False] * 200000*2

dp[0] = ab_list[0]
j = ab_list[0]

count=1

for i in range(1,len(ab_list)):
    # print(ab_list[i]+j)
    a_min = a_list.pop(0)
    b_min = b_list.pop(0)
    dp[i] = min(a_min, b_min) + j
    j += ab_list[i]
    
    if dp[i]<=k:
        count+=1
    
    if j+ab_list[i+1]>k:
        break

    

print(count)

Submission status: Runtime Error

Solution code:
n, m, k = map(int, input().strip().split())
a = list(map(int, input().strip().split()))
b = list(map(int, input().strip().split()))
c = 0
if sum(a) + sum(b) <= k: print(len(a) + len(b))
else:
    while 1:
        if c >= n+m:  # cannot read from any table anymore
            c += 1
            break
        elif len(a) <= 0 or a[0] > k:  # cannot read from a anymore
            k -= b[0]
            c += 1
            b.pop(0)
        elif len(b) <= 0 or b[0] > k:  # cannot read from b anymore
            k -= a[0]
            c += 1
            a.pop(0)
        else:
            if a[0] < b[0]:
                # read from a
                k -= a[0]
                c += 1
                a.pop(0)
            else:
                # read from b
                k -= b[0]
                c += 1
                b.pop(0)
        if k <= 0:
            break
    print(c-1)

Submission status: Runtime Error

Solution code:
n,m,k=map(int,input().split())
l1=list(map(int,input().split()))
l2=list(map(int,input().split()))
l3=[]
time=0
books=0
if n<=m:
    for i in range(n):
        if i%2==0:
            for j in range(n):
               l3.append(l1[j])
        else:
            for j in range(n):
              l3.append(l2[j])
    for x in range(n,m):
        l3.append(l2[x])
else:
    for i in range(m):
        if i%2==0:
            for j in range(m):
              l3.append(l2[j])
        else:
            for j in range(m):
              l3.append(l1[j])
    for i in range(m,n):
        l3.append(l1[i])
for i in range(n+m):
    if time > k or time > sum(l3) or l3[0]>k:
        break
    else:
        books+=1
        time+=l3[i]
print(books)

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

# print(K)
# print(a)
# print(b)

# K=2
# a=[60, 90, 120]
# b=[80, 150, 80, 150]

ans=0
inf=K*2

for i in range (N+M):
    if a==[]:
        a.append(inf)
    if b==[]:
        b.append(inf)
    A=a[0]
    B=b[0]
    if min(A,B)<=K:
        ans+=1
        K-=min(A,B)
        if A<B:
            a.pop(0)
        elif A>B:
            b.pop(0)
        else:
            if a[0]+a[1]<=b[0]+b[1]:
                a.pop(0)
            else:
                b.pop(0)
    else:
        break
print(ans)

Submission status: Runtime Error

Solution code:
import sys #追加
sys.setrecursionlimit(500*500) #追加

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

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

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

resolve()

Submission status: Runtime Error

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

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

dfs(0, 0, T, 0)
print(ans)

Submission status: Runtime Error

Solution code:
from sys import stdin, stdout
import heapq
import cProfile
from collections import Counter, defaultdict, deque
from functools import reduce
import math
from bisect import bisect, bisect_right, bisect_left


def get_int():
    return int(stdin.readline().strip())


def get_tuple():
    return map(int, stdin.readline().split())


def get_list():
    return list(map(int, stdin.readline().split()))


def fun(n, m, k, ans):
    global mx, a, b, dp,save

    mx = max(mx, ans)

    if n <= 0 and m <= 0 or k <= 0:
        return ans

    key = str(n)+"_"+str(m)+"_"+str(k)+"_"+str(ans)
    if key in dic:
        save += 1
        return dic[key]

    x, y = -100, -100
    if n > 0 and a[n - 1] <= k:
        key1 = str(n-1)+"_"+str(m)+"_"+str(k - a[n - 1])+"_"+str(ans+1)
        x = fun(n - 1, m, k - a[n - 1], ans + 1)
        dic[key1] = x

    if m > 0 and b[m - 1] <= k:
        key2 = str(n) + "_" + str(m-1) + "_" + str(k - b[m - 1])+"_"+str(ans+1)
        y = fun(n, m - 1, k - b[m - 1], ans + 1)
        dic[key2] = y

    dic[key] = max(x,y)
    return max(x, y)


mx = 0
n, m, k = get_tuple()
dic = defaultdict(int)
a = get_list()
b = get_list()
a = a[::-1]
b = b[::-1]
fun(n, m, k, 0)
print(mx)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
string s,t;
ll a[200500], b[200500], pa[200500], pbb[200500];

int main() {
	//freopen("input.txt","r",stdin);
 	//freopen("output.txt","w",stdout);
    int n,m,x,i;
	ll tests=1;
	while (tests--)
	{
		cin >> n >> m >> x;
		for (i = 0; i < n; i++)
		{
			cin >> a[i];
			pa[i+1] = pa[i] + a[i];
		}
		for (i = 0; i < m; i++)
		{
			cin >> b[i];
			pbb[i+1] = pbb[i] + b[i];
		}
		int ans = 0;
		for (i = 0; i <= n; i++)
		{
			if (pa[i] <= x)
			   ans = max(ans, i+(int)(upper_bound(pbb, pbb+m+1, x-pa[i])-pbb-1));
			else
				break;
		}
		cout << ans << endl;
	}
    return 0;
}

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 = 0
def D(a_list, b_list, K, count):
    global ans
    
    if len(a_list) == 0 and len(b_list) == 0:
        ans = max(ans, count)
        print(ans)
        sys.exit()
    
    elif len(a_list) == 0:
        if K - b_list[0] < 0:
            ans = max(ans, count)
            print(ans)
            sys.exit()
        else:
            D(a_list, b_list[1:], K-b_list[0], count+1)
        
    elif len(b_list) == 0:
        if K - a_list[0] < 0:
            ans = max(ans, count)
            print(ans)
            sys.exit()
        else:
            D(a_list[1:], b_list, K-a_list[0], count+1)
            
    else:
        
        if K - a_list[0] < 0:
            ans = max(ans, count)
        else:
            D(a_list[1:], b_list, K-a_list[0], count+1)
            
        if K - b_list[0] < 0:
            ans = max(ans, count)  
        else:
            D(a_list, b_list[1:], K-b_list[0], count+1)
            
D(A_list, B_list, K, 0)
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N, M, K = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))
Acount = -1
Bcount = -1
tmpA = []
tmpB = []
tmp = 0
for i in range(N):
    tmp += A[i]
    if tmp > K:
        break
    tmpA.append(tmp)
    Acount = i
tmp = 0
for i in range(M):
    tmp += B[i]
    if tmp > K:
        break
    tmpB.append(tmp)
    Bcount = i

if len(tmpA) > 0 and len(tmpB):
    res = 0
    Bcount = 0
    Acount = len(tmpA) - 1
    while Acount > -1 and Bcount < M:
        for i in range(Bcount, M):
            if tmpA[Acount] + tmpB[i] > K:
                break
            Bcount = i
        tmpRes = Acount + Bcount + 2
        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:
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=N+M
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:
        if time+B[b]<=K:
            time+=B[b]
            b+=1
            ans+=1
        else:
            break
        
    elif b==M and a!=N:
        if time+A[a]<=K:
            time+=A[a]
            a+=1
            ans+=1
        else:
            break
print(ans)

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)

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

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

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

C1 = np.zeros((len(A)*len(B),2))
lenB = len(B)
for i in range(len(A)):
    for j in range(len(B)):
        C1[lenB*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:

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
    
    j = 0
    
    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:
def resolve():
    def dfs(a, b, k):
        nonlocal cnt
        seen[a][b] = True
        na = a + 1
        nb = b + 1
        if na <= N and seen[na][b] is False and k >= book_a[na]:
            k -= book_a[na]
            cnt = max(cnt, na + b)
            dfs(na, b, k)
        if nb <= M and seen[a][nb] is False and k >= book_b[nb]:
            k -= book_b[nb]
            cnt = max(cnt, a + nb)
            dfs(a, nb, k)

    N, M, k = map(int, input().split())
    book_a = [int(x) for x in input().split()]
    book_b = [int(x) for x in input().split()]
    book_a.insert(0, 0)
    book_b.insert(0, 0)
    cnt = 0
    seen = [[False] * (M + 1) for _ in range(N + 1)]
    dfs(0, 0, k)
    print(cnt)


if __name__ == "__main__":
    resolve()