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

ma = [0] * (N + 1)
mb = [0]*(M+1)


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

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

j = M
max_num = 0

for i in range(N+1):
    if ma[i] > K:
        break
    while ma[i] + mb[j] > K:
        j -= 1
    max_num = max(max_num, i + j)
print(max_num)

Solution (Python):
import bisect
n, m, k = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a_acc = [0]
b_acc = [0]
now = 0
for i in range(n):
  now += a[i]
  a_acc.append(now)

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

ans = 0
for i in range(n+1):
  t = k - a_acc[i]
  if t < 0:
    break
  ind = bisect.bisect_left(b_acc, t)
  if ind == m+1:
    ans = max(ans, i+m)
    ind -= 1
  if b_acc[ind] == t:
    ans = max(ans, i+ind)
  else:
    ans = max(ans, i+ind-1)

print(ans)

Solution (Python):
m, n, o = map(int, input().split()) 
y = list(map(int, input().split())) 
z = list(map(int, input().split())) 
a, b = [0], [0]
for i in range(m):
    a.append(a[i] + y[i])
for i in range(n):
    b.append(b[i] + z[i])
ans, j = 0, n
for i in range(m + 1):
    if a[i] > o:
        break
    while b[j] > o - 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_sum = [0]
for i in range(len(A)):
    a_sum.append(a_sum[-1]+A[i])
    
b_sum = [0]
for i in range(len(B)):
    b_sum.append(b_sum[-1]+B[i])

max_time = 0
b_idx = M
for i in range(len(a_sum)):
    if a_sum[i] > K:
        break
    for j in range(b_idx, -1, -1):
        if a_sum[i]+b_sum[j] <= K:
            max_time = max(max_time, i + j)
            b_idx = j
            break
print(max_time)

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

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


for j in range(1,m+1):
  B[j]+=B[j-1]


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):
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

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

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

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

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

ans = 0
j = M

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

print(ans)

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

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

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()))
book_a = [0] * (n + 1)
book_b = [0] * (m + 1)

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

for j in range(m):
    book_b[j + 1] = book_b[j] + b[j]

r = m
ans = 0

for i in range(n + 1):
    if book_a[i] > k:
        break
    while book_a[i] + book_b[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()))


def cumsum(l):
    s = 0
    for v in l:
        s += v
        yield s


a = [0] + list(cumsum(A))
b = [0] + list(cumsum(B))
ans = 0
n = N + 1
m = M
for i in range(n):
    if a[i] > K:
        continue
    for j in range(m, -1, -1):
        if a[i] + b[j] <= K:
            m = j
            break
        else:
            continue
    ans = max(ans, i + j)
print(ans)

Solution (Python):
import numpy as np
import scipy.sparse as sps
import scipy.misc as spm
import collections as col
import functools as func
import itertools as ite
import fractions as frac
import math as ma
from math import cos,sin,tan,sqrt
import cmath as cma
import copy as cp
import sys
import re
import bisect as bs
sys.setrecursionlimit(10**7)
EPS = sys.float_info.epsilon
PI = np.pi; EXP = np.e; INF = np.inf
MOD = 10**9 + 7

def sinput(): return sys.stdin.readline().strip()
def iinput(): return int(sinput())
def imap(): return map(int, sinput().split())
def fmap(): return map(float, sinput().split())
def iarr(n=0):
    if n: return [0 for _ in range(n)]
    else: return list(imap())
def farr(): return list(fmap())
def sarr(n=0):
    if n: return ["" for _ in range(n)]
    else: return sinput().split()
def adj(n): return [[] for _ in range(n)]

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

def isOK(num): return bcum[num] <= tb

n,m,k = imap()
a,b = iarr(),iarr()
acum,bcum = np.array(a).cumsum(),np.array(b).cumsum()

anss = []
for i in range(n+1):
    ans = 0 if i==n else i + 1
    ta = 0 if i==n else acum[i]
    if ta > k: continue
    else: tb = k - ta
    ok,ng = -1,m
    ans += meguru(ok,ng) + 1 
    
    anss.append(ans)
