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:
# coding: utf-8
#import numpy as np
import re
import math
from collections import defaultdict,deque,Counter
from fractions import Fraction
import bisect
import itertools
from itertools import accumulate
from copy import deepcopy
import heapq
import random
import time
import os
import sys
from functools import lru_cache,reduce
readline=sys.stdin.readline
sys.setrecursionlimit(100000)
alp="abcdefghijklmnopqrstuvwxyz"
numbers="0123456789"
mod=int(10**9+7)
inf=int(10**19)
class Set():
    def __init__(self,A=[]):
        self.heap=[]
        self.dic=defaultdict(bool)
        self.size=0
        for a in A:self.add(a)
    
    def __str__(self):
        return str(self.heap)
    
    def add(self,x):
        if not self.find(x):
            heapq.heappush(self.heap,x)
            self.dic[x]=True
            self.size+=1

    def remove(self,x):
        if self.find(x):
            self.dic[x]=False
            self.size-=1

    def find(self,x):
        return self.dic[x]
    
    def top(self):
        while self.heap and self.dic[self.heap[0]]==False:
            heapq.heappop(self.heap)
        if self.heap:return self.heap[0]
        else:return None

    def pop(self):
        ret=None
        if self.heap:
            ret=self.top()
            self.remove(ret)
        return ret
class multiset():
    def __init__(self,A=[]):
        self.heap=[]
        self.dic=defaultdict(int)
        self.size=0
        for a in A:self.add(a)
    
    def __str__(self):
        return str(self.heap)
    
    def add(self,x):
        heapq.heappush(self.heap,x)
        self.dic[x]+=1
        self.size+=1

    def remove(self,x):
        if self.dic[x]>0:
            self.dic[x]-=1
            self.size-=1
    
    def count(self,x):
        return self.dic[x]

    def find(self,x):
        return self.dic[x]>0
    
    def top(self):
        while self.heap and self.dic[self.heap[0]]==0:
            heapq.heappop(self.heap)
        if self.heap:return self.heap[0]
        else:return None

    def pop(self):
        ret=None
        if self.heap:
            ret=self.top()
            self.remove(ret)
        return ret
class Edge():
    def __init__(self,x,y,val=1):
        self.x=x
        self.y=y
        self.val=val
class unionfind():
    def __init__(self,n):
        self.n=n
        self.P=[a for a in range(n)]
        self.rank=[0]*n
 
    def find(self,x):
        if(x!=self.P[x]):
            self.P[x]=self.find(self.P[x])
        return self.P[x]
 
    def same(self,x,y):
        return self.find(x)==self.find(y)
 
    def link(self,x,y):
        if self.rank[x]<self.rank[y]:
            self.P[x]=y
        elif self.rank[y]<self.rank[x]:
            self.P[y]=x
        else:
            self.P[x]=y
            self.rank[y]+=1
 
    def unite(self,x,y):
        self.link(self.find(x),self.find(y))
 
    def size(self):
        S=set()
        for a in range(self.n):
            S.add(self.find(a))
        return len(S)
def yn(b):print("yes" if b else "no")
def Yn(b):print("Yes" if b else "No")
def YN(b):print("YES" if b else "NO")
def ispow(a,b):
    now=b
    while now<a:
        now*=b
    return now==a
def getbin(num,size):
    A=[0]*size
    for a in range(size):
        if (num>>(size-a-1))&1==1:
            A[a]=1
        else:
            A[a]=0
    return A
def getfacs(n,mod_=0):
    A=[1]*(n+1)
    for a in range(2,len(A)):
        A[a]=A[a-1]*a
        if(mod_>0):A[a]%=mod_
    return A
def comb(n,r,mod,fac):
    if(n-r<0):return 0
    return (fac[n]*pow(fac[n-r],mod-2,mod)*pow(fac[r],mod-2,mod))%mod
def nextcomb(num,size):
    x=num&(-num)
    y=num+x
    z=num&(~y)
    z//=x
    z=z>>1
    num=(y|z)
    if(num>=(1<<size)):return False
    else:return num
def getprimes(n):
    if n==0:return []
    A=[True]*(n+1)
    A[0]=False
    A[1]=False
    for a in range(2,n+1):
        if A[a]:
            for b in range(a*2,n+1,a):
                A[b]=False
    ret=[]
    for a in range(n+1):
        if(A[a]):ret.append(a)
    return ret
