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:
N, M, K = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))
Acount = -1
Bcount = -1
tmpA = []
tmpB = []
tmp = 0
for i in range(N):
    tmp += A[i]
    if tmp > K:
        break
    tmpA.append(tmp)
tmp = 0
for i in range(M):
    tmp += B[i]
    if tmp > K:
        break
    tmpB.append(tmp)

if len(tmpA) > 0 and len(tmpB):
    res = 0
    Bcount = -1
    Acount = len(tmpA) - 1
    while Acount > -1 and Bcount < M:
        for i in range(Bcount, M):
            if tmpA[Acount] + tmpB[i] > K:
                break
            Bcount = i
        tmpRes = Acount + Bcount + 2 if Bcount > -1 else Acount + 1
        if res < tmpRes:
            res = tmpRes
        Acount -= 1
    print(res)
elif len(tmpA) > 0:
    print(len(tmpA))
elif len(tmpB) > 0:
    print(len(tmpB))
else:
    print(0)

Submission status: Runtime Error

Solution code:
a = input()
n = input()
m = input()
first = a.split(" ")
N = n.split(" ")
M = m.split(" ")
overflow = 0
for i in N:
    if int(i) >= 10**9:
        overflow = 1
        break
        
for i in M:
    if int(i) >= 10**9:
        overflow = 1
        break
if int(first[0]) >= 200000 or int(first[1]) >= 200000:
    overflow = 1
if int(first[2]) >= 10**9:
    overflow = 1
         
if overflow == 0:
    totaltime = 0
    count = 0
    for i in range(int(first[1]) + int(first[0])):
      if int(N[0]) < int(M[0]):
        totaltime += int(N[0])
        N.remove(N[0])
        count += 1
      elif int(M[0]) < int(N[0]):
        totaltime += int(M[0])
        M.remove(M[0])
        count += 1
      if totaltime + min(int(N[0]),int(M[0])) > int(first[2]):
          break
    print(count)
else:
    print(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()))

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

if k < a[0] and k >= b[0]:
    for i in range(len(b)):
        k -= b[i]
        if k < 0:
            print(i)
            exit()
        if i == len(b)-1:
            print(m)
            exit()

if k >= a[0] and k < b[0]:
    for i in range(len(a)):
        k -= a[i]
        if k < 0:
            print(i)
            exit()
        if i == len(a)-1:
            print(n)
            exit()

i = 0
j = 0
c = []

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(len(c))
        exit()

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[47]:


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


# In[56]:


A_sum = [sum(A[:i]) for i in range(N+1)]
B_sum = [sum(B[:i]) for i in range(N+1)]
ans = 0
for a in range(N+1):
    if A_sum[a] > K:
        break
    for b in range(M+1):
        if A_sum[a] + B_sum[b] > K:
            break
        ans = max(ans, a+b)
print(ans)


# In[ ]:

Submission status: Runtime Error

Solution code:
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a.append(10**9+1)
b.append(10**9+1)
idx_a = 0
idx_b = 0
c = 0
while k > 0:
    c += 1
    if a[idx_a] > b[idx_b]:
        k -= b[idx_b]
        idx_b += 1
    elif a[idx_a] < b[idx_b]:
        k -= a[idx_a]
        idx_a += 1
    else:
        tmpa = idx_a
        tmpb = idx_b
        while tmpa < len(a) and tmpb < len(b) and a[tmpa] == b[tmpb]:
            tmpa += 1
            tmpb += 1
        if a[tmpa] < b[tmpb]:
            k -= a[idx_a]
            idx_a += 1
        else:
            k -= b[idx_b]
            idx_b += 1
if k < 0:
    c -= 1
print(c)

Submission status: Runtime Error

Solution code:
import numpy as np

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

if len(A)>len(B):
    tmp = B[:]
    B = A
    A = tmp

Acum = np.cumsum(np.array(A))
Bcum = np.cumsum(np.array(B))

