Problem:


Score : 300 points

Problem Statement

We have two desks: A and B. Desk A has a vertical stack of N books on it, and Desk B similarly has M books on it.

It takes us A_i minutes to read the i-th book from the top on Desk A (1 \leq i \leq N), and B_i minutes to read the i-th book from the top on Desk B (1 \leq i \leq M).

Consider the following action:

Choose a desk with a book remaining, read the topmost book on that desk, and remove it from the desk.

How many books can we read at most by repeating this action so that it takes us at most K minutes in total? We ignore the time it takes to do anything other than reading.

Constraints

1 \leq N, M \leq 200000

1 \leq K \leq 10^9

1 \leq A_i, B_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M K
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_M

Output

Print an integer representing the maximum number of books that can be read.

Sample Input 1

3 4 240
60 90 120
80 150 80 150

Sample Output 1

3

In this case, it takes us 60, 90, 120 minutes to read the 1-st, 2-nd, 3-rd books from the top on Desk A, and 80, 150, 80, 150 minutes to read the 1-st, 2-nd, 3-rd, 4-th books from the top on Desk B, respectively.

We can read three books in 230 minutes, as shown below, and this is the maximum number of books we can read within 240 minutes.

Read the topmost book on Desk A in 60 minutes, and remove that book from the desk.

Read the topmost book on Desk B in 80 minutes, and remove that book from the desk.

Read the topmost book on Desk A in 90 minutes, and remove that book from the desk.

Sample Input 2

3 4 730
60 90 120
80 150 80 150

Sample Output 2

7

Sample Input 3

5 4 1
1000000000 1000000000 1000000000 1000000000 1000000000
1000000000 1000000000 1000000000 1000000000

Sample Output 3

0

Watch out for integer overflows.



Sample input:
3 4 240
60 90 120
80 150 80 150


Sample output:
3

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline

if __name__ == '__main__':
  n,m,k=map(int,input().strip("\n").split())
  A=list(map(int,input().strip("\n").split()))
  B=list(map(int,input().strip("\n").split()))
  ans=0
  a=0
  b=0
  while k>0 and (a<n or b<m):
    if a>=n:
      if k<B[b]:
        break
      ans+=1
      k-=B[b]
      b+=1
      continue

    if b>=m:
      if k<A[a]:
        break
      ans+=1
      k-=A[A]
      a+=1
      continue

    if A[a]<=B[b]:
      if k<A[a]:
        break
      ans+=1
      k-=A[a]
      a+=1
    else:
      if k<B[b]:
        break
      ans+=1
      k-=B[b]
      b+=1

  print(ans)

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

Submission status: Runtime Error

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

A_BOOK = list([int(x) for x in input().split()])
B_BOOK = list([int(x) for x in input().split()])


def read_book(read_time, book_count, a_remain, b_remain):

    if a_remain:
        a = a_remain[0]
    else:
        a = 10**9

    if b_remain:
        b = b_remain[0]
    else:
        b = 10*9

    a_count = book_count
    b_count = book_count

    if read_time + a < K:
        a_count = read_book(read_time + a, book_count +
                            1, a_remain[1:], b_remain)

    if read_time + b < K:
        b_count = read_book(read_time + b, book_count +
                            1, a_remain, b_remain[1:])

    if a_count > b_count:
        return a_count

    return b_count


count = read_book(0, 0, A_BOOK, B_BOOK)

print(count)

Submission status: Runtime Error

Solution code:
# from collections import deque
def main():
    n, m, k = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    cnt_A = 0
    cnt_B = 0
    time = 0
    cnt = 0

    while time <= k:
        # Aの本がまだ無くなっていない時
        if cnt_A < n:
            # Aの本の方が読みやすい
            if A[cnt_A] < B[cnt_B]:
                time += A[cnt_A]
                cnt_A += 1
                cnt += 1

            # Bの本の方が読みやすい
            else:
                time += B[cnt_B]
                cnt_B += 1
                cnt += 1

        # Aの本がなくなっている時
        else:
            if cnt_B < m:
                time += B[cnt_B]
                cnt_B += 1
                cnt += 1
            else:
                break

        #print(cnt,time)


    if time > k:
        print(cnt - 1)
    else:
        print(cnt)