ans = max(anss) if anss else 0
print(ans)

Solution (Python):
import numpy as np
import scipy.sparse as sps
import scipy.misc as spm
import collections as col
import functools as func
import itertools as ite
import fractions as frac
import math as ma
from math import cos,sin,tan,sqrt
import cmath as cma
import copy as cp
import sys
import re
import bisect as bs
sys.setrecursionlimit(10**7)
EPS = sys.float_info.epsilon
PI = np.pi; EXP = np.e; INF = np.inf
MOD = 10**9 + 7

def sinput(): return sys.stdin.readline().strip()
def iinput(): return int(sinput())
def imap(): return map(int, sinput().split())
def fmap(): return map(float, sinput().split())
def iarr(n=0):
    if n: return [0 for _ in range(n)]
    else: return list(imap())
def farr(): return list(fmap())
def sarr(n=0):
    if n: return ["" for _ in range(n)]
    else: return sinput().split()
def adj(n): return [[] for _ in range(n)]

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

def isOK(num): return bcum[num] <= tb

n,m,k = imap()
a,b = iarr(),iarr()
acum,bcum = np.array(a).cumsum(),np.array(b).cumsum()

anss = []
for i in range(n+1):
    ans = 0 if i==n else i + 1
    ta = 0 if i==n else acum[i]
    if ta > k: continue
    else: tb = k - ta
    
    
    ans += bs.bisect_right(bcum, tb)
    anss.append(ans)
ans = max(anss) if anss else 0
print(ans)

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

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

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

x = [0]

y = [0]

for i in range(n):
    x.append(x[i]+a[i])

for i in range(m):
    y.append(y[i]+b[i])

ans = 0

cnt = m

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

Solution (Python):
import numpy as np
import bisect

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

A = [0] + A
B = [0] + B

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

SA = np.cumsum(A)
SB = np.cumsum(B)

ans = 0
for i in range(N+1):
    if(SA[i] > K): break
    id = bisect.bisect_right(SB, K-SA[i])
    ans = max(ans, i + id-1)

print(ans)

Solution (Python):
import sys

import numba as nb
import numpy as np

input = sys.stdin.readline


@nb.njit("i8(i8,i8,i8,i8[:],i8[:])")
def solve(N, M, K, A, B):
    B_cumsum = B.cumsum()
    ans = 0
    time = 0
    for i in range(N + 1):
        if time > K:
            break
        
        
        key = K - time
        ok = -1
        ng = len(B_cumsum)
        while (ng - ok > 1):
            mid = (ok + ng) // 2
            if B_cumsum[mid] <= key:
                ok = mid
            else:
                ng = mid
        j = ok + 1
        ans = max(ans, i + j)
        if i < N:
            time += A[i]
    return ans


def main():
    N, M, K = map(int, input().split())
    A = np.array(input().split(), dtype=np.int64)
    B = np.array(input().split(), dtype=np.int64)
    
    

    ans = solve(N, M, K, A, B)
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import sys

import numba as nb
import numpy as np

input = sys.stdin.readline


@nb.njit("i8(i8[:], i8)")
def binary_search(a, key):
    """Meguru type binary search"""
    ok = -1
    ng = len(a)

    while (ng - ok > 1):
        mid = (ok + ng) // 2
        if a[mid] <= key:
            ok = mid
        else:
            ng = mid

    return ok


@nb.njit("i8(i8,i8,i8,i8[:],i8[:])")
def solve(N, M, K, A, B):
    B_cumsum = B.cumsum()
    ans = 0
    time = 0
    for i in range(N + 1):
        if time > K:
            break
        j = binary_search(B_cumsum, K - time) + 1
        ans = max(ans, i + j)
        if i < N:
            time += A[i]
    return ans


def main():
    N, M, K = map(int, input().split())
    A = np.array(input().split(), dtype=np.int64)
    B = np.array(input().split(), dtype=np.int64)
    
    

    ans = solve(N, M, K, A, B)
    print(ans)


if __name__ == "__main__":
    main()

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(N):
	A[i+1] += A[i]
