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.

Solution (Python):
import sys
input=sys.stdin.readline
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
A=[0]
B=[0]
for i in range(n):
    A.append(A[i]+a[i])
for i in range(m):
    B.append(B[i]+b[i])
import bisect
ans=0
for i in range(n+1):
    if A[i]<=k:
        j=bisect.bisect(B,k-A[i])
        temp=i+j-1
        ans=max(ans,temp)
print(ans)

Solution (Python):


import bisect

LI = lambda: list(map(int, input().split()))


N, M, K = LI()
A = LI()
B = LI()


def main():
    sa = [0] * (N + 1)
    for i in range(N):
        sa[i + 1] = sa[i] + A[i]
    sb = [0] * (M + 1)
    for i in range(M):
        sb[i + 1] = sb[i] + B[i]
    ans = 0
    for i in range(N + 1):
        a = sa[i]
        if a > K:
            break
        j = bisect.bisect_right(sb, K - a)
        ans = max(ans, i + j - 1)
    print(ans)


if __name__ == "__main__":
    main()

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

b_counter = [0]
for _b in b :
    b_counter.append(b_counter[-1]+_b)

res = 0
cur = 0
for i in range(N+1):    
    left = K-cur
    _i = 0
    _j = len(b_counter)-1
    while _i < _j :
        m = (_i+_j+1)//2
        if b_counter[m] <= left :
            _i = m
        else :
            _j = m-1
    if b_counter[_i] <= left :
        res = max(i+_i, res)

    if i == N : break 
    cur += a[i]

print(res)

Solution (Python):
from bisect import bisect_right
def main():
    n, m, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    for i in range(n-1):
        a[i+1] += a[i]
    for i in range(m-1):
        b[i+1] += b[i]
    ans = bisect_right(b, k)
    for i in range(1, n+1):
        if k < a[i-1]:
            break
        x = k - a[i-1]
        tmp = i
        tmp += bisect_right(b, x)
        if ans < tmp:
            ans = tmp
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
import sys
from collections import defaultdict, deque, Counter
import math


from bisect import bisect_left, bisect_right





input = sys.stdin.readline

getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = 10 ** 20
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)

def solve():
    n,m,k = getList()
    anums = getList()
    bnums = getList()
    arui = []
    tmp = 0
    for num in anums:
        tmp += num
        arui.append(tmp)
    brui = []
    tmp = 0
    for num in bnums:
        tmp += num
        brui.append(tmp)
    ans = 0
    for i, ar in enumerate(arui):
        if ar <= k:
            nokori = k - ar
            byom = bisect_right(brui, nokori)
            tans = byom + i + 1
            if tans > ans:
                ans = tans

    tans = bisect_right(brui, k)
    if tans > ans:
        ans = tans
    print(ans)
def main():
    n = getN()
    for _ in range(n):
        solve()


if __name__ == "__main__":
    
    solve()

Solution (Python):
from bisect import bisect_left, bisect_right





n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for i in range(1, n):
    a[i] += a[i - 1]
a = [0] + a
for i in range(1, m):
    b[i] += b[i - 1]
ans = 0
for i in range(n + 1):
    if a[i] > k:
        break
    x = bisect_right(b, k - a[i])
    ans = max(x + i, ans)
print(ans)

Solution (Python):
import bisect
import itertools
N, M, K = [int(_) for _ in input().split()]
A = list(itertools.accumulate([0] + [int(_) for _ in input().split()]))
B = list(itertools.accumulate([0] + [int(_) for _ in input().split()]))
ans = 0
for i, a in enumerate(A):
    if a > K:
        break
    ans = max(ans, i + bisect.bisect(B, K - a) - 1)
print(ans)

Solution (Python):
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = [0] + list(reversed(b)) + a
for i in range(n + m):
    c[i + 1] += c[i]
r = m
ans = 0
for l in range(m + 1):
    while r <= n + m and c[r] - c[l] <= k:
        ans = max(ans, r - l)
        r += 1
print(ans)

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

now = sum(b)
j = len(b)
while j > 0 and now > k:
    j -= 1
    now -= b[j]
ans = j
for i, ai in enumerate(a, 1):
    now += ai
    while j > 0 and now > k:
        j -= 1
        now -= b[j]
    if now > k:
        break
    ans = max(ans, i + j)
print(ans)

Solution (Python):
from math import gcd

from math import factorial as f

from math import ceil,floor,sqrt
import math

import bisect
import re
import heapq


from copy import deepcopy
import itertools
from itertools import permutations

from sys import exit

ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))


yes = "Yes"
no = "No"