def isprime(num):
    if(num<=1):return False
    i=2
    while i*i<=num:
       if(num%i==0):return False
       i+=1
    return True
def ifelse(b,t,f):return t if b else f
def factorize(n):
    b = 2
    ret=defaultdict(int)
    while b * b <= n:
        while n % b == 0:
            n //= b
            ret[b]+=1
        b+=1
    if n > 1:ret[n]+=1
    return ret
def inputint(row=1): 
    if row==1:
        ret=list(map(int,input().split()))
        if len(ret)==1:ret=ret[0]
    else:
        ret=[None]*row
        for a in range(row):
            ret[a]=int(input())
    return ret
def inputfloat(row=1): 
    if row==1:
        ret=list(map(float,input().split()))
        if len(ret)==1:ret=ret[0]
    else:
        ret=[None]*row
        for a in range(row):
            ret[a]=float(input())
    return ret
def istanchouzouka(A):
    f=True
    prev=min(A)-1
    for a in A:
        if prev>=a:
            f=False
        prev=a
    return f
def istanchouhigensyou(A):
    f=True
    prev=min(A)-1
    for a in A:
        if prev>a:
            f=False
        prev=a
    return f
def getallsum(A):
    s=sum(A)
    dp=defaultdict(int)
    dp[0]=1
    for a in range(0,len(A)):
        for b in range(s,-1,-1):
            if b-A[a]>=0:
                dp[b]+=dp[b-A[a]]
    return dp
def yuukoutomukou(edges):
    ret=deepcopy(edges)
    for edge in edges:
        if not edge.x==edge.y:
            ret.append(Edge(edge.y,edge.x,edge.val))
    return ret
def dijkstra(edges,V,start):
    mincost=[inf]*V
    G=[[] for a in range(V)]
    for edge in edges:
        G[edge.x].append([edge.val,edge.y])
    Q=PriorityQueue()
    Q.put([0,start])#[cost,x]
    while not Q.empty():
        nowcost,nowx=Q.get()
        if mincost[nowx]==inf:
            mincost[nowx]=nowcost
            for cost,y in G[nowx]:
                newcost=nowcost+cost
                Q.put([newcost,y])
    return mincost
def warshallfloyd(edges,V):
    mincost=[[inf for b in range(V)] for a in range(V)]
    for a in range(V):mincost[a][a]=0
    for edge in edges:
        mincost[edge.x][edge.y]=min(mincost[edge.x][edge.y],edge.val)#x→yが複数ある場合のためにmin()する
    for k in range(V):
        for s in range(V):
            for t in range(V):
                if mincost[s][k]==inf or mincost[k][t]==inf:continue
                mincost[s][t]=min(mincost[s][t],mincost[s][k]+mincost[k][t])
    return mincost
def bellemanford(edges,V,start):
    mincost=[inf]*V
    mincost[start]=0
    for _ in range(V):
        for edge in edges:
            if mincost[edge.x]==inf:continue
            mincost[edge.y]=min(mincost[edge.y],mincost[edge.x]+edge.val)
    return mincost
def getmd(x1,y1,x2,y2):return abs(x1-x2)+abs(y1-y2)
def geted(x1,y1,x2,y2):return math.sqrt((x1-x2)**2+(y1-y2)**2)
class fordfulkerson():
    def __init__(self,edges,V,s,t):
        self.V=V
        self.used=[False]*V
        self.G=[[] for a in range(V)]
        self.s=s
        self.t=t
        for edge in edges:
            self.G[edge.x].append({"x":edge.x,"y":edge.y,"cap":edge.val,"rev":len(self.G[edge.y])})
            self.G[edge.y].append({"x":edge.y,"y":edge.x,"cap":0,"rev":len(self.G[edge.x])-1})
    def dfs(self,v,t,f=inf):
        if v==t:return f
        self.used[v]=True
        for a in range(len(self.G[v])):
            x=self.G[v][a]["x"]
            y=self.G[v][a]["y"]
            cap=self.G[v][a]["cap"]
            rev=self.G[y][self.G[x][a]["rev"]]
            if self.used[y] or cap==0:continue
            f2=self.dfs(y,t,min(f,cap))
            if f2>0:
                self.G[v][a]["cap"]-=f2
                rev["cap"]+=f2
                return f2
        return 0
    def maxflow(self):
        flow=0
        while True:
            self.used=[False]*V
            zouka=self.dfs(self.s,self.t)
            if zouka==0:break
            flow+=zouka
        return flow
