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):
import bisect
import itertools
n,m,k=map(int,input().split())
A=list(itertools.accumulate([0]+list(map(int,input().split()))))
B=list(itertools.accumulate([0]+list(map(int,input().split()))))

ans=0
for i,a in enumerate(A):
    if a>k:
        break
    cnt=bisect.bisect_right(B,k-a)-1
    ans=max(ans,i+cnt)
print(ans)

Solution (Python):
N, M, K = map(int, input().split())
As = list(map(int, input().split()))
Bs = list(map(int, input().split()))
ans = 0
j = M
a, b = [0], [0]

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

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

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

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

sum_alist=[0]
for i in range(N):
  sum_alist.append(sum_alist[-1]+alist[i])


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


limit_a=bisect.bisect(sum_alist,K)


limit_b=bisect.bisect(sum_blist,K)


answer=0
for i in reversed(range(limit_a)):
  a_sum=sum_alist[i]
  b_sum=K-a_sum
  pos_b=bisect.bisect(sum_blist,b_sum)
  
  answer=max(answer,i+pos_b-1)
  
if limit_a==1:
  pos_b=bisect.bisect(sum_blist,K)
  answer=pos_b-1
  
print(answer)

Solution (Python):

from collections import deque

def main():
    n, m, k = map(int, input().split())
    a_lst = list(map(int, input().split()))
    b_lst = list(map(int, input().split()))
    if n < m:
        a_lst, b_lst = b_lst, a_lst
        n, m = m, n

    a_time = [0]
    b_time = [0]

    t = 0
    for a in a_lst:
        t += a
        if t > k:
            break
        a_time.append(t)
    t = 0
    for b in b_lst:
        t += b
        if t > k:
            break
        b_time.append(t)

    ret = 0
    j = len(b_time) - 1
    for i, a in enumerate(a_time):
        while a + b_time[j] > k:
            j -= 1
        ret = max(ret, i+j)

    print(ret)

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

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

    ret = max(i+j, ret)


print(ret)

Solution (Python):
from collections import deque, defaultdict, Counter
N, M, K = (int(i) for i in input().split())  
A = list(map(int, input().split()))
B = list(map(int, input().split()))
CA = [0]
CB = [0]
for i in range(N):
  CA.append(CA[i] + A[i])
  
for i in range(M):
  CB.append(CB[i] + B[i])

r = M
ans = 0
for i in range(N+1):
  if CA[i] > K:
    break
    
  while CA[i] + CB[r] > K:
    r -= 1  
  ans = max(ans, i + 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] * (n+1)
B = [0] * (m+1)
for i in range(n):
    A[i+1] = A[i] + a[i]
for i in range(m):
    B[i+1] = B[i] + b[i]

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

Solution (Python):
from itertools import accumulate
from bisect import bisect_right
N,M,K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
b=list(accumulate(B))
book=bisect_right(b,K)
for i,a in enumerate(accumulate(A),1):
    if a>K:
        break
    j=bisect_right(b,K-a)
    book=max(book,i+j)
print(book)

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

A_cumsum=[0]*(N+1)
B_cumsum=[0]*(M+1)


a_cumsum=0
for n in range(N):
    a_cumsum=a_cumsum+A[n]
    A_cumsum[n+1] = a_cumsum
        
b_cumsum=0
for m in range(M):
    b_cumsum=b_cumsum+B[m]
    B_cumsum[m+1] = b_cumsum
    

ans = []
for n in range(N+1):
    a=A_cumsum[n]
    K_=K-a
    
    if K_< 0:
        break

    i=0
    left=0
    right=len(B_cumsum)-1

    while i < 100:
        i+=1
        if left >= right:
            break
        half = int((left+right)/2)
        if B_cumsum[half] < K_:
            left = half
        else:
            right=half

    for l in range(left,right+1):
        if B_cumsum[l] > K_:
            l=l-1
            break
    
    ans.append(n+l)
