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):
INF = float('inf')
N, M, K = list(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,x in enumerate(A):
    a[i+1] = a[i] + x

for i,x in enumerate(B):
    b[i+1] = b[i] + x

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









ans=0

accum_a=[0]
for i in a:
    _a=accum_a[-1]+i
    if _a>K:
        break
    else:
        accum_a.append(_a)
accum_a.append(K)

accum_b=[0]
for i in b:
    accum_b.append(accum_b[-1]+i)

ans=0

for j in range (len(accum_a)-1):
    Kb=K-accum_a[j]
    bookB=bisect.bisect_right(accum_b,Kb)-1
    if j+bookB>ans:
        ans=j+bookB
    else:
        pass

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

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

j = m

for i in range(n + 1):
    if a[i] > k:
        break
    while a[i] + b[j] > k:
        j -= 1
    else:
        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()))
cum_a = []
total = 0
for i in range(n):
  total += a[i]
  if total > k:
    break
  cum_a.append(total)
  
cum_b = []
total = 0
for j in range(m):
  total += b[j]
  if total > k:
    break
  cum_b.append(total)
lim_a = len(cum_a)
lim_b = len(cum_b)
idx = 0
ans = lim_a
for l in range(1, lim_a + 1):
  while idx < lim_b:
    if k >= cum_a[-l] + cum_b[idx]:
      ans = max(ans, lim_a - l + idx + 2)
    else:
      break
    idx += 1
ans = max(ans, lim_b)
print(ans)

Solution (Python):
N,M,K = list(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 j in range(M):
    b[j+1] = b[j] + B[j]

n_b = M
best = 0

for n_a in range(N+1):
    if K < a[n_a]:
        break

    while  K < a[n_a] + b[n_b]:
        n_b -= 1

    best = max(n_a + n_b,best)

print(int(best))

Solution (Python):
import sys
import bisect
import itertools
import collections
import fractions
import heapq
import math
from operator import mul
from functools import reduce
from functools import lru_cache


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    N, M, K = map(int, readline().split())
    A = list(map(int, readline().split()))
    B = list(map(int, readline().split()))

    def is_ok(bcount):
        return Asum[acount] + Bsum[bcount] <= K


    def meguru_bisect(ng, ok):
        '''
        初期値のng,okを受け取り,is_okを満たす最小(最大)のokを返す
        まずis_okを定義すべし
        ng ok は  とり得る最小の値-1 とり得る最大の値+1
        最大最小が逆の場合はよしなにひっくり返す
        '''
        while (abs(ok - ng) > 1):
            mid = (ok + ng) // 2
            if is_ok(mid):
                ok = mid
            else:
                ng = mid
        return ok

    nowtime = 0

    Asum = [0]
    Bsum = [0]
    tmp = 0
    for a in A:
        tmp += a
        Asum.append(tmp)
    tmp = 0
    for b in B:
        tmp += b
        Bsum.append(tmp)

    maxbooks = 0
    before = M
    ans = 0
    for acount in range(N+1):
        maxB = meguru_bisect(M+1, -1)
        if maxB < 0:
            continue
        ans = max(ans, acount+maxB)

    print(ans)



if __name__ == '__main__':
    solve()

Solution (Python):
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])

j = m

for i in range(n + 1):
    if a[i] > k:
        break
    while a[i] + b[j] > k:
        j -= 1
    else:
        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):
N,M,K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
C=[0]
for x in range(N):
 C.append(C[-1]+A[x])
D=[0]
for x in range(M):
 D.append(D[-1]+B[x])
count=0
y=M
for x in range(N+1):
 if C[x]>K:
    break
 while D[y]>K-C[x] and y>0:
   y-=1
 count=max(count,x+y)
print(count)

Solution (Python):



from fractions import gcd
from itertools import combinations,permutations,accumulate 

from collections import deque,defaultdict,Counter
import decimal
import re











import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
n,m,k = readInts()
A = [0] + list(accumulate(readInts()))
B = list(accumulate(readInts()))
ans = 0
import bisect

for i in range(len(A)):
    if A[i] > k:
        continue
    x = bisect.bisect_left(B,k - A[i] + 1)
    
    ans = max(ans,x+i)
print(ans)

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

time_A = [0]*(N+1)
time_B = [0]*(M+1)

for i in range(N):
    time_A[i+1] = time_A[i] + A[i]
