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:
#!/usr/bin/env python3
import sys
from typing import List

dp = {}


# def solve(K: int, A: "List[int]", B: "List[int]"):
#     cost_a = calc_cost(A)
#     cost_b = calc_cost(B)
#     i, j = 0, 0
#     c = K
#     num = 0
#     print(cost_a)
#     print(cost_b)
#     while i < len(cost_a) or j < len(cost_b):
#         tmp = []
#         if i == len(cost_a) or cost_b[j] < cost_a[i]:
#             if c < B[j]:
#                 break
#             c -= B[j]
#             j += 1
#         elif j == len(cost_b) or cost_a[i] < cost_b[j]:
#             if c < A[i]:
#                 break
#             c -= A[i]
#             i += 1
#         else:
#             print('????', i, j)
#         num += 1
#
#     return num
#
# def calc_cost(list):
#     l2 = [0 for _ in list]
#     pre = 0
#     for i in range(len(list)):
#         j = len(list) - 1 - i
#         l2[j] = pre + list[j]
#         pre = l2[j]
#     return l2

def solve(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]


# 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]"
    N, M, K = [int(k) for k in input().split()]
    A = [int(k) for k in input().split()]
    B = [int(k) for k in input().split()]
    print(solve(K, A, B))

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
global info
global A
global B

def calc(a, b, total):
  if a == info[0]-1 and b == info[1]-1:
    return a+b+2
  if a >= info[0] or b >= info[1]:
    return a + b + 1
  if total + A[a] > info[2] and total + B[b] > info[2]:
    return a + b
  else:
    return max(calc(a+1, b, total+A[a]), calc(a, b+1, total+B[b]))
  
info = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
a, b, total = 0, 0, 0

print(calc(a, b, total))

Submission status: Runtime Error

Solution code:
def resolve():
    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)
    b = deque(b)
    min = 0
    ans = 0
    while True:
        if len(a) == 0 and len(b)==0:
            break
        elif len(a) == 0:
            bb = b.popleft()
            read = bb
            if min + read <= k:
                min += read
                ans += 1
            else:
                break
        elif len(b) == 0:
            ab = a.popleft()
            read = ab
            if min + read <= k:
                min += read
                ans += 1
            else:
                break
        else:
            ab = a.popleft()
            bb = b.popleft()
            if ab == bb:
                if a[0] < b[0]:
                    b.appendleft(bb)
                    read = ab
                else:
                    a.appendleft(ab)
                    read = bb

            elif ab < bb:
                b.appendleft(bb)
                read = ab
            else:
                a.appendleft(ab)
                read = bb
            if min + read <= k:
                min += read
                ans += 1
            else:
                break

    print(ans)
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()))
A=[0]+A
B=[0]+B

from itertools import accumulate

AA=list(accumulate(A))
BB=list(accumulate(B))
a,b=0,0

import bisect
for i in range(N+1):
    a=i
    b=bisect.bisect_right(BB,K-AA[i])-1
    if b==-1:
        continue
    count=max(count,a+b)
print(count)

Submission status: Runtime Error

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

t = k
i = 0
asum = [0]* n
bsum = [0]* m
suma = 0
sumb = 0
j =0
for  i in a:
    suma += i
    asum[j]= suma
    j +=1
j =0
for  i in b:
    sumb += i
    bsum[j] = sumb
    j +=0

count = 1
ans = 1

for j in range(m + n-1):
    count = 0
    for i in range(ans):
        if asum[ans-1]+bsum[ans]<=k:
            count = 1

    if count ==1:
        ans +=1
    else:
        break
print(min(ans,m+n))

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()))
ans = 0

while len(A) > 0 and len(B) >0 and k>0:
  if A[0] < B[0]:
    k -= A[0]
    A.popleft()
    ans += 1
  elif A[0] > B[0]:
    k -= B[0]
    B.popleft()
    ans += 1
  elif A[0] == B[0]:
    if (len(A) == 1 and B[1] <= B[0]) or (len(A) == 1 and len(B) == 1) or (len(B) == 1 and A[1] > A[0]):
      k -= B[0]
      B.popleft()
      ans += 1
    elif (len(A) == 1 and B[1] > B[0]) or (len(B) == 1 and A[1] <= A[0]):
      k -= A[0]
      A.popleft()
      ans += 1
    else:
      if A[1] < B[1]:
        k -= A[0]
        A.popleft()
        ans += 1
      elif A[1] > B[1]:
        k -= B[0]
        B.popleft()
        ans += 1
