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:
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 first[0] >= 200000 or first[1] >= 200000:
    overflow = 1
if first[2] >= 10**9:
    overflow = 1
         
if overflow == 0:
    totaltime = 0
    count = 1
    for i in range(int(first[1]) + int(first[0])):
      if i == 6:
        break
      if  len(N) != 0 and len(M) != 0:
        if totaltime + int(N[0]) > int(first[2]) or totaltime + int(M[0]) > int(first[2]):
          break
      if len(N) == 0:
        totaltime += int(M[0])
        M.remove(M[0])
        count += 1
        if totaltime > int(first[2]):
          break
      elif len(M) == 0:
        totaltime += int(N[0])
        N.remove(N[0])
        count += 1
        if totaltime > int(first[2]):
          break
      elif 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
    print(count)
else:
    print(0)

Submission status: Runtime Error

Solution code:
import numpy as np
N, M, K = map(int, input().split())

A = [0] + list(map(int, input().split()))
B = [0] + list(map(int, input().split()))

cum_A = np.cumsum(A)
cum_B = np.cumsum(B)

a = 0
b = 0

flg = False

if cum_A[N] + cum_B[M] <= K:
    ans = N + M
elif cum_A[N - 1] + cum_B[M] <= K:
    ans = N + M - 1
elif cum_A[N] + cum_B[M - 1] <= K:
    ans = N + M - 1
else:
    while True:
        if cum_A[a] <= cum_B[b]:
            if cum_A[a + 1] + cum_B[b] <= K:
                a += 1
            else:
                flg = True
        elif cum_A[a] > cum_B[b]:
            if cum_A[a] + cum_B[b + 1] <= K:
                b += 1
            else:
                flg = True
        
        if flg:
            ans = a + b
            break

print(ans)

Submission status: Runtime Error

Solution code:
def Tsundoku():

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

    value_A = list(map(int,input().split()))
    value_B = list(map(int,input().split()))

    total_time = 0
    total_num = 0
    
    numA = 0
    numB = 0
    
    while total_time < Num[2]:

        if len(value_B) != numB and value_A[numA] > value_B[numB]:
            total_time = total_time + value_B[numB]
            numB = numB + 1
            total_num = total_num + 1
        elif len(value_A) != numA and value_A[numA] <= value_B[numB]:
            total_time = total_time + value_A[numA]
            numA = numA + 1
            total_num = total_num + 1
        elif len(value_A) == numA and len(value_B) != numB:
            break

        if len(value_B) == numB and total_time + value_A[numA] > Num[2]:
            total_num = total_num - 1
            break

        if len(value_A) == numA and total_time + value_B[numB] > Num[2]:
            total_num = total_num - 1
            break

    print(total_num)


if __name__ == '__main__':
    Tsundoku()

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
B = 0

s = 0
for i in range(len(a)):
    if s + a[i] <= k:
        s += a[i]
        A += 1
        #print(s)
    else:
        break
S = k - s
ans = 0
ans += A
#A -= 1
#print(A)

q = 0
for i in range(A+1):
    if q == 0 and S > b[0]:
        pass
    else:
        S += a[A-i]
        A -= 1
    q += 1
    for j in range(B,len(b)):
        if S - b[j] >= 0:
            S -= b[j]
            B += 1
            if A+B >= ans:
                ans = A+B
        else:
            break
            
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
n, m, k = [int(k) for k in input().split()]
a = [int(k) for k in input().split()][::-1]
b = [int(k) for k in input().split()][::-1]
r = 0
c = 0
while True:
    if len(a) == 0 and len(b) == 0:
        break
    if len(a) == 0 or b[-1] < a[-1]:
        r += b.pop(-1)
    else:
        r += a.pop(-1)
    if r <= k:
        c += 1
    else:
        break
    if len(a) == 0 and len(b) == 0:
        break
print(c)

Submission status: Runtime Error

Solution code:
import sys
from collections import deque
N, M, K = map(int, input().split())
A = deque([int(i) for i in input().split()])
B = deque([int(i) for i in input().split()])
if sum(A) + sum(B) <= K:
    print(N+M)
    sys.exit()