if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
def func(a, b, total):
  if total + A[a+1] > info[2] && total + B[a+1] > info[2]:
    return a + b + 2
  else:
    return max(func(a+1, b, total+A[a+1]), func(a, b+1, total+B[b+1]))
  
global info = [int(i) for i in input().split()]
global A = [int(i) for i in input().split()]
global B = [int(i) for i in input().split()]
a, b, total = 0, 0, 0

print(func(a, b, total))

Submission status: Runtime Error

Solution code:
acount = 0
bcount = 0

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

curr_time = 0
books = 0
a_books = list(map(int, input().split()))
b_books = list(map(int, input().split()))

while curr_time + a_books[acount] <= limit or curr_time + b_books[bcount] <= limit:
  if acount < a:
    if bcount < b:
      if a_books[acount] < b_books[bcount]:
        curr_time += a_books[acount]
        acount += 1
        books += 1
      else:
        curr_time += b_books[acount]
        bcount += 1
        books += 1
    else: 
      curr_time += a_books[acount]
      acount += 1
      books += 1
  else:
    curr_time += b_books[acount]
    bcount += 1
    books += 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()))
ans = 0
c = 0
i = 0
l, s = len(A), len(B)
if s>l:
    l, s = len(B), len(A)
while ans<K and i <=l:
    l, h = A[0], B[0]
    if l>h:
        l, h = B[0], A[0]
    ans+=l
    if l == A[0]:
        A = A[1:]
    else:
        B = B[1:]
    c += 1

print(c-1)

Submission status: Runtime Error

Solution code:
def main(n, m, k, a_n, b_n):
    r = 0

    a = 0
    b = 0

    for i in range(n+m):
        if len(a_n)-1 < a:
            a_v = float('inf')
        else:
            a_v = a_n[a]

        if len(b_n)-1 < a:
            b_v = float('inf')
        else:
            b_v = b_n[b]

        if a_v < b_v:
            a += 1
            v = a_v
        else:
            b += 1
            v = b_v

        k -= v
        if k < 0:
            return r

        r += 1
    return r


if __name__ == "__main__":
    n, m, k = map(int, input().split())
    a_n = list(map(int, input().split()))
    b_n = list(map(int, input().split()))
    print(main(n, m, k, a_n, b_n))

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
while time<k:
    if count==(n+m-1):
        count=n+m+1
        break
    if count%2==0:
        time+=a[count//2]
        count+=1
    else:
        time+=b[count//2]
        count+=1
print(count-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()))

time = 0
i_a = 0
i_b = 0
ans = 0

if sum(A) + sum(B) <= K:
    print(len(A)+len(B))
    exit()
elif A[i_a] > K and B[i_b] > K:
    print(0)
    exit()
    
while time <= K :
    if A[i_a] >= B[i_b]:
        time += A[i_a]
        i_a += 1
        ans += 1
    elif A[i_a] < B[i_b]:
        time += B[i_b]
        i_b += 1
        ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline

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

while 0 < k:
    tmp = 0

    if a == [] or a[0] >= b[0]:
        tmp = b[0]
        b.pop(0)
    elif b == [] or a[0] < b[0]:
        tmp = a[0]
        a.pop(0)
    
    k -= tmp
    if 0 <= k:
        ans += 1

print(ans)

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

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

if len(a)>len(b):
	a,b=b,a
i=0
j=0
total=0
cnt=0
while total<=k:
	if i<len(a):
		if a[i]<b[j]:
			total+=a[i]
			i+=1
		else:
			total+=b[j]
			j+=1
	elif j<len(b):
		total+=b[j]
		j+=1

	cnt+=1
	if total>k:
		cnt-=1
	if j==len(b) and i==len(a):
		break

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()))
now = 0
mode = 0
ans = 0
while now <= K:
    if mode == 0:
        if A[0] < B[0]:
            now += A[0]
            A.remove(A[0])
        elif A[0] > B[0]:
            now += B[0]
            B.remove(B[0])
        else:
            if A[1] == B[1]:
                now += A[0]
                A.remove(A[0])
            elif A[1] > B[1]:
                now += B[0]
                B.remove(B[0])
            elif A[1] < B[1]:
                now += A[0]
                A.remove(A[0])
    elif mode == 1:
        now += B[0]
        B.remove(B[0])
    elif mode == 2:
        now += A[0]
        A.remove(A[0])
    ans += 1
    if len(A) == 0 and len(B) == 0:
        break
    elif len(A) == 0 and len(B) != 0:
        mode = 1
    elif len(B) == 0 and len(A) != 0:
        mode = 2
