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 sys
from io import StringIO
import unittest
import numpy as np

def resolve():
    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 _ in range(n + 1)]

    for i in range(len(cum_A)):
        if k < cum_A[i]:
            pass
        elif k == cum_A[i]:
            ans[i] = i
        else:
            tmp_k = k - cum_A[i]

            left = 0
            right = len(cum_B) - 1

            if tmp_k >= cum_B[-1]:
                ans[i] = i + len(cum_B) - 1
            else:
                while 1:
                    mid = (left + right) // 2 
                    
                    if mid == left or mid == right:
                        break
                    else:
                        if tmp_k >= cum_B[mid]:
                            left = mid
                        else:
                            right = mid

                if tmp_k > cum_B[right]:
                    ans[i] = i + right
                else:
                    ans[i] = i + left

    
    print(max(ans))



class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """3 4 240
60 90 120
80 150 80 150"""
        output = """3"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """3 4 730
60 90 120
80 150 80 150"""
        output = """7"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """5 4 1
1000000000 1000000000 1000000000 1000000000 1000000000
1000000000 1000000000 1000000000 1000000000"""
        output = """0"""
        self.assertIO(input, output)


if __name__ == "__main__":
    
    resolve()

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





def solv():
	global a,b,n,m,k
	akari = [0]*(n+1)
	bkari = [0]*(m+1)
	for i in range(n):
		akari[i+1] = akari[i] + a[i]

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

	def bin(ls,s,e,sikii):
		if e - s <= 1:
			return s
		mid = (s+e)//2
		if sikii < ls[mid]:return bin(ls,s,mid,sikii)
		else:return bin(ls,mid,e,sikii)

	ans = 0
	for i in range(n+1):
		time = akari[i]
		if k - time < 0:break
		
		ref = bisect.bisect_right(bkari,k-time)
		ans = max(ans,ref+i)
	return ans

def karisolv():
	global a,b,n,m,k
	akari = [0]*(n+1)
	bkari = [0]*(m+1)
	for i in range(n):
		akari[i+1] = akari[i] + a[i]

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

	ans = 0
	for i in range(n+1):
		time = akari[i]
		ref = 0
		if k - time <= 0:break
		cost = 0
		for j in range(m+1):
			if k - time - bkari[j] >= 0:
				ans = max(ans,j+i)
		
	return ans

def makedata():
	global a,b,n,m,k
	n,m,k = random.randint(1,10000),random.randint(1,10000),random.randint(1,1000000)
	a = [random.randint(1,10**5) for i in range(n)]
	b = [random.randint(1,10**5) for i in range(m)]
import sys
def main():
	for i in range(1000000):
		makedata()
		kari = karisolv()
		kotae = solv()
		if kari != kotae:
			print(n,m,k,a,b)
			sys.exit()
print(solv()-1)

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

suma = [0 for i in range(len(a)+1)]
sumb = [0 for i in range(len(b)+1)]

for i in range(1, len(a)+1):
    suma[i] = suma[i-1] + a[i-1]

for i in range(1, len(b)+1):
    sumb[i] = sumb[i-1] + b[i-1]

mnum = 0
best = len(sumb)
for i in range(len(suma)):
    for j in range(best)[::-1]:
        if suma[i] + sumb[j] <= k:
            mnum = max(mnum, i+j)
            best = j+1
            break

print(mnum)

Solution (Python):
import sys

def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  
def S(): return sys.stdin.readline().rstrip()

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


a = 0
b = N-1

r = sum(A)
ans = 0
while a <= N:
    if r > K:
        r -= C[a]
        a += 1
    else:
        ans = max(ans,b-a+1)
        b += 1
        if b < len(C):
            r += C[b]
        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 = [int(s) for s in input().split()]
B = [int(s) for s in input().split()]

time = 0
maxBooks = 0

for i in range(N):
  if K - time >= A[i]:
    time += A[i]
    maxBooks += 1
  else:
    break

A_count = maxBooks - 1
nowBooks = maxBooks

for i in range(M):
  time += B[i]
  nowBooks += 1
  
  while K < time:
    if A_count < 0:
      nowBooks = 0
      break
    time -= A[A_count]
    A_count -= 1
    nowBooks -= 1
      
  if maxBooks < nowBooks:
    maxBooks = nowBooks
    
print(maxBooks)

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

def csum(l,ll):
    s = 0
    res = [0] * ll
    for i in range(ll):
        s += l[i]
        res[i] = s
    return res

csln = csum(ln,n)
cslm = csum(lm,m)

import numpy as np

lk = [k] + [k-x for x in csln if k-x >=0]
res = np.searchsorted(np.array(cslm), np.array(lk), side='right')
rmax = 0
for i,j in zip(range(len(lk)+1), res):
    rmax = max(rmax, i+j)
print(rmax)

Solution (Python):
N, M, K = [int(i) for i in input().split()]

array_A = [int(i) for i in input().split()]
array_B = [int(i) for i in input().split()]

array_A_2 = [0]
array_B_2 = [0]

for a in array_A:array_A_2.append(array_A_2[- 1]+a)
for b in array_B:array_B_2.append(array_B_2[- 1]+b)

output, j = 0, M

for i in range(N + 1):
    if array_A_2[i] > K:
        break
    while array_B_2[j] > K - array_A_2[i]:
        j -= 1
    output = max(output, i + j)

print(output)

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

S = [0]

Sum = 0


for i in range(N):
  K = K - A[i]
  if K >= 0:
    Sum += 1
  else:
    K = K + A[i]
    break
else:
  i = N
S.append(Sum)

for j in range(M):
  K = K - B[j]
  if K >= 0:
    Sum += 1
  else:
    if i == 0:
      break
    for k in range(i):
      K = K + A[i-k-1]
      
      Sum = Sum - 1
      if K >= 0:
        i = i-k-1
        break
    else:
      i = 0
      continue
    if K >= 0:
      Sum += 1
    
  S.append(Sum)
 
print(max(S))

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()))
SA = list(itertools.accumulate(A))
SB = list(itertools.accumulate(B))
ans = 0
j = len(B)-1
for i in range(len(SA)):
    while(j>=0):
        if SA[i] + SB[j] <= K:
            ans = max(ans,i+j)
            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()))
subseqA = [0] 
subseqB = [0]
for elem in a:
    subseqA.append(subseqA[-1]+elem)
for elem in b:
    subseqB.append(subseqB[-1]+elem)
maxBooks = 0
curABooks = 0
curBBooks = 0
for i in range(N+1):
    if K < subseqA[i]:
        curABooks = i - 1
        maxBooks = i - 1
        break
if subseqA[N] <= K:
    curABooks = N
for i in range(curABooks,-1,-1):
    for j in range(curBBooks + 1,M+1):
        if subseqA[i] + subseqB[j] > K:
            curBBooks = j - 1
            break
    if subseqA[i] + subseqB[M] <= K:
        curBBooks = M
    if i + curBBooks > maxBooks:
        maxBooks = i + curBBooks
print(maxBooks)

Solution (Python):
import sys
input = sys.stdin.readline


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

    pre_A = [0] * (N + 1)
    pre_B = [0] * (M + 1)


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

    for i in range(1,M+1):
        pre_B[i]= pre_B[i-1] + B[i-1]
        if pre_B[i] > K:
            idx_b = i-1
            break
    else:
        idx_b = M

    ans = max(idx_a,idx_b)

    j = idx_b
    for i in range(idx_a + 1):
        while pre_B[j] > K - pre_A[i]:
            j -= 1
        ans = max(ans,i+j)

    return ans

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

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

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

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

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):
def main():
    N, M, K = map( int, input().split())
    A = list( map( int, input().split()))
    B = list( map( int, input().split()))
    indb = M-1
    now = sum(B)
    count = M
    while now > K:
        now -= B[indb]
        count -= 1
        indb -= 1
 
    ans = count
    for a in A:
        
        now += a
        count += 1
        while now > K and indb >= 0:
            now -= B[indb]
            count -= 1
            indb -= 1
        if now <= K:
            if ans < count:
                ans = count
    print(ans)
if __name__ == '__main__':
    main()

Solution (Python):
from bisect import *
def solve():
    ans = 0
    N, M, K = map(int, input().split())
    A = list(map(int, input().split()))+[float('inf')]
    B = list(map(int, input().split()))+[float('inf')]
    cumA = [0]*(N+1)
    cumB = [0]*(M+1)
    for i in range(N):
        cumA[i+1] = cumA[i]+A[i]
    for i in range(M):
        cumB[i+1] = cumB[i]+B[i]
    for i in range(N+1):
        rest = K-cumA[i]
        if rest<0:
            break
        ind = bisect_right(cumB,rest)
        ans = max(ans,i+ind-1)
    return ans
print(solve())

Solution (Python):
from itertools import accumulate
from bisect import bisect_right
 
R = lambda: map(int, input().split())
*_, k = R()
a = accumulate(R())
b = list(accumulate(R()))
r = bisect_right(b, k)
for i, x in enumerate(a, 1):
  if x > k:
    break
  r = max(r, i + bisect_right(b, k - x)) 
print(r)

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

Asum=[0]
for i in A:
  if Asum[-1]+i>K:break
  Asum.append(Asum[-1]+i)
import numpy as np
Asum=np.array(Asum)
Bsum=[0]
for i in B:
  if Bsum[-1]+i>K:break
  Bsum.append(Bsum[-1]+i)
Bsum=np.array(Bsum)

maxval=len(Asum)-1

tmp=0
lenB=len(Bsum)
lenA=len(Asum)
for i in range(lenA):
  if lenA-1-i+lenB<maxval:break
  for j in range(lenB):
    j=tmp+j
    if Asum[-i-1]+Bsum[j]>K:
      maxval=max(maxval,len(Asum)-1-i+j-1)
      tmp=j
      break
    elif j==lenB-1:
      maxval=max(maxval,lenA-1-i+j)
      break
print(maxval)

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

sum_A = list(accumulate(A))
sum_B = list(accumulate(B))





list_N = [0 for i in range(len(A)+1)]

pre_n = len(B)

for i in range(len(A)+1):
    if i == 0:
        num_B = 0
        for j in range(pre_n)[::-1]:
            if sum_B[j] <= K:
                num_B = j+1
                break
            else:
                continue
        
        pre_n = num_B
        list_N[i] = i + num_B
    else:
        if K>=sum_A[i-1]:
            num_B = 0
            for j in range(pre_n)[::-1]:
                if sum_B[j] <= (K-sum_A[i-1]):
                    num_B = j+1
                    break
                else:
                    continue
            
            pre_n = num_B
            list_N[i] = i + num_B
            
            
        else:
            list_N[i] = 0
    





print(max(list_N))

Solution (Python):
N,M,K = map(int,input().split())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
AA = [0]
BB = [0]
r = 0
for i in A:
    r += i
    AA.append(r)
r = 0
for i in B:
    r += i
    BB.append(r)
ans = 0
a = N
b = 0
while AA[a]+BB[b] > K:
    a -= 1
ans = a+b

while a >= 0:
    while b < M:
        if AA[a]+BB[b+1] <= K:
            b += 1
        else:
            break
    if ans < a+b:
        ans = a+b
    if b == M:
        break
    a -= 1
print(ans)

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

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

ans = 0
acounter = 0
bcounter = 0
ans_counter = 0

while True:
  if acounter == len(a):
    break
  if ans + a[acounter] <= k:
    ans += a[acounter]
    acounter += 1
  else:
    break
while True:
  if bcounter == len(b):
    break
  if ans+b[bcounter] <= k:
    ans += b[bcounter]
    bcounter += 1
  else:
    break
ans_counter = acounter + bcounter

while acounter > 0:
  acounter -= 1
  ans -= a[acounter]
  while True:
    if bcounter == len(b):
      break
    if ans + b[bcounter] <= k:
      ans += b[bcounter]
      bcounter += 1
    else:
      break
  
  ans_counter = max(ans_counter, bcounter+acounter)
print(ans_counter)

Solution (Python):


import sys

def input(): return sys.stdin.readline()
def iIn(): return int(input())
def iInM(): return map(int, input().split())
def iInLH(): return list(map(int, input().split()))

sys.setrecursionlimit(10 ** 6)


N, M, K = iInM()
A = iInLH()
B = iInLH()

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

id_b = -1
for j in range(M):
    if t_pre + B[j] <= K: 
        id_b += 1
        t_pre += B[j]
    else:
        break

ans = (id_a + 1) + (id_b + 1)
if id_a < 0:
    print(ans)
    exit()

while (id_a >= 0):
    t_pre -= A[id_a]
    id_a -= 1
    for j in range(id_b+1, M):
        if t_pre + B[j] <= K:
            id_b += 1
            t_pre += B[j]
        else:
            break
    if ans < (id_a + 1) + (id_b + 1):
        ans = (id_a + 1) + (id_b + 1)
    


print(ans)

Solution (Python):
def input_int():
    return map(int, input().split())

def one_int():
    return int(input())

def one_str():
    return input()

def many_int():
    return list(map(int, input().split()))

N, M, K = input_int()
A = many_int()
B = many_int()


import itertools as it
import bisect

def bins(A, B, K):
    A_it = list(it.accumulate(A))
    B_it = list(it.accumulate(B))

    A_index = bisect.bisect_right(A_it, K)
    B_index = bisect.bisect_right(B_it, K)

    A_it = [0]+A_it[:A_index]
    B_it = B_it[:B_index]

    maxs = 0
    count=0
    for i in A_it[::-1]:
        if K-i < 0:
            continue
        now = K-i
        B_index = bisect.bisect_right(B_it, now)

        maxs = max(maxs, B_index + max(A_index - count,0))
        count+=1

    return maxs

print(bins(A,B,K))

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

A_ruiseki = [0]
for i in range(n):
    if A_ruiseki[-1]+A[i] <= k :
        A_ruiseki.append(A_ruiseki[-1]+A[i])
    else:
        break

B_ruiseki = [0]
for i in range(m):
    if B_ruiseki[-1]+B[i] <= k :
        B_ruiseki.append(B_ruiseki[-1]+B[i])
    else:
        break

ans = len(A_ruiseki)-1

for i in range(len(A_ruiseki)):
    time = A_ruiseki[i]
    expected = max(ans-i, 0)

    if len(B_ruiseki) <= expected:
        continue

    if time + B_ruiseki[expected] > k:
        continue

    time += B_ruiseki[expected]

    for j in range(expected, len(B)):
        if time + B[j] > k:
            break

        time += B[j]
        ans =i+j+1

print(ans)

Solution (Python):
n, m, k = map(int, input().split())
aes = list(map(int, input().split()))
bs = list(map(int, input().split()))
r_aes = [0]*n
for i in range(1,n):
    aes[i] += aes[i-1]
for i in range(1,m):
    bs[i] += bs[i-1]
cnt_a = 0
for i in range(n):
    if aes[i] > k:
        break
    cnt_a += 1
cnt_b = 0
res = cnt_a

while True:
    
    
    if cnt_b >= m or bs[cnt_b] > k:
        break
    if cnt_a >= 1 and k-aes[cnt_a-1]>=bs[cnt_b]:
        cnt_b += 1
        res = max(res,cnt_a+cnt_b)
        continue
    if cnt_a < 1 and k>=bs[cnt_b]:
        cnt_b += 1
        res = max(res,cnt_b)
        continue
    cnt_a -= 1
    if cnt_a < -1:
        break
    
        
print(res)

Solution (Python):
n,m,k=list(map(int,input().split()))
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=0
for i in range(1,n):
    if a[i]+a[i-1]<=k:
        a[i]+=a[i-1]
        c=i

    else:
        break
    


d=0 

for i in range(1,m):
    if b[i]+b[i-1]<=k:
        b[i]+=b[i-1]
        d=i

    else:
        break


if a[0]<=k:
    mx=c+1
else:
    mx=0
j=0
while c>=0 and j<=d:
    if a[c]+b[j]<=k:
        
        j+=1
        mx=max(mx,c+1+j)
        
    else:
        c-=1
if b[0]<=k:
    mx=max(mx,d+1)
print(mx)

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

ans = 0

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

print (ans)

Solution (Python):

import sys, re
from collections import deque, defaultdict, Counter, OrderedDict
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from heapq import heappush, heappop, heapify, nlargest, nsmallest
def STR(): return list(input())
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def list2d(a, b, c): return [[c] * b for i in range(a)]
def sortListWithIndex(listOfTuples, idx):   return (sorted(listOfTuples, key=lambda x: x[idx]))
def sortDictWithVal(passedDic):
    temp = sorted(passedDic.items(), key=lambda kv: (kv[1], kv[0]))
    toret = {}
    for tup in temp:
        toret[tup[0]] = tup[1]
    return toret
def sortDictWithKey(passedDic):
    return dict(OrderedDict(sorted(passedDic.items())))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

def solve(N,M,p1,p2):
    ans = 0

    for i in range(N):
        left = k - p1[i]

        lo = 0
        hi = M - 1

        temp = (i + 1)

        if (left >= 0):
            while (lo <= hi):
                
                mid = (lo + hi) // 2

                if (p2[mid] > left):
                    hi = mid - 1
                else:
                    lo = mid + 1
                    ans = max(ans, temp + lo)
            ans = max(ans, temp + lo)


    return ans


n,m,k = MAP()

a = LIST()

b = LIST()

prefA = [0]*n
prefA[0] = a[0]

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


pref = [0]*m

pref[0] = b[0]

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




print(max(solve(n,m,prefA,pref), solve(m,n,pref,prefA)))

Solution (Python):
n, m, k = (int(x) for x in input().split())

a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]

K = k
ans = 0

i = 0
while i<n and K-a[i]>=0:
    K -= a[i]
    i += 1
    
    
ans = i

    

l = 0






for j in range(i, -1, -1):
    while l<m and K-b[l]>=0:
        K -= b[l]
        l += 1
    ans = max(ans, j+l)
    if j != 0:
        K += a[j-1]




while l<m and K-b[l]>=0:
    K -= b[l]
    l += 1
    ans = max(ans, l)

print(ans)

Solution (Python):
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
for i in range(1, len(A)):
    A[i] += A[i - 1]
for i in range(1, len(B)):
    B[i] += B[i - 1]
A.insert(0, 0)
B.insert(0, 0)
r = 0
for i, a in enumerate(A):
    min_idx = 0
    max_idx = len(B) - 1
    while min_idx < max_idx:
        j = (min_idx + max_idx + 1) // 2
        k = K - A[i] - B[j]
        if k < 0:
            max_idx = j - 1
        else:
            min_idx = j
    j = min_idx
    while j + 1 < len(B) and K - A[i] - B[j + 1] >= 0:
        j += 1
    if K - A[i] - B[j] >= 0:
        r = max(r, i + j)
print(r)

Solution (Python):
import bisect

from collections import deque

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

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

aa = [0]*(n+1)

bb = [0]*(m+1)

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

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

count = 0

for i in range(n+1):
    if k < aa[i]:
        break
    serch = k-aa[i]
    index = bisect.bisect_right(bb, serch)
    index -= 1
    count = max(index+i,count)
print(count)

Solution (Python):
import sys
n,m,k=map(int,input().split())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]

ans = 1
c = [0]
d = [0]
for i in range(1,n+1):
    c.append(c[i-1] + a[i-1])
    if(c[i] > k):
        c.pop()
        break
for i in range(1,m+1):
    d.append(d[i-1] + b[i-1])
    if(d[i] > k):
        d.pop()
        break


leng = len(c)
lend = len(d)
ans = leng - 1
temp = 0
for i in range(leng):
    p = c[leng-i-1]
    t = temp
    while(t < lend and p+d[t] <= k):
        t += 1
        if(t==lend):
            print(max(ans,leng-i-2+t))
            sys.exit()
    ans = max(ans,leng-i-2+t)
    temp = t

print(ans)

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

s=0
ac=0
bc=0
max=0

for i in range(n):
  s=s+a[i]
  ac=ac+1
  if s > k:
    s=s-a[i]
    ac=ac-1
    break
for i in range(m):
  s=s+b[i]
  bc=bc+1
  if s > k:
    s=s-b[i]
    bc=bc-1
    break

max=ac+bc

flag=0
while flag==0 and ac!=0:
  ac=ac-1
  s=s-a[ac]
  while s <= k:
    bc=bc+1
    if bc > m:
      flag=1
      break
    s=s+b[bc-1]
  bc=bc-1
  if flag==0:
    s=s-b[bc]
  
  if max < ac+bc:
    max=ac+bc
    
print(max)

Solution (Python):
import bisect
import itertools
n ,m ,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
a = list(itertools.accumulate(a))
b = list(itertools.accumulate(b))
A =bisect.bisect_left(a,k)
B =bisect.bisect_left(b,k)
if B<m:
  if b[B]==k:
    B+=1


if A<n:
  if a[A]==k:
    A+=1
count=max(A,B)
if b[0]>k:
  print(A)
  exit()
  
if a[0]>k:
  B =bisect.bisect_left(b,k)
  if B<m:
    if b[B]==k:
      B+=1
  print(B)
  exit()

for i in range(A,0,-1):
  ans=k-a[i-1]
  B =bisect.bisect_left(b,ans)
  if B<m:
    if b[B]==ans:
      B+=1
  count=max(count,i+B)
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
t = 0
a = 0
b = 0

while True:
    if not t + A[a] > K:
        t += A[a]
        a += 1
        
        if a >= N:
            break
    else:
        break

flug = True     
while True:
    if not t + B[b] > K:
        t += B[b]
        b += 1
        
        if b >= M:
            flug = False
            break
    else:
        break


ans = a + b
flug2 = True    
if flug:
    while True:
        a -= 1
        if a < 0:
            break
        t -= A[a]
        while True:
            if not t + B[b] > K:
                t += B[b]
                b += 1
                if b >= M:
                    flug2 = False
                    break
            else:
                break
        ans = max(ans, a + b)
        if not flug2:
            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()))
ruiseki_a = [0]
for i in range(n):
         ruiseki_a.append(a[i] + ruiseki_a[i])
ruiseki_b = [0]
for i in range(m):
         ruiseki_b.append(b[i] + ruiseki_b[i])
ans = 0
aread = bisect.bisect_right(ruiseki_a,k) - 1
for i in range(aread,-1,-1):
         zantime = k - ruiseki_a[i]
         bread = bisect.bisect_right(ruiseki_b,zantime) - 1
         tmp = (i + bread)
         if ans < tmp:
                  ans = tmp
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()))
timea=[0]
timeb=[0]
time=0
ans=0
for i in range(N):
    time+=A[i]
    if time>K:
        break
    timea.append(time)
time=0
for i in range(M):
    time+=B[i]
    if time>K:
        break
    timeb.append(time)
for i in range(len(timea)):
    b=K-timea[i]
    if b<0:
        break
    num=bisect.bisect_right(timeb,b)
    num+=i-1
    ans=max(ans,num)
if ans==10**10:
    print(0)
else:
    print(ans)

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

a = [False]*N
b = [False]*M

ans = 0
now = 0
bf = 0

for i in range(N):
    if now + A[i] > K:
        break
    now += A[i]
    ans += 1
    a[i] = True

for i in range(M):
    if now + B[i] > K:
        break
    now += B[i]
    ans += 1
    b[i] = True
    bf = i

if all(a) and all(b):
    print(ans)
    exit()

if not any(a) and not any(b):
    print(ans)
    exit()

n = K - now
ans2 = ans
for i in reversed(range(N)):
    if a[i]:
        a[i] = False
        n += A[i]
        ans2 -= 1
        for j in range(bf,M):
            if not b[j]:
                if n - B[j] < 0:
                    break
                b[j] = True
                bf = j
                n -= B[j]
                ans2 += 1
    if ans2 > ans:
        ans = ans2

print(ans)

Solution (Python):
import bisect

def cumsum(a):
    s = 0
    sums = [0]
    for a_i in a:
        s+=a_i
        sums.append(s)

    return sums

def main():
    n,m,k = tuple([int(t)for t in input().split()])

    a = [int(t)for t in input().split()]
    b = [int(t)for t in input().split()]

    acusum = cumsum(a)
    bcusum = cumsum(b)

    a_index = bisect.bisect(acusum,k)
    b_index = bisect.bisect(bcusum,k-acusum[a_index-1])

    ans = a_index+b_index-2

    while a_index > 0:
        a_index -= 1
        b_index = bisect.bisect(bcusum,k-acusum[a_index-1])

        ans = max(a_index+b_index-2,ans)
    
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import bisect
anum, bnum, time= map(int, input().split(" "))
a = list(map(int, input().split(" ")))
b = list(map(int, input().split(" ")))
tempa = [0]
tempb = [0]
total = 0
for i in range(anum):
    tempa.append(tempa[-1] + a[i])
    if total > time:
        break
for i in range(bnum):
    tempb.append(tempb[-1] + b[i])
    if total > time:
        break
maxi = 0
starta = bisect.bisect_right(tempa, time) - 1
for i in range(starta, -1, -1):
    startb = bisect.bisect_right(tempb, time - tempa[i]) - 1
    if i + startb > maxi:
        maxi = i + startb
print(maxi)

Solution (Python):
from collections import deque

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

cnt = 0
cnt_max = 0

t = 0




c = []

for i in a:
  t += i
  if t <= k:
    c.append(i)
    cnt += 1
  else:
    t -= i
    break


cnt_max = cnt
tmp_cnt = cnt

t_max = t

c.reverse()
for i in range(len(c) + 1):
  d = []
  
  if i == 0:
    pass
  else:
    t_max -= c[i-1]
    tmp_cnt -= 1
    
  
  
  t = t_max
  cnt = tmp_cnt
  
  for j in b:
    t += j
    
    if t <= k:
      cnt += 1
      d.append(j)
      t_max = t
      if cnt > cnt_max:
        cnt_max = cnt
        
    else:
      break
      
  for i in range(len(d)):
    b.popleft()
    tmp_cnt += 1
      
print(cnt_max)

Solution (Python):
import bisect
N,M,K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
timea=[0]
timeb=[0]
time=0
ans=0
for i in range(N):
    time+=A[i]
    if time>K:
        break
    timea.append(time)
time=0
for i in range(M):
    time+=B[i]
    if time>K:
        break
    timeb.append(time)
for i in range(len(timea)):
    b=K-timea[i]
    if b<0:
        break
    num=bisect.bisect_right(timeb,b)
    num+=i-1
    ans=max(ans,num)
if ans==10**10:
    print(0)
else:
    print(ans)

Solution (Python):
from collections import deque
import bisect
N,M,K = list(map(int, input().split()))
As = list(map(int, input().split()))
Bs = list(map(int, input().split()))






Asruisekiwa = []
Bsruisekiwa = []

Asruisekiwa.append(As[0])
Bsruisekiwa.append(Bs[0])

for i, j in enumerate(As[1:]):
    Asruisekiwa.append(Asruisekiwa[-1]+j)

for i, j in enumerate(Bs[1:]):
    Bsruisekiwa.append(Bsruisekiwa[-1] + j)




ans = 0



if(Asruisekiwa[0] >= Bsruisekiwa[0]):
    tmp = Asruisekiwa
    Asruisekiwa = Bsruisekiwa
    Bsruisekiwa = tmp

Asruisekiwa.insert(0,0)
Bsruisekiwa.insert(0,0)





for index, i in enumerate(Asruisekiwa):

    if(i > K):
        break

    tmp = i
    maxcheck = K - i


    hoge = bisect.bisect_right(Bsruisekiwa, maxcheck)

    ans = max(ans, index-1 + hoge)



print(ans)