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 = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]

sumT = 0
cnt = 0
cntL = []
cntMax = 0
AMax = 0
BMax = 0

if (A[0]>K and B[0]>K):
    print(0)
    exit()

for i in range(len(A)):
    sumT += A[i]
    cnt += 1
    if (sumT > K):
        sumT -= A[i]
        cnt -= 1
        break

AMax = cnt-1
cntL.append(cnt)


for i in range(len(B)):
    sumT += B[i]
    cnt += 1
    if (sumT<=K):
        cntL.append(cnt)
    while (sumT > K):
        if (AMax<0):
            break
        sumT -= A[AMax]
        AMax -= 1
        cnt -= 1

print(max(cntL))

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 numpy as np
import math
import collections
import bisect
from difflib import SequenceMatcher


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

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

    cumsumA = np.cumsum(A)
    p = bisect.bisect_left(cumsumA, K + 0.1)
    time = cumsumA[p - 1]
    tmp = p
    ans = tmp

    cntA = 0
    import numpy as np


import math
import collections
import bisect
from difflib import SequenceMatcher


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

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

    cumsumA = np.cumsum(A)
    a = bisect.bisect_left(cumsumA, K + 0.1) - 1
    time = cumsumA[a]
    tmp = a + 1
    ans = tmp

    b = 0
    while(b < len(B)):
        
        if time + B[b] <= K:
            time += B[b]
            tmp += 1
            b += 1
            if tmp > ans:
                ans = tmp
        else:
            if a < 0:
                break
            time -= A[a]
            tmp += -1
            a += -1

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
suma=[0]*(n+1)
suma[1]=a[0]
acnt=1
for i in range(2,n+1):
  if suma[i-1] > k:
    break
  suma[i] = suma[i-1]+a[i-1]
  acnt+= 1

btt=0
btl=0
res=0
ans=0
for i in reversed(range(acnt+1)):
  res=suma[i]+btt
  if res > k:
    continue
  for j in range(btl,m):
    if res+b[j] <= k:
      res += b[j]
      btt += b[j]
      btl += 1
    else:
      break
  ans = max(ans, i+btl)
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]
b=[0]

tmp_a=0
for i in A:
    tmp_a+=i
    a.append(tmp_a)
    
tmp_b=0
for i in B:
    tmp_b+=i
    b.append(tmp_b)
    


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

Solution (Python):
from itertools import accumulate

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 = [0] + list((accumulate(b)))

ans = 0
bn = m

for an in range(n + 1):
    rem = k - a_acc[an]
    if rem < 0:
        break

    
    while b_acc[bn] > rem:
        bn -= 1
    ans = max(ans, an + bn)

print(ans)

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):
from itertools import accumulate

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 = [0] + list(accumulate(b))  

ans = 0
for an in range(n + 1):
    rem = k - a_acc[an]
    if rem < 0:
        break

    
    ok = 0
    ng = m + 1   
    
    while abs(ng - ok) > 1:
        mid = (ok + ng) // 2
        
        if b_acc[mid] <= rem:
        
            ok = mid
        else:
        
            ng = mid

    bn = ok
    ans = max(ans, an + bn)

print(ans)

Solution (Python):
from itertools import accumulate

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 = [0] + list((accumulate(b)))

ans = 0
bn = m

for an in range(n + 1):
    rem = k - a_acc[an]
    if rem < 0:
        break

    
    while b_acc[bn] > rem:
        bn -= 1
    ans = max(ans, an + bn)

print(ans)

Solution (Python):
from itertools import accumulate

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 = [0] + list((accumulate(b)))  

ans = 0
for an in range(n + 1):
    rem = k - a_acc[an]
    if rem < 0:
        break

    
    ok = 0
    ng = m + 1   
    
    while abs(ng - ok) > 1:
        mid = (ok + ng) // 2
        
        if b_acc[mid] <= rem:
        
            ok = mid
        else:
        
            ng = mid

    bn = ok
    ans = max(ans, an + bn)

print(ans)

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

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 numpy as np
N,M,K = map(int, input().split())

A = np.array([0] + list(map(int, input().split())))
B = np.array([0] + list(map(int, input().split())))

CA = np.cumsum(A)
CB = np.cumsum(B)

ans = 0
for i in range(N+1):
  if CA[i] > K:
    break
  j = np.searchsorted(CB, K-CA[i], side='right') - 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()))
Ncan = 0
Ktmp = 0
for i in range(N):
    if(Ktmp + A[i] > K):
        break
    Ktmp += A[i]
    Ncan += 1

