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
inp,ip = lambda :int(input()),lambda :[int(w) for w in input().split()]

from bisect import bisect_right as bs
n,m,k = ip()
a = ip()
b = ip()
x = [0]*(n+1)
for i in range(n):
    x[i+1] = x[i]+a[i]
y = [0]*(m+1)
for i in range(m):
    y[i+1] = y[i]+b[i]
ans = 0
for i in range(n+1):
    if x[i] <= k:
        ans = max(ans,i+bs(y,k-x[i])-1)
    else:
        break
print(ans)

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

p1, p2 = n, 0
f = [0]
s = [0]
for i in range(n):
    f.append(f[i]+arr1[i])
for i in range(m):
    s.append(s[i]+arr2[i])
highest = 0
while p1 >= 0:
    while p2 < m+1 and f[p1] + s[p2] <= k:
        highest = max(highest, p1+p2)
        p2 += 1
    p1 -= 1
    if p2 > 0:
        p2 -= 1
print(highest)

Solution (Python):

import sys

for a0 in range(1):
    n,m,x = input().strip().split(' ')
    n,m,x = [int(n),int(m),int(x)]
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    a.reverse()
    b.reverse()
    total = 0
    atmp = []
    for i in range(n):
        val = a.pop()
        if total + val > x:
            break
        total += val 
        atmp.append(val)

    max_count = len(atmp)
    cur_count = max_count
    while m:
        if total + b[-1] <= x:
            total += b.pop() 
            m -= 1
            cur_count += 1
            if cur_count > max_count:
                max_count = cur_count
            continue
        if not len(atmp):
            break
        aval = atmp.pop()
        total -= aval
        cur_count -= 1
    print (max_count)

Solution (Python):

N, M, K = [int(s) for s in input().split()]
A = [int(s) for s in input().split()]
B = [int(s) for s in input().split()]
i = 0
j = M-1

B_SUM = [0] * M
A_SUM = [0] * N
B_SUM[0] = B[0]
A_SUM[0] = A[0]
max_book = 0

if B_SUM[0] <= K:
    max_book = 1

for i in range(1, M):
    B_SUM[i] = B[i] + B_SUM[i - 1]
    if B_SUM[i] <= K:
        max_book = i + 1

A_SUM = 0
b_index = M - 1

for i in range(N):
    A_SUM += A[i]
    while (b_index >= 0):
        B_SUM[b_index]
        if A_SUM + B_SUM[b_index] <= K:
            max_book = max(max_book, b_index + 1 + i + 1)
            break
        b_index -= 1
    if (A_SUM <= K):
        max_book = max(max_book, i + 1)
print(max_book)

Solution (Python):
from sys import stdin





def ii(): return int(stdin.readline())
def fi(): return float(stdin.readline())
def mi(): return map(int, stdin.readline().split())
def fmi(): return map(float, stdin.readline().split())
def li(): return list(mi())
def si(): return stdin.readline().rstrip()
def lsi(): return list(si())

res=['NET', 'DA']


def bin_search(a, val):
    pos, l, r=0, 0, len(a)-1
    while l<=r:
        m=(l+r)//2
        if a[m]<=val:
            pos=m+1
            l=m+1
        else:
            r=m-1
    return pos


test_case=1
while test_case:
    test_case-=1


    n, m, k=mi()
    a=li()
    b=li()
    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=[0]+a
    for i in range(n+1):
        if a[i]<=k:
            c=bin_search(b, k-a[i])
            ans=max(ans, c+i)
    print(ans)

Solution (Python):
N, M, K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
a = -1
b = -1
total = 0
ans = 0
while a<len(A)-1:
    if total+A[a+1] > K:
        break
    a+=1
    total+=A[a]
    
while b<len(B)-1:
    if total+B[b+1] > K:
        break
    b+=1
    total+=B[b]
    
ans = a+b+2
subs = K - total
while a>=0:
    subs+=A[a]
    a-=1
    for i in range(b+1, len(B)):
        if subs < B[i]:
            break
        subs-=B[i]
        b = i
    ans = max(ans,a+b+2)
print(ans)

Solution (Python):
n,m,k = map(int,input().split())
a_list = list(map(int,input().split()))
b_list = list(map(int,input().split()))
sum_a = [a_list[0]]
sum_b = [b_list[0]]
for i in range(1,n):
  sum_a.append(sum_a[-1] + a_list[i])
  