maxnum = 0
for n in range(N,-1,-1):
    if n>0:
        tA = Acum[n-1]
    else:
        tA = 0
    if tA>K:
        continue
    
    for m in range(M,-1,-1):
        if m>0:
            tB = Bcum[m-1]
        else:
            tB = 0
        
        if tA+tB<=K:
            num = n+m
            if num>maxnum:
                maxnum = num
            break

print(maxnum)

Submission status: Runtime Error

Solution code:
def recurse(A,B,i,j,suma,count):
    if(suma>K):
        return count
    elif(i==N):
        temp=suma+B[j]
        if(temp>K):
            return count
        else:
            if(j<M-1):
                return recurse(A,B,i,j+1,suma+B[j],count+1)
            else:
                if(suma+B[j]<=K):
                    return count+1
                else:
                    return count
    elif(j==M):
        temp=suma+A[i]
        if(temp>K):
            return count
        else:
            if(i<N-1):
                return recurse(A,B,i+1,j,suma+A[i],count+1)
            else:
                if(suma+A[i]<=K):
                    return count+1
                else:
                    return count
    elif(A[i]<B[j]):
        temp=suma+A[i]
        if(temp>K):
            return count
        else:
            suma+=A[i]
            count+=1
            return recurse(A,B,i+1,j,suma,count)
    elif(A[i]==B[j]):
        temp=suma+A[i]
        if(temp>K):
            return count
        else:
            return max(recurse(A,B,i+1,j,suma+A[i],count+1),recurse(A,B,i,j+1,suma+B[j],count+1))
    else:
        temp=suma+B[i]
        if(temp>K):
            return count
        else:
            return recurse(A,B,i,j+1,suma+B[j],count+1)

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

Submission status: Runtime Error

Solution code:
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
now = b[0]
ans = 0
for i in range(len(a)+len(b)):
    if now >= k:
        break
    else:
        if len(a) == 0:
            ans += 1
            now += b[0]
            b.remove(b[0])
        elif len(b) == 0:
            ans += 1
            now += a[0]
            a.remove(a[0])
        else:
            if a[0] < now:
                ans += 1
                now += a[0]
                a.remove(a[0])
            else:
                now = b[1]
                ans += 1
                now += b[0]
                b.remove(b[0])
print(ans)

Submission status: Runtime Error

Solution code:
a,b,c=map(int,input().split())
li_a=list(map(int,input().split()))
li_b=list(map(int,input().split()))

count=0
x=0
y=0
while c>=0:
  if a!=0 and b!=0:
    
    c=c-min(li_a[x],li_b[y])
    if c-min(li_a[x],li_b[y])<0:
      break
    if li_a[x]>=li_b[y]:
      x+=1
      a-=1
    else:
      y+=1
      b-=1

    count+=1
  if a!=0 and b==0:
    c=c-li_a[x]
    x+=1
    a-=1
    if c<0:
      break
    count+=1
  if a==0 and b!=0:
    y+=1
    b-=1
    c=c-li_b[y]
    if c<0:
      break
    count+=1
  if a==0 and b==0:
    break
    
    
print(count)

Submission status: Runtime Error

Solution code:
nums=input()
nums=nums.split()
M,N,K = int(nums[0]),int(nums[1]),int(nums[2])

al=input()
al2=al.split()
for n in range(M):
    al2[n] = int(al2[n])

bl=input()
bl2=bl.split()
for n in range(N):
    bl2[n] = int(bl2[n])

count=0

ai=0
bi=0

while 1:
    if al2[ai] <= bl2[bi] and al2[ai] <= K:
        K = K-al2[ai]
        ai+=1
        count+=1
        

    elif al2[ai] > bl2[bi] and bl2[bi] <= K:
        K = K-bl2[bi]
        bi+=1
        count+=1
    else:
        break

print(count)

Submission status: Runtime Error

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

sum_a={}
tmp=0
for i in range(len(a)):
    tmp+=a[i]
    if tmp>=k:
        break
    sum_a.update({i+1:tmp})
    
sum_b={}
tmp=0
for i in range(len(b)):
    tmp+=b[i]
    if tmp>=k:
        break
    sum_b.update({i+1:tmp})

