AtCoder Beginner Contest 172 - Tsundoku

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-----
3 4 240
60 90 120
80 150 80 150

-----Sample Output-----
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.

Difficulty: EASY

Starter code:

Solutions:
import sys
from collections import deque, defaultdict, Counter
from itertools import accumulate, product, permutations, combinations
from operator import itemgetter
from bisect import bisect_left, bisect_right
from heapq import heappop, heappush
from math import ceil, floor, sqrt, gcd, inf
from copy import deepcopy
import numpy as np
import scipy as sp
INF = inf
MOD = 1000000007
(n, m, k) = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
tmp = sum(A)
cnt = len(A)
res = 0
A = deque(A)
B = deque(B)
while True:
	if tmp <= k:
		res = max(res, cnt)
		if len(B) <= 0:
			break
		b = B.popleft()
		tmp += b
		cnt += 1
	else:
		if len(A) <= 0:
			break
		a = A.pop()
		tmp -= a
		cnt -= 1
print(res)


(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])
ak = [i - K for i in a]
b = [0]
for i in range(M):
	b.append(b[i] - B[i])
import bisect
ans = 0
for i in range(M + 1):
	k = bisect.bisect_right(ak, b[i])
	if k > 0:
		ans = max(k + i - 1, ans)
print(ans)


import itertools
import bisect
(n, m, k) = map(int, input().split())
a = list(map(int, input().split()))
b = [0] + list(map(int, input().split()))
a = list(itertools.accumulate(a))
b = list(itertools.accumulate(b))
ans = 0
for i in range(m + 1):
	x = k - b[i]
	y = bisect.bisect_right(a, x)
	z = i + y
	if x < 0:
		z = 0
	ans = max(ans, z)
print(ans)


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


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


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


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


import bisect
(N, M, K) = map(int, input().split())
A = [0] + [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
for i in range(1, N + 1):
	A[i] = A[i - 1] + A[i]
for i in range(1, M):
	B[i] = B[i - 1] + B[i]
ans = 0
for i in range(N + 1):
	if A[i] <= K:
		ans = max(ans, i + bisect.bisect_right(B, K - A[i]))
print(ans)


(n, m, k) = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
asum = [0]
bsum = [0]
for i in range(n):
	asum.append(asum[i] + A[i])
for j in range(m):
	bsum.append(bsum[j] + B[j])
ans = 0
ko = m
for i in range(n + 1):
	if asum[i] > k:
		break
	while asum[i] + bsum[ko] > k:
		ko -= 1
	ans = max(ans, i + ko)
print(ans)


(n, m, k) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
(x, i, j) = (0, 0, 0)
a.append(10 ** 9 + 1)
b.append(10 ** 9 + 1)
while x + a[i] <= k:
	x += a[i]
	i += 1
maxs = i
for c in range(n + m):
	if x + b[j] <= k:
		x += b[j]
		j += 1
		maxs = max(maxs, i + j)
	elif i > 0:
		x = max(0, x - a[i - 1])
		i -= 1
print(maxs)


from bisect import bisect
(N, M, K) = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
SA = [0] * (N + 1)
SB = [0] * (M + 1)
for i in range(N):
	SA[i + 1] = SA[i] + A[i]
for i in range(M):
	SB[i + 1] = SB[i] + B[i]
result = 0
for x in range(N + 1):
	if SA[x] > K:
		break
	y = bisect(SB, K - SA[x]) - 1
	result = max(result, x + y)
print(result)


(n, m, k) = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
from itertools import accumulate
import bisect
cumsum_a = list(accumulate(a))
cumsum_b = list(accumulate(b))
ans = 0
for i in range(1, n + 1):
	if cumsum_a[i - 1] > k:
		break
	cnt_b = bisect.bisect(cumsum_b, k - cumsum_a[i - 1])
	ans = max(ans, i + cnt_b)
for i in range(1, m + 1):
	if cumsum_b[i - 1] > k:
		break
	cnt_a = bisect.bisect(cumsum_a, k - cumsum_b[i - 1])
	ans = max(ans, i + cnt_a)
print(ans)


(n, m, k) = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
sum_as = [0]
sum_bs = [0]
for i in range(n):
	sum_as.append(sum_as[i] + a[i])
for i in range(m):
	sum_bs.append(sum_bs[i] + b[i])
ans = 0
i = 0
j = m
while i <= n and sum_as[i] <= k:
	while sum_bs[j] > k - sum_as[i]:
		j -= 1
	ans = max(ans, i + j)
	i += 1
print(ans)


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


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


read = lambda : list(map(int, input().split()))
(n, m, k) = read()
a = read()
b = read()
(at, bt) = ([0], [0])
for i in range(n):
	at.append(at[i] + a[i])
for i in range(m):
	bt.append(bt[i] + b[i])
(ans, j) = (0, m)
for i in range(n + 1):
	if at[i] > k:
		break
	while bt[j] > k - at[i]:
		j -= 1
	ans = max(ans, i + j)
print(ans)


(n, m, k) = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
t = sum(B)
res = 0
j = m
for i in range(n + 1):
	while j > 0 and t > k:
		j -= 1
		t -= B[j]
	if t > k:
		break
	res = max(res, i + j)
	if i == n:
		break
	t += A[i]
print(res)


from bisect import *
(n, m, k) = map(int, input().split())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
c = [0]
for i in a:
	c += [c[-1] + i]
d = [0]
for i in b:
	d += [d[-1] + i]
ans = j = bisect(d, k) - 1
for i in range(n + 1):
	while c[i] + d[j] > k and c[i] < k:
		j -= 1
	if c[i] + d[j] <= k:
		ans = max(ans, i + j)
print(ans)


from itertools import accumulate
from bisect import bisect_left
ri = lambda S: [int(v) for v in S.split()]

def rii():
	return ri(input())
(N, M, K) = rii()
A = rii()
B = rii()
i = j = 0
pA = [0] + list(accumulate(A))
pB = [0] + list(accumulate(B))
ans = 0
for (i, a) in enumerate(pA):
	br = 0
	if K >= a:
		r = K - a
		br = i
	if r:
		j = bisect_left(pB, r)
		if j != len(pB):
			if pB[j] > r:
				j -= 1
			br += j
	ans = max(ans, br)
print(ans)


def main():
	(n, m, k) = list(map(int, input().split()))
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	s = sum(B)
	ans = 0
	(i, j) = (0, m - 1)
	while i < n:
		if s <= k:
			ans = max(ans, i + j + 1)
			s += A[i]
			i += 1
		else:
			if j < 0:
				break
			s -= B[j]
			j -= 1
	if s <= k:
		ans = max(ans, n + j + 1)
	print(ans)
	return

def __starting_point():
	main()
__starting_point()


import bisect
from itertools import accumulate
(n, m, k, *AB) = map(int, open(0).read().split())
(A, B) = ([0] + AB[:n], AB[n:])
A_ = list(accumulate(A))
B_ = list(accumulate(B))
ans = 0
for i in range(n + 1):
	if k < A_[i]:
		break
	t = i + bisect.bisect(B_, k - A_[i])
	ans = max(ans, t)
print(ans)


import bisect
import itertools
(n, m, k) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ca = [0] + list(itertools.accumulate(a))
cb = [0] + list(itertools.accumulate(b))
ans = 0
for i in range(n + 1):
	bk = ca[i]
	if bk <= k:
		j = bisect.bisect(cb, k - bk)
		ans = max(ans, i + j - 1)
	else:
		break
print(ans)


import bisect
(n, m, k) = map(int, input().split())
s = list(map(int, input().split()))
t = list(map(int, input().split()))
ds = [0]
for i in range(n):
	ds.append(ds[-1] + s[i])
ans = 0
p = 0
for i in range(m + 1):
	if i > 0:
		p += t[i - 1]
	if p > k:
		break
	si = bisect.bisect_left(ds, k - p + 1)
	ans = max(ans, si + i - 1)
print(ans)


(N, M, K) = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
curr = 0
A_index = 0
B_index = 0
while curr + A[A_index] <= K:
	curr += A[A_index]
	A_index += 1
	if A_index == N:
		break
while curr + B[B_index] <= K:
	curr += B[B_index]
	B_index += 1
	if B_index == M:
		break
ans = A_index + B_index
while A_index:
	A_index -= 1
	curr -= A[A_index]
	if B_index == M:
		break
	while curr + B[B_index] <= K:
		curr += B[B_index]
		B_index += 1
		if B_index == M:
			break
	ans = max(ans, A_index + B_index)
print(ans)


(N, M, K) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
A = [0]
B = [0]
ans = 0
j = M
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
	while A[i] + B[j] > K:
		j -= 1
	ans = max(ans, i + j)
print(ans)


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


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


from bisect import bisect_right
(n, m, k) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
cnt = 0
a_cum = [0] * (n + 1)
b_cum = [0] * (m + 1)
for i in range(n):
	a_cum[i + 1] = a_cum[i] + a[i]
for i in range(m):
	b_cum[i + 1] = b_cum[i] + b[i]
for i in range(n + 1):
	a_cnt = i
	rest = k - a_cum[i]
	if rest < 0:
		break
	b_cnt = bisect_right(b_cum, rest) - 1
	cnt = max(cnt, a_cnt + b_cnt)
print(cnt)


import bisect
(n, m, k) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
(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])
for j in range(n + 1):
	K = k - A[j]
	if K < 0:
		break
	else:
		ans = max(ans, bisect.bisect_right(B, K) + j - 1)
