Problem:
 Score : 300 points    Problem Statement We have two desks: A and B. Desk A has a vertical stack of N books on it, and Desk B similarly has M books on it.  It takes us A_i minutes to read the i -th book from the top on Desk A (1 \leq i \leq N) , and B_i minutes to read the i -th book from the top on Desk B (1 \leq i \leq M) .  Consider the following action:   Choose a desk with a book remaining, read the topmost book on that desk, and remove it from the desk.   How many books can we read at most by repeating this action so that it takes us at most K minutes in total? We ignore the time it takes to do anything other than reading.      Constraints  1 \leq N, M \leq 200000  1 \leq K \leq 10^9  1 \leq A_i, B_i \leq 10^9  All values in input are integers.         Input Input is given from Standard Input in the following format:  N  M  K  A_1  A_2  \ldots  A_N  B_1  B_2  \ldots  B_M       Output Print an integer representing the maximum number of books that can be read.        Sample Input 1 3 4 240
60 90 120
80 150 80 150      Sample Output 1 3  In this case, it takes us 60 , 90 , 120 minutes to read the 1 -st, 2 -nd, 3 -rd books from the top on Desk A, and 80 , 150 , 80 , 150 minutes to read the 1 -st, 2 -nd, 3 -rd, 4 -th books from the top on Desk B, respectively.  We can read three books in 230 minutes, as shown below, and this is the maximum number of books we can read within 240 minutes.   Read the topmost book on Desk A in 60 minutes, and remove that book from the desk.  Read the topmost book on Desk B in 80 minutes, and remove that book from the desk.  Read the topmost book on Desk A in 90 minutes, and remove that book from the desk.        Sample Input 2 3 4 730
60 90 120
80 150 80 150      Sample Output 2 7       Sample Input 3 5 4 1
1000000000 1000000000 1000000000 1000000000 1000000000
1000000000 1000000000 1000000000 1000000000      Sample Output 3 0  Watch out for integer overflows.

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

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

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

Solution (Python):
n, m, k = map(int, input().split())
a = [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]
i, j, z = 0, m, 0
while i <= n and a[i] <= k:
    while b[j] > k - a[i]:
        j -= 1
    z = max(z, i + j)
    i += 1
print(str(z))

Solution (Python):
n, m, k = map(int, input().split())
a = [0] + list(map(int, input().split()))
b = [0] + list(map(int, input().split()))
for i in range(1, n + 1):
    a[i] += a[i - 1]
for i in range(1, m + 1):
    b[i] += b[i - 1]
i, j, z = 0, m, 0
while i <= n and a[i] <= k:
    while b[j] > k - a[i]:
        j -= 1
    z = max(z, i + j)
    i += 1
print(str(z))

Solution (Python):
def main():
    n,m,k=map(int,input().split())
    aa=list(map(int,input().split()))
    bb=list(map(int,input().split()))
    na=nb=0
    while na < n and aa[na] <= k:
        k-=aa[na]
        na+=1
    while nb < m and bb[nb] <= k:
        k-=bb[nb]
        nb+=1
    nmax=na+nb
    while na > 0:
        na-=1
        k+=aa[na]
        while nb < m and bb[nb] <= k:
            k-=bb[nb]
            nb+=1
        if na+nb > nmax:
            nmax=na+nb
    print(nmax)
main()

Solution (Python):
from sys import stdin, stdout
input = stdin.readline
print = stdout.write
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]
i, j, z = 0, m, 0
while i <= n and a[i] <= k:
    while b[j] > k - a[i]:
        j -= 1
    z = max(z, i + j)
    i += 1
print(str(z))

Solution (Python):
from sys import stdin, stdout
input = stdin.readline
print = stdout.write
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]
i, j, z = 0, m, 0
while i <= n and a[i] <= k:
    while b[j] > k - a[i]:
        j -= 1
    z = max(z, i + j)
    i += 1
print(str(z))

