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):
def main():
    n, m, k = map(int, input().split())
    a = [int(x) for x in input().split()]
    b = [int(x) for x in input().split()]

    a_sum = [0]
    b_sum = [0] 

    max_counter = 0

    j = m

    for i in range(n):
        a_sum.append(a_sum[i] + a[i])

    for i in range(m):
        b_sum.append(b_sum[i] + b[i])

    for i in range(n + 1):
        if a_sum[i] > k:
            break

        while k < a_sum[i] + b_sum[j]:
            j -= 1
        
        max_counter = max(max_counter, i + j)

    print(max_counter)

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

a, b = [0], [0]
tmp = 0
for i in range(N):
  tmp += A[i]
  a.append(tmp)
tmp = 0
for i in range(M):
  tmp += B[i]
  b.append(tmp)

j = len(b)-1
ans = 0
for i in range(len(a)):
  if a[i] > K:
    break
  while a[i] + b[j] > K:
    j -= 1
  ans = max(ans, i+j)
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()))

    SUMA = [0]
    for a in A:
        if SUMA[-1] + a > K: break
        SUMA.append(SUMA[-1] + a)
    SUMB = [0]
    for b in B:
        if SUMB[-1] + b > K: break
        SUMB.append(SUMB[-1] + b)

    if len(SUMA) == 1:
        ans = len(SUMB) - 1
    elif len(SUMB) == 1:
        ans = len(SUMA) - 1
    else:
        ans = 0
        i = len(SUMA)
        j = 1
        while i:
            i -= 1
            while SUMA[i] + SUMB[j] <= K:
                j += 1
                if j == len(SUMB): break
            j -= 1
            ans = max(ans, i + j)

    print(ans)


if __name__ == '__main__':
    main()

exit()

Solution (Python):
N, M, K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
a, b=[0], [0]
for i in range(N):
    a.append(a[i]+A[i])
for i in range(M):
    b.append(b[i]+B[i])
ans, j=0, M
for i in range(N+1):
    if a[i]>K:
        break
    while b[j]>K-a[i]:
        j-=1
    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()))

a, b = [0], [0]
tmp = 0
for i in range(N):
  tmp += A[i]
  a.append(tmp)
tmp = 0
for i in range(M):
  tmp += B[i]
  b.append(tmp)

total = 0
count = []
best_j = len(b)-1
for i in range(len(a)):
  if a[i] <= K:
    for j in range(best_j, -1, -1):
      total = a[i] + b[j]
      if total <= K:
        count.append(i+j)
        best_j = j
        break
if len(count) == 0:
  print(0)
else:
  print(max(count))

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

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

for i in range(n): 
	
    
 acum1.append(acum1[-1]+arr1[i])
acum2=[0]
for i in range(m):
 acum2.append(acum2[-1]+arr2[i])
ans=0
j=m
for i in range(n+1):
    
    
    
 if acum1[i]>k:
   break
       
    
    
 while acum2[j]>k-acum1[i] and j>0: 
   j-=1
 ans=max(ans,i+j)
print(ans)

Solution (Python):
n,m,k=map(int,input().split())
arr1=list(map(int,input().split()))
arr2=list(map(int,input().split()))
acum1=[0]
for i in range(n): 
 acum1.append(acum1[-1]+arr1[i])
acum2=[0]
for i in range(m):
 acum2.append(acum2[-1]+arr2[i])
ans=0
j=m
for i in range(n+1):
 if acum1[i]>k: 
   break
 while acum2[j]>k-acum1[i] and j>0: 
   j-=1
 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()))
 
a,b = [0],[0]
for i in range(N):
  a.append(a[i] + A[i])
for i in range(M):
  b.append(b[i] + B[i])
  
ans = 0
j = M
for i in range(N + 1):
  if(a[i] > K):
    break
  while(a[i] + b[j] > K):
    j -= 1
  ans = max(ans, i+j)
print(ans)

Solution (Python):
from bisect import*

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

sa=[0]+[0]*N
i=0
while i<N:
    sa[i+1]=sa[i]+A[i]
    i+=1

sb=[0]+[0]*M
j=0
while j<M:
    sb[j+1]=sb[j]+B[j]
    j+=1

i=bisect_right(sa,K)-1
ans=0
while 0<=i:
    j=bisect_right(sb,K-sa[i])-1
    ans=max(ans,i+j)
    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()))

a,b = [0],[0]
for i in range(N):
  a.append(a[i] + A[i])