def zipsort(*args):
    ziplist=sorted(zip(*args))
    for a in range(len(args)):
        for b in range(len(ziplist)):
            args[a][b]=ziplist[b][a]
def zipreverse(*args):
    ziplist=list(zip(*args))
    rev=[]
    for a in range(len(ziplist)-1,-1,-1):
        rev.append(ziplist[a])
    for a in range(len(args)):
        for b in range(len(rev)):
            args[a][b]=rev[b][a]

######################################################################################################
N,M,K=inputint()
A=inputint()
B=inputint()
RA=[0]*(N+1)
RB=[0]*(M+1)
for a in range(N):
    RA[a+1]=RA[a]+A[a]
for a in range(M):
    RB[a+1]=RB[a]+B[a]

ans=0
for a in range(0,N+1):
    n=RA[a]
    if K<n:break
    k=K-n
    i=bisect.bisect_right(RB,k)-1
    ans=max(ans,a+i)
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()))

sa=[0]*n
sb=[0]*m
s=0
for i in range(n):
  s+=a[i]
  sa[i]=s
s=0
for i in range(m):
  s+=b[i]
  sb[i]=s

if sa[n-1]+sb[m-1]<=k:
  print(n+m)
  exit()
  
if m<n:
  ans=n if sa[n-1]<=k else 0
else:
  ans=m if sb[m-1]<=k else 0

j=0
for i in reversed(range(n)):
  if sa[i]+sb[j]>k:
    continue
  while j<m and sa[i]+sb[j+1] <= k:
    j+=1
  if sa[i]+sb[j] <= k and ans<i+j+2:
    ans=i+j+2

print(ans)

Submission status: Runtime Error

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

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

time = 0
cnt = 0
ans = 0
ai = -1
bi = -1

def dfs(time, cnt):
    global ans, ai, bi
    # print(time)
    if time > K:
        # print(cnt)
        ans = max(ans, cnt-1)
        return
    if ai <= N-2:
        ai += 1
        dfs(time+A[ai], cnt+1)
    if bi+1 <= M-2:
        bi += 1
        dfs(time+B[bi]+1, cnt+1)

if sum(A)+sum(B)<=K:
    print(N+M)
else:
    dfs(0, 0)
    print(ans)

Submission status: Runtime Error

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

a.append( 10 ** 9 + 1 )
b.append( 10 ** 9 + 1 )

i = 0
j = 0
time = 0
read = 0

def dp( i, j, t ):
    if i == n and j == m:
        return 0
    elif a[ i ] + t > k and b[ j ] + t > k:
        return 0
    elif a[ i ] + t > k:
        return dp( i, j+1, t+b[j] ) + 1
    elif b[ j ] + t > k:
        return dp( i+1, j, t+a[i] ) + 1
    else:
        return max( dp( i+1, j, t+a[i] ), dp( i, j+1, t+b[j] ) ) + 1

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

Submission status: Runtime Error

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

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

time = 0
count = 0
i = 0
j = 0
for _ in range(n + m):
    if a[i] < b[j]:
        time += a[i]
        i +=1
    elif a[i] > b[j]:
        time += b[j]
        j +=1
    else:
        time += a[i]
        if a[i + 1] < b[j+1]:
            i += 1
        else:
            j += 1
    if time > k:
        break
    else:
        count += 1
print(count)

Submission status: Runtime Error

Solution code:
def read(a,b,k):
    book,i,j = 0,0,0,
    B = len(b)
    A = len(a)
    condition = A+B
    while condition>0:
        if k<a[i] and k<b[i]:
            break
        if a[i]<b[j] and A>0:
            k-=a[i]
            book+=1
            A-=1
            if i<len(a)-1:
                i+=1
        elif b[j]<a[i] and B>0:
            k-=b[j]
            book+=1
            B-=1
            if j<len(b)-1:
                j+=1
        else:
            if k>=a[i] and A>0:
                k-=a[i]
                book+=1
                A-=1
                if i<len(a)-1:
                    i+=1
            if k>=b[j] and B>0:
                k-=b[j]
                book+=1
                B-=1
                if j<len(b)-1:
                    j+=1
        condition -= 1
        
    return book