for j in range(M):
    time_B[j+1] = time_B[j] + B[j]

ans = 0
l = M

for k in range(N+1):
    if time_A[k] > K:
        break
    while time_A[k] + time_B[l] > K:
        l -= 1
    ans = max(ans,k+l)

print(ans)

Solution (Python):


import numpy as np


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

    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)


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 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 b[j]>K-a[i]:
        j-=1
    ans=max(ans,i+j)
print(ans)

Solution (Python):
from sys import stdin
input = lambda: stdin.readline().rstrip("\r\n")
from collections import defaultdict as vector, deque as que
read = lambda: list(map(int,input().split()))
from bisect import bisect
from heapq import heappush as hpush,heappop as hpop

n,m,k=read()
A=read()
B=read()
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
for i in range(0,n+1):
	curr=a[i]
	if curr>k:
		break
	j=bisect(b,k-curr,0,m+1)
	if j>m or b[j]>k-curr:
		j-=1
	if j==-1:
		ans=max(0,ans)
	else:
		ans=max(ans,i+j)
print(ans)

Solution (Python):
from sys import stdin
input = lambda: stdin.readline().rstrip("\r\n")
from collections import defaultdict as vector, deque as que
read = lambda: list(map(int,input().split()))
from bisect import bisect
from heapq import heappush as hpush,heappop as hpop

n,m,k=read()
A=read()
B=read()
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(0,n+1):
	curr=a[i]
	if curr>k:
		break
	while(b[j]>k-curr):
		j-=1
	ans=max(ans,i+j)
print(ans)

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

N, M, K = [int(n) for n in input().split()]
A = [0]+[x for x in accumulate([int(n) for n in input().split()])]
B = [0]+[x for x in accumulate([int(n) for n in input().split()])]


ans = 0
a = bisect_right(A, K)

for i in range(a):
  b = bisect_right(B, K-A[i])
  ans = max(ans, i+b)

print(ans-1)

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
sys.setrecursionlimit(300000)


def solve(N: int, M: int, K: int, A: "List[int]", B: "List[int]"):
    a, b = [0], [0]
    for v in A:
        a.append(a[-1] + v)
    for v in B:
        b.append(b[-1] + v)

    j = M
    ret = 0
    
    
    for i, v in enumerate(a):
        while j > 0 and v + b[j] > K:
            j -= 1
        if v + b[j] <= K:
            
            ret = max(ret, i + j)
    print(ret)
    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())

array=list(map(int,input().split()))
A=[0]
pre=0
for i in array:
    pre+=i
    A.append(pre)
    
array=list(map(int,input().split()))
B=[0]
pre=0
for i in array:
    pre+=i
    B.append(pre)

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

from itertools import accumulate
import bisect
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 in range(n + 1):
    c = bisect.bisect_right(b, k - a[i]) - 1
    
    if c != -1:
        ans.append(c + i)
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, 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 = [0]*(N+1)
b = [0]*(M+1)
for n in range(N):
    a[n+1] = a[n] + A[n]
for n in range(M):
    b[n+1] = b[n] + B[n]
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]
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, N, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

ruisekiA = [0]
ruisekiB = [0]
tmpA=0
tmpB=0
for i in range(len(A)):
    tmpA += A[i]
    ruisekiA.append(tmpA)
for i in range(len(B)):
    tmpB += B[i]
    ruisekiB.append(tmpB)
ans = 0
for i in range(len(ruisekiA)):
    
    l = 0
    r = len(ruisekiB)
    if ruisekiA[i] > K:
        continue
    while r - l > 1:
        mid = (r + l)//2
        tmp = ruisekiA[i]+ruisekiB[mid]
        if tmp <= K:
            l = mid
        else:
            r = mid

    ans = max(ans, i+l)
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 = [0] + list(map(int, input().split()))
b = [0] + list(map(int, input().split()))

for i in range(1, N+1):
    a[i] = a[i-1] + a[i]
for i in range(1, M+1):
    b[i] = b[i-1] + b[i]

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

Solution (Python):

import sys
import math
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import itertools
import random
from decimal import *

input = sys.stdin.readline

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
def inputStr(): return input()[:-1]

inf = float('inf')
mod = 1000000007