Mcan = 0
for i in range(M):
    if(Ktmp + B[i] > K):
        break
    Ktmp += B[i]
    Mcan += 1

ans = 0

while(1):
    while(1):
        if(Mcan >= M):
            break
        if(Ktmp + B[Mcan] > K):
            break
        Ktmp += B[Mcan]
        Mcan += 1
    anstmp = Ncan + Mcan
    ans = max(ans, anstmp)

    Ncan -= 1
    if(Ncan == -1):
        break
    Ktmp -= A[Ncan]

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] + a
b = [0] + b

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):
    while j > 0 and b[j] + a[i] > k:
        j -= 1
    if a[i] + b[j] > k:
        break

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

a, b = [0], [0]

for i in range(len(A)):
    a.append(a[i] + A[i])
for i in range(len(B)):
    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()))
 
asum=[0]
bsum=[0]
for i in range(n):
    asum.append(asum[i]+a[i]) 
for i in range(m):
    bsum.append(bsum[i]+b[i]) 
 
t=0
for i in range(n+1):
    if asum[i]>k: 
        break
    now=k-asum[i] 
    while bsum[m]>now:
        m-=1 
    if t<i+m:
        t=i+m 
        
print(t)

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

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

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

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

Solution (Python):
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,len(A)):
    A[i]+=A[i-1]
for i in range(1,len(B)):
    B[i]+=B[i-1]
honn=a=bisect.bisect(B,K)
for i in range(len(A)):
    s=K-A[i]
    if s>=0:
        a=bisect.bisect(B,s)
        if i+1+a>honn:
            honn=i+1+a
print(honn)

Solution (Python):
n_book_a, n_book_b, limit_time = map(int, input().split(" "))
read_time_a = [int(i) for i in input().split(" ")]
read_time_b = [int(i) for i in input().split(" ")]

read_sum_time_a = [0]  
for i in range(n_book_a):
    read_sum_time_a.append(read_sum_time_a[i] + read_time_a[i])
    
read_sum_time_b = [0]  
for i in range(n_book_b):
    read_sum_time_b.append(read_sum_time_b[i] + read_time_b[i])




ans = 0
i_b = n_book_b  
for i_a, time_a in enumerate(read_sum_time_a):
    
    
    if time_a > limit_time:
        break
    else:     
        while read_sum_time_b[i_b] + time_a > limit_time:
            i_b += -1
            
        ans = max(ans, i_a + i_b)
        
    
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)
Asum[0] = 0
Bsum[0] = 0
for i in range(N):
    Asum[i+1] = Asum[i]+A[i]
for i in range(M):
    Bsum[i+1] = Bsum[i]+B[i]
    
bestbooks = 0
upup = M+1
for i in range(N+1):
    lasttime = K - Asum[i]
    if lasttime < 0:
        break
    for j in reversed(range(0,upup)):
        
        if Bsum[j] <= lasttime and i+j>bestbooks:
            bestbooks = i+j
           
        if Bsum[j] <= lasttime:
            upup = j+1
            break
            
print(bestbooks)

Solution (Python):
import itertools
import bisect
N, M ,K = map(int,input().split())
al = list(map(int,input().split()))
bl = list(map(int,input().split()))

accum_al = list(itertools.accumulate(al))
accum_bl = list(itertools.accumulate(bl))
accum_al.insert(0,0)
accum_bl.insert(0,0)



ans = 0
index = 0
for i in range(N+1):
    nokori = K - accum_al[i]
    if nokori < 0:
        continue
    else:
        index = bisect.bisect_right(accum_bl,nokori)-1
    tmp = i + index
    
    if tmp > ans:
        ans = tmp
print(ans)

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

arui = [0]
brui = [0]
for i in range(n):
    arui.append(arui[-1]+a[i])
for i in range(m):
    brui.append(brui[-1]+b[i])

left = 0
right = n+m+1
while left+1 < right:
    mid = (left + right) // 2
    if mid <= n:
        aidx = mid
        bidx = 0
    else:
        aidx = n
        bidx = mid - n
    ok = 0
    while aidx >= 0 and bidx <= m:
        time = arui[aidx] + brui[bidx]
        if time <= k:
            ok = 1
            break
        aidx -= 1
        bidx += 1
    if ok:
        left = mid
    else:
        right = mid

print(left)

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

asum=[0]
bsum=[0]
cnt=0
for i in range(n):
    cnt+=a[i]
    asum.append(cnt)






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






