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):
x0=input().split()
a0=int(x0[0])
b0=int(x0[1])
min=int(x0[2])

a1=input().split()
al=[int(x) for x in a1]
b1=input().split()
bl=[int(x) for x in b1]

ar=[0]
ar0=0
for x in al:
  ar0+=x
  ar.append(ar0)
  if ar0>min:
    break
an=len(ar)
if an-1==len(al):
  ar.append(1000000001)
  an+=1

br=[0]
br0=0
for x in bl:
  br0+=x
  br.append(br0)
  if br0>min:
    break
bn=len(br)
if bn-1==len(bl):
  br.append(1000000001)
  bn+=1


ans=0
if an==2:
  ans=bn-2
elif bn==2:
  ans=an-2
else:
  bnow=0
  for i in range(an-2,-1,-1):
    
    if i+bn-2<=ans:
      break
    bcan=min-ar[i]
    for j in range(bnow,bn):
      if bcan>=br[j]:
        
        bnow+=1
      else:
        
        ans1=i+j-1
        if ans1>ans:
          ans=ans1
        break
    if bnow==bn-2:
      break
      
print(ans)

Solution (Python):
n, m, x = map(int, input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
i = 0
while i < len(a) and x >= a[i]:
    x -= a[i]
    i += 1

f = i
j = 0
for p in b:
    j += 1
    x -= p
    
    while x < 0 and i > 0:
        i -= 1
        x += a[i]
    
    if x >= 0: f = max(f, j + i)

print(f)

Solution (Python):
n,m,k=map(int, input().split())
*a,=map(int, input().split())
*b,=map(int, input().split())
now=0
cnt=0
from itertools import accumulate  
a=list(accumulate(a))
a=[0]+a
b=list(accumulate(b))

from bisect import bisect_right
tmp=0
for i in range(n+1):
    ta=k-a[i]
    nb=bisect_right(b,ta)
    if ta>=0:
        tmp=max(tmp,nb+i)
        
print(tmp)

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





a = [0] + a
for i in range(n):
    a[i + 1] = a[i] + a[i + 1]

b = [0] + b
for i in range(m):
    b[i + 1] = b[i] + b[i + 1]
    
ans = 0
for i in range(n + 1):
    for j in range(max(0, ans - i + 1), m + 1):
        if a[i] + b[j] <= k:
            ans = i + j
        else:
            break

print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10000000)
import math
import bisect
from collections import deque
def input():
    return sys.stdin.readline()[:-1]

n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
sa = [0]
sb = [0]
for i in range(n):
    sa.append(sa[-1]+a[i])
for i in range(m):
    sb.append(sb[-1]+b[i])
anst = 0
ans = 0


for i in range(n+1):
    tmp = k
    tmp-=sa[i]
    if tmp<0:
        continue
    iti = bisect.bisect_right(sb,tmp)
    
    anst = i+iti-1
    ans = max(ans,anst)

print(ans)

Solution (Python):

import sys


def solve(N: int, M: int, K: int, A: "List[int]", B: "List[int]"):
    SA = [0]
    SB = [0]
    for i in range(N):
        SA.append(SA[-1]+A[i])
    for i in range(M):
        SB.append(SB[-1]+B[i])
    acount = 0
    acost = 0
    for i in range(1,len(SA)):
        if SA[i] <= K:
            acount += 1
            acost = SA[i]
        else:
            break
    bcost = 0
    bcount = 0
    for i in range(1,len(SB)):
        if SB[i] + acost <= K:
            bcount += 1
            bcost = SB[i]
        else:
            break
    
    if acount == N and bcount == M:
        print(acount+bcount)
    else:
        mb = 0
        bi = 0
        for aindex in range(acount,-1,-1):
            acost = SA[aindex]
            while bi < M and acost + SB[bi+1] <=K:
                bi += 1
            
            mb = max(mb,aindex+bi)
        print(mb)


    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    M = int(next(tokens))  
    K = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(N)]  
    B = [int(next(tokens)) for _ in range(M)]  
    solve(N, M, K, A, B)

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

suma = 0
sumb = 0

As = [0]*N
for i,a in enumerate(A):
    suma += A[i]
    As[i] = suma

