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 = 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
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_ls = [0] + list(map(int, input().split(' ')))
for i in range(N):
    A_ls[i + 1] += A_ls[i]
B_ls = [0] + list(map(int, input().split(' ')))
for i in range(M):
    B_ls[i + 1] += B_ls[i]
b_cnt, rst = M, 0
for a_cnt in range(N + 1):
    if A_ls[a_cnt] > K:
        break
    while A_ls[a_cnt] + B_ls[b_cnt] > K:
        b_cnt -= 1
    rst = max(rst, a_cnt + b_cnt)
print(rst)

Solution (Python):
N,M,K = list(map(int,input().split()))
A = list(map(int,input().split()))
B = list(map(int,input().split()))
sum_time = 0
now_read_book = 0
ans = 0
now_A = -1

for i in range(N):
  if sum_time + A[i] <= K:
    sum_time += A[i]
    now_read_book += 1
    now_A = i
  else:
    break
ans = now_read_book

for i in range(M):
  if sum_time + B[i] > K:
    while sum_time + B[i] > K and now_A >= 0:
      sum_time -= A[now_A]
      now_A -= 1
      now_read_book -= 1
  if sum_time + B[i] <= K:
    sum_time += B[i]
    now_read_book += 1
  else:
    break
  if now_read_book > ans:
    ans = now_read_book
print(ans)

Solution (Python):
N, M, K = map(int, input().split(' '))
A_ls = [0] + list(map(int, input().split(' ')))
for i in range(N):
    A_ls[i + 1] += A_ls[i]
B_ls = [0] + list(map(int, input().split(' ')))
for i in range(M):
    B_ls[i + 1] += B_ls[i]

b_cnt, result = M, 0
for a_cnt in range(N + 1):
    if A_ls[a_cnt] > K:
        break
    while A_ls[a_cnt] + B_ls[b_cnt] > K and b_cnt > 0:
        b_cnt -= 1
    result = max(result, a_cnt + b_cnt)
print(result)

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

c=[0]
d=[0]

for i in range(n):
  c.append(c[-1]+a[i])
  
for j in range(m):
  d.append(d[-1]+b[j])

ans=0
for x in range(n+1):
  l=c[x]
  B=0
  if l<=k:
    h=k-l
    mi=0
    ma=m+1
    while ma-mi>1:
      mid=(mi+ma)//2
      if d[mid]<=h:
        mi=mid
      elif d[mid]>h:
        ma=mid
    B=mi
    
    ans=max(ans,x+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):
N,M,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
a = [0]
b = [0]
ans = 0
max_num = 0
j = M
for i in range(N):
    a.append(a[i]+A[i])
for i in range(M):
    b.append(b[i]+B[i])
for i in range(N+1):
    if a[i] > K:
        break
    else:
        while b[j] > K - a[i]:
            j -=1
            if j <0:
                break
    if K>= 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()))
ans,right=0,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])
for i in range(m+1):
  if k<B[i] or i==m:
    right=i-1
    if i==m:
      right=i
    ans=right
    break

for j in range(0,n):
  if k<a[j]:
    break
  k-=a[j]
  while k<B[right]:
    right-=1
  ans=max(ans,right+j+1)
print(ans)

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

    for i in range(1, n + 1):
        A[i] = A[i - 1] + a[i - 1]
        if A[i] > k:
            n = i - 1
            break
    for i in range(1, m + 1):
        B[i] = B[i - 1] + b[i - 1]
        if B[i] > k:
            m = i - 1
            break
    ans = 0
    for i in range(0, n + 1):
        j = max(ans - i, 0)
        if j > m or A[i] + B[j] > k: continue
        while j <= m:
            if A[i] + B[j] > k:
                j -= 1
                break
            j += 1
        j = min(m,j)
        ans = max(i + j, ans)
    print(ans)


abc172c_tsundoku()

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
  else:
    while b[j] > K - a[i]:
      j-=1
      if j<0:
        break
    if K>=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()))
if M > N:
    N,M = M,N
    A,B = B,A
a = [0]
b = [0]
for _ in range(max(N, M)):
    if _ < min(N,M):
        a.append(a[_] + A[_])
        b.append(b[_] + B[_])
    else:
        a.append(a[_] + A[_])
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):
N, M, K = map(int, input().split(' '))
A_ls = [0] + list(map(int, input().split(' ')))
for i in range(len(A_ls) - 1):
    A_ls[i + 1] += A_ls[i]

