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

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

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



arr1 = [0] * (n + 1)
arr2 = [0] * (m + 1)
for i in range(0, n):
    arr1[i + 1] = arr1[i] + a[i]
for i in range(0, m):
    arr2[i + 1] = arr2[i] + b[i]




ans = 0

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


print(ans)

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

Solution (Python):
from itertools import accumulate
from bisect import bisect

n, m, k = map(int, input().split())
a = accumulate(map(int, input().split()), initial=0)
b = [*accumulate(map(int, input().split()))]
print(max(i + bisect(b, k - x) for i, x in enumerate(a) if x <= k))

Solution (Python):
from itertools import accumulate
from bisect import bisect

n, m, k = map(int, input().split())
a = [*accumulate(map(int, input().split()), initial=0)]
b = [*accumulate(map(int, input().split()), initial=0)]
print(max(i - 1 + bisect(b, k - x) for i, x in enumerate(a) if x <= k))

Solution (Python):


N,M,K = map(int, input().split())
Nnums = list(map(int, input().split()))
Mnums = list(map(int, input().split()))

Nmax=len(Nnums)
Mmax=len(Mnums)
Nsum=0
Ncnt=0


for Ni,Nl in enumerate(Nnums):
  Nsum += Nl
  Ncnt = Ni
  if Nsum > K:
    Nsum -= Nl
    Ncnt = Ni-1
    break
    
chkSum = Nsum
chkCnt = Ncnt + 1
AnsCnt = Ncnt + 1


for Mi,Ml in enumerate(Mnums):
  chkSum += Ml
  chkCnt += 1
  if (chkSum > K):
    flg = 0
    while chkSum > K:
      if Ncnt >= 0:
        chkSum -= Nnums[Ncnt]
        chkCnt -= 1
        Ncnt -= 1
        flg = 1
      else:
        if flg == 0:
          
          chkCnt -= 1
          
        break
  
  if (AnsCnt < chkCnt):
    AnsCnt = chkCnt
  

  

  if (chkSum > K):
    break
    
print(AnsCnt)

Solution (Python):
import numpy as np
import sys
N, M, K = [int(x) for x in input().split(" ")]
A = [0] + [int(x) for x in input().split(" ")]
B = [0] + [int(x) for x in input().split(" ")]

Acum = np.cumsum(A)
Bcum = np.cumsum(B)


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

Solution (Python):
from itertools import accumulate
from bisect import bisect

n, m, k = map(int, input().split())
a = [*map(int, input().split())]
b = [*map(int, input().split())]
ca = [*accumulate(a, initial=0)]
cb = [*accumulate(b, initial=0)]
print(max(i - 1 + bisect(cb, k - x) for i, x in enumerate(ca) if x <= k))

Solution (Python):


import io
import sys
import math


def solve(n,m,k,a_lst,b_lst):
    
    aa_lst = make_sum_list(a_lst,k)
    bb_lst = make_sum_list(b_lst,k)
    len_aa = len(aa_lst)
    len_bb = len(bb_lst)

    
    if len_aa == 0:   return len_bb
    elif len_bb == 0: return len_aa
    
    
    if aa_lst[-1] + bb_lst[-1] <= k: return len_aa + len_bb 
    
    
    max_read = max(len_aa,len_bb)

    
    for i in reversed(range(len_aa)):
        limit = k - aa_lst[i]
        read = i + 1
        if read + len_bb + 1 <= max_read:
            
            break

        imax = len_bb-1
        imin = 0
        imid = len_bb // 2
        while True:
            if _DEB: logd(f"imid: {imid}")

            if bb_lst[imid] <= limit:
                
                if imax - imid <= 1:
                    
                    if bb_lst[imax] <= limit:
                        read += imax + 1
                    else:
                        read += imid + 1
                    break
                imin = imid
            else:
                
                if imid - imin <= 1:
                    
                    if bb_lst[imin] <= limit:
                        read += imin + 1
                    break
                imax = imid
            imid = (imin + imax) // 2
        max_read = max(read,max_read)
    return max_read

def make_sum_list(lst,max):
    sum_lst = []
    sum = 0
    for n in lst:
        sum += n
        if sum > max:
            break
        sum_lst.append(sum)
    return sum_lst