Bs = [0]*M
for i,b in enumerate(B):
    sumb += B[i]
    Bs[i] = sumb
sum_time=0
max_count = 0
count = 0

import bisect
for i in range(N+1):
    count = 0
    if i ==0:
        sum_time=0
    else:
        sum_time=As[i-1]
        count += i
    if sum_time>K:
        continue
    ind = bisect.bisect_right(Bs,K-sum_time)
    count += ind
    max_count = max(max_count,count)

print(max_count)

Solution (Python):
def sumup(l):
  k=[0]
  y=0
  for x in l:
    y+=x
    k.append(y)
  return k


ans=[]
n,m,k=map(int,input().split())
al=list(map(int,input().split()))
bl=list(map(int,input().split()))

al=sumup(al)
bl=sumup(bl)

for i in range(n+1):
  x=al[i]

  while True:
    if not bl:
      break
    
    else:
      b=bl[-1]
      if x+b>k : 
        bl.pop()

      else: 
        ans.append(i+len(bl)-1)
        break
print(max(ans))

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

from itertools import accumulate

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

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

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

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

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

Ac=[0]+list(accumulate(A))
Bc=[0]+list(accumulate(B))
ans=0
for i,ac in enumerate(Ac):
    j=bisect_left(Bc,K-ac)

    for k in range(j-1,min(j+2,M+1)):
        if ac+Bc[k]<=K:
            ans=max(ans,i+k)

print(ans)

Solution (Python):

"""
@author: H_Hoshigi
"""
import bisect

def main():
    N, M, K = map(int, input().split())
    A_lsit = list(map(int, input().split()))
    B_lsit = list(map(int, input().split()))

    A_sum_list = [0] + A_lsit[:]
    for i in range(1, N+1):
        A_sum_list[i] = A_sum_list[i-1] + A_lsit[i-1]
    B_sum_list = [0] + B_lsit[:]
    for i in range(1, M+1):
        B_sum_list[i] = B_sum_list[i-1] + B_lsit[i-1]
    
    

    max_book_number = 0
    for i in range(N+1):
        if A_sum_list[i] <= K:
            book_number_A = i
            book_number_B = bisect.bisect_right(B_sum_list, K-A_sum_list[i]) - 1
            max_book_number = max(
                max_book_number,
                book_number_A + book_number_B
            )
            
    print(max_book_number)

if __name__ == "__main__":
    main()

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

c = bisect.bisect_right(ax,k)
ma = 0
for i in range(c):
    d = bisect.bisect_right(bx,k-ax[i])
    ma = max(ma,i+d-1)
print(ma)

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

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

aa=[a[0]]
for i in range(n-1):
    aa.append(aa[i]+a[i+1])
aa.insert(0,0)
bb=[b[0]]
for i in range(m-1):
    bb.append(bb[i]+b[i+1])
    
import bisect
l=[]
for i in range(n+1):
    p=k-aa[i]
    if p>=0:
        l.append(i+bisect.bisect_right(bb,p))
    else:
        break
print(max(l))

Solution (Python):


from bisect import bisect_left


def leq_search(xs, x, s, t):
    
    if t-s <= 5:
        res = 0
        for i in range(s, t):
            if xs[i] <= x:
                res = i
        return res

    i = (s+t)//2
    if xs[i] <= x:
        return leq_search(xs, x, i, t)
    else:
        return leq_search(xs, x, s, i)


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

    S = [None] * (N+1)
    S[0] = 0
    for i in range(1, N+1):
        S[i] = S[i-1] + A[i-1]

    T = [None] * (M+1)
    T[0] = 0
    for i in range(1, M+1):
        T[i] = T[i-1] + B[i-1]

    n_read = 0
    for i in range(N+1):
        
        if S[i] > K:
            break
        n = leq_search(T, K-S[i], 0, M+1)
        
        if n_read < n + i:
            n_read = n + i
    
    return n_read


print(main())

Solution (Python):
import bisect
def main():

    N, M, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    prefixA = [0]
    for j in range(N):
        prefixA.append(prefixA[-1] + A[j])
    prefixB = [0]
    for j in range(M):
        prefixB.append(prefixB[-1] + B[j])

    ans = 0
    for i in range(N+1):
        v = prefixA[i]
        j = bisect.bisect_left(prefixB, K-v)
        if j< len(prefixB) and prefixB[j] + v <= K:
            ans = max(ans, i + j)
        if j-1 >=0 and prefixB[j-1] + v <= K:
            ans = max(ans, i + j-1)
    return ans