B_ls = [0] + list(map(int, input().split(' ')))
for i in range(len(B_ls) - 1):
    B_ls[i + 1] += B_ls[i]

b_cnt,result = M, 0
for a_cnt in range(N + 1):
    if A_ls[a_cnt] > K:
        break
    while A_ls[a_cnt] + B_ls[b_cnt] > K:
        b_cnt -= 1
    result = max(result, a_cnt + b_cnt)
print(result)

Solution (Python):


import bisect

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[-1]+A[i])


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

ans=0
for i in range(N+1):
    target=K-sum_A[i]
    if target>=0:
        j=bisect.bisect(sum_B,target)
        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()))

T = 0


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

m = a
count = a
num = 0

while a >= 0 and num < M:
    
    while num < M and K-T >= B[num]:
        T += B[num]
        count += 1
        num += 1
    
    m = max(m,count)

    if a == 0:
        break
    T -= A[a-1]
    a -= 1
    count -= 1

print(m)

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

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


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

T = K
ans = 0
num = M-1
for i in range(N):
    T -= A[i]
    if T < 0:
        break
    while P[num] > T and num >= 0:
        num -= 1
    ans = max(ans,i+num+2)
    


for i in range(M):
    if P[i] <= K:
        ans = max(ans,i+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()))
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])

for j in range(n+1):
  K=k-A[j]
  if K<0:
    break
  else:
    ans=max(ans,bisect.bisect_right(B,K)+j-1)
print(ans)

Solution (Python):
from itertools import accumulate as acc
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_acc = [0] + list(acc(A))
B_acc = [0] + list(acc(B))

max_num = 0
for i in range(min(K+1, N+1)):
    
    count_a = i
    time = A_acc[i]
    
    
    count_b = bisect_right(B_acc, K-time)-1
    time += B_acc[count_b]
    
    
    count = count_a + count_b
    if time <= K:
        max_num = max(max_num, count)

print(max_num)

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

for j in range(n+1):
  K=k-A[j]
  if K<0:
    break
  left=0
  right=m+1
  while left<right:
    mid=(left+right)//2
    if B[mid]>K:
      right=mid
    else:
      left=mid+1
  ans=max(ans,j+left-1)
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())))
a = np.cumsum(A)
b = np.cumsum(B)

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

SA = [0]
SB = [0]

tmp_A, tmp_B = 0 ,0

for i in range(N):
    tmp_A += A[i]
    if tmp_A <= K:
        SA.append(tmp_A)
    else:
        break

for i in range(M):
    tmp_B += B[i]
    if tmp_B <= K:
        SB.append(tmp_B)
    else:
        break

ans = 0
cursol_B = len(SB) - 1

for i in range(len(SA)):
    while SA[i] + SB[cursol_B] > K:
        cursol_B -= 1
        
    ans = max(ans, i + cursol_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]
Bsum=[0]
for i, a in enumerate(A):
  Asum.append(Asum[i]+a)
for i, b in enumerate(B):
  Bsum.append(Bsum[i]+b)

ans, b = 0, M
for a, asum in enumerate(Asum):
  if asum > K:
    break
  while Bsum[b]>K - asum:
    b-=1
  ans = max(ans, a+b)
print(ans)

Solution (Python):




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

time_sum = sum(bb)

a_idx = 0
b_idx = m-1
ans = 0


book_num = m
while True:
    if time_sum > k:

        if b_idx < 0:
            break
        time_sum -= bb[b_idx]
        b_idx -= 1
        book_num -= 1
    else:
        
        ans = max(ans, book_num)
        
        if a_idx > n-1:
            break
        time_sum += aa[a_idx]
        a_idx += 1
        book_num += 1

print(ans)

Solution (Python):


import bisect

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


b_cumsum = [0] * (m+1)
for i in range(m):
    b_cumsum[i+1] = b_cumsum[i] + bb[i]

ans = 0
a_sum = 0
b_rest = k

temp = bisect.bisect(b_cumsum, b_rest) - 1
ans = max(ans, temp)

for idx, a in enumerate(aa):
    a_sum += a
    b_rest -= a
    if a_sum > k:
        break
    temp = idx + 1 + bisect.bisect(b_cumsum, b_rest) - 1
    ans = max(ans, temp)

print(ans)

