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:
from collections import deque

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

t = 0
res = 0
while a or b:
    if not a:
        if t + b[0] > k:
            break
        else:
            t += b.popleft()
    elif not b:
        if t + a[0] > k:
            break
        else:
            t += a.popleft()
    else:
        if t + a[0] > k and t + b[0] > k:
            break
        if a[0] < b[0]:
            t += a.popleft()
        elif a[0] > b[0]:
            t += b.popleft()
        else:
            if a[1] < b[1]:
                t += a.popleft()
            else:
                t += b.popleft()
    res += 1
print(res)

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:
  min_time = B_Times[0]
  if A_Times[0]<B_Times[0]:
    min_time = A_Times[0]
    del A_Times[0]
  else: del B_Times[0]
  if total_time+min_time<=K:
    total_time += min_time
    num_books+=1
print(num_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()))
count = 0
while True:
  if a != [] and a[0] <= b[0]:
    k -= a[0]
    a.pop(0)
  elif b !=[]:
    k -= b[0]
    b.pop(0)
  else:
    break
  if k < 0:
    break
  count += 1
print(count)

Submission status: Runtime Error

Solution code:
def iteraccumulate(l):
    from itertools import accumulate
    return list(accumulate(l))


def resolve():
    from bisect import bisect_right
    n, m, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    aa = [0] + iteraccumulate(a)
    bb = iteraccumulate(b)
    lbb = len(bb)
    ans = 0
    for index, i in enumerate(aa):
        j = bisect_right(bb, k - i)
        if i + bb[j - 2] <= k:
            ans = max(ans, index + j - 1)
        if i + bb[j - 1] <= k:
            ans = max(ans, index + j)
        if j < lbb and i + bb[j] <= k:
            ans = max(ans, index + j + 1)
    print(ans)


if __name__ == '__main__':
    resolve()

Submission status: Runtime Error

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

ans = 0

def dfs(a, b, k, ans):
  
  if k<=0 or (not a and not b):
    return ans
  
  ans1, ans2 = ans, ans
  if a:
    next_book = a[0]
    if k - next_book >= 0:
      ans1 = dfs(a[1:], b, k - next_book, ans+1)
  if b:
    next_book = b[0]
    if k - next_book >= 0:
      ans2 = dfs(a, b[1:], k - next_book, ans+1)

  return max(ans1, ans2)

print(dfs(a, b, k, 0))

Submission status: Runtime Error

Solution code:
from collections import deque
N, M, K = map(int, input().split())
*A, = map(int, input().split())
*B, = map(int, input().split())
a = deque(A)
b = deque(B)
ans = 0
l = len(a) + len(b)
while a and b:
    if K - a[0] >= 0 or K - b[0] >= 0:
        if a[0] > b[0]:
            x = b.popleft()
            K = K - x
            ans += 1
        if a[0] <= b[0]:
            x = a.popleft()
            K = K - x
            ans += 1
    else:
        break
C = list(a) + list(b)
c = deque(C)
while c:
    if K - c[0] >= 0:
        x = c.popleft()
        K = K - x
        ans += 1
    else:
        break
print(ans)

Submission status: Runtime Error

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

run_time = K
count_A = 0
count_B = 0
while(run_time > 0):
	if(A[count_A] >= B[count_B]):
		run_time -= A[count_A]
		count_A += 1
	elif(A[count_A] < B[count_B]):
		run_time -= B[count_B]
		count_B += 1
print(count_A + count_B)

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
count=0
i,j=0,0
while ans<k:
  if i > n-1:
    num=b[j]
  if j > m-1:
    num=a[i]
  else:
    num=min(a[i],b[j])
    if num==a[i]:
      i+=1
      count+=1
      ans+=num
    else:
      j+=1
      count+=1
      ans+=num
  
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()))

akari = [0]*(n+1)
bkari = [0]*(m+1)
for i in range(n):
	akari[i+1] = akari[i] + a[i]

for i in range(m):
	bkari[i+1] = bkari[i] + b[i]

def bin(ls,s,e,sikii):
	if e - s <= 1:
		return s
	mid = (s+e)//2
	if sikii < ls[mid]:return bin(ls,s,mid,sikii)
	else:return bin(ls,mid,e,sikii)