if now > K:
    print(ans-1)
else:
    print(ans)

Submission status: Runtime Error

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

i = 0
j = 0
while True:
    
    if a[i] < b[j]:
        ans += [a[i]]
        i += 1
    else:
        ans += [b[j]]
        j += 1

    if sum(ans) > k:
        print(len(ans) - 1)
        break

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 = A+B

ans = 0
count = 0
a = 0
b = 0
while ans <= K:
  if a > len(A) - 1 and b > len(B)-1:
    break

  if a > len(A) - 1 and b <= len(B)-1:
    ans += B[b]
    if ans <= K:
      b += 1
      continue
    else:
      break
    

  if a <= len(A) - 1 and b > len(B)-1:
    ans += B[b]
    if ans <= K:
      b += 1
      continue
    else:
      break
     
  if A[a] >= B[b]:
    ans += B[b]
    if ans <= K:
      b += 1
    else:
      break
  else:
    ans += A[a]
    if ans <= K:
      a += 1
    else:
      break

print(a+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()))

cumsum=0
cnt=0
i=0
j=0
while cumsum<k:
    if i>len(a)-1:
        a_i=float('inf')
    else:
        a_i=a[i]

    if j>len(b)-1:
        a_i=float('inf')
    else: 
        b_i=b[j]   

    if a_i<=b_i:
        cumsum+=a[i]
        i+=1
    else:
        cumsum+=b[j]
        j+=1

    if cumsum<=k:
        cnt+=1
    else:
        break

    if i==len(a)-1 and j==len(b)-1:
        break
    
print(cnt)

Submission status: Runtime Error

Solution code:
n, m, k = map(int,input().split(" "));
arr = list(map(int,input().split(" ")));
brr = list(map(int,input().split(" ")));
i = 0;
j = 0;
cur = 0;
c = 0;
while(i<n and j<m):
  if(arr[i] > brr[j]):
    if(k < cur + brr[j]):
      cur+=brr[j];
      j+=1;
  else:
    if(k < cur + arr[i]):
    	cur+=arr[i];
		i+=1;
  if(k <= cur):
      break;
print(c);

Submission status: Runtime Error

Solution code:
import sys

if __name__ == '__main__':
    n,m,k = list(map(lambda x: int(x), sys.stdin.readline().split()))
    A = list(map(lambda x: int(x), sys.stdin.readline().split()))
    B = list(map(lambda x: int(x), sys.stdin.readline().split()))

    i, j = 0, 0
    offset = 0
    summ = 0
    while True:
        if summ > k:
            offset = -1 
            break
        if i == len(A) and j == len(B):
            break
        if j  == len(B) or A[i] < B[j]:
            summ += A[i]
            i += 1
        else:
            summ += B[j]
            j += 1
    print(i+j+offset)

Submission status: Runtime Error

Solution code:
N,M,K=int(input()),int(input()),int(input())
A=[]
B=[]
for i in range(N):
  s=int(input())
  A.append(s)
for i in range(M):
  k=int(input)
  B.append(k)
k=0
count1=0
count2=0
for i in range(len(A)):
  
  if k<=K:
    k=k+A[i]
    count1+=1
  else:
      break
k=0
for i in range(len(B)):
  if k<=K:
    k=k+A[i]
    count2+=1
  else:
    break
print(max(count1,count2))

Submission status: Runtime Error

Solution code:
# coding: UTF-8

import numpy as np

N, M, K = np.fromstring(input(), sep=" ", dtype=np.int64)
A = np.fromstring("0 " + input(), sep=" ", dtype=np.int64)
B = np.fromstring("0 " + input(), sep=" ", dtype=np.int64)

aa = np.add.accumulate(A)
bb = np.add.accumulate(B)

cc = np.add.outer(aa, bb)

dd = cc <= K