Solution (Python):
from sys import stdin, stdout
input = stdin.readline
print = stdout.write
n, m, k = map(int, input().split())
a, b = [0], [0]
a.extend(list(map(int, input().split())))
b.extend(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]
i, j, z = 0, m, 0
while i <= n and a[i] <= k:
    while b[j] > k - a[i]:
        j -= 1
    z = max(z, i + j)
    i += 1
print(str(z))

Solution (Python):
from sys import stdin, stdout
input = stdin.readline
print = stdout.write
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]
i, j, z = 0, m, 0
while i <= n and a[i] <= k:
    while b[j] > k - a[i]:
        j -= 1
    z = max(z, i + j)
    i += 1
print(str(z))

Solution (Python):
from sys import stdin, stdout
input = stdin.readline
print = stdout.write 
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]
i, j, z = 0, m, 0
while i <= n and a[i] <= k:
    while b[j] > k - a[i]:
        j -= 1
    z = max(z, i + j)
    i += 1
print(str(z))

Solution (Python):
from sys import stdin, stdout
input = stdin.readline
print = stdout.write 
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]
i, j, z = 0, m, 0
while i <= n and a[i] <= k:
    while b[j] > k - a[i]:
        j -= 1
    z = max(z, i + j)
    i += 1
print(str(z))

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

Solution (Python):
N,M,K = map(int,input().split())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
sum_AB = 0
cnt = 0
idx_A = -1
idx_B = -1
for i,a in enumerate(A):
    if sum_AB + a > K:
        break
    else:
        sum_AB += a
        cnt += 1
        idx_A = i
for i,b in enumerate(B):
    if sum_AB + b > K:
        break
    else:
        sum_AB += b
        cnt += 1
        idx_B = i
ans = cnt
A = A[0:idx_A+1][::-1]
from collections import deque
B = deque(B[idx_B+1:])
for a in A:
    sum_AB -= a
    cnt -= 1
    while B:
        if sum_AB + B[0] > K:
            break
        else:
            b = B.popleft()
            sum_AB += b
            cnt += 1
    ans = max(cnt,ans)
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()))
s_A = [0]+list(accumulate(A))
s_B = [0]+list(accumulate(B))

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

dp = [0]*(N+1)
tot, lb = sum(B), M
while tot > K: 
    tot -= B.pop(-1)
    lb -= 1
dp[0] = lb

for i in range(N):
    K -= A[i]
    if K < 0:
        continue
    while tot > K and lb > 0: 
        tot -= B.pop(-1)
        lb -= 1
    dp[i+1] = i+1 + lb

print(max(dp))

Solution (Python):


import copy
import random
import bisect 
import fractions 
import math
import sys
import collections
from decimal import Decimal 

mod = 10**9+7
sys.setrecursionlimit(mod) 

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))

"""
dp
time / books_no?
10 0 0
20 0 0
30 0 0
40 0 0
50 0 0
60 1 0
70 1 0
80 0 1




"""



N, M, K = LI()
A = LI()
B = LI()

"""
片方に注目して探索すればよい　

単純にfor,forだと計算量がまずいが，前回の値の部分から探索すればいいのでそこまで
計算量は増えない

"""

a = [0]
b = [0]

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

for i in range(M):
    b.append(b[-1] + 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):
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

n,m,k=nm()
a=nl()
b=nl()

aa,bb=[0],[0]
for i in range(n):
  aa.append(a[i]+aa[i])
for i in range(m):
  bb.append(b[i]+bb[i])

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

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

aarr = [0]
total = 0
for a in arr:
    total+=a
    aarr.append(total)
bbrr = [0]
total = 0
for b in brr:
    total+=b
    bbrr.append(total)

ans = 0
bi = len(brr)
for ai in range(len(arr)+1):
    while aarr[ai]+bbrr[bi]>k:
        if bi == 0:
            break
        bi -= 1

    if bi==0 and aarr[ai]+bbrr[bi]>k:
        break
    ans = max(ans,ai+bi)

print(ans)

Solution (Python):
from collections import deque

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

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

ans,j = 0,M

for i in range(N+1):
    
    if a[i] > K:
        break
    
    while b[j] > 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()))

a1 = [0]
b1 = [0]

for i in range(n):
    a1.append(a1[-1]+a[i])