def main():
    n,m,k = mi()
    a = li()
    b = li()
    arui = [0]
    brui = [0]
    tmp = 0
    ans = 0

    for i in range(n):
        tmp+=a[i]
        arui.append(tmp)
    tmp = 0
    for i in range(m):
        tmp+=b[i]
        brui.append(tmp)
    anstmp = 0
    for i in range(n+1):
        if arui[i]>k:
            break
        anstmp = i
        anstmp+=bisect.bisect_right(brui,k-arui[i])
        anstmp-=1
        ans = max(ans,anstmp)
    print(ans)



main()

Solution (Python):
import unittest


def main():
    n, m, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    total_a = [0]*(n+1)
    total_b = [0] * (m + 1)
    for i in range(n):
        total_a[i+1] = total_a[i] + a[i]
    for i in range(m):
        total_b[i + 1] = total_b[i] + b[i]
    ai = 0
    bi = m
    max_v = 0
    while total_b[bi] > k:
        bi -= 1
    while ai <= n and total_a[ai] <= k:
        while bi > 0 and total_a[ai]+total_b[bi] > k:
            bi -= 1
        if total_a[ai]+total_b[bi] <= k:
            max_v = max(max_v, ai + bi)
        ai += 1
    print(max_v)


if __name__ == "__main__":
    main()


class TestAbc138_e(unittest.TestCase):
    def test_abc138_e(self):
        self.assertEqual(solve('contest', 'son'), 10)
        self.assertEqual(solve('contest', 'programming'), -1)
        self.assertEqual(solve('contest', 'sentence'), 33)
        self.assertEqual(solve('abc', 'abccccac'), 15)

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip()
import bisect

def cumsum(a):
   r = [0]
   for v in a:
      r.append(r[-1] + v)
   return r

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

ans = 0
for a_num, v in enumerate(a):
   tmp = 0
   if k >= v:
      b_num = bisect.bisect_right(b, k - v) - 1
      tmp = a_num + b_num
   ans = max(ans, tmp)
print(ans)

Solution (Python):
"""
NTC here
"""
 
 

import os
import sys
from io import BytesIO, IOBase

profile = 0
pypy = 1
 
 
def iin(): return int(input())


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


def main():
    n, m, k = lin()
    a = lin()
    b = lin()
    a1 = []
    sm = 0
    a2 = []
    for i in a:
        if sm+i<=k:
            a1.append(i)
            sm += i
        else:
            break
    ans = len(a1)
    for i in b:
        while a1 and sm+i>k:
            x = a1.pop()
            sm -= x
        if sm+i<=k:
            a2.append(i)
            sm += i
        else:
            break
        ans = max(ans, len(a1)+len(a2))
        
    print(ans)
        










    




                

        

 
 
 
 
 
 

 
BUFSIZE = 8192
 
 
class FastIO(IOBase):
    newlines = 0
 
    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None
 
    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()
 
    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()
 
    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)
 
 
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")
 
if pypy:
    sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
    input = lambda: sys.stdin.readline().rstrip("\r\n")
 

 
if __name__ == "__main__":
    if profile:
        import cProfile
        cProfile.run('main()')
    else:
        main()

Solution (Python):
import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N,M,K=MI()
    A=LI()
    B=LI()
    AS=[0]*(N+1)
    BS=[0]*(M+1)
    
    for i in range(N):
        AS[i+1]=AS[i]+A[i]
        
    for i in range(M):
        BS[i+1]=BS[i]+B[i]

    import bisect
    
    ans=0
    for i in range(N+1):
        at=AS[i]
        bt=K-at
        if bt<0:
            break
        j=bisect.bisect_right(BS,bt)-1
        temp=i+j
        ans=max(ans,temp)
        
    print(ans)
        

            

main()

Solution (Python):
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
aa = [a[0]]
bb = [b[0]]
for i in range(1,n):
  aa.append(aa[-1]+a[i])
for i in range(1,m):
  bb.append(bb[-1]+b[i])
import bisect


ans = bisect.bisect_right(bb,k)

for i in range(n):
  if aa[i] > k:
    
    break
  tmp = bisect.bisect_right(bb,k-aa[i])+i+1
  ans = max(ans,tmp)
  
print(ans)

Solution (Python):
import bisect
N,M,K = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
ac = [0]*(N+1)
bc = [0]*(M)
bc[0] = b[0]
for i,val in enumerate(a):
    ac[i+1] = ac[i] + val
for i,val in enumerate(b[1:]):
    bc[i+1] = bc[i] + val
res = 0
for i,val in enumerate(ac):
    if val > K:
        break
    rest = K-val
    idx = bisect.bisect_right(bc, rest)
    if idx+i > res:
        res = idx+i
print(res)

Solution (Python):
from itertools import accumulate
N, M, T = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

Asum = list(accumulate(A, initial=0))
Bsum = list(accumulate(B, initial=0))

ans = 0
j = 0
for i in reversed(range(N + 1)):
    x = Asum[i]
    if x > T:
        continue
    while j < M and Bsum[j + 1] + x <= T:
        j += 1
    ans = max(ans, i + j)