for i in range(M):
  b.append(b[i] + B[i])
  
ans = 0
j = M
for i in range(N + 1):
  if(a[i] > K):
    break
  while(a[i] + b[j] > K):
    j -= 1
  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()))

j = m - 1
cost = sum(b)
cnt = m
ans = 0
for i in range(n + 1):
    if i != 0:
        cost += a[i - 1]
        cnt += 1
    while cost > k and j >= 0:
        cost -= b[j]
        cnt -= 1
        j -= 1
    if cost <= k:
        ans = max(cnt, ans)
print(ans)

Solution (Python):
n,m,k=map(int,input().split())
a=[int(j)for j in input().split()]
b=[int(j)for j in input().split()]
from itertools import accumulate
a=[0]+list(accumulate(a))
b=[0]+list(accumulate(b))
ans=0
j=m
for i in range(n+1):
    if a[i]<=k:
        while a[i]+b[j]>k:j-=1
        ans=max(ans,i+j)
print(ans)

Solution (Python):
from itertools import accumulate

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

a_cumsum = [0] + list(accumulate(a))
b_cumsum = [0] + list(accumulate(b))
j = m
res = 0
for i in range(n+1):
    if a_cumsum[i] > k:
        break
    while a_cumsum[i] + b_cumsum[j] > k:
        j -= 1
    if (v := i + j) > res:
        res = v
print(res)

Solution (Python):
import bisect

def Csum(a):
    b,c=[0],0
    for i in range(len(a)):
        c+=a[i]
        b.append(c)
    return b

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

Solution (Python):
def main():
    import sys
    sys.setrecursionlimit(10**9)
    input = sys.stdin.readline

    INF = 10**18
    n, m, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    A = [0]
    for i in range(n):
        A.append(a[i] + A[i])
    B = [0]
    for i in range(m):
        B.append(b[i] + B[i])

    ans = 0
    j = m
    for i in range(n + 1):
        if A[i] > k:
            break
        while k < A[i] + B[j]:
            j -= 1
        ans = max(ans, i + j)
    print(ans)

main()

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

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

    import itertools
    import bisect
    ac = [0] + list(itertools.accumulate(A))
    bc = [0] + list(itertools.accumulate(B))

    ans = 0
    for i, a in enumerate(ac):
        if a > K:
            break
        j = bisect.bisect_right(bc, K - a)
        ans = max(ans, i+j-1)
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    n, m, k = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    nid = m-1
    s = sum(B)
    cnt = m
    while s > k:
        s -= B[nid]
        cnt -= 1
        nid -= 1

    ans = cnt
    for a in A:
        s += a
        cnt += 1
        while s > k and nid >= 0:
            s -= B[nid]
            cnt -= 1
            nid -= 1
        if s <= k:
            ans = max(cnt, ans)
    print(ans)

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()))
ans=0
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])
for i in range(N+1):
  if a[i]>K:
    break
  c=K-a[i]
  ans=max(ans,i+bisect.bisect_right(b,c)-1)
print(ans)

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

A, B = [0], [0]

for i in range(n):
  A.append(a[i] + A[i])
for i in range(m):
  B.append(b[i] + B[i])

ans, j = 0, m
for i in range(n+1):
  if A[i] > k:
    break
  while B[j] > k - A[i]:
    j -= 1
  ans = max(ans, i+j)
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()))

from itertools import accumulate
sum_a_list = list(accumulate([0] + a_list))
sum_b_list = list(accumulate([0] + b_list))

ans = 0
b_idx = m
for a_idx in range(n+1):
    if sum_a_list[a_idx] > k:
        continue
 
    while sum_a_list[a_idx] + sum_b_list[b_idx] > k:
        b_idx -= 1
    
    ans = max(ans, a_idx + b_idx)
        
print(ans)

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

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

    import itertools
    ac = [0] + list(itertools.accumulate(A))
    bc = [0] + list(itertools.accumulate(B))
    ans, j = 0, M
    for i, a in enumerate(ac):
        if a > K:
            break
        while a + bc[j] > K:
            j -= 1
        ans = max(ans, i+j)
    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()))

aL = [0 for _ in range(n+1)]
bL = [0 for _ in range(m+1)]

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

ans = 0
j = m

for i in range(n+1):
    if aL[i] > k:
        break
    while bL[j] > k - aL[i]:
        j -= 1
    ans = max(ans, i+j)

