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()))
arui = [0] * (N + 1)
brui = [0] * (M + 1)
for i in range(N):
    arui[i + 1] = a[i] + arui[i]
for i in range(M):
    brui[i + 1] = b[i] + brui[i]


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

print(ans)

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

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

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

print(ans)

Solution (Python):
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])

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

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

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

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

ans = 0
for i, a in enumerate(A):
    if K - a < 0:
        break
    ans = max(ans, bisect_right(B, K - a) + i - 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()))

dpA = [0]*(N+1)
dpB = [0]*(M+1)

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

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

ans = 0
for i in range(N+1):
    if dpA[i] > K:
        continue
    indexB = bisect.bisect_right(dpB, K - dpA[i])
    ans = max(ans, i + indexB - 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 = [0]
B = [0]
for i in range(n):
	A.append(A[i]+a[i])
for j in range(m):
	B.append(B[j]+b[j])



maxij = 0
l = m
for i in range(n+1):
	for j in range(m):

		if(l == -1):
			break
		elif((A[i]+B[l]) <= k):
		 	maxij=max(maxij,i+l)

		 	break
		else:
		 	l-=1
print(maxij)

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

ruisekiA = [0]
ruisekiB = [0]
tmpA=0
tmpB=0
for i in range(len(A)):
    tmpA += A[i]
    ruisekiA.append(tmpA)
for i in range(len(B)):
    tmpB += B[i]
    ruisekiB.append(tmpB)
ans = 0
for i in range(len(ruisekiA)):
    
    l = -1
    r = len(ruisekiB)
    while r - l > 1:
        mid = (r + l)//2
        tmp = ruisekiA[i]+ruisekiB[mid]
        if tmp <= K:
            l = mid
        else:
            r = mid
    if l == -1:
        continue
    ans = max(ans, i+l)
print(ans)

Solution (Python):
"""everything has a cost!"""

from math import *
import random, functools, collections, sys

__author__ = 'p7x'







if __name__ == '__main__':
    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]

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

    for inx in range(i+1):
        while j > -1 and a[inx] + b[j] > k:
            j -= 1
        ans = max(inx+j+2,ans)
    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()))
c = [0] * (n + 1)
d = [0] * (m + 1)

for i in range(n):
  c[i+1] = c[i]+a[i]
for i in range(m):
  d[i+1] = d[i]+b[i]
  
cnt = 0
  
for i in range(n+1):
  if c[i] > k:
    continue
  e = bisect.bisect_right(d, k-c[i])
  if cnt < i + e -1:
    cnt =  i + e -1
print(cnt)

Solution (Python):

from math import gcd
from collections import Counter, deque, defaultdict
from heapq import heappush, heappop, heappushpop, heapify, heapreplace, merge
from bisect import bisect_left, bisect_right, bisect, insort_left, insort_right, insort
from itertools import accumulate, product, permutations, combinations, combinations_with_replacement



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 in range(n+1):
    c = bisect_right(b, k-a[i])-1
    if c!=-1: ans.append(c+i)
print(max(ans))

Solution (Python):
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()))

bs = 0
ans = 0

i = 0
while i < n:
    if a[i] <= k:
        k -= a[i]
        bs += 1
        ans = max(ans, bs)
    else:
        break
    i += 1
i -= 1

j = 0
while j < m:
    while i >= 0 and b[j] > k:
        k += a[i]
        bs -= 1
        i -= 1
    if b[j] <= k:
        k -= b[j]
        bs += 1
        ans = max(ans, bs)
    else:
        break
    j += 1

print(ans)

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

a = [0]
b = [0]

for i in range(n):
    a.append(A[i] + a[-1])
for i in range(m):
    b.append(B[i] + b[-1])

best = 0
ans = 0

