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:
#C
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
i = 0
j = 0
for _ in range(n+m):
    # print(i,j,k)
    if n <= i:#
        k -= b[j]
        j += 1
        if k < 0:
            break
        ans += 1
        if k == 0:
            break
        continue
    elif n <= j:
        k -= a[i]
        i += 1
        if k < 0:
            break
        ans += 1
        if k == 0:
            break
        continue
    if a[i] < b[j]:
        k -= a[i]
        i += 1
    else:
        k -= b[j]
        j += 1
    if k < 0:
        break
    ans += 1
    if k == 0:
        break
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)
  elif booka[0]==bookb[0]:
    n=1
    while booka[n]=bookb[n]=booka[0]:
      n+=1
      if n>len(booka) 
        listp=[2*len(booka),math.floor(K/booka[0])]
        answer+=max(listp)
        break
      print(answer)
      elif booka[n]<bookb[n]:
        K-=booka[0] 
        answer+=1
        booka=booka.pop(0)
      else:
        K-=bookb[0]
        answer+=1
        bookb=bookb.pop(0)
  else:
    K-=bookb[0]
    answer+=1
    bookb=bookb.pop(0)

print(answer)

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):
        
        k = k - n_list[i]
        if k < 0 :
            break
        else :
            count += 1

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

print(count)

Submission status: Runtime Error

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


def log(*args):
    print(*args, file=sys.stderr)


def main():
    n, m, k = map(int, input().split())
    ns = list(map(int, input().rstrip().split()))
    ms = list(map(int, input().rstrip().split()))
    # 全部Aの場合上からなんこ取れるかを見つつ、Bを入れて押し出していく
    sum = 0
    last_flg = False
    for (i, v) in enumerate(ns):
        if sum + v > k:
            break
        else:
            sum += v
        if i == len(ns) - 1:
            last_flg = True

    if last_flg:
        i += 1
    ans_now = i
    ans = ans_now
    i -= 1
    for v in ms:
        break_flg = False
        if sum + v > k:
            while sum + v > k:
                sum -= ns[i]
                i -= 1
                ans_now -= 1
                if i == 0:
                    break_flg = True
                    break
            if break_flg:
                break
            sum += v
            ans_now += 1
        else:
            sum += v
            ans_now += 1
        ans = max(ans, ans_now)
    print(ans)


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

cnt = 0
cost = 0
for i, x in enumerate(a):
    cost += x
    if cost > k:
        cnt = i
        cost -= x
        break
else:
    for j, y in enumerate(b):
        cost += y
        if cost > k:
            cnt = n + j
            cost -= y
            break
    else:
        cnt = n + m

if cnt <= n:
    i_max, j = cnt - 1, 0
else:
    i_max, j = n - 1, cnt - n + 1

while cost + b[j] <= k and j < m - 1:
    j += 1
    cnt += 1
    cost += b[j]

ans = cnt
for i_max in range(i_max, -1, -1):
    cost -= a[i]
    cnt -= 1
    while cost + b[j] <= k and j < m - 1:
        j += 1
        cnt += 1
        cost += b[j]
    ans = max(cnt, ans)
    if j == m - 1: break
    i -= 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()))

ai = 0
bi = 0

def read(ai, bi, count, rest):
    if rest <= 0:
        return count
    else:
        if ai >= N:
            a_c = count
        else:
            if rest >= A[ai]:
                a_c = read(ai+1, bi, count+1, rest - A[ai])
            else:
                a_c = count

        if bi >= M:
            b_c = count
        else:
            if rest >= B[bi]:
                b_c = read(ai, bi+1, count+1, rest - B[bi])
            else:
                b_c = count

        return max(a_c, b_c)

res = read(ai,bi,0,K)
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()))
ans = 0

# もしすべてaから選んだ場合
time = k
aOnly = 0
for i in range(len(a)):
    time -= a[i]
    if time < 0:
        time += a[i]
        break
    aOnly += 1
a_idx = i-1
b_idx = -1
if time > 0:
    for i in range(len(b)):
        time -= b[i]
        if time < 0:
            break
        aOnly += 1
    b_idx = i-1
    ans = aOnly
    time += b[i]

else:
    ans = aOnly
    time += a[i]