for i in range(M):
	B[i+1] += B[i]

b = M
r = 0
for i,a in enumerate(A):
	if a > K:
		break
	while a+B[b]>K and b>=0:
		b -= 1
	r = max(r, i+b)

print(r)

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 sys

import numba as nb
import numpy as np

input = sys.stdin.readline


@nb.njit("i8(i8[:], i8)")
def binary_search(a, key):
    """Meguru type binary search"""
    ok = -1
    ng = len(a)

    while (ng - ok > 1):
        mid = (ok + ng) // 2
        if a[mid] <= key:
            ok = mid
        else:
            ng = mid

    return ok


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

    B_cumsum = np.cumsum(B)
    ans = 0
    time = 0
    for i in range(N + 1):
        if time > K:
            break
        j = binary_search(B_cumsum, K - time) + 1
        ans = max(ans, i + j)

        if i < N:
            time += A[i]

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):




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

    a_sum = b_sum = 0
    a_i = b_i = 0
    while a_i < len(a_list) and a_sum + a_list[a_i] <= k:
        a_sum += a_list[a_i]
        a_i += 1
    ans = a_i

    while a_i > 0:
        
        while b_i < len(b_list) and a_sum + b_sum + b_list[b_i] <= k:
            b_sum += b_list[b_i]
            b_i += 1
            ans = max(ans, a_i + b_i)
        a_sum -= a_list[a_i - 1]  
        a_i -= 1

    while b_i < len(b_list) and a_sum + b_sum + b_list[b_i] <= k:
        b_sum += b_list[b_i]
        b_i += 1
        ans = max(ans, a_i + b_i)

    print(ans)


if __name__ == '__main__':
    main()

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

from itertools import accumulate
import bisect

sum_a_list = list(accumulate([0] + a_list))
sum_b_list = list(accumulate([0] + b_list))

ans = 0
b_idx = 0
for a_idx in range(n+1):
    if sum_a_list[a_idx] > k:
        continue

    time_rest = k - sum_a_list[a_idx]
    b_idx = bisect.bisect_right(sum_b_list,time_rest)

    ans = max(ans, a_idx + b_idx - 1)

print(ans)

Solution (Python):
import sys


from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right



input = sys.stdin.readline
sys.setrecursionlimit(10**7)
inf = 10**17
mod = 10**9 + 7

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

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

ans = 0
for i in range(len(aa)):
    sub = k-aa[i]
    cnt = 0
    if 0 <= sub:
        cnt += i
        t = bisect_right(bb, sub)
        if 0 < t: cnt += t-1
    ans = max(ans, cnt)

print(ans)

Solution (Python):
def bisect(x):
    l,r=-1,M+1
    while(r-l>1):
        mid=(l+r)//2
        if cumB[mid]>=x:
            r=mid
        else:
            l=mid
    return l

N,M,K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
cumB=[0]*(M+1)
for i in range(M):
    cumB[i+1]=cumB[i]+B[i]
c=0
ans=0
for i in range(N+1):
    if c>K:
        break
    r=K-c
    b=bisect(r+1)
    ans=max(ans,i+b)
    if i!=N:
        c+=A[i]
print(ans)

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

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

ans = 0
import bisect
for i in range(N+1):
    if K - sumA[i] >= 0:
        idx = bisect.bisect_right(sumB,K-sumA[i])
        ans = max(ans,i + idx - 1)

print(ans)

Solution (Python):
import sys
from itertools import accumulate

input = sys.stdin.readline


def binary_search(a, key):
    """Meguru type binary search"""
    ok = -1
    ng = len(a)

    while (ng - ok > 1):
        mid = (ok + ng) // 2
        if a[mid] <= key:
            ok = mid
        else:
            ng = mid

    return ok


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

    B_cumsum = tuple(accumulate(B))
    ans = 0
    time = 0
    for i in range(N + 1):
        if time > K:
            break
        j = binary_search(B_cumsum, K - time) + 1
        if ans < i + j:
            ans = i + j
        if i < N:
            time += A[i]

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import sys
from itertools import accumulate

