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()))
asum=[0]
bsum=[0]
current=0
for i in range(n):
  current+=a[i]
  asum.append(current)
current=0
pointer=0
for i in range(m):
  current+=b[i]
  bsum.append(current)
for i in range(n+1):
  if asum[i]>k:
    pointer=i-1
    break
  if i==n:
    pointer=n
ans=pointer
bpointer=0
for i in range(pointer,-1,-1):
  while bpointer<m+1 and bsum[bpointer]+asum[i]<=k:
    bpointer+=1
    if bpointer==m+1:
      break
  bpointer-=1
  ans=max(ans,i+bpointer)
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]

gen=0
for i in range(n):
    gen+=a[i]
    sa.append(gen)

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


ans=0

for i in range(n+1):
    if(sa[i]>k):
        break
    basho=bisect.bisect_right(sb,k-sa[i])-1
    if(ans<i+basho):
        ans=i+basho

print(ans)

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

s = 0
num = 0
for i in range(n):
    if s + a[i] > k:
        break
    s += a[i]
    num += 1
an = num

bi = 0
while True:
    if s + b[bi] > k:
        break
    s += b[bi]
    num += 1
    bi += 1
    if bi == m:
        break
    
num_max = num
for i in range(an)[::-1]:
    if bi == m:
        break
    s -= a[i]
    num -= 1
    while True:
        if s + b[bi] > k:
            break
        s += b[bi]
        num += 1
        bi += 1
        if bi == m:
            break
        
    num_max = max([num, num_max])
print(num_max)

Solution (Python):

import bisect
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
a.append(float("INF"))
b.append(float("INF"))
la = [a[0]]
lb = [b[0]]
for i in range(1,n):
    la.append(la[-1]+a[i])
for i in range(1,m):
    lb.append(lb[-1]+b[i])
ans = 0
for i in range(n):
    if la[i] > k:
        break
    x = k-la[i]

    count = i+1
    ind = bisect.bisect_right(lb,x)
    count += ind
    ans = max(ans,count)
for i in range(m):
    if lb[i] > k:
        break
    x = k-lb[i]

    count = i+1
    ind = bisect.bisect_right(la,x)
    count += ind
    ans = max(ans,count)
print(ans)

Solution (Python):
def is_ok(a, b, k, val):
    for i in range(val + 1):
        a_val = i
        b_val = val - a_val
        if a_val > len(a) or b_val > len(b):
            continue
        ret = 0
        if a_val > 0:
            ret += a[a_val-1]
        if b_val > 0:
            ret += b[b_val-1]
        if ret <= k:
            return True
    return False


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]

ok = 0
ng = 400001
while ng - ok > 1:
    mid = (ok + ng) // 2
    if is_ok(a, b, k, mid):
        ok = mid
    else:
        ng = mid

print(ok)

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]*N
A[0]=a[0]
ans=0
for i in range(N-1):
 A[i+1]=A[i]+a[i+1]
B=[0]*M
B[0]=b[0]
for i in range(M-1):
 B[i+1]=B[i]+b[i+1]
for i in range(N):
 if A[i]<=K:
  ans=max(bisect.bisect_right(B,K-A[i])+i+1,ans)
ans=max(bisect.bisect_right(B,K),ans)
print(ans)

Solution (Python):

from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right,bisect_left
from itertools import accumulate

def main():
    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 = list(accumulate(B))
    
    ans = 0
    for i in range(N+1):
        if K - A[i] >= 0:
            j = bisect_right(B,K-A[i])
            
            ans = max(ans,i+j)

    print(ans)
        


if __name__ == "__main__":
    main()

Solution (Python):
N,M,K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
la=[0]
lb=[0]
t=0
g=N
for i in range(N):
    t+=A[i]
    la.append(t)
    if t>K:
        g=i
        break
t=0
for i in B:
    t+=i
    lb.append(t)
a=g
s=0
for i in range(g,-1,-1):
    while s<M:
        s+=1
        if K<la[i]+lb[s]:
            s-=1
            break
    if a<i+s:
        a=i+s
print(a)

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

AR=[0]
for i in range(N):
    AR.append(AR[-1]+A[i])
AR+=[10**10]
BR=[0]
for i in range(M):
    BR.append(BR[-1]+B[i])
BR+=[10**10]



import bisect