ans_kari = ans
for i in range(a_idx, -1, -1):
    time += a[i]
    ans_kari -= 1
    for j in range(b_idx+1, len(b)):
        time -= b[i]
        if time < 0:
            time += b[i]
            b_idx = i-1
            break
        ans_kari += 1
    ans = max(ans, ans_kari)
    
print(ans)

Submission status: Runtime Error

Solution code:
n, m, k = map(int, input().split())
a = list(map(int, input().split())) # list()でリスト化、それを変数に代入。
b = list(map(int, input().split())) # list()でリスト化、それを変数に代入。
ans = 0
kazu = 0
c_a = 0
c_b = 0
time = 0
'''
a.append(10**9 + 1)
b.append(10**9 + 1)


while True:
    if time + min(a[c_a],b[c_b])>k:
        break
    if a[c_a]>b[c_b]:
        time += b[c_b]
        c_b += 1
    elif a[c_a]<b[c_b]:
        time += a[c_a]
        c_a += 1

    ans += 1
'''
a = [0]+a
b = [0]+b

for i in range(1,n+1):
    a[i]= a[i-1]+a[i]
for i in range(1,m+1):
    b[i]= b[i-1]+b[i]

for i in range(0,n+1):
    for j in range(min(0,kazu-i+1),m+1):
        time = a[i] + b[j]
        if time > k:
            break
        kazu = i + j

print(kazu)

Submission status: Runtime Error

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

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


s = []
t = []


for i in range(n):
    #print(k + a[i])
    if len(s) > k:
        break
    elif len(s) < k < len(s) + a[i]:
        s += [i] * (k + 1 - len(s))
    else:
        s += [i] * a[i]

if len(s) <k:
    s += [n] * (k + 1 - len(s))

for i in range(m):
    if len(t) > k:
        break
    elif len(t) < k < len(t) + b[i]:
        t += [i] * (k + 1 - len(t))
    else:
        t += [i] * b[i]

if len(t) <k:
    t += [m] * (k + 1 - len(t))

m = 0

# print(s)
# print(t)

#print(s[-1], t[-1])

if len(s) + len(t) - 2 <= k:
    m = s[-1] + t[-1]
else:
    for i in range(k+1):
        m = max(m, s[i] + t[k-i])

print(m)

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

Submission status: Runtime Error

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

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

cnt = 0

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

    elif a[0] > b[0]:
        k -= b.popleft()
        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.popleft()
                    cnt += 1
                    if k < a[0]:
                        break
                break
            elif a[i] > b[i]:
                for t in range(0, i+1):
                    k -= b[0]
                    b.popleft()
                    cnt += 1
                    if k < a[0]:
                        break
                break
        else:
            if len(a) > len(b):
                for i in range(len(a)):
                    k -= a[0]
                    a.popleft()
                    if k < a[0]:
                        break
            else:
                for i in range(len(b)):
                    k -= b[0]
                    b.popleft()
                    cnt += 1
                    if k < b[0]:
                        break
    if a == deque([]):
        a = [float("inf")]
    if b == deque([]):
        b = [float("inf")]

print(cnt)

Submission status: Runtime Error

Solution code:
N = int(input())

M= int(input())


K=int(input())

arr = input()   # takes the whole line of n numbers
A= list(map(int,arr.split(' '))) 

i=0
la=len(A)


arra = input()   # takes the whole line of n numbers
B= list(map(int,arra.split(' '))) 
lb=len(B)

for x in range(la):
    if(A[x]<K):
        K=K-A[0]
        i=i+1
    for y in range(lb):
    	if(B[y]<K):
        	K=K-B[0]
        	i=i+1

print(i)

Submission status: Runtime Error

Solution code:
#C
from itertools import accumulate
import bisect

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

cnt = 0

a_c = list(accumulate(list(a)))
b_c = list(accumulate(list(b)))
result = []

idx_a = bisect.bisect_left(a_c,k)
idx_a_b = bisect.bisect_left(b_c,k-list(a)[idx_a])
result.append(idx_a + idx_a_b)

idx_b = bisect.bisect_left(b_c,k)
idx_b_a = bisect.bisect_left(a_c,k-list(b)[idx_b])
result.append(idx_b + idx_b_a)