while len(A) > 0 and k > 0:
  k -= A[0]
  A.popleft()
  ans += 1
while len(B) > 0 and k > 0:
  k -= B[0]
  B.popleft()
  ans += 1
if k == 0:
  print(ans)
else:
  print(ans-1)

Submission status: Runtime Error

Solution code:
n, m ,k = map(int,input().split())
a_list = [int(a) for a in input().split()]
b_list = [int(b) for b in input().split()]
time = 0
a_num = 0
b_num = 0
count = 0
 
while time < k:
    # listサイズを超えている場合のチェック
    if len(a_list) == a_num + 1:
        time += b_list[b_num]      
        b_num += 1
    if len(b_list) == b_num + 1:
        time += b_list[b_num]      
        b_num += 1
 
    if a_list[a_num] < b_list[b_num]:
        time += a_list[a_num]
        a_num += 1
    else:
        time += b_list[b_num]      
        b_num += 1
    count += 1

print(count)

Submission status: Runtime Error

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

def dfs(i,j, time, count):
    if time > k:
        return count
    else:
        if i >= n and j >= m:
            return count
        elif i >= n:
            dfs(i, j+1, time+b[j], count+1)
        elif j >= m:
            dfs(i+1, j, time+a[i], count+1)
        else:
            dfs(i, j+1, time+b[j], count+1)
            dfs(i+1, j, time+a[i], count+1)

ans = dfs(0,0,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()))

time = 0
cnt = 0
ans = 0

def dfs(time, cnt):
    global ans
    # print(time)
    if time > K:
        # print(cnt)
        ans = max(ans, cnt-1)
        return
    if A:
        dfs(time+A.pop(0), cnt+1)
    if B:
        dfs(time+B.pop(0), cnt+1)
    if len(A)==0==len(B):
        return cnt

if sum(A)+sum(B)<=K:
    print(N+M)
else:
    dfs(0, 0)
    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()))

i_a = 0
i_b = 0

total_time = 0

while True:
    if i_a == N and i_b == M:
        break
    else:
        if i_a < N and A[i_a] <= B[i_b]:
            read_a = True
        else:
            read_a = False
    
    if read_a:
        if A[i_a] <= K - total_time:
            total_time += A[i_a]
            i_a += 1
        else:
            break
    else:
        if B[i_b] <= K - total_time:
            total_time += B[i_b]
            i_b += 1
        else:
            break

print(int(i_a+i_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()))
time = 0
ans = 0

while time <= K:
    
    if A[0] > K and B[0] > K:
        break
    
    if len(A) == 0 and len(B) == 0:
        break
     
    if len(A) == 0:
        time+=B[0]
        B.pop(0)
        ans+=1
        
    elif len(B) == 0 :
        time+=A[0]
        A.pop(0)
        ans+=1
        
    elif A[0] <= B[0] :
        time += A[0]
        A.pop(0)
        ans+=1
        
    elif B[0] <= A[0] :
        time+=B[0]
        B.pop(0)
        ans+=1
if time > K:
    ans-=1
print(ans)

Submission status: Runtime Error

Solution code:
import sys
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
abl = sorted(a+b)
ans,cnt = 0,0
if sum(abl) <= k:
	print(len(abl))
	sys.exit()
for i in range(1000):
	ans += abl[i]
	if abl[i] <= k:
		cnt += 1
	else:
		print(cnt)
		sys.exit()

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()))
while i<N or j<M:
    if A[i]>=B[j]:
        ans=ans+B[j]
        j=j+1
        if ans>K:
            print(i+j-1)
            break
    else:
        ans=ans+A[i]
        i=i+1
        if ans>K:
            print(i+j-1)
            break
print(i+j)

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:
    if not A == [] and not B == []:
        if A[0] >= B[0]:
            K = K - A[0]
            A.remove(A[0])
            if K >= 0:
                c += 1
        else:
            K = K - B[0]
            B.remove(B[0])
            if K >= 0:
                c += 1
    if A == []:
        K = K - B[0]
        B.remove(B[0])
        if K >= 0:
                c += 1
    if B == []:
        K = K - A[0]
        A.remove(A[0])
        if K >= 0:
                c += 1