for i in range(m):
    b1.append(b1[-1]+b[i])

cnt = m
ans = 0

for i in range(n+1):
    while a1[i] + b1[cnt] > k:
        if cnt == 0:
            break
        cnt -= 1
    if a1[i] + b1[cnt] <= k:
        ans = max(i + cnt, ans)
    else:
        break

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()))
A=[0]
B=[0]
for i in range(n):
    A.append(A[-1]+a[i])
for i in range(m):
    B.append(B[-1]+b[i])
ans=0
for i in range(n+1):
    if A[i]>k:
        break
    index=bisect.bisect_right(B,k-A[i])-1
    index=max(0,index)
    ans=max(ans,i+index)
print(ans)

Solution (Python):

import itertools
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
j = m

aa = [0] + list(itertools.accumulate(a))
bb = [0] + list(itertools.accumulate(b))





for i, a_item in enumerate(aa):
    if a_item > k:
        break
    while a_item + bb[j] > k:
        j -= 1
    ans = max(i + j, ans)
print(ans)

Solution (Python):
from itertools import accumulate
import bisect
n,m,k = map(int, input().split())
alist= list(map(int, input().split()))
blist = list(map(int, input().split()))


alist = list(accumulate(alist))
blist = list(accumulate(blist))

alist.insert(0,0)
blist.insert(0,0)

ans = 0
j = m
for i in range(0,n+1):
  if alist[i] > k:
    pass
  else:
    while blist[j] > k - alist[i]:
      j -= 1
    ans = max(ans,i+j)

print(ans)

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

Solution (Python):
import numpy
import bisect
n,m,k = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
a = [0]+list(numpy.cumsum(A))
b = [0]+list(numpy.cumsum(B))
ans = 0

for i in range(len(a)):
    x = k - a[i]
    if x >= 0:
        j = bisect.bisect_right(b,x)-1
        ans = max(ans, i+j)
print(ans)

Solution (Python):
import itertools


def main():
    n, m, k = [int(x) for x in input().split()]
    array_a = [0] + list(itertools.accumulate(int(x) for x in input().split()))
    array_b = [0] + list(itertools.accumulate(int(x) for x in input().split()))

    def generate_anses():
        from_b = m
        for from_a in range(n + 1):
            if array_a[from_a] > k:
                break
            while array_b[from_b] > k - array_a[from_a]:
                from_b -= 1
            yield from_a + from_b

    print(max(generate_anses()))


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, 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):
ans=0

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

r=0

for l in range(n):
    
        while r<m and x+b[r]<=k:
            x+=b[r]
            r+=1
            z+=1
        if x<=k:ans=max(z,ans)
        
        x-=a.pop()
        z-=1

while r<m and x+b[r]<=k:
    x+=b[r]
    r+=1
    z+=1
ans=max(z,ans)

print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
from collections import deque
def main():
    N, M, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    As = [0]*(N+1)
    Bs = sum(B)
    As[0] = 0
    for i in range(1,N+1):
        As[i] = As[i-1] + A[i-1]
    T = K
    j = M 
    ans = 0
    for i in range(N+1):
        T = K - As[i]
        if T < 0:
            break
        while Bs > T:
            j -= 1
            Bs -= B[j]
        ans = max(ans, i+j)
    print(ans)

if __name__ == '__main__':
    main()

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

N, M, K = [int(n) for n in input().split()]
A = [0]+[*accumulate([int(n) for n in input().split()])]
B = [*accumulate([int(n) for n in input().split()])]
ans = 0
a = bisect_right(A, K)
for i in range(a):
    b = bisect_right(B, K-A[i])
    ans = max(ans, 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()))
 
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
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

sa = [0 for i in range(n+1)]
sb = [0 for i in range(m+1)]

c = 0

for i in range(n):
  c += a[i]
  sa[i+1] = c
c = 0
for i in range(m):
  c += b[i]
  sb[i+1] = c
ans = 0



for i in range(n+1):
  x = k - sa[i]
  if x < 0:
    continue
  y = bisect.bisect_right(sb,x)
  y += i
  ans = max(ans, y-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())