def main():
	N,M,K = inputMap()
	A = inputList()
	B = inputList()

	sumA = []
	for i,val in enumerate(A):
		if i == 0:
			sumA.append(val)
		else:
			sumA.append(val + sumA[i-1])

	sumB = []
	for i,val in enumerate(B):
		if i == 0:
			sumB.append(val)
		else:
			sumB.append(val + sumB[i-1])

	ans = 0
	for i,val in enumerate(sumA):
		if val > K:
			break
		amari = K - val
		indexs = bisect_right(sumB, amari)
		tmp = (i+1) + indexs
		
		if ans < tmp:
			ans = tmp

	for i,val in enumerate(sumB):
		if val > K:
			break
		amari = K - val
		indexs = bisect_right(sumA, amari)
		tmp = (i+1) + indexs
		
		if ans < tmp:
			ans = tmp

	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 i in range(m):
    b.append(b[i]+B[i])

ptr = m
ans = 0

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

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

a_cs, b_cs = [0], [0]
for i in range(n):
  a_cs.append(a_cs[i] + a[i])
for i in range(m):
  b_cs.append(b_cs[i] + b[i])

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

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

i = bisect_left(SA, K)
SA = SA[: i+1]
SA.append(2*10**9)
i = bisect_left(SB, K)
SB = SB[: i+1]
if SB[-1] > K: SB.pop()

i, ii = 0, 0
j = len(SB) - 1
ans = [j]
while j >= 0:
    diff = K - SB[j]
    jdg = False
    for ii in range(i, len(SA)):
        if SA[ii] > diff:
            jdg = True
            break
    i = ii
    ans.append(i + j)
    j -= 1
print(max(ans))

Solution (Python):
from sys import stdin,stdout
import bisect as bs
n,m,s=map(int, stdin.readline().split())
dn=list(map(int, stdin.readline().split()))
dm=list(map(int, stdin.readline().split()))
pre=[dm[0]];s1=0
for v in dm[1:]:
    pre+=[pre[-1]+v]
m=bs.bisect_right(pre,s)
for i in range(n):
    s1+=dn[i]
    if s-s1>=0:m=max(m,i+1+bs.bisect_right(pre,s-s1))
print(m)

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


def Cum(X):
    temp = 0
    Cum = [0]*len(X)
    for i in range(len(X)):
        temp += X[i]
        Cum[i] += temp
    return Cum

a = Cum(A)
a.insert(0,0)
b = Cum(B)
b.insert(0,0)

na = 0
for i in range(N+1):
    if i == N and a[i]<=K:
        na = N
    elif a[i]<=K and a[i+1]>K:
        na = i

ans = na

nb = 0
for i in range(na,-1,-1):
    temp = K-a[i]
    for j in range(nb,M+1):
        if j == M and b[j] <= temp:
            nb = M
            ans = max(ans,i+nb)
        elif b[j]<=temp and b[j+1]>temp:
            nb = j
            ans = max(ans,i+nb)
            break
        
print(ans)

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


def Cum(X):
    temp = 0
    Cum = [0]*len(X)
    for i in range(len(X)):
        temp += X[i]
        Cum[i] += temp
    return Cum

a = Cum(A)
a.insert(0,0)
b = Cum(B)
b.insert(0,0)

na = 0
for i in range(N+1):
    if i == N and a[i]<=K:
        na = N
    elif a[i]<=K and a[i+1]>K:
        na = i

ans = na

nb = 0
for i in range(na,-1,-1):
    temp = K-a[i]
    for j in range(nb,M+1):
        if j == M and b[j] <= temp:
            nb = M
            ans = max(ans,i+nb)
        elif b[j]<=temp and b[j+1]>temp:
            nb = j
            ans = max(ans,i+nb)
            break
        
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]
b=[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]
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]
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 = j-1
    Ans = max(Ans,i+j)

print(Ans)

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


i = j = 0
t = 0
while i < N and t + A[i] <= K:
	t += A[i]
	i += 1

best = i

while True:
	while j < M and t + B[j] <= K:
		t += B[j]
		j += 1

	best = max(best, i + j)

	if not i: break

	i -= 1
	t -= A[i]

print(best)