if __name__ == "__main__":
    n,m,k = map(int,input().split())
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))
    print(read(a,b,k))

Submission status: Runtime Error

Solution code:

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

x=0
y=0

for t in range(N+M):
  if A[x]>=B[y]:
      C=K-int(A[x])
      x+=1
  else:
      C=K-int(B[y])
      y+=1

  if K<0:
      break
  else:
      K=K-C

print(x+y-1)

Submission status: Runtime Error

Solution code:
n,m,k = list(map(int, input().strip().split()))
A = list(map(int, input().strip().split()))
B = list(map(int, input().strip().split()))
x=0
t=0
s=0
a=0
b=0
r=0
if n>m:
    r=m
else:
    r=n
q=0
for i in range (r):
    q+=A[i]+B[i]
for i in range (n):
    s+=A[i]
    a+=A[i]
for i in range(m):
    s+=B[i]
    b+=B[i]
if n==m:
    if s<=k:
        print(n+m)
    else:
        if min(A[0],B[0])==k:
            print(1)
        elif max(A)<=min(B):
            for i in range(n):
                a+=B[i]
                if a>k:
                    print(n+i)
                    break
        elif min(A)>=max(B):
            for i in range(n):
                b+=a[i]
                if b>k:
                    print(n+i)
                    break
        else:
            for i in range (n):
                t=t+1
                if A==[]:
                    x+=B[0]
                elif B==[]:
                    x+=A[0]
                else:    
                    x+=min(A[0],B[0])
                    if A[0]<=B[0]:
                        A.pop(0)
                    else:
                        B.pop(0)
                if x>k:
                    print(t-1)
                    break
        
else:
    if s<=k:
        print(n+m)
    elif q<k:
        if r==n:
            for i in range(r,m+1):
                q+=B[i]
                if q>k:
                    print(i+r)
                    break
        elif r==m:
            for i in range(r,n+1):
                q+=A[i]
                if q>k:
                    print(i+r)
                    break
    else:
        for i in range (max(n,m)):
            t=t+1
            if A==[]:
                x+=B[0]
            elif B==[]:
                x+=A[0]
            else:    
                x+=min(A[0],B[0])
                if A[0]<=B[0]:
                    A.pop(0)
                else:
                    B.pop(0)
            if x>k:
                print(t-1)
                break

Submission status: Runtime Error

Solution code:
m,n,k =map(int,input().split())
ml= []
for i in range(m):
  el=int(input())
  ml.append(el)
nl=[]
for i in range(N):
  ele=int(input())
  nl.append(ele)
tl=ml+nl
tl.sort()
sum=0
count=0
for i in range(len(t1)):
  if sum+t[i]<k:
    sum=sum+t[i]
    count+=1
print(count)

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
for i in range(0,len(n)):
    A.append(int(input()))
for i in range(0,len(m)):
    B.append(int(input()))
while time !> k:
    if (i> -1 and j> -1 and A[i]<B[j]) or (i>-1 and !(j>-1)):
        time = time+A[i]
        i=i-1
        books=books+1
    elif (i> -1 and j> -1 and !(A[i]<B[j])) or (j>-1 and !(i>-1)):
            time = time+A[i]
            i=i-1
            books=books+1
print(books)

Submission status: Runtime Error

Solution code:
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
time_of_A = [A[0]]
for i in range(1, N):
  time_of_A.append(time_of_A[i-1]+A[i])
B = list(map(int, input().split()))
time_of_B = [B[0]]
for i in range(1, M):
  time_of_B.append(time_of_B[i-1]+B[i])

book = []
for i in range(N):
  if time_of_A[i] <= K:
    book.append(i+1)

start = M-1
for i in range(N):
  j = start
  time = 10 ** 9 + 1
  while time > K and j >= 0:
    time = time_of_A[i] + time_of_B[j]
    j -= 1
  if time <= K:
    book.append(i+j+3)
    j +=1
    start = j
  else:
    break
print(max(book))

Submission status: Runtime Error

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