Solution (Python):
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from itertools import accumulate
from bisect import bisect
def main():
    n, m, k = map(int, input().split())
    a = tuple(map(int, input().split()))
    b = tuple(map(int, input().split()))

    aa = [0] + list(accumulate(a))
    ba = [0] + list(accumulate(b))
    r = 0
    for i1, aae in enumerate(aa):
        if aae > k:
            break
        else:
            time_remain = k - aae
            b_num = bisect(ba, time_remain)
            r = max(r, i1 + b_num - 1)
    print(r)

if __name__ == '__main__':
    main()

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

from itertools import accumulate

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

ans = 0
num = b
for i in range(a+1):
    if A[i] > k:
        continue
    while True :
        if A[i] + B[num] > k:
            num -=1
        else:
            ans = max(ans,i+num)
            break
print(ans)

Solution (Python):
n,m,k = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
if sum(A)+sum(B)<=k:
    print(n+m)
    exit()

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

from bisect import bisect_right
ans = bisect_right(B,k)
for i in range(n):
    if A[i]>k:
        break
    ans = max(ans, i+1+bisect_right(B,k-A[i]))
print(ans)

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

a_sum = [0]
b_sum = [0]
for i in range(n):
  a_sum.append(a_sum[i] + a_list[i])
for i in range(m):
  b_sum.append(b_sum[i] + b_list[i])
 
ans = 0
for i in range(n+1):
  if a_sum[i] > k:
    break
        
  rem = k - a_sum[i]
  while b_sum[m] > rem:
    m -= 1
  if ans < i + m:
    ans = i + m
        
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()))
P, Q = [0]*(N+1), [0]*(M+1)
for i in range(N):
  P[i+1] = P[i]+A[i]
for i in range(M):
  Q[i+1] = Q[i]+B[i]
j, result = M, 0
for i in range(N+1):
  if P[i]>K:
    break
  if K-P[i]>=Q[j]:
    result = max(result, i+j)
  else:
    while 0<j and K-P[i]<Q[j-1]: j -= 1
    result = max(result, i+j-1)
print(result)

Solution (Python):
import itertools
N,M,K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
A_=[0]+list(itertools.accumulate(A))
B_=[0]+list(itertools.accumulate(B))

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

Solution (Python):
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
from math import sqrt
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul, itemgetter
from functools import reduce
from bisect import bisect_left, bisect_right

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





N, M, K = getNM()
A = [0] + getList()
B = [0] + getList()

total = K
b_left = sum(B)
l = M

ans = 0

for r in range(N + 1):
    total -= A[r]
    
    if total < 0:
        continue
    
    while total < b_left:
        b_left -= B[l]
        l -= 1
    ans = max(ans, l + r)
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]
for i in range(len(a)):
  a_.append(a_[-1]+a[i])
b_=[0]
for i in range(len(b)):
  b_.append(b_[-1]+b[i])
ans=[0]
a_now=0
b_now=m
for i in range(n+1):
  k_=k-a_[i]
  while b_[b_now]>k_:
    b_now+=-1
    if b_now==-1:
      b_now=0
      break
  if a_[i]+b_[b_now]<=k:
    ans.append(i+b_now)
print(max(ans))

Solution (Python):
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")

n,m,k =I()
a = [0]+list(itertools.accumulate(l()))
b = [0]+list(itertools.accumulate(l()))

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

def binary_serch(x:int, b_sum:list):
    l = 0
    r = len(b_sum) - 1
    mid = (l + r + 1) // 2
    res = mid
    while True:
        if x == b_sum[mid]:
            return mid
        elif x < b_sum[mid]:
            res = mid - 1
            r = mid
            mid = (l + r + 1) // 2
        elif b_sum[mid] < x:
            res = mid 
            l = mid
            mid = (l + r + 1) // 2
        if l + 1 == r:
            return res

n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
b_sum = [0] * (m + 2)
for i in range(m):
    b_sum[i + 1] = b[i] + b_sum[i]

b_sum[-1] = 10 ** 9 + 1
a = [0] + a
pass_k = 0
ans = 0
for i in range(n + 1):
    if pass_k + a[i] <= k:
        pass_k += a[i]
        book = i
        book += binary_serch(k - pass_k, b_sum)
        ans = max(ans, book)
    else:
        break

print(ans)

Solution (Python):
import typing

