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

Solution (Python):
N,M,K=[int(s) for s in input().split()]
A=[int(s) for s in input().split()]
B=[int(s) for s in input().split()]
Asum=[0 for i in range(N+1)]
Bsum=[0 for i in range(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]

def nibu(x):
  time=set()
  for i in range(0,N+1):
    if i>x:
      break
    if x-i>M:
      continue
    time.add(Asum[i]+Bsum[x-i])
  if min(time)<=K:
    return True
  else:
    return False
  
nibu_l=0
nibu_r=N+M
while nibu_r-nibu_l>1:
  c=(nibu_l+nibu_r)//2
  if nibu(c)==True:
    nibu_l=c
  else:
    nibu_r=c
if nibu(nibu_r)==True:
  print(nibu_r)
else:
  print(nibu_l)

Solution (Python):

import sys
input = sys.stdin.readline



def swaparr(arr, a,b):
    temp = arr[a];
    arr[a] = arr[b];
    arr[b] = temp


def gcd(a,b):
    if b == 0:
        return a
    return gcd(b, a % b);


def nCr(n, k): 
    if(k > n - k): 
        k = n - k 
    res = 1
    for i in range(k): 
        res = res * (n - i) 
        res = res / (i + 1) 
    return int(res) 


def upper_bound(a, x, lo=0):
    hi = len(a)
    while lo < hi:
        mid = (lo+hi)//2
        if a[mid] < x:
            lo = mid+1
        else:
            hi = mid
    return lo


def primefs(n):
    
    primes = {}
    while(n%2 == 0 and n > 0):
        primes[2] = primes.get(2, 0) + 1
        n = n//2
    for i in range(3, int(n**0.5)+2, 2):
        while(n%i == 0 and n > 0):
            primes[i] = primes.get(i, 0) + 1
            n = n//i
    if n > 2:
        primes[n] = primes.get(n, 0) + 1
    
    
    return primes


def power(x, y, p): 
    res = 1
    x = x % p  
    if (x == 0) : 
        return 0
    while (y > 0) : 
        if ((y & 1) == 1) : 
            res = (res * x) % p 
        y = y >> 1      
        x = (x * x) % p 
    return res 


def swap(a,b):
    temp = a
    a = b
    b = temp
    return a,b









def find(x, link):
    p = x;
    while( p != link[p]):
        p = link[p];
    
    while( x != p):
        nex = link[x];
        link[x] = p;
        x = nex;
    return p;




def union(x, y, link, size):
    x = find(x, link)
    y = find(y, link)
    if size[x] < size[y]:
        x,y = swap(x,y)
    if x != y:
        size[x] += size[y]
        link[y] = x


def sieve(n): 
    prime = [True for i in range(n+1)] 
    p = 2
    while (p * p <= n): 
        if (prime[p] == True): 
            for i in range(p * p, n+1, p):
                prime[i] = False
        p += 1
    return prime


MAXN = int(1e7 + 5)
def spf_sieve():
    spf[1] = 1;
    for i in range(2, MAXN):
        spf[i] = i;
    for i in range(4, MAXN, 2):
        spf[i] = 2;
    for i in range(3, ceil(MAXN ** 0.5), 2):
        if spf[i] == i:
            for j in range(i*i, MAXN, i):
                if spf[j] == j:
                    spf[j] = i;
    




def factoriazation(x):
    ret = {};
    while x != 1:
        ret[spf[x]] = ret.get(spf[x], 0) + 1;
        x = x//spf[x]
    return ret
    
    


def int_array():
    return list(map(int, input().strip().split()))

def str_array():
    return input().strip().split();


MOD = int(1e9)+7;
CMOD = 998244353;
INF = float('inf'); NINF = -float('inf');



n, m, k = int_array();
a = int_array(); b = int_array();

for i in range(1, n):
    a[i] += a[i-1];
for i in range(1, m):
    b[i] += b[i-1];

ans = upper_bound(b, k + 0.5);
now = 0;
for i in range(n):
    now = a[i];
    if now <= k:
        x = upper_bound(b, k - now + 0.5);
        ans = max(ans, x + i + 1);
print(ans);

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

b = []
b.append(0)
tmp = 0
for i in range(M):
    b.append(tmp+B[i])
    tmp = tmp+B[i]
a = [0]
for i in range(N):
    a.append(a[i]+A[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):
from collections import deque
import random


"""
def mine(k, a, b):
    bookA = deque(a)
    bookB = deque(b)

    cnt = 0
    r = k
    while r > 0:
        if len(bookA) == 0 and len(bookB) == 0:
            break
        elif len(bookA) > 0 and len(bookB) == 0:
            r -= bookA.popleft()
        elif len(bookA) == 0 and len(bookB) > 0:
            r -= bookB.popleft()
        elif len(bookA) > 0 and len(bookB) > 0:
            if bookA[0] <= bookB[0]:
                r -= bookA.popleft()
            else:
                r -= bookB.popleft()
        if r >= 0:
            cnt += 1
    return cnt
"""



def tsundoku():
    n, m, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    da = [0]
    db = [0]
    for i in range(n):
        da.append(da[i] + a[i])
    for i in range(m):
        db.append(db[i] + b[i])

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

    return print(cnt)


tsundoku()
"""
for t in range(100):
    n = random.randint(1, 200000)
    m = random.randint(1, 200000)
    k = random.randint(1, 1000000000)
    a = [random.randint(1, 1000000000) for i in range(n)]
    b = [random.randint(1, 1000000000) for i in range(m)]

    res1 = mine(k, a, b)
    res2 = answer(n, m, k, a, b)
    if res1 != res2:
        print(res1, end=' ')
        print(res2, end=' \t')
        print(a[0:3], b[0:3])
        print(k)
"""

Solution (Python):
import bisect
import sys

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

a_accumulated = [0] * (n + 1)
b_accumulated = [0] * (m + 1)

for idx, a in enumerate(A):
    a_accumulated[idx + 1] = a_accumulated[idx] + a

for idx, b in enumerate(B):
    b_accumulated[idx + 1] = b_accumulated[idx] + b

a_limit = bisect.bisect(a_accumulated, k)
if a_limit == 0:
    position = bisect.bisect(b_accumulated, k)
    print(position - 1)
    sys.exit(0)

best_num = 0
for i in reversed(range(0, a_limit)):
    position = bisect.bisect_right(b_accumulated, k - a_accumulated[i])
    best_num = max(best_num, i + position - 1)
print(best_num)

Solution (Python):
import bisect
import sys

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

a_accumulated = [0] * (n + 1)
b_accumulated = [0] * (m + 1)

for idx, a in enumerate(A):
    a_accumulated[idx + 1] = a_accumulated[idx] + a 

for idx, b in enumerate(B):
    b_accumulated[idx + 1] = b_accumulated[idx] + b

a_limit = bisect.bisect(a_accumulated, k)
if a_limit == 0:
    position = bisect.bisect(b_accumulated, k)
    print(position - 1)
    sys.exit(0)

best_num = 0
for i in reversed(range(0, a_limit)):
    position = bisect.bisect_right(b_accumulated, k - a_accumulated[i])
    best_num = max(best_num, i + position - 1)
print(best_num)

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):
def main():
    N , M, K = map(int, input().split())
    A_list = list(map(int, input().split()))
    B_list = list(map(int, input().split()))

    al = [0]
    bl = [0]

    for i in range(N) :
        al.append(al[i] + A_list[i])
    for i in range(M) :
        bl.append(bl[i] + B_list[i])
    
    
    book = 0
    re = M

    for i in range(N+1) :
        if al[i] > K :
            break
        for j in bl :
            if bl[re] <= K - al[i] :
                break
            else :
                re -= 1
        
        

        book = max(book, i + re)

    print(book)


if __name__ == '__main__':
    main()

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

    al = [0]
    bl = [0]

    for i in range(N) :
        al.append(al[i] + A_list[i])
    for i in range(M) :
        bl.append(bl[i] + B_list[i])
    
    
    book = 0
    re = M

    for i in range(N+1) :
        if al[i] > K :
            break
        
        
        
        
        
        while bl[re] > K - al[i] :
            re -= 1

        book = max(book, i + re)

    print(book)


if __name__ == '__main__':
    main()

Solution (Python):
def INT():
    return int(input())

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]
    
    if B[i] <= K:
        ans = i