ans=0
for a in range(len(AR)):
    if AR[a]>K:
        break
    cnt=a
    cnt+=bisect.bisect_right(BR,K-AR[a])-1
    if ans<cnt:
        ans=cnt
    
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()))



Cum_B = []
yoryoku = [K]



na = K
for i in range(len(A)):
    na -= A[i]
    yoryoku.append(na)
    

nb = 0
for j in range(len(B)):
    nb += B[j]
    Cum_B.append(nb)




    


ans = 0
for i in range(len(yoryoku)):
    r = yoryoku[i]
    if(r < 0):
        break
    
    ans = max(ans , i+bisect.bisect_right(Cum_B, r))
print(ans)

Solution (Python):
from collections import deque
import bisect
n,m,k = map(int, input().split())
a = [0]
b = [0]
a1 = list(map(int, input().split()))
b1 = list(map(int, input().split()))


ans = 0
a = a + a1
b = b + b1

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

for i in range(n+1):
    now = k-a[i]
    if now<0:
        break
    idx = bisect.bisect_right(b,now)
    ans = max(ans,idx-1+i)

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

Solution (Python):
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
numa = sum(a)
numb = 0
cntb = 0
ans = 0
for i in range(n+1):
    if numa <= k:
        while cntb < m and numa + b[cntb] <= k:
            numa += b[cntb]
            cntb += 1
        ans = max(ans,n-i+cntb)
    if i != n:
        numa -= a[-(i+1)]
print(ans)

Solution (Python):
import bisect
from itertools import accumulate

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

C = list(accumulate(A))
D = list(accumulate(B))

x = bisect.bisect_right(C, K)
cnt = K - C[x -1]
y = bisect.bisect_right(D, cnt)
ans = x + y

for i in range(x)[::-1]:
    cnt += A[i]
    y = bisect.bisect_right(D,cnt)
    ans = max(ans, i + y)

print(ans)

Solution (Python):
import sys
read = sys.stdin.read
readline = sys.stdin.buffer.readline
import numpy as np
from bisect import bisect_right
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7


def main():
    N, M, K = map(int, readline().split())
    A = [0] + list(map(int, readline().split()))
    B = list(map(int, readline().split()))

    A = np.array(A, np.int64)
    B = np.array(B, np.int64)

    A = A.cumsum()
    B = B.cumsum()
    ans = 0

    for i in range(N+1):
        a = A[i]
        rest = K - a
        if rest<0:
            break
        result = bisect_right(B,rest)
        ans = max(ans,result+i)

    print(ans)



if __name__ == '__main__':
    main()

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

A_acc=[0]
B_acc=[0]

for a in A:
    A_acc.append(a+A_acc[-1])
for b in B:
    B_acc.append(b+B_acc[-1])

ans=0
for i in range(len(A_acc)):
    a_time = A_acc[i]
    remain = K-a_time
    if remain>=0:
        idx = bisect.bisect_right(B_acc,remain)
        if idx==0:
            continue
        ans=max(ans,i+idx-1)
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_T = [0]
B_T = [0]
c = 0
for i in range(N):
    c += A[i]
    A_T.append(c)
c = 0
for i in range(M):
    c += B[i]
    B_T.append(c)

ans = 0
for i in range(N+1):
    if K - A_T[i] >= 0:
        k = bisect.bisect_right(B_T, K-A_T[i])
        ans = max(ans, k+i-1)
print(ans)

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

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

accumA = [0] + list(accumulate(A))
accumB = list(accumulate(B))
ans = 0
for i in range(N + 1):
    if accumA[i] > K:
        break
    n = i + bisect(accumB, K - accumA[i])
    ans = max(n, ans)
print(ans)

Solution (Python):
N, M, K = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]+[10**18]
B = [int(x) for x in input().split()]+[10**18]

A_total = 0
i = 0
while A_total+A[i] <= K:
	A_total += A[i]
	i += 1

ans = i
B_total = 0
for j in range(M):
	B_total += B[j]
	if B_total > K:
		break
	while A_total+B_total > K:
		i -= 1
		A_total -= A[i]
	ans = max(ans,i+(j+1))
print(ans)

Solution (Python):
import sys
from bisect import bisect