print(ans)

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

    a_culm = [0]
    for i in range(n):
        a_culm.append(a_culm[-1] + a[i])
    b_culm = [0]
    for i in range(m):
        b_culm.append(b_culm[-1] + b[i])

    ans = 0
    for i in range(n+1):
        rem = k - a_culm[i]
        if rem < 0:
            break
        b_count = bisect.bisect(b_culm, rem) - 1
        if i + b_count > ans:
            ans = i + b_count

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

t=sum(b)

j=m
ans=0
for i in range(n+1):
  while j>0 and t > k:
    j-=1
    t -= b[j]
  if t > k:
    break
  ans = max(ans,i+j)
  if i==n:
    break
  t += a[i]
print(ans)

Solution (Python):
n, m, k = map(int,input().split(' '))
a = [0] + list(map(int, input().split(' ')))
b = [0] + list(map(int, input().split(' ')))
j = m
ta, tb =0, sum(b)
r = 0
for i in range(n + 1):
    ta += a[i]
    if ta > k:
        break
    while tb > k - ta:
        tb -= b[j]
        j -= 1
    r = max(r, i + j)
print(r)

Solution (Python):
import sys
import itertools
import bisect

input = sys.stdin.readline

def main():
    N, M, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    Acum = list(itertools.accumulate(A))
    Bcum = list(itertools.accumulate(B))
    A = A[:bisect.bisect_right(Acum, K)]
    B = B[:bisect.bisect_right(Bcum, K)]
    base = sum(A)
    books = len(A)
    ans = max(books, len(B))
    i = 0; Bflag = True
    while True:
        if Bflag:
            if i == len(B):
                break
            tmp = base + B[i]
            if tmp > K:
                if len(A) == 0:
                    break
                Bflag = False
                continue
            base = tmp
            books += 1
            i += 1
            ans = max(books, ans)
        else:
            if len(A) == 0:
                Bflag = True
            Atmp = A.pop()
            base -= Atmp
            books -= 1
            if base <= K:
                Bflag = True
    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()))

a, b = [0], [0]
for i in range(n):
    a.append(a[i] + A[i])
for j in range(m):
    b.append(b[j] + B[j])

ans, j = 0, m
for i in range(n + 1):
    if a[i] > k:
        break
    while b[j] > k - a[i]:
        j -= 1
    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()))

t = sum(b)
j = m
ans = 0

for i in range(n+1):
    while(j > 0 and t > k):
        j -= 1
        t -= b[j]
    if(t > k): break
    ans = max(ans, i+j)
    if(i == n): break
    t += a[i]
print(ans)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from collections import deque
def main():
    N, M, K = map(int, readline().split())
    A = [int(i) for i in readline().split()]
    B = [int(i) for i in readline().split()]

    ca = [0]
    cb = [0]
    for i in range(N):
        ca.append(ca[i] + A[i])
    for i in range(M):
        cb.append(cb[i] + B[i])

    ans = 0  
    j = M
    for i in range(N+1):
        if K < ca[i]:
            break
        while cb[j] > K-ca[i]:
            j -= 1
        ans = max(ans, i+j)
    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()))

sa = [0] * (n+1)
sb = [0] * (m+1)
r = m
ans = 0

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

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):
n,m,k=map(int, input().split())
a=list(map(int, input().split()))
b=list(map(int, input().split()))

from itertools import accumulate
a_acc=list(accumulate(a))
b_acc=list(accumulate(b))
a_acc.insert(0,0)
b_acc.insert(0,0)

cnt=0

import bisect
for i in range(n+1):
    nokori=k-a_acc[i]
    if nokori<0:
        break
    b_i=bisect.bisect_right(b_acc,nokori)-1
    cnt=max(cnt,i+b_i)
    

print(cnt)

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

def accumulate(l, X):
    acclist = [0]*(l+1)
    for i in range(1,l+1):
        acclist[i] = acclist[i-1] + X[i]
    return acclist

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

X = accumulate(N, A)
Y = accumulate(M, B)

maxbook = 0
j = M
for i in range(N+1):
    x = X[i]
    y = Y[j]
    while x + y > K:
        j -= 1 
        if j < 0:
            break
        y = Y[j]
    if j < 0:
        break

    maxbook = max(maxbook, i + j)

print(maxbook)

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

ans = 0
for i in range(n+1):
	if a[i] > k:
		break
	ans = max(ans, i + bisect(b, k-a[i]) - 1)
print(ans)