ans = 0
sum_a = 0
for i in range(len(a)):
  sum_a += a[i]
  sum_b = 0
  if sum_a > k:
    break
  if sum_a == k:
    temp = i+1
    if temp > ans:
      ans = temp
    break
  for j in range(len(b)):
    sum_b += b[j]
    if (sum_a + sum_b) > k:
      temp = i+1+j
      ans_list.append(temp)
      if temp > ans:
        ans = temp
      break
    if (sum_a + sum_b) == k:
      temp = i+1+j+1
      if temp > ans:
        ans = temp
      break
      
sum_b = 0
for i in range(len(b)):
  sum_b += b[i]
  sum_a = 0
  if sum_b > k:
    break
  if sum_b == k:
    temp = i+1
    if temp > ans:
      ans = temp
    break
  for j in range(len(a)):
    sum_a += a[j]
    if (sum_b + sum_a) > k:
      temp = i+1+j
      if temp > ans:
        ans = temp
      break
    if (sum_b + sum_a) == k:
      temp = i+1+j+1
      if temp > ans:
        ans = temp
      break
      
print(ans)

Submission status: Runtime Error

Solution code:
n, m, k = map((lambda x: int(x)), input().split())
a_list = list(map((lambda x: int(x)), input().split()))
b_list = list(map((lambda x: int(x)), input().split()))
read_cnt = 0
a_cnt = 0
b_cnt = 0
a_list.append(200001)
b_list.append(200001)
while k > 0:
    if a_list[a_cnt] > b_list[b_cnt]:        
#         pdb.set_trace()
        k -= b_list[b_cnt]
        b_cnt += 1
        read_cnt += 1
    elif a_list[a_cnt] == b_list[b_cnt]:
        if a_list[a_cnt] == 200001:
            break
        else:
            compare_list(a_list, b_list, a_cnt, b_cnt)
    else:
        k -= a_list[a_cnt]
        a_cnt += 1
        read_cnt += 1

print(read_cnt)        
        
def compare_list(a, b, dummy_a_cnt, dummy_b_cnt):
    while a_list[dummy_a_cnt] == b_list[dummy_b_cnt]:
        dummy_a_cnt += 1
        dummy_b_cnt += 1
    if a_list[dummy_a_cnt] > b_list[dummy_b_cnt]:
        k -= b_list[b_cnt]
        b_cnt += 1
        read_cnt += 1
    else:
        k -= a_list[a_cnt]
        a_cnt += 1
        read_cnt += 1

Submission status: Runtime Error

Solution code:
from collections import *
import sys
from bisect import *
input=sys.stdin.readline
from math import *
t=1
while(t):
    t-=1
    n,m,k=map(int,input().split())
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    prea=[a[0]]
    preb=[b[0]]
    for i in range(1,n):
        prea.append(prea[-1]+a[i])
    for j in range(1,m):
        preb.append(preb[-1]+b[i])
    r=0
    for i in range(n):
        if(k-prea[i]<0):
            break
        re=k-prea[i]
        rr=bisect_right(preb,re)
        r=max(r,i+1+rr)
    for i in range(m):
        if(k-preb[i]<0):
            break
        re=k-preb[i]
        rr=bisect_right(prea,re)
        r=max(r,i+1+rr)
    print(r)

Submission status: Runtime Error

Solution code:
import collections.deque

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

sum_minute = 0
ans = 0
while (sum_minute <= k) and (a or b):
    if a and b:
        if sum_minute + min(a[0], b[0]) <= k:
            if a[0] <= b[0]:
                r = a.pop(0)
                sum_minute += r
                ans += 1
            else:
                r = b.pop(0)
                sum_minute += r
                ans += 1
        else:
            break
    elif not a:
        if sum_minute + b[0] <= k:
            r = b.pop(0)
            sum_minute += r
            ans += 1
        else:
            break
    elif not b:
        if sum_minute + a[0] <= k:
            r = a.pop(0)
            sum_minute += r
            ans += 1
        else:
            break

print(ans)

Submission status: Runtime Error

Solution code:
import sys
# from math import sqrt, gcd, ceil, log
from bisect import bisect
from collections import defaultdict, Counter
inp = sys.stdin.readline
read = lambda: list(map(int, inp().strip().split()))