for i in range(1,m):
  sum_b.append(sum_b[-1] + b_list[i])
import bisect
result = [0]
for i in range(n):
  border = k - sum_a[i]
  if border < 0:
    break
  index = bisect.bisect_right(sum_b, border)
  result.append(i+1+index)
  
for i in range(m):
  border = k - sum_b[i]
  if border < 0:
    break
  index = bisect.bisect_right(sum_a, border)
  result.append(i+1+index)
print(max(result))

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log,gcd
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')



 
import numpy as np
from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson
from scipy.sparse import csr_matrix

 

def ii(): return int(sys.stdin.readline().rstrip()) 
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) 
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]

def ss(): return sys.stdin.readline().rstrip() 
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) 
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]





n,m,k=mii()
arra=[0]+limii()
arrb=[0]+limii()

cuma=np.cumsum(arra)
cumb=np.cumsum(arrb)
ans=0
cnt=0
tmp=0
placea=0
for i in cuma:
    if i<=k:
        tmp=i
        placea+=1
        ans+=1
        cnt+=1
    else:
        continue

sumb=0
for i in arrb:
    
    
    if tmp+sumb+i<=k:
        tmp+=i
        sumb+=i
        cnt+=1
        
    else:

        while sumb+i+cuma[placea-1]>k:
            placea-=1
            cnt-=1
            
            if placea<1:
                break
        if sumb+i<=k:
            tmp+=i
            sumb+=i
            cnt+=1
        
    ans=max(ans,cnt)
    if placea<1:
        break
print(ans-2)

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

def main():
    n, m, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    ans = 0
    a_time = [0]
    b_time = [0]
    for i in range(1, len(a) + 1):
        a_time.append(a_time[i - 1] + a[i - 1])
    for i in range(1, len(b) + 1):
        b_time.append(b_time[i - 1] + b[i - 1])

    a_max = len(a_time) - 1
    b_max = len(b_time) - 1
    for i in range(1, len(a_time)):
        if a_time[i] > k:
            a_max = i - 1
            break
    for i in range(1, len(b_time)):
        if b_time[i] > k:
            b_max = i - 1
            break

    for i in range(len(b_time)):
        if a_time[a_max] + b_time[i] <= k:
            ans = max(ans, a_max + i)
        else:
            if a_max > 0:
                a_max -= 1
    
    
    
    
    
    

    print(ans)

if __name__ == '__main__':
    main()

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
from collections import deque
A=deque(list(accumulate(A)))
B=deque(list(accumulate(B)))
A.appendleft(0)
B.appendleft(0)
ans1=0
for i in range(N+1):
    if A[i]>K:
        ans1=i-1
        break
else:
    ans1=N
ans2=0
ans3=0

for i in range(ans1+1):

    while i+ans2<=M and A[ans1-i]+B[i+ans2]<=K:
        ans2+=1

    ans3=max(ans3,ans1+ans2-1)
print(ans3)

Solution (Python):
N,M,K=input().split()
N=int(N)
M=int(M)
K=int(K)
tmp=input().split()
A=[]
Atime=[]
for i in range(len(tmp)):
  A.append(int(tmp[i]))
  if i==0: Atime.append(A[0])
  else: Atime.append(Atime[i-1]+A[i])
tmp=input().split()
B=[]
Btime=[]
for i in range(len(tmp)):
  B.append(int(tmp[i]))
  if i==0: Btime.append(B[0])
  else: Btime.append(Btime[i-1]+B[i])

Blim=len(Btime)
for i in range(len(Btime)):
  if Btime[i]>K:
    Blim=i
    break
result=Blim

for i in range(len(Atime)):
  while Blim>0:
    if Atime[i]+Btime[Blim-1]<=K:
      if Blim+i+1>result: result=Blim+i+1
      break
    Blim-=1
  if Blim==0:
    if Atime[i]<=K:
      if i+1>result: result=i+1
print(result)

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