print(c)

Submission status: Runtime Error

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

a_i = 0
while k > 0 and a_i < n:
    if  k >= a[a_i]:
        out += 1
        k -= a[a_i]
        a_i += 1
    else:
        break
b_i = 0

if k > 0:
    while k > 0 and b_i < m:
        if  k >= b[b_i]:
            out += 1
            k -= b[b_i]
            b_i += 1
        else:
            break

real = out
if b_i < m:
    while a_i >= 0:
        out -= 1
        k += a[a_i]
        a_i -= 1
        while k > 0:
            if k >= b[b_i]:
                out += 1
                k -= b[b_i]
                b_i += 1
                if real < out:
                    real = out
            else:
                break
            
print(real)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)
# input = sys.stdin.readline    ####
def int1(x): return int(x) - 1
def II(): return int(input())
def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())
def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def MS(): return input().split()
def LS(): return list(input())
def LLS(rows_number): return [LS() for _ in range(rows_number)]
def printlist(lst, k=' '): print(k.join(list(map(str, lst))))
INF = float('inf')
# from math import ceil, floor, log2
# from collections import deque
from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product, permutations
# from heapq import heapify, heappop, heappush
# import numpy as np
# from numpy import cumsum  # accumulate

def solve():
    N, M, K = MI()
    A = LI()
    B = LI()
    
    # print(list(accumulate(A)))
    # print(list(accumulate(B)))
    ra = list(accumulate(A))[::-1]
    rb = list(accumulate(B))[::-1]
    na = N
    for a in ra:
        if a > K:
            na = na - 1
        else:
            break
    nb = M
    for b in rb:
        if b > K:
            nb = nb -1
        else:
            break
    # print(na, nb)
    if na == 0 and nb == 0:
        print(0)
        return
    elif na == 0:
        print(nb)
        return
    elif nb == 0:
        print(na)
        return

    ra = ra[-na:]
    rb = rb[-nb:]
    # print(ra)
    # print(rb)
    ia = 0
    ib = 0
    while ra[ia] + rb[ib] > K:
        if ra[ia] > rb[ib]:
            ia = ia + 1
            na = na - 1
        else:
            ib = ib + 1
            nb = nb - 1
    print(na + nb)


    
if __name__ == '__main__':
    solve()

Submission status: Runtime Error

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

A = [i for i in map(int, input().split())]
B = [i for i in map(int, input().split())]
ans = 0
a = 0
b = 0
t = 0
af = True
bf = True

while 1:
    if not af and not bf:
        break
    elif not af and t+B[b] <= K:
        t += B[b]
        b += 1
        if b == len(B):
            bf = False
        ans += 1
    elif not bf and t+A[a] <= K:
        t += A[a]
        a += 1
        if a == len(A):
            af = False

        ans += 1

    elif A[a] >= B[b] and t+B[b] <= K:
        t += B[b]
        b += 1
        if b == len(B):
            bf = False
        ans += 1
    elif A[a] < B[b] and t+A[a] <= K:
        t += A[a]
        a += 1
        if a == len(A):
            af = False

        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()))
ans = 0
time = 0
n = 0
m = 0
for i in range(N+M):
    if n < N and m < M:
        if A[n] <= B[m]:
            time += A[n]
            n += 1
        else:
            time += B[m]
            m += 1
    else:
        if n >= N and m >= M:
            break
        elif n == N -1:
            time += B[m]
            m += 1
        elif m == M -1:
            time += A[n]
            n += 1
    if time < K:
        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()))
ans = 0
cnt = 0


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

if sum(a) + sum(b) <= k:
    print(n + m)
    exit()

lena = len(a)
lenb = len(b)

timecnt = 0
cnt = 0
while timecnt <= k:
    inda = len(a) - lena
    indb = len(b) - lenb
    if inda < 0 and indb < 0:
        break
    elif inda < 0 and indb >= 0:
        timecnt += b[indb]
        lenb -= 1
        cnt += 1
    elif inda >= 0 and indb < 0:
        timecnt += a[inda]
        lena -= 1
        cnt += 1
    elif a[inda] <= b[indb]:
        timecnt += a[inda]
        lena -= 1
        cnt += 1
    else:
        timecnt += b[indb]
        lenb -= 1
        cnt += 1