print(max(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 = [int(x) for x in input().split(' ')]
A = [int(x) for x in input().split(' ')]
B = [int(x) for x in input().split(' ')]





A_deep = 0
B_deep = 0
time = 0
while A_deep < N:
  if K >= time + A[A_deep]:
    time += A[A_deep]
    A_deep += 1
  else:
    break 
while B_deep < M:
  if K >= time + B[B_deep]:
    time += B[B_deep]
    B_deep += 1
  else:
    break 

i = A_deep - 1
j = B_deep
ans = i + j + 1
while i >= 0 and j < M:
  while i >= 0:
    time -= A[i]
    i -= 1
    if K - time >= B[j]:
      break 
  while j < M:
    if K >= time + B[j]:
      time += B[j]
      j += 1
      ans = max(ans, i + j + 1)
    else: 
      break 


print(ans)

Solution (Python):
import bisect

n, m, k = map(int, input().split())
a = [int(_) for _ in input().split()]
b = [int(_) for _ in 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 = [0] + a
b = [0] + b

ans = 0

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

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

a_time = [0]
for i in range(len(a)):
  a_time.append(a_time[i] + a[i])

b_time = [0]
for i in range(len(b)):
  b_time.append(b_time[i] + b[i])

b_time.reverse()
cnt = 0
best_j = 0
for i in range(len(a_time)):
  k_dumy = k - a_time[i]
  for j in range(best_j,len(b_time)):
    if b_time[j] <= k_dumy:
      cnt = max(cnt, i + len(b_time)-j-1)
      best_j = j
      break

print(cnt)

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]+A[i]
for i in range(1,m+1):
    B[i]=B[i-1]+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 = list(map(int, input().split(" ")))
a = list(map(int, input().split(" ")))
b = list(map(int, input().split(" ")))

sa = [0]
for i in range(n):
	if (sa[-1] + a[i]) > k:
		break
	sa.append(sa[-1] + a[i])
sb = [0]
for i in range(m):
	if (sb[-1] + b[i]) > k:
		break
	sb.append(sb[-1] + b[i])

r = 0
j = 0
cur = sa[-1]

for i in range(len(sa)-1, -1, -1):
	while j < len(sb):
		if sa[i] + sb[j] > k:
			break
		j += 1
	r = max(r, i + j - 1)
print (r)

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

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

    a_acc, b_acc = A[:], B[:]
    for i in range(1, N):
        a_acc[i] += a_acc[i-1]
    
    for i in range(1, M):
        b_acc[i] += b_acc[i-1]
    
    res = 0
    for i, a in enumerate(a_acc):
        if a > K:
            break
        left = K - a
        j = bisect_right(b_acc, left)
        res = max(res, i + 1 + j)
    
    res = max(res, bisect_right(b_acc, K))  
    print(res)

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

count = 0
ar = [0] * (N + 1)
br = [0] * (M + 1)

for i, v in enumerate(A):
    ar[i+1] = ar[i] + v

for i, v in enumerate(B):
    br[i+1] = br[i] + v

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

print(max_read)

Solution (Python):
from itertools import accumulate

def bs(t):
    l,h = 0,len(b)-1
    while l<=h:
        m = (l+h)//2
        if b[m]<t:
            l=m+1
        elif b[m]>t:
            h=m-1
        else:
            return m
    return h

n,m,k = map(int,input().split())
a = list(accumulate([0]+list(map(int,input().split()))))
b = list(accumulate([0]+list(map(int,input().split()))))
ans = 0
for idx,i in enumerate(a):
    if i<=k:
        
        ans = max(ans,bs(k-i)+idx)
print(ans)

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from functools import reduce
from bisect import bisect_left, insort_left
from heapq import heapify, heappush, heappop

INPUT = lambda: sys.stdin.readline().rstrip()
INT = lambda: int(INPUT())
MAP = lambda: map(int, INPUT().split())
S_MAP = lambda: map(str, INPUT().split())
LIST = lambda: list(map(int, INPUT().split()))
S_LIST = lambda: list(map(str, INPUT().split()))
ZIP = lambda: zip(*(MAP() for _ in range(n)))

sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    N, M, K = MAP()
    A = LIST()  
    B = LIST() 

    
    
    

    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)  


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=[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(K < a[i]):break
  
  while(b[j] > K - a[i]):
    j -= 1
  if(ans < i+j):
    ans = i+j
print(ans)

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

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

    a_acc, b_acc = A[:], B[:]
    for i in range(1, N):
        a_acc[i] += a_acc[i-1]
    
    for i in range(1, M):
        b_acc[i] += b_acc[i-1]
    
    res = 0
    for i, a in enumerate(a_acc):
        if a > K:
            break
        left = K - a
        j = bisect_right(b_acc, left)
        res = max(res, i + 1 + j)
    
    
    res = max(res, bisect_right(b_acc, K))

    print(res)

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









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



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

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

ans = 0
j = m
for i in range(n+1):
    if arui[i] > p:
        break
    bmax = p-arui[i]
    while brui[j] > bmax:
        j -= 1
    if ans <= i+j:
        ans = i+j


print(ans)

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

t = k
i = 0
asum = [0]* (n+1)
bsum = [0]* (m+1)
suma = 0
sumb = 0

for  i in range(n):
    suma += a[i]
    asum[i+1]= suma


for  i in range(m):
    sumb += b[i]
    bsum[i+1] = sumb

count = 1
ans = 0

for i in range(n+1):
    if ans < i :
        if asum[i] <= k:
            ans=i
    else:
        for j in range(ans-i,m+1,1):
            if asum[i] + bsum[j]<=k:
                if i+j>ans:
                    ans=i+j
            else:
                break

print(min(ans,m+n))

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

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

ans = 0
j = M
for i in range(N+1):
    if a[i] > K:
        break
    while b[j] > K - a[i]:
        j = j -1
    ans = max(ans, i+j)
    
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 a in A_list:
    A_sum.append(A_sum[-1] + a)

for b in B_list:
    B_sum.append(B_sum[-1] + b)

count = 0
j = M
for i, a in enumerate(A_sum):
    if a > K:
        break
    while B_sum[j] > K - a:
        j -= 1
    if count < i + j:
        count = i + j

print(count)

Solution (Python):
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
ans=0
aindex=n
bindex=m
acumsum = [0]
bcumsum = [0]

ab =0
for aa in range(n):
  ab += a[aa]
  acumsum.append(ab)
  if ab>k:
    aindex= aa
    break

ab =0
for bb in range(m):
  ab += b[bb]
  bcumsum.append(ab)
  if ab>k:
    bindex= bb
    break

j= bindex
for i in range(aindex+1):
  if acumsum[i] >k:
    break
  while bcumsum[j] > k-acumsum[i]:
    j -=1
  ans = max(ans, i+j)

print(ans)

Solution (Python):
import numpy
import bisect

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

A = numpy.cumsum(numpy.array(A))
A = list(A)
A.insert(0, 0)
B = numpy.cumsum(numpy.array(B))
B = list(B)
B.insert(0, 0)
ans = 0

for i in range(N+1):
    if K - A[i] < 0:
        break
    b = bisect.bisect(B, K - A[i])
    ans = max(ans, i+b-1)

print(ans)

Solution (Python):
import sys


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()


def calc_cumsum(l):
    cumsum = [l[0]]
    for i in l[1:]:
        cumsum.append(cumsum[-1] + i)
    return cumsum


def main():
    n, m, k = LI()
    a = LI()
    b = LI()
    costs = [float('inf')] * (n + m + 1)
    costs[0] = 0
    
    ruiseki_a = [0] + calc_cumsum(a)
    ruiseki_b = [0] + calc_cumsum(b)
    best_b_num = 0  
    for i in range(m + 1):
        if ruiseki_b[i] <= k:
            best_b_num = i

    ans = 0
    j = best_b_num
    for i in range(n + 1):
        
        if ruiseki_a[i] > k:
            break

        
        
        
        while ruiseki_b[j] > k - ruiseki_a[i]:
            j -= 1
        ans = max(ans, i + j)
    print(ans)


main()

Solution (Python):
n,m,k = map(int, input().split(' '))

a=list(map(int, input().split(' ')))
b=list(map(int, input().split(' ')))

ans=0

asums = [0]
asum=0
for i in a:
    asum+=i
    asums.append(asum)

bsums = [0]
bsum=0
for i in b:
    bsum+=i
    bsums.append(bsum)


j=m

i=0

while i<len(asums) and k>=asums[i]:
    while k-asums[i]<bsums[j]:
        j-=1
    ans = max(ans, i+j)
    i+=1
print(ans)

Solution (Python):
import itertools
n, m, k = [int(i) for i in input().split()]
a_list = [int(i) for i in input().split()]
b_list = [int(i) for i in input().split()]

a_acc = [0]
a_acc.extend(list(itertools.accumulate(a_list)))
b_acc = [0]
b_acc.extend(list(itertools.accumulate(b_list)))

ans = 0
j = m
for i in range(n+1):
    if a_acc[i] > k:
        break

    while b_acc[j] > k - a_acc[i]:
        j -= 1

    ans = max(ans, i+j)

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





consumptions_A = [0]
for i in range(len(A)):
    consumptions_A.append(consumptions_A[i] + A[i])

consumptions_B = [0]
for i in range(len(B)):
    consumptions_B.append(consumptions_B[i] + B[i])

answer = 0
j = M
for i in range(len(consumptions_A)):
    if (consumptions_A[i] > K): break

    remaining_time = K - consumptions_A[i]
    while consumptions_B[j] > remaining_time:
        j -= 1
    
    num_books = i + j
    answer = max(answer, num_books)

print(answer)

Solution (Python):
from itertools import accumulate


def binary(N, T):  
    l, r = -1, N
    while r - l > 1:
        if T < b[(l + r) // 2]:  
            r = (l + r) // 2
        else:
            l = (l + r) // 2
    return r - 1


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

ans = 0
for i, ai in enumerate(a):
    t = k - ai
    if t >= 0:
        j = binary(m + 1, t)
        ans = max(ans, i + j)
print(ans)

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

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


def solve():
    N, M, K = map(int, rl().split())
    A = list(map(int, rl().split()))
    B = list(map(int, rl().split()))
    
    accA = [0] + list(accumulate(A))
    accB = list(accumulate(B))
    ans = 0
    for i, v in enumerate(accA):
        if K < v:
            break
        ans = max(ans, i + bisect_right(accB, K - v))
    print(ans)


if __name__ == '__main__':
    solve()

Solution (Python):

import bisect
import numpy

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

cost_a = numpy.insert(numpy.cumsum(a), 0, 0)
cost_b = numpy.insert(numpy.cumsum(b), 0, 0)

ans = 0
j =  len(cost_b) -1
for i in range(len(cost_a)):
    for j in range(j, -1, -1):
        if cost_a[i] + cost_b[j] <= k:
            ans  = i + j if ans < i + j else ans
            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):
if __name__ == "__main__":
    N, M, K = list(map(int, input().split()))
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    ans, j = 0, M
    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])
    for i in range(N+1):
        if a[i] > K:
            break
        else:
            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 = tuple(map(int, input().split()))