# sys.setrecursionlimit(10**6)

# def func(arr, b):
# 	return([i-b for i in arr])

# def f(i, j, t):
# 	if t >=k:
# 		return(i+j)
# 	return(max(f(i+1, j, t+a_[i+1]), f(i, j+1, t+b_[j+1])))



def solve():
	a, b, k = read(); a_ = read(); b_ = read()
	dic = {}
	def f(i, j, t):
		# print(i, j, t, "******")
		key = (i, j, t)
		if key in dic:
			return(dic[key])
		if t == k:
			# print(i, j, t, "11111")
			dic[key] = i+j
			return(dic[key])
		if t > k:
			# print(i, j, t, "22222")
			dic[key] = i+j-1
			return(dic[key])
		if i ==a and j ==b:
			# print(i, j, t, "0"*5)
			dic[key] = i+j
			return(dic[key])
		if i == a:
			dic[key] = f(i, j+1, t + b_[j])
			return(dic[key])
		if j == b:
			dic[key] = f(i+1, j, t + a_[i])
			return(dic[key])
		

		x = f(i+1, j, t+a_[i]) if i < a else 0#f(i+1, j, t)
		y = f(i, j+1, t+b_[j]) if j < b else 0#f(i, j+1, t)
		dic[key] = max(x, y)
		return(dic[key])
	# for i in range(1, a): a_[i] += a_[i-1]
	# for i in range(1, b): b_[i] += b_[i-1]
	# ans = 0
	# while k > 0:
	# 	inda = bisect(a_, k); indb = bisect(b_, k)
	# 	# print(k, inda, indb, a_[inda-1], b_[indb-1])
	# 	if inda > indb:
	# 		k -= a_[inda-1];
	# 		ans += inda
	# 		a_ = func(a_[inda:], a_[inda-1])
	# 	elif inda == indb:
	# 		if a_[inda-1] < b_[indb-1]:
	# 			k -= a_[inda-1];
	# 			ans += inda
	# 			a_ = func(a_[inda:], a_[inda-1])
	# 		else:
	# 			k -= b_[indb-1];
	# 			ans += indb
	# 			b_ = func(b_[indb:], b_[indb-1])
	# 	else:
	# 		k -= b_[indb-1];
	# 		ans += indb
	# 		b_ = func(b_[indb:], b_[indb-1])
	ans = f(0, 0, 0)
	print(ans)





		

if __name__ == "__main__":
	solve()

Submission status: Runtime Error

Solution code:
N, M, K = map(int,input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
time = 0
books = 0
A.append(0)
B.append(0)

while time <= K:
    if A[0] < B[0]:
        if A[0] == 0:
            time += B.pop(0)
        
        time += A.pop(0)
    elif B[0] < A[0]:
        if B[0] == 0:
            time += A.pop(0)
        
        time += B.pop(0)
    elif B[0] == A[0]:
        break
    books += 1
if books == 0:
    print(0)
else:
    print(books - 1)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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

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

count = [[0 for i in range(n + 1)] for j in range(m + 1)]

answer = 0
for i in range(n+2):
    for j in range(m):
        if j != 0:
            count[i][j] = count[i][j-1] + a[j-1]
        if count[i][j] > k:
            break
        if answer < i + j and count[i][j] <= k:
            answer = i + j
    if i == n + 1:
        break
    count[i+1][0] = count[i][0] + b[i]
    if count[i+1][j] > k:
        break 

print(answer)

Submission status: Runtime Error

Solution code:
import sys, os, math, bisect, itertools, collections, heapq, queue
# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from decimal import Decimal
from collections import defaultdict, deque

# import fractions

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)
# lcm = lambda x, y: (x * y) // fractions.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N, M, K = il()
    A = il()
    SA = [0] * (N + 1)
    for n in range(N):
        SA[n + 1] = A[n] + SA[n]
    B = il()
    SB = [0] * (M + 1)
    for m in range(M):
        SB[m + 1] = B[m] + SA[m]

    ret = 0
    for n in range(N+1):
        for m in range(M+1):
            if SA[n] + SB[m] <= K:
                ret = max(ret, n + m)
    print(ret)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
# C

import itertools
import bisect

n, m, k = map(int, input().split())
a_l = [int(x) for x in input().split()]
b_l = [int(x) for x in input().split()]

