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

i, j = 0, 0
c = 0
num = 0
while i < n and c + A[i] <= k:
    c = c + A[i]
    num += 1
    i += 1
while j < m and B[j] + c <= k:
    c = c + B[j]
    num += 1
    j += 1
for a in range(i-1, -1, -1):
    c -= A[a]
    while j < m and c + B[j] <= k:
        c = c + B[j]
        j += 1
        num = max(num, a+j)
print (num)

Solution (Python):
import sys
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]

mod = 10 ** 9 + 7

N, M, K = LI()
desk = LIR(2)
a, b = [0], [0]



for i in range(N):
    a.append(a[i] + desk[0][i])
for j in range(M):
    b.append(b[j] + desk[1][j])



ans, j = 0, M 

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

print(ans)

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

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

for i in range(n):
    sum_a.append(sum_a[i] + a[i])
for j in range(m):
    sum_b.append(sum_b[j] + b[j])

temp = 0
j = m
ans = 0
for i in range(n+1):
    if sum_a[i] > k:
        temp = i -1
        if temp > ans:
            ans = temp
        break
    else:
        while k - sum_a[i] < sum_b[j]:
            j -= 1
        temp = i + j
        if temp > ans:
            ans = temp
            
print(ans)

Solution (Python):
A,B,K=map(int,input().split())
List_A = list(map(int, input().split()))
List_B = list(map(int, input().split()))
SumListA=[]
SumListA.append(0)
SumListB=[]
SumListB.append(0)
res =0
a=0
for i in range(A):
  SumListA.append(SumListA[i]+List_A[i])
for i in range(B):
  SumListB.append(SumListB[i]+List_B[i])

j=B
for i in range(A+1):
  if SumListA[i] > K:
    break
  while K-SumListA[i] < SumListB[j]:
    j += -1
  res = max(i+j,res)
print(res)

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]
aa = 0
bb = 0
for i in A:
    aa += i
    a.append(aa)
for j in B:
    bb += j
    b.append(bb)

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

print(ans)

Solution (Python):
import sys
readline = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7

INF = 10**18
eps = 10**-7

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

csuma = [0]
csumb = [0]
for ai in a:
    csuma.append(csuma[-1]+ai)
for bi in b:
    csumb.append(csumb[-1]+bi)


ans = 0
for i,csumai in enumerate(csuma):
    ng = m+1
    ok = -1
    while ng - ok > 1:
        mid = (ok + ng)//2
        if csumb[mid] <= k-csumai:
            ok = mid
        else:
            ng = mid
    if ok >= 0:
        ans = max(ans,i+ok)
print(ans)

Solution (Python):
N, M, K = map(int, input().split())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]

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

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

Solution (Python):

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

ruia = [0]*N
ruia[0]=A[0]
for i in range(1,N):
    ruia[i]=ruia[i-1]+A[i]
ruia=[0]+ruia

ruib = [0]*M
ruib[0]=B[0]
for i in range(1,M):
    ruib[i]=ruib[i-1]+B[i]

import bisect

ans=0
for i,r in enumerate(ruia):
    nokori =K-r
    if nokori>=0:
        n_nokori = bisect.bisect(ruib,nokori)
        ans = max(ans,i+n_nokori)

print(ans)

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

SumA=[0 for _ in range(N+1)]
SumB=[0 for _ in range(M+1)]
for i in range(1,N+1):
    SumA[i]=SumA[i-1]+A[i-1]
for i in range(1,M+1):
    SumB[i]=SumB[i-1]+B[i-1]

def binary_search(Time,IndexA):
    if Time>K:
        return 0 
    l=0
    r=M
    c=(l+r)//2
    while r-l>1:
        if Time+SumB[c]>K: r=c
        if Time+SumB[c]<=K: l=c
        c=(l+r)//2
    if Time+SumB[r]>K: IndexB=l
    else: IndexB=r
    NumOfBooks=IndexA+IndexB
    return NumOfBooks

MaxNumOfBooks=0
for IndexA in range(N+1):
    NumOfBooks=binary_search(SumA[IndexA],IndexA)
    if MaxNumOfBooks<NumOfBooks: MaxNumOfBooks=NumOfBooks
print(MaxNumOfBooks)

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

SumA=[0 for _ in range(N+1)]
SumB=[0 for _ in range(M+1)]
for i in range(1,N+1):
    SumA[i]=SumA[i-1]+A[i-1]
for i in range(1,M+1):
    SumB[i]=SumB[i-1]+B[i-1]