def main():
    n,m,k = map(int, input().split())
    a_lst = list(map(int, input().split()))
    b_lst = list(map(int, input().split()))
    ans = solve(n,m,k,a_lst,b_lst)
    print(ans)


_INPUT = """\
3 4 240
60 90 120
80 150 80 150
"""
_DEB = 0

def logd(str):
    if _DEB:
        print(f"[deb] {str}")


if __name__ == "__main__":
    if _DEB:
        sys.stdin = io.StringIO(_INPUT)
        print("!! Debug Mode !!")

    main()

Solution (Python):


import io
import sys
import math


def solve(n,m,k,a_lst,b_lst):
    
    aa_lst = make_sum_list(a_lst,k)
    bb_lst = make_sum_list(b_lst,k)
    len_aa = len(aa_lst)
    len_bb = len(bb_lst)

    
    if len_aa == 0:   return len_bb
    elif len_bb == 0: return len_aa
    
    
    if aa_lst[-1] + bb_lst[-1] <= k: return len_aa + len_bb 
    
    
    max_read = max(len_aa,len_bb)

    
    for i in reversed(range(len_aa)):
        limit = k - aa_lst[i]
        read = i + 1
        if read + len_bb + 1 <= max_read:
            
            break

        imax = len_bb-1
        imin = 0
        imid = len_bb // 2
        while True:
            logd(f"imid: {imid}")

            if bb_lst[imid] <= limit:
                
                if imax - imid <= 1:
                    
                    if bb_lst[imax] <= limit:
                        read += imax + 1
                    else:
                        read += imid + 1
                    break
                imin = imid
            else:
                
                if imid - imin <= 1:
                    
                    if bb_lst[imin] <= limit:
                        read += imin + 1
                    break
                imax = imid
            imid = (imin + imax) // 2
        max_read = max(read,max_read)
    return max_read

def make_sum_list(lst,max):
    sum_lst = []
    sum = 0
    for n in lst:
        sum += n
        if sum > max:
            break
        sum_lst.append(sum)
    return sum_lst

def main():
    n,m,k = map(int, input().split())
    a_lst = list(map(int, input().split()))
    b_lst = list(map(int, input().split()))
    ans = solve(n,m,k,a_lst,b_lst)
    print(ans)


_INPUT = """\
3 4 240
60 90 120
80 150 80 150
"""
_DEBUG = 0

def logd(str):
    if _DEBUG:
        print(f"[deb] {str}")


if __name__ == "__main__":
    if _DEBUG:
        sys.stdin = io.StringIO(_INPUT)
        print("!! Debug Mode !!")

    main()

Solution (Python):
from bisect import bisect_right
from sys import stdin, stdout

int_in = lambda: int(stdin.readline())
arr_in = lambda: [int(x) for x in stdin.readline().split()]
mat_in = lambda rows: [arr_in() for _ in range(rows)]
str_in = lambda: stdin.readline().strip()
out = lambda o: stdout.write("{}\n".format(o))
arr_out = lambda o: out(" ".join(map(str, o)))
bool_out = lambda o: out("YES" if o else "NO")
tests = lambda: range(1, int_in() + 1)
case_out = lambda i, o: out("Case 


def solve(n, m, k, a, b):
    prefix_sum_a = [0]
    for i in range(len(a)):
        prefix_sum_a.append(a[i] + prefix_sum_a[-1])
    prefix_sum_b = [b[0]]
    for i in range(1, len(b)):
        prefix_sum_b.append(b[i] + prefix_sum_b[-1])

    pa = bisect_right(prefix_sum_a, k) - 1
    result = 0
    while pa >= 0:
        left = k - prefix_sum_a[pa]
        pb = bisect_right(prefix_sum_b, left)
        result = max(result, pa + pb)
        pa -= 1
    return result


if __name__ == "__main__":
    n, m, k = arr_in()
    a = arr_in()
    b = arr_in()
    out(solve(n, m, k, a, b))

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

sumA=sum(arr)
bpointer=0
ans=0
sumB=0
for i in range(len(arr),-1,-1):
    if i<n:
        sumA-=arr[i]
    if sumA<=k:
        while bpointer<m and sumA+sumB+brr[bpointer]<=k:
            sumB+=brr[bpointer]
            bpointer+=1
        ans=max(ans,i+bpointer)
print(ans)

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

stackA = [int(x) for x in input().split()]
stackB = [int(x) for x in input().split()]

indexB = 0
indexA = 0

books = 0
mostBooks = 0
time = 0

for x in stackB:
    if time + x > K:
        break

    else:
        time += x
        indexB += 1
        books += 1

indexB -= 1

mostBooks = books

for x in stackA:
    time += x
    books += 1

    while time > K and indexB >= 0:
        time -= stackB[indexB]
        indexB -= 1
        books -= 1

    if indexB < 0 and time > K:
        break

    mostBooks = max(mostBooks, books)

print(mostBooks)

Solution (Python):
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()]
ReadA = [0]*(N+1)
ReadB = [0]*(M+1)
for TA in range(0,N):
    ReadA[TA+1] = ReadA[TA]+A[TA]