num = np.add.outer(np.arange(N+1), np.arange(M+1))
# print(dd.shape)
# print(num.shape)
print(np.max(num[dd]))

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
D=0
while A[C]+B[D]<=K:
  if A[C]<=B[D]:
    C+=1
  else:
    D+=1
print(C+D+2)

Submission status: Runtime Error

Solution code:
from collections import deque
N, M, K = map(int, input().split())
A = sorted(list(map(int, input().split())))
B = sorted(list(map(int, input().split())))
if sum(A) + sum(B) <= K:
  print(len(A)+len(B))
else:
  time = 0
  ans = 0
  while time < K:
      if A[0] <= B[0]:
          time += A[0]
          ans += 1
          A.popleft()
      else:
          time += B[0]
          ans += 1
          B.popleft()
  print(ans-1)

Submission status: Runtime Error

Solution code:
INF = 10**9+7
N, M, K = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
A.append(INF)
B.append(INF)
ans = 0
i, j = [0, 0]
while K >= min(A[i], B[j]):
    useA = A[i] < B[j]
    if A[i] == B[j]:
        k, l = [i+1, j+1]
        while A[k] == B[l]:
            k += 1
            l += 1
        useA = A[k] < B[l]
    if useA:
        ans += 1
        K -= A[i]
        i += 1
    else:
        ans += 1
        K -= B[j]
        j += 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()))

c = 0

while (True):
    if A[0] < B[0] and A[0] <= K:
        K -= A.pop(0)
        c += 1

    elif B[0] <= K:
        K -= B.pop(0)
        c += 1

    else:
        break

print(c)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys
# from typing import List

# dp = {}

def solve(N: int, M: int, K: int, A: "List[int]", B: "List[int]"):
    if K == 0: return 0

    r = _solve(0, K, A, B, 0, 0)
    return r


def _solve(current:int, K: int, A: "List[int]", B: "List[int]", i, j):
    # key = f'{i}_{j}'
    # if key in dp: return dp[key]

    res = [current]
    if i < len(A) and A[i] <= K:
        res.append(_solve(current + 1, K - A[i], A, B, i + 1, j))

    if j < len(B) and B[j] <= K:
        res.append(_solve(current + 1, K - B[j], A, B, i, j + 1))

    # dp[key] = max(res)
    # return dp[key]
    return max(res)


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    B = [int(next(tokens)) for _ in range(M)]  # type: "List[int]"
    print(solve(N, M, K, A, B))

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()))
time = 0
count = 0
Ain = 0
Bin = 0
if(min(min(A),min(B))>K):
    print(0)
    exit()
while time<=K:
    if(len(A)!=0 and len(B)!=0):
        if(A[Ain]<B[Bin]):
            time+= A[Ain]
            Ain+=1
            count +=1
        elif(A[Ain]>B[Bin]):
            time += B[Bin]
            Bin+=1
            count+=1
    if((len(A)==0) and (len(B)==0)):
        break
    elif(len(A)==0):
        time += B[Bin]
        Bin += 1
        count += 1
    elif(len(B)==0):
        time += A[Ain]
        Ain += 1
        count += 1
if(time == K):
    print(count)
else:
    print(count-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
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==[] 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:
N, M, K = map(int, input().split())
A = list(map(int, input().split()))[::-1]
B = list(map(int, input().split()))[::-1]

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

Submission status: Runtime Error

Solution code:
def input_int():
    return map(int, input().split())

def one_int():
    return int(input())

def one_str():
    return input()

def many_int():
    return list(map(int, input().split()))

N, M, K = input_int()
A = many_int()
B = many_int()


A_index=0
B_index=0
books = 0

while True:
    temp = books
    if N <= A_index and M <= B_index:
        break
    
    elif N <= A_index  and K>=B[B_index]:
        books += 1
        K -= B[B_index]
        B_index += 1
        
    elif M <= B_index  and K>=A[A_index]:
        books += 1
        K -= A[A_index]
        A_index += 1
        
    elif A[A_index] > B[B_index] and K>=B[B_index]:
        books += 1
        K -= B[B_index]
        B_index += 1
        
    elif A[A_index] < B[B_index] and K>=A[A_index]:
        books += 1
        K -= A[A_index]
        A_index += 1
    if temp==books:
        break
print(books)

Submission status: Runtime Error

Solution code:
# coding: utf-8

N, M, K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
t = 0
ia = 0
ib = 0
ans = 0
#qa = deque()
#qb = deque()
while t <= K:
    #print('ans=' + str(ans), ia,ib)
    if ia == N and ib == M:
        break
    if ia < N and ib < N:
        if A[ia] <= B[ib]:
            t += A[ia]
            ia += 1
            ans += 1
        else:
            t += B[ib]
            ib += 1
            ans += 1
    elif ia == N:
        t += B[ib]
        ib += 1
        ans += 1
    elif ib == M:
        t += A[ia]
        ia += 1
        ans += 1
    #print(ans)
if t > K:
    ans -= 1
print(ans)

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) + sum(m_list) <= k) :
    count = n + m