def main():
    n, m, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    ans = 0
    a_time = [0]
    b_time = [0]
    for i in range(1, len(a) + 1):
        a_time.append(a_time[i - 1] + a[i - 1])
    for i in range(1, len(b) + 1):
        b_time.append(b_time[i - 1] + b[i - 1])

    a_max = len(a_time) - 1
    b_max = len(b_time) - 1
    for i in range(1, len(a_time)):
        if a_time[i] > k:
            a_max = i - 1
            break
    for i in range(1, len(b_time)):
        if b_time[i] > k:
            b_max = i - 1
            break

    for i in range(len(b_time)):
        if a_time[a_max] + b_time[i] <= k:
            ans = max(ans, a_max + i)
        else:
            if a_max > 0:
                a_max -= 1
    for i in range(len(a_time)):
        if b_time[b_max] + a_time[i] <= k:
            ans = max(ans, b_max + i)
        else:
            if b_max > 0:
                b_max -= 1

    print(ans)

if __name__ == '__main__':
    main()

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

A_= [0]
for a in A:
  A_.append(A_[-1] + a)


b_sum = 0
ans = 0
b_cnt = 0

while True:
  if b_sum > K:
    break
  else:
    k = K - b_sum
    ans = max(bisect_right(A_, k)-1 + b_cnt, ans)
    
    if b_cnt == M:
      break
    b_sum += B[b_cnt]
    b_cnt += 1
    

print(ans)

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

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

cnt=0


for a in A:
  if K >= a:
    K-=a
    cnt+=1
  else:
    break

maxcnt=cnt
A=A[: cnt ]

for i,b in enumerate(B):
  
  
  while b>K and len(A)!=0:
    a = A.pop()
    K += a
    cnt -= 1
    
    
  if len(A) == 0:
    _B=B.copy()[i:]
   
    _B.pop(0)
    
    while(K>=b):
      K-=b
      cnt+=1
      
      if len(_B) == 0:
        break
      b=_B.pop(0)
    if maxcnt < cnt:
      maxcnt=cnt
    break

  K -= b
  cnt+=1
  
  
  if maxcnt < cnt:
    maxcnt=cnt



print(maxcnt)

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

A = [0]
B = [0]
A.extend(a)
B.extend(b)
time = sum(A)
ai = len(A)-1
bi = 0
con = 0
flg = None
count = 0

    

while True:
    
    
    if time > K:
        if flg is not None:
            con = max(con,flg-1)
            flg = None
        
        if ai == 0:
            break
        time -=A[ai]
        ai -= 1
    else:
        
        flg = ai+bi
        time +=B[bi]
        bi += 1
        if bi == len(B):
            if time > K:
                if flg is not None:
                    con = max(con, flg-1)
                pass
            else:
                con = max(con,ai+bi-1)
            break
    
print(con)

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

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

ans = 0
m = [0] + list(accumulate(A))
l = list(accumulate(B))
for i, x in enumerate(m):
    if x > K:
        break        
    else:
        ans = max(ans, i + bisect_right(l, K-x))
print(ans)

Solution (Python):
import sys
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
prog=0
a_idx=0
b_idx=0

while a_idx<n:
    if a[a_idx]+prog>k:
        break
    prog+=a[a_idx]
    a_idx+=1

if a_idx==n:
    while b_idx<m:
        if b[b_idx]+prog>k:
            break
        prog+=b[b_idx]
        b_idx+=1

mx=a_idx+b_idx

if mx==n+m:
    print(mx)
    sys.exit()

for i in range(a_idx+1):
    while b_idx<m and b[b_idx]+prog<=k:
        prog+=b[b_idx]
        b_idx+=1
    if a_idx+b_idx>mx:
        mx=a_idx+b_idx
    if b_idx==m:
        break
    if a_idx==0:
        break
    a_idx-=1
    prog-=a[a_idx]

print(mx)

Solution (Python):
import bisect

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





tmp = 0
for i in range(N):
    A[i], tmp = A[i] + tmp, tmp + A[i]
A = [0] + A

tmp = 0
for i in range(M):
    B[i], tmp = B[i] + tmp, tmp + B[i]
B = [0] + B

re = 0
for i, element in enumerate(A):
    K_tmp = K - element
    if K_tmp < 0:
        pass
    else:
        idx_B = bisect.bisect_right(B, K_tmp)
        re_tmp = i  + idx_B - 1
        re = max(re, re_tmp)
print(re)

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