c_a = max(sum_a.keys())
c_b = max(sum_b.keys())
tmp = sum_a[c_a]+sum_b[c_b]
if tmp<=k:
    print(c_a+c_b)
else:
    for i in range(max(c_a,c_b)+1):
        if sum_a[c_a]>=sum_b[c_b]:
            c_a-=1
        else:
            c_b-=1
        if sum_a[c_a]+sum_a[c_b]<=k:
            print(c_a+c_b)
            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()))

read = 0

i = 0
j = 0


while K - A[i] >= 0 or K - B[j] >= 0:
    if N > 0 and M > 0:
        if A[i] < B[j]:
            N -= 1
            read += 1
            K -= A[i]
            i += 1
            A.append(10**9+1)
        elif A[i] > B[j]:
            M -= 1
            read += 1
            K -= B[j]
            j += 1
            B.append(10**9+1)
        else:
            i2 = i
            j2 = j
            while A[i2] == B[j2]:
                if A[i2] < B[j2]:
                   N -= 1
                   read += 1
                   K -= A[i]
                   i += 1
                   A.append(10**9+1)
                   break
                elif A[i2] > B[j2]:
                   M -= 1
                   read += 1
                   K -= B[j]
                   j += 1
                   B.append(10**9+1)
                   break
                else:
                   i2 += 1
                   j2 += 1
    elif N <= 0 and M > 0:
        M -= 1
        read += 1
        K -= B[j]
        j += 1
        B.append(10**9+1)
    elif N > 0 and M <= 0:
        N -= 1
        read += 1
        K -= A[i]
        i += 1
        A.append(10**9+1)
    else:
        break

print(read)

Submission status: Runtime Error

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


b_book = list(map(int,input().split()))

total = 0
count = 0
A = 0
B = 0
for i in range(N):
  if a_book[i]>=K:
    A = 1
for i in range(M):
  if b_book[i]>=K:
    B = 1

if A==1 or B==1:
  count = 0
elif sum(a_book)+sum(b_book)<=K:
    count = N + M
else:
  i = 0
  while K>=total:
    if a_book[i] <= b_book[i]:
      total += a_book[i]
      count+=1
      a_book.pop(i)
    else:
      total += b_book[i]
      count+=1
      b_book.pop(i)
    
if count==0:
  print(count)
elif count<N+M:
  print(count-1)
else:
  print(count)

Submission status: Runtime Error

Solution code:
# C - Tsundoku
import sys
sys.setrecursionlimit = 10**18

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())
la = len(A)
lb = len(B)
T = 0
r = 0
ans = 0
def dfs(i, j, T, r):
    global ans
    ans = max(ans, r)
    if i < la and T+A[i] <= K:
        dfs(i+1, j, T+A[i], r+1)
    if j < lb and T+B[j] <= K:
        dfs(i, j+1, T+B[j], r+1)
    return
    
dfs(0, 0, T, 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()))

i = 0
j = 0
results = []

class Node:
  def __init__(self, i, j, cur, total):
    self.i = i
    self.j = j
    self.cur = cur
    self.total = total
  def child(self):
    results = []
    if self.i + 1 < N and self.cur + A[i+1] <= K:
      results.append(Node(self.i+1, self.j, self.cur+A[self.i+1], self.total + 1))
    if self.j + 1 < M and self.cur + B[j+1] <= K:
      results.append(Node(self.i, self.j+1, self.cur+B[self.j+1], self.total + 1))
    return results
  def __repr__(self):
    return str([self.i, self.j, self.cur, self.total])
  
nodes = [Node(-1,-1,0,0)]
max_total = 0
while True:
  if len(nodes) == 0: break
  cur = nodes.pop()
  if cur.total > max_total: max_total = cur.total
  children = cur.child()
  nodes.extend(children)
  
print(max_total)

Submission status: Runtime Error

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

a.sort()
b.sort()
s,c = 0,0
if a[0] < k or b[0] < k:
    for i in range(m+n):
        if i< len(a):
            if s < k and s+a[i]<k:
                s += a[i]
                c += 1
        if i<len(b)
            if s<k and s+b[i] < k:
                s += b[i]
                c += 1