else :
    for i in range(0, 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()))
dp = [0] * k
aidx = 0
bidx = 0
ans = 0
time = 0
while True:
    if aidx < n and bidx < m:
        if a[aidx] < b[bidx]:
            time += a[aidx]
            if time > k:
                break
            aidx += 1
            ans += 1
        else:
            time += b[bidx]
            if time > k:
                break
            bidx += 1
            ans += 1
    elif aidx < n:
        time += a[aidx]
        if time > k:
            break
        ans += 1
        aidx += 1
    elif bidx < m:
        time += b[bidx]
        if time > k:
            break
        ans += 1
        bidx += 1
    else:
        break
print(ans)

Submission status: Runtime Error

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

if k < A[0]:
	print(0)
else:
	for i in range(1, n+m+1):
		A[i] = A[i] + A[i-1]
		if k < A[i]:
			print(i)
			break
		if i == n+m:
			print(i+1)

Submission status: Runtime Error

Solution code:
n, m, k = map(int, input().split())
a = list(map(int, input().split()))+[10**10]
b = list(map(int, input().split()))+[10**10]



def dfs(ai, bi, time, finished=False):
    if finished:
        return ai+bi

    if time >= a[ai]:
        ret1 = dfs(ai+1, bi, time-a[ai])
    else:
        ret1 = dfs(ai, bi, time, finished=True)

    
    if time >= b[bi]:
        ret2 = dfs(ai, bi+1, time-b[bi])
    else:
        ret2 = dfs(ai, bi, time, finished=True)

    return max(ret1, ret2)

print(dfs(0, 0, k))

Submission status: Runtime Error

Solution code:
import sys

if __name__ == '__main__':
    n,m,k = map(lambda x: int(x), sys.stdin.readline().split())
    A = map(lambda x: int(x), sys.stdin.readline().split())
    B = map(lambda x: int(x), sys.stdin.readline().split())

    i, j = 0, 0
    offset = 0
    summ = 0
    while True:
        if summ > k:
            offset = -1 
            break
        if i == len(A) and j == len(B):
            break
        if j  == len(B) or A[i] < B[j]:
            summ += A[i]
            i += 1
        else:
            summ += B[j]
            j += 1
    print(i+j+offset)

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()))
time = 0
a_idx = 0
b_idx = 0
cnt = 0
while True:
  if a_idx < N and b_idx < M:
    if A_list[a_idx] >= B_list[b_idx]:
      time += B_list[b_idx]
      b_idx += 1
      cnt += 1
    else:
      time += A_list[a_idx]
      a_idx += 1
      cnt += 1
  elif a_idx >= N and b_idx < M:
    time += B_list[b_idx]
    b_idx += 1
    cnt += 1
  elif a_idx < N and b_idx >= M:
    time += B_list[b_idx]
    a_idx += 1
    cnt += 1
  else:
    break
  
  if time > K:
    cnt -= 1
    break
print(cnt)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;

#define LL long long
//#define LL __int64
typedef pair<int, int> pii;
typedef pair<LL, LL> pLL;
typedef vector<int> VI;
typedef vector< vector<int> > VVI;

template< typename T > inline void read(T &x)
{
    static char _c;
    static bool _f;

    x = 0;
    _f = 0;
    _c = getchar();

    while(_c < '0' || '9' < _c) {if(_c == '-') _f = true; _c = getchar();}
    while('0' <= _c && _c <= '9') {x = (x << 1) + (x << 3) + (_c & 15); _c = getchar();}
    if(_f) x = -x;
}
template < typename T, typename ...Args > inline void read(T &x, Args &...args) {read(x); read(args...);}