print(cnt - 1)

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)
B = deque(B)
 
def check(n, A_, B_):
  if len(A_) >= n and len(B_) >= n:
    if A_[n-1] > B_[n-1]:
      return 'B'
    elif A_[n-1] < B_[n-1]:
      return 'A'
    else:
      check(n+1, A_, B_)
  elif len(A_) < n and len(B_) <  n:
    return 'A'
  elif len(A_) < n:
    return 'B'
  else:
    return 'A'
  
#print(check(1, A, B))


ans = 0
while True:
  if A:
    a = A[0]
  else:
    a = 10**9+7
  if B:
    b = B[0]
  else:
    b = 10**9+7
  if min(a, b) > K:
    break
  elif len(A) == 0 and len(B) == 0:
    break
  else:
    j =check(1, A, B)
    if j ==  'A':
      K -= A.popleft()
      ans += 1
    else:
      K -= B.popleft()
      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()))
#print(N,M,K)
#print(A)
#print(B)
i=0
j=0
cnt=0
time=0
if sum(A)+sum(B)<=K:
    cnt = len(A)+len(B)
elif A[0]>K or B[0]>K:
    cnt = 0
else:
    for n in range(200000):
        if A[i]<=B[j]:
            
            time = time+A[i]
            i = i+1
            if time>K:
                break
            else:
                cnt=cnt+1
        elif A[i]>B[j]:
            time = time+B[j]
            j = j+1
            if time>K:
                break
            else:
                cnt=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()))

time = 0
ans = 0
while time < k:
    
    if len(a) == 0:
        ans += 1
        time += min(max(b)+1, b[0])
        b.pop(0)
        
    elif len(b) == 0:
        ans += 1
        time += min(a[0], max(b)+1)
        a.pop(0)
        
    elif len(a) == 0 and len(b) == 0:
        ans += 1
        break

    else:
        if time + min(a[0], b[0]) > k:
            break
        else:
            ans += 1
            time += min(a[0], b[0])
            if a[0] >= b[0]:
                b.pop(0)
            else:
                a.pop(0)
print(ans)

Submission status: Runtime Error

Solution code:
n,m,k = map(int,input().split());
alist = list(map(int,input().split()));
blist = list(map(int,input().split()));
alist.append(0);
blist.append(0);

book = 0;
time = 0;
aa = 0;
bb = 0;
allread = 0;

while True:
    if aa >= n and bb >= m:
        allread = 1;
        break;
    elif aa >= n and bb < m:
        b = blist[bb];
        a = b + 1;
    elif bb >= m and aa > n:
        a = alist[aa];
        b = a + 1;
    else:
        a = alist[aa]
        b = blist[bb]

    if a == b:
        if alist[aa+1] >= blist[bb+1]:
            time += b;
            bb += 1;
        else:
            time += a;
            aa += 1;
    elif a > b:
        time += b;
        bb += 1;
    else:
        time += a;
        aa += 1;
    if time > k:
        break;
    book += 1;