def binary_serch(x:int, b_sum:list):
    l = 0
    r = len(b_sum) - 1
    mid = (l + r + 1) // 2
    res = mid
    while True:
        if x == b_sum[mid]:
            return mid
        elif x < b_sum[mid]:
            res = mid - 1
            r = mid
            mid = (l + r + 1) // 2
        elif b_sum[mid] < x:
            res = mid 
            l = mid
            mid = (l + r + 1) // 2
        if l + 1 == r:
            return res

n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
b_sum = [0] * (m + 2)
for i in range(m):
    b_sum[i + 1] = b[i] + b_sum[i]

b_sum[-1] = 10 ** 9 + 1
a = [0] + a
pass_k = 0
ans = 0
for i in range(n + 1):
    if pass_k + a[i] <= k:
        pass_k += a[i]
        book = i
        book += binary_serch(k - pass_k, b_sum)
        ans = max(ans, book)
    else:
        break

print(ans)

Solution (Python):
N,M,K = map(int, input().split())
As = [int(n) for n in input().split()]
Bs = [int(n) for n in input().split()]

a_sum = [0]
b_sum = [0]

for i in range(len(As)):
    a_sum.append(a_sum[-1] + As[i])

for j in range(len(Bs)):
    b_sum.append(b_sum[-1] + Bs[j])

 
max_book_num = 0
j = M
for i in range(N+1):
    if a_sum[i] > K:
        continue

    while b_sum[j] + a_sum[i] > K:
        j -= 1
    
    max_book_num = max(max_book_num, i + j)

        
print(max_book_num)

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

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

ans = 0
j = M
for i in range(N + 1):
    if a[i] > K:
        break

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

print(ans)
exit()

Solution (Python):
N,M,K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
sumA=[0]
for i in range(N):
  sumA.append(sumA[-1]+A[i])
sumB=[0]
for i in range(M):
  sumB.append(sumB[-1]+B[i])

ans=0
for i in range(N+1):
  k=K-sumA[i]
  if k<0:
    break
  left=0
  right=M
  while right>=left:
    mid=(right+left)//2
    if sumB[mid]>k:
      right=mid-1
    else:
      left=mid+1
  l=right
  ans=max(ans,i+l)
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()))
sumA=[0]
for i in range(N):
  sumA.append(sumA[-1]+A[i])
sumB=[0]
for i in range(M):
  sumB.append(sumB[-1]+B[i])

ans=0
for i in range(N+1):
  k=K-sumA[i]
  if k<0:
    break
  l=bisect.bisect_right(sumB,k)-1
  ans=max(ans,i+l)
print(ans)

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

a_num = 0
b_num = 0
book_num = 0
passed_k = 0
for i in range(n):
    if a[i] + passed_k <= k:
        a_num += 1
        passed_k += a[i]
    else:
        break
for i in range(m):
    if b[i] + passed_k <= k:
        b_num += 1
        passed_k += b[i]
    else:
        break
book_num = a_num + b_num

while a_num > 0:
    passed_k -= a[a_num - 1]
    a_num -= 1
    while b_num < m:
        if passed_k + b[b_num] <= k:
            passed_k += b[b_num]
            b_num += 1
        else:
            break
    book_num = max(book_num, a_num + b_num)
    if b_num == m:
        break

print(book_num)

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

Asums = [0] * (N + 1)
for i, a in enumerate(A):
    Asums[i+1] = Asums[i] + a

Bsums = [0] * (M + 1)
for i, b in enumerate(B):
    Bsums[i+1] = Bsums[i] + b

ans = 0
for anum in range(N + 1):
    t = Asums[anum]
    if K < t:
        break
    bnum = bisect.bisect_right(Bsums, K - t) - 1
    ans = max(ans, anum + bnum)
print(ans)

Solution (Python):
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
la = [0]
lb = [0]
mx = 0
for i in range(n): 
    if la[i] + a[i] > k:
        break
    la.append(la[i]+a[i])
for j in range(m):
    if lb[j] + b[j] > k:
        break
    lb.append(lb[j]+b[j])
now = len(lb)-1
for l in range(len(la)):
    cnt = l
    q = now
    while la[l] + lb[q] > k:
        q -= 1
    now = q
    cnt += q
    if cnt > mx:
        mx = cnt
print(mx)