def solve():
      input = sys.stdin.readline
      N, M, K = map(int, input().split())
      A = [int(a) for a in input().split()]
      B = [int(b) for b in input().split()]
      AS = [0] * (N + 1)
      BS = [0] * (M + 1)
      for i in range(N):
            AS[i+1] = AS[i] + A[i]
      for i in range(M):
            BS[i+1] = BS[i] + B[i]
      
      maxCount = 0
      for i in range(N + 1):
            if AS[i] > K: break

            bcount = bisect(BS, K - AS[i])
            maxCount = max(maxCount, i + bcount - 1)
            
      print(maxCount)


      return 0

if __name__ == "__main__":
      solve()

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

N, M, K = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a = list(accumulate(a))
b = list(accumulate(b))
ans = bisect(b, K)
for i in range(N):
    rest = K - a[i]
    if rest < 0:
        continue
    else:
        pre = bisect(b, rest)
        ans = max(ans, i+1+pre)
print(ans)

Solution (Python):

import sys
input = sys.stdin.readline
INF = 10**12
import bisect

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

acum = [0]
for item in a:
    acum.append(acum[-1] + item)
bcum = [0]
for item in b:
    bcum.append(bcum[-1] + item)

ans = 0
for i, item in enumerate(acum):
    if item > k:
        continue
    index = bisect.bisect_right(bcum, k - item) - 1
    num = i + index
    ans = max(ans, num)
print(ans)

Solution (Python):
from bisect import bisect_right

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

sum_a = [0]
for v in a:
    sum_a.append(sum_a[-1] + v)

sum_b = [0]
for v in b:
    sum_b.append(sum_b[-1] + v)

x = bisect_right(sum_a, k)

res = 0
for i in range(x):
    res = max(res, bisect_right(sum_b, k - sum_a[i]) + i - 1)
print(res)

Solution (Python):
from itertools import accumulate
import bisect
n, m, k = map(int, input().split())
Acum = [0]+list(accumulate(map(int, input().split())))
Bcum = [0]+list(accumulate(map(int, input().split())))

ans = 0
for a in range(n+1):
    if k-Acum[a] < 0:
        break
    b = bisect.bisect_right(Bcum, k-Acum[a])-1
    if ans < a+b:
        ans = a+b
print(ans)

Solution (Python):
import numpy as np
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(' ')]
cA = np.cumsum([0] + A)
cB = np.cumsum([0] + B)
ans = 0
for nA in range(len(cA)):
  if K < cA[nA]:
    break
  time_left = K - cA[nA]
  nB = np.searchsorted(cB, time_left, side='right') - 1
  
  ans = max(ans, nA + nB)
print(ans)

Solution (Python):
import bisect


def main():
    N, M, K = list(map(int, input().split(' ')))
    A = list(map(int, input().split(' ')))
    B = list(map(int, input().split(' ')))
    cumsum_A = [0] * (N + 1)
    for i, a in enumerate(A):
        cumsum_A[i + 1] = a + cumsum_A[i]
    cumsum_B = [0] * (M + 1)
    for i, b in enumerate(B):
        cumsum_B[i + 1] = b + cumsum_B[i]
    
    ans = 0
    for n in range(N + 1):
        if cumsum_A[n] > K:
            break
        m = bisect.bisect_right(cumsum_B, K - cumsum_A[n])
        m -= 1
        if m < 0:
            continue
        ans = max(ans, m + n)
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
from collections import defaultdict, Counter, namedtuple, deque
import itertools
import functools
import bisect
import heapq
import math


MOD = 10 ** 9 + 7




n, m, k = map(int, input().split())
A = [0] + list(map(int, input().split()))
B = [0] + list(map(int, input().split()))

rA = list(itertools.accumulate(A))
rB = list(itertools.accumulate(B))


ia = bisect.bisect(rA, k)-1
ib = 0
while ib+1 < len(B) and rA[ia]+rB[ib+1] <= k:
    ib += 1

vmax = ia+ib

for i in reversed(range(ia)):
    while ib+1 < len(B) and rA[i] + rB[ib+1] <= k:
        ib += 1

    
    vmax = max(vmax, i+ib)

print(vmax)

Solution (Python):
import sys
sys.setrecursionlimit(10**8)
input = sys.stdin.readline
import bisect

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

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

ans = 0
for i in range(N+1):
    num_A = i
    time = K - A_cum[i]
    if time<0:
        continue
    num_B = bisect.bisect_right(B_cum, time)
    num_B = max(0, num_B-1)
    ans = max(ans, num_A + num_B)