MAX = 10 ** 18


accA, accB = [0] * (n + 1), [0] * (m + 1)
accA[1], accB[1] = A[0], B[0]
for i in range(2, n + 1):
    accA[i] = accA[i - 1] + A[i - 1]
for i in range(2, m + 1):
    accB[i] = accB[i - 1] + B[i - 1]
accA.append(MAX)
accB.append(MAX)

from bisect import bisect_left, bisect_right

res = 0
for i in range(n + 1):
    tmp = accA[i]
    idx = bisect_right(accB, k - tmp) - 1
    if tmp + accB[idx] <= k:
        res = max(res, i + idx)
print(res)

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

waA = [0]
for i in range(n):
    waA.append(waA[-1] + a[i])

waB = [0]
for j in range(m):
    waB.append(waB[-1] + b[j])

j = 0
mx = 0
for i in reversed(range(n + 1)):
    while j != m + 1 and waA[i] + waB[j] <= k:
        if mx < i + j:
            mx = i + j
        j += 1

print(mx)

Solution (Python):
import math
import bisect



n,m,k=map(int,input().split())
a=[int(x) for x in input().split()]
b=[int(x) for x in input().split()]
pa=[0]*n
pb=[0]*m
pa[0]=a[0]
pb[0]=b[0]
for i in range(1,n):
	pa[i]=pa[i-1]+a[i]
for i in range(1,m):
	pb[i]=pb[i-1]+b[i]

ans=0
for i in range(n):
	if pa[i]<=k:
		p=bisect.bisect_right(pb,k-pa[i])
		ans=max(ans,i+1+p)

p=bisect.bisect_right(pb,k)
ans=max(ans,p)
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

from itertools import accumulate

n, m, k = map(int, input().split())
a = [0]
for i in accumulate(map(int, input().split())):
    if i <= k:
        a.append(i)
b = [0]
for i in accumulate(map(int, input().split())):
    if i <= k:
        b.append(i)

ans = 0
a_index = 0
b_index = len(b) - 1
for a_index in range(len(a)):
    while a[a_index] + b[b_index] > k:
        b_index -= 1
        if b_index < 0:
            break
    if b_index < 0:
        break
    else:
        ans = max(ans, a_index + b_index)
print(ans)

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

for i in range(n):
    if samax + a[i]<=k:
        amax += 1
        samax += a[i]
    else:
        break
        
book = amax
ans = book
samax = sum(a[0:amax])
sbmax = 0

for j in range(m):
    sbmax += b[j]
    while samax + sbmax > k and amax >= 1:
        amax -= 1
        samax = samax - a[amax]
        book -= 1
    if samax + sbmax <= k:
        book += 1
        if book > ans:
            ans = book
    else:
        break

print(ans)

Solution (Python):
n,m,k = map(int, input().split())
la = list(map(int, input().split()))
lb = list(map(int, input().split()))
lt = []
x = 0
a_max = 0
t = 0
for i in range(n):
  t += la[i]
  if t > k:
    a_max = i-1
    t -= la[i]
    x = i
    lt.append(x)
    break
  if i == n-1:
    x = n
    a_max = n-1
    lt.append(x)

j = 0
for i in range(m):
  t += lb[i]
  x += 1
  if t > k:
    lt.append(x-1)
    if j <= a_max:
      t -= la[a_max-j]
      x -= 1
      j += 1
    else:
      break
  if i == m-1:
    lt.append(x)

print(max(lt))

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


lim = n+m
i = 0
cnt_a = 0
loop = len(a)
while k > 0 and i < loop:
    if k - a[i] >= 0:
        k -= a[i]
        i += 1
        cnt_a += 1
    else:
        break

i = 0
cnt_b = 0
loop = len(b)
while k > 0 and i < loop:
    if k - b[i] >= 0:
        k -= b[i]
        i += 1
        cnt_b += 1
    else:
        break

cnt = cnt_a + cnt_b
max_cnt = cnt

for j in range(cnt_a-1,-1,-1):
    if max_cnt == lim:
        break
    cnt -= 1
    k += a[j]
    while k > 0 and i < loop:
        if k - b[i] >= 0:
            k -= b[i]
            i += 1
            cnt += 1
        else:
            break
    if max_cnt < cnt:
        max_cnt = cnt