ans = 0

for bk_a in range(N + 1):
    time = A[bk_a]

    if time > K:
        break
        
    l, r = -1, M + 1
    while abs(l - r) > 1:
        bk_b = (l + r) // 2

        if time + B[bk_b] <= K:
            l = bk_b
        else:
            r = bk_b
    
    bk_b = max(0, l)
    ans = max(ans, bk_a + bk_b)

print(ans)

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float("INF")


def solve():
    n, m, k = LI()
    a = LI()
    b = LI()
    a = list(itertools.accumulate(a))
    b = list(itertools.accumulate(b))
    ans = bisect_right(a, k)
    for i, bi in enumerate(b, start=1):
        if bi > k:
            break
        ans = max(ans, i + bisect_right(a, k - bi))
    print(ans)
    return



if __name__ == '__main__':
    solve()

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float("INF")


def solve():
    n, m, k = LI()
    a = LI()
    b = LI()
    a = list(itertools.accumulate(a))
    b = list(itertools.accumulate(b))
    ans = bisect_right(a, k)
    for i, bi in enumerate(b, start=1):
        if bi > k:
            break
        ans = max(ans, i + bisect_right(a, k - bi))
    print(ans)
    return



if __name__ == '__main__':
    solve()

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

A = [0]
B = [0]
ans = 0

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

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