print(ans)

Solution (Python):
ma = lambda :map(int,input().split())
ni = lambda:int(input())
import collections
import math
import itertools
gcd = math.gcd
def isok(x,y):
    if x+y<=k:
        return True
    else:
        return False

def bisect(ls,val): 
    ok = -1
    ng = len(ls)
    x = (ok+ng)//2
    while ng-ok>1:
        num = ls[x]
        if isok(num,val):
            ok = x
        else:
            ng = x
        x = (ok+ng)//2
    return ok 


n,m,k = ma()
A  = list(ma())
B = list(ma())
la = [0]*(n+1)
lb = [0]*(m+1)
for i in range(n):
    la[i+1] = la[i] + A[i]
for i in range(m):
    lb[i+1] = lb[i] + B[i]

ans = 0
for i in range(n+1):
    if la[i] >k:break
    idx = bisect(lb,la[i])
    ans = max(ans,idx+i)
print(ans)

Solution (Python):
import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())  
def LS2(): return list(sys.stdin.readline().rstrip())  

N,M,K = map(int,S().split())
A = LI()
A.reverse()
B = LI()
C = A+B

a = 0
b = N-1
r = sum(A)
ans = 0
while a <= N:
    if r > K:
        a += 1
        r -= C[a-1]
    else:
        ans = max(ans,b-a+1)
        b += 1
        if b < len(C):
            r += C[b]
        else:
            break

print(ans)

Solution (Python):
import sys
import math
import heapq
import bisect
sys.setrecursionlimit(10**7)
INTMAX = 9223372036854775807
INTMIN = -9223372036854775808
DVSR = 1000000007
def POW(x, y): return pow(x, y, DVSR)
def INV(x, m=DVSR): return pow(x, m - 2, m)
def DIV(x, y, m=DVSR): return (x * INV(y, m)) % m
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def FLIST(n):
    res = [1]
    for i in range(1, n+1): res.append(res[i-1]*i%DVSR)
    return res

N,M,K = LI()
AS=LI()
BS=LI()
ASC=[0]*(N+1)
BSC=[0]*(M+1)

for i in range(N):
    ASC[i+1] = ASC[i] + AS[i]
for i in range(M):
    BSC[i+1] = BSC[i] + BS[i]




res = 0

for i in range(0, N+1):
    
    rest = K-ASC[i]
    if rest >= 0:
        idx = bisect.bisect_right(BSC, rest)
        
        res = max(res, i+idx-1)
print(res)

Solution (Python):
import sys
import bisect

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


def main():
    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()]

    ruisekia = [0] * N
    ruisekib = [0] * M

    for i, a in enumerate(A):
        ruisekia[i] = ruisekia[i - 1] + a

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

    ruisekia = [0] + ruisekia
    ruisekib = [0] + ruisekib


    ans = 0
    for i in range(N + 1):
        nokori = K - ruisekia[i]
        if nokori == 0:
            ans = max(ans, i)
            continue
        elif nokori <= 0:
            continue

        x = bisect.bisect_right(ruisekib, nokori)
        ans = max(ans, x + i - 1)

    print(ans)





if __name__ == '__main__':
    main()

Solution (Python):
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
sa = [0, a[0]]
sb = [0, b[0]]
s = a[0]
for i in range(1, n):
  s += a[i]
  sa.append(s)
s = b[0]
for i in range(1, m):
  s += b[i]
  sb.append(s)
  
ans = 0
books = 0

from bisect import bisect
a_books = bisect(sa, k) - 1
b_books = bisect(sb, k) - 1
if a_books > b_books:
  for i in range(b_books+1):
    books = i
    books += bisect(sa, k-sb[books]) - 1
    ans = max(ans, books)
else:
  for i in range(a_books+1):
    books = i
    books += bisect(sb, k-sa[books]) - 1
    ans = max(ans, books)
print(ans)

Solution (Python):
N,M,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
import bisect
As = [0]
for i in range(N):
    As.append(As[-1]+A[i])
Bs = [0]
for i in range(M):
    Bs.append(Bs[-1]+B[i])
ans = 0
for i in range(len(As)):
    v = K-As[i]
    if v ==0: ans = max(ans,i)
    elif v >0:
        idx = bisect.bisect_right(Bs,v)
        if idx==0: ans = max(ans,i)
        else: ans = max(ans,i+idx-1)