input = sys.stdin.readline


def binary_search(a, key):
    """Meguru type binary search"""
    ok = -1
    ng = len(a)

    while (ng - ok > 1):
        mid = (ok + ng) // 2
        if a[mid] <= key:
            ok = mid
        else:
            ng = mid

    return ok


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

    B_cumsum = tuple(accumulate(B))
    ans = 0
    time = 0
    for i in range(N + 1):
        if time > K:
            break
        j = binary_search(B_cumsum, K - time) + 1
        ans = max(ans, i + j)

        if i < N:
            time += A[i]

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import sys
from itertools import accumulate

input = sys.stdin.readline


def binary_search(a, key):
    """Meguru type binary search"""
    ok = -1
    ng = len(a)

    while (abs(ok - ng) > 1):
        mid = (ok + ng) // 2
        if a[mid] <= key:
            ok = mid
        else:
            ng = mid

    return ok


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

    B_cumsum = tuple(accumulate(B))
    ans = 0
    time = 0
    for i in range(N + 1):
        if time > K:
            break
        j = binary_search(B_cumsum, K - time) + 1
        ans = max(ans, i + j)

        if i < N:
            time += A[i]

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import sys
from itertools import accumulate

input = sys.stdin.readline


def is_ok(a, key, idx):
    if a[idx] <= key:
        return True
    else:
        return False


def binary_search(a, key):
    """Meguru type binary search"""
    ok = -1
    ng = len(a)

    while (abs(ok - ng) > 1):
        mid = (ok + ng) // 2
        if is_ok(a, key, mid):
            ok = mid
        else:
            ng = mid

    return ok


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

    B_cumsum = tuple(accumulate(B))
    ans = 0
    time = 0
    for i in range(N + 1):
        if time > K:
            break
        j = binary_search(B_cumsum, K - time) + 1
        ans = max(ans, i + j)

        if i < N:
            time += A[i]

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
from bisect import bisect_right


def main():
    n, m, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    answer = 0
    is_changed = False
    if a[0] <= k or b[0] <= k:
        answer = 1
    elif a[0] + b[0] <= k:
        answer = 2
    for i in range(1, n):
        a[i] += a[i - 1]
        if k < a[i] and not is_changed:
            answer = i
            is_changed = True
    if a[-1] <= k:
        answer = n
    is_changed = False
    for i in range(1, m):
        b[i] += b[i - 1]
        if k < b[i] and not is_changed:
            answer = max(answer, i)
            is_changed = True
    if b[-1] <= k:
        answer = max(answer, m)
    if a[-1] + b[-1] <= k:
        answer = n + m
    elif all(k < x for x in [a[0], b[0], a[0] + b[0]]):
        answer = 0
    else:
        for i in range(n):
            l = bisect_right(b, k - a[i])
            if l == 0 or (l == m and b[l - 1] + a[i] > k):
                continue
            answer = max(answer, i + 1 + l)
    print(answer)


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):
from itertools import accumulate
from bisect import bisect_right
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
sum_a = [0]+list(accumulate(a))
b = list(map(int,input().split()))
sum_b = list(accumulate(b))
ma = 0

for i in range(len(sum_a)):
    x = sum_a[i]
    if k<x:
      index = bisect_right(sum_b,k)
      ma = max(index,ma)
    else:
      k -= x
      index = bisect_right(sum_b,k)
      ma = max(i+index,ma)
      k += x
    
print(ma)

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

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

b_sum = [0]
for i in range(m):
    b_sum.append(b_sum[i] + b[i])
b_sum.append(10**20)
ans = 0

for i in range(n+1):
    t = a_sum[i]
    l = 0
    r = len(b_sum)
    
    while l+1<r:
        
        c = (l + r) // 2
        
        
        if t+b_sum[c]<=k:
            l = c
        
        
        else:
            r = c
    
    if a_sum[i]+b_sum[l]<=k:
        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,b=[0] ,[0]
for i in range(N):
    a.append(a[i] + A[i])
for i in range(M):
    b.append(b[i] + B[i])
    