Solution (Python):
import itertools
import functools
import math
from collections import Counter
from itertools import combinations
import re



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

    A_len = len(A)
    B_len = len(B)

    Asum = [0] * (A_len + 1)
    Bsum = [0] * (B_len + 1)

    maxA = len(A) + 1
    maxB = len(B) + 1

    add = 0
    for i in range(1,A_len+1):
        Asum[i] = A[i-1] + Asum[i-1]

    add = 0
    for i in range(1,B_len+1):
        Bsum[i] = B[i-1] + Bsum[i-1]

    ans = 0
    prev = 0
    for i in reversed(range(maxA)):
        for j in range(prev,maxB):
            if Asum[i] + Bsum[j] <= K:
                prev = j
                if ans < i + j:
                    ans = i + j
                if j == (maxB - 1):
                    print(ans)
                    exit()
            else:
                break

    print(ans)




main_sum()

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

A = cumsum([0] + A)
B = cumsum([0] + B)
n_read = 0
l = len(B) - 1
for i, a in enumerate(A):
    if a > k:
        break
    for j, b in enumerate(B[l::-1]):
        if a + b <= k:
            l -= j
            n_read = max(n_read, i + l)
            break
print(n_read)

Solution (Python):
from itertools import chain


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

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

    answer = 0

    sum_b = sum(B)
    idx_b = M
    remain = K

    for i, a in enumerate(chain([0], A)):
        remain -= a
        if remain < 0:
            break

        while sum_b > remain and idx_b >= 0:
            idx_b -= 1
            b = B[idx_b]
            sum_b -= b

        i += idx_b
        answer = i if answer < i else answer

    return answer


def main():
    print(solve())


if __name__ == "__main__":
    main()

Solution (Python):
from itertools import chain


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

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

    answer = 0

    sum_b = sum(B)
    idx_b = M
    remain = K

    for i, a in enumerate(chain([0], A)):
        remain -= a
        if remain < 0:
            break

        while sum_b > remain and idx_b >= 0:
            idx_b -= 1
            b = B[idx_b]
            sum_b -= b
        
        i += idx_b
        answer = i if answer < i else answer

    return answer


def main():
    print(solve())


if __name__ == "__main__":
    main()

Solution (Python):
def main():
    from itertools import chain
    N, M, K = map(int, input().split())

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

    answer = 0

    sum_b = sum(B)
    idx_b = M
    remain = K

    for i, a in enumerate(chain([0], A)):
        remain -= a
        if remain < 0:
            break

        while sum_b > remain and idx_b >= 0:
            idx_b -= 1
            b = B[idx_b]
            sum_b -= b

        i += idx_b
        answer = i if answer < i else answer

    print(answer)


if __name__ == "__main__":
    main()

Solution (Python):
from itertools import chain


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

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

    answer = 0

    sum_b = sum(B)
    idx_b = M
    remain = K

    for i, a in enumerate(chain([0], A)):
        remain -= a
        if remain < 0:
            break

        while sum_b > remain and idx_b >= 0:
            idx_b -= 1
            b = B[idx_b]
            sum_b -= b

        i += idx_b
        answer = i if answer < i else answer

    print(answer)


if __name__ == "__main__":
    main()

Solution (Python):
from itertools import chain


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

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

    answer = 0

    sum_b = sum(B)
    idx_b = M
    remain = K

    for i, a in enumerate(chain([0], A)):
        remain -= a
        if remain < 0:
            break

        while sum_b > remain and idx_b >= 0:
            idx_b -= 1
            b = B[idx_b]
            sum_b -= b
        
        i += idx_b
        answer = i if answer < i else answer

    return answer


def main():
    print(solve())


if __name__ == "__main__":
    main()

Solution (Python):
from itertools import chain


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

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

    answer = 0

    sum_b = sum(B)
    idx_b = M
    remain = K

    for i, a in enumerate(chain([0], A)):
        remain -= a
        if remain < 0:
            break

        while sum_b > remain:
            if idx_b < 0:
                break
            idx_b -= 1
            b = B[idx_b]
            sum_b -= b

        answer = max(answer, i + idx_b)

    return answer


def main():
    print(solve())


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





def cuml(li):
    rlis = [0]

    for i in range(len(li)):
        rlis.append(rlis[-1] + li[i])

    return rlis


A_cuml = cuml(A)
B_cuml = cuml(B)

res = 0

for a in range(len(A_cuml)):
    rest = K - A_cuml[a]
    if rest < 0:
        break
    
    
    

    
    b = bisect.bisect_right(B_cuml, K - A_cuml[a])

    res = max(res, a + b - 1)

print(res)