print(ans)

Solution (Python):
from itertools import accumulate
import bisect

n,m,K = map(int, input().split())
a = [0] + list(accumulate(list(map(int, input().split()))))
b = list(accumulate(list(map(int, input().split()))))
ans = 0
for k,v in enumerate(a):
    if v > K:
        break
    tmp = k + bisect.bisect_right(b,K-v)
    if tmp > ans:
        ans = tmp
print(ans)

Solution (Python):
from bisect import bisect_right
from itertools import accumulate
N, M, K = map(int, input().split())
As = list(map(int, input().split()))
Bs = list(map(int, input().split()))

accA = list(accumulate(As)) + [0]
accB = list(accumulate(Bs))

ans = 0
for numA in range(N+1):
    t = accA[numA-1]
    if t > K:
        break
    numB = bisect_right(accB, K-t)
    tmp = numA + numB
    ans = ans if ans > tmp else tmp
print(ans)

Solution (Python):
from itertools import accumulate
from bisect import bisect_right

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

acc_a = list(accumulate(a))
acc_b = list(accumulate(b))

ans = 0
for i, e in enumerate(acc_b):
    if e > k:
        break
    a_cnt = bisect_right(acc_a, k - e)
    ans = max(ans, i + a_cnt)

print(ans)

Solution (Python):
def main():
    from itertools import accumulate as ac
    from bisect import bisect_left as bl
    n, m, k = map(int, input().split())
    a = [0]+list(map(int, input().split()))
    b = [0]+list(map(int, input().split()))
    aa = list(ac(a))
    bb = list(ac(b))
    ans = 0
    for i in range(n+1):
        if aa[i] > k:
            break
        ans = max(ans, i+bl(bb, k-aa[i]+0.1)-1)
    print(ans)


main()

Solution (Python):
import sys
sys.setrecursionlimit(10**7) 
import math
from copy import copy, deepcopy
from copy import deepcopy as dcp
from operator import itemgetter
from bisect import bisect_left, bisect, bisect_right

from collections import deque


from collections import Counter

from itertools import accumulate,combinations,permutations

from heapq import heapify,heappop,heappush


from functools import lru_cache

from decimal import Decimal

def input(): 
    x=sys.stdin.readline()
    return x[:-1] if x[-1]=="\n" else x
def printl(li): _=print(*li, sep="\n") if li else None
def argsort(s, return_sorted=False): 
    inds=sorted(range(len(s)), key=lambda k: s[k])
    if return_sorted: return inds, [s[i] for i in inds]
    return inds
def alp2num(c,cap=False): return ord(c)-97 if not cap else ord(c)-65
def num2alp(i,cap=False): return chr(i+97) if not cap else chr(i+65)
def matmat(A,B):
    K,N,M=len(B),len(A),len(B[0])
    return [[sum([(A[i][k]*B[k][j]) for k in range(K)]) for j in range(M)] for i in range(N)]
def matvec(M,v):
    N,size=len(v),len(M)
    return [sum([M[i][j]*v[j] for j in range(N)]) for i in range(size)]
def T(M):
    n,m=len(M),len(M[0])
    return [[M[j][i] for j in range(n)] for i in range(m)]
def main():
    mod = 1000000007
    

    
    N, M,K = map(int, input().split())
    A = tuple(map(int, input().split())) 
    
    
    B = tuple(map(int, input().split()))
    A= list(accumulate(A))
    B= list(accumulate(B))
    ans=0
    ans=bisect_right(B,K)
    for i in range(N):
        a=A[i]
        if a>K:
            break
        ans=max(ans,i+1+bisect_right(B,K-a))
    print(ans)




if __name__ == "__main__":
    main()

Solution (Python):
from bisect import bisect_left, bisect_right

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

res = 0

cA = [0]
for i in range(N):
    cA.append(cA[-1] + A[i])

cB = [0]
for i in range(M):
    cB.append(cB[-1] + B[i])

for i in range(N + 1):
    rem = K - cA[i]
    if rem < 0:
        continue
    j = bisect_right(cB, rem)
    res = max(res, i + j - 1)

print(res)

Solution (Python):

from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n,m,k = LI()
    a = LI()
    b = LI()
    a = [0]+list(accumulate(a))
    b = [0]+list(accumulate(b))
    ans = 0
    for i in range(len(a)):
        sa = a[i]
        res = k-sa
        if res < 0:
            continue
        j = bisect.bisect_right(b,res)
        s = i+j-1
        if ans < s:
            ans = s
    print(ans)
    return


if __name__ == "__main__":
    solve()

Solution (Python):
import bisect
N,M,K = map(int,input().split())

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

A.append(float("inf"))
B.append(float("inf"))
bs = [0]
for i in B:
    bs.append(bs[-1]+i)