cur = 0
ans = 0
while cur < K and ans < N+M:
    if len(A) != 0 and len(B) != 0:
        a = A.popleft()
        b = B.popleft()
        if a <= b:
            cur += a
            if cur > K:
                break
            B.appendleft(b)
            ans += 1
        else:
            cur += b
            if cur > K:
                break
            A.appendleft(a)
            ans += 1
    elif len(A) == 0:
        b = B.popleft()
        cur += b
        if cur > K:
            break
        ans += 1
    elif len(B) == 0:
        a = A.popleft()
        cur += a
        if cur > K:
            break
        ans += 1
print(ans)

from collections import deque
N, M, K = map(int, input().split())
A = deque([int(i) for i in input().split()])
B = deque([int(i) for i in input().split()])

num = 0
cur1, cur2 = 0, 0
while cur1 < N and cur2 < M and num < K:
    if A[cur1] <= B[cur2]:
        num += A[cur1]
        cur1 += 1
    else:
        num += B[cur2]
        cur2 += 1
amari = N-cur1-cur2

Submission status: Runtime Error

Solution code:
n, m, k = [int(s) for s in input().split(" ")]
alist = [int(s) for s in input().split(" ")]
blist = [int(s) for s in input().split(" ")]

def max_inds(aind, bind, cur_time, k):
  if aind >= len(alist) and bind >= len(blist):
    return aind + bind

  if aind < len(alist) and bind < len(blist) and cur_time + alist[aind] > k and cur_time + blist[bind] > k:
    return aind + bind
  else:
    if aind < len(alist) and cur_time + alist[aind] <= k:
      t1 = max_inds(aind + 1, bind, cur_time + alist[aind], k)
    else:
      t1 = aind + bind
    if bind < len(blist) and cur_time + blist[bind] <= k:
      t2 = max_inds(aind, bind + 1, cur_time + blist[bind], k)
    else:
      t2 = aind + bind
    return max(t1, t2)

print(max_inds(0, 0, 0, k))

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
cnt = 0
tt = 0
while True:
    if a[ai] == b[bi]:
        while True:
            aii = ai + 1
            bii = bi + 1
            if a[aii] == b[bii]:
                continue
            if a[aii] < b[bii]:
                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)

Submission status: Runtime Error

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

A=[int(x) for x in input().split()]
B=[int(x) for x in input().split()]

a=0
b=0
ans=0
books=len(A)+len(B)
time=0


while(1):
    if a+b==books or time>=K:
        break
    
    elif (a!=N and b!=M):
        if A[a]>B[b]:
            if time+B[b]<=K:
                time+=B[b]
                b+=1
                ans+=1
            else:
                break
        
        elif B[b]>A[a]:
            if time+A[a]<=K:
                time+=A[a]
                a+=1
                ans+=1
            else:
                break
        
        else:
            if time+A[a]>K:
                break
            else:
                tmpa=a
                tmpb=b
                while(1):
                    tmpa+=1
                    tmpb+=1
                    if A[tmpa]>B[tmpb]:
                        time+=A[b]
                        b+=1
                        ans+=1
                        break
                    
                    elif A[tmpa]<B[tmpb]:
                        time+=A[a]
                        a+=1
                        ans+=1
                        break
            
    elif a==N:
        time+=B[b]
        b+=1
        ans+=1
    elif b==M:
        time+=A[a]
        a+=1
        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()))

L = N + M
dp = [0] * (L + 1)
a = A[0]
b = B[0]
for i in range(1, L + 1):
    if A != [] and B != []:
        dp[i] = dp[i - 1] + min(a, b)
        if A[0] >= B[0]:
            b = iter(B)
        else:
            a = iter(A)
    else:
        if A != []:
            dp[i] = dp[i - 1] + A[0]
            a = iter(A)
        else:
            dp[i] = dp[i - 1] + B[0]
            b = iter(B)

ans = 0
for i in range(L + 1):
    if dp[i] <= K:
        ans = i
print(ans)