for TB in range(0,M):
    ReadB[TB+1] = ReadB[TB]+B[TB]
MAX = 0
MAXTB = M
for TA in range(0,N+1):
    if ReadA[TA]>K:
        break
    else:
        for TB in range(MAXTB,-1,-1):
            if ReadA[TA]+ReadB[TB]<=K:
                MAXTB = TB
                if MAX<TA+TB:
                    MAX = TA+TB
                break
print(MAX)

Solution (Python):


import io
import sys
import math


def solve(n,m,k,a_lst,b_lst):
    
    aa_lst = make_sum_list(a_lst,k)
    bb_lst = make_sum_list(b_lst,k)
    len_aa = len(aa_lst)
    len_bb = len(bb_lst)

    
    if len_aa == 0:   return len_bb
    elif len_bb == 0: return len_aa
    
    
    if aa_lst[-1] + bb_lst[-1] <= k: return len_aa + len_bb 
    
    
    max_read = max(len_aa,len_bb)

    
    for i in reversed(range(len_aa)):
        limit = k - aa_lst[i]
        read = i + 1
        if read + len_bb + 1 <= max_read:
            
            break

        imax = len_bb-1
        imin = 0
        imid = len_bb // 2
        while True:
            if bb_lst[imid] <= limit:
                
                if imax - imid <= 1:
                    
                    if bb_lst[imax] <= limit:
                        read += imax + 1
                    else:
                        read += imid + 1
                    break
                imin = imid
            else:
                
                if imid - imin <= 1:
                    
                    if bb_lst[imin] <= limit:
                        read += imin + 1
                    break
                imax = imid
            imid = (imin + imax) // 2
        max_read = max(read,max_read)
    return max_read

def make_sum_list(lst,max):
    sum_lst = []
    sum = 0
    for n in lst:
        sum += n
        if sum > max:
            break
        sum_lst.append(sum)
    return sum_lst

def main():
    n,m,k = map(int, input().split())
    a_lst = list(map(int, input().split()))
    b_lst = list(map(int, input().split()))
    ans = solve(n,m,k,a_lst,b_lst)
    print(ans)


_INPUT = """\
3 4 730
60 90 120
80 150 80 150
"""
_DEBUG = 0

if __name__ == "__main__":
    if _DEBUG:
        sys.stdin = io.StringIO(_INPUT)
        print("!! Debug Mode !!")
    main()

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

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

def judge(x):
    for i in range(x+1):
        if i<=N and x-i<=M and accA[i]+accB[x-i]<=K:
            return True
    
    return False

def binary_search():
    l, r = 0, N+M
    
    while l<=r:
        m = (l+r)//2
        
        if judge(m):
            l = m+1
        else:
            r = m-1
    
    return r

N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
accA = [0]

for Ai in A:
    accA.append(accA[-1]+Ai)

accB = [0]

for Bi in B:
    accB.append(accB[-1]+Bi)

print(binary_search())

Solution (Python):
import itertools
import bisect

N,M,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
B = B + [10**10]

Ac = list(itertools.accumulate(A))

iA = bisect.bisect_right(Ac,K)

A_ok = Ac[:iA][::-1]+[0]
Alen = len(A_ok)