for i in range(n+1):
    if a[i] > k:
        break
    for j in range(best,m+1):
       min = a[i] + b[j]
       if min > k:
           book = i + j - 1
           ans = max(book,ans)
           best = j - 1
           break

       if j == m:
           book = i + j
           ans = max(book, ans)
           best = j - 1
           break


print(ans)

Solution (Python):
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
if K >= sum(A) + sum(B):
    print(M + N)
    exit()
acc = 0
ans = 0
for j, b in enumerate(B):
    acc += b
    if K >= acc:
        ans = j + 1
for i in range(N):
    acc += A[i]
    if K >= acc:
        ans = max(ans, i + j + 2)
        continue
    else:
        while (K < acc) & (j > -1):
            acc -= B[j]
            j -= 1
        if K >= acc:
            ans = max(ans, i + j + 2)
print(ans)

Solution (Python):
N,M,K = map(int,input().split())
a=list(map(int, input().split()))
b=list(map(int, input().split()))
a.insert(0,0)
b.insert(0,0)
b_num = sum(b)
b_best = M
j = M
a_num = 0
maxmize = 0
for i in range(N+1):
  
  X = 0
  a_num += a[i]
  if a_num>K:
    break
  X +=a_num
  handan = True
  j = b_best
  while handan ==True:
    
    X +=b_num
    
    if X<=K:
      b_best = j
      maxmize = max(i+j,maxmize)
      handan=False
    else:
      X -=b_num
      b_num -=b[j]
      j-=1
    
    
print(maxmize)

Solution (Python):

n,m,k = 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

j = m
for i,a in enumerate(sumA):
    while j >= 0 and a + sumB[j] > k:
        j -= 1
    if j < 0:break
    ans = max(ans,i+j)
print(ans)

Solution (Python):
from itertools import accumulate

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

la = [0] + list(accumulate(a))
lb = [0] + list(accumulate(b))

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

print(ans)

Solution (Python):
import numpy as numpy 
import bisect as bisect
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
a.insert(0,0)
ali = numpy.cumsum(a)
bli = numpy.cumsum(list(map(int,input().split())))
a = [0]
for i in range(n+1):
    if ali[i] > k:
        break
    b = bisect.bisect_right(bli,k-ali[i])
    a.append(i+b)
print(max(a))

Solution (Python):
import sys
from functools import reduce

def main(n, m, k, a, b):
    """ main
    """

    bestScore = 0
    _k = 0
    _ai = 0

    while True:
        if _ai >= n or _k + a[_ai] > k:
            bestScore = _ai
            break
        else:
            _k = _k + a[_ai]
            _ai = _ai + 1

    _bi = 0

    while _bi < m:
        _k = _k + b[_bi]

        while _k > k and _ai - 1 >= 0:
            _k = _k - a[_ai - 1] 
            _ai = _ai - 1

        if _k > k:
            break

        _bi = _bi + 1

        if _ai + _bi > bestScore:
            bestScore = _ai + _bi

    return bestScore

if __name__ == "__main__":
    N, M, K = map(int, sys.stdin.readline().split(" "))
    A = list(map(int, sys.stdin.readline().split(" ")))
    B = list(map(int, sys.stdin.readline().split(" ")))

    print(main(N, M, K, A, B))

Solution (Python):
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])

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
input = sys.stdin.readline

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

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

j = m

for i in range(n+1):
    if a[i] > k:
        break
    while True:
        if a[i] + b[j] <= k:
            break
        j -= 1
    res = max(res, i+j)

print(res)

Solution (Python):
n,m,k=map(int, input().split())
*a,=map(int, input().split())
*b,=map(int, input().split())
a=[0]+a
b=[0]+b
import numpy as np
a=np.cumsum(a)
b=np.cumsum(b)
bi=m
ans=0
for i in range(n+1):
    asm=a[i]
    while a[i]+b[bi]>k and bi:
        bi-=1
    if a[i]+b[bi]<=k:
        ans=max(ans,i+bi)
print(ans)

Solution (Python):
import bisect


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

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