print(ans)


def getmax(x, y):
	if x > y:
		return x
	else:
		return y
(N, M, K) = list(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)


from sys import stdin
nii = lambda : map(int, stdin.readline().split())
lnii = lambda : list(map(int, stdin.readline().split()))
from bisect import bisect_right
from itertools import accumulate
(n, m, k) = nii()
a = lnii()
b = lnii()
ar = [0] + list(accumulate(a))
br = list(accumulate(b))
ans = 0
for i in range(n + 1):
	time = ar[i]
	if time > k:
		break
	t_ans = i
	inx = bisect_right(br, k - time)
	t_ans += inx
	ans = max(ans, t_ans)
print(ans)


import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
import numpy as np

def main():
	(n, m, k) = map(int, input().split())
	a = np.array(readline().split(), np.int64)
	b = np.array(readline().split(), np.int64)
	aa = a.cumsum()
	ba = b.cumsum()
	r = np.searchsorted(ba, k, side='right')
	for (i1, aae) in enumerate(aa):
		if k < aae:
			break
		r = max(r, np.searchsorted(ba, k - aae, side='right') + i1 + 1)
	print(r)

def __starting_point():
	main()
__starting_point()


(a, b, money) = list(map(int, input().split()))
A = [0] + list(map(int, input().split()))
B = [0] + list(map(int, input().split()))
for i in range(1, a + 1):
	A[i] += A[i - 1]
for j in range(1, b + 1):
	B[j] += B[j - 1]
ans = 0
b_cnt = b
for a_cnt in range(a + 1):
	if A[a_cnt] > money:
		continue
	while A[a_cnt] + B[b_cnt] > money:
		b_cnt -= 1
	ans = max(ans, a_cnt + b_cnt)
print(ans)


(N, M, K) = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))
b = 0
tb = 0
for i in range(M):
	if tb + B[i] > K:
		break
	tb += B[i]
	b = i + 1
ans = [0, b]
m = b
a = 0
ta = 0
for i in range(N):
	if ta + A[i] > K:
		break
	a = i + 1
	ta += A[i]
	while True:
		if ta + tb > K:
			if b == 0:
				break
			b -= 1
			tb -= B[b]
		else:
			if a + b > m:
				m = a + b
				ans = [a, b]
			break
print(ans[0] + ans[1])


((n, m, k), *A) = [list(map(int, i.split())) for i in open(0)]
a = A[0]
b = A[1]
aa = [0]
bb = [0]
for i in range(n):
	aa.append(aa[i] + a[i])
for i in range(m):
	bb.append(bb[i] + b[i])
ans = 0
for i in range(n + 1):
	if aa[i] > k:
		break
	while bb[m] > k - aa[i]:
		m -= 1
	ans = max(ans, m + i)
print(ans)


(N, M, K) = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))
cumsum_a = [0] * (N + 1)
cumsum_b = [0] * (M + 1)
for i in range(1, N + 1):
	cumsum_a[i] = cumsum_a[i - 1] + A[i - 1]
for i in range(1, M + 1):
	cumsum_b[i] = cumsum_b[i - 1] + B[i - 1]
ans = 0
bi = M
for i in range(N + 1):
	for j in range(bi, -1, -1):
		t = cumsum_a[i] + cumsum_b[j]
		if t <= K:
			ans = max(ans, i + j)
			bi = j
			break
print(ans)


from collections import deque
from itertools import accumulate
(n, m, k) = map(int, input().split())
a_list = list(accumulate(list(map(int, input().split()))))
a_list = [0] + a_list
b_list = list(accumulate(list(map(int, input().split()))))
b_list = [0] + b_list + [float('inf')]
cnt_list = []
for i in range(len(a_list)):
	t = k
	cnt = 0
	a = a_list[i]
	if t < a:
		continue
	t -= a
	cnt += i
	ok = 0
	no = m + 2
	while no != ok + 1:
		center = (ok + no) // 2
		if t >= b_list[center]:
			ok = center
		if t < b_list[center]:
			no = center
	cnt += ok
	cnt_list.append(cnt)
ans = max(cnt_list)
print(ans)


import numpy as np
import bisect
(N, M, K) = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
if A[0] > K and B[0] > K:
	print(0)
	return