def binary_search(Time,IndexA):
    if Time>K:
        return 0 
    l=0
    r=M
    c=(l+r)//2
    while r-l>1:
        if Time+SumB[c]>K: r=c
        if Time+SumB[c]<=K: l=c
        c=(l+r)//2
    if Time+SumB[r]>K: IndexB=l
    else: IndexB=r
    NumOfBooks=IndexA+IndexB
    return NumOfBooks

MaxNumOfBooks=0
for IndexA in range(N+1):
    NumOfBooks=binary_search(SumA[IndexA],IndexA)
    if MaxNumOfBooks<NumOfBooks: MaxNumOfBooks=NumOfBooks
print(MaxNumOfBooks)

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

aa,bb=[0],[0]
for i in range(n):
  aa.append(aa[i]+a[i])
for i in range(m):
  bb.append(bb[i]+b[i])
ans,j=0,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):
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 = 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)

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

index_a = -1
index_b = 0
num_books = 0
now_times = 0

for a in A :
  if now_times + a <= K :
    num_books += 1
    now_times += a
    index_a += 1
  else :
    break

temp_books = num_books
temp_times = now_times
  
while index_a > -2 :
  while index_b < len(B) and temp_times + B[index_b] <= K :
    temp_books += 1
    temp_times += B[index_b]
    index_b += 1
    if temp_books > num_books :
      num_books = temp_books
  temp_books -= 1
  temp_times -= A[index_a]
  index_a -= 1
      
print(num_books)

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

Solution (Python):
import sys



import bisect


import itertools
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
inf = 10**18
MOD = 1000000007
ri = lambda : int(input())
rs = lambda : input().strip()
rl = lambda : list(map(int, input().split()))

n,m,k = rl()
a = rl()
b = rl()
sa=[0]
sb=[]
tmp=0
for i in a:
    tmp+=i
    sa.append(tmp)
tmp=0
for i in b:
    tmp+=i
    sb.append(tmp)
ans=[]
for i,e in enumerate(sa):
    if k-e>=0:
        ans.append(i+bisect.bisect_right(sb, k-e))
print(max(ans))

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

Solution (Python):

import sys

from collections import deque





sys.setrecursionlimit(1000000000)



def main():
    n, m, k = map(int, input().split())
    A = deque(map(int, input().split()))
    B = deque(map(int, input().split()))

    cnt_a = 0
    time = 0
    readed_a = deque([])

    for a in A:
        if time + a > k:
            break
        cnt_a += 1
        time += a
        readed_a.append(a)

    readed_b = deque([])
    cnt_ab = cnt_a
    maxcnt_b = cnt_ab
    for b in B:
        while True:
            if time + b <= k:       
                readed_b.append(b)
                cnt_ab += 1
                if maxcnt_b < cnt_ab:
                    maxcnt_b = cnt_ab
                time += b
                break
            else:                   
                if len(readed_a) > 0:   
                    cnt_ab -= 1
                    time -= readed_a.pop()
                else:                   
                    print(max(cnt_a, maxcnt_b))
                    return
    print(max(cnt_a, maxcnt_b))


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

i = 0
j = 0
count = 0
a = [0]*N
b = [0]*M
ans = 0

sum = 0
for i in range(N):
    sum += A[i]
    a[i] = sum
sum = 0
for j in range(M):
    sum += B[j]
    b[j] = sum

ind = bisect.bisect_right(b, K)
ans = ind

for i in range(N):
    if a[i] <= K:
        ind = bisect.bisect_right(b, K-a[i])
        ans = max(ans, ind+i+1)
    else:
        break

print(ans)

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

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

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

    asum = [0]*(n+1)
    bsum = [0]*(m+1)

    for i in range(n):
        asum[i+1] = asum[i] + a[i]

    for i in range(m):
        bsum[i+1] = bsum[i] + b[i]

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


if __name__ == '__main__':
    main()

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

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

time = sum(A)
count = len(A)
ans = 0
j=0
for i in range(N,-1,-1):
    if i != N:
        time -= A[i]
        count -= 1
    if time>K:
        continue
    if time <= K and j<M:
        while time+B[j] <=K:
            time +=B[j]
            j +=1
            count +=1
            if j >=M:
                break
    ans = max(ans,count)
print(ans)

Solution (Python):
import numpy as np

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])
b_set = set(b)
b_array = np.array(b)
max_cnt = 0




for i in range(N+1):
    temp = K - a[i]
    if temp >= 0:
        cnt = 0
        if temp in b_set:
            
            cnt = i + b_array.searchsorted(temp)
        else:
            
            cnt = i + b_array.searchsorted(temp) - 1
        if max_cnt < cnt:
            max_cnt = cnt