sumA = [0]*(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

for i,a in enumerate(sumA):
    if a > k:break
    j = bisect.bisect_right(sumB,k-a)
    ans = max(i + j - 1, ans)
print(ans)

Solution (Python):
import itertools
import bisect
N, M, K = map(int,input().split())
al = list(map(int,input().split()))
bl = list(map(int,input().split()))

als = list(itertools.accumulate(al))
als.insert(0,0)
bls = list(itertools.accumulate(bl))
bls.insert(0,0)



ans = 0
for i in range(N+1):
    nokori = K - als[i]
    if nokori >= 0:
        index = bisect.bisect_right(bls,nokori)
        if index > 0:
            index -= 1
        tmp = i + index
        
        if tmp > ans:
            ans = tmp
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 math import gcd
from collections import Counter, deque, defaultdict
from heapq import heappush, heappop, heappushpop, heapify, heapreplace, merge
from bisect import bisect_left, bisect_right, bisect, insort_left, insort_right, insort
from itertools import accumulate, product, permutations, combinations, combinations_with_replacement



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 in range(n+1):
    c = bisect_right(b, k-a[i])-1
    if c!=-1: ans.append(c+i)
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=[0]
B=[0]
p=0
q=0
ans=0

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

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

Solution (Python):
import bisect
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
a_sum=[]
b_sum=[]
a_sum.append(0)
b_sum.append(0)
for i in range(n):
    a_sum.append(a_sum[i]+a[i])
for j in range(m):
    b_sum.append(b_sum[j]+b[j])
ans=0
for x in range(n+1):
    timecount=k-a_sum[x]
    if timecount>=0:
        y=bisect.bisect_right(b_sum,k-a_sum[x])-1
        ans=max(x+y,ans)
    else:
        break
print(ans)

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

ac = np.cumsum(a)
bc = np.cumsum(b)
ans = 0




for i in range(n+1):
    if k < ac[i]:
        break
    time = k - ac[i]
    j = bisect_right(bc, time)
    ans = max(ans, j + i)

print(ans)

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

Solution (Python):
def solve():
    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)


if __name__ == '__main__':
    solve()

Solution (Python):
from sys import stdin, stdout, setrecursionlimit
from collections import deque, defaultdict, Counter
import math

rl = lambda: stdin.readline()
rll = lambda: stdin.readline().split()
rli = lambda: map(int, stdin.readline().split())
rlf = lambda: map(float, stdin.readline().split())

INF, NINF = float('inf'), float('-inf')

def main():
	n, m, k = rli()
	A = deque(rli())
	B = deque(rli())
	L = deque()
	p, q = 0, 0
	currsum = 0
	for i, a in enumerate(A):
		if currsum + a <= k:
			currsum += a
			p += 1
		else:
			break
	cnt = p 
	for np in range(p, -1, -1):
		for i in range(q, len(B)):
			add = B[i]
			if add + currsum <= k:
				currsum += add
				q += 1
			else:
				break
		
		currsum -= A[np-1] if np > 0 else 0
		cnt = max(cnt, np + q)
	print(cnt)
	stdout.close()

if __name__ == "__main__":
	main()

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

def int1(x): return int(x) - 1
def II(): return int(input())
def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())
def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def MS(): return input().split()
def LS(): return list(input())
def LLS(rows_number): return [LS() for _ in range(rows_number)]
def printlist(lst, k=' '): print(k.join(list(map(str, lst))))
INF = float('inf')


from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product, permutations



from bisect import bisect_right, bisect_left

def solve():
    N, M, K = MI()
    A = LI()
    B = LI()
    A = [0] + list(accumulate(A))
    B = [0] + list(accumulate(B))

    ans = 0
    for i, a in enumerate(A):
        if a > K:
            break
        idx = bisect_right(B, K - a) - 1
        ans = max(ans, i + idx)
    print(ans)

        



if __name__ == '__main__':
    solve()

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