template < typename T > inline void Min(T &x, T y) {if(y < x) x = y;}
template < typename T > inline void Max(T &x, T y) {if(x < y) x = y;}


#define lowbit(x) ((x) & -(x))
#define lson l,mid,id<<1
#define rson mid+1,r,id<<1|1
#define ls id<<1
#define rs id<<1|1
#define MID(l,r) ((l)+(((r)-(l))>>1))
#define fi first
#define se second
#define mk make_pair
#define mt make_tuple
#define pb push_back
#define epb emplace_back
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()

const int INF = 0x3f3f3f3f;
const LL LINF = 0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int e5 = 100000; // 1e5
const int e6 = 1000000; //1e6;
//const int MOD = 1000000007; //1e9 + 7
const int MOD = 998244353;
//const int MOD = 911451407;

template< typename T0, typename T1 > inline void depair(T0 &x, T1 &y, pair<T0, T1> &p) {x = p.fi, y = p.se;}
inline int sig(double x) {return x < -eps ? -1 : eps < x;}
LL fp(LL a, LL n, LL mod = MOD)
{
    if(n < 0) a = fp(a, mod - 2, mod), n = -n;
    LL res = 1; for(; n; n >>= 1, a = a * a % mod) if(n & 1) res = res * a % mod; return res;
}

struct Mint
{
    int x;
    Mint():x(0) {}
    Mint(int _x): x(_x)
    {
        if(x < 0 || x >= MOD) x %= MOD;
        if(x < 0) x += MOD;
    }
    Mint(LL _x)
    {
        if(_x < 0 || _x >= MOD) _x %= MOD;
        if(_x < 0) _x += MOD;
        x = _x;
    }
    Mint operator - () const{
        return Mint(MOD - x);
    }
    Mint operator + (const Mint &rhs) const{
        return Mint(x + rhs.x >= MOD ? x - MOD + rhs.x : x + rhs.x);
    }
    Mint operator - (const Mint &rhs) const{
        return Mint(x - rhs.x < 0 ? x - rhs.x + MOD : x - rhs.x);
    }
    Mint operator * (const Mint &rhs) const{
        return Mint((LL)x * rhs.x % MOD);
    }
    Mint operator / (const Mint &rhs) const{
        return Mint((LL)x * fp(rhs.x, -1) % MOD);
    }
    Mint& operator += (const Mint &rhs) {
        x += rhs.x; if(x >= MOD) x -= MOD; return *this;
    }
    Mint& operator -= (const Mint &rhs) {
        x -= rhs.x; if(x < 0) x += MOD; return *this;
    }
    Mint& operator *= (const Mint &rhs) {
        x = ((LL)x * rhs.x) % MOD; return *this;
    }
    Mint& operator /= (const Mint &rhs) {
        x = (LL)x * fp(rhs.x, -1) % MOD; return *this;
    }
    bool operator == (const Mint &rhs) const{
        return x == rhs.x;
    }
    bool operator != (const Mint &rhs) const{
        return x != rhs.x;
    }
    friend ostream& operator << (ostream &out, const Mint &rhs){
        return out << rhs.x;
    }
    friend istream& operator >> (istream &in, Mint &rhs){
        return in >> rhs.x;
    }

    Mint inv() {return Mint(fp(x, -1));}
    Mint pow(int k) {return Mint(fp(x, k));}
    Mint pow(const Mint &t) {return Mint(fp(x, t.x));}
};


const int maxn = (int) 1e6 + 20;
const int maxm = (int) 2e6 + 20;

void work()
{
    int n, m;
    LL k;
    cin >>n >>m >>k;

    vector<LL> a(n), b(m);
    for(int i = 0; i < n; i++) cin >>a[i];
    for(int i = 0; i < m; i++) cin >>b[i];

    sort(all(a));
    sort(all(b));

    int j = 0;
    LL jt = 0;
    while(j < m && jt + b[j] <= k) jt += b[j++];

    int cnt = j;
    for(int i = 0; i < n; i++)
    {
        k -= a[i];
        if(k < 0) break;
        while(j && jt > k) jt -= b[--j];
        cnt = max(cnt, j + i + 1);
    }
    cout <<cnt <<endl;
}