Asum = [0]
for i in range(N):
	if Asum[-1] + A[i] <= K:
		Asum.append(Asum[-1] + A[i])
	else:
		break
Bsum = [0]
for j in range(M):
	if Bsum[-1] + B[j] <= K:
		Bsum.append(Bsum[-1] + B[j])
	else:
		break
C = []
for i in range(len(Asum)):
	C.append(bisect.bisect(Bsum, K - Asum[i]) + i - 1)
print(max(C))


(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(N):
	A[i + 1] += A[i]
for i in range(M):
	B[i + 1] += B[i]
b = M
r = 0
for (i, a) in enumerate(A):
	if a > K:
		break
	while a + B[b] > K and b >= 0:
		b -= 1
	r = max(r, i + b)
print(r)


from itertools import accumulate

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

def LI():
	return list(map(int, input().split()))
(N, M, K) = MI()
A = [0] + LI()
A = list(accumulate(A))
B = [0] + LI()
B = list(accumulate(B))
j = M
ans = 0
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)


from bisect import *
from itertools import *
(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 n in range(1 + N):
	C = bisect_right(B, K - A[n]) - 1
	if C != -1:
		ans += [C + n]
print(max(ans))


from itertools import accumulate
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, initial=0))
B = list(accumulate(B, initial=0))
ans = 0
for (i, a) in enumerate(A):
	remain = k - a
	if remain < 0:
		break
	tmp = i
	tmp += bisect.bisect_right(B, remain) - 1
	ans = max(ans, tmp)
print(ans)


(N, M, K) = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
SA = [0]
SB = [0]
for i in range(len(A)):
	SA.append(SA[i] + A[i])
for j in range(len(B)):
	SB.append(SB[j] + B[j])
l = M
ans = 0
for k in range(N + 1):
	if SA[k] > K:
		break
	while SA[k] + SB[l] > K:
		l -= 1
	ans = max(ans, k + l)
print(ans)


import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
(n, m, k) = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
aa = [0] * (len(a) + 1)
bb = [0] * (len(b) + 1)
for ii in range(len(a)):
	aa[ii + 1] = aa[ii] + a[ii]
for ii in range(len(b)):
	bb[ii + 1] = bb[ii] + b[ii]
ii = 0
jmax = 0
r = 0
for jj in range(len(aa)):
	if aa[jj] > k:
		break
	else:
		r = ii + jj
		jmax = jj
for ii in range(len(bb)):
	for jj in range(jmax, -1, -1):
		if bb[ii] > k - aa[jj]:
			pass
		else:
			jmax = jj
			r = max(r, ii + jj)
			break
print(r)


(N, M, K) = map(int, input().split())
lsA = list(map(int, input().split()))
lsB = list(map(int, input().split()))
lsAN = [0]
sumA = 0
for i in range(N):
	sumA += lsA[i]
	lsAN.append(sumA)
lsBN = [0]
sumB = 0
for i in range(M):
	sumB += lsB[i]
	lsBN.append(sumB)
j = M
ans = 0
for i in range(N + 1):
	if lsAN[i] > K:
		break
	while lsBN[j] > K - lsAN[i]:
		j -= 1
	ans = max(ans, i + j)
print(ans)


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


(n, m, k) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
sa = [0]
sb = [0]
for i in range(n):
	sa.append(sa[i] + a[i])
for i in range(m):
	sb.append(sb[i] + b[i])
maxn = 0
j = m
for i in range(n + 1):
	if sa[i] <= k:
		while sa[i] + sb[j] > k:
			j -= 1
		maxn = max(maxn, i + j)
print(maxn)


from bisect import *
from itertools import *
(n, _, k, *x) = map(int, open(0).read().split())
c = accumulate
(*b,) = c(x[n:])
print(max((i + bisect(b, k - v) for (i, v) in enumerate(c([0] + x[:n])) if v <= k)))


from itertools import accumulate
import bisect
(n, m, k) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a_c = [0] + list(accumulate(a))
b_c = [0] + list(accumulate(b))
result = []
for i in range(n + 1):
	if k >= a_c[i]:
		k_temp = k - a_c[i]
		b_idx = bisect.bisect_left(b_c, k_temp + 1)
		result.append(i + b_idx - 1)
	else:
		pass
result.append(0)
print(max(result))


from itertools import accumulate
from bisect import bisect
(n, m, k) = map(int, input().split())
(*a,) = accumulate(map(int, input().split()))
(*b,) = accumulate(map(int, input().split()))
a = (0,) + tuple((i for i in a if i <= k))
b = tuple((i for i in b if i <= k))
print(max((i + bisect(b, k - a[i]) for i in range(len(a)))))


def main(N, M, K, A, B):
	Ai = [0]
	for i in range(N):
		Ai.append(Ai[i] + A[i])
	Bj = [0]
	for j in range(M):
		Bj.append(Bj[j] + B[j])
	ans = 0
	best_i = 0
	for j in range(M + 1):
		tmp = 0
		for i in range(best_i, N + 1):
			if Ai[N - i] + Bj[j] <= K:
				tmp = N - i + j
				best_i = i
				break
		if tmp > ans:
			ans = tmp
	return ans

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


from bisect import bisect_right
from itertools import accumulate
(N, M, K) = map(int, input().split())
A = list(accumulate([0] + [int(x) for x in input().split()]))
B = list(accumulate([0] + [int(x) for x in input().split()]))
m = 0
for i in range(0, N + 1):
	if K < A[i]:
		break
	m = max(m, i + bisect_right(B, K - A[i]) - 1)
print(m)


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


import bisect
from itertools import *
(n, m, k, *x) = [int(v) for l in open(0) for v in l.split()]
a = accumulate([0] + x[:n])
b = [*accumulate(x[n:])]
print(max((i + bisect.bisect(b, k - v) for (i, v) in enumerate(a) if v <= k)))


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