an = list(map(int, input().split()))
bn = list(map(int, input().split()))
ls = an[::-1] + bn
l = 0
r = n
s = sum(an)
ans = 0
while r < n + m and l <= n:
    if s <= k:
        ans = max(ans, r - l)
        s += ls[r]
        r += 1
    else:
        s -= ls[l]
        l += 1
if s <= k:
    ans = max(ans, r-l)
print(ans)

Solution (Python):
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
from itertools import accumulate
from bisect import bisect_right
n,m,k=MI()
A=LI()
B=LI()
a=[0]+list(accumulate(A))
b=[0]+list(accumulate(B))
ans=0
for i in range(n+1):
  if a[i]>k:
    break
  j=bisect_right(b,k-a[i])
  ans=max(ans,i+j-1)
print(ans)

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

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

time = 0
books = 0
a_index = 0
b_index = 0

for i in range(1, n + 1):
  if time + a[i] <= k:
    time += a[i]
    books += 1
    a_index = i
  else:
    break

for i in range(1, m + 1):
  if time + b[i] <= k:
    time += b[i]
    books += 1
    b_index = i
  else:
    break

max_books = books

for i in range(a_index, 0, -1):
  time -= a[i]
  books -= 1
  for j in range(b_index + 1, m + 1):
    if time + b[j] <= k:
      time += b[j]
      books += 1
      b_index = j
      max_books = max(max_books, books)
    else:
      break

print(max_books)

Solution (Python):
import bisect

N,M,K=map(int,input().split())
list_a=list(map(int,input().split()))
list_b=list(map(int,input().split()))


totaltame_a=[0]
for a in list_a:
    temp_a=totaltame_a[-1]+a
    if temp_a>K:
        break
    else:
        totaltame_a.append(temp_a)


totaltime_b=[0]
for b in list_b:
    totaltime_b.append(totaltime_b[-1]+b)

ans=0

for i in range (len(totaltame_a)):
    Kb=K-totaltame_a[i]
    j=bisect.bisect_right(totaltime_b,Kb)-1
    if i+j>ans:
        ans=i+j
    else:
        pass

print(ans)

Solution (Python):
N,M,K=map(int,input().split())
A=[0]+list(map(int,input().split()))
B=list(map(int,input().split()))
x=sum(B)
y=M-1
r=0
for i in range(N+1):
    x+=A[i]
    while x>K and y>=0:
        x-=B[y]
        y-=1
    if x<=K:
        r=max(r,i+y+1)
print(r)

Solution (Python):


from pprint import pprint
from bisect import bisect_right
import math
import sys

sys.setrecursionlimit(10 ** 6)
INF = float('inf')


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

sum_A = [0] * (N+1)
for i in range(N):
    sum_A[i+1] = sum_A[i] + A[i]
sum_B = [0] * (M+1)
for i in range(M):
    sum_B[i+1] = sum_B[i] + B[i]


res = 0
for i in range(N+1):
    time_A = sum_A[i]
    rest = K - time_A
    if rest < 0:
        continue
    j = bisect_right(sum_B, rest) - 1
    res = max(res, i + j)
print(res)

Solution (Python):
from sys import stdin
import bisect

sysin=stdin.readline

N,M,K=[int(x) for x in sysin().rstrip().split()]
a=[int(x) for x in sysin().rstrip().split()]
b=[int(x) for x in sysin().rstrip().split()]






ans=0

accum_a=[0]
for i in a:
    _a=accum_a[-1]+i
    if _a>K:
        break
    else:
        accum_a.append(_a)
accum_a.append(K)

accum_b=[0]
for i in b:
    accum_b.append(accum_b[-1]+i)

ans=0

for j in range (len(accum_a)-1):
    Kb=K-accum_a[j]
    bookB=bisect.bisect_right(accum_b,Kb)-1
    if j+bookB>ans:
        ans=j+bookB
    else:
        pass

print(ans)

Solution (Python):


from pprint import pprint
from bisect import bisect_right
import math
import sys

sys.setrecursionlimit(10 ** 6)
INF = float('inf')


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