print(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
cnt = 0


while K >= 0:
    if A != [] and B != []:
        if A[0] > B[0]:
            ele = B.pop(0)
            K -= ele
            cnt += 1
            
        elif A[0] == B[0]:
            if A[1] >= B[1]:
                ele = B.pop(0)
                K -= ele
                cnt += 1 
            else:
                ele = A.pop(0)
                K -= ele
                cnt += 1 
            
        else:
            ele = A.pop(0)
            K -= ele
            cnt += 1
    
    elif A != []:
        ele = A.pop(0)
        K -= ele
        cnt += 1
    
    elif B != []:
        ele = B.pop(0)
        K -= ele
        cnt += 1
    
    else:
        break
    
    if K < 0:
        cnt -= 1
        
print(cnt)

Submission status: Runtime Error

Solution code:
counter = 0
ans = 0

for i in range(n+1):
    if sum(A[:i])>k:
        break
    for j in range(m+1):
        if sum(A[:i])+sum(B[:j])<=k:
            ans = max(ans, i+j)
        else:
            break
            
print(ans)

Submission status: Runtime Error

Solution code:
from sys import exit
# import copy
# import numpy as np
# from collections import deque

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

cnt = 0
while k >= a[0] or k >= b[0]:
    if a[0] < b[0]:
        k -= a[0]
        a.pop(0)
        cnt += 1

    elif a[0] > b[0]:
        k -= b[0]
        b.pop(0)
        cnt += 1

    else:
        for i in range(1, min(len(a), len(b))+1):
            if a[i] < b[i]:
                for t in range(0, i+1):
                    k -= a[0]
                    a.pop(0)
                    cnt += 1
                    if k < a[0]:
                        break
                break
            elif a[i] > b[i]:
                for t in range(0, i+1):
                    k -= b[0]
                    b.pop(0)
                    cnt += 1
                    if k < a[0]:
                        break
                break
        else:
            if len(a) > len(b):
                for i in range(len(a)):
                    k -= a[0]
                    a.pop(0)
                    if k < a[0]:
                        break
            else:
                for i in range(len(b)):
                    k -= b[0]
                    b.pop(0)
                    cnt += 1
                    if k < b[0]:
                        break
    if a == []:
        a = [float("inf")]
    if b == []:
        b = [float("inf")]
print(cnt)

Submission status: Runtime Error

Solution code:
n, m, k = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ai = -1
bi = -1
answer = 0
t_sum = 0
if sum(a) + sum(b) <= k:
    print(n + m)
elif sum(a) <= k:
    t_sum = sum(a)
    ai = n-1
    while True:
        bi += 1
        if t_sum + b[bi] > k:
            break
        t_sum += b[bi] 
    bi -= 1
    answer = ai + bi + 2
    while ai >= 0:
        t_sum -= a[ai]
        ai -= 1
        while bi <= m:
            bi += 1
            if t_sum + b[bi] > k:
                break
            t_sum += b[bi]
        bi -= 1
        if ai + bi +2 > answer:
            answer = ai + bi +2
    print(answer)
else:
    for i in range(n):
        t_sum += a[i]
        if t_sum > k:
            ai = i-1
            break
    while True:
        bi += 1
        if t_sum + b[bi] > k:
            break
        t_sum += b[bi] 
    bi -= 1
    answer = ai + bi + 2
    while ai >= 0:
        t_sum -= a[ai]
        ai -= 1
        while bi <= m:
            bi += 1
            if t_sum + b[bi] > k:
                break
            t_sum += b[bi]
        bi -= 1
        if ai + bi +2 > answer:
            answer = ai + bi +2
    print(answer)

Submission status: Runtime Error

Solution code:
n, m ,k = map(int,input().split())
a_list = [int(a) for a in input().split()]
b_list = [int(b) for b in input().split()]
time = 0
a_num = 0
b_num = 0
count = 0

while time <= k:
    # listサイズを超えている場合のチェック
    if len(a_list) == a_num + 1:
        time += b_list[b_num]      
        b_num += 1
    if len(b_list) == b_num + 1:
        time += b_list[b_num]      
        b_num += 1

    if a_list[a_num] < b_list[b_num]:
        time += a_list[a_num]
        a_num += 1
    else:
        time += b_list[b_num]      
        b_num += 1
    count += 1

print(count)

Submission status: Runtime Error

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

for i in range(N+M):

    a = lista[A]
    b = listb[B]
    if A < len(lista):
        if a <= K:
            K -= lista[A]
            A += 1

    if B < len(listb):
        if b <= K:
            K -= listb[B]
            B += 1

print(A + B)

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

read_time = 0
books = 0
while True:
    if A_list[0] < B_list[0]:
        read_time += A_list[0]
        if read_time > K:
            break
        books += 1
        del A_list[0]
    else:
        read_time += B_list[0]
        if read_time > K:
            break
        books += 1
        del B_list[0]

print(books)

Submission status: Runtime Error

Solution code:
"""everything has a cost!"""

from math import *
import random, functools, collections, sys

__author__ = 'p7x'


# sys.stdin = open('in.txt', 'r')
# sys.stdout = open('out.txt', 'w')
# sys.stderr = open('err.txt', 'w')

i, j = 0, 0
n , m = 0, 0

def read(ti, tj) -> int:
    if (ti < n and (tj == m or a[ti] < b[tj])):
            return 1
    elif (tj < m and (ti == n or b[tj] < a[ti])):
        return 2
    elif (ti < n and tj < m):
        return read(ti+1, tj+1)
    else:
        if (ti == i): return 3
        else: return 1


if __name__ == '__main__':
    n, m, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    
    mins = 0
    while (mins < k):
        v = read(i, j)
        if (v == 1):
            mins += a[i]
            i += 1
        elif (v == 2):
            mins += b[j]
            j += 1
        else:
            break
    if mins > k:
        print(i+j-1)
    else:
        print(i+j)

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

s = abs(len(a) - len(b))
#print(s)
if len(a) < len(b):
    for i in range(s):
        a.append(100100100)
else:
    for i in range(s):
        b.append(100100100)

time = 0
count = 0
while time < k:
    #print(min(a[0], b[0]))
    if time + min(a[0], b[0]) > k:
        break
    time += min(a[0], b[0])
    count += 1
    if a[0] <= b[0]:
        del a[0]
    else:
        del b[0] 

#print(time)
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)
B = deque(B)
 