else:
    c = 0
print(c)

Submission status: Runtime Error

Solution code:
import collections
def C():
    N, M, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    memo = {}
    def dfs(i, j, k):
        if (i, j, k) in memo:
            return memo[(i, j, k)]
        if i >= N and j >= M:
            return 0
        if i >= N or k < A[i]:
            ret = 0
            while j < M and k >= B[j]:
                ret += 1
                k -= B[j]
                j += 1
            memo[(i, j, k)] = ret
            return ret
        if j >= M or k < B[j]:
            ret = 0
            while i < N and k >= A[i]:
                ret += 1
                k -= A[i]
                i += 1
            memo[(i, j, k)] = ret
            return ret
        ret = max(1+dfs(i+1, j, k-A[i]), 1+dfs(i, j+1, k-B[j]))
        memo[(i, j, k)] = ret
        return ret
    print(dfs(0, 0, K))
        
C()

Submission status: Runtime Error

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

A = list(map(int, input().split()))
B = list(map(int, input().split()))
maxsum = sum(A)+sum(B)
sumsum=0

if maxsum<K:
    print(S)
elif K < maxsum-K:
    
    for s in range(S):
 
        if len(A)==0 or A[0]>B[0]:
           sumsum+=B[0]
           B.pop(0)
        elif len(B)==0 or A[0]<B[0]:
          sumsum+=A[0]
          A.pop(0)
        if K <= sumsum:
            print(s)
            break
else:
    for s in range(S):
 
        if len(A)==0 or A[len(A)-1]>B[len(B)-1]:
           maxsum-=B[len(B)-1]
           B.pop()
        elif len(B)==0 or A[len(A)-1]<B[len(B)-1]:
          maxsum-=A[len(A)-1]
          A.pop()
        if K >= maxsum:
            print(s)
            break

Submission status: Runtime Error

Solution code:
from collections import deque
a=deque()
b=deque()
nmk=[int(i) for i in input().split()]
for i in input().split():
  a.append(int(i))
for i in input().split():
  b.append(int(i))
k=nmk[2]
cnt=0
while k>=0:
  if len(b)==0:
    k-=a.pop()
    cnt+=1
  elif len(a)==0:
    k-=b.pop()
    cnt+=1
  elif a[0]<=b[0]:
    k-=a.pop()
    cnt+=1
  elif a[0]>b[0]:
    k-=b.pop()
    cnt+=1
  print(a,b,k)
  if k<0:
    cnt-=1
  if len(a)==0 and len(b)==0:
    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()))

ans = 0
t = 0
a = 0
b = 0

while True:
    if not t + A[a] > K:
        t += A[a]
        a += 1
        # print(a, t, A[a-1])
        if a >= N:
            break
    else:
        break

flug = True     #全探索を行うか
while True:
    if not t + B[b] > K:
        t += B[b]
        b += 1
        # print(b, t, B[b-1])
        if b >= M:
            flug = False
            break
    else:
        break

# print(a, b, flug)
ans = a + b
flug2 = True    #処理を続けるか
if flug:
    while True:
        a -= 1
        t -= A[a]
        while True:
            if not t + B[b] > K:
                t += B[b]
                b += 1
                if b >= M:
                    flug2 = False
                    break
            else:
                break
        ans = max(ans, a + b)
        if not flug2:
            break
print(ans)

Submission status: Runtime Error

Solution code:
n,m,k =map(int,input().split())
arra = [int(x) for x in input().split()]
# arra = arra[::-1]

arrb = [int(x) for x in input().split()]
# arrb=arrb[::-1]
topa=0
topb=0
count=0
total = 0
while(total<=k):
    if(arra[0]>k and arrb[0]>k):
        break
    if(topa==n and topb==m):
        break
    if(topa==n):
        total+=arrb[topb]
        topb+=1
        count+=1
        continue
    elif(topb==m):
        total+=arra[topa]
        topb+=1
        count+=1
        continue
        
    if(arra[topa]<=arrb[topb]):
        total+=arra[topa]
        topa+=1
        count+=1
    else:
        total+=arrb[topb]
        topb+=1
        count+=1