B = tuple(map(int, input().split()))
from itertools import accumulate
AS = [0] + list(accumulate(A))
BS = [0] + list(accumulate(B))
 
ans = 0
j = m
for i in range(n+1):
    if AS[i] > k:
        break
 
    while AS[i] + BS[j] > k:
        j -= 1
    ans = max(ans, i+j)
        
print(ans)

Solution (Python):
import numpy as np


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

cumA = np.cumsum(A)
cumB = np.cumsum(B)

cumA = cumA[cumA <= k]
cumB = cumB[cumB <= k]

candidates = np.arange(len(cumA)) + np.searchsorted(cumB, k-cumA, side="right")
print(candidates.max())

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

from itertools import accumulate
a_acc=list(accumulate(a))
b_acc=list(accumulate(b))
a_acc.insert(0,0)
b_acc.insert(0,0)

ans=0
j=m

for i in range(n+1):
    nokori=k-a_acc[i]
    if nokori<0:
        break
    while b_acc[j]>nokori:
        j-=1
    ans=max(ans,i+j)
    

print(ans)

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

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, M, K, *AB = map(int, read().split())
    A = AB[:N]
    B = AB[N:]

    time_A = [0]
    time_A.extend(accumulate(A))

    time_B = [0]
    time_B.extend(accumulate(B))

    ans = 0
    j = M
    for i, ta in enumerate(time_A):
        while j > 0 and ta + time_B[j] > K:
            j -= 1

        if ta + time_B[j] > K:
            break

        if ans < i + j:
            ans = i + j

    print(ans)
    return