Submission status: Runtime Error

Solution code:
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
i = 0
j = 0
time = 0
book = 0
def choose(A, B, i, j):
    if A[i] < B[j]:
        return True
    elif A[i] > B[j]:
        return False
    else:
        if i+1<n and j+1<m:
            return choose(A, B, i+1, j+1)
        elif i+1<n and j+1>=m:
            return True
        elif i+1>=n and j+1<m:
            return False
        else:
            return True

while True:
    if i < n and j < m:
        if choose(a, b, i, j):
            time += a[i]
            if time > k:
                break
            i += 1
            book += 1
        else:
            time += b[j]
            if time > k:
                break
            j += 1
            book += 1
    elif i < n and j >= m:
        time += a[i] 
        if time > k:
            break
        i += 1
        book += 1
    elif i >= n and j < m:
        time += b[j]
        if time > k:
            break
        j += 1
        book += 1
    else:
        break
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()))
def onlyb(y, cnt, s:int):
  if s+b[y]>k:
    return cnt
  else:
    if y==len(b)-1:
      return cnt+1
    else: 
      return onlyb(y+1, cnt+1, s+b[y])
def onlya(x, cnt, s:int):
  if s+a[x]>k:
    return cnt
  else:
    if x==len(a)-1:
      return cnt+1
    else:
      return onlya(x+1, cnt+1, s+a[x])
def dfs(x: int, y: int, cnt: int, s:int):
  if s+a[x]>k and s+b[y]>k: # それぞれ残り１個以上で、両方ともNGだった場合
    return cnt  
  else:
    if a[x]>b[y]:  #ｂの方がみじかいばあい
      if y==len(b)-1: #Bが最後の一冊の場合   
        return onlya(x, cnt+1, s+b[y])
      else:
        return dfs(x, y+1, cnt+1, s+b[y])
    else:
      if x==len(a)-1:  #Aが最後の一冊の場合   
        return onlyb(y, cnt+1, s+a[x])
      else:
        return dfs(x+1, y, cnt+1, s+a[x])
print(dfs(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()))

sa=[0]*n
sb=[0]*m
s=0
for i in range(n):
  s+=a[i]
  sa[i]=s
s=0
for i in range(m):
  s+=b[i]
  sb[i]=s

if sa[n-1]+sb[m-1]<=k:
  print(n+m)
  exit
  
if m<n:
  ans=n if sa[n-1]<=k else 0
else:
  ans=m if sb[m-1]<=k else 0

j=0
for i in reversed(range(n)):
  if sa[i]+sb[j]>k:
    continue
  while j<m and sa[i]+sb[j+1] <= k:
    j+=1
  if sa[i]+sb[j] <= k and ans<i+j+2:
    ans=i+j+2

print(ans)

Submission status: Runtime Error

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

time=0
a_idx=0
b_idx=0
count=0
while time<K:
    count+=1
    try:
        if a_idx+1==N:
            time+=b[b_idx]
            b_idx+=1
            if time>K:
                count-=1
            continue
        elif b_idx+1==M:
            time+=a[b_idx]
            a_idx+=1
            if time>K:
                count-=1
            continue    
        
        if a[a_idx]<b[b_idx]:
            time+=a[a_idx]
            a_idx+=1
            
        elif a[a_idx]>b[b_idx]:
            time+=b[b_idx]
            b_idx+=1
        else:
            try:
                a_com=a_idx
                b_com=b_idx

                if a[a_com]>b[b_com]:
                    time+=a[a_idx]
                    a_idx+=1
                    
                elif a[a_com]<b[b_com]:
                    time+=b[b_idx]
                    b_idx+=1

        except:
                time+=a[a_idx]
                a_idx+=1
        if time>K:
            count-=1            
    except:
        break
print(count)

Submission status: Runtime Error

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

dp = [[-1 for _1 in range(N+1)] for _2 in range(M+1)]
dp[0][0] = 0
for i in range(N):
    dp[0][i+1] = dp[0][i] + A[i]
for j in range(M):
    dp[j+1][0] = dp[j][0] + B[j]