def int1(x): return int(x) - 1
def II(): return int(input())
def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())
def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def MS(): return input().split()
def LS(): return list(input())
def LLS(rows_number): return [LS() for _ in range(rows_number)]
def printlist(lst, k=' '): print(k.join(list(map(str, lst))))
INF = float('inf')


from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product, permutations



from bisect import bisect_right, bisect_left

def solve():
    N, M, K = MI()
    A = LI()
    B = LI()
    A = [0] + list(accumulate(A))
    B = [0] + list(accumulate(B))

    def is_ok(arg, a):
        
        
        return B[arg] <= K - a

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

    
    
    ans = 0
    for i, a in enumerate(A):
        idx = meguru_bisect(M+1, -1, a)
        if 0 <= idx <= M:
            ans = max(ans, i + idx)
    print(ans)

        



if __name__ == '__main__':
    solve()

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

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

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

print(ans)

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

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

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

Solution (Python):
a,b,k = map(int,input().split(" "))
r = list(map(int,input().split(" ")))
l = list(map(int,input().split(" ")))
ar = [0]
br = [0]
for i in range(a):
    ar.append(ar[-1] + r[i])
for i in range(b):
    br.append(br[-1] + l[i])
ans = 0
j = b
for i in range(len(ar)):
    if ar[i] > k:
        break
    while True:
        if ar[i] + br[j] <= k:
            t = i + j
            break
        j -= 1
    ans = max(ans,t)
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)
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]
ans = 0
for i in range(n+1):
    cnt_a = i
    rest = k - sa[i]
    if rest < 0:
        break
    cnt_b = bisect_right(sb, rest) - 1
    ans = max(ans, cnt_a+cnt_b)
print(ans)

Solution (Python):
N, M, K = map(int, input().split())
A = list(map(int,input().split()))
B = list(map(int, input().split()))
a = [0]
b = [0]

ans = 0
j = M

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

for i in range(M):
  b.append(b[i] + B[i])

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

print(ans)

Solution (Python):
import bisect

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

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

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

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

    val = bisect.bisect_right(cum_b, k - cum_a[i]) - 1

    
    

    ans = max(ans, i + val)

print(ans)

Solution (Python):
import itertools

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

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

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

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.insert(0,0)
b.insert(0,0)
j=1
while j<=m and b[j]+b[j-1]<=k:
  b[j]+=b[j-1]
  j+=1
  

j-=1
books=j


i=1
while i<n+1:
  a[i]+=a[i-1]
  while j>=0 and a[i]+b[j]>k:
    j-=1
  if(j!=-1) and a[i]+b[j]<=k:
    books=max(books,j+i)
    
    
    i+=1
    
  else:
    break
print(books)

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

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

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

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

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

while a<n and cnt<k:
    if cnt+A[a]<=k:
        cnt+=A[a]
        a+=1
    else:
        break
ans=a      
        
while b<m:
    if cnt+B[b]<=k:
        cnt+=B[b]
        b+=1
        ans=max(a+b,ans)
    elif a>0:
        a-=1
        cnt-=A[a]
    else:
        break
        
print(ans)

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

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

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

Solution (Python):
import sys

INF = 10**9 + 5

N, M, K = map(int, sys.stdin.readline().rstrip().split())
A = [int(x) for x in sys.stdin.readline().rstrip().split()]
B = [int(x) for x in sys.stdin.readline().rstrip().split()]

tmpA = [INF] * (N + 1)
tmpA[0] = 0

cntA = 0

for i in range(1, N + 1):
    tmpA[i] = A[i - 1] + tmpA[i - 1]
    cntA = i
    if tmpA[i] > K:
        break




j = 0
vol = 0
tmpB = 0

for i in range(cntA, -1, -1):
    rest = K - tmpA[i]  
    
    while j < M + 1:
        
        if rest < tmpB:  
            break
        vol = max(i + j, vol)
        if j >= M:
            break
        tmpB += B[j]  
        j += 1