cumsuma_l = list(itertools.accumulate(a_l)) 
cumsumb_l = list(itertools.accumulate(b_l))

_idx = bisect.bisect_left(cumsumb_l, k)
if _idx == m or cumsumb_l[_idx] != _tmp:
    _idx -= 1
ans = _idx + 1
    
for i in range(n):
    _a = cumsuma_l[i]
    if _a > k:
        break
    _tmp = k - _a
    _idx = bisect.bisect_left(cumsumb_l, _tmp)
    if _idx == m or cumsumb_l[_idx] != _tmp:
        _idx -= 1
    ans = max(ans, i + _idx + 2)
        
print(ans)

Submission status: Runtime Error

Solution code:
#!python3

import sys
iim = lambda: map(int, sys.stdin.readline().rstrip().split())
from bisect import bisect


def resolve():
    it = map(int, sys.stdin.read().split())
    N, M, K = next(it), next(it), next(it)

    A = [0] * (N+1)
    B = [0] * (M+1)
    ans = 0
    for i in range(1, N+1):
        A[i] = A[i-1] + next(it)
    for i in range(1, M+1):
        B[i] = B[i-1] + next(it)

    NM = N + M
    for i in range(1, N+1):
        for j in range(i+1):
            if A[j] + B[i-j] <= K:
                ans = i
                break
        else:
            break
    k = K - A[N]
    if k > 0:
        i = bisect(B, K-A[N])
        ans = max(ans, i + N -1)

    print(ans)


if __name__ == "__main__":
    resolve()

Submission status: Runtime Error

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


def fun(As,Bs,time_rem):
    if time_rem < 0:
        return -1
    if As == [] and Bs == []:
        return 0
    elif As == []:
        B = Bs.pop()
        return 1 + fun([],Bs,time_rem-B)
    elif Bs == []:
        A = As.pop()
        return 1 + fun(As,[],time_rem-A)
    else:
        A = As.pop()
        B = Bs.pop()
        return 1 + max(fun(As,Bs+[B],time_rem-A),fun(As+[A],Bs,time_rem-B))


As.reverse()
Bs.reverse()
print(fun(As,Bs,k))

Submission status: Runtime Error

Solution code:
import bisect

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

a_sum = []
for i in range(len(a)):
  a_sum.append(sum(a[:i+1]))
b_sum = []
for j in range(len(b)):
  b_sum.append(sum(b[:j+1]))

if a_sum[0] > k and b_sum[0] > 0:
  print(0)
  exit()

iMax = bisect.bisect(a_sum, k)

ans = 0
for i in range(iMax+1):
  tmp = i + 1 + bisect.bisect(b_sum, k - a_sum[i])
  ans = max(ans, tmp)

print(ans)

Submission status: Runtime Error

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

a = [int(w) for w in input().split()]
b = [int(w) for w in input().split()]

ans = 0

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

elif k >= sum(a)+sum(b):
    print(n+m)
else:
    t = 0
    i = 0
    j = 0
    z = min(n, m)
    while(k > 0 and (i < n or j < m)):
        if i < n and (a[i] < b[j]):
                k -= a[i]
                i += 1
        
        elif j < m:
            k -= b[j]
            j += 1
            
        if k>=0:
            ans+=1
    
    
    print(ans)

Submission status: Runtime Error

Solution code:
def read(a,b,k):
    book,i,j = 0,0,0,
    B = len(b)
    A = len(a)
    condition = max(A,B)
    while condition>0:
        if k<a[i] and k<b[i]:
            break
        if a[i]<b[j] and A>0:
            k-=a[i]
            book+=1
            A-=1
            if i<len(a)-1:
                i+=1
        elif b[j]<a[i] and B>0:
            k-=b[j]
            book+=1
            B-=1
            if j<len(b)-1:
                j+=1
        else:
            if k>=a[i] and A>0:
                k-=a[i]
                book+=1
                A-=1
                if i<len(a)-1:
                    i+=1
            if k>=b[j] and B>0:
                k-=b[j]
                book+=1
                B-=1
                if j<len(b)-1:
                    j+=1
        condition -= 1
        
    return book


if __name__ == "__main__":
    n,m,k = map(int,input().split())
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))
    print(read(a,b,k))