(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 = [0]
idx = m
for i in range(n + 1):
	x = k - a[i]
	if x < 0:
		break
	while b[idx] > x:
		idx -= 1
	ANS.append(i + idx)
print(max(ANS))


import bisect
(n, m, k) = map(int, input().split())
aaa = list(map(int, input().split()))
bbb = list(map(int, input().split()))
sum_a = [0] * (n + 1)
sum_b = [0] * (m + 1)
for i in range(n):
	sum_a[i + 1] = sum_a[i] + aaa[i]
for j in range(m):
	sum_b[j + 1] = sum_b[j] + bbb[j]
ans = 0
for i in range(n + 1):
	if sum_a[i] > k:
		break
	tmp = bisect.bisect_right(sum_b, k - sum_a[i])
	ans = max(ans, i + tmp - 1)
print(ans)


import bisect
(N, M, K) = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
for i in range(1, N):
	A[i] += A[i - 1]
index = bisect.bisect(A, K)
for i in range(M):
	K -= B[i]
	if K >= 0:
		a = bisect.bisect(A, K)
		if a + i + 1 >= index:
			index = a + i + 1
print(index)


(n, m, k) = map(int, input().split())
(A, B) = eval('[0]+[*map(int,input().split())],' * 2)
s = t = x = 0
j = m
for i in range(n + 1):
	s += A[i]
	A[i] = s
for i in range(m + 1):
	t += B[i]
	B[i] = t
for i in range(n + 1):
	if A[i] > k:
		break
	while B[j] > k - A[i]:
		j -= 1
	x = max(x, i + j)
print(x)


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

def LI():
	return list(map(int, input().split()))
from itertools import accumulate
from bisect import bisect_right
(n, m, k) = MI()
A = LI()
B = LI()
a = [0] + list(accumulate(A))
b = [0] + list(accumulate(B))
ans = 0
for i in range(n + 1):
	if a[i] > k:
		break
	j = bisect_right(b, k - a[i])
	ans = max(ans, i + j - 1)
print(ans)


from itertools import accumulate as a
(N, M, K) = map(int, input().split())
l = [0]
P = l + list(a(map(int, input().split())))
Q = l + list(a(map(int, input().split())))
i = j = 0
f = lambda : P[i] + Q[j] <= K
while i <= N and f():
	i += 1
while i >= 0:
	i -= 1
	while j <= M and f():
		j += 1
	l += [i + j - 1]
print(max(l))


import bisect
(n, m, k) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
x = [0]
y = [0]
for i in range(n):
	x.append(x[i] + a[i])
for i in range(m):
	y.append(y[i] + b[i])
res = 0
for i in range(n + 1):
	if x[i] > k:
		break
	else:
		t = k - x[i]
		bb = bisect.bisect(y, t)
		res = max(res, i + bb - 1)
print(res)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time, copy, bisect
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
import sys
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
mod = 10 ** 9 + 7
stdin = sys.stdin
ni = lambda : int(ns())
nf = lambda : float(ns())
na = lambda : list(map(int, stdin.readline().split()))
nb = lambda : list(map(float, stdin.readline().split()))
ns = lambda : stdin.readline().rstrip()
(N, M, K) = na()
A = na()
B = na()
now = 0
ct = 0
x = -1
y = 0
ans = 0
for i in range(N):
	if A[i] + now <= K:
		now += A[i]
		ct += 1
		x += 1
	else:
		break
res = K - now
ans = ct
while y < M:
	if res - B[y] >= 0:
		ct += 1
		ans = max(ct, ans)
		res -= B[y]
		y += 1
	elif res - B[y] < 0 and x >= 0:
		res += A[x]
		ct -= 1
		x -= 1
	else:
		break
print(ans)


def getval():
	(n, m, k) = map(int, input().split())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	a = [A[0]]
	b = [B[0]]
	for i in range(0, n - 1):
		a.append(a[i] + A[i + 1])
	for i in range(0, m - 1):
		b.append(b[i] + B[i + 1])
	return (n, m, k, a, b)

def main(n, m, k, a, b):
	idxa = 0
	ans = 0
	t = 0
	while a[idxa] <= k and idxa < n:
		idxa += 1
		if idxa == n:
			break
	ans = idxa
	idxa -= 1
	t = a[idxa]
	for i in range(m):
		t = a[idxa] + b[i]
		if t <= k:
			ans = max(ans, idxa + i + 2)
		else:
			flag = False
			while t > k:
				idxa -= 1
				if idxa < 0:
					a.append(0)
					break
				t = a[idxa] + b[i]
			ans = max(ans, idxa + i + 2)
	print(ans)

def __starting_point():
	(n, m, k, a, b) = getval()
	main(n, m, k, a, b)
__starting_point()


import itertools
import bisect
(n, m, k) = map(int, input().split())
a = [0] + list(map(int, input().split()))
b = list(map(int, input().split()))
aa = list(itertools.accumulate(a))
bb = list(itertools.accumulate(b))
r = 0
for (i, ax) in enumerate(aa):
	bis = bisect.bisect_right(bb, k - ax)
	if bis == 0 and ax > k:
		continue
	r = max(r, i + bis)
print(r)


(n, m, p) = 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]
now = -1
import bisect as bi
for i in range(n + 1):
	if a[i] > p:
		break
	G = p - a[i]
	now = max(bi.bisect(b, G) + i - 1, now)
print(now)


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


(n, m, k) = list(map(int, input().split()))
aArr = list(map(int, input().split()))
bArr = list(map(int, input().split()))
t = sum(bArr)
ans = 0
j = m
for i in range(n + 1):
	while j > 0 and t > k:
		j -= 1
		t -= bArr[j]
	if t > k:
		break
	ans = max(ans, i + j)
	if i == n:
		break
	t += aArr[i]
print(ans)


from bisect import bisect_right
from itertools import accumulate
(N, M, K, *AB) = map(int, open(0).read().split())
(A, B) = (AB[:N], AB[N:])
A = [0] + list(accumulate(A))
B = [0] + list(accumulate(B))
ans = 0
for (i, a) in enumerate(A):
	if K - a < 0:
		break
	ans = max(ans, bisect_right(B, K - a) + i - 1)
print(ans)


import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
import numpy as np

def main():
	(n, m, k) = map(int, input().split())
	a = np.array(readline().split(), np.int64)
	b = np.array(readline().split(), np.int64)
	aa = np.zeros(n + 1, np.int64)
	ba = np.zeros(m + 1, np.int64)
	aa[1:] = a.cumsum()
	ba[1:] = b.cumsum()
	aa = aa[aa <= k]
	ba = ba[ba <= k]
	rs = np.searchsorted(ba, k - aa, side='right') - 1
	rs += np.arange(len(aa))
	r = rs.max()
	print(r)

def __starting_point():
	main()
__starting_point()


import sys

def input():
	return sys.stdin.readline().rstrip()
from itertools import accumulate

def main():
	(n, m, k) = map(int, input().split())
	A = [0] + list(map(int, input().split()))
	B = [0] + list(map(int, input().split()))
	A = list(accumulate(A))
	B = list(accumulate(B))
	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)

def __starting_point():
	main()
__starting_point()


(N, M, K) = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
from itertools import accumulate
AA = [0] + list(accumulate(A))
BB = [0] + list(accumulate(B))
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)


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


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


from itertools import *
from bisect import *
((n, m, k), a, b) = [list(map(int, o.split())) for o in open(0)]
(*a,) = accumulate([0] + a)
(*b,) = accumulate([0] + b)
c = 0
for i in range(n + 1):
	if a[i] > k:
		break
	c = max(c, i + bisect(b, k - a[i]) - 1)
print(c)


from itertools import accumulate
import bisect
(n, m, k) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
A = list(accumulate(a))
B = list(accumulate(b))
ans = 0
for i in range(n):
	if k - A[i] >= 0:
		c = bisect.bisect_right(B, k - A[i])
		ans = max(i + c + 1, ans)