Solution (Python):
from bisect import bisect
from collections import deque

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]
A.insert(0, 0)
indexB = len(B) - 1
results = [0]
for i in range(len(A)):
    time = A[i]
    if time > K:
        break
    enable_for_b = K - time
    results.append(i + bisect(B, enable_for_b))

print(max(results))

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]

ans = 0
b_cnt = M
for a_cnt in range(N + 1):
    if A[a_cnt] > K:
        continue
    while A[a_cnt] + B[b_cnt] > K:
        b_cnt -= 1
    ans = max(ans, a_cnt + b_cnt)

print(ans)

Solution (Python):
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
from functools import reduce
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

def is_ok(arg):
    return a[i] + b[arg] <= k

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

n, m, k = MAP()
a = [0] + list(accumulate(LIST()))
b = [0] + list(accumulate(LIST()))

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

if __name__ == "__main__":
    main()

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

asum = [0]
bsum = [0]

for i, new in enumerate(alst):
    if asum[i] + new <= k:
        asum.append(asum[i] + new)
    else:
        break
for i, new in enumerate(blst):
    if bsum[i] + new <= k:
        bsum.append(bsum[i] + new)
    else:
        break

bmax = len(bsum)-1
ans = 0

for i, atime in enumerate(asum):
    while atime + bsum[bmax] > k:
        bmax -= 1
    ans = max(ans, i+bmax)

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 i in range(n + 1):
    rem = k - a_acc[i]
    if rem < 0:
        break

    j = bisect.bisect_right(b_acc, rem)
    ans = max(ans, i + j)

print(ans)

Solution (Python):
from itertools import groupby, accumulate, product, permutations, combinations
def solve():
    ans = 0
    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))
    b = M
    for a in range(N+1):
        if A[a]>K:
            break
        while A[a]+B[b]>K:
            b -= 1
        ans = max(a+b,ans)
    return ans
print(solve())

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

Solution (Python):
N, M, K = map(int, input().split(' '))
A_ls = [0] + list(map(int, input().split(' ')))
for i in range(N):
    A_ls[i + 1] += A_ls[i]
B_ls = [0] + list(map(int, input().split(' ')))
for i in range(M):
    B_ls[i + 1] += B_ls[i]

a_cnt, b_cnt, rst = 0, M, 0
for a_cnt in range(N + 1):
    if A_ls[a_cnt] > K:
        break
    while A_ls[a_cnt] + B_ls[b_cnt] > K and b_cnt > 0:
        b_cnt -= 1
    rst = max(rst, a_cnt + b_cnt)
print(rst)

Solution (Python):
from bisect import bisect_right

def bisectsearch_right(L,a):
    i=bisect_right(L,a)
    return(i)

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

Asum=[0]*N
Bsum=[0]*M
Asum[0]=A[0]
Bsum[0]=B[0]
for i in range(1,N):
    Asum[i]=Asum[i-1]+A[i]
for j in range(1,M):
    Bsum[j]=Bsum[j-1]+B[j]


ans=[0]*(N+1)
ans[0]=bisectsearch_right(Bsum,K)

for i in range(1,N+1):
    if Asum[i-1]>K:
        continue
    j=bisectsearch_right(Bsum,K-Asum[i-1])
    
    ans[i]=i+j

print(max(ans))

Solution (Python):
def solve():
    N,M,K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    ai = 0
    bi = 0
    s = 0

    ans = 0
    for i, a in enumerate(A):
        if s + a > K:
            ai = i-1
            ans = i
            break
        s += a
    else:
        ans = len(A)
        ai = len(A)-1

    
    while ai >= -1:
        while bi < len(B):
            if s + B[bi] > K:
                break
            s += B[bi]
            bi += 1
        ans = max(ans, ai+1+bi)
        if ai != -1:
            s -= A[ai]
        ai -= 1

    print(ans)

solve()

Solution (Python):


import math

import itertools
from collections import deque
from collections import Counter
import heapq


import bisect
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
ar=[0]*(n+1)
br=[0]*(m+1)
ans=0

for i in range(n):
    ar[i+1]=ar[i]+a[i]
for i in range(m):
    br[i+1]=br[i]+b[i]
for i in range(m+1):
    sup=k-br[i]
    if sup<0:
      break
    res=bisect.bisect_right(ar,sup)+i-1
    ans=max(res,ans)
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
    else:
        while k - A[i] < B[j]:
            j -= 1

    ans = max(ans, i+j)

print(ans)