ans = 0
SUM = 0
cnt = 0
for i in range(Alen):
    K2 = K - A_ok[i]
    k = cnt
    for j in range(M-k):
        if SUM + B[j+k] <= K2:
            cnt += 1
            SUM += B[j+k]
        else:
            break
    ans = max(ans,(Alen-i-1+cnt))

print(ans)

Solution (Python):
n, m, k = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
arr1, arr2 = [0], [0]
for i in range(n):
	arr1.append(arr1[i] + A[i])
for i in range(m):
	arr2.append(arr2[i] + B[i])

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

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 bisect
from itertools import accumulate
n,m,k=map(int,input().split())
a=list(accumulate(map(int,input().split())))
b=list(accumulate(map(int,input().split())))
ans=0
j=bisect.bisect_right(b,k)
ans=max(ans,j)
for i,x in enumerate(a):
    y=k-x
    if y>0:
        j=bisect.bisect_right(b,y)
        ans=max(ans,i+j+1)
i=bisect.bisect_right(a,k)
ans=max(ans,i)
for j,y in enumerate(b):
    x=k-y
    if x>0:
        i=bisect.bisect_right(a,x)
        ans=max(ans,i+j+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):
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
i=0
mx=0
lol=0
while i<n and a[i]<=k:
    k-=a[i]
    i+=1
    mx+=1
    lol=1
j=0
while j<m and b[j]<=k:
    k-=b[j]
    j+=1
    mx+=1
cou=mx

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

Solution (Python):
from bisect import bisect_right

N, M, K = map(int, input().split())
A = [0] + list(map(int, input().split()))
B = [0] + list(map(int, input().split()))

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

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

b_cnt = M

ans = 0
for a_cnt in range(N+1):
    if A[a_cnt] > K:
        continue

    rest = K - A[a_cnt]

    while A[a_cnt] + B[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()))

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]

for i in range(N):
    a.append(a[i]+A[i])
for i in range(M):
    b.append(b[i]+B[i])
j=M
ans=0
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 itertools import accumulate
import bisect
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ac = [0] + list(accumulate(a))
bc = [0] + list(accumulate(b))

ans = 0
for i in range(bisect.bisect(ac, k)):
    bi = bisect.bisect(bc, k - ac[i])
    ans = max(ans, i+bi-1)
print(ans)

Solution (Python):
from bisect import *
n, m, k = map(int, input().split())
A, B = [0], [0]
for x in map(int, input().split()): A.append(A[-1]+x)
for x in map(int, input().split()): B.append(B[-1]+x)
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 = [0] + list(map(int, input().split()))
b = [0] + list(map(int, input().split()))
s = sum(b)
j = m
ans = 0
for i in range(n+1):
    s += a[i]
    while j >= 0 and s > k:
        s -= b[j]
        j -= 1
    if j < 0:
        break
    else:
        ans = max(ans, i+j)
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)

N, M, K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
Asum = [0]
Bsum = [0]
count = 0
for p in A:
    count += p
    Asum.append(count)
count = 0
for p in B:
    count += p
    Bsum.append(count)
best = 0
Blast = M
for t in range(N+1):
    if Asum[t] > K:
        break
    if best < t:
        best=t
    for s in reversed(range(0,Blast+1)):
        if Asum[t]+Bsum[s]<=K:
            if best < t+s:
                best=t+s
            Blast = s
            break
print(best)

Solution (Python):
N,M,K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
import bisect
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])

ans=0
for i in range(N+1):
    if A_sum[i]>K:
        continue
    b_read=bisect.bisect_right(B_sum,K-A_sum[i])-1
    ans=max(ans,i+b_read)
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
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):
"""
AtCoder :: Beginner Contest 172 :: C :: Tsundoku
https://atcoder.jp/contests/abc172/tasks/abc172_c

"""
import sys
from collections import deque


def solve(A, B, K):
    """Solve puzzle."""
    time_read = 0

    
    a_books_read = []
    while A:
        if A[0] + time_read <= K:
            time_read += A[0]
            a_books_read.append(A[0])
            A.popleft()
        else:
            break

    
    b_books_read = []
    while B:
        if B[0] + time_read <= K:
            b_books_read.append(B[0])
            time_read += B[0]
            B.popleft()
        else:
            break

    max_books = len(a_books_read) + len(b_books_read)

    
    
    while a_books_read:
        time_read -= a_books_read[-1]
        a_books_read.pop()
        while B:
            if B[0] + time_read <= K:
                b_books_read.append(B[0])
                time_read += B[0]
                B.popleft()
            else:
                break
        max_books = max(max_books, len(a_books_read) + len(b_books_read))

    return max_books