j = m

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

print(ans)

Solution (Python):
import bisect
N,M,K=map(int,input().split())
SumA=[0]*(N+1)
SumB=[0]*(M+1)
A=list(map(int, input().split()))
B=list(map(int,input().split()))
for i in range(1,N+1):
    SumA[i]= A[i-1]+SumA[i-1]
for i in range(1,M+1):
    SumB[i] = B[i-1]+SumB[i-1]

ans = 0
for i in range(N+1):
    num = i
    rest = K-SumA[i]
    if rest<0:
        break;
    num += bisect.bisect_right(SumB, rest)-1
    ans = max(num, ans)

print(ans)

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

for i in range(n):
    if i == 0:
        continue
    else:
        a[i] += a[i-1]

for i in range(m):
    if i == 0:
        continue
    else:
        b[i] += b[i - 1]

a = [0]+a
b = [0]+b

i = 0
j = m
ans = 0

while a[i] <= k:
    while b[j] > k-a[i]:
        j -= 1
        if j<0:
            j=0
            break

    ans =max(ans,i+j)
   
    i += 1
    if i > n:
        break

print(ans)

Solution (Python):
from sys import stdin
import numpy as np
def main():
    
    readline=stdin.readline
    n,m,k=map(int,readline().split())
    a=list(map(int,readline().split()))
    b=list(map(int,readline().split()))

    a=np.array(a,dtype=np.int64).cumsum()
    b=np.array(b,dtype=np.int64).cumsum()

    a=a[a<=k]
    a=k-a

    if a.size>=1:
        res=np.searchsorted(b,a,side="right")
        tmp=np.arange(1,a.size+1,dtype=np.int64)

        print(max((res+tmp).max(),b[b<=k].size))

    else:
        print(b[b<=k].size)

if __name__=="__main__":
    main()

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

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

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

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

A = [0]
B = [0]
ans = 0

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

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

j = m

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

print(ans)

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

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

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

for i in range(1, n + 1):
    a[i] = A[i - 1] + a[i - 1]

for j in range(1, m + 1):
    b[j] = B[j - 1] + b[j - 1]

ans = 0
num_b = m

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

cuma = [0]

for a in A:
  cuma.append(cuma[-1]+a)

cumb = [0]
for b in B:
  cumb.append(cumb[-1]+b)

ans = 0
from bisect import bisect
for i, ca in enumerate(cuma):
  if ca > K: break
  t = K - ca
  j = bisect(cumb, t)
  ans = max(ans, i+j-1)

print(ans)

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

Solution (Python):
n,m,k = list(map(int,input().split()))
a = list(map(int,input().split()))
b = list(map(int,input().split()))
 
import numpy as np
a = [0] + a
b = [0] + b
sa = np.cumsum(a)
sb = np.cumsum(b)
ans = 0
r = m
for i in range(n+1):
  if sa[i] > k:
    break
  while sa[i] + sb[r] > k:
    r -= 1
  ans = max(ans, i+r)

print(ans)

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

most = 0
cur = 0
tot = 0
for i in range(n):
	if tot + a[i] <= k:
		cur += 1
		tot += a[i]
		if cur > most:
			most = cur
	else:
		i -= 1
		break
j = 0
while j < m:
	if tot + b[j] <= k:
		cur += 1
		tot += b[j]
		if cur > most:
			most = cur
		j += 1
	else:
		if i >= 0:
			cur -= 1
			tot -= a[i]
			i -= 1
		else:
			break
print(most)

Solution (Python):
n,m,k = list(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]
ans = 0
r = m
for i in range(n+1):
  if sa[i] > k:
    break
  while sa[i] + sb[r] > k:
    r -= 1
  ans = max(ans, i+r)

print(ans)

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

Solution (Python):
n, m, k = [int(_) for _ in input().split(' ')]

tmp = [int(_) for _ in input().split(' ')]
a = [0]
for i in range(n):
    a.append(a[i] + tmp[i])