ans = 0
for i in range(n+1):
	time = akari[i]
	if k - time <= 0:break
	ref = bin(bkari,0,m+2,k-time)
	ans = max(ans,ref+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()))

count = 0
now_a = 0
now_b = 0
while k > 0 and a[now_a] < k or b[now_b] < k:
  if a[0] > b[0] and now_a > len(a) or now_b < len(b):
    k -= b[now_b]
    now_b += 1
  else:
    k -= a[now_a]
    now_a += 1
  count += 1
  
print (count)

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

# 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()
    B = il()
    aq, bq = 0, 0
    time, ret = 0, 0

    for _ in range(N+M):
        if aq < N and A[aq] < B[bq] and A[aq] + time <= K:
            time += A[aq]
            ret += 1
            aq += 1
        elif bq < M and B[bq] + time <= K:
            time += B[bq]
            ret += 1
            bq += 1
        else:
            break
    print(ret)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
N,M,K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
c = 0
while K >=0:
    c +=1
    if not A:
        K -= B.pop(0)
    elif not B:
        K -= A.pop(0)
    elif A[0] >=B[0]:
        K -= B.pop(0)
    else:
        K -= A.pop(0)
if K == 0:
    print(c)
else:
    print(c-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()))
ans = 0
aa = 0
bb = 0
while True:
    if aa >= n and bb >= m:
        break
    elif aa >= n:
        k -= b[bb]
        bb += 1
    elif bb >= m:
        k -= a[aa]
        aa += 1
    else:        
        if a[aa] > b[bb]:
            k -= b[bb]
            bb += 1
        elif a[aa] < b[bb]:
            k -= a[aa]
            aa += 1
        else:
          if a[aa+1] > b[bb+1]:
            k-= b[bb]
            bb += 1
          else:
            k -= a[aa]
            aa += 1
    if k < 0:
      break

if k < 0:
	print(aa+bb-1)
else:
    print(aa+bb)

Submission status: Runtime Error

Solution code:
a=input().split(" ")
b=input().split(" ")
c=input().split(" ")
d=0
e=0
f=0
count=0
while int(a[2])>=e:
    if d<3:
        e=e+int(b[d])
        d=d+1
        count+=1
    elif f<4 and d>=3:
        e=e+int(c[f])
        f=f+1
        count+=1
    else:
        break

print(count)

Submission status: Runtime Error

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

def problem():
    min0 = 0
    ans = 0
    while k >= min0:
        if (len(a) == 0) & (len(b) ==0):
            return ans
        if (len(a) == 0) & (len(b) != 0):
            min0 += b[0]
            ans += 1
            del b[0]
        if (len(b) == 0) & (len(a) != 0):
            min0 += a[0]
            ans += 1
            del a[0]
        if (len(a) != 0) & (len(b) !=0):
            if a[0] >= b[0]:
                min0 += b[0]
                ans += 1
                del b[0]
            if a[0] < b[0]:
                min0 += a[0]
                ans += 1
                del a[0]
        if k < min0:
            return ans-1
    return ans

print(problem())

Submission status: Runtime Error

Solution code:
str = input()
lst = str.split(' ')
n,m,k = lst
n = int(n)
m = int(m)
k = int(k)
str1 = input()
str2 = input()
a = str1.split(' ')
b = str2.split(' ')
i=0
j=0
count = 0
while c<=k :
  
  
  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])
  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])
print(count)

Submission status: Runtime Error

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

    sum_a = []
    sum_b = []

    cnt = 0

    if a[0] > k and b[0] > 0 :
        print(0)
        return None

    if sum(a) + sum (b) <= k :
        print(len(a)+len(b))
        return None

    while k >= 0 :
        if a[0] >= b[0] :
            k -= a[0]
            a = a[1::]
            cnt += 1

        else :
            k -= b[0]
            b = b[1::]
            cnt +=1

    print(cnt)

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

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

print(ans)

Submission status: Runtime Error

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

idxA = idxB = 0
readTime :int = 0
readBooks :int = 0
while readTime < k:
    #if arA[idxA] <= arA[idxB] and idxA < n:
    if idxA < n and arA[idxA] <= arA[idxB]:
        if k < readTime + arA[idxA]:
            break
        readTime += arA[idxA]
        idxA += 1
    else:
        if k < readTime + arB[idxB]:
            break
        readTime += arB[idxB]
        idxB += 1
    readBooks += 1
    if idxA >= n and idxB >= m:
        break