print(max_cnt)

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

cs_A = [0] 
for i in A:
    cs_A.append(cs_A[-1] + i)
cs_B = [0] 
for i in B:
    cs_B.append(cs_B[-1] + i)

def is_ok(mid, v):
    if v + cs_B[mid] <= K:
        return True
    else:
        return False

def meguru_bisect(v, 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, v):
            ok = mid
        else:
            ng = mid
    return ok
ans = 0
for i in range(0, N+1):
    v = cs_A[i]
    idx = meguru_bisect(v, M+1, -1)
    if 0 <= idx <= M:
        if v + cs_B[idx] <= K:
            ans = max(ans, i + idx) 
print(ans)

Solution (Python):
n, m, k = map(int, input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
a = [0] + a
seen = []
runSum = 0
for i, val in enumerate(b):
    runSum += val
    seen.append((runSum, i))
seen.sort()
def findLessEqual(target):
    l, r = 0, len(seen) - 1
    result = -1
    resultVal = 0
    while l <= r:
        mid = (l + r) // 2
        if seen[mid][0] == target:
            return seen[mid][0], seen[mid][1]
        elif seen[mid][0] < target:
            resultVal, result = seen[mid][0], seen[mid][1]
            l = mid + 1
        else:
            r = mid - 1
    return resultVal, result

runSum = 0
result = 0
for i, val in enumerate(a):
    runSum += val
    if runSum <= k:
        complement = k - runSum
        compVal, compI = findLessEqual(complement)
        if compVal + runSum <= k:
            result = max(result, i + compI + 1)
        else:
            result = max(result, i)
print(result)

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

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

t = 0
ans = 0

for i in range(m):
    t += b[i]

j = m

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

print(ans)

Solution (Python):
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
from functools import reduce

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

n, m, k = rm()
a = rl()
b = list(itertools.accumulate(rl()))
if a[0] > k:
    ans = 0
else:
    left = 0
    right = m
    while right - left > 1:
        j = (left + right) // 2
        if b[j] > k:
            right = j
        else:
            left = j
    ans = left + 1
t = 0
b0 = b[0]
for cnt, i in enumerate(a, 1):
    t += i
    if t > k:
        break
    if b0 > k-t:
        ans = max(ans, cnt)
        continue
    left = 0
    right = m
    while right - left > 1:
        j = (left + right) // 2
        if b[j] > k-t:
            right = j
        else:
            left = j
    ans = max(ans, cnt + left + 1)
print(ans)

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

a = [0]
b = [0]
for i in range(N):
    a.append(a[i]+A_list[i])
for i in range(M):
    b.append(b[i]+B_list[i])

j = M
while b[j] > K:
    j -= 1
max = j
i = 0
while a[i]<=K :
    while b[j]> K-a[i]:
        j -= 1
    if max <= i+j:
        max = i+j
    if i < N:
        i += 1
    else:
        break

print(max)

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

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

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 9)

def int1(x): return int(x) - 1
def II(): return int(input())
def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())
def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def MS(): return input().split()
def LS(): return list(input())
def LLS(rows_number): return [LS() for _ in range(rows_number)]
def printlist(lst, k=' '): print(k.join(list(map(str, lst))))
INF = float('inf')


from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product, permutations


from bisect import bisect_left, bisect_right

def solve():
    N, M, K = MI()
    A = LI()
    B = LI()

    A = list(accumulate(A))
    B = list(accumulate(B))
    
    
    
    ans = bisect_right(B, K)
    for i, a in enumerate(A):
        th = K - a
        if th < 0: break
        ib = bisect_right(B, th)
        ans = max(i+1+ib, ans)
    print(ans)
    

if __name__ == '__main__':
    solve()

Solution (Python):
import itertools
import math
import fractions
import functools
import copy
from collections import deque
from functools import reduce
from decimal import Decimal


def main():
    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(len(a)):
        a_sum.append(a_sum[i]+a[i])
    for i in range(len(b)):
        b_sum.append((b_sum[i]+b[i]))

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

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):

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)

Solution (Python):
def main():
    import bisect
    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
    for i in range(n+1):
        cost = A[i]
        c = i
        if cost > k:
            continue
        cost = k-cost
        c += bisect.bisect_right(B,cost)-1
        if c>ans:
            ans = c
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
n, m, k = map(int, input().split(' '))
ans = 0

a_ls = [0] + list(map(int, input().split(' ')))
b_ls = [0] + list(map(int, input().split(' ')))