print(max(ans, bisect.bisect_right(B, k)))


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


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


(N, M, K) = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
s = sum(B)
ans = 0
(i, j) = (0, M - 1)
while i < N:
	if s <= K:
		ans = max(ans, i + j + 1)
		s += A[i]
		i += 1
	else:
		if j < 0:
			break
		s -= B[j]
		j -= 1
if s <= K:
	ans = max(ans, N + j + 1)
print(ans)


(n, m, x) = map(int, input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
i = 0
while i < len(a) and x >= a[i]:
	x -= a[i]
	i += 1
f = i
j = 0
for p in b:
	j += 1
	x -= p
	while x < 0 and i > 0:
		i -= 1
		x += a[i]
	if x >= 0:
		f = max(f, j + i)
print(f)


(N, M, K) = map(int, input().split())
A = [0] + list(map(int, input().split()))
B = list(map(int, input().split()))
x = sum(B)
y = M - 1
r = 0
for i in range(N + 1):
	x += A[i]
	while x > K and y >= 0:
		x -= B[y]
		y -= 1
	if x <= K:
		r = max(r, i + y + 1)
print(r)


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


import itertools as it, bisect as bs
f = lambda : map(int, input().split())
g = lambda : [0] + [*it.accumulate(f())]
(n, m, k) = f()
(A, B) = (g(), g())
(a, j) = (0, m)
for i in range(n + 1):
	t = bs.bisect(B, k - A[i])
	if t:
		a = max(a, i + t - 1)
print(a)


from bisect import bisect
(N, M, K) = map(int, input().split())
A = [0] + list(map(int, input().split()))
B = list(map(int, input().split()))
R_B = [0] * (M + 1)
i = 0
while i < M:
	R_B[i + 1] = R_B[i] + B[i]
	i += 1
ans = 0
i = 0
while i <= N and K >= A[i]:
	K -= A[i]
	idx = bisect(R_B, K)
	ans = max(ans, i + idx - 1)
	i += 1
print(ans)


def main():
	line1 = input()
	line2 = input()
	line3 = input()
	(n, m, k) = list(map(int, line1.split()))
	a = [0] + list(map(int, line2.split()))
	b = [0] + list(map(int, line3.split()))
	(mark1, mark2) = (0, 0)
	for i in range(1, len(a)):
		a[i] += a[i - 1]
		if a[i] > k:
			mark1 = i
			break
	for i in range(1, len(b)):
		b[i] += b[i - 1]
		if b[i] > k:
			mark2 = i
			break
	if mark1:
		a = a[:mark1]
	if mark2:
		b = b[:mark2]
	ans = 0
	for i in range(len(a)):
		target = k - a[i]
		tmp = 0
		(low, high) = (0, len(b))
		while low < high:
			mid = (low + high) // 2
			if b[mid] <= target:
				tmp = mid
				low = mid + 1
			else:
				high = mid - 1
		ans = max(ans, i + tmp)
	print(ans)
	return

def __starting_point():
	main()
__starting_point()


import bisect
(N, M, K) = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
for i in range(1, N):
	A[i] += A[i - 1]
for i in range(1, M):
	B[i] += B[i - 1]
ma = bisect.bisect(A, K)
for m in range(0, M):
	if B[m] > K:
		break
	n = bisect.bisect(A, K - B[m])
	if ma < n + m + 1:
		ma = n + m + 1
print(ma)


(N, M, K) = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
SA = 0
SB = sum(B)
Bn = len(B)
ans = 0
for An in range(len(A) + 1):
	if An > 0:
		SA += A[An - 1]
	if SA > K:
		break
	while SA + SB > K:
		Bn -= 1
		SB -= B[Bn]
	ans = max(ans, An + Bn)
print(ans)


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


from bisect import *
(n, m, k) = map(int, input().split())
A = [0]
for x in map(int, input().split()):
	if A[-1] + x > k:
		break
	A.append(A[-1] + x)
B = [0]
for x in map(int, input().split()):
	if B[-1] + x > k:
		break
	B.append(B[-1] + x)
ans = 0
for i in range(len(A)):
	j = bisect_right(B, k - A[i]) - 1
	ans = max(ans, i + j)
print(ans)


(N, M, K) = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
(ab_sum, answer) = (sum(B), 0)
j = M
for i in range(N + 1):
	while j > 0 and ab_sum > K:
		j -= 1
		ab_sum -= B[j]
	if ab_sum > K:
		break
	answer = max(answer, i + j)
	if i == N:
		break
	ab_sum += A[i]
print(answer)


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

def get_integral(nums):
	integral = [0]
	for i in range(0, len(nums)):
		integral.append(integral[i] + nums[i])
	return integral
integral_a = get_integral(A)
integral_b = get_integral(B)
ans = 0
bestj = M
for i in range(N + 1):
	ta = integral_a[i]
	if ta > K:
		break
	while ta + integral_b[bestj] > K:
		bestj -= 1
	best = i + bestj
	ans = max(best, ans)
print(ans)


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

def LI():
	return list(map(int, input().split()))
(n, m, k) = MI()
A = [0] + LI()
B = [0] + LI()
for i in range(1, n + 1):
	A[i] += A[i - 1]
for i in range(1, m + 1):
	B[i] += B[i - 1]
j = m
ans = 0
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)


(N, M, K) = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
(C, D) = ([0], [0])
for i in range(N):
	C.append(C[-1] + A[i])
for i in range(M):
	D.append(D[-1] + B[i])
ans = 0
for i in range(N + 1):
	T = K - C[i]
	(l, r) = (0, M + 1)
	while r - l > 1:
		m = (l + r) // 2
		if D[m] <= T:
			l = m
		else:
			r = m
	if T >= 0:
		ans = max(ans, i + l)
print(ans)


from itertools import *
f = lambda : map(int, input().split())
g = lambda : [0] + [*accumulate(f())]
(n, m, k) = f()
(A, B) = (g(), g())
(a, j) = (0, m)
for i in range(n + 1):
	while j and B[j] > k - A[i]:
		j -= 1
	if A[i] + B[j] <= k:
		a = max(a, i + j)
print(a)


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(accumulate(A))
b = list(accumulate(B))
book = bisect_right(b, K)
for i in range(N):
	if a[i] > K:
		break
	j = bisect_right(b, K - a[i])
	book = max(book, i + 1 + j)
print(book)


from itertools import accumulate
from bisect import bisect_right
(N, M, K) = map(int, input().split())
A = [0] + list(accumulate(map(int, input().split()), lambda x, y: x + y))
B = [0] + list(accumulate(map(int, input().split()), lambda x, y: x + y))
ans = 0
for i in range(N + 1):
	left = K - A[i]
	if left < 0:
		break
	j = bisect_right(B, left)
	ans = max(ans, i + j - 1)
print(ans)