Solution (Python):
from itertools import chain


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

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

    answer = 0

    sum_b = sum(B)
    idx_b = M
    remain = K

    for i, a in enumerate(chain([0], A)):
        remain -= a
        if remain < 0:
            break

        while sum_b > remain:
            if idx_b < 0:
                break
            idx_b -= 1
            b = B[idx_b]
            sum_b -= b

        answer = max(answer, i + idx_b)

    return answer


def main():
    print(solve())


if __name__ == "__main__":
    main()

Solution (Python):
from itertools import accumulate
n,m,k = map(int, input().split())
a = map(int, input().split())
b = map(int, input().split())
varl = [0] + list(accumulate(a))
varl2 = [0] + list(accumulate(b))
ans, i = 0, n
for iterater in range(m+1):
    while varl[i]+varl2[iterater] > k and i>0:
        i -= 1
    if i==0 and varl2[iterater] > k:
        break
    ans = max(ans, i+iterater)
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(A)
i = N
ans = 0

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

b = [0]
for i in range(m):
    b.append(b[i]+b_[i])

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

print(ans)

Solution (Python):
from bisect import bisect
n, m, k = map(int, input().split())
list_A = list(map(int, input().split()))
list_B = list(map(int, input().split()))

list_AS = []
list_BS = []
a = 0
b = 0
list_AS.append(0)
for i in list_A:
    a += i
    list_AS.append(a)
list_BS.append(0)
for i in list_B:
    b += i
    list_BS.append(b)
ans = 0
for i in range(n+1):
    j = bisect(list_BS, k-list_AS[i])
    if list_AS[i] + list_BS[j-1] <= k:
        ans = max(ans, i+j-1)

print(ans)

Solution (Python):
n,m,k=map(int,input().split())
al=[int(i) for i in input().split()]
bl=[int(i) for i in input().split()]
aa,bb=[0],[0]
for i in range(n):
    aa.append(aa[i]+al[i])
for i in range(m):
    bb.append(bb[i]+bl[i])
ans=0
j=m
for i in range(n+1):
    if aa[i]>k:
        break
    while bb[j]>k-aa[i]:
        j-=1
    ans=max(ans,i+j)
print(ans)

Solution (Python):
from bisect import bisect_right


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

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

    sum_as = [0] * (N+1)
    sum_bs = [0] * (M+1)

    for i, a in enumerate(A):
        sum_as[i+1] = sum_as[i] + a

    for i, b in enumerate(B):
        sum_bs[i+1] = sum_bs[i] + b
    sum_bs = sum_bs[1:]

    answer = 0
    for i, sa in enumerate(sum_as):
        remain = K - sa
        if remain < 0:
            continue
        
        idx = bisect_right(sum_bs, remain)
        answer = max(answer, i + idx)

    return answer


def main():
    print(solve())


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_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(10**10)

res = 0
for i in range(len(A_sum)):
    low = 0
    high = len(B_sum)
    while high - low > 1:
        mid = (low + high) // 2
        if A_sum[i] + B_sum[mid] <= K:
            low = mid
        else:
            high = mid
    if A_sum[i] + B_sum[low] <= K:
        res = max(res, i + low)

print(res)

Solution (Python):
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)
INF = float('inf')

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

A_sum = [0]
B_sum = [0]
for i in range(N):
    A_sum.append(A_sum[-1] + A[i])
for i in range(M):
    B_sum.append(B_sum[-1] + B[i])

ans = 0
j_max = M
for i in range(N+1):
    for j in range(j_max, -1, -1):
        time = A_sum[i] + B_sum[j]
        if time > K:
            continue
        j_max = j
        ans = max(i + j, ans)
        break
print(ans)

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

N, M, K = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A_ = list(accumulate(A))
B_ = list(accumulate(B))
A_all = bisect(A_, K)
B_all = bisect(B_, K)
if B_all > A_all:
    A_, B_ = B_, A_
    A_all, B_all = B_all, A_all
ans = 0
for i in range(A_all):
    ans = max(ans, i+1 + bisect(B_, K - A_[i]))
print(ans)

Solution (Python):
N, M, K = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
arui = [0] * (N + 1)
brui = [0] * (M + 1)
for i in range(N):
    arui[i + 1] = a[i] + arui[i]
for i in range(M):
    brui[i + 1] = b[i] + brui[i]

ans = 0
bcnt = M
for i in range(N + 1):
    if arui[i] > K:
        break

    while brui[bcnt] + arui[i] > K:
        bcnt -= 1
    ans = max(ans, i + bcnt)

print(ans)