#print(dp)
for m in range(1,M+1):
    for n in range(1,N+1):
        dp[m][n] = dp[m][0] + dp[0][n]
#print(dp)
ans = 0
for a in range(N+M+1):
    for b in range(a+1):
        #print(a,b,dp[M-a+b][N-b])
        if dp[M-a+b][N-b] <= K:
            ans =N+M -a
            print(ans)
            exit()

Submission status: Runtime Error

Solution code:
n,m,k = map(int,input().split())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
import sys
sys.setrecursionlimit(10**5)
maxc = 0
a_i=0
b_i=0
def dfs(a_i,b_i,t,cnt):
    global maxc
    
    if(a_i==n and b_i==m):
        maxc = m+n
        return
    
    if(a_i < n and (A[a_i]+t)<=k):
        dfs(a_i+1,b_i,A[a_i]+t,cnt+1)
    else:
        maxc = max(cnt,maxc)
    if(b_i < m and (B[b_i]+t)<=k):
        dfs(a_i,b_i+1,B[b_i]+t,cnt+1)
    else:
        maxc = max(cnt,maxc)


        
dfs(0,0,0,0)
print(maxc)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3

from collections import deque


def book(A, B, t, cnt):
    if len(A) == 0 and len(B) == 0:
        return cnt
    elif t == K:
        return cnt
    elif t > K:
        return cnt - 1

    a_r = 0
    b_r = 0
    if len(A) != 0:
        a = A.popleft()
        a_r = book(A, B, t+a, cnt+1)
        
    if len(B) != 0:
        b = B.popleft()
        b_r = book(A, B, t+b, cnt+1)

    return max(a_r, b_r)




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)

t = 0
ret = 0

ret = book(A, B, 0, 0)

print(ret)

Submission status: Runtime Error

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

t = 0

while t < k:
    if int(a[0]) <= int(b[0]):
        t += int(a[0])
        ans += 1
        a = list(a)
        a.pop(0)
        
    else:
        t += int(b[0])
        ans += 1
        b = list(b)
        b.pop(0)
       
    if len(a) == 0:
        while len(b) > 0 and t< k:
            t += int(b[0])
            ans += 1
            b = list(b)
            b.pop(0)
           
    if len(a)+len(b) == 0:
        ans += 1
        break

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

dp = [[-1 for i in range(n+2)] for j in range(m+2)]
def find(i, j, sum):
    if sum >= k:
        return 0
    if i < len(a) and j < len(b) and dp[i][j] != -1:
        return dp[i][j]
    dp[i][j] = 0
    if j < len(b) and sum + b [j] <= k :
        dp[i][j] = 1 + find(i , j+1, sum+b[j])
    if i < len(a) and sum + a[i] <= k :
        dp[i][j] = max(dp[i][j], 1 + find(i+1, j, sum+a[i]))
    return dp[i][j]

print(find(0, 0, 0))

Submission status: Runtime Error

Solution code:
#C
from itertools import accumulate
from collections import deque
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:
A=[]
B=[]
books = 0
time = 0
n, m, k = map(int,input().split())
i = n-1
j = m - 1
for i in range(0,len(n)):
    A.append(int(input()))
for i in range(0,len(m)):
    B.append(int(input()))
while time !> k:
    if (i> -1 and j> -1 and A[i]<B[j]) or (i>-1 and j!>-1):
        time = time+A[i]
        i=i-1
        books=books+1
    elif (i> -1 and j> -1 and A[i] !<B[j]) or (j>-1 and i!>-1):
            time = time+A[i]
            i=i-1
            books=books+1
print(books)

Submission status: Runtime Error

Solution code:
n,m,k=map(int,input().split(" "))
a=list(map(int,input().split(" ")))
b=list(map(int,input().split(" ")))
i=0
j=0
d=0
c=0
while(d<=k):
  if(a[i]<b[j]):
    d=d+a[i]
    i=i+1
    c=c+1
  else:
    d=d+b[j]
    j=j+1
    c=c+1
print(c-1)

Submission status: Runtime Error