(N, M, K, *AB) = map(int, open(0).read().split())
A = AB[:N]
B = AB[N:]
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 = 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)


from itertools import accumulate
import bisect
(n, m, k) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ac = [0] + list(accumulate(a))
bc = [0] + list(accumulate(b))
ans = 0
for i in range(bisect.bisect(ac, k)):
	bi = bisect.bisect(bc, k - ac[i])
	ans = max(ans, i + bi - 1)
print(ans)


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


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


(n, m, k) = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
import bisect
AR = [0]
BR = [0]
for i in range(n):
	AR.append(A[i] + AR[i])
for i in range(m):
	BR.append(B[i] + BR[i])
ans = 0
for i in range(n + 1):
	if AR[i] > k:
		break
	s = k - AR[i]
	t = bisect.bisect(BR, s) - 1
	ans = max(ans, i + t)
print(ans)


def binary_search(arr, num):
	left = 0
	right = len(arr) - 1
	while right - left > 0:
		mid = (left + right) // 2
		if arr[mid] > num:
			right = mid
		else:
			left = mid + 1
	if arr[left] > num:
		return left
	else:
		return len(arr)

def main():
	(n, m, k) = map(int, input().split())
	a_books = list(map(int, input().split()))
	b_books = list(map(int, input().split()))
	b_cum_sum = [0] * (m + 1)
	for i in range(m):
		b_cum_sum[i + 1] = b_cum_sum[i] + b_books[i]
	current = 0
	ans = 0
	for i in range(n + 1):
		if i > 0:
			current += a_books[i - 1]
		if current > k:
			break
		rest = k - current
		index = binary_search(b_cum_sum, rest)
		ans = max(ans, i + index - 1)
	print(ans)

def __starting_point():
	main()
__starting_point()


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


readints = lambda : list(map(int, input().split()))
(n, m, k) = readints()
a = readints()
b = readints()
a.insert(0, 0)
b.insert(0, 0)
ans = 0
j = m
tot = sum(b)
for i in range(n + 1):
	tot += a[i]
	while j >= 0 and tot > k:
		tot -= b[j]
		j -= 1
	if j < 0:
		break
	ans = max(ans, i + j)
print(ans)


import bisect
import itertools
(n, m, k) = map(int, input().split())
A = list(itertools.accumulate([0] + list(map(int, input().split()))))
B = list(itertools.accumulate([0] + list(map(int, input().split()))))
ans = 0
for (i, a) in enumerate(A):
	if a > k:
		break
	cnt = bisect.bisect_right(B, k - a) - 1
	ans = max(ans, i + cnt)
print(ans)


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


(n, m, k) = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
readtime_a = [0]
readtime_b = [0]
for i in range(n):
	readtime_a.append(readtime_a[i] + a[i])
for i in range(m):
	readtime_b.append(readtime_b[i] + b[i])
cnt = [0]
import bisect
for i in range(n + 1):
	if readtime_a[i] > k:
		continue
	else:
		cnt.append(i - 1 + bisect.bisect(readtime_b, k - readtime_a[i]))
print(max(cnt))


import itertools
(N, M, K) = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A_ = [0] + list(itertools.accumulate(A))
B_ = [0] + list(itertools.accumulate(B))
ans = 0
j = M
for i in range(N + 1):
	a = A_[i]
	K_a = K - a
	while j >= 0 and B_[j] > K_a:
		j -= 1
	if a + B_[j] <= K:
		ans = max(ans, i + j)
print(ans)


(n, m, k) = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))
b = sum(B)
ka = k
i = m
c = 0
re = 0
A.insert(0, 0)
for a in A:
	ka -= a
	if ka < 0:
		break
	while ka < b and i > 0:
		i -= 1
		b -= B[i]
	c += 1
	re = max(re, c + i)
print(re - 1)


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


(N, M, K) = map(int, input().split())
X = [0] + list(map(int, input().split()))
Y = list(map(int, input().split()))
(R, sY, j, t) = (0, sum(Y), M, 0)
for i in range(N + 1):
	t += X[i]
	if t > K:
		break
	while j and sY + t > K:
		j -= 1
		sY -= Y[j]
	R = max(R, j + i)
print(R)


(N, M, K) = map(int, input().split())
A = [0] + list(map(int, input().split()))
B = [0] + list(map(int, input().split()))
(ASum, BSum) = ([0] * (N + 1), [0] * (M + 1))
(ASum[0], BSum[0]) = (A[0], B[0])
(AOver, BOver) = (-1, -1)
for i in range(1, N + 1):
	ASum[i] = ASum[i - 1] + A[i]
	if AOver == -1 and ASum[i] > K:
		AOver = i - 1
for i in range(1, M + 1):
	BSum[i] = BSum[i - 1] + B[i]
	if BOver == -1 and BSum[i] > K:
		BOver = i - 1
if AOver == -1:
	AOver = N
if BOver == -1:
	BOver = M
ans = 0
import bisect
for i in reversed(range(AOver + 1)):
	if i + BOver <= ans:
		break
	time = K - ASum[i]
	ans = max(ans, i + bisect.bisect_right(BSum, time) - 1)
print(ans)


(N, M, K) = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))
RA = [0]
RB = [0]
tmp = 0
for i in range(N):
	tmp += A[i]
	RA.append(tmp)
tmp = 0
for i in range(M):
	tmp += B[i]
	RB.append(tmp)
ans = 0
best = M
for i in range(N + 1):
	if RA[i] > K:
		break
	while RB[best] > K - RA[i]:
		best -= 1
	ans = max(ans, i + best)
print(ans)


from math import gcd
from collections import Counter, deque, defaultdict
from heapq import heappush, heappop, heappushpop, heapify, heapreplace, merge
from bisect import bisect_left, bisect_right, bisect, insort_left, insort_right, insort
from itertools import accumulate, product, permutations, combinations, combinations_with_replacement
(n, m, k) = list(map(int, input().split()))
A = list(accumulate(list(map(int, input().split()))))
B = list(accumulate(list(map(int, input().split()))))
A = [0] + A
B = [0] + B
ans = []
for i in range(n + 1):
	c = bisect_right(B, k - A[i]) - 1
	if c != -1:
		ans.append(c + i)
print(max(ans))


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


(N, M, K) = map(int, input().split())
A = [0] + list(map(int, input().split()))
B = [0] + list(map(int, input().split()))
(ans, Bi, Asum, Bsum) = (0, M, 0, sum(B))
for Ai in range(N + 1):
	Asum += A[Ai]
	while Asum + Bsum > K and Bi > 0:
		Bsum -= B[Bi]
		Bi -= 1
	if Asum + Bsum <= K:
		ans = max(ans, Ai + Bi)
print(ans)


(N, M, K) = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
Asum = [0]
Bsum = [0]
for i in range(N):
	Asum.append(Asum[-1] + A[i])
for i in range(M):
	Bsum.append(Bsum[-1] + B[i])