if __name__ == '__main__':
    main()

Solution (Python):
import itertools as it
def main():
    n,m,k=map(int,input().split())
    a = [0]
    a.extend(it.accumulate(map(int, input().split())))
    b = [0]
    b.extend(it.accumulate(map(int, input().split())))
    
    ans = 0
    M = m
    for A in range(n+1):
        aa = a[A]
        Kaa = k-aa
        if Kaa < 0:
            break
        for B in range(M,-1,-1):
            if b[B] <= Kaa:
                ans = max(ans, A+B)
                M = B
                break
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
NMK=input().split(" ")
N=int(NMK[0])
M=int(NMK[1])
K=int(NMK[2])

def list2int(List):
    out=[]
    for i in range(len(List)):
        out.append(int(List[i]))
    return out

A=list2int(input().split(" "))
B=list2int(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])   

result=0
j=M

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

print(result)

Solution (Python):
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
sa = [0] * (n + 1)
sb = [0] * (m + 1)
for i in range(n):
  sa[i + 1] = sa[i] + a[i]
for i in range(m):
  sb[i + 1] = sb[i] + b[i]
r = m
ans = 0
for i in range(n + 1):
  if sa[i] > k:
    break
  while sa[i] + sb[r] > k:
    r -= 1
  ans = max(ans, i + r)
print(ans)

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

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

for n in range(1+N):
  C = bisect_right(B,K-A[n])-1
  if C!=-1:
    ans.append(C+n)

print(max(ans))

Solution (Python):
from itertools import accumulate

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