Solution (Python):
import os
import sys
from atexit import register
from io import BytesIO
sys.stdin = BytesIO(os.read(0, os.fstat(0).st_size))
sys.stdout = BytesIO()
register(lambda: os.write(1, sys.stdout.getvalue()))
input = lambda: sys.stdin.readline().rstrip('\r\n')
raw_input = lambda: sys.stdin.readline().rstrip('\r\n')

n,m,k = (int(x) for x in input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
prea = [0,]
preb = [0,]
for i in range(n):
    prea.append(prea[-1] + a[i])
for i in range(m):
    preb.append(preb[-1] + b[i])
ans = 0
j = m
for i in range(n+1):
    t = prea[i]
    while j>0 and t + preb[j] > k:
        j -= 1
    if t + preb[j] <= k:
        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()))

a = [0]*(n+1)
b = [0]*(m+1)

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

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

Solution (Python):
N,M,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
a,b = [0],[0]
for i in range(N):  
    a.append(a[i]+A[i])
for i in range(M):  
    b.append(b[i]+B[i])

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

Solution (Python):

import sys
import numpy as np
input = sys.stdin.readline

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


A_list.insert(0, 0)
B_list.insert(0, 0)
A_cumsum = np.cumsum(A_list)
B_cumsum = np.cumsum(B_list)

time = B_cumsum[-1]
ans, j = 0, M



for i in range(N+1):
    if A_cumsum[i] > K:
        break
    while B_cumsum[j] > K - A_cumsum[i]:
        j -= 1
    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()))

a, b = [0], [0]
for i in range(N):
  a.append(a[i] + A[i])
for i in range(M):
  b.append(b[i] + B[i])

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

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

input = sys.stdin.readline


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

    B_cumsum = tuple(accumulate(B))
    ans = 0
    time = 0
    for i in range(N + 1):
        if time > K:
            break
        j = bisect.bisect_right(B_cumsum, K - time)
        ans = max(ans, i + j)

        if i < N:
            time += A[i]

    print(ans)


if __name__ == "__main__":
    main()

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

input = sys.stdin.readline


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

    B_cumsum = list(accumulate(B))
    ans = 0
    time = 0
    for i in range(N + 1):
        if time > K:
            break
        j = bisect.bisect_right(B_cumsum, K - time)
        ans = max(ans, i + j)

        if i < N:
            time += A[i]

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
from collections import deque
def readinput():
    n,m,k=list(map(int,input().split()))
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    return n,m,k,a,b

def main(n,m,k,a,b):
    qa=deque(a)
    qb=deque(b)
    count=0
    res=k
    while(len(qa)>0 and len(qb)>0):
        if qa[0] < qb[0]:
            res-=qa[0]
            qa.popleft()
        else:
            res-=qb[0]
            qb.popleft()
        if res<0:
            return count
        count+=1
    else:
        if len(qa)==0:
            while(len(qb)>0):
                res-=qb[0]
                qb.popleft()
                if res<0:
                    return count
                count+=1
            return count
        else:
            while(len(qa)>0):
                res-=qa[0]
                qa.popleft()
                if res<0:
                    return count
                count+=1
            return count

import sys
INFTY=sys.maxsize
def find(t,s):
    '''
    単調増加配列sからs[i]がtを超えない最大のiを返す
    '''
    ss=s[:]
    ss.append(INFTY)
    left=0
    right=len(ss)
    while(left<right):
        middle=(left+right)//2
        
        if ss[middle]==t:
            
            return middle
        if t<ss[middle]:
            right=middle
        else:
            left=middle+1
    else:
        return left-1

def main2(n,m,k,a,b):
    sa=[0]
    for i in range(1,n+1):
        sa.append(sa[i-1]+a[i-1])
    sb=[0]
    for i in range(1,m+1):
        sb.append(sb[i-1]+b[i-1])
    maxab=0
    nb=m+1
    for na in range(0,n+1):
        res=k-sa[na]
        if res>=0:
            nb=find(res,sb[:nb+1])
            
            maxab=max(maxab,na+nb)
    return maxab

from bisect import bisect_left, bisect_right
def find_le(a, x):
    'Find rightmost index less than or equal to x'
    i = bisect_right(a, x)
    if i:
        return i-1
    raise ValueError

def main3(n,m,k,a,b):
    sa=[0]
    for i in range(1,n+1):
        sa.append(sa[i-1]+a[i-1])
    sb=[0]
    for i in range(1,m+1):
        sb.append(sb[i-1]+b[i-1])
    maxab=0
    nb=m+1
    for na in range(0,n+1):
        res=k-sa[na]
        if res>=0:
            nb=find_le(sb,res)
            
            maxab=max(maxab,na+nb)
    return maxab