ans=0
j=m
for i in range(n+1):
    c=k-asum[i]


    if c<0:
        continue
    while c<bsum[j] and j>0:
        j-=1
    ans=max(ans,i+j)

print(ans)

Solution (Python):

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

a=map(int,input().split(" "))

b=map(int,input().split(" "))

t=0
i=0
la=[]
for x in a :
    if t+x>k :
        break
    t+=x
    i+=1
    la.append(x)












j=0
for x in b :
    
    if t+x>k :
        break
    t+=x
    j+=1

ans=i+j
while i>0 :
    i-=1
    t-=la[-1]
    la.pop(-1)
    if t+x>k :
        continue
    else:
        t+=x
        j+=1
    for x in b :
        
        if t+x>k :
            break
        t+=x
        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=map(int,input().split(" "))

t=0
i=0
while i<n :
    if t+a[i]>k :
        break
    t+=a[i]
    i+=1







j=0
for x in b :
    
    if t+x>k :
        break
    t+=x
    j+=1

ans=i+j
while i>0 :
    i-=1
    t-=a[i]
    if t+x>k :
        continue
    else:
        t+=x
        j+=1
    for x in b :
        
        if t+x>k :
            break
        t+=x
        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
i=0
while i<n :
    if t+a[i]>k :
        break
    t+=a[i]
    i+=1

j=0
while j<m :
    if t+b[j]>k :
        break
    t+=b[j]
    j+=1

ans=i+j
while i>0 :
    i-=1
    t-=a[i]
    while j<m and t+b[j]<=k :
        t+=b[j]
        j+=1
    ans=max(ans,i+j)

print(ans)

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

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

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

a, b = [0], [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])

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 = input().split()
A = input().split()
B = input().split()
ta = 0
cnta = 0
while cnta < int(N) and ta + int(A[cnta]) <= int(K):
    ta += int(A[cnta])
    cnta+=1

tb = 0
cntb = 0
while cntb < int(M) and ta + tb + int(B[cntb]) <= int(K):
    tb += int(B[cntb])
    cntb += 1
    
ans = cnta + cntb

while cnta - 1 >= 0:
    ta -= int(A[cnta - 1])
    cnta -= 1
    while cntb < int(M) and ta + tb + int(B[cntb]) <= int(K):
        tb += int(B[cntb])
        cntb += 1
    ans = max(ans, cnta + cntb)

print(ans)

Solution (Python):
n,m,k=input().split()
a=input().split()
b=input().split()
ta=0
cnta=0
while int(cnta)<int(n) and ta+int(a[cnta])<=int(k) :
    ta= ta+ int(a[cnta])
    cnta=cnta+1
tb=0
cntb=0
while cntb<int(m) and ta+tb+int(b[cntb])<=int(k):
    tb = tb+ int(b[cntb])
    cntb=cntb+1 
ans=cnta+cntb

while cnta-1>=0:
    ta=ta-int(a[cnta-1])
    cnta=cnta-1
    while cntb<int(m) and ta+tb+int(b[cntb])<=int(k):
        tb= tb+ int(b[cntb])
        cntb=cntb+1
    ans=max(ans,cnta+cntb)
print(ans)

Solution (Python):

n,m,k=map(int,input().split(" "))
a=[-9]
b=list(a)
a.extend(map(int,input().split(" ")))
b.extend(map(int,input().split(" ")))

t=0
i=1
while i<=n :
    
    if t+a[i]>k :
        break
    t+=a[i]
    
    i+=1
i-=1

j=1
while j<=m :
    if t+b[j]>k :
        break
    t+=b[j]
    j+=1
j-=1

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

print(ans)

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

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

sum_a = [0]
for i in range(1,n+1):
    sum_a.append(sum_a[i-1] + a[i])

sum_b = [0]
for i in range(1,m+1):
    sum_b.append(sum_b[i-1] + b[i])

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

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

a, b = [0], [0]


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

ans = 0

j = M

for i in range(N + 1):
    
    if a[i] > K:
        break

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

Solution (Python):

import sys
import numpy as np

input = sys.stdin.readline


def S():
    return input().rstrip()


def I():
    return int(input())


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


n, m, k = MI()
a = np.array(list(MI()))
b = np.array(list(MI()))

cuma = np.cumsum(a)
cuma = cuma[cuma <= k]
cumb = np.cumsum(b)
cumb = cumb[cumb <= k]
lena = len(cuma)
lenb = len(cumb)