tmp = [int(_) for _ in input().split(' ')]
b = [0]
for i in range(m):
    b.append(b[i] + tmp[i])

answer = 0
i2 = m
for i1 in range(n + 1):
    while a[i1] + b[i2] > k and i2 > 0:
        i2 -= 1
    if i1 + i2 >= answer and a[i1] + b[i2] <= k:
        answer = i1 + i2

print(answer)

Solution (Python):
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
book_a = [0] * (n + 1)
book_b = [0] * (m + 1)

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

for j in range(m):
    book_b[j + 1] = book_b[j] + b[j]

r = m
ans = 0

for i in range(n + 1):
    if book_a[i] > k:
        break
    while book_a[i] + book_b[r] > k:
        r -= 1
    ans = max(ans, i + r)

print(ans)

Solution (Python):
from collections import defaultdict as dd
from collections import deque
import bisect
import heapq

def ri():
    return int(input())

def rl():
    return list(map(int, input().split()))


def solve():
    n, m, k = rl()
    A = rl()
    B = rl()
    Ap = [0]
    for a in A:
        nxt = Ap[-1] + a
        if nxt > k:
            break
        Ap.append(nxt)

    best = bisect.bisect_right(Ap, k) - 1
    
    bs = 0
    bcount = 0
    for b in B:
        bs += b
        bcount += 1
        if bs > k:
            break
        diff = k - bs
        acount = bisect.bisect_right(Ap, diff) - 1
        best = max(best, acount + bcount)
    print (best)




mode = 's'

if mode == 'T':
    t = ri()
    for i in range(t):
        solve()
else:
    solve()

Solution (Python):
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
al = [0]*(n+1)
bl = [0]*(m+1)
al[0] = 0
bl[0] = 0

for i in range(1,n+1):
    al[i] = al[i-1]+a[i-1]

for j in range(1,m+1):
    bl[j] = bl[j-1]+b[j-1]

ans = 0
l = m

for m in range(n+1):
    if al[m] > k:
        break

    while bl[l] > k - al[m]:
        l -= 1
           
    ans = max(ans,m+l)


print(ans)

Solution (Python):
import numpy as np
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
 

N, M, K = map(int, readline().split())
AB = np.array(read().split(), np.int32)
A = [0] + np.cumsum(AB[:N]).tolist()
B = [0] + np.cumsum(AB[N:]).tolist()
assert (N == len(A) - 1) and (M == len(B) - 1)
 
ans = 0
left = M
for i in range(len(A)):
    if A[i] > K:
        break
 
    while A[i] + B[left] > K:
        left -= 1
 
    ans = max(ans, left + i)

print(ans)

Solution (Python):
import numpy as np
import sys
read = sys.stdin.read


N, M, K = map(int, input().split())
AB = np.array(read().split(), np.int32)
A = [0] + np.cumsum(AB[:N]).tolist()
B = [0] + np.cumsum(AB[N:]).tolist()
assert (N == len(A) - 1) and (M == len(B) - 1)

ans = 0
left = M
for i in range(len(A)):
    if A[i] > K:
        break

    while A[i] + B[left] > K:
        left -= 1

    ans = max(ans, left + i)

print(ans)

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


A=[0]
B=[0]


for a in At:
    if A[-1]+a<=K:
        A.append(A[-1]+a)
    else:
        break
for b in Bt:
    if B[-1]+b<=K:
        B.append(B[-1]+b)
    else:
        break

def is_ok(arg):
    
    if Ak+B[arg]<=K:
        return True
    else:
        return False


def meguru_bisect(ng, ok):
    '''
    めぐる2分探査
    okはBの冊数たりうる値
    ngはBの冊数たり得ない値を入れる。
    '''

    while (abs(ok - ng) > 1):
        mid = (ok + ng) // 2
        if is_ok(mid):
            ok = mid
        else:
            ng = mid
    return ok


Ak=0

maxBooks=0
for Ai in range(len(A)): 
    Ak=A[Ai]
    Bi=meguru_bisect(len(B),0) 
    if maxBooks<(Ai+Bi):
        maxBooks=Ai+Bi
print( maxBooks )

Solution (Python):

import sys,io,os,time
from collections import defaultdict
from collections import OrderedDict
from collections import deque
from itertools import combinations
from itertools import permutations
import bisect,math,heapq
alphabet = "abcdefghijklmnopqrstuvwxyz"

input = sys.stdin.readline



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



from bisect import bisect_left, bisect_right

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