print(readBooks)

Submission status: Runtime Error

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

books   = 0
minutes = 0

while True:
    if len(ai) != 0 and len(bi) != 0:
        if ai[0] <= bi[0]:
            if minutes + ai[0] > k: break

            books += 1
            minutes += ai[0]
            ai = ai[1:]

        else:
            if minutes + bi[0] > k: break

            books += 1
            minutes += bi[0]
            bi = bi[1:]

    else:
        if len(ai) == 0:
            if (minutes + bi[0]) > k: break

            books += 1
            minutes += bi[0]
            bi = bi[1:]

        elif len(bi) == 0:
            if (minutes + ai[0]) > k: break

            books += 1
            minutes += ai[0]
            ai = ai[1:]

        else:
            break

print(books)

Submission status: Runtime Error

Solution code:

n, m, k = (int(x) for x in input().split())

n_list = list(map(int, input().split()))
m_list = list(map(int, input().split()))

count = 0

# 全て読み切れる
if(sum(n_list + m_list) <= k) :
    count = n + m
# どこかで時間切れになる
else :
    for i in range(n + m):

        if 0 <= k - n_list[i] :
            k = k - n_list[i]
            count = count + 1
        else :
            break

        if 0 <= k - m_list[i] :
            k = k - m_list[i]
            count = 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()))

count = 0
sum = 0
while 1:
    if a[0] > b[0]:
        tmp = b.pop(0)
    elif a[0] < b[0]:
        tmp = a.pop(0)
    if sum + tmp > k:
        break
    sum += tmp
    count += 1
print(count)

Submission status: Runtime Error

Solution code:
from collections import deque

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

A = list(map(int, input().split(' ')))
B = list(map(int, input().split(' ')))

A = deque(A[::-1])
B = deque(B[::-1])

count = 0

while K > 0:
    if len(A) == 0 and len(B) == 0:
        break
    if len(A) == 0:
        K = K - B.pop()
    elif len(B) == 0:
        K = K - A.pop()
    elif A[-1] > B[-1]:
        K = K - B.pop()
    elif A[-1] < B[-1]:
        K = K - A.pop()
    elif A[-1] == B[-1]:
        if A[-2] > B[-2]:
            K = K - B.pop()
        else:
            K = K - A.pop()
    count += 1
    if K < 0:
        count -= 1

print(count)

Submission status: Runtime Error

Solution code:
import sys
from functools import lru_cache

sys.setrecursionlimit(10 ** 5)


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


@lru_cache(None)
def dp(i, j, cur):
    if cur > K:
        return 0
    elif i >= n:
        if j < m and cur + B[j] <= K:
            return dp(i, j + 1, cur + B[j]) + 1
        else:
            return 0
    elif j >= m:
        if i < n and cur + A[i] <= K:
            return dp(i + 1, j, cur + A[i]) + 1
        else:
            return 0
    else:
        res = float('-inf')
        if cur + A[i] <= K:
            res = max(1 + dp(i + 1, j, cur + A[i]), res)
        if cur + B[j] <= K:
            res = max(1 + dp(i, j + 1, cur + B[j]), res)
        if res == float('-inf'):
            return 0
        else:
            return res


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

cnt = 0
def dfs(a, b, t):
    global cnt
    cnt = max(cnt, a+b)
    if a < N and t + A[a] <= K:
        dfs(a+1, b, t+A[a])
    if b < M and t + B[b] <= K:
        dfs(b+1, a, t+B[b])
    
dfs(0, 0, 0)
print(cnt)

Submission status: Runtime Error

Solution code:
from collections import deque
N, M, K = map(int, input().split())
*A, = map(int, input().split())
*B, = map(int, input().split())
a = deque(A)
b = deque(B)
ans = 0
l = len(a) + len(b)
while a and b:
    if K - a[0] >= 0 or K - b[0] >= 0:
        if a[0] > b[0]:
            x = b.popleft()
            K = K - x
            ans += 1
        if a[0] <= b[0]:
            x = a.popleft()
            K = K - x
            ans += 1
    else:
        break
c = a + b
while c:
    if K - c[0] >= 0:
        x = c.popleft()
        K = K - x
        ans += 1
    else:
        break