int main()
{
#ifdef yukihana0416
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // yukihana0416

    int tc = 1;
//    read(tc);
    for(int ca = 1; ca <= tc; ca++)
    {
//        printf("Case #%d: ", ca);
        work();
    }
    return 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()))

ans = 0
k_list = []

while k > 0:
    ans += 1
    if len(a) > 0 and a[0] <= b[0]:
        k -= a.pop(0)
    else:
        k -= b.pop(0)
    k_list.append(k)

if k_list[-1] < 0:
    print(ans-1)
else:
    print(ans)

Submission status: Runtime Error

Solution code:
N,M,K=map(int,input().split())
A=[int(a) for a in input().split()]
B=[int(b) for b in input().split()]

cnt=0
ans=0
if A[0]>K and B[0]>K:
    print(0)
    exit()
else:
    for i in range(10**9):
        if cnt>K:
            print(ans)
            exit()
        else:
            if len(A)==0 and len(B)==0:
                cnt+=10**9
            elif len(A)==0 and len(B)!=0:
                cnt+=B[0]
                B.remove(B[0])
                ans+=1
            elif len(B)==0 and len(A)!=0:
                cnt+=A[0]
                A.remove(A[0])
                ans+=1
            else:
                if A[0]>B[0]:
                    cnt+=A[0]
                    A.remove(A[0])
                    ans+=1
                elif A[0]<B[0]:
                    cnt+=B[0]
                    B.remove(B[0])
                    ans+=1
                else:
                    if A[1]>B[1]:
                        cnt+=B[0]
                        B.remove(B[0])
                        ans+=1
                    else:
                        cnt+=A[0]
                        B.remove(A[0])
                        ans+=1

Submission status: Runtime Error

Solution code:
from collections import deque
def LI():
  return list(map(int,input().split()))

N,M,K = map(int,input().split())
A = deque(LI())
B = deque(LI())
res = 0

while K >= 0:
  if len(A) == 0:
    if K >= B[0]:
      b = B.popleft()
      K -= b
    else:
      break
  elif len(B) == 0:
    if K>=A[0]:
      a = A.popleft()
      K -= a
    else:
      break
  else:
    if A[0] > B[0]:
      b = B.popleft()
      K -= b
    elif A[0] < B[0]:
      a = A.popleft()
      K -= a
    else:
      if A[1] > B[1]:
        b = B.popleft()
        K -= b
      else:
        a = A.popleft()
        K -= a

  if K >= 0:
    res += 1
  else:
    break

  if len(A) == 0 and len(B) == 0:
    break


print(res)

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()))
k = 0
idx1 = 0
idx2 = 0
while True:
    if idx1 >= N and idx2 >= M:
        break
    if idx1 >= N:
        if B[idx2] <= K:
            K -= B[idx2]
            idx2 += 1
            continue
        else:
            break
    elif idx2 >= N:
        if A[idx1] <= K:
            K -= A[idx1]
            idx1 += 1
        else:
            break
    if A[idx1] <= B[idx2]:
        if A[idx1] <= K:
            K -= A[idx1]
            idx1 += 1
            continue
        else:
            break
    else:
        if B[idx2] <= K:
            K -= B[idx2]
            idx2 += 1
            continue
        else:
            break
print(idx1 + idx2)

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()))
if n > m:
    b += [10**9+1]*(n-m)
elif m > n:
    a += [10**9+1]*(m-n)

t = 0
i, j = 0, 0
ans = 0
while i < n or j < m:
    if a[i] <= b[j]:
        if t + a[i] <= k:
            t += a[i]
            i += 1
        else:
            break
    else:
        if t + b[j] <= k:
            t += b[j]
            j += 1
        else:
            break
    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()))
at,bt=0,0
p=0
while p<=k:
    if (n!=at and a[at]<b[bt]) or m==bt:
        p+=a[at]
        at+=1
    else:
        p+=b[bt]
        bt+=1
    if n==at and m==bt:
        print(at+bt)
        break
    #print(p)
else:
    print(at+bt-1)