for i in range(n+m):
    if len(a) > 0 and len(b) > 0:
        s,t = a.popleft(),b.popleft()
    elif len(a) > 0:
        s = a.popleft()
        t = float('inf')
    elif len(b) > 0:
        s = float('inf')
        t = b.popleft()
    else:
        break
    if s >= t:
        k -= t
        a.appendleft(s)
    else:
        k -= s
        b.appendleft(t)
    if k < 0:
        break
    else:
        cnt += 1
        #print('a:',a)
        #print('b:',b)
    
result.append(cnt)

print(min(result))

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

l = []
nn, mm = 0, 0
for i in range(n+m):
  if n == nn:
    l.append(b[mm])
    mm+=1
  elif m == mm:
    l.append(a[nn])
    nn += 1
  else:
    l.append(min(a[nn], b[mm]))
    if a[nn] < b[mm]:
      nn += 1
    elif a[nn] > b[mm]:
      mm += 1
    else:
      if b[mm+1] => a[nn+1]:
        nn += 1
      else:
        mm += 1

total = 0
cnt = 0
for i in range(n+m):
  if total+l[i] > k:
    break
  total += l[i]
  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()))
print(n,m,k)

minamo = []
def saiki(i,j,sum):
    if sum > k:
        minamo.append(i+j)
        return

    if i+1 <= n:
        saiki(i+1,j,sum+a[i])
    if j+1 <= m:
        saiki(i,j+1,sum+b[j])
    """
    else:
        print(sum)
        return
    """

saiki(0,0,0)
print(max(minamo))

Submission status: Runtime Error

Solution code:
import bisect

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

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

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

ans = 0
for i in range(len(a)):
  tmp = i + 1 + bisect.bisect(b_sum, k - a_sum[i])
  ans = max(ans, tmp)
    if a_sum[i] > k:
    break

print(ans)

Submission status: Runtime Error

Solution code:
#C
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
i = 0
j = 0
for _ in range(n+m)
    # print(i,j,k)
    if n <= i:#
        k -= b[j]
        j += 1
        if k < 0:
            break
        ans += 1
        if k == 0:
            break
        continue
    elif n <= j:
        k -= a[i]
        i += 1
        if k < 0:
            break
        ans += 1
        if k == 0:
            break
        continue
    if a[i] < b[j]:
        k -= a[i]
        i += 1
    else:
        k -= b[j]
        j += 1
    if k < 0:
        break
    ans += 1
    if k == 0:
        break
print(ans)

Submission status: Runtime Error

Solution code:
import sys
from collections import deque


def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

N,M,K = LI()
A = LI()
B = LI()
a = deque(A)
b = deque(B)
ans = 0
while True:
    if len(a) == 0 and len(b) == 0:
        break
    elif len(a) == 0 and len(b) > 0:
        K = K - b.popleft()
    elif len(a) > 0 and len(b) == 0:
        K = K - a.popleft()
    elif len(a) > 0 and len(b) > 0:
        if a[0] > b[0]:
            K = K - b.popleft()
        elif a[0] < b[0]:
            K = K - a.popleft()
        elif a[0] == b[0]:
            if a[1] > b[1]:
                K = K - b.popleft()
            elif a[1] < b[1]:
                K = K - a.popleft()
            elif a[1] == b[1]:
                if a[2] >= b[2]:
                    K = K - b.popleft()
                elif a[2] < b[2]:
                    K = K - a.popleft()
    if K < 0:
        break
    elif K >= 0:
        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()))

#累積和
sa = [0]*len(a)
sa[0] = a[0]
for i,k in enumerate(a):
    if i == 0:continue
    sa[i] = sa[i-1] + a[i]
sb = [0]*len(b)
sb[0] = b[0]
for i,k in enumerate(b):
    if i == 0:continue
    sb[i] = sb[i-1] + b[i]

ai = len(a)-1
bi = len(b)-1

minu = sa[ai] + sb[bi]
while(minu > K):
    if (sa[ai] - sa[ai-1]) > (sb[bi] - sb[bi-1]):
        ai -= 1
        minu = sa[ai] + sb[bi]
    else:
        bi -= 1
        minu = sa[ai] + sb[bi]

print(sa)
print(sb)
print(ai + bi + 2)

Submission status: Runtime Error

Solution code:
from sys import stdin
from itertools import accumulate