print(ans)

Submission status: Runtime Error

Solution code:
import sys, bisect, math, itertools, heapq, collections
from operator import itemgetter
# a.sort(key=itemgetter(i)) # i番目要素でsort
from functools import lru_cache
# @lru_cache(maxsize=None)
sys.setrecursionlimit(10**8)
input = sys.stdin.readline
INF = float('inf')
mod = 10**9 + 7
eps = 10**-7


def inp():
    '''
    一つの整数
    '''
    return int(input())


def inpl():
    '''
    一行に複数の整数
    '''
    return list(map(int, input().split()))


def str_inp():
    '''
    文字列をリストとして読み込む
    '''
    return list(input()[:-1])


n, m, k = inpl()
a = inpl()
b = inpl()
tmp = 0
for i in range(len(a)):
    tmp += a[i]
    a[i] = tmp
tmp = 0
for i in range(len(b)):
    tmp += b[i]
    b[i] = tmp
ans = 0
a_start = bisect.bisect_right(a, k)
if a_amount==0:
    print(bisect.bisect_right(b, k))
for a_amount in range(a_start, 0, -1):
    new_k = k - a[a_amount - 1]
    b_amount = bisect.bisect_right(b, new_k)
    ans = max(ans, a_amount + b_amount)
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()))

total = 0
cur = 0
len_A = len(A)
len_B = len(B)
i = 0
j = 0
while True:
  a_i = A[i] if i < len_A else 1e10
  b_j = B[j] if j < len_B else 1e10
  if a_i == 1e10 and b_i == 1e10:
    break
  if a_i < b_j:
    cur += a_i
    i += 1
  else:
    cur += b_j
    j += 1
  if cur <= K:
    total += 1
  else:
    break
    
print(total)

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
n_a = 0
n_b = 0

while True:
    if n_a < n and n_b < m:
        if a[n_a] < b[n_b]:
            k = k-a[n_a]
            if k >= 0:
                n_a = n_a + 1
                cnt = cnt + 1
            else:
                print(cnt)
                exit()
        else:
            k = k - b[n_b]
            if k >= 0:
                n_b = n_b + 1
                cnt = cnt + 1
            else:
                print(cnt)
                exit()
    elif n_a < n:
        k = k - a[n_b]
        if k >= 0:
            n_a = n_a + 1
            cnt = cnt + 1
        else:
            print(cnt)
            exit()
    elif n_b < m:
        k = k - b[n_b]
        if k >= 0:
            n_b = n_b + 1
            cnt = cnt + 1
        else:
            print(cnt)
            exit()
    else:
        print(cnt)
        exit()

Submission status: Runtime Error

Solution code:
n,m,k = input().split()
N = int(n)
M = int(m)
K = int(k)
A_Times = list()
B_Times = list()
for i in range(N):
  A_Times.append(int(input()))
for i in range(M):
  B_Times.append(int(input()))
total_time = 0
num_books = 0
while total_time<=K:
  min_time = B_Times[0]
  if A_Times[0]<B_Times[0]:
    min_time = A_Times[0]
    del A_Times[0]
  else: del B_Times[0]
  if total_time+min_time<=K:
    total_time += min_time
    num_books+=1
print(num_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()))

A_p=0
B_p=0
read=0
for i in range(N+M):
	if A_p<N and A[A_p]<=B[B_p] :
		K-=A[A_p]
		A_p+=1
	else:
		K-=B[B_p]
		B_p+=1
	
	if K<0:
		break
	else:
		read+=1

print(read)

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

readtime = 0
readbook = 0
while readtime<=K:
    if len(A)>0 and A[0]<=B[0]:
        tmp = A.pop(0)
    elif len(B)>0:
        tmp = B.pop(0)
    if readtime+tmp>K:
        break
    readtime += tmp
    readbook += 1
    if len(A)<1 and len(B)<1:
        break

print(readbook)

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
time = 0
while time <= k:
    if i < n and A[i] < B[j]:
        time += A[i]
        i += 1
    elif j < m:
        time += B[j]
        j += 1
    if i >= n and j >= m:
        break
ans = i + j
if time > k:
    ans -= 1
print(ans)

Submission status: Runtime Error

