Buggy python submission (verdict: Time Limit Exceeded):
# def helper(i, j, a, b, k, dp):
# 	n = len(a)
# 	m = len(b)

# 	if dp[i][j][k-1] != -1:
# 		return dp[i][j][k-1]
# 	# if i == n:
# 	# 	dp[i][j][k-1] = 0
# 	# 	s = 0
# 	# 	for jj in range(j, m):
# 	# 		if b[jj] + s > k:
# 	# 			return dp[i][j][k-1]
# 	# 		dp[i][j][k-1] += 1
# 	# 		s += b[jj]
# 	# 	return dp[i][j][k-1]
# 	# if j == m:
# 	# 	dp[i][j][k-1] = 0
# 	# 	s = 0
# 	# 	for ii in range(i, n):
# 	# 		if a[ii] + s > k:
# 	# 			return dp[i][j][k-1]
# 	# 		dp[i][j][k-1] += 1
# 	# 		s += a[ii]
# 	# 	return dp[i][j][k-1]

# 	# if min(a[i], b[j]) > k:
# 	# 	dp[i][j][k-1] = 0
# 	# 	return dp[i][j][k-1]
# 	dp[i][j][k-1] = 0
# 	print (i, j)
# 	if i < n:
# 		if a[i] < k:
# 			dp[i][j][k-1] = max(dp[i][j][k-1], helper(i+1, j, a, b, k-a[i], dp) + 1)
# 	if j < m:
# 		if b[j] < k:
# 			dp[i][j][k-1] = max(dp[i][j][k-1], helper(i, j+1, a, b, k-b[j], dp) + 1)
# 	return dp[i][j][k-1]

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

# dp = [[[-1 for _ in range(k)] for _ in range(m+1)] for _ in range(n+1)]

if k >= sum(a) + sum(b):
	print (m+n)
# r = helper(0, 0, a, b, k, dp)
else:
	r = 0
	stack = [(0, 0, k)]
	dic = {}
	dic[(0, 0)] = k

	while len(stack) != 0:
		i, j, kk = stack.pop()
		if i == n:
			# if j < m:
			# 	if b[j] > kk:
			# 		r = max(r, i + j)
			# 		continue
			# 	if b[j] <= kk:
			# 		if (i, j+1) not in dic:
			# 			stack.append((i, j+1, kk - b[j]))
			# 			dic[(i, j+1)] = kk - b[j]
			# 		elif (kk - b[j]) > dic[(i, j+1)]:
			# 			stack.append((i, j+1, kk - b[j]))
			# 			dic[(i, j+1)] = kk - b[j]
			while j < m:
				if b[j] > kk:
					r = max(r, i + j)
					break
				if (i, j+1) in dic and dic[(i, j+1)] > kk - b[j]:
					break
				dic[(i, j+1)] = kk - b[j]
				kk -= b[j]
				j += 1
					


		elif j == m:
			# if i < n:
			# 	if a[i] > kk:
			# 		r = max(r, i+j)
			# 		continue
			# 	if a[i] <= kk:
			# 		if (i+1, j) not in dic:
			# 			stack.append((i+1, j, kk - a[i]))
			# 			dic[(i+1, j)] = kk - a[i]
			# 		elif (kk - a[i]) > dic[(i+1, j)]:
			# 			stack.append((i+1, j, kk - a[i]))
			# 			dic[(i+1, j)] = kk - a[i]
			while i < n:
				if a[i] > kk:
					r = max(r, i + j)
					break
				if (i+1, j) in dic and dic[(i+1, j)] > kk - a[i]:
					break
				dic[(i+1, j)] = kk - a[i]
				kk -= a[i]
				i += 1
		else:
			if min(a[i], b[j]) > kk:
				r = max(r, i + j)
			if a[i] <= kk:
				if (i+1, j) not in dic:
					stack.append((i+1, j, kk-a[i]))
					dic[(i+1, j)] = kk - a[i]
				elif dic[(i+1, j)] < (kk - a[i]):
					stack.append((i+1, j, kk-a[i]))
					dic[(i+1, j)] = kk - a[i]
			if b[j] <= kk:
				if (i, j+1) not in dic:
					stack.append((i, j+1, kk - b[j]))
					dic[(i, j+1)] = kk - b[j]
				elif dic[(i, j+1)] < (kk - b[j]):
					stack.append((i, j+1, kk - b[j]))
					dic[(i, j+1)] = kk - b[j]

	print (str(r))