a = a[lena - 1 :: -1]
b = b[:lenb]
ab = np.concatenate([a, b], axis=0)
n = len(ab)

ans = lena
left = 0
right = lena
sum = a.sum()
for left in range(n):
    while right < n and sum + ab[right] <= k:
        sum += ab[right]
        right += 1

    ans = max(ans, right - left)

    if left == right:
        right += 1
    else:
        sum -= ab[left]

print(ans)

Solution (Python):
import sys
import itertools as it

input = sys.stdin.readline

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

AS = list(it.accumulate([0]+A))
BS = list(it.accumulate([0]+B))
max_value = 0
for i in range(N+1):
    if AS[i] > K:
        continue
    ok, ng = 0, M+1
    while ng - ok > 1:
        mid = (ok + ng) // 2
        time = AS[i] + BS[mid]
        if time <= K:
            ok = mid
        else:
            ng = mid
    num = i + ok
    if num > max_value:
        max_value = num
print(max_value)

Solution (Python):


def resolve():
    import sys
    input = sys.stdin.readline
    nmk = [int(x) for x in input().rstrip().split(" ")]
    n = nmk[0]
    m = nmk[1]
    k = nmk[2]
    a_list = [int(x) for x in input().rstrip().split(" ")]
    b_list = [int(x) for x in input().rstrip().split(" ")]

    from collections import deque
    qa = deque()
    qb = deque()
    total_min = 0
    for i in range(len(a_list)):
        if a_list[i] + total_min <= k:
            qa.append(a_list[i])
            total_min += a_list[i]
        else:
            break
    

    
    max_book = len(qa)

    for i in range(len(qb), len(b_list)):
        if b_list[i] + total_min <= k:
            qb.append(b_list[i])
            total_min += b_list[i]
        else:
            break
    if len(qa) + len(qb) > max_book:
        max_book = len(qa) + len(qb)

    while len(qa) > 0:
        poped = qa.pop()
        total_min -= poped
    
        for i in range(len(qb), len(b_list)):
            if b_list[i] + total_min <= k:
                qb.append(b_list[i])
                total_min += b_list[i]
            else:
                break
        if len(qa) + len(qb) > max_book:
            max_book = len(qa) + len(qb)
    
    print(max_book)
        

if __name__ == "__main__":
    resolve()

Solution (Python):
import bisect


def main():
    N, M, K = map(int, input().split())
    A = [int(v) for v in input().split()]
    B = [int(v) for v in input().split()]
    ruisekiA = [0 for _ in range(N+1)]
    ruisekiB = [0 for _ in range(M+1)]
    for i in range(N):
        ruisekiA[i + 1] = ruisekiA[i] + A[i]
    for i in range(M):
        ruisekiB[i + 1] = ruisekiB[i] + B[i]

    ans = 0
    
    for i in range(N+1):
        tans = i
        tk = K - ruisekiA[i]
        if tk < 0:
            break
        idx = bisect.bisect_left(ruisekiB, tk)
        if M+1 == idx or ruisekiB[idx] != tk:
            idx -= 1
        tans += idx
        ans = max(ans, tans)
    print(ans)


if __name__ == '__main__':
    main()

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

idxA = 0
for a in A:
    Atime.append(a+Atime[idxA])
    idxA += 1

idxB = 0
for b in B:
    Btime.append(b+Btime[idxB])
    idxB += 1

ans,j = 0,M
for i in range(N+1):
    if Atime[i] > K:
        break
    while Btime[j] > K - Atime[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()))
Atime = [0]
Btime = [0]

idxA = 0
for a in A:
    Atime.append(a+Atime[idxA])
    idxA += 1

idxB = 0
for b in B:
    Btime.append(b+Btime[idxB])
    idxB += 1

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

print(ans)

Solution (Python):
from itertools import zip_longest, count, accumulate
from bisect import bisect, bisect_left
 
def LIST(): return list(map(int, input().split()))
 
n, m, k = map(int, input().split())
a = [0] + list(accumulate(LIST()))
b = list(accumulate(LIST()))
 
num = 0
 
for i, a in enumerate(a):
    time_rest = k - a
    
    if time_rest < 0:
        continue
    
    num = max(num, i + bisect(b, time_rest))
 
print(num)

Solution (Python):
import bisect

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

DP_1 = [0]
DP_2 = [0]
Max = 0

Sum = 0
for i in range(len(A)):
    Sum += A[i]
    if Sum <= K:
        DP_1.append(Sum)
    else:
        break