Solution code:
n,m,k = map(int,input().split())
al=list(map(int,input().split()))
bl=list(map(int,input().split()))
if sum(al)+sum(bl)<=k:
  print(n+m)
  exit()
ans = 0
used = 0
indexa = indexb = 0
for i in range(n):
  if used+al[i] <= k:
    used += al[i]
  else:
    indexa = i
    ans += i
    break
for i in range(m):
  if used+bl[i] <= k:
    used += bl[i]
  else:
    indexb = i
    ans += i
    break

tmp = ans
for i in range(indexa-1,-1,-1):
  used -= al[i]
  tmp -= 1
  while bl[indexb]+used <=k:
    used += bl[indexb]
    indexb += 1
    tmp += 1
  ans = max(tmp,ans)
print(ans)

Submission status: Runtime Error

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

    def dfs(n, i, j, d, k):
        if k >= K or (i == len(A) and j == len(B)):
            return n
        if d == 1 and i < len(A) 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)
        elif d == -1 and j < len(B) 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)
        else:
            return n

        return max(l, r)

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

resolve()

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
t = 0
if k<a[0] and k<b[0]:
  print(0)
else:
  while t < k:
    ans += 1
    if a!=[] and b!=[]:
      if a[0] > b[0]:
        t += a[0]
        a.pop()
      elif a[0] < b[0]:
        t += b[0]
        b.pop()
      elif a[0] == b[0]:
        if a[1] <= b[0]:
          t += a[0]
          a.pop()
        elif b[1] <= a[0]:
          t += b[0]
          b.pop()
    elif a==[] and b!=[]:
      t += b[0]
      b.pop()
    elif b==[] and a!=[]:
      t += a[0]
      a.pop()
    else:
      print(n+m)
      exit()
  
  print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque
n,m,k = map(int,input().split())
a = deque(map(int,input().split()))
b = deque(map(int,input().split()))
cnt = 0
ans = 0
for i in range(n+m):
    if len(a) == len(b) == 0:
        break
    elif len(a) == 0 or a[0] >= b[0]:
        cnt += b[0]
        b.popleft()
        if cnt > k:
            break
    elif len(b) == 0 or a[0] < b[0]:
        cnt += a[0]
        a.popleft()
        if cnt > k:
            break
    ans += 1
print(ans)

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_index = 0
b_index = 0
count = 0

def nap(a_index, b_index, time, count):
    if a_index >= len(a_list) and b_index >= len(b_list):
        return count
    else:
        if a_index >= len(a_list) or a_list[a_index]>time:
            nap1 = count
        else:
            nap1 = nap(a_index+1, b_index, time-a_list[a_index], count+1)
        if b_index >= len(b_list) or b_list[b_index]>time:
            nap2 = count
        else:
            nap2 = nap(a_index, b_index+1, time-b_list[b_index], count+1)
    return max(nap1, nap2)

print(nap(0, 0, k, 0))

Submission status: Runtime Error

Solution code:
import sys
from functools import lru_cache

sys.setrecursionlimit(10 ** 5)


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


@lru_cache(None)
def dp(i, j, cur):
    if cur > K:
        return 0
    elif i >= n:
        if j < m and cur + B[j] <= K:
            return dp(i, j + 1, cur + B[j]) + 1
        else:
            return 0
    elif j >= m and cur + A[i] <= K:
        if i < n:
            return dp(i + 1, j, cur + A[i]) + 1
        else:
            return 0
    else:
        res = float('-inf')
        if cur + A[i] <= K:
            res = max(1 + dp(i + 1, j, cur + A[i]), res)
        if cur + B[j] <= K:
            res = max(1 + dp(i, j + 1,  cur + B[j]), res)
        if res == float('-inf'):
            return 0
        else:
            return res


print(dp(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()))
i, j = 0, 0
time = 0
while time <= k:
    print(i, j)
    if i < n and A[i] < B[j]:
        time += A[i]
        i += 1
    elif j < m:
        time += B[j]
        j += 1
    if i >= n and j >= m:
        break
ans = i + j
if time > k:
    ans -= 1
print(ans)

Submission status: Runtime Error

Solution code:

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

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

books = 0

p1 = 0
p2 = 0