def main():
    """Main program."""
    N, M, K = (int(i) for i in sys.stdin.readline().split())
    A = deque(int(i) for i in sys.stdin.readline().strip().split())
    B = deque(int(i) for i in sys.stdin.readline().strip().split())
    print(solve(A, B, K))


if __name__ == '__main__':
    main()

Solution (Python):
import math

def solve(n,m,k,a_lst,b_lst):
    
    aa_lst = make_sum_list(a_lst,k)
    bb_lst = make_sum_list(b_lst,k)
    len_aa = len(aa_lst)
    len_bb = len(bb_lst)

    
    if len_aa == 0:   return len_bb
    elif len_bb == 0: return len_aa
    
    
    if aa_lst[-1] + bb_lst[-1] <= k: return len_aa + len_bb 
    
    
    max_read = max(len_aa,len_bb)

    
    for i in reversed(range(len_aa)):
        limit = k - aa_lst[i]
        read = i + 1
        if read + len_bb + 1 <= max_read:
            
            break

        imax = len_bb-1
        imin = 0
        imid = len_bb // 2
        while True:
            if bb_lst[imid] <= limit:
                
                if imax - imid <= 1:
                    
                    if bb_lst[imax] <= limit:
                        read += imax + 1
                    else:
                        read += imid + 1
                    break
                imin = imid
            else:
                
                if imid - imin <= 1:
                    
                    if bb_lst[imin] <= limit:
                        read += imin + 1
                    break
                imax = imid
            imid = (imin + imax) // 2
        max_read = max(read,max_read)
    return max_read

def make_sum_list(lst,max):
    sum_lst = []
    sum = 0
    for n in lst:
        sum += n
        if sum > max:
            break
        sum_lst.append(sum)
    return sum_lst

n,m,k = map(int, input().split())
a_lst = list(map(int, input().split()))
b_lst = list(map(int, input().split()))
ans = solve(n,m,k,a_lst,b_lst)
print(ans)

Solution (Python):
n,m,k = map(int,input().split())
A = [int(p) for p in input().split()]
B = [int(p) for p in input().split()]
a,b = [0],[0]

a_apnd = a.append
b_apnd = b.append
for i in range(n):
  a_apnd(a[i]+A[i])
for i in range(m):
  b_apnd(b[i]+B[i])

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

print(count)

Solution (Python):
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
A=[0]
for i in range(n):
  A.append(A[i]+a[i])
B=[0]
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):

from itertools import accumulate
from bisect import bisect_right

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

a = [0] + list(accumulate(A))
b = [0] + list(accumulate(B))

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

Solution (Python):

from itertools import accumulate
from bisect import bisect_right

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

a = [0] + list(accumulate(A))
b = [0] + list(accumulate(B))

result = 0
for i in range(N + 1):
    t = K - a[i]
    if t < 0:
        break
    j = bisect_right(b, t)
    result = max(result, i + j - 1)
print(result)

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

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
for i in range(N + 1):
    cntA = i
    rest = K - SA[i]
    if rest < 0:
        break
    cntB = bisect_right(SB, rest) - 1
    ans = max(ans, cntA + cntB)
print(ans)

Solution (Python):
import sys
import math
import heapq
from collections import defaultdict, deque
sys.setrecursionlimit(10**6)
input = sys.stdin.readline
def r():
    return int(input())
def rm():
    return map(int,input().split())
def rl():
    return list(map(int,input().split()))

'''C Tsundoku'''
n,m,k=rm()
a=rl()
b=rl()
c=[0]*(n+1)
d=[0]*(m+1)
for i in range(n):
    c[i+1]=c[i]+a[i]
for i in range(m):
    d[i+1]=d[i]+b[i]
ans=0;j=m
for i in range(n+1):
    if c[i]>k:
        break
    while c[i]+d[j]>k:
        j-=1
    ans=max(ans,i+j)