n, m, k = [int(x) for x in stdin.readline().rstrip().split()]
a = [int(x) for x in stdin.readline().rstrip().split()]
b = [int(x) for x in stdin.readline().rstrip().split()]

goukei = 0
kaisu = 0
new = []
for i in range(n + m):
  if goukei <= k:
    if len(a) == 0:
      goukei += b[0]
      b.pop(0)
      kaisu += 1
    elif len(b) == 0:
      goukei += a[0]
      a.pop(o)
      kaisu += 1
    elif a[0] <= b[0]:
      goukei += a[0]
      a.pop(0)
      kaisu += 1
    elif a[0] > b[0]:
      goukei += b[0]
      b.pop(0)
      kaisu += 1
print(kaisu)

Submission status: Runtime Error

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

dp=[[0 for j in range(K)] for i in range(max(N,M))]
if(N<M):
    ma=M
    mi=N
    turn='M'
else:
    ma=N
    mi=M
    turn='N'

if(turn='M')
    for i in range(nn):
        for j in range(K+1):
            if(mi<i):
                if(B[i]<j):
                    dp[i+1][j]=max(dp[i][j-B[i]]+1,dp[i][j])
                else:
                    dp[i+1][j]=dp[i][j]
            else:
                if(A[i]<j and B[i]<j):
                    dp[i+1][j]=max(dp[i][j-A[i]-B[i]]+2,dp[i][j])

                elif(A[i]<j):
                    dp[i+1][j]=max(dp[i][j-A[i]+1,dp[i][j]])
                elif(B[i]<j):
                    dp[i+1][j]=max(dp[i][j-B[i]]+1,dp[i][j])
                else:
                    dp[i+1][j]=dp[i][j]
else:
    for i in range(nn):
        for j in range(K+1):
            if(mi<i):
                if(A[i]<j):
                    dp[i+1][j]=max(dp[i][j-A[i]]+1,dp[i][j])
                else:
                    dp[i+1][j]=dp[i][j]
            else:
                if(A[i]<j and B[i]<j):
                    dp[i+1][j]=max(dp[i][j-A[i]-B[i]]+2,dp[i][j])

                elif(A[i]<j):
                    dp[i+1][j]=max(dp[i][j-A[i]+1,dp[i][j]])
                elif(B[i]<j):
                    dp[i+1][j]=max(dp[i][j-B[i]]+1,dp[i][j])
                else:
                    dp[i+1][j]=dp[i][j]

print(dp[nn][K])

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
co =0
flag = True
while time < k:
    if sum(a) + sum(b) <= k:
        print(len(a)+len(b))
        flag = False
        break
    elif len(a) ==0:
        time += b[0]
        co += 1
        p = b[0]
        b.remove(p)
    elif a[0] >= b[0]:
        time += b[0]
        co += 1
        p = b[0]
        b.remove(p)
    elif len(b) ==0:
        time += a[0]
        co += 1
        q = a[0]
        a.remove(q)
    else :
        time += a[0]
        co +=1
        q = a[0]
        a.remove(q)
if flag:
    print(co - 1)

Submission status: Runtime Error

Solution code:
import copy
N,M,K = map(int,input().split())
A = [0]*N
B = [0]*M
A = list(map(int,input().split()))
B = list(map(int,input().split()))
a = 0
b = 0

tmp = 0
ans = 0
# for i in range(200000):
#     if A[a] < B[b]:
#         if tmp <= K:
#             tmp += A[a]
#             ans += 1
#             a += 1
#     elif A[a] > B[b]:
#         if tmp <= K:
#             tmp += B[b]
#             ans += 1
#             b += 1
#     if a > len(A)-1 or b > len(B)-1:
#         break
fa = False
fb = False

while len(A) > 0 or len(B) > 0:
    AA = copy.copy(A)
    BB = copy.copy(B)
    if len(AA) != 0:
        a = AA.pop(0)
        fa = True
    if len(BB) != 0:
        b = BB.pop(0)
        fb = True
    if fa and fb:
        if a<=b:
            tmp += a
            ans += 1
            A = AA
        else:
            tmp += b
            ans += 1
            B = BB
    else:
        if fa:
            tmp += a
            ans += 1
            A = AA
        if fb:
            tmp += b
            ans += 1
            B = BB

    if tmp > K:
        break
    fa = False
    fb = False
    
    print(tmp,A,B)
    