ans = 0
now = 0

for i in range(N+1):

    ind = bisect.bisect_right(bs,K-now)-1

    ans = max(ans , i + ind)
    
    now += A[i]
    if now > K:
        break

print (ans)

Solution (Python):
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
for i in range(n-1):a[i+1]+=a[i]
for i in range(m-1):b[i+1]+=b[i]
from bisect import bisect_left,bisect_right
ans=bisect_right(b,k)
for i in range(n):
  if a[i]>k:break
  ans=max(ans,i+1+bisect_right(b,k-a[i]))
print(ans)

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

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

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

C = [0]
c = 0
for i in range(N):
    c+=A[i]
    C.append(c)

D = [0]
d = 0
for i in range(M):
    d+=B[i]
    D.append(d)

ans = 0

def bisect(m):
    if m>=D[M]:
        return M
    if m==0:
        return 0
    r = M
    l = 0
    while r-l>1:
        if m>=D[(r+l)//2]:
            l = (r+l)//2
        else:
            r = (r+l)//2
    return l

for i in range(N+1):
    if K<C[i]:
        break
    ans = max(i+bisect(K-C[i]),ans)
print(ans)

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

SA = 0
SB = sum(B)
Bn = len(B)
ans = 0
for An in range(len(A)+1):
  if An>0:
    SA += A[An-1]
  if SA>K:
    break
  while SA+SB>K:
    Bn -= 1
    SB -= B[Bn]
  ans = max(ans, An+Bn)
print(ans)

Solution (Python):
import sys
from bisect import bisect_right

import numpy as np

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    N, M, K = map(int, input().split())
    A = [0] + list(map(int, input().split()))
    B = [0] + list(map(int, input().split()))
    a = np.cumsum(A)
    b = np.cumsum(B)

    ans = 0
    for i in range(N + 1):
        time = a[i]
        if time > K:
            continue
        idx = bisect_right(b, K - time)
        tmp = i + idx - 1
        if tmp > ans:
            ans = tmp

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):

import sys
from bisect import bisect_left,bisect_right
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

N,M,K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
sa=[0]*(N+1)
sb=[0]*(M+1)
for i in range(N):
    sa[i+1]=sa[i]+A[i]
for i in range(M):
    sb[i+1]=sb[i]+B[i]
ans=0
for i in range(N+1):
    if sa[i]<=K:
        res=bisect_right(sb,K-sa[i])
        tmp=i+res-1
        ans=max(ans,tmp)
print(ans)

Solution (Python):
mod = 1000000007
eps = 10**-9


def main():
    import sys
    from bisect import bisect_left
    input = sys.stdin.readline

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

    cs_A = [0] * (N+1)
    cs_B = [0] * (M+1)
    for i in range(N):
        cs_A[i+1] = cs_A[i] + A[i]
    for i in range(M):
        cs_B[i+1] = cs_B[i] + B[i]

    ans = 0
    for i in range(N+1):
        if cs_A[i] > K:
            break
        j = bisect_left(cs_B, K - cs_A[i] + 1) - 1
        if j == M+1:
            j = M
        ans = max(i+j, ans)
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
from bisect import bisect
from itertools import accumulate

n, m, k, *ab = map(int, sys.stdin.buffer.read().split())
aaa = [0] + ab[:n]
bbb = [0] + ab[n:]
acc = list(accumulate(aaa))
bcc = list(accumulate(bbb))

ans = 0
for i in range(n + 1):
    r = k - acc[i]
    if r < 0:
        break
    j = bisect(bcc, r) - 1
    ans = max(ans, i + j)
print(ans)

Solution (Python):
import bisect
N,M,K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
for i in range(1,N):
    A[i]+=A[i-1]
for i in range(1,M):
    B[i]+=B[i-1]

ans=bisect.bisect_right(B,K)
for i in range(0,N):
    if K>=A[i]:
        temp=i+1
        id=bisect.bisect_right(B,K-A[i])
        temp+=id
        ans=max(ans,temp)


print(ans)

Solution (Python):
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
sa = [0] * (n + 1)
sb = [0] * (m + 1)
for i in range(n):
  sa[i + 1] = sa[i] + a[i]
for i in range(m):
  sb[i + 1] = sb[i] + b[i]
r = m
ans = 0
for i in range(n + 1):
  if sa[i] > k:
    break
  while sa[i] + sb[r] > k:
    r -= 1
  ans = max(ans, i + r)
print(ans)

Solution (Python):
from bisect import bisect_left, bisect_right

n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a_cs = [0] * (n + 1)
b_cs = [0] * (m + 1)

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

ans = 0
for i in range(n + 1):
    if a_cs[i] <= k:
        cand = i
        cand += bisect_right(b_cs, k - a_cs[i]) - 1
        ans = max(ans, cand)

print(ans)