sum_A = [0] * (N+1)
for i in range(N):
    sum_A[i+1] = sum_A[i] + A[i]
sum_B = [0] * (M+1)
for i in range(M):
    sum_B[i+1] = sum_B[i] + B[i]


res = 0
for i in range(N+1):
    time_A = sum_A[i]
    rest = K - time_A
    if rest < 0:
        continue
    j = bisect_right(sum_B, rest) - 1
    res = max(res, i + j)
print(res)

Solution (Python):





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






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

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




ans = 0
for i in range(M+1):
    A_use = K-B_wa[i]
    if A_use<0: 
        break
    
    
    A_MAX = 0
    start = 0
    end = N
    while  end-start>=1 : 
        now = int((start + end+1)/2) 
        if A_wa[now]==A_use :  
            A_MAX = now 
            break
        elif A_wa[now]<A_use : 
            start = now 
            A_MAX = now 
        else : 
            end = now-1
    
    
    
    if A_MAX+i>ans :
        ans=A_MAX+i


print(ans)

Solution (Python):
from bisect import bisect_right
import numpy as np
def main():
    N, M, K = map(int, input().split())
    A = np.array([0] + list(map(int, input().split())), dtype=np.int64)
    B = np.array([0] + list(map(int, input().split())), dtype=np.int64)

    As = np.cumsum(A)
    Bs = np.cumsum(B)

    count = 0

    for i, a in enumerate(As):
        if a > K:
            break
        j = bisect_right(Bs, K-a)
        
        count = max(count, (i+j-1))

    print(count)

if __name__ == '__main__':
    main()

Solution (Python):
def search(b, s, e, t):
    if e - s == 1:
        return s

    mid = (e + s) // 2

    if b[mid] <= t:
        return search(b, mid, e, t)
    else:
        return search(b, s, mid, t)

INF = float('inf')
N, M, K = list(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,x in enumerate(A):
    a[i+1] = a[i] + x

for i,x in enumerate(B):
    b[i+1] = b[i] + x

ans = 0
j = M
for i, va in enumerate(a):
    if va > K:
        break
    index = search(b, 0, len(b), K-va)
    ans = max(i+index, ans)
print(ans)

Solution (Python):

def main():
    import sys
    imput = sys.stdin.readline
    
    
    
    n, m, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))

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

    

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

    
if __name__ == "__main__":
    main()

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

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

    n_list = list(map(int,input().split()))
    m_list = list(map(int,input().split()))
    a, b =[0],[0]
    for i in range(n):
        a.append(a[i]+n_list[i])
    for j in range(m):
        b.append(b[j]+m_list[j])
    
    j=m
    ans = 0
    for i in range(n+1):
        if a[i]>k:
            break
        while b[j]+a[i]>k:
            j -= 1
        ans = max(ans,i+j)
    print(ans)
main()

Solution (Python):
a,b,c=map(int,input().split())
ad=list(map(int,input().split()))
bd=list(map(int,input().split()))

ae=-1
be=-1
ans=0
gen=0
nokori=c
for i in range(a):
  if nokori>=ad[i]:
    nokori-=ad[i]
    gen+=1
    ae+=1
  else:
    break

for i in range(b):
  if nokori>=bd[i]:
    nokori-=bd[i]
    gen+=1
    be+=1
  else:
    break

ans=gen
while ae!=-1:
  nokori+=ad[ae]
  gen-=1
  ae-=1
  while be<b-1:
    if nokori>=bd[be+1]:
      nokori-=bd[be+1]
      gen+=1
      be+=1
    else:
      break
    
  if ans<gen:
    ans=gen
    
print(ans)