Fixed submission:
n, m, k = list(map(int, input().split(" ")))
a = list(map(int, input().split(" ")))
b = list(map(int, input().split(" ")))

sa = [0]
for i in range(n):
	if (sa[-1] + a[i]) > k:
		break
	sa.append(sa[-1] + a[i])
sb = [0]
for i in range(m):
	if (sb[-1] + b[i]) > k:
		break
	sb.append(sb[-1] + b[i])

r = 0
j = 0
cur = sa[-1]

for i in range(len(sa)-1, -1, -1):
	while j < len(sb):
		if sa[i] + sb[j] > k:
			break
		j += 1
	r = max(r, i + j - 1)
print (r)

Buggy python submission (verdict: Time Limit Exceeded):
n, m, k = list(map(int, input().split(" ")))
a = list(map(int, input().split(" ")))
b = list(map(int, input().split(" ")))

# dp = [[[-1 for _ in range(k)] for _ in range(m+1)] for _ in range(n+1)]

if k >= sum(a) + sum(b):
	print (m+n)
# r = helper(0, 0, a, b, k, dp)
else:
	r = 0
	stack = [(0, 0, k)]
	dic = {}
	dic[(0, 0)] = k

	while len(stack) != 0:
		i, j, kk = stack.pop()
		if i == n:
			if j < m:
				if b[j] > kk:
					r = max(r, i + j)
					continue
				if b[j] <= kk:
					if (i, j+1) not in dic:
						stack.append((i, j+1, kk - b[j]))
						dic[(i, j+1)] = kk - b[j]
					elif (kk - b[j]) > dic[(i, j+1)]:
						stack.append((i, j+1, kk - b[j]))
						dic[(i, j+1)] = kk - b[j]
		elif j == m:
			if i < n:
				if a[i] > kk:
					r = max(r, i+j)
					continue
				if a[i] <= kk:
					if (i+1, j) not in dic:
						stack.append((i+1, j, kk - a[i]))
						dic[(i+1, j)] = kk - a[i]
					elif (kk - a[i]) > dic[(i+1, j)]:
						stack.append((i+1, j, kk - a[i]))
						dic[(i+1, j)] = kk - a[i]
		else:
			if min(a[i], b[j]) > kk:
				r = max(r, i + j)
			if a[i] <= kk:
				if (i+1, j) not in dic:
					stack.append((i+1, j, kk-a[i]))
					dic[(i+1, j)] = kk - a[i]
				elif dic[(i+1, j)] < (kk - a[i]):
					stack.append((i+1, j, kk-a[i]))
					dic[(i+1, j)] = kk - a[i]
			if b[j] <= kk:
				if (i, j+1) not in dic:
					stack.append((i, j+1, kk - b[j]))
					dic[(i, j+1)] = kk - b[j]
				elif dic[(i, j+1)] < (kk - b[j]):
					stack.append((i, j+1, kk - b[j]))
					dic[(i, j+1)] = kk - b[j]

	print (str(r))



Fixed submission:
n, m, k = list(map(int, input().split(" ")))
a = list(map(int, input().split(" ")))
b = list(map(int, input().split(" ")))

sa = [0]
for i in range(n):
	if (sa[-1] + a[i]) > k:
		break
	sa.append(sa[-1] + a[i])
sb = [0]
for i in range(m):
	if (sb[-1] + b[i]) > k:
		break
	sb.append(sb[-1] + b[i])

r = 0
j = 0
cur = sa[-1]

for i in range(len(sa)-1, -1, -1):
	while j < len(sb):
		if sa[i] + sb[j] > k:
			break
		j += 1
	r = max(r, i + j - 1)
print (r)