if __name__ == '__main__':
    print(main())

Solution (Python):
from collections import deque
N,M,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
SA = [0]
for i in range(N):
  temp = SA[-1] + A[i]
  SA.append(temp)

SB = [0]
for j in range(M):
  temp = SB[-1] + B[j]
  SB.append(temp)
ans = 0
for i in range(N+1):
  an = i
  if SA[i] > K:
    break
  else:
    nokori = K -SA[i]
    if nokori >= SB[-1]:
      bn = M
    else:
      ok = 0
      ng = M
      while abs(ok-ng) > 1:
        mid = (ok+ng)//2
        if nokori >= SB[mid]:
          ok = mid
        else:
          ng = mid
      bn = ok
  
  ans = max(ans,an+bn)
print(ans)

Solution (Python):
import math
import time
from collections import defaultdict, deque
from sys import stdin, stdout
from bisect import bisect_left, bisect_right,bisect
n,m,k=map(int,stdin.readline().split())
a=list(map(int,stdin.readline().split()))
b=list(map(int,stdin.readline().split()))
prea=[]
ans=0
for i in a:
    ans+=i 
    prea.append(ans)
final=bisect(prea,k)
temp=0
for i in range(m):
    temp+=b[i]
    if(temp>k):
        break
    temp2=bisect(prea,k-temp)
    final=max(final,temp2+i+1)
print(final)

Solution (Python):
N,M,K = map(int,input().split())
import bisect
A = list(map(int,input().split()))
B = list(map(int,input().split()))
ans = 0
sa = [0]*(N+1)
lim = N
for i in range(N):
    sa[i+1] = sa[i] + A[i]
    if sa[i+1] > K:
        lim = i
        break

sb = [0]*(M+1)
for i in range(M):
    sb[i+1] = sb[i] + B[i]
sb = sb[1:]




for reada in range(lim+1): 
    temp = sa[reada]
    
    cand = bisect.bisect_right(sb,K-temp)
    
    ans = max(ans,cand + reada)
print(ans)

Solution (Python):
N,M,K = map(int,input().split())
lA = list(map(int, input().split()))
lB = list(map(int, input().split()))
aA = []
aB = []
m = 0
k = 0
for A in lA:
    nx = k + A
    if nx > K:
        break
    aA.append(A)
    k = nx
    m += 1
for B in lB:
    nx = k + B
    if nx > K:
        break
    aB.append(B)
    k = nx
    m += 1
aA.reverse()
aB.reverse()
M = m
i = len(aB)
for A in aA:
    k = k - A
    m -= 1
    while i < len(lB):
        nx = k + lB[i]
        if nx > K:
            break
        k = nx
        m += 1
        if m > M:
            M = m
        i += 1
print(M)

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

from itertools import accumulate
import bisect

A = [0] + list(accumulate(a))
B = list(accumulate(b))

ans = 0
for ac in range(n+1):
    if A[ac] > k:
        break
    bc = bisect.bisect_right(B, k-A[ac])
    ans = max(ans, ac+bc)

print(ans)

Solution (Python):
from bisect import bisect_right as br


def main():
    n, m, k = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    tmp = 0
    a = 0
    b = 0
    Sb = [B[0]]
    for i in range(1, m):
        Sb.append(Sb[-1] + B[i])
    Sa = [A[0]]
    for i in range(1, n):
        Sa.append(Sa[-1] + A[i])
    mx = 0
    for j in range(n):
        index = br(Sb, k - Sa[j]) - 1
        tmp = Sa[j] + Sb[index] if index > 0 else Sa[j]
        if tmp <= k:
            mx = max(mx, j + index + 2)
    li = br(Sb, k)
    mx = max(li, mx)
    print(mx)
    return


if __name__ == "__main__":
    main()

Solution (Python):
from itertools import accumulate
import bisect
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
i = 0
j = 0
total = 0

ac = list(accumulate(a))
bc = list(accumulate(b))