ans, j=0, M
for i in range(N+1):
    if a[i]>K:
        break
    while b[j]>K-a[i]:
        j-=1
    ans=max(ans, i+j)
print(ans)

Solution (Python):
import bisect
import itertools
N, M, K = [int(_) for _ in input().split()]
A = list(itertools.accumulate([0] + [int(_) for _ in input().split()]))
B = list(itertools.accumulate([0] + [int(_) for _ in input().split()]))
ans = 0
for i, a in enumerate(A):
    if a > K:
        break
    ans = max(ans, i + bisect.bisect(B, K - a) - 1)
print(ans)

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

def main():
    N,M,K = map(int, readline().split())
    A = [int(i) for i in readline().split()]
    B = [int(i) for i in readline().split()]

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

    ans = 0
    b_ind = M
    for i, a in enumerate(acc_A):
        remain = K - a
        if remain < 0:
            break
        while acc_B[b_ind] > remain:
            b_ind -= 1

        tmp = i + b_ind
        ans = max(ans, tmp)
   
    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()))
sa,sb = [0],[0]
ans = 0
for i in range(n):
    sa.append(sa[i]+a[i])
for i in range(m):
    sb.append(sb[i]+b[i])


for i in range(n+1):
    sai = sa[i]
    x = k-sai
    if x>=0:
        ans = max(ans,bisect.bisect_right(sb,x)-1+i)
print(ans)

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

def main():
    N,M,K = map(int, readline().split())
    A = [int(i) for i in readline().split()]
    B = [int(i) for i in readline().split()]

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

    ans = 0
    for i, a in enumerate(acc_A):
        tmp = i
        rest = K - a
        if rest < 0:
            ans = max(ans, bisect_right(acc_B, K) - 1)
            break
        b_ind = bisect_right(acc_B, rest) - 1
        tmp += b_ind

        ans = max(ans, tmp)
   
    print(ans)
       


if __name__ == "__main__":
    main()

Solution (Python):




def input_int():
    return int(input())



def int1(x):
    return int(x) - 1



def input_to_int_map():
    return map(int, input().split())



def input_to_int_tuple():
    return tuple(map(int, input().split()))



def input_to_int_tuple_minus1():
    return tuple(map(int1, input().split()))

import itertools
def main():
    n, m, k = input_to_int_map()
    a_list = input_to_int_tuple()
    b_list = input_to_int_tuple()


    a_sums = [0]
    temp_sums = 0
    for temp in a_list:
        temp_sums += temp
        a_sums.append(temp_sums)
    temp_sums = 0
    b_sums = [0]
    for temp in b_list:
        temp_sums += temp
        b_sums.append(temp_sums)

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

        while b_sums[j] > k - now_a_sum:
            j -= 1

        ans = max(ans, i + j)

    return ans

if __name__ == "__main__":
    print(main())

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

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

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


if __name__ == '__main__':
    main()

Solution (Python):
import numpy as np
from bisect import bisect_right



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

cum_A = np.cumsum(A)
cum_B = np.cumsum(B)



ans = 0

for idx_a in range(N + 1):
    if A[idx_a] > K:
        break

    b = K - cum_A[idx_a]
    idx_b = max(0, bisect_right(cum_B, b) - 1)
    if cum_A[idx_a] + cum_B[idx_b] > K:
        idx_b -= 1
    if idx_b < 0:
        break
    ans = max(ans, idx_a + idx_b)
    














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

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

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

ans = 0
for i in range(N+1):
    r = K - Sa[i]
    if r < 0:
        break
    j = bisect_right(Sb, r) - 1
    ans = max(ans, i + j)

print(ans)

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

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

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


if __name__ == '__main__':
    main()

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

    SUMA = [0]
    for a in A:
        if SUMA[-1] + a > K: break
        SUMA.append(SUMA[-1] + a)
    SUMB = [0]
    for b in B:
        if SUMB[-1] + b > K: break
        SUMB.append(SUMB[-1] + b)

    ans = 0
    j = len(SUMB) - 1
    for i, suma in enumerate(SUMA):
        while suma + SUMB[j] > K:
            j -= 1
        ans = max(ans, i + j)

    print(ans)