Solution (Python):
def main():
    from itertools import chain

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

    t = sum(B)
    ans = 0
    j = M
    for i, x in enumerate(chain([0], A)):
        t += x
        while j > 0 and t > K:
            j -= 1
            t -= B[j]
        if t > K: break
        if ans < i + j:
            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()))

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(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
    time=a[i]+b[j]
    while time>k:
        j-=1
        time=a[i]+b[j]
    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_cum, B_cum = [0], [0]
for i, a in enumerate(A):
    A_cum.append(A_cum[i] + a)
for i in range(m):
    B_cum.append(B_cum[i] + B[i])

ans = 0
best = m
for i in range(n+1):
    a_time = A_cum[i]
    for j in range(best, -1, -1):
        if B_cum[j] <= k - a_time:
            ans = max(ans, i + j)
            break
    best = j
print(ans)

Solution (Python):
from itertools import accumulate
N, M, K = map(int, input().split())
A = [0] + list(map(int, input().split()))
B = [0] + list(map(int, input().split()))
A = list(accumulate(A))
B = list(accumulate(B))
ans = 0
j = M
for i in range(N+1):
    while(j>=0):
        if A[i] + B[j] <= K:
            ans = max(ans,i+j)
            break
        j -= 1
    """for m in reversed(range(M+1)):
        if A[n] + B[m] <= K:
            ans = max(ans, n+m)
            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, 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(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 itertools import accumulate
from bisect import bisect_right

n, m, k = map(int, input().split())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]


a_cum = [0] + list(accumulate(a))
b_cum = [0] + list(accumulate(b))

ans = [0]
for i in range(n + 1):    
    c = bisect_right(b_cum, k-a_cum[i]) - 1 
                                 
    if c != -1: 
        ans.append(c+i)

print(max(ans))

Solution (Python):


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


AA = [0] * (N + 1)
BB = [0] * (M + 1)
for i, v in enumerate(A):
    AA[i + 1] += v + AA[i]
for i, v in enumerate(B):
    BB[i + 1] += v + BB[i]

ans = 0
b_cnt = M
for a_cnt in range(N + 1):
    if AA[a_cnt] > K:
        continue
    while AA[a_cnt] + BB[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()))





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


A.insert(0, 0)
B.insert(0,0)
ans = 0


for i in range(N+1):
    if K - A[i] < 0:
        break
    
    ok = -1
    ng = M+1
    while (abs(ok - ng) > 1):
        mid = (ok + ng) // 2
        if B[mid] <= K - A[i]: ok = mid
        else: ng = mid
        
    ans = max(ans, i + ok)
    
print(ans)

Solution (Python):
import itertools
N,M,K = map(int,input().split())
A = [0]+list(map(int,input().split()))
B = [0]+list(map(int,input().split()))
SA = list(itertools.accumulate(A))
SB = list(itertools.accumulate(B))
ans = 0
j = len(B)-1
for i in range(len(SA)):
    while(j>=0):
        if SA[i] + SB[j] <= K:
            ans = max(ans,i+j)
            break
        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()))
ans = 0

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

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
N, M, K = map(int, input().split())
Nlist = [0]+list(map(int, input().split()))
Mlist = [0]+list(map(int, input().split()))
Asum = list(accumulate(Nlist))
Bsum = list(accumulate(Mlist))
count = 0
j = 0
for i in range(N, -1, -1):
    if K >= Asum[i]:
        while Asum[i]+Bsum[j] <= K:
            j += 1
            if j == M+1:
                break
        if count < i+j-1:
            count = i+j-1
    if j == M+1:
        break
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]
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 bisect import bisect_right
n , m , k = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
acc_a = [0 for i in range(n+1)]
acc_b = [0 for i in range(m + 1)]
acc_a[0] = 0
acc_b[0] = 0
for i in range(1,n+1):
    acc_a[i] = acc_a[i-1] + a[i-1]
for i in range(1,m+1):
    acc_b[i] = acc_b[i-1] + b[i-1]
ans = 0
for i in range(n + 1):
    if k >= acc_a[i]:
        index = bisect_right(acc_b, k - acc_a[i])
        ans = max(ans, i + index - 1)
    else:
        break
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()))


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

book_b = [0] * (m + 1) 
for j in range(m):
    book_b[j + 1] = book_b[j] + b[j]
count = 0
for read in range(n + 1):
    tmp = k - book_a[read]
    if tmp < 0:
        break
    read_b = bisect.bisect_right(book_b, tmp) -1
    count = max(count, read_b + read)

print(count)