print(ans)

Solution (Python):



from bisect import bisect_left
from itertools import accumulate
n,m,k=list(map(int,input().split()))
a=list(map(int,input().split()))
b=list(map(int,input().split()))
A=[0]+list(accumulate(a))
B=[0]+list(accumulate(b))

ans=0
sup_i=bisect_left(A,k+1)
for i in range(sup_i):
  ans=max(ans,i+bisect_left(B,k-A[i]+1)-1)
 
print(ans)

Solution (Python):
from collections import deque
 
def solve():
    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(len(a)):
        if a[i] > K:
            break
        while b[j] > K - a[i]:
            j -= 1
        ans = max(ans, i+j)
    
    print(ans)
            
solve()

Solution (Python):
import math

def solve(n,m,k,a_lst,b_lst):
    
    aa_lst = make_sum_list(a_lst,k)
    bb_lst = make_sum_list(b_lst,k)
    len_aa = len(aa_lst)
    len_bb = len(bb_lst)

    
    if len_aa == 0:
        if len_bb == 0: return 0
        else: return len_bb
    elif len_bb == 0: return len_aa
    
    
    if aa_lst[-1] + bb_lst[-1] <= k: return len_aa + len_bb 
    
    
    max_read = max(len_aa,len_bb)

    
    for i in reversed(range(len_aa)):
        time = aa_lst[i]
        read = i + 1
        if read + len_bb + 1 <= max_read:
            break
        imax = len_bb-1
        imin = 0
        imid = len_bb // 2
        while True:
            if time + bb_lst[imid] <= k:
                
                if imax - imid <= 1:
                    
                    if time + bb_lst[imax] <= k:
                        read += imax + 1
                    else:
                        read += imid + 1
                    break
                imin = imid
            else:
                
                if imid - imin <= 1:
                    
                    if time + bb_lst[imin] <= k:
                        read += imin + 1
                    break
                imax = imid
            imid = (imin + imax) // 2
        max_read = max(read,max_read)
    return max_read


def make_sum_list(lst,max):
    sum_lst = []
    sum = 0
    for n in lst:
        sum += n
        if sum > max:
            break
        sum_lst.append(sum)
    return sum_lst

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

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

Solution (Python):
import bisect
n, m, k = map(int,input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
sa = [0]
sb = [0]
for i in range(n):
    sa.append(sa[-1] + a[i])
for i in range(m):
    sb.append(sb[-1] + b[i])
ans = 0
for i in range(n+1):
    if sa[i] > k:
        continue
    else:
        j = bisect.bisect_right(sb, k-sa[i])
        ans = max(ans, i+j-1)
print(ans)

Solution (Python):
from itertools import accumulate
from bisect import bisect_right

n, m, k = map(int, input().split())
a = [0] + list(accumulate(list(map(int, input().split()))))
b = [0] + list(accumulate(list(map(int, input().split()))))
ans = 0

for i in range(n + 1):
    j = bisect_right(b, k - a[i])
    if a[i] + b[j - 1] <= k:
            ans = max(ans, i + (j - 1))
    
print(ans)

Solution (Python):
import sys

input = sys.stdin.readline


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

    time = sum(B)
    i_B = M - 1
    ans = 0
    for i in range(0, N + 1):
        for j in reversed(range(i_B + 1)):
            if time <= K:
                break
            time -= B[j]
            i_B -= 1
        if time > K:
            break
        ans = max(ans, i + (i_B + 1))
        if i < N:
            time += A[i]

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


def cumsum(a):
    """l[i] = sum(a[:i]) なるlを返す
    sum(a[i:j]) == l[j+1] - l[i]
    """
    c = 0
    l = [None] * (len(a)+1)
    for i,num in enumerate(a):
        l[i] = c
        c += num
    l[-1] = c
    return l

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

r = m-1
s = sum(b)
num = m
best = 0
while s>k and r>=0:
    s -= b[r]
    r -= 1
    num -= 1
if s<=k:
    best = max(best, num)
for l in range(n):
    num += 1
    s += a[l]
    while s>k and r>=0:
        s -= b[r]
        r -= 1
        num -= 1
    if s<=k:
        best = max(best, num)
print(best)