if len(A)==0 and len(B)==0:
    print(ans)
else:
    print(ans-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()))
temp=0
an,bn=0,0
ans=0
while temp<=k:
    if an==n and bn==m: break
    if an>=n:
        temp+=b[bn]
        if temp>k: break
        bn+=1
    elif bn>=m:
        temp+=a[an]
        if temp>k: break
        an+=1
    else:
        if a[an]<b[bn]:
            temp+=a[an]
            if temp>k: break
            an+=1
        elif a[an]>b[bn]:
            temp+=b[bn]
            if temp>k: break
            bn+=1
        else:
            cnt=0
            while True:
                if a[an]<b[bn]:
                    temp+=a[an]
                    an+=1
                    bn-=cnt
                    break
                elif a[an]>b[bn]:
                    temp+=b[bn]
                    bn+=1
                    an-=cnt
                    break
                if an==n:
                    break
                if bn==m:
                    break
                temp+=a[an]
                if temp>k:
                    break
                an+=1
                bn+=1
                ans+=1
                cnt+=1
            if temp>k:
                break
    ans+=1
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()))
time = 0
ans = 0

def f(i,j,time):
  if i >= len(A) and j < len(B):
    return 1 + f(i, j+1, time+B[j])
  elif i < len(A) and j >= len(B):
    return 1 + f(i+1, j, time+A[i])
  elif i >= len(A) and j >= len(B):
    return 0
  else:
    timeA = A[i] + time
    timeB = B[i] + time
    if timeA > K and timeB > K:
      return 0
    elif timeA > K and timeB <= K:
      return 1 + f(i, j+1, time+B[j])
    elif timeA <= K and timeB > K:
      return 1 + f(i+1, j, time+A[i])
    else:
      countA = 1 + f(i+1, j, timeA)
      countB = 1 + f(i, j+1, timeB)
      return max(countA, countB)

ans = f(0, 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()))
a_sum = sum(a)
b_sum = sum(b)
min_count = 0
count = 0
def a_func():
  c = a[0]
  del a[0]
  return c

def b_func():
  c = b[0]
  del b[0]
  return c

while True:
  if min_count >= k or (len(a) == 0 and len(b) == 0) or (a[0] > k and b[0] > k):
    print(count)
    break
  elif a_sum + b_sum <= k:
    print(n+m)
    break
  else:
    if len(a) != 0 and len(b) != 0:
      if a[0] > b[0]:
        min_count += b_func()
      else:
        min_count += a_func()
    else:
      try:
        min_count += a_func()
      except:
        min_count += b_func()
    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()))

i = 0
j = 0
c = []

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

while True:
    if a[i] <= b[j]:
        c.append(a[i])
        i += 1
        if i == n -1:
            if j == m -1:
                break
            else:
                for l in range(j, m):
                    c.append(b[l])
                break
    else:
        c.append(b[j])
        j += 1
        if j == m -1:
            if i == n -1:
                break
            else:
                for l in range(i, m):
                    c.append(a[l])
                break

for i in range(len(c)):
    k -= c[i]
    if k < 0:
        print(i)
        exit()
    if i == len(c)-1:
        print(n+m)
        exit()

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

a.append(10 ** 10)
b.append(10 ** 10)
na = a.popleft()
nb = b.popleft()

cnt = 0
min = 0
while min <= k:
    if na <= nb:
        cnt += 1
        min += na
        na = a.popleft()
    elif na > nb:
        cnt += 1
        min += nb
        nb = b.popleft()
    elif na == 10 ** 10 and nb == 10 ** 10:
        cnt += 1
        break
print(cnt-1)

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

while b != len(B) or a != len(A):
    if b == len(B) and A[a]+t <= K:
        t += A[a]
        ans += 1
        a += 1
    # else:
    #     break
    elif a == len(A) and B[b]+t <= K:
        t += B[b]
        ans += 1
        b += 1
    # else:
    #     break
    elif B[b] >= A[a] and A[a]+t <= K:
        t += A[a]
        ans += 1
        a += 1
    else:
        t += B[b]
        ans += 1
        b += 1


print(ans)

Submission status: Runtime Error