ans = 0
for i in range(bisect.bisect(ac, k)):
    bi = bisect.bisect(bc, k - ac[i])
    ans = max(ans, i+1+bi)
for i in range(bisect.bisect(bc, k)):
    bi = bisect.bisect(ac, k - bc[i])
    ans = max(ans, i+1+bi)
ans = max(ans, bisect.bisect(ac, k))
ans = max(ans, bisect.bisect(bc, k))
print(ans)

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

N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
acc_A = [0] + list(accumulate(A))
acc_B = [0] + list(accumulate(B))
res = 0
for i, a in enumerate(acc_A):
    if a > K:
        break
    idx = bisect_left(acc_B, K - a)
    idx = min(idx, M)
    if acc_B[idx] > K - a:
        idx -= 1
    res = max(res, i + idx)
print(res)

Solution (Python):
import bisect
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
ca = A[:]
cb = B[:]

for i in range(N-1):
  ca[i+1] += ca[i]
for i in range(M-1):
  cb[i+1] += cb[i]

ans = 0
for i in range(N):
  if ca[i] <= K:
    idx = bisect.bisect_right(cb, K-ca[i])
  else:
    continue
  
  ans = max(ans, i+idx+1)

for i in range(M):
  if cb[i] <= K:
    idx = bisect.bisect_right(ca, K-cb[i])
  else:
    continue
  
  ans = max(ans, i+idx+1)
print(ans)

Solution (Python):
INF = 10**10
N,M,K = map(int,input().split())
A = [0]+list(map(int,input().split()))+[INF]
B = [0]+list(map(int,input().split()))+[INF]
tmp = 0
i = 0
j = 0
while tmp+A[i+1]<=K:
    tmp += A[i+1]
    i += 1
while tmp+B[j+1]<=K:
    tmp += B[j+1]
    j += 1
ans = i+j
while i>0:
    tmp -= A[i]
    i -= 1
    while tmp+B[j+1]<=K:
        tmp += B[j+1]
        j += 1
    ans = max(ans,i+j)
print(ans)

Solution (Python):
production = True

import sys, math, collections

def input(input_format = 0, multi = 0):

    if multi > 0: return [input(input_format) for i in range(multi)]
    else:
        next_line = sys.stdin.readline()[:-1]

        if input_format >= 10:
            use_list = False
            input_format = int(str(input_format)[-1])
        else: use_list = True

        if input_format == 0: formatted_input = [next_line]
        elif input_format == 1: formatted_input = list(map(int, next_line.split()))
        elif input_format == 2: formatted_input = list(map(float, next_line.split()))
        elif input_format == 3: formatted_input = list(next_line)
        elif input_format == 4: formatted_input = (lambda x: [x[0], list(map(int, x[1:]))])(next_line.split())
        elif input_format == 5: formatted_input = (lambda x: [x[0], list(map(float, x[1:]))])(next_line.split())
        elif input_format == 6: formatted_input = next_line.split()
        else: formatted_input = [next_line]

        return formatted_input if use_list else formatted_input[0]


def out(output_line, output_format = 0, newline = True):

    formatted_output = ""

    if output_format == 0: formatted_output = str(output_line)
    elif output_format == 1: formatted_output = " ".join(map(str, output_line))
    elif output_format == 2: formatted_output = "\n".join(map(str, output_line))

    print(formatted_output, end = "\n" if newline else "")


def log(*args):
    if not production:
        print("$$$", end = "")
        print(*args)

enum = enumerate






def solve():

    n, m, k = input(1)
    a = input(1)
    b = input(1)


    ap = [0]
    bp = [0]

    for i in a:
        ap.append(ap[-1] + i)

    for i in b:
        bp.append(bp[-1] + i)


    log(ap, bp)

    pa = 0

    while pa < len(ap) and ap[pa] <= k:
        pa += 1

    m = pa - 1
    pb = 1

    c = k - ap[pa - 1]

    for i in range(pa - 1, -1, -1):
        while pb < len(bp) and bp[pb] <= c:
            m = max(pa - 1 + pb, m)
            pb += 1
        pa -= 1
        c += a[pa - 1]

    out(m)


        
    



    return



solve()

Solution (Python):
from itertools import accumulate