print(max_cnt)

Solution (Python):
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
s=0
pa,pb=[0,a[0]],[0,b[0]]
for i in range(1,n):
    pa.append(pa[-1]+a[i])
for i in range(1,m):
    pb.append(pb[-1]+b[i])
ac,bc=0,0
x,y=0,0
while ac<=n and pa[ac]<=k:
    ac+=1
    x=1

if x: ac-=1

while bc<=m and  pa[ac]+pb[bc]<=k:
    bc+=1
    y=1
if y: bc-=1
ans=ac+bc

for i in range(ac-1,-1,-1):
    for j in range(bc+1,m+1):
        if pa[i]+pb[j]>k: break
        bc=j
        ans=max(ans,i+j)
print(ans)

Solution (Python):
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
s = 0
b_count = 0
for i in range(m):
    if s + b[i] <= k:
        s += b[i]
        ans += 1
        b_count = i+1
    else:
        break
a_count = 0
while True:
    if a_count < n and s+a[a_count] <= k:
        s += a[a_count]
        a_count += 1
        ans += 1
    else:
        break
now = ans
for i in range(b_count-1, -1, -1):
    s -= b[i]
    now -= 1
    while True:
        if a_count < n and s +a[a_count] <= k:
            s += a[a_count]
            a_count += 1
            now += 1
        else:
            break
    ans = max(now, ans)
print(ans)

Solution (Python):
from collections import deque

N, M, K = 3, 4, 240
ARR = [60, 90, 120]
BRR = [80, 150, 80, 150]





N, M, K = 5, 4, 1
ARR = [1000000000, 1000000000, 1000000000, 1000000000, 1000000000]
BRR = [1000000000, 1000000000, 1000000000, 1000000000]

N, M, K = 7, 4, 14
ARR = [1, 6, 1, 1, 1, 7, 2]
BRR = [5, 1, 5, 6, 7]


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


def calculate(n, m, k, arr, brr):
    q = deque()

    result = []
    total = 0

    for i in range(n):
        if (total + arr[i]) > k:
            break
        total += arr[i]
        q.append(arr[i])

    result.append(len(q))

    remain = k - total

    q2 = deque(brr)
    sss = 0

    while (len(q2) > 0) and ((sss + q2[0]) <= remain):
        v = q2.popleft()
        sss += v

    result.append(len(q) + (m - len(q2)))

    while len(q) > 0:
        right = q.pop()
        remain += right

        while (len(q2) > 0) and ((sss + q2[0]) <= remain):
            v = q2.popleft()
            sss += v

        result.append(len(q) + (m - len(q2)))

    print(max(result))


calculate(N, M, K, ARR, BRR)

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

input = lambda: sys.stdin.readline().rstrip()
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

a_accum = list(accumulate(a))
b_accum = list(accumulate(b))



ans = 0
for i in range(len(a_accum)):
    nokori = k - a_accum[i]
    if nokori < 0:
        break
    
    b_pos = bisect.bisect(b_accum, nokori) - 1
    ans = max(ans, i + b_pos)
    
print(ans)

Solution (Python):
def count_books(books, limit):
    num_books = len(books)
    total_time = 0
    for i, time in enumerate(books):
        if total_time + time > limit:
            num_books = i
            break
        total_time += time
    return [num_books, total_time]

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

    max_num_A, total_time_A = count_books(A, K)

    read_book_nums = [0] * (max_num_A + 1)

    remain = K - total_time_A
    num_B, total_time_B = count_books(B, remain)
    read_book_nums[-1] = max_num_A + num_B

    num_A = max_num_A
    m = 0
    total_time_B = 0
    for n in range(max_num_A-1, -1, -1):
        num_A -= 1
        remain += A[n]

        while m < M and total_time_B + B[m] <= remain:
            total_time_B += B[m]
            m += 1
        num_B = m

        read_book_nums[n] = num_A + num_B

    print(max(read_book_nums))
    
main()

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

at = [0]
for i in range(len(A)):
    at.append(A[i])
    if i > 0:
        at[-1] += at[i]

bt = [0]
for i in range(len(B)):
    bt.append(B[i])
    if i > 0:
        bt[-1] += bt[i]


n = 0
time = 0