print(ans)

Solution (Python):
from bisect import bisect_right
from itertools import accumulate
import sys
input = sys.stdin.readline

N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A = list(accumulate(A))
B = list(accumulate(B))
ans = bisect_right(B, K)
for i in range(N):
    if A[i] > K:
        break
    j = bisect_right(B, K - A[i])
    ans = max(ans, i + j + 1)
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()))


cnt = 0
ru_a = [0] * (n + 1)
ru_b = [0] * (m + 1)
for i in range(n):
    ru_a[i + 1] = ru_a[i] + a[i]
for i in range(m):
    ru_b[i + 1] = ru_b[i] + b[i]

ans = 0
for i in range(n + 1):
    if ru_a[i] > k:
        continue
    ind = bisect.bisect_right(ru_b, k - ru_a[i])
    ans = max(i + ind - 1, ans)
print(ans)

Solution (Python):
from bisect import *
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
for i in range(n+1):
    if a[i] > k:
        continue
    ind = bisect_right(b, k-a[i])
    ans = max(ans, ind+i-1)

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=list(accumulate(a))
B=list(accumulate(b))

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

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

ar = [0]*N
br = [0]*M
ar[0] = A[0]
br[0] = B[0]

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

res = bisect.bisect_right(br, K)

for i in range(N):
    if ar[i] <= K:
        res = max(i+bisect.bisect_right(br, K-ar[i])+1, res)

print(res)

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

acc1 = [0]+list(itertools.accumulate(a))
acc2 = list(itertools.accumulate(b))

ans = 0
for i in range(n+1):
    num = i
    temp = acc1[i]
    rest = k-temp

    if rest >= 0:
        hoge = bisect.bisect_right(acc2,rest)

        num += hoge

        if num > ans:
            ans = num

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

ans=0

time=0
book=0

for i in range(n):
    if A[i]+time<=k:
        time+=A[i]
        book+=1
    else:
        break

ans=max(ans,book)
abook=book
bnow=0

for i in range(book+1):
    
    if abook+1==i:
        break
    if bnow>=m:
        break
    while time+B[bnow]<=k:
        time+=B[bnow]
        bnow+=1
        book+=1
        if bnow>=m:
            break
    ans=max(ans,book)
    time-=A[abook-i-1]
    book-=1

print(ans)

Solution (Python):
from bisect import bisect_right

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

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

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

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

print(ans)

Solution (Python):
def main():
    N, M, K = map( int, input().split())
    A = list( map( int, input().split()))
    B = list( map( int, input().split()))
    indb = M-1
    now = sum(B)
    count = M
    while now > K:
        now -= B[indb]
        count -= 1
        indb -= 1

    ans = count
    for a in A:
        
        now += a
        count += 1
        while now > K and indb >= 0:
            now -= B[indb]
            count -= 1
            indb -= 1
        if now <= K:
            if ans < count:
                ans = count
    print(ans)
if __name__ == '__main__':
    main()

Solution (Python):
import bisect
N, M, K = [int(_) for _ in input().split()]
A = [int(_) for _ in input().split()]
B = [int(_) for _ in input().split()]

A_sum = [0]
a_sum = 0
for a in A:
  a_sum += a
  A_sum.append(a_sum)
B_sum = [0]
b_sum = 0
for b in B:
  b_sum += b
  B_sum.append(b_sum)

ans = 0
for i in range(N+1):
  b_time = K - A_sum[i]
  if b_time < 0:
    break
  idx = bisect.bisect_right(B_sum, b_time)
  ans = max(ans, i + idx-1)
print(ans)

Solution (Python):
import bisect
def main():
    N, M, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    bt = []
    at = []
    at.append(0)
    bt.append(0)
    for i in range(M):
        bt.append(bt[-1] + B[i])
    for i in range(N):
        at.append(at[-1] + A[i])
    m = 0
    for i in range(N+1):
        t = K - at[i]
        if t < 0:
            break
        j = bisect.bisect_right(bt, t)
        m = max(m, i+j-1)
    return m
print(main())

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

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

ans = 0
for i in range(N + 1):
    res = K - x[i]
    if res > 0:
        j = bisect_right(y, res)
        j -= 1
    elif res == 0:
        j = 0
    else:
        continue

    ans = max(ans, i + j)