def main4(n,m,k,a,b):
    sa=[0]
    for i in range(1,n+1):
        sa.append(sa[i-1]+a[i-1])
    sb=[0]
    for i in range(1,m+1):
        sb.append(sb[i-1]+b[i-1])
    maxab=0
    nb=m
    for na in range(0,n+1):
        res=k-sa[na]
        while res<sb[nb] and nb>=0:
            nb-=1
        else:
            if nb>=0:
                maxab=max(maxab,na+nb)
        
    return maxab

def readinput2():
    m=2*10**5
    n=2*10**5
    a=[10**9]*n
    b=[10**9]*m
    k=1
    return n,m,k,a,b

if __name__=='__main__':
    n,m,k,a,b=readinput()
    ans=main3(n,m,k,a,b)
    print(ans)

Solution (Python):

from itertools import accumulate

N,M,K = map(int,input().split())
A, B = [0], [0]
A.extend(list(accumulate(map(int,input().split()))))
B.extend(list(accumulate(map(int,input().split()))))

def it():
    j = M
    for i ,a in enumerate(A):
        while j >= 0 and a + B[j] > K:
            j -= 1
        if j >= 0:
            yield i + j
            
print(max(it()))

Solution (Python):
import sys












stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)
MIN = -10 ** 9
MOD = 10 ** 9 + 7
INF = float("inf")
IINF = 10 ** 18

def solve():
    
    N,M,K = map(int, stdin.readline().rstrip().split())
    A = list(map(int, stdin.readline().rstrip().split()))
    B = list(map(int, stdin.readline().rstrip().split()))
    
    
    
    
    a = [0]
    for i in range(N):
        a.append(a[i] + A[i])
    b = [0]
    for i in range(M):
        b.append(b[i] + B[i])
    ans, j = 0, M
    for i in range(N + 1):
        if a[i] > K:
            break
        while b[j] > K - a[i]:
            j -= 1
        ans = max(ans, i + j)
    print(ans)


if __name__ == '__main__':
    solve()

Solution (Python):
N,M,K=list(map(int,input().split()))
a_list = list(map(int,input().split()))
b_list = list(map(int,input().split()))
ans = 0
b_sum=[0]
cnt = 0
for i,b in enumerate(b_list):
  b_sum.append(b_sum[i] + b)
  if b_sum[i+1]<=K:
    cnt+=1
    ans=max(ans,cnt)

a_sum=0
for i,a in enumerate(a_list):
  a_sum+=a
  pos = K - a_sum
  books = i+1
  if pos<0:
    break
  
  left = 0
  right = len(b_sum)-1
  while left<=right:
    mid = (left+right)//2

    if b_sum[mid]>pos:
        right = mid - 1
    else:
        left = mid + 1
  books+=right
  ans = max(ans, books)

print(ans)

Solution (Python):
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
a, b = [0], [0]
for i in range(N):
  a.append(a[i] + A[i])
for i in range(M):
  b.append(b[i] + B[i])
ans, j = 0, M
for i in range(N + 1):
  if a[i] > K:
    break
  while b[j] > K - a[i]:
    j -= 1
  ans = max(ans, i + j)
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()))
new_a_list = [[0, 0]]
a_sum = 0
for i in range(0, n):
    a_sum += a_list[i]
    if a_sum <= k:
        new_a_list.append([i + 1, a_sum])
    else:
        break

best = len(new_a_list) - 1
b_sum = 0
a_sum = new_a_list[-1][1]
a_count = new_a_list[-1][0]
flag = 1
for i in range(0, m):
    b_sum += b_list[i]
    while True:
        if b_sum <= k - a_sum:
            if best < i + 1 + a_count:
                best = i + 1 + a_count
            break
        if a_count == 0:
            flag = 0
            break
        a_count -= 1
        a_sum = new_a_list[-(len(new_a_list) - a_count)][1]
    if flag == 0:
        break
print(best)

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

ta, tb = 0, sum(B)
ptr = M
ans = 0
for i in range(N+1):
    while ptr > 0 and ta + tb > K:
        ptr -= 1
        tb -= B[ptr]
    if ta + tb <= K:
        ans = max(ans, i + ptr)
    if i < N:
        ta += A[i]
print(ans)