Solution code:
def main():
    n, m, k = list(map(int, input().split()))
    a_books = list(map(int, input().split()))
    b_books = list(map(int, input().split()))

    sumbooks = sum(a_books) + sum(b_books)
    count = len(a_books) + len(b_books)
    if a_books[0] > k and b_books[0] > k:
        print(0)
        exit()
    elif sumbooks <= k:
        print(len(a_books) + len(b_books))
        exit()

    while sumbooks > k:
        if len(a_books) and a_books[-1] >= b_books[-1]:
            sumbooks -= a_books.pop(-1)
        elif len(b_books) and b_books[-1] >= a_books[-1]:
            sumbooks -= b_books.pop(-1)

        if sumbooks <= k:
            break
        count -= 1
    print(count)


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

d=[0]*(k+1)

xx=[0]*(k+1)
yy=[0]*(k+1)

x=0
y=0

cnt=0

for i in range(k+1):

  if yy[i]<m:
    if (i+b[yy[i]])<=k:
      if d[i+b[yy[i]]]==0 or d[i+b[yy[i]]]<(d[i]+b[yy[i]]):
        d[i+b[yy[i]]]=d[i]+b[yy[i]]
        yy[i+b[yy[i]]]=yy[i]+1
        xx[i+b[yy[i]]]=xx[i]
        if cnt==0 or cnt<(yy[i+b[yy[i]]]+xx[i+b[yy[i]]]):
          cnt=yy[i+b[yy[i]]]+xx[i+b[yy[i]]]

  if xx[i]<n:
    if (i+a[xx[i]])<=k:
      if d[i+a[xx[i]]]==0 or d[i+a[xx[i]]]<(d[i]+a[xx[i]]):
        d[i+a[xx[i]]]=d[i]+a[xx[i]]
        xx[i+a[xx[i]]]=xx[i]+1
        yy[i+a[xx[i]]]=yy[i]
        if cnt==0 or cnt<(xx[i+a[xx[i]]]+yy[i+a[xx[i]]]):
          cnt=xx[i+a[xx[i]]]+yy[i+a[xx[i]]]

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()))
a.append(100000000000)
a.append(100000000000)
i, j, ans = 0, 0, 0
while k > 0:
    if i == n and j == m: break
    if a[i] < b[j]:
        k -= a[i]
        i += 1
    else:
        k -= b[j]
        j += 1
    if k >= 0:
        ans += 1
print(ans)

Submission status: Runtime Error

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

# sys.setrecursionlimit(10**6)

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

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



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

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





		

if __name__ == "__main__":
	solve()

Submission status: Runtime Error

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

max_num = 0


def dfs(curr_a, curr_b, choice, time_required, count):
    global max_num
    if choice == 0:
        if curr_a >= N - 1:
            if max_num < count:
                max_num = count
            return
        else:
            curr_a += 1
            time_required += a[curr_a]
    else:
        if curr_b >= M - 1:
            if max_num < count:
                max_num = count
            return
        else:
            curr_b += 1
            time_required += b[curr_b]
    if time_required > K:
        if max_num < count:
            max_num = count
        return
    count += 1
    dfs(curr_a, curr_b, 0, time_required, count)
    dfs(curr_a, curr_b, 1, time_required, count)

curr_a = -1
curr_b = -1
time_required = 0
count = 0
dfs(curr_a, curr_b, 0, time_required, count)
curr_a = -1
curr_b = -1
time_required = 0
count = 0
dfs(curr_a, curr_b, 1, time_required, count)

print(max_num)

Submission status: Runtime Error

Solution code:
import sys
import numpy as np
import math
import collections
from collections import deque 
from functools import reduce

# input = sys.stdin.readline

n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ai, bi = 0, 0
a.append(10**10)
b.append(10**10)
cnt = 0
tt = 0
cnt = 0
tt = 0
while True:
    if a[ai] == b[bi]:
        while True:
            ai += 1
            bi += 1
            if a[ai] == b[bi]:
                continue
            if a[ai] < b[bi]:
                flg = 'a'
                break
            else:
                flg = 'b'
                break
    elif a[ai] > b[bi]:
        flg = 'b'
    else:
        flg = 'a'

    if flg == 'b':
        if (tt + b[bi] > k):
            break
        cnt += 1
        tt += b[bi]
        bi += 1
    else:
        if (tt + a[ai] > k):
            break
        cnt += 1
        tt += a[ai]
        ai += 1
        
print(cnt)