m = 0
j = M
for i in range(len(Asum)):
	if Asum[i] > K:
		break
	while Asum[i] + Bsum[j] > K:
		j -= 1
	if m < i + j:
		m = i + j
print(m)


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


from itertools import accumulate
from bisect import bisect_right
(N, M, K) = [int(n) for n in input().split()]
A = [0] + [*accumulate([int(n) for n in input().split()])]
B = [*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)


(n, m, k) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = [0]
d = [0]
for i in range(n):
	c.append(a[i] + c[i])
for i in range(m):
	d.append(b[i] + d[i])
e = 0
for i in range(n + 1):
	if c[i] > k:
		break
	while c[i] + d[m] > k:
		m -= 1
	e = max(e, i + m)
print(e)


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


(N, M, K) = map(int, input().split())
A = [0] + [int(x) for x in input().split()]
B = [0] + [int(x) for x in input().split()]
s_b = 0
for i in range(M + 1):
	s_b += B[i]
	if s_b > K:
		s_b -= B[i]
		best = i - 1
		break
else:
	best = M
res = best
s_a = 0
for i in range(N + 1):
	s_a += A[i]
	if s_a > K:
		break
	for j in reversed(range(best + 1)):
		if s_b <= K - s_a:
			if i + j > res:
				res = i + j
			best = j
			break
		else:
			s_b -= B[j]
print(res)


from itertools import accumulate
import bisect
(n, m, k) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a_acc = [0] + list(accumulate(a))
b_acc = list(accumulate(b))
ans = 0
for an in range(n + 1):
	rem = k - a_acc[an]
	if rem < 0:
		break
	bn = bisect.bisect_right(b_acc, rem)
	ans = max(ans, an + bn)
print(ans)


(n, m, k) = map(int, input().split())
a = [int(x) for x in input().split()] + [10 ** 9 + 5]
b = [int(x) for x in input().split()] + [10 ** 9 + 5]
cnt = i = 0
for ai in a:
	if cnt + ai > k:
		break
	cnt += ai
	i += 1
j = 0
for bi in b:
	if cnt + bi > k:
		break
	cnt += bi
	j += 1
ans = s = i + j
for i in reversed(range(i)):
	s -= 1
	cnt -= a[i]
	while cnt + b[j] <= k:
		s += 1
		cnt += b[j]
		j += 1
	ans = max(ans, s)
print(ans)


def solve():
	(N, M, K) = map(int, input().split())
	A = list(map(int, input().split()))
	B = list(map(int, input().split()))
	ret = 0
	A_cusum = [0]
	A_border_index = 0
	for a_ in A:
		A_end = A_cusum[-1]
		if A_end + a_ > K:
			break
		A_cusum.append(A_end + a_)
	A_border_index = len(A_cusum) - 1
	ret = A_border_index
	B_cusum = [0]
	B_border_index = 0
	while A_border_index >= 0 and B_border_index < M:
		while B_border_index < M:
			B_end = B_cusum[-1]
			b_ = B[B_border_index]
			if A_cusum[A_border_index] + B_end + b_ > K:
				break
			B_cusum.append(B_end + b_)
			B_border_index += 1
		ret = max(ret, A_border_index + B_border_index)
		A_border_index -= 1
	print(ret)
solve()


from bisect import *
(n, m, k) = map(int, input().split())
A = [*map(int, input().split())]
B = [*map(int, input().split())]
tot = sum(B)
(ans, j) = (0, m)
for i in range(n + 1):
	while j > 0 and tot > k:
		j -= 1
		tot -= B[j]
	if tot > k:
		break
	if i < n:
		tot += A[i]
	ans = max(ans, i + j)
print(ans)


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

def LI():
	return list(map(int, input().split()))
(n, m, k) = MI()
a = [0] + LI()
b = [0] + LI()
for i in range(1, n + 1):
	a[i] += a[i - 1]
for i in range(1, m + 1):
	b[i] += b[i - 1]
ans = 0
j = m
for i in range(n + 1):
	if a[i] > k:
		continue
	while a[i] + b[j] > k:
		j -= 1
	ans = max(ans, i + j)
print(ans)


(n, m, k, *a) = map(int, open(0).read().split())
s = sum(a[:n])
x = i = j = n
a += (k,)
while ~i:
	while s + a[j] <= k:
		s += a[j]
		j += 1
	if s <= k:
		x = max(x, i + j)
	i -= 1
	s -= a[i]
print(x - n)


import bisect
(n, m, k) = map(int, input().split())
(a, b) = ([0], [0])
for (i, x) in enumerate(input().split()):
	a.append(a[i] + int(x))
for (i, x) in enumerate(input().split()):
	b.append(b[i] + int(x))
res = 0
a_ = 0
for i in range(n + 1):
	if a[i] > k:
		break
	j = bisect.bisect(b, k - a[i]) - 1
	res = max(res, i + j)
print(res)


(n, m, k) = list(map(int, input().split()))
aa = list(map(int, input().split()))
bb = list(map(int, input().split()))
asum = [0]
bsum = [0]
for i in range(len(aa)):
	asum.append(asum[i] + aa[i])
for i in range(len(bb)):
	bsum.append(bsum[i] + bb[i])
j = len(bsum) - 1
ans = 0
for i in range(len(asum)):
	while j >= 0:
		if k >= asum[i] + bsum[j]:
			ans = max(ans, i + j)
			break
		else:
			j -= 1
print(ans)


(n, m, k) = map(int, input().split())
shelfA = list(map(int, input().split()))
shelfB = list(map(int, input().split()))
prodA = [0] * (n + 1)
prodB = [0] * (m + 1)
for i in range(1, n + 1):
	prodA[i] = prodA[i - 1] + shelfA[i - 1]
for i in range(1, m + 1):
	prodB[i] = prodB[i - 1] + shelfB[i - 1]
ans = -1
j = m
for i in range(n + 1):
	if prodA[i] > k:
		break
	while prodB[j] > k - prodA[i]:
		j -= 1
	ans = max(ans, i + j)
print(ans)


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())))
X = 0
Y_num = [0] * (N + 1)
for i in range(N + 1):
	if A[i] <= K:
		X = bisect.bisect_right(B, K - A[i])
		Y_num[i] = i + X - 1
print(max(Y_num))


from bisect import bisect_right as br
from itertools import accumulate as ac
(n, m, k) = map(int, input().split())
a = list(ac([0] + list(map(int, input().split()))))
b = list(ac([0] + list(map(int, input().split()))))
l = 0
for i in range(n + 1):
	if k < a[i]:
		break
	else:
		l = max(l, i + br(b, k - a[i]) - 1)
print(l)