Solution (Python):
from collections import deque
def readinput():
    n,m,k=list(map(int,input().split()))
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    return n,m,k,a,b

def main(n,m,k,a,b):
    qa=deque(a)
    qb=deque(b)
    count=0
    res=k
    while(len(qa)>0 and len(qb)>0):
        if qa[0] < qb[0]:
            res-=qa[0]
            qa.popleft()
        else:
            res-=qb[0]
            qb.popleft()
        if res<0:
            return count
        count+=1
    else:
        if len(qa)==0:
            while(len(qb)>0):
                res-=qb[0]
                qb.popleft()
                if res<0:
                    return count
                count+=1
            return count
        else:
            while(len(qa)>0):
                res-=qa[0]
                qa.popleft()
                if res<0:
                    return count
                count+=1
            return count

import sys
INFTY=sys.maxsize
def find(t,s):
    '''
    単調増加配列sからs[i]がtを超えない最大のiを返す
    '''
    ss=s[:]
    ss.append(INFTY)
    left=0
    right=len(ss)
    while(left<right):
        middle=(left+right)//2
        
        if ss[middle]==t:
            
            return middle
        if t<ss[middle]:
            right=middle
        else:
            left=middle+1
    else:
        return left-1

def main2(n,m,k,a,b):
    sa=[0]
    for i in range(1,n+1):
        sa.append(sa[i-1]+a[i-1])
    sb=[0]
    for i in range(1,m+1):
        sb.append(sb[i-1]+b[i-1])
    maxab=0
    nb=m+1
    for na in range(0,n+1):
        res=k-sa[na]
        if res>=0:
            nb=find(res,sb[:nb+1])
            
            maxab=max(maxab,na+nb)
    return maxab

from bisect import bisect_left, bisect_right
def main3(n,m,k,a,b):
    sa=[0]
    for i in range(1,n+1):
        sa.append(sa[i-1]+a[i-1])
    sb=[0]
    for i in range(1,m+1):
        sb.append(sb[i-1]+b[i-1])
    maxab=0
    nb=m+1
    for na in range(0,n+1):
        res=k-sa[na]
        if res>=0:
            nb=bisect_right(sb,res)-1
            
            maxab=max(maxab,na+nb)
    return maxab

def main4(n,m,k,a,b):
    sa=[0]
    for i in range(1,n+1):
        sa.append(sa[i-1]+a[i-1])
    sb=[0]
    for i in range(1,m+1):
        sb.append(sb[i-1]+b[i-1])
    maxab=0
    nb=m
    for na in range(0,n+1):
        res=k-sa[na]
        while res<sb[nb] and nb>=0:
            nb-=1
        else:
            if nb>=0:
                maxab=max(maxab,na+nb)
        
    return maxab

def readinput2():
    m=2*10**5
    n=2*10**5
    a=[10**9]*n
    b=[10**9]*m
    k=1
    return n,m,k,a,b

if __name__=='__main__':
    n,m,k,a,b=readinput()
    ans=main3(n,m,k,a,b)
    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 = list(map(int, input().split()))
A = list(filter(lambda x: x <= k, accumulate(A)))
B = list(filter(lambda x: x <= k, accumulate(B)))
ans = 0
for i, a in enumerate(A):
    j = bisect_right(B, k-a)
    ans = max(ans, i+j+1)
ans = max(ans, len(B))
print(ans)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from collections import deque
def main():
    N, M, K = map(int, readline().split())
    A = [int(i) for i in readline().split()]
    B = [int(i) for i in readline().split()]

    
    

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

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

    mx = 0
    for i in range(N+1):
        
        r = K-csumA[i]
        if r < 0:
            break
        left = -1
        right = M+1
        while right-left>1: 
            mid = (left+right)//2
            if r >= csumB[mid]:
                left = mid
            else:
                right = mid

        mx = max(mx, i+left)
    print(mx)

if __name__ == '__main__':
    main()

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from collections import deque
def main():
    N, M, K = map(int, readline().split())
    A = [int(i) for i in readline().split()]
    B = [int(i) for i in readline().split()]

    
    

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

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

    mx = 0
    for i in range(N+1):
        
        if K < csumA[i]:
            break
        left = -1
        right = M+1
        while right-left>1: 
            mid = (left+right)//2
            if K - csumA[i] >= csumB[mid]:
                left = mid
            else:
                right = mid
        mx = max(mx, i+left)
    print(mx)

if __name__ == '__main__':
    main()