print(count)

Submission status: Runtime Error

Solution code:
import itertools
N, M, K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
Ac = list(itertools.accumulate(A))
Bc = list(itertools.accumulate(B))
amax, bmax = 0, 0
for i in range(len(A)):
    if Ac[i] <= K:
        amax = i + 1
    else: break
if amax == 0:
    At = 0
else:
    At = Ac[amax - 1]
for i in range(len(B)):
    if Bc[i] + At <= K:
        bmax = i + 1
    else: break
ans = amax + bmax
if amax > 0:
    for i in range(1, amax):
        anow = amax - i
        At = Ac[anow - 1]
        for j in range(bmax-1, len(B)):
            if Bc[j] + At <= K:
                bmax = j + 1
            else: break
        ans = max(ans,anow+bmax)
        
for i in range(len(B)):
    if Bc[i] <= K:
        bmax = i + 1
    else: break
ans = max(ans,bmax)
if bmax == 0:
    Bt = 0
else:
    Bt = Bc[bmax - 1]
for i in range(len(A)):
    if Ac[i] + Bt <= K:
        amax = i + 1
    else: break
if bmax > 0:
    for i in range(1, bmax):
        bnow = bmax - i
        Bt = Bc[anow - 1]
        for j in range(bmax-1, len(B)):
            if Bc[j] + At <= K:
                bmax = j + 1
            else: break
        ans = max(ans,anow+bmax)
print(ans)

Submission status: Runtime Error

Solution code:
N,M,K=map(int,input().split())
A=list(int,input.split())
B=list(int,input().split())
C=0
D=0
E=0
F=0
for i in range(0,K):
    for e in range(0,K):
        i=i+0
        e=e+0
        if C<=K:
            F=C+A[i]
            E=C+B[e]
            if F<=K and E<=K
                if N>=1 and M==0 and A[i]<=K:
                    C=C+A[i]
                    D=D+1
                    i=i+1
                elif N==0 and M<=1 and B[e]<=K:
                    C=C+B[e]
                    D=D+1
                    e=e+1
                elif N>=1 and M>=1 and A[i]<=K and B[e]>K:
                    C=C+A[i]
                    D=D+1
                    i=i+1
                elif N>=1 and M>=1 and A[i]>K and B[e]<=K:
                    C=C+B[e]
                    D=D+1
                    e=e+1
                elif N>=1 and M>=1 and A[i]<=K and B[e]<=K:
                    if A[i]<=B[e]:
                        C=C+A[i]
                        D=D+1
                        i=i+1
                    elif A[i]>=B[e]:
                        C=C+B[e]
                        D=D+1
                        e=e+1
                    else:
                        break
                else:
                    break
            elif F<=K and E>K:
                if N>=1:
                    C=C+A[i]
                    D=D+1
                    i=i+1
                else:
                    break
            elif F>K and E<=K:
                if M>=1:
                    C=C+B[i]
                    D=D+1
                    e=e+1
                else:
                    break
            else:
                break
        else:
            break
            
print(D)

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=0
b=0
count=0
kazu=0
time=0
A1=list()
A1.append(0)
B1=list()
B1.append(0)
mA=0
mB=0
flag=0
if A[0]==1000000000 and B[0]==1000000000:
    flag=-3
    ans=0
if A[0]==1000000000:
    flag=-1
    ans=0
    memo=0
    for i in range(len(B)):
        memo+=B[i]
        if memo<=K:
            ans+=1
        else:
            break
if B[0]==1000000000:
    flag=-2
    ans=0
    memo=0
    for i in range(len(A)):
        memo+=A[i]
        if memo<=K:
            ans+=1
        else:
            break
for i in range(len(A)):
    mA+=A[i]
    if mA<1000000000:
        A1.append(mA)
for i in range(len(B)):
    mB+=B[i]
    if mB<1000000000:
        B1.append(mB)   