print(ans)

Solution (Python):
from collections import deque
from bisect import bisect_left, bisect_right

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

al = list(map(int, input().split()))
bl = list(map(int, input().split()))

al_sums = [0]*(n+1)
bl_sums = [0]*(m)

curr_sum = 0
for i,a in enumerate(al):
    curr_sum += a
    al_sums[i+1] = curr_sum

curr_sum = 0
for i,b in enumerate(bl):
    curr_sum += b
    bl_sums[i] = curr_sum





ans = 0
for i in range(n+1):
    a_sum = al_sums[i]
    if a_sum > k: continue
    cnt = bisect_right(bl_sums, k-a_sum)
    curr_ans = i+cnt
    
    ans = max(ans,curr_ans)

print(ans)

Solution (Python):


import sys

class CTsundoku:
    def solve(self, tc=0):
        from bisect import bisect_right
        n, m, k = [int(_) for _ in input().split()]
        a = [int(_) for _ in input().split()]
        b = [int(_) for _ in input().split()]
        prea = [0] * (n + 1)
        preb = [0] * (m + 1)
        for i in range(1, n + 1):
            prea[i] = prea[i - 1] + a[i - 1]
        for i in range(1, m + 1):
            preb[i] = preb[i - 1] + b[i - 1]

        ans = 0
        for i in range(n + 1):
            if prea[i] > k:
                break
            j = bisect_right(preb, k - prea[i]) - 1
            ans = max(ans, i + j)

        print(ans)


solver = CTsundoku()
input = sys.stdin.readline

solver.solve()

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 bisect import bisect_right
from bisect import bisect_left
n, m, k = map(int, input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]

aa = [0]
bb = [0]
for x in a:
    aa.append(aa[-1]+x)

for x in b:
    bb.append(bb[-1]+x)

ans = 0
for i in range(len(aa)):
    if aa[i] > k:
        break
    x = bisect_right(bb, k-aa[i])
    ans = max(ans, i+x-1)

print(ans)

Solution (Python):


def ans(A, B, K):
    pA = [0]
    for a in A:
        pA += [pA[-1] + a]
    pB = [0]
    for b in B:
        pB += [pB[-1] + b]

    

    j = len(B)

    ans = float("-inf")

    for i in range(0, len(pA)):
        
        while j > 0 and pA[i] + pB[j] > K: j -= 1
        if pA[i] + pB[j] <= K:
            ans = max(ans, i + j)

    return ans

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

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

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

print(ans(A, B, K))

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

ans = bisect.bisect_right(T,K)
for k in range(N):
    if K-S[k] >= 0:
        ans = max(ans,k+1+bisect.bisect_right(T,K-S[k]))
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]

def bin_isOK(index,key):
    if sB[index] <= key:
        return True
    else:
        return False


def binary_search(key):
    ng = len(sB)
    ok = -1

    while abs(ok - ng) >1:
        mid = (ok + ng)//2

        if bin_isOK(mid,key):
            ok = mid
        else:
            ng = mid
    
    return ok

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

ans=0
for i in range(len(sA)):
    if sA[i]>K:
        break
    j=binary_search(K-sA[i])
    ans=max(ans,i+j)
    
print(ans)

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

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

a = list(accumulate(A))
b = list(accumulate(B))

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

Solution (Python):
import bisect
def resolve():
    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]

    mx = 0
    for co,v in enumerate(A):
        if v > K:
            break
        cb = bisect.bisect_right(B,K-v)
        
        mx = max(mx, cb+co-1)
    print(mx)
resolve()

Solution (Python):
from itertools import accumulate
import sys
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()))))

tmp=m
ans=0
for i in range(n+1):
    while a[i]+b[tmp]>k:
        if tmp>0:tmp-=1
        else:
            print(ans)
            sys.exit()
    ans=max(ans,i+tmp)

print(ans)

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

    m = 0
    k = 0
    ia, ib = 0, 0
    while ia < N:
        a = A[ia]
        if k + a > K:
            break
        k += a
        ia += 1

    while True:
        while ib < M:
            b = B[ib]
            if k + b > K:
                break
            k += b
            ib += 1
        m = max(m, ia + ib)
        if ia > 0:
            ia -= 1
            k -= A[ia]
        else:
            break

    return m

print(main())