for i in range(1, n+1):
    a_ls[i] += a_ls[i-1]
for i in range(1, m+1):
    b_ls[i] += b_ls[i-1]

b_cnt = m
for a_cnt in range(0, n+1):
    if a_ls[a_cnt] > k:
        continue
    while a_ls[a_cnt] + b_ls[b_cnt] > k:
        b_cnt -= 1
    ans = max(ans, a_cnt + b_cnt)

print(ans)

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

AccA = [0 for i in range(N+1)]
AccB = [0 for i in range(M+1)]

for i in range(1,N+1):
    AccA[i] = AccA[i-1] + A[i-1]
for j in range(1,M+1):
    AccB[j] = AccB[j-1] + B[j-1]

import bisect

Max = 0


for n in range(N+1):
    if(AccA[n] > K): break
    books = n
    time_left = K - AccA[n]
    books += bisect.bisect_right(AccB,time_left) - 1
    Max = max(Max,books)
    

print(Max)

Solution (Python):
N, M, K = list(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[i]+A[i])
for i in range(M):
    B_sum.append(B_sum[i]+B[i])

num = 0
J = M
for i in range(N+1):
    if A_sum[i] > K:
        break
    for j in reversed(range(J+1)):
        if A_sum[i] + B_sum[j] <= K:
            if num < i+j:
                num = i+j
            J = j
            break

print(num)

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

a = [0 for _ in range(n)]
b = [0 for _ in range(m)]

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

mx = sum(a)+sum(b)

a_sum = [0 for _ in range(n)]
b_sum = [0 for _ in range(m)]

sm = 0
for i in range(n):
    sm += a[i]
    a_sum[i] = sm

sm = 0
for i in range(m):
    sm += b[i]
    b_sum[i] = sm



def solve():
    if(k >= mx):
        print(n+m)
        return
    
    
    caca = bisect.bisect_right(b_sum,k)
    
    for i in range(n):
        a_a = a_sum[i]
        rd = k - a_a
        if(rd < 0):
            continue

        b_b = bisect.bisect_right(b_sum,rd)
        caca_tmp = i + 1 + b_b
        caca = max(caca,caca_tmp)
        
    
    print(caca)



solve()

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

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

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

suma = [0] * (n)
sumb = [0] * (m)
suma[0] = a[0]
sumb[0] = b[0]

for i in range(1,n):
    suma[i] = suma[i-1]+a[i]
for j in range(1,m):
    sumb[j] = sumb[j-1]+b[j]
    
suma.insert(0,0)
sumb.insert(0,0)

ans, cur = 0, m
for i in range(n+1):
  target = k - suma[i]
  if target < 0:
      break
  while sumb[cur] > target:
      cur -= 1
  ans = max(ans, i+cur)
print(ans)

Solution (Python):
n, m, x = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
b = list(map(int, input().split(' ')))
A = [0]
B = [0]

for i in range(len(a)):
	A.append(A[-1] + a[i])
for i in range(len(b)):
	B.append(B[-1] + b[i])

y = len(B) - 1
ans = 0
for i in range(len(A)):
	while A[i] + B[y] > x:
		y -= 1
		if y == -1:
			break
	if y == -1:
		break
	if i + y > ans:
		ans = i + y

print(ans)

Solution (Python):

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

import bisect
A=[0]
for i in range(n):
    A.append(A[i]+al[i])
B=[0]
for i in range(m):
    B.append(B[i]+bl[i])

mx=0
for i in range(n+1):
    rem=k-A[i]
    if rem<0:
        continue
    bnum=bisect.bisect_right(B,rem)-1
    mx=max(mx,i+bnum)

print(mx)

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

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

Solution (Python):

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


A=[0]
for i in range(n):
    A.append(A[i]+al[i])
B=[0]
for i in range(m):
    B.append(B[i]+bl[i])
B.append(max(B[m],k)+1)
mx=0
for i in range(n+1):
    rem=k-A[i]
    if rem<0:
        continue
    left=0;right=m+1
    while right-left>1:
        mid=(left+right)//2
        if B[mid]>rem:
            right=mid
        else:
            left=mid
    mx=max(left+i,mx)

print(mx)

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

print(ans)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random,resource

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


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

    d = [0]
    for c in a:
        d.append(d[-1] + c)

    e = [0]
    for c in b:
        e.append(e[-1] + c)

    r = 0
    di = len(d) - 1
    for ei in range(len(e)):
        if e[ei] > k:
            break

        while d[di] + e[ei] > k:
            di -= 1

        t = ei + di
        if r < t:
            r = t

    return r


print(main())