Ak=len(A1)-1
Bk=len(B1)-1

count=Ak+Bk
t=0
f=0
if flag==0:
    while True:
        for i in range(f+1):
            if A1[Ak-i]+B1[count-(Ak-i)]<=K:
                ans=count
                t=1
                break
        
        if t==1:
            break
        count-=1
        if count==0:
            ans=0
            break
        f+=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()))

ans = 0
t = 0
a = 0
b = 0

while True:
    if not t + A[a] > K:
        t += A[a]
        a += 1
        # print(a, t, A[a-1])
        if a >= N:
            break
    else:
        break

flug = True     #全探索を行うか
while True:
    if not t + B[b] > K:
        t += B[b]
        b += 1
        # print(b, t, B[b-1])
        if b >= M:
            flug = False
            break
    else:
        break

# print(a, b, flug)
ans = a + b
flug2 = True    #処理を続けるか
if flug:
    while True:
        a -= 1
        t -= A[a]
        while True:
            if not t + B[b] > K:
                t += B[a]
                b += 1
                if b >= M:
                    flug2 = False
                    break
            else:
                break
        ans = max(ans, a + b)
        if not flug2:
            break
print(ans)

Submission status: Runtime Error

Solution code:
# easy input
debug = True

import numpy as np
n, m, k = np.array([int(i) for i in input().split()])
a, b = np.array([int(i) for i in input().split()]), np.array([int(i) for i in input().split()])

i, j = -1, -1
left = []


def smaller(i, j, append):
    #print(i, j)
    tmp = -1
    if j >= m:
        tmp = True
    elif i >= n:
        tmp = False
    elif a[i] < b[j]:
        tmp = True
    elif a[i] > b[j]:
        tmp = False
    else:
        if smaller(i+1, j+1, False):
            tmp = True
        else:
            tmp = False
    if append:
        left.append(tmp)
    return tmp


while i + j != n + m - 2:
    if len(left) < i+j+3:
        smaller(i+1, j+1, True)
    sm = a[i+1] if left[i+j+2] else b[j+1]
    #print(sm)
    if sm > k:
        break
    else:
        k -= sm
        if left[i+j+2]:
            i += 1
        else:
            j += 1
#print(left)
print(i+j+2)

Submission status: Runtime Error

Solution code:
from collections import deque
import math

n,m,k = map(int,input().split())
li_a = deque(list(map(int,input().split())))
li_b = deque(list(map(int,input().split())))
ans,cnt = 0,0

for i in range(n+m+1):
    if n != 0:
        a_num = li_a[0]
    else:
        a_num = math.inf

    if m != 0:
        b_num = li_b[0]
    else:
        b_num = math.inf

    if a_num <= b_num:
        temp = a_num
        li_a.popleft()
        n -= 1
    else:
        temp = b_num
        li_b.popleft()
        m -= 1

    if cnt + temp > k:
        break
    else:
        cnt += temp
        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()))
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 = list(map(int, input().split()))
b_list = list(map(int, input().split()))
a_cnt = 0
b_cnt = 0


def haba(cnt, sum, jogen, list1, list2, a, b):
    if len(list1) - 1 == a and len(list2) == b:
        return a + b + 1

    if sum == jogen:
        return cnt
    elif sum > jogen:
        return cnt - 1

    max_ = cnt
    if a <= len(list1) - 1:
        t1 = haba(cnt+1, sum+list1[a], jogen, list1, list2, a + 1, b)
        if max_ < t1:
            max_ = t1
    if b <= len(list2) - 1:
        t2 = haba(cnt+1, sum+list2[b], jogen, list1, list2, a, b + 1)
        if max_ < t2:
            max_ = t2
    return max_


print(haba(0, 0, k, a_list, b_list, 0, 0))

Submission status: Runtime Error

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

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

if k < a[0] and k >= b[0]:
    for i in range(len(b)):
        k -= b[i]
        if k < 0:
            print(i)
            exit()
        if i == len(b)-1:
            print(m)
            exit()