def binary_search(key):
    bad, good = -1, len(B)
    while good - bad > 1:
        mid = (bad + good) // 2
        if B[mid] > key:
            good = mid
        else:
            bad = mid
    return good


N, M, K = map(int, input().split())
A = [0] + list(accumulate(map(int, input().split())))
B = list(accumulate(map(int, input().split())))
ans = 0
for i in range(N + 1):
    tmp = A[i]
    if tmp > K:
        break
    m = binary_search(K - tmp)
    ans = max(ans, i + m)
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 = 0
a.insert(0, 0)
for i in range(n + 1):
    x = k - a[i]
    if not x < 0:
        j = bisect.bisect_right(b, x)
        ans = max(ans, i + j)
print(ans)

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

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

liA = [0]
liB = [0]

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

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




ans = 0
for i in range(N+1):
    mm = K - liA[i]
    if mm<0:
        break
    fst = 0
    lst = M

    while(lst-fst>0):
        mid = ((fst+lst)//2)+1
        if mm >= liB[mid]:
            fst = mid
        elif mm<liB[mid]:
            lst = mid-1
        
    ans = max(ans, i+fst)

print(ans)

Solution (Python):
n,m,k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
A = n
B = 0
for i in range(1,n):
    a[i] += a[i-1]
    if (a[i]>k):
        A = i
        break
for i in range(1,m):
    b[i] += b[i-1]
ans = A
if (a[0]>k):
        for i in range(m):
            if (b[i]>k):
                print(i)
                exit()
        print(len(b))
        exit()
a = [0] + a
b = [0] + b
while True:
    if (A<0):
        break
    if (B>=m):
        break
    B += 1
    
    if (a[A]+b[B] >k):
        B -= 1
        A -= 1
    else:
        ans = max(ans,A+B)
print(ans)

Solution (Python):
import sys

import math
import collections

import bisect
import itertools
import fractions

import copy

import decimal

import queue



sys.setrecursionlimit(10000001)
INF = 10 ** 16
MOD = 10 ** 9 + 7


ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))




def main():
    n, m, k = ns()
    a = na()
    b = na()

    perm_a = [0 for _ in range(n + 1)]
    perm_b = [0 for _ in range(m + 1)]

    for i, ai in enumerate(a):
        perm_a[i + 1] = perm_a[i] + ai

    for i, bi in enumerate(b):
        perm_b[i + 1] = perm_b[i] + bi

    perm_a.append(INF)
    perm_b.append(INF)

    ans = 0
    for i, pa in enumerate(perm_a):
        bserch = k - pa
        if bserch < 0:
            continue
        if bserch == 0:
            ans = max(ans, i)
            continue

        idx = bisect.bisect_left(perm_b, bserch)
        tmp_ans = i + idx - 1
        if perm_b[idx] == bserch:
            tmp_ans += 1
        ans = max(ans, tmp_ans)

    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()))
cuma = [0]
cumb  = [0]
for a in A:
    cuma.append(cuma[-1] + a)
for b in B:
    cumb.append(cumb[-1] + b)
from bisect import bisect_left
ans = 0
for i in range(n+1):
    if k < cuma[i]: continue
    idx = bisect_left(cumb, k - cuma[i] + 1)
    ans = max(ans, i + idx - 1)
for j in range(m+1):
    if k < cumb[j]: continue
    idx = bisect_left(cuma, k - cumb[j] + 1)
    ans = max(ans, j + idx - 1)
print(ans)

Solution (Python):

def main():
  n, m, k = map(int, input().split())
  A = list(map(int, input().split()))
  B = list(map(int, input().split()))
  B.insert(0, 0)

  data = [None] * n
  total = 0
  for i in range(n):
    a = A[i]
    data[i] = (total, total + a)
    total += a
  data.append((total, float('inf')))

  def bi_search(num, l, r):
    if l > r:
      return n

    i = (l + r) // 2

    tail, head = data[i]
    if tail <= num < head:
      return i

    if num < tail:
      return bi_search(num, l, i - 1)

    return bi_search(num, i + 1, r)


  ans = 0
  total = 0
  for j in range(m + 1):
    total += B[j]
    if total > k:
      break

    i = bi_search(k - total, 0, n - 1)
    if i + j > ans:
      ans = i + j

  print(ans)


main()