if __name__ == '__main__':
    main()

exit()

Solution (Python):
import numpy as np

from bisect import bisect_left

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

cum_A = np.cumsum(A)
cum_B = np.cumsum(B)



ans = 0





for idx_a in range(N + 1):
    if A[idx_a] > K:
        break

    b = K - cum_A[idx_a]
    idx_b = min(bisect_left(cum_B, b), M)
    if cum_A[idx_a] + cum_B[idx_b] > K:
        idx_b -= 1
    if idx_b < 0:
        break
    ans = max(ans, idx_a + idx_b)
    

print(ans)

Solution (Python):
import bisect

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

    
    A_cum = [0] * (N+1)
    B_cum = [0] * (M)
    ap = 0
    for i, a in enumerate(A_list):
        A_cum[i+1] = ap + a
        ap += a
    bp = 0
    for i, b in enumerate(B_list):
        B_cum[i] = bp + b
        bp += b

    max_cnt = 0
    for i in range(N+1):
        a = A_cum[i]
        if a > K:
            break
        bi = bisect.bisect_right(B_cum, K-a)
        max_cnt = max(max_cnt, i+bi)

    print(max_cnt)

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

sum_m = 0
max_ai = 0
for i in range(1, N + 1):
    if sum_m + A[i - 1] <= K:
        sum_m += A[i - 1]
        max_ai = i
    else:
        break

max_bi = 0
max_count = 0
for j in reversed(range(0, max_ai + 1)):
    for k in range(max_bi + 1, M + 1):
        if sum_m + B[k - 1] <= K:
            sum_m += B[k - 1]
            max_bi = k
        else:
            break
    if j + max_bi > max_count:
        max_count = j + max_bi
    
    sum_m -= A[j - 1] if j >= 0 else 0

print(max_count)

Solution (Python):
ii = lambda:int(input())
mi = lambda:list(map(int,input().split()))
ix = lambda x:list(input() for _ in range(x))
mix = lambda x:list(mi() for _ in range(x))
iix = lambda x:list(int(input()) for _ in range(x))


def resolve():
    n,m,k = mi()
    a = mi()
    b = mi()
    
    cum_a = create_cum(a)
    cum_b = create_cum(b)
    
    res, j = 0, m
    for i in range(n+1):
        if cum_a[i] > k:
            break
        while cum_a[i] + cum_b[j] > k:
            j -= 1
        res = max(res, i+j)

    print(res)

def create_cum(x):
    res = [0]
    for i in range(len(x)):
        res.append(res[i]+x[i])
    return res

if __name__ == "__main__":
    resolve()

Solution (Python):
import numpy as np
N, M, K = map(int, input().split())
A = [0]
B = [0]
A += list(map(int, input().split()))
B += list(map(int, input().split()))
A_cumsum = np.cumsum(A, dtype='int64')
B_cumsum = np.cumsum(B, dtype='int64')

def binary_search(k):
    if B_cumsum[-1] <= k:
        return M
    right = M
    left = 0
    while right != left:
        mid = (left + right) // 2
        if B_cumsum[mid] > k:
            right = mid
        else:
            left = mid + 1
    return left - 1

ans = 0
for i in range(N+1):
    count = 0
    if A_cumsum[i] <= K:
        count += i
    else:
        break
    count += binary_search(K - A_cumsum[i])
    if ans < count:
        ans = count
print(ans)

Solution (Python):
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
aa=[0]
bb=[0]
a_sum=0
b_sum=0

for i in range(n):
    if (a_sum+a[i])<=k:
        a_sum+=a[i]
        aa.append(a_sum)
    else:
        break
for i in range(m):
    if (b_sum+b[i])<=k:
        b_sum+=b[i]
        bb.append(b_sum)
    else:
        break



cnt=len(bb)-1
ans=0
for i in range(len(aa)):
    for j in range(cnt,-1,-1):
        if bb[j]<=(k-aa[i]):
            cnt=j
            ans=max(ans,(i+j))
            break
print(ans)