for i, a in enumerate(at):
    if a > K:
        break
    l = 0
    r = len(bt)-1
    while r - l > 1:
        m = (l+r)//2
        if bt[m] < K-a:
            l = m
        else:
            r = m
    if bt[r] <= K -a:
        b = bt[r]
    else:
        r -= 1
        b = bt[r]
    if i + r >= n:
        n = i + r

print(n)

Solution (Python):















mod = int(1e9+7)

INF = 1<<50

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

def main():
    n,m,k = readInt()
    a = readInt()
    b = readInt()
    a_sum = [0]
    for i in range(n):
        a_sum.append(a_sum[i] + a[i])
    b_sum = [0]
    for i in range(m):
        b_sum.append(b_sum[i] + b[i])
    b_sum.append(INF)
    ans = 0
    for i in range(n+1):
        t = a_sum[i]
        l = 0
        r = m+2
        while l+1<r:
            c = (l + r) // 2
            if t+b_sum[c]<=k:
                l = c
            else:
                r = c
        if a_sum[i]+b_sum[l]<=k:
            ans = max(ans,i+l)
    print(ans)
    return

if __name__=='__main__':
    main()

Solution (Python):
from itertools import accumulate


def binary(N, T):  
    l, r = -1, N
    while r - l > 1:
        if T < b[(l + r) // 2]:  
            r = (l + r) // 2
        else:
            l = (l + r) // 2
    return r - 1


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

ans = 0
for i, ai in enumerate(a):
    t = k - ai
    if t >= 0:
        j = binary(m + 1, t)
        ans = max(ans, i + j)
print(ans)

Solution (Python):
import bisect


def m():
    N, M, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    ans = 0
    AR = [0] * (N + 1)
    BR = [0] * (M + 1)
    for i in range(N):
        AR[i + 1] = AR[i] + A[i]
    for i in range(M):
        BR[i + 1] = BR[i] + B[i]

    ai = bisect.bisect_right(AR, K)
    for a in range(ai-1, -1, -1):
        bk = K - AR[a]
        bi = bisect.bisect_right(BR, bk)

        ans = max(a + bi - 1, ans)
    return ans


print(m())

Solution (Python):
from itertools import accumulate


def binary(N, T):  
    l, r = -1, N + 1
    while r - l > 1:
        if T < b[(l + r) // 2]:  
            r = (l + r) // 2
        else:
            l = (l + r) // 2
    return r - 1


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

ans = 0
for i, ai in enumerate(a):
    t = k - ai
    if t >= 0:
        j = binary(m, t)
        ans = max(ans, i + j)
print(ans)

Solution (Python):
import sys
import itertools
import bisect

N, M, K = map(int, input().split())
A = list(map(int, sys.stdin.readline().rsplit()))
B = list(map(int, sys.stdin.readline().rsplit()))

sA = [0] + list(itertools.accumulate(A))
sB = [0] + list(itertools.accumulate(B))

res = 0
for i, a in enumerate(sA):
    if K < a:
        break
    j = bisect.bisect_right(sB, K - a) - 1
    res = max(res, i + j)

print(res)

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

from itertools import accumulate
from bisect import bisect_right
aacc = list(accumulate(As))
bacc = list(accumulate(Bs))


ans = 0
for i in range(n):
    a = aacc[i]
    if k - a < 0:continue
    
    n = bisect_right(bacc, k-a)
    
    ans = max(i+1+n,ans)

for i in range(m):
    a = bacc[i]
    if k - a < 0:continue
    n = bisect_right(aacc, k-a)
    ans = max(i+1+n,ans)


print(ans)

Solution (Python):


import sys
iim = lambda: map(int, sys.stdin.readline().rstrip().split())

def resolve():
    it = map(int, sys.stdin.read().split())
    N, M, K = next(it), next(it), next(it)

    A = [0] * (N+1)
    B = [0] * (M+1)
    ans = 0
    for i in range(1, N+1):
        A[i] = A[i-1] + next(it)
    for i in range(1, M+1):
        B[i] = B[i-1] + next(it)

    lo = 0
    hi = 2*(N+M)+1
    while lo < hi:
        mid = (lo+hi)//2
        for j in range(max(0, mid-M), min(mid, N)+1):
            if A[j] + B[mid-j] <= K:
                lo = mid+1
                break
        else:
            hi = mid

    print(lo-1)


if __name__ == "__main__":
    resolve()

Solution (Python):
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
t = 0
num = 0
bt = 0
bi = 0
for i in range(n):
    t += a[i]
    if t > k:
        t -= a[i]
        break
    else:
        num += 1

for j in range(m):
    t += b[j]
    if t > k:
        t -= b[j]
        break
    else:
        bt += b[j]
        bi += 1

inum = num
num += bi
cnum = num

for i in range(inum):
    t -= a[inum - i - 1]
    cnum -= 1
    while bi < m:
        t += b[bi]
        if t > k:
            t -= b[bi]
            break
        else:
            bi += 1
            cnum += 1
    if num < cnum:
        num = cnum
print(num)

Solution (Python):
import itertools

N, M, K = map(int, input().split())
a = list(input().split())
b = list(input().split())
A = list(itertools.accumulate([int(a[i]) for i in range(N)]))
B = list(itertools.accumulate([int(b[i]) for i in range(M)]))

count = 0
tmp = K

for i in range(N+1):
    low = 0
    high = M - 1
    while low <= high:
        mid = int((low + high) // 2)
        guess = B[mid]
        if low == high or guess == tmp:
            break
        elif guess > tmp:
            high = mid - 1
        else:
            low = mid + 1
    count = max(count, (i+mid+1 if B[mid] <= tmp else i+mid))
    if i == N:
        break
    else:
        tmp = K - A[i]
        if tmp < 0:
            break

print(count)

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, gcd, sqrt
from operator import mul
from functools import reduce
from operator import mul
from pprint import pprint



sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
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)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, m, k = LI()
A = LI()
B = LI()
a_acc = [0] + list(accumulate(A))
b_acc = [0] + list(accumulate(B))
ans = 0
for i in range(n + 1):
    if a_acc[i] > k:
        continue
    idx = bisect_right(b_acc, k - a_acc[i])
    ans = max(ans, i + idx - 1)

print(ans)

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

import itertools
al_su = list(itertools.accumulate(al))
bl_su = list(itertools.accumulate(bl))

import bisect
ka = bisect.bisect(al_su,k)

kc = 0

for i in range(ka):
    kk = k - al_su[i]
    kb = bisect.bisect(bl_su,kk)
    kc = max(kc,i+1+kb)

kb = bisect.bisect(bl_su,k)

for j in range(kb):
    kk = k - bl_su[j]
    ka = bisect.bisect(al_su,kk)
    kc = max(kc,j+1+ka)

print(kc)

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()))
ex=0
inda=-1
t=0
for i in range(len(a)):
    ex+=a[i]
    if ex<=k:
        inda=i
        t+=a[i]
    else:
        break