if k >= a[0] and k < b[0]:
    for i in range(len(a)):
        k -= a[i]
        if k < 0:
            print(i)
            exit()
        if i == len(a)-1:
            print(n)
            exit()

i = 0
j = 0
c = []

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:
import itertools
N, M, K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
Ac = list(itertools.accumulate(A))
Bc = list(itertools.accumulate(B))
amax, bmax = 0, 0
for i in range(len(A)):
    if Ac[i] <= K:
        amax = i + 1
    else: break
if amax == 0:
    At = 0
else:
    At = Ac[amax - 1]
for i in range(len(B)):
    if Bc[i] + At <= K:
        bmax = i + 1
    else: break
ans = amax + bmax
if amax > 0:
    for i in range(1, amax):
        anow = amax - i
        At = Ac[anow - 1]
        for j in range(bmax-1, len(B)):
            if Bc[j] + At <= K:
                bmax = j + 1
            else: break
        ans = max(ans,anow+bmax)
        
for i in range(len(B)):
    if Ac[i] <= K:
        bmax = i + 1
    else: break
ans = max(ans,bmax)
if bmax == 0:
    Bt = 0
else:
    Bt = Bc[bmax - 1]
for i in range(len(A)):
    if Ac[i] + Bt <= K:
        amax = i + 1
    else: break
if bmax > 0:
    for i in range(1, bmax):
        bnow = bmax - i
        Bt = c[anow - 1]
        for j in range(bmax-1, len(B)):
            if Bc[j] + At <= K:
                bmax = j + 1
            else: break
        ans = max(ans,anow+bmax)
print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque
N, M, K = map(int, input().split())
A = deque(map(int, input().split()))
B = deque(map(int, input().split()))

def doku(li, K, time):
  cnt = 0
  for i in range(len(li)):
    if time+li[0] <= K:
      time += li[0]
      li.popleft()
      cnt += 1
    else:
      return cnt
  return cnt

ans = 0
time = 0
while time+min(A[0], B[0]) <= K:
  plus = min(A[0], B[0])
  time += plus
  ans += 1
  if A[0] == B[0]:
    if A[1] < B[1]:
      A.popleft()
      if not A:
        ans += doku(B, K, time)
        break;
    else:
      B.popleft()
      if not B:
        ans += doku(A, K, time)
        break;
  elif A[0] < B[0]:
    A.popleft()
    if not A:
      ans += doku(B, K, time)
      break;
  else:
    B.popleft()
    if not B:
      ans += doku(A, K, time)
      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()))
c=0
t=[]
if sum(a)+sum(b)<=k:
    c=n+m
elif a[0]==k and b[0]==k:
    c=1
elif a[0]>k:
    while k>=b[0]:
        k-=b[0]
        b=b[1:]
        c+=1
elif b[0]>k:
    while k>=a[0]:
        k-=a[0]
        a=a[1:]
        c+=1
else:
    for u in range(n):
        for r in range(m):
            if sum(a[:u+1])+sum(b[:r+1])<=k:
                t.append(u+r+2)
    c=max(t)
print(c)

Submission status: Runtime Error

Solution code:
from collections import deque

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

r = 0
s = 0
t = 0

for i in range(M):
  if Ali[i] <= K-t:
    r += 1
    t += Ali[i]
    
t = 0
for i in range(N):
  if Bli[i] <= K-t:
    r += 1
    t += Bli[i]
    
for i in range(M):
  if Bli[i] <= K-t:
    s += 1
    t += Bli[i]
    
t = 0
for i in range(N):
  if Ali[i] <= K-t:
    s += 1
    t += Ali[i]
    
print(max(r,s))

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.reverse()
B.reverse()

ans = 0
total_time = 0
while A or B:
    if not A:
        t = B.pop()
    elif not B:
        t = A.pop()
    else:
        if A[-1] < B[-1]:
            t = A.pop()
        elif B[-1] < A[-1]:
            t = B.pop()
        else:
            if A[-2] < B[-2]:
                t = A.pop()
            else:
                t = B.pop()
    total_time += t
    if total_time <= K:
        ans += 1
    else:
        break
print(ans)