print(vol)

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

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

Solution (Python):
from heapq import *
import sys
from collections import *
from itertools import *
from decimal import *
import copy
from bisect import *
import math
sys.setrecursionlimit(4100000)
def gcd(a,b):
    if(a%b==0):return(b)
    return (gcd(b,a%b))
input=lambda :sys.stdin.readline().rstrip()

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

suma=0
sumB=sum(B)
b=M-1

while K<sumB and b>=0:
    sumB-=B[b]
    b-=1
c=b+1

for a in range(N):
    sumB+=A[a]
    while K<sumB and b>=0:
        sumB-=B[b]
        b-=1
    
    if K>=sumB:
        c=max(c,b+1+a+1)
print(c)

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

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

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

Solution (Python):
def bins(a, x):
    l = -1
    h = len(a)
    while (h-l) > 1:
        m = (l+h)//2
        if x >= a[m]:
            l = m
        else:
            h = m
    return l
n, m, k = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
pa, pb = [0] * (n+1), [0] * (m+1)
for i in range(n):
    pa[i+1] = pa[i] + a[i]
for i in range(m):
    pb[i+1] = pb[i] + b[i]
m = 0
for i in range(len(pa)):
    if pa[i] > k:
        break
    v = k - pa[i]
    bv = bins(pb, v)
    m = max(m, bv+i)
        
print(m)

Solution (Python):
n, m, k = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
pa, pb = [0] * (n+1), [0] * (m+1)
for i in range(n):
    pa[i+1] = pa[i] + a[i]
for i in range(m):
    pb[i+1] = pb[i] + b[i]
mx = 0
j = m
for i in range(n+1):
    if pa[i] > k:
        break
    v = k - pa[i]
    while pb[j] > v:
        j -= 1
    mx = max(mx, i+j)
print(mx)

Solution (Python):
FAST_IO = 1
if FAST_IO:
    import io, sys, atexit
    rr = iter(sys.stdin.read().splitlines()).next
    sys.stdout = _OUTPUT_BUFFER = io.BytesIO()
    @atexit.register
    def write():
        sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
else:
    rr = raw_input
rri = lambda: int(rr())
rrm = lambda: map(int, rr().split())
N,M, k= rrm()
a = rrm()
b = rrm()
prefix_a = [0]
for n in a:
    prefix_a.append(prefix_a[-1]+n)
prefix_b = [0]
for n in b:
    prefix_b.append(prefix_b[-1]+n)
ans = 0
b = M
for i in range(0, N+1):
    
    while b>0 and (prefix_a[i] + prefix_b[b])>k :
        b -= 1
    if prefix_a[i]+prefix_b[b]<=k:
        ans = max(ans, i+b)
print(ans)

Solution (Python):




from itertools import accumulate, chain

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

def lister(a, b, k):
    """sequence of i+j while sum(a[:i]) + sum(b[:j]) <= k"""
    j = len(b)
    sumb = sum(b)
    for i, suma in enumerate(chain([0], accumulate(a))):
        if suma > k:
            break
        while suma + sumb > k and j > 0:
            j -= 1
            sumb -= b[j]
        if suma + sumb <= k:
            yield i + j

print(max(lister(a, b, k)))

Solution (Python):
import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    N, M, K = LI()
    A = LI()
    B = LI()

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

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

    print(ans)

if __name__ == '__main__':
    resolve()

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) - 1
    
    ans = max(ans, a_idx + b_idx)
        
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()))

    t = 0
    for i in range(m):
        t += b[i]

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

if __name__ == '__main__':
    main()

Solution (Python):
n,m,k=map(int,input().split())
al=list(map(int,input().split()))
bl=list(map(int,input().split()))
a=[0]
b=[0]
for i in range(n):
    a.append(a[i]+al[i])
for i in range(m):
    b.append(b[i]+bl[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)