if inda!=-1:
    ans=inda+1
else:
    ans=0
aq=deque(a[:inda+1])
bq=deque(b)
now=ans
while len(aq)!=0 and len(bq)!=0:
    if t+bq[0]<=k:
        t+=bq.popleft()
        now+=1
        ans=max(ans,now)
    else:
        t+=bq.popleft()
        t-=aq.pop()
if len(bq)==0:
    print(ans)
else:
    while len(bq)!=0:
        t+=bq.popleft()
        if t<=k:
            now+=1
            ans=max(ans,now)
        else:
            break
    print(ans)

Solution (Python):
n, m, k = [int(_) for _ in input().split()]

a = [int(_) for _ in input().split()]
b = [int(_) for _ in input().split()]

b.append(k+100)
cnt = 0
ans1, ans2 = 0, 0
for i in range(n-1):
    a[i+1] += a[i]
    if a[i+1] <= k:
        ans1 = i+2
for j in range(m-1):
    b[j+1] += b[j]
    if b[j+1] <= k:
        ans2 = j+2

ans = max(ans1, ans2)
for i in range(n):
    nokori = k-a[i]
    r, l = 0, m
    if nokori < 0:
        break
    if b[0] > nokori:
        ans = max(i+1, ans)
        continue

    while(l-r > 1):
        if b[(r+l)//2] <= nokori:
            r = (r+l)//2
        else:
            l = (r+l)//2
        
    ans = max(i+l+1, ans)
    
print(ans)