Solution code:
from fractions import gcd
from collections import Counter, deque, defaultdict
from heapq import heappush, heappop, heappushpop, heapify, heapreplace, merge
from bisect import bisect_left, bisect_right, bisect, insort_left, insort_right, insort
from itertools import accumulate, product, permutations, combinations

def main():
    N, M, K = map(int, input().split())
    A = deque(list(map(int, input().split())))
    B = deque(list(map(int, input().split())))

    time = 0
    res = 0
    same_flag = False

    while True:
        if len(A) != 0:
            tmp1 = A[0]
        else:
            tmp1 = 10 ** 10
        
        if len(B) != 0:
            tmp2 = B[0]
        else:
            tmp2 = 10 ** 10
        
        if time + tmp1 > K and time + tmp2 > K:
            break

        if tmp1 == 10 ** 10 and tmp2 == 10 ** 10:
            break
        elif tmp1 < tmp2:
            tmp_time = A.popleft()
            time += tmp_time
            res += 1
        elif tmp1 == tmp2:
            sumA = sum(A[1:])
            sumB = sum(B[1:])

            if same_flag:
                tmp_time = A.popleft()
                time += tmp_time
                res += 1
            elif sumA < sumB:
                tmp_time = A.popleft()
                time += tmp_time
                res += 1
            elif sumA > sumB:
                tmp_time = B.popleft()
                time += tmp_time
                res += 1
            else:
                tmp_time = A.popleft()
                time += tmp_time
                res += 1
                same_flag = True
        else:
            tmp_time = B.popleft()
            time += tmp_time
            res += 1
        
        if time > K:
            time -= tmp_time
            res -= 1
            break
    
    print(res)

if __name__ == '__main__':
    main()

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

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

count = [[0 for i in range(n + 1)] for j in range(m + 1)]

answer = 0
for i in range(n+2):
    for j in range(m):
        if j != 0:
            count[i][j] = count[i][j-1] + a[j-1]
        if count[i][j] > k:
        #    print(count[i][j])
            break
        if answer < i + j and count[i][j] <= k:
            #print(count[i][j],i,j)
            answer = i + j
    if i == n + 1:
        break
    count[i+1][0] = count[i][0] + b[i]
    if count[i+1][j] > k:
        break 
    #if answer < i + j and count[i][j] <= k:
        #answer = i + j
        #print(count[i][j])

print(answer)

Submission status: Runtime Error

Solution code:
N,M,K = map(int,input().split())
A =list(map(int,input().split()))
B =list(map(int,input().split()))
C = A+B
a = 0
b = 0
min = 0
cnt= 0

if sum(C) <= K:
  print(len(C))
else:
  while min < K:
    if A[a] <= B[b]:
      min += A[a]
      a +=1
      cnt +=1
    else:
      min +=B[b]
      b +=1
      cnt+=1
  print(cnt-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()))

#累積和
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(ai + bi + 2)

Submission status: Runtime Error

Solution code:
# /usr/bin/python3
# -*- coding: utf-8 -*-
from queue import Queue
from queue import LifoQueue as Stack
from math import sqrt, floor, ceil, log2, log10, pi
from fractions import gcd
from itertools import permutations, combinations
from operator import itemgetter
from functools import cmp_to_key


INF=1001001001
__MOD__=(10**9)+7
yn = 'YNeos'
judge = False
cnt = 0
ans = None


def lcm(a, b):
    return (a*b) // gcd(a,b)

def intinput():
    return int(input())

def mulinputs():
    return map(int,input().split())

def lineinputs(func=intinput):
    datas = []
    while True:
        try:
            datas.append(func())
        except EOFError:
            break
    return datas