Sum = 0
for i in range(len(B)):
    Sum += B[i]
    if Sum <= K:
        DP_2.append(Sum)
    else:
        break

for i in range(len(DP_1)):

    j = bisect.bisect_left(DP_2,K-DP_1[i]+1) - 1

    a = i + j

    Max = max(Max,a)
    


print(Max)

Solution (Python):
import sys
import heapq
sys.setrecursionlimit(10 ** 8)

def Z(): return int(input())
def ZZ(): return [int(_) for _ in input().split()]

def main():
    N, M, K = ZZ()
    A = ZZ()
    B = ZZ()
    k = 0
    for i in range(N):
        if k + A[i] <= K: k += A[i]
        else: break
    else: i += 1
    ans = i
    for j in range(M):
        if k + B[j] <= K: k += B[j]
        else: break
    else: j += 1
    ans += j
    for ii in range(1, i+1):
        k -= A[i-ii]
        while j < M and k + B[j] <= K:
            k += B[j]
            j += 1
        ans = max(ans, i+j-ii)
    print(ans)

    return

if __name__ == '__main__':
    main()

Solution (Python):
import numpy as np
import sys
from collections import defaultdict
readline = sys.stdin.buffer.readline



def geta(fn=lambda s: s.decode()):
    return map(fn, readline().split())


def gete(fn=lambda s: s.decode()):
    return fn(readline().rstrip())


def main():
    n, m, k = geta(int)
    ca = np.array([0] + list(geta(int))).cumsum()
    cb = np.array([0] + list(geta(int))).cumsum()

    ans = 0
    for i in range(n+1):
        if ca[i] > k:
            break
        j = np.searchsorted(cb, k-ca[i], side="right") - 1
        if ans < i + j:
            ans = i + j

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):


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

    Asum, Bsum = [0], [0]

    for i in range(N):
        Asum.append(Asum[i] + A[i])
    for i in range(M):
        Bsum.append(Bsum[i] + B[i])

    ans, j = 0, M

    for i in range(N + 1):
        if K < Asum[i]:
            break
        while Bsum[j] > K - Asum[i]:
            j -= 1
        ans = max(ans, i + j)
    print(ans)


if __name__ == "__main__":
    main()

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

aa = [0]
bb = [0]

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

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

Solution (Python):
from bisect import *

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

a_b=[0]
for i in range(1,n+1):
  a_b.append(a_b[i-1]+a[i-1])

b_b=[0]
for i in range(1,m+1):
  b_b.append(b_b[i-1]+b[i-1])

ans=0
for i in range(n+1):
  t_ans=i
  if a_b[i]>k:
    break
  t_ans+=bisect_right(b_b,k-a_b[i])-1
  ans=max(ans,t_ans)

print(ans)

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

sum_A = [0]
for  i in range(N):
    sum_A.append(sum_A[i] + A[i])

sum_B = [0]
for  i in range(M):
    sum_B.append(sum_B[i] + B[i])

ans = 0
j = M

for i in range (N+1):
    if sum_A[i] > K:
        break
    while sum_B[j] > K - sum_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=map(int, input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))


cumA = [0]*(n+1)
for i in range(n):
    cumA[i+1] = cumA[i] + A[i]

cumB = [0]*(m+1)
for i in range(m):
    cumB[i+1] = cumB[i] + B[i]

ans = 0
j = m

for i in range(n+1):
    if cumA[i]>k: 
        break
    while cumB[j] > k-cumA[i] and j>0: 
        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()))

check_A = [0]
check_B = [0]

for a in range(N):
    check_A.append(check_A[a] + A[a])

for b in range(M):
    check_B.append(check_B[b] + B[b])

ans = 0
j = M

for i in range(N+1):
    if check_A[i] > K:
        break
    while check_B[j] > K - check_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]
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
    while B[m] > k-A[i]:
        m -= 1
    ans = max(ans,i+m)
print(ans)

Solution (Python):
import bisect

N, M, K = input().split()

desk1 = list(map(int, input().split()))
desk2 = list(map(int, input().split()))

max_time = int(K)
item1 = [0]
item2 = [0]
for i in desk1:
    item1.append(item1[-1] + i)
for i in desk2:
    item2.append(item2[-1] + i)

ret = 0
for i, t in enumerate(item1):
    remain_time = max_time - t
    if remain_time >= 0:
        j = bisect.bisect(item2, remain_time) - 1
        ret = max(ret, i + j)

print(ret)