Solution (Python):
import numpy as np
n,m,k=map(int,input().split())
a=[0]
c=list(map(int,input().split()))
b=list(map(int,input().split()))

for _ in range(n):
    a.append(c[_])

a.append(10**10)
b.append(10**10)

ara=np.array(a)
arb=np.array(b)
la=np.cumsum(ara)
lb=np.cumsum(arb)

ans=0

i=0
while la[i]<=k:
    rest=k-la[i]
    j=np.searchsorted(lb,rest,side="right")
    ans=max(i+j,ans)
    i+=1
print(ans)

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

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

suma=sum(a)
ai=n-1
sumb=0
bi=-1

ans=0

while ai>=0:
    if ai ==n-1:
        while suma+sumb<=k:
            ans=max(ans,ai+1+bi+1)

            if bi+1<m:
                bi+=1
                sumb+=b[bi]
            else:
                break


    suma-=a[ai]
    ai-=1
    while suma+sumb<=k:
        ans=max(ans,ai+1+bi+1)

        if bi+1<m:
            bi+=1
            sumb+=b[bi]
        else:
            break
    



print(ans)

Solution (Python):
from sys import stdin, setrecursionlimit

setrecursionlimit(10 ** 7)

from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
from collections import deque, defaultdict, Counter
from itertools import combinations, permutations, combinations_with_replacement
from itertools import accumulate
from math import ceil, sqrt, pi, radians, sin, cos

MOD = 10 ** 9 + 7
INF = 10 ** 18



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


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

AcumA = [0] + list(accumulate(A))
AcumB = [0] + list(accumulate(B))

ia, ib = 0, M 
answer = 0

while ib >= 0 and ia <= N:
    if AcumA[ia] + AcumB[ib] <= K:
        answer = max(answer, ia + ib)
        ia += 1
    else:
        ib -= 1

print(answer)

Solution (Python):
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 8)
from itertools import accumulate
from itertools import permutations
from itertools import combinations
from collections import defaultdict
from collections import Counter
import fractions
import math
from collections import deque
from bisect import bisect_left
from bisect import bisect_right
from bisect import insort_left
import itertools
from heapq import heapify
from heapq import heappop
from heapq import heappush
import heapq
from copy import deepcopy
from decimal import Decimal
alf = list("abcdefghijklmnopqrstuvwxyz")
ALF = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")

INF = float("inf")


N,M,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
a = []
aa = 0
bb = 0
b = []
flag = 0
for i in range(N):
    aa += A[i]
    a.append(aa)
for i in range(M):
    bb += B[i]
    b.append(bb)
MAX = 0
for i in range(N):
    t = a[i]
    if t > K:
        break
    else:
        tt = K-t
        index = bisect_left(b,tt)
        if index == M:
            MAX = max(MAX,i+1+M)
        elif b[index] == tt:
            MAX = max(MAX,i+1+1+index)
        else:
            MAX = max(MAX,i+1+index)
for i in range(M):
    t = b[i]
    if t > K:
        break
    else:
        tt = K-t
        index = bisect_left(a,tt)
        if index == N:
            MAX = max(MAX,i+1+N)
        elif a[index] == tt:
            MAX = max(MAX,i+1+1+index)
        else:
            MAX = max(MAX,i+1+index)


        
print(MAX)

Solution (Python):
import sys
from collections import deque
import bisect

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_nl = lambda: list(map(int, readline().split()))
in_na = lambda: map(int, read().split())
in_s = lambda: readline().rstrip().decode('utf-8')


def main():

    N, M, K = in_nn()
    A = in_nl()
    B = in_nl()

    Asum = [0] * (N + 1)
    Bsum = [0] * (M + 1)

    for i in range(N):
        Asum[i + 1] += Asum[i] + A[i]

    for i in range(M):
        Bsum[i + 1] += Bsum[i] + B[i]

    ans = 0
    for i in range(N + 1):
        time = K - Asum[i]
        if time < 0:
            break
        else:
            ans = max(ans, i + bisect.bisect_right(Bsum, time) - 1)

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
n, m, k = map(int, input().split())
a_ls = list(map(int, input().split()))
b_ls = list(map(int, input().split()))
sub = 0
a_ls_cnt = 0
a_ls_len = len(a_ls)
now_time = 0