a = [0] + list(accumulate(A))
b = [0] + list(accumulate(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):
import sys
sys.setrecursionlimit(10**6)
n, M, k = map(int,input().split())
a_l = list(map(int,input().split()))
b_l = list(map(int,input().split()))
read_a = [0]*n
read_b = [0]*M
time = 0
ans = 0
memo = -1
for j in range(n):
    if time + a_l[j] <= k:
        time += a_l[j]
        read_a[j] = 1
        memo = j
        ans = j+1
    else:
        break

kotae = [ans]


def dfs(i,a,t,m):
    global kotae
    if i < M:
        if b_l[i] + t <= k:
            tt = t + b_l[i]
            aa = a + 1
            kotae.append(aa)
            ii = i+1
            mm = m
            
            dfs(ii,aa,tt,mm)
            
        else:
            if m >= 0:
                tt = t - a_l[m]
                mm = m - 1
                aa = a - 1
                ii = i
                dfs(ii,aa,tt,mm)
            


dfs(0,ans,time,memo)


print(max(kotae))

Solution (Python):
import sys, os, math, bisect, itertools, collections, heapq, queue

from decimal import Decimal
from collections import defaultdict, deque



sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)


MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N, M, K = il()
    A = list(itertools.accumulate([0] + il()))
    B = list(itertools.accumulate(([0] + il())))

    ret, m = 0, M
    for n in range(N+1):
        if A[n] > K:
            break

        while B[m] > K - A[n]:
            m -= 1
        ret = max(ret, n+m)
    print(ret)




if __name__ == '__main__':
    main()

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

sum_alist=[0]
for a in alist:
  sum_alist.append(sum_alist[-1]+a)
sum_blist=[0]
for b in blist:
  sum_blist.append(sum_blist[-1]+b)
  
index_b=M
answer=0
for i in range(N+1):
  sum_a=sum_alist[i]
  if sum_a>K:
    break
    
  while sum_a+sum_blist[index_b]>K:
    index_b-=1
    
  
  answer=max(answer,i+index_b)
  
print(answer)

Solution (Python):
import bisect

n, m, k = map(int, input().split())
INF = 10**18
A = list(map(int, input().split()))
B = list(map(int, input().split()))
cum_a = [0] * (n+1)
cum_b = [0] * (m+1)
for i in range(n):
    cum_a[i+1] = cum_a[i] + A[i]
for i in range(m):
    cum_b[i+1] = cum_b[i] + B[i]
INF = 10**18
ans = -INF
for i in range(n+1):
    mod = k - cum_a[i]
    b = bisect.bisect(cum_b, mod)
    if b == 0:
        ans = max(ans, i)
        break
    ans = max(ans, b+i)
ans -= 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):
from itertools import accumulate
from bisect import bisect_right

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

acca = [0] + list(accumulate(a))
accb = list(accumulate(b))
ans = 0

for i in range(n + 1):
    if k < acca[i]:
        break
    t = k - acca[i]
    ans = max(ans, bisect_right(accb, t) + i)
print(ans)

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

read=bisect(a,k)
time=a[read-1]
ans=read

for i in range(read):
    extra=k-time
    r=bisect(b,extra)
    ans=max(ans,read+r)
    read-=1
    time=a[read-1]
read=bisect(b,k)
ans=max(ans,read)
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**6)

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

Solution (Python):
import itertools as it
def main():
    n,m,k=map(int,input().split())
    Aa = list(map(int,input().split()))
    Bb = list(map(int, input().split()))
    a, b = [0],[0]
    for i in range(n):
        a.append(a[i] + Aa[i])
    for i in range(m):
        b.append(b[i] + Bb[i])
    M = m
    ans = 0
    for A in range(n+1):
        aa = a[A]
        Kaa = k-aa
        if Kaa < 0:
            break
        for B in range(M,-1,-1):
            if b[B] <= Kaa:
                ans = max(ans, A+B)
                M = B
                break
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
import bisect

n, m, k = map(int, input().split())
INF = 10**18
A = list(map(int, input().split()))
B = list(map(int, input().split()))
cum_a = [0] * (n+1)
cum_b = [0] * (m+1)
for i in range(n):
    cum_a[i+1] = cum_a[i] + A[i]
for i in range(m):
    cum_b[i+1] = cum_b[i] + B[i]
INF = 10**18
ans = -INF
for i in range(n+1):
    mod = k - cum_a[i]
    b = bisect.bisect(cum_b, mod)
    if b == 0:
        ans = max(ans, i)
        break
    ans = max(ans, b+i)
print(ans-1)