cumsum_b = [0 for _ in range(m+1)]
for i in range(1,m+1):
    cumsum_b[i] = cumsum_b[i-1] + b[i-1]
cumsum_a = [0 for _ in range(n+1)]
for i in range(1,n+1):
    cumsum_a[i] = cumsum_a[i-1] + a[i-1]


ans = -1
for i in range(n+1):
    res = k-cumsum_a[i]


    if res < 0:
        break
    bl = bisect_right(cumsum_b, res) - 1 


    tmp = i + bisect_right(cumsum_b, res) - 1 
    if ans <= tmp:
        ans = tmp 

print(ans)

Solution (Python):
"""
// Author : snape_here - Susanta Mukherjee
     
 """
 
from __future__ import division, print_function
 
import os,sys
from io import BytesIO, IOBase
 
if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip
 
 
def ii(): return int(input())
def fi(): return float(input())
def si(): return input()
def mi(): return map(int,input().split())
def li(): return list(mi())
 
 
def read():
    sys.stdin = open('input.txt', 'r')  
    sys.stdout = open('output.txt', 'w') 

def isPrime(n):
    if (n <= 1) : return False
    if (n <= 3) : return True
    if (n % 2 == 0 or n % 3 == 0) : return False
    i = 5
    while(i * i <= n) : 
        if (n % i == 0 or n % (i + 2) == 0) : 
            return False
        i = i + 6
    return True

def gcd(x, y):
    while y:
        x, y = y, x % y
    return x

mod=1000000007

from math import *

import bisect

from decimal import *

getcontext().prec = 50

abc="abcdefghijklmnopqrstuvwxyz"

pi=3.141592653589793238

def main():

    n,m,k=mi()
    a=li()
    b=li()
    p1=[0]
    p2=[0]
    for i in range(n):
        p1.append(p1[i]+a[i])
    for i in range(m):
        p2.append(p2[i]+b[i])
    ans=0
    j=m 
    for i in range(n+1):
        if p1[i]>k:
            break
        while p1[i]+p2[j]>k:
            j-=1 
        ans=max(ans,i+j)
    print(ans)



 
BUFSIZE = 8192
 
 
class FastIO(IOBase):
    newlines = 0
 
    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None
 
    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()
 
    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()
 
    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)
 
 
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")
 
 
def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()
 
 
if sys.version_info[0] < 3:
    sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
    sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
 
input = lambda: sys.stdin.readline().rstrip("\r\n")
 

 
 
if __name__ == "__main__":
    
    main()

Solution (Python):

import bisect

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


cum_sum_A = [0]
cum_sum_B = [0]

for i in range(len(A_list)):
    cum_sum_A.append( cum_sum_A[i] + A_list[i] )

for i in range(len(B_list)):
    cum_sum_B.append( cum_sum_B[i] + B_list[i] )


max_num_book = 0

for i_a in range(len(cum_sum_A)):
    if cum_sum_A[i_a] > K:
        break
    
    remain = K - cum_sum_A[i_a]
    i_b = bisect.bisect_right(cum_sum_B, remain)
    
    num_book = i_a + (i_b - 1)
    max_num_book = max(max_num_book, num_book)


print(max_num_book)

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

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):
A, B, C = map(int, input().split())
D = list(map(int, input().split()))
E = list(map(int, input().split()))

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

ans, j = 0, B
for i in range(A + 1):
    if a[i] > C:
        break

    while b[j] > C - a[i]:
        j -= 1
    ans = max(ans, i + j)
print(ans)

Solution (Python):
from sys import stdin

n, m, k = map(int, stdin.readline().rstrip().split())

a = list(map(int, stdin.readline().rstrip().split()))
b = list(map(int, stdin.readline().rstrip().split()))

A,B = [0],[0]

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

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

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

print(ans)

Solution (Python):
from sys import stdin

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

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

A,B = [0],[0]

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

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

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

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

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

ans,j=0,m
for i in range(n+1):
	if a_pre[i]>k:
		break
	while b_pre[j]>k-a_pre[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, ans = sum(B), 0
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):
from bisect import bisect_right
n,m,k = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))

cA = [0]*(n+1)
cB = [0]*(m+1)

for i in range(n):
  cA[i+1] = cA[i]+A[i]
  
for i in range(m):
  cB[i+1] = cB[i]+B[i]
  
ans = bisect_right(cA,k) - 1 
ai = ans
bi = 0
while ai>=0 :
  r = k - cA[ai]
  tmp = bisect_right(cB,r) - 1 
  ans = max(ans,tmp+ai)
  ai -= 1
  
print(ans)