while K > 0 and ( p1 < N or p2 < M ):
    books += 1

    if p1 < N and A[p1] < B[p2]:
        # print("chosen", "p1", p1)
        K -= A[p1]
        p1 += 1

    elif p2 < M:
        # print("chosen", "p2", p2)
        K -= B[p2]
        p2 += 1

    # print(K, p1, p2)

# print(K)

if K < 0:
    books -= 1
    print(books)

elif K == 0:
    print(books)

else:
    if p1 < N:
        while K > 0 and p1 < N:
            books += 1
            K -= A[p1]
            p1 += 1

    if p2 < M:
        while K > 0 and p2 < M:
            books += 1
            K -= B[p2]
            p2 += 1

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

A_ = [sum(A[:i+1]) for i in range(len(A)) if sum(A[:i+1]) <= K]
B_ = [sum(B[:i+1]) for i in range(len(B)) if sum(B[:i+1]) <= K]

x = [i+j+2 for i in range(len(A_)) for j in range(len(B_)) if A_[i]+B_[j] <= K]
print(max(x))

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
ac = 0
bc = 0

while cnt < k:
    if ac == len(A):
        cnt += B[bc]
        if cnt <= k:
            bc += 1
        else:
            break
    if bc == len(B):
        cnt += A[ac]
        if cnt <= k:
            ac += 1
        else:
            break
    if ac == len(A) and bc == len(B):
        break
    if A[ac] < B[ac]:
        cnt += A[ac]
        if cnt <= k:
            ac += 1
        else:
            break
    else:
        cnt += B[bc]
        if cnt <= k:
            bc += 1
        else:
            break

print(ac + bc)

Submission status: Runtime Error

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

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()]
lA = len(A)
lB = len(B)
a_i=0
b_i=0
cnt = 0
t = 0
while(k > t):
    if(lA==a_i and lB==b_i):
        break
    if(lA==a_i):
        t += B[b_i]
        b_i += 1
        cnt += 1
        continue
    elif(lB==a_i):
        t += A[a_i]
        a_i += 1
        cnt += 1
        continue
    else:
        if(A[a_i] > B[b_i]):
            now = B[b_i]
            b_i += 1
        else:
            now = A[a_i]
            a_i += 1
        t += now
        cnt += 1

if(t > k):
    cnt -= 1
print(cnt)

Submission status: Runtime Error

Solution code:
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
ans=0
while k>=0:
    if len(a)==0 and len(b)!=0:
        if k>=b[0]:
            ans+=1
            k-=b[0]
            del b[0]
        else:
            break
    elif len(a)!=0 and len(b)==0:
        if k>=a[0]:
            ans+=1
            k-=a[0]
            del a[0]
        else:
            break
    elif len(a)==0 and len(b)==0:
        break
    else:
        mi=min(a[0],b[0])
        if mi>k:
            break
        ans+=1
        k-=mi
        if a[0]>b[0]:
            del b[0]
        elif a[0]<b[0]:
            del a[0]
        else:
            if a[1]>=b[1]:
                del b[0]
            else:
                del a[0]
print(ans)

Submission status: Runtime Error

Solution code:
N, M, K = [int(i) for i in input().split()]

As = [int(i) for i in input().split()]
Bs = [int(i) for i in input().split()]

cnt = 0
time = 0
while time <= K:
    cnt += 1
    if As and Bs:
        if As[0] > Bs[0]:
            p = Bs.pop(0)
        elif Bs[0] > As[0]:
            p = As.pop(0)
    else :
        if As:
            p = As.pop(0)
        elif Bs:
            p = Bs.pop(0)
        else :
            break
    time += p
    
print(cnt - 1)

Submission status: Runtime Error

Solution code:
def resolve():
    lines = sys.stdin.readlines()

    n, m, k = tuple(map(int, lines[0].strip().split(' ')))
    a = list(map(int, lines[1].strip().split(' ')))
    b = list(map(int, lines[2].strip().split(' ')))
    c_max = 0
    c = 0
    s = 0
    for q in b:
        s += q
        if s > k:
            break
        c += 1
    c_max = max(c, c_max)
    s = 0
    c = 0
    for p in a:
        s += p
        c += 1
        if s > k:
            break
        sb = s
        cb = c
        for q in b:
            sb += q
            if sb > k:
                break
            cb += 1
        c_max = max(cb, c_max)


    print(c_max)
    
resolve()