(n, m, k) = map(int, input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
c = [0]
for i in range(n):
	if c[i] + a[i] <= k:
		c.append(c[i] + a[i])
	else:
		break
d = [0]
for i in range(m):
	if d[i] + b[i] <= k:
		d.append(d[i] + b[i])
	else:
		break
e = 0
f = len(d) - 1
for i in range(len(c)):
	for j in range(f, -1, -1):
		if c[i] + d[j] <= k:
			if i + j > e:
				e = i + j
			f = j
			break
print(e)


(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]
for i in range(1, M + 1):
	B[i] += B[i - 1]
best = 0
j = M
for i in range(0, N + 1):
	while j > 0:
		if A[i] + B[j] <= K:
			break
		else:
			j -= 1
	if A[i] + B[j] <= K:
		best = max(best, i + j)
	pass
print(best)


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


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


from bisect import *
(N, M, K) = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = [0]
for i in range(N):
	C.append(C[i] + A[i])
D = [0]
for i in range(M):
	D.append(D[i] + B[i])
P = 0
for i in range(N + 1):
	if C[i] > K:
		break
	P = max(P, i + bisect_right(D, K - C[i]) - 1)
print(P)


(n, m, k) = list(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]
for i in range(1, m + 1):
	b[i] += b[i - 1]
ans = 0
b_cnt = m
for a_cnt in range(n + 1):
	if a[a_cnt] > k:
		continue
	while a[a_cnt] + b[b_cnt] > k:
		b_cnt -= 1
	ans = max(ans, a_cnt + b_cnt)
print(ans)


from itertools import accumulate
(n, m, k) = map(int, input().split())
Acum = [0] + list(accumulate(map(int, input().split())))
Bcum = [0] + list(accumulate(map(int, input().split())))
ans = 0
b = m
for a in range(n + 1):
	if k < Acum[a]:
		break
	while k < Acum[a] + Bcum[b]:
		b -= 1
	if ans < a + b:
		ans = a + b
print(ans)


(n, m, k) = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
sum_a = [0]
sum_b = [0]
counta = 0
for x in a:
	counta += x
	if counta <= k:
		sum_a.append(counta)
	else:
		break
countb = 0
for y in b:
	countb += y
	if countb <= k:
		sum_b.append(countb)
	else:
		break
n = len(sum_b)
ans = 0
for i in range(len(sum_a)):
	for j in reversed(range(n)):
		if sum_a[i] + sum_b[j] <= k:
			ans = max(ans, i + j)
			n = j + 1
			break
	else:
		break
print(ans)


from itertools import accumulate
from bisect import bisect_right
R = lambda : map(int, input().split())
(*_, k) = R()
a = accumulate(R())
b = list(accumulate(R()))
r = bisect_right(b, k)
for (i, x) in enumerate(a, 1):
	if x > k:
		break
	r = max(r, i + bisect_right(b, k - x))
print(r)


(N, M, K) = list(map(int, input().split()))
A = list(map(int, input().split())) + [10000000000]
B = list(map(int, input().split())) + [10000000000]
S = 0
a = 0
while S + A[a] <= K:
	S += A[a]
	a += 1
b = 0
ans = a
while a >= 0:
	if S + B[b] <= K:
		S += B[b]
		b += 1
	else:
		S -= A[a - 1]
		a -= 1
	ans = max(ans, a + b)
print(ans)


from itertools import accumulate
(N, M, K) = map(int, input().split())
Alist = list(map(int, input().split()))
Blist = list(map(int, input().split()))
cumsum = list(accumulate(Alist))
ans = 0
num = 0
flag = 0
flag1 = 0
for i in range(N):
	if cumsum[i] > K:
		suma = cumsum[i - 1]
		break
else:
	flag1 = True
	ans += 1
	num += 1
	suma = cumsum[i]
ans += i
num += i
k = 0
while suma + Blist[k] <= K:
	suma += Blist[k]
	k += 1
	num += 1
	if k == M:
		print(num)
		return
ans = max(ans, num)
for j in range(i + 1):
	if flag1:
		suma -= Alist[i - j]
	else:
		suma -= Alist[i - 1 - j]
	num -= 1
	while suma + Blist[k] <= K:
		suma += Blist[k]
		k += 1
		num += 1
		ans = max(ans, num)
		if k >= M:
			flag = True
			break
	if flag:
		break
ans = max(ans, num)
print(ans)


from bisect import *
(n, m, k) = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A.insert(0, 0)
for i in range(1, len(A)):
	A[i] += A[i - 1]
for i in range(1, len(B)):
	B[i] += B[i - 1]
ans = 0
for i in range(len(A)):
	rest_time = k - A[i]
	if rest_time >= 0:
		numb = bisect_right(B, rest_time)
		anstmp = i + numb
		ans = max(ans, anstmp)
print(ans)


import numpy as np
(n, m, k) = map(int, input().split())
li_a = np.array(list(map(int, input().split())))
li_b = np.array(list(map(int, input().split())))
(a_i, b_i) = (np.hstack([np.array([0]), li_a.cumsum()]), np.hstack([np.array([0]), li_b.cumsum()]))
j = m
ans = 0
for i in range(n + 1):
	if a_i[i] > k:
		break
	while b_i[j] > k - a_i[i]:
		j -= 1
	ans = max(ans, i + j)
print(ans)


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


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


import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from itertools import accumulate
from bisect import bisect

def main():
	(n, m, k) = map(int, input().split())
	a = tuple(accumulate(map(int, input().split())))
	b = tuple(accumulate(map(int, input().split())))
	r = bisect(b, k)
	for (i1, ae) in enumerate(a):
		if k < ae:
			break
		r = max(r, bisect(b, k - ae) + i1 + 1)
	print(r)

def __starting_point():
	main()
__starting_point()


import bisect
(N, M, K) = map(int, input().split())
arr1 = list(map(int, input().split()))
arr2 = list(map(int, input().split()))
A = [0]
for i in range(N):
	A.append(arr1[i] + A[-1])
B = [0]
for i in range(M):
	B.append(arr2[i] + B[-1])
ans = 0
for i in range(N + 1):
	if A[i] > K:
		break
	j = bisect.bisect_right(B, K - A[i]) - 1
	ans = max(ans, i + j)
print(ans)


import collections as cc
import itertools as it
import bisect as bi
I = lambda : list(map(int, input().split()))
(n, m, k) = I()
a = I()
b = I()
a = [0] + list(it.accumulate(a))
b = [0] + list(it.accumulate(b))
ans = 0
for i in range(n + 1):
	if k - a[i] < 0:
		break
	x = bi.bisect_right(b, k - a[i])
	ans = max(ans, i + x - 1)
print(ans)


(n, m, k) = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
AS = [0]
BS = [0]
for i in A:
	if i + AS[-1] <= k:
		AS.append(i + AS[-1])
	else:
		break
for i in B:
	if i + BS[-1] <= k:
		BS.append(i + BS[-1])
	else:
		break
(r, j) = (0, len(BS) - 1)
for i in range(len(AS)):
	while AS[i] + BS[j] > k:
		j -= 1
	r = max(r, i + j)
print(r)