class ModInt():
    def __init__(self, x):
        self.__x = (x % __MOD__)
    def __add__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x + other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x + other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __radd__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((other.__x + self.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((other.__x + self.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")

    def __sub__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x - other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x - other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __rsub__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((other.__x - self.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((other.__x - self.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")

    def __mul__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x * other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x * other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __rmul__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((other.__x * self.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((other.__x * self.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")

    def __truediv__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x * other.__modinv())%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x * other.__modinv())%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __rtruediv__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((other.__x * self.__modinv())%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((other.__x * self.__modinv())%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")

    def __pow__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__(pow(self.__x, other.__x, __MOD__)))
        elif type(other) == ModInt:
            return self.__class__(pow(self.__x, other.__x, __MOD__))
        else:
            raise Exception("Not Int or Not ModInt")
    def __rpow__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__(pow(other.__x, self.__x, __MOD__)))
        elif type(other) == ModInt:
            return self.__class__(pow(other.__x, self.__x, __MOD__))
        else:
            raise Exception("Not Int or Not ModInt")

    def __modinv(self, m=__MOD__):
        a = self.__x
        if a == 0:
            raise ZeroDivisionError()
        if gcd(a, m) != 1:
            raise Exception("%sの逆数は求まりません。" % a)
        b, u, v = m, 1, 0
        while b != 0:
            t = a//b;
            a -= t*b
            a, b = b, a
            u -= t * v
            u, v = v, u
        u %= m
        if u < 0:
            u += m
        return u
    def __int__(self):
        return self.__x
    def __str__(self):
        return str(self.__x)



if __name__ == '__main__':
    N, M, K = mulinputs()
    an = list(map(int, input().split()))
    bm = list(map(int, input().split()))

    # i, j, la, lb, t = 0, 0, len(an), len(bm), 0
    # l = max(la, lb)
    # while True:
    #     if i < la and j < lb:
    #         a, b = an[i], bm[j]
    #     elif i < la:
    #         a, b = an[i], INF
    #     elif j < lb:
    #         a, b = INF, bm[j]
    #     else:
    #         break
    #     c = 0
    #     if a <= b:
    #         c = a
    #         i += 1
    #     else:
    #         c = b
    #         j += 1
    #     if t + c <= K:
    #         t += c
    #         cnt += 1
    #     else:
    #         break
    # # t = 0
    # # while len(an) != 0 or len(bm) != 0:
    # #     a, b = INF, INF
    # #     if len(an) > 0:
    # #         a = an.pop()
    # #     if len(bm) > 0:
    # #         b = bm.pop()
    # #     if t + min(a, b) > K:
    # #         break
    # #     if a >= b:
    # #         t += a
    # #         cnt += 1
    # #         bm.append(b)
    # #     else:
    # #         t += b
    # #         cnt += 1
    # #         an.append(a)
# 
    # # 出力
    # print(cnt)


    la, lb = len(an), len(bm)
    def dfs(i, j, t, c):
        if i >= la and j >= lb:
            return c
        elif i >= la and j < lb:
            a, b = INF, bm[j]
        elif i < la and j >= lb:
            a, b = an[i], INF
        else:
            a, b = an[i], bm[j]
        if t + min(a,b) > K:
            return c
        if a < b:
            return dfs(i+1, j, t+a, c+1)
        elif a > b:
            return dfs(i, j+1, t+b, c+1)
        else:
            return max(dfs(i+1, j, t+a, c+1), dfs(i, j+1, t+a, c+1))

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

Submission status: Runtime Error

Solution code:
from itertools import accumulate

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

acc1 = list(accumulate(A))
acc2 = list(accumulate(B))

def is_ok(a, b):
    if acc1[a] + acc2[b] <= K:  return 1
    return 0

def binary_search(i, left, right):
    while (abs(right - left) > 1):
        mid = (left + right) // 2
        if is_ok(i, mid):  right = mid
        else:  left = mid
    return right

ans = 0
for i in range(N):
    ret = binary_search(i, M, -1)
    if ret == -1:  
      if acc1[i] <= K:  ans = max(i + 1, ans)
    else:
      ans = max(ans,  i + ret + 2)
      
acc1, acc2 = acc2, acc1
      
for i in range(N):
    ret = binary_search(i, M, -1)
    if ret == -1:  
      if acc1[i] <= K:  ans = max(i + 1, ans)
    else:
      ans = max(ans,  i + ret + 2)
      
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()))

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)