def check(n, A_, B_):
  if len(A_) >= n and len(B_) >= n:
    if A_[n-1] > B_[n-1]:
      return 'B'
    elif A_[n-1] < B_[n-1]:
      return 'A'
    else:
      check(n+1, A_, B_)
  elif len(A_) < n and len(B_) <  n:
    return 'A'
  elif len(A_) < n:
    return 'B'
  else:
    return 'A'
  
#print(check(1, A, B))


ans = 0
while True:
  if A:
    a = A[0]
  else:
    a = 10**9+7
  if B:
    b = B[0]
  else:
    b = 10**9+7
  if min(a, b) > K:
    break
  else:
    j =check(1, A, B)
    if j ==  'A':
      K -= A.popleft()
      ans += 1
    else:
      K -= B.popleft()
      ans += 1
      
print(ans)

Submission status: Runtime Error

Solution code:
N,M,K = map(int, input().split())
A_list = [int(e) for e in input().split()] #N
B_list = [int(e) for e in input().split()] #M

#ABの読む順序は関係ない
A_list = A_list[::-1]
B_list = B_list[::-1]

A_readed_books = list()

#まずAをずっと読んだ時の最大冊数を求める
reading_time_sum = 0
read_books_num = 0

while len(A_list)!=0:
    if reading_time_sum + A_list[-1] <= K:
        reading_time_sum += A_list[-1]
        read_books_num += 1
        A_readed_books.append(A_list.pop(-1))
    else:
        break

read_books_num_max = read_books_num
B_readed_books = list()

if reading_time_sum < K:
    while len(B_list)!=0:
        if reading_time_sum + B_list[-1] <= K:
            reading_time_sum += B_list[-1]
            B_readed_books.append(B_list.pop(-1))
            read_books_num += 1
        else:
            break

if len(B_list)==0:
    print(read_books_num)
else:
    read_books_num_max = read_books_num
    #Aの読む冊数を減らして、Bに割り当てて冊数の増加を試す
    while len(A_readed_books)!=0:
        last_readed_books = A_readed_books.pop(-1)
        reading_time_sum -= last_readed_books
        read_books_num -= 1
        while 1:
            if B_list[-1] + reading_time_sum <= K:
                reading_time_sum += B_list[-1]
                B_readed_books.append(B_list.pop(-1))
                read_books_num += 1
            else:
                break
        if read_books_num_max < read_books_num:
            read_books_num_max = read_books_num

    print(read_books_num_max)

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:
    for c in range(M - N):
        A.append(1000000001)
elif M < N:
    for c in range(N - M):
        B.append(1000000001)

a = 0
b = 0
k = 0
ans = 0

while k < K:
    if N + M == ans:
        break
    if A[a] >= B[b]: # Bのほうが小さい場合
        k += B[b]
        b += 1
    else: # Aのほうが小さい場合
        k += A[a]
        a += 1
    if k > K:
        break
    ans += 1

print(ans)

Submission status: Runtime Error

Solution code:
N,M,K=map(int,input().split())
booka=list(map(int,input().split()))
bookb=list(map(int,input().split()))
answer=0
while len(booka)*len(bookb)>0 and K-booka[0]>=0 and K-bookb[0]>=0:
  if booka[0]>=bookb[0]:
    K-=booka[0]
    answer+=1
    booka=booka.pop(0)
  else:
    K-=bookb[0]
    answer+=1
    bookb=bookb.pop(0)
print(answer)