while now_time < k:
    now_time+=a_ls[a_ls_cnt]
    if now_time > k:
        now_time-=a_ls[a_ls_cnt]
        break
    a_ls_cnt+=1
    if a_ls_len == a_ls_cnt:
        break

b_ls_cnt = 0
b_ls_len = len(b_ls)

while now_time < k:
    now_time+=b_ls[b_ls_cnt]
    if now_time > k:
        now_time-=b_ls[b_ls_cnt]
        break
    b_ls_cnt+=1
    if b_ls_len == b_ls_cnt:
        break

sub = a_ls_cnt+b_ls_cnt
break_flg = 0


while a_ls_cnt >= 1:
    if break_flg == 1:
        break
    now_time-=a_ls[a_ls_cnt-1]
    a_ls_cnt-=1
    while now_time < k:
        
        if b_ls_cnt == b_ls_len:
            break_flg = 1
            sub = max(sub, a_ls_cnt+b_ls_cnt)
            break
        now_time+=b_ls[b_ls_cnt]
        if now_time > k:
            now_time-=b_ls[b_ls_cnt]
            break
        b_ls_cnt+=1
    sub = max(sub, a_ls_cnt+b_ls_cnt)
    
print(sub)

Solution (Python):
import itertools
import bisect

def main():
    N,M,K  = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    AS = list(itertools.accumulate(A))
    BS = list(itertools.accumulate(B))
    Amaxin = bisect.bisect_right(AS, K)
    Bmaxin = bisect.bisect_right(BS, K)
    ans = max(Amaxin-1,Bmaxin-1)
    index = 0
    for i in reversed(range(0,Amaxin)):
        while index<M and AS[i] + BS[index] <=K:
            index+=1
        ans = max(i+index+1,ans)
    index = 0
    for i in reversed(range(0,Bmaxin)):
        while index<N and BS[i] + AS[index] <=K:
            index+=1
        ans = max(i+index+1,ans)

    print(max(ans,0))

if __name__ == '__main__':
    main()

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 = list(map(int, input().split()))

def ruiseki(lst):
    return [0]+list(accumulate(lst))

ar = ruiseki(A)
br = ruiseki(B)
ans = 0
for i in range(1+N):
    left = K - ar[i]
    if left < 0:
        continue
    bidx = bisect_right(br, left) - 1
    ans = max(ans,i+bidx)

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()))
at=[0]
bt=[]
temp=0
for i in a:
    temp+=i
    at.append(temp)
temp=0
for i in b:
    temp+=i
    bt.append(temp)
ans=0
for i in range(len(at)):
    if at[i]>k:
        break
    ind= bisect.bisect_right(bt,k-at[i])
    ans= max(ans, ind+i)
print(ans)

Solution (Python):
from itertools import accumulate
import bisect
n,m,k=map(int,input().split())
a=[int(i) for i in input().split()]
b=[int(i) for i in input().split()]

A=list(accumulate(a))
B=list(accumulate(b))

ans = max(bisect.bisect_right(B,k),bisect.bisect_right(A,k))

for i in range(len(a)):
    r = (k-A[i])
    if r<0:
        break
    
    
    ans=max(ans,i+1+bisect.bisect_right(B,r))
    
    
for i in range(len(b)):
    r = (k-B[i])
    if r<0:
        break
    
    
    ans=max(ans,i+1+bisect.bisect_right(A,r))
    
    
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**9)

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

    a.insert(0, 0)
    b.insert(0, 0)

    a_sum = [0]*(n+1)
    a_sum[0] = a[0]
    for i in range(1, n+1):
        a_sum[i] = a_sum[i-1] + a[i]

    b_sum = [0]*(m+1)
    b_sum[0] = b[0]
    for i in range(1, m+1):
        b_sum[i] = b_sum[i-1] + b[i]

    b_sum = b_sum[::-1]

    i_b = 0
    ans = 0
    for i_a in range(n+1):
        while i_b <= m:
            if k < a_sum[i_a] + b_sum[i_b]:
                i_b += 1
                continue
            else:
                ans = max(ans, i_a+(m-i_b))
                break

    print(ans)
    return


if __name__ == "__main__":
    main()
    sys.exit(0)