Problem:
 Score : 300 points    Problem Statement We have two desks: A and B. Desk A has a vertical stack of N books on it, and Desk B similarly has M books on it.  It takes us A_i minutes to read the i -th book from the top on Desk A (1 \leq i \leq N) , and B_i minutes to read the i -th book from the top on Desk B (1 \leq i \leq M) .  Consider the following action:   Choose a desk with a book remaining, read the topmost book on that desk, and remove it from the desk.   How many books can we read at most by repeating this action so that it takes us at most K minutes in total? We ignore the time it takes to do anything other than reading.      Constraints  1 \leq N, M \leq 200000  1 \leq K \leq 10^9  1 \leq A_i, B_i \leq 10^9  All values in input are integers.         Input Input is given from Standard Input in the following format:  N  M  K  A_1  A_2  \ldots  A_N  B_1  B_2  \ldots  B_M       Output Print an integer representing the maximum number of books that can be read.        Sample Input 1 3 4 240
60 90 120
80 150 80 150      Sample Output 1 3  In this case, it takes us 60 , 90 , 120 minutes to read the 1 -st, 2 -nd, 3 -rd books from the top on Desk A, and 80 , 150 , 80 , 150 minutes to read the 1 -st, 2 -nd, 3 -rd, 4 -th books from the top on Desk B, respectively.  We can read three books in 230 minutes, as shown below, and this is the maximum number of books we can read within 240 minutes.   Read the topmost book on Desk A in 60 minutes, and remove that book from the desk.  Read the topmost book on Desk B in 80 minutes, and remove that book from the desk.  Read the topmost book on Desk A in 90 minutes, and remove that book from the desk.        Sample Input 2 3 4 730
60 90 120
80 150 80 150      Sample Output 2 7       Sample Input 3 5 4 1
1000000000 1000000000 1000000000 1000000000 1000000000
1000000000 1000000000 1000000000 1000000000      Sample Output 3 0  Watch out for integer overflows.

Solution (Python):

import bisect
import sys


buff_readline = sys.stdin.readline
readline = sys.stdin.readline

INF = 2**62-1


def read_int():
    return int(buff_readline())


def read_int_n():
    return list(map(int, buff_readline().split()))


def read_float():
    return float(buff_readline())


def read_float_n():
    return list(map(float, buff_readline().split()))


def read_str():
    return readline().strip()


def read_str_n():
    return readline().strip().split()

def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, M, K, A, B):
    from itertools import accumulate
    sa = [0] + list(accumulate(A))
    sb = [0] + list(accumulate(B))

    ans = 0
    for n, a in enumerate(sa):
        if a > K:
            break
        m = bisect.bisect_right(sb, K-a) - 1
        ans = max(ans, n+m)
    return ans


def main():
    N, M, K = read_int_n()
    A = read_int_n()
    B = read_int_n()
    print(slv(N, M, K, A, B))


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()))
s = [0]*(n+1)
t = [0]*(m+1)
x = 0
for i in range(n+1):
    s[i] += x + a[i]
    x = s[i]
if(s[-1] <= k):
    p = n
else:
    for i in range(n+1):
        if(s[i] >= k):
            p = i
            break
x = 0
for i in range(m+1):
    t[i] += x + b[i]
    x = t[i]
if(t[-1] <= k):
    p = min(p,m)
else:
    for i in range(m+1):
        if(t[i] >= k):
            p = max(p,i)
            break

while(1):
    f = 0
    for j in range(p-min(p,m),min(p,n)+1):
        
        if(s[j]+t[p-j] <= k):
            
                f = 1
                break
    if(f == 0):
        break
    p += 1
print(p-1)

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

Solution (Python):
N,M,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
from itertools import accumulate
accumulated1 = [0] + list(accumulate(A))
accumulated2 = [0] + list(accumulate(B)) + [10**10]
def check(x,now):
    if accumulated1[now] + accumulated2[x] <= K:
        return True
    else:
        return False



def bisect(l,r,now):
    if r - l == 1: return  l
    mid = (r+l)//2
    if check(mid,now):
        return bisect(mid,r,now)
    else:
        return bisect(l,mid,now)


ans = 0
for i in range(N+1):
    if accumulated1[i] > K: pass
    else:
        ans = max(ans,bisect(0,M+1,i) + i)
print(ans)

Solution (Python):
def main():
    N, M, K = tuple(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)


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

ri,sum=-1,0
for i in range(M):
    if sum+B[i]<=K:
        ri=i
        sum+=B[i]
    else :
        break

sum2,ans=0,ri+1
for i in range(N):
    sum2+=A[i]
    if sum2>K:
        break
    while ri>=0 and sum2+sum>K:
        sum-=B[ri]
        ri-=1
    ans=max(ans,i+1+ri+1)
print(ans)

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

N,M,K = map(int,readline().split())
A = [0] + list(map(int,readline().split()))
B = [0] + list(map(int,readline().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]

ans = 0
b_ind = len(B) - 1
for i in range(len(A)):
  if A[i] > K:
    break
  while b_ind >= 0 and A[i] + B[b_ind] > K:
    b_ind -= 1
    
  if ans < i + b_ind:
    ans = i + b_ind
    
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])

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(i + j, 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()))
ans = 0
cnta = [0 for i in range(n + 1)]
cntb = [0 for i in range(m + 1)]

for i in range(n):
    cnta[i + 1] = cnta[i] + a[i]
for i in range(m):
    cntb[i + 1] = cntb[i] + b[i]
for i in range(n + 1):
    cnt = 0
    if k - cnta[i] >= 0:
        cnt = i
        cnt += min(m, bisect.bisect_right(cntb, k - cnta[i]) - 1)
    ans = max(ans, cnt)
print(ans)

Solution (Python):
from itertools import*
from bisect import*
(n, m, k), a, b = [list(map(int, o.split())) for o in open(0)]
*a, = accumulate([0] + a)
*b, = accumulate([0] + b)
c = 0
for i in range(n + 1):
    if a[i] > k:
        break
    c = max(c, i + bisect(b, k - a[i]) - 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=[0]
B=[0]
for i in a:
  A.append(i+A[-1])
for i in b:
  B.append(i+B[-1])
  
ans,j = 0,m
for i in range(n+1):
  if A[i] > k:
    break
  while B[j] + A[i] > k:
    j -= 1
  ans=max(ans,i+j)
print(ans)

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





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

ans=0
point=M
for i in range(N+1):
    ans_b = K-a[i]
    for j in reversed(range(point+1)):
        if ans_b>=b[j]:
            ans=max(ans,i+j)
            point=j
            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])

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

print(ans)

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

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

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

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

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

TA = [0]
TB = [0]
na = 0
nb = 0

for i in range(N):
  n = TA[-1]+A[i]
  if n > K:
    break
  TA.append(n)
  na = i + 1

for i in range(M):
  n = TB[-1]+B[i]
  if n > K:
    break
  TB.append(n)
  nb = i + 1

a = 0
ans = 0

for b in range(nb,-1,-1):
  while TB[b]+TA[a] <= K:
    ans = max(ans,a+b)
    a += 1
    if a > na:
      a -= 1
      break

print(ans)

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

    CUSUM_A = [0] * (N+1)
    CUSUM_B = [0] * (M+1)

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

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


    
    ans = 0
    j = M
    for i in range(N+1):
        
        if CUSUM_A[i] > K:
            break

        SUM_VAL = CUSUM_A[i] + CUSUM_B[j]
        
        while SUM_VAL > K:
            j -= 1
            SUM_VAL = CUSUM_A[i] + CUSUM_B[j]
        
        ans = max(ans, i+j)
        
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
a, 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):

"""
Created on Sun Jun 28 13:30:11 2020

@author: NEC-PCuser
"""

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

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

a_li = [0, A[0]]

for i in range(1, len(A)):
    if (a_li[len(a_li) - 1] + A[i] > K):
        break
    
    a_li.append(a_li[len(a_li) - 1] + A[i])
    
    
j = len(a_li)
ans = -1
count = 0
b_sum = 0
for i in range(-1, len(B)):
    if (i != -1):
        b_sum += B[i]
        if b_sum > K:
            break
    while (b_sum + a_li[j - 1] > K):
        j -= 1
    count = j + 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()))

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

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

N, M, K = map(int, readline().split())
A = list(map(int, readline().split()))
B = list(map(int, readline().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])

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

Solution (Python):
from itertools import accumulate
import bisect
n, m, k = map(int, input().split())
A = list(map(int, input().split()))
b = list(map(int, input().split()))
A.insert(0, 0)
b.insert(0, 0)

A = list(accumulate(A))
b = list(accumulate(b))

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

print(ans)

Solution (Python):
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections
import copy
import bisect
INF = 10**18

if __name__ == "__main__":
    n,m,k = map(int,input().split())
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))
    preA = [0]
    for i in range(n):
        preA.append(preA[i] + a[i])
    preB = [0]
    for i in range(m):
        preB.append(preB[i]+b[i])
    ans = 0
    for i in range(n+1):
        if preA[i] <= k:
            find = bisect.bisect_right(preB,k-preA[i])
            ans = max(ans,find+i-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]
aa = 0
for i in range(1,N+1):
    aa += A[i-1]
    a.append(aa)
    
b = [0]
bb = 0
for i in range(1,M+1):
    bb += B[i-1]
    b.append(bb)
    
time0 = 1
ans = 0
j = M
for i in range(N+1):
    if a[i] > K:
        break
    else:
        while(j >= 0):
            if b[j] <= K - a[i]:
                ans = max(ans, i+j)
                break
            else:
                j -= 1
        
        
            
print(ans)

Solution (Python):
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections
import copy
import bisect
INF = 10**18

if __name__ == "__main__":
    n,m,k = map(int,input().split())
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))
    preA = [0]
    for i in range(n):
        preA.append(preA[i] + a[i])
    preB = [0]
    for i in range(m):
        preB.append(preB[i]+b[i])
    ans = 0
    preB.pop(0)
    for i in range(n+1):
        if preA[i] <= k:
            find = bisect.bisect_right(preB,k-preA[i])
            ans = max(ans,find+i)
    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()))
count=0
time=0
A_ruiseki=[0]
B_ruiseki=[0]
for i in range(N):
    A_ruiseki.append(A_ruiseki[-1]+A[i])
for j in B:
    B_ruiseki.append(B_ruiseki[-1]+j)
B_ruiseki.pop(0)
for i in range(N+1):
    time=A_ruiseki[i]
    if A_ruiseki[i]<=K:
        count=max(count,bisect_right(B_ruiseki,K-A_ruiseki[i])+i)
        
print(count)

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

acum = [0] * (n + 1)
bcum = [0] * (m + 1)

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

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

max_ans = 0
j = m
for i in range(n + 1):
    if acum[i] > k:
        break
    while acum[i] + bcum[j] > k:
        j -= 1
    max_ans = max(max_ans, i + j)

print(max_ans)

Solution (Python):

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


    
if __name__ == '__main__':
    main()

Solution (Python):
def main2():
    n, m, k = list(map(int, input().split()))
    a_books = list(map(int, input().split()))
    b_books = list(map(int, input().split()))

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

    for i in range(len(b_books)):
        b.append(b[i] + b_books[i])
    ans, j  = 0, len(b_books)
    
    for i in range(len(a)):
        if a[i] > k :
            break
        while b[j] > k - a[i]:
            j -= 1
        ans = max(ans, i+j)
    print(ans)


if __name__ == '__main__':
    main2()

Solution (Python):
def main():
    import sys
    def input(): return sys.stdin.readline().rstrip()
    n, m, k = map(int, input().split())
    from collections import deque
    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)


if __name__ == '__main__':
    main()

Solution (Python):
from bisect import bisect_right

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

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):
  cntA = i
  rest = K - SA[i]
  if rest < 0:
    break
  cntB = bisect_right(SB, rest) - 1
  ans = max(ans, cntA+cntB)

print(ans)

Solution (Python):
from bisect import bisect_right

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

Solution (Python):
from bisect import bisect_right

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

Solution (Python):

import sys
from bisect import bisect_left, bisect_right
N,M,K=map(int, sys.stdin.readline().split())
A=map(int, sys.stdin.readline().split())
B=map(int, sys.stdin.readline().split())
ASUM=[0]
BSUM=[0]
for a in A: ASUM.append(ASUM[-1]+a)
for b in B: BSUM.append(BSUM[-1]+b)

ans=0
for ai in range(N+1):   
    nokori=K-ASUM[ai]
    if 0<=nokori:   
        ans=max(ans,ai+bisect_right(BSUM,nokori)-1)   
print ans

Solution (Python):
import bisect
n,m,k = map(int,input().split())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]

Sum_A = [0]
Sum_B = [0]
for i in range(n):
    Sum_A.append(Sum_A[-1]+A[i])
for i in range(m):
    Sum_B.append(Sum_B[-1]+B[i])
ans = 0
for i in range(n+1):
    nokori = k-Sum_A[i]
    if nokori<0:
        continue
    index = bisect.bisect_right(Sum_B,nokori)
    ans = max(ans,i+index-1)
print(ans)

Solution (Python):
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))



N,M,K = LI()
A = LI()
B = LI()

cusumA = [A[0]]*N
cusumB = [B[0]]*M

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

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

ans = 0
for i in range(N):
    left = K-cusumA[i]
    if left < 0:
        break
    p = bisect_right(cusumB,left)
    num = i+1+p
    if num > ans:
        ans = num

p = bisect_right(cusumB,K)
if p > ans:
    ans = p

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 sys
from itertools import accumulate


def _tsundoku(n, m, k, a, b):
    a_temp = tuple(accumulate(a, initial=0))
    b_temp = tuple(accumulate(b, initial=0))

    ans = 0
    b_index = m
    for a_index in range(n + 1):
        if a_temp[a_index] > k:
            break
        while a_temp[a_index] + b_temp[b_index] > k:
            b_index -= 1
        else:
            ans = max(ans, a_index + b_index)
    return ans


def tsundoku(f):
    readline = f.readline
    n, m, k = map(int, readline().split())
    a = list(map(int, readline().split()))
    b = list(map(int, readline().split()))

    return _tsundoku(n, m, k, a, b)


def main():
    ans = tsundoku(sys.stdin.buffer)
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):


from itertools import accumulate
import bisect
import sys
input = sys.stdin.readline
N,M,K = (int(i) for i in input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
acuum_A =list(accumulate(A))
acuum_B =list(accumulate(B))


ans =[]
a =bisect.bisect_right(acuum_A,K)
remainder =K -acuum_A[a-1]
b =bisect.bisect_right(acuum_B,remainder)
ans.append(a+b)


for i in range(1,N+M):
    ai =a -i
    if ai -1 >=0:
        remainder =K -acuum_A[ai-1]
    else:
        remainder =K
    b =bisect.bisect_right(acuum_B,remainder)
    ans.append(ai+b)
    if b==M:
        break
    

print(max(ans))

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

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

Solution (Python):
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))

import numpy as np

n,m,k = iim()
a = iil()
cuma = [0]
for i in a:
    cuma.append(cuma[-1]+i)
b = iil()
cumb = [0]
for i in b:
    cumb.append(cumb[-1]+i)

best = m
ans = 0
for i in range(n+1):
    if cuma[i] > k:
        break
    while cumb[best] > k-cuma[i]:
        best -= 1
    ans = max(ans,best+i)

print(ans)

Solution (Python):
from collections import deque
def readinput():
    n,m,k=list(map(int,input().split()))
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    return n,m,k,a,b

def main(n,m,k,a,b):
    qa=deque(a)
    qb=deque(b)
    count=0
    res=k
    while(len(qa)>0 and len(qb)>0):
        if qa[0] < qb[0]:
            res-=qa[0]
            qa.popleft()
        else:
            res-=qb[0]
            qb.popleft()
        if res<0:
            return count
        count+=1
    else:
        if len(qa)==0:
            while(len(qb)>0):
                res-=qb[0]
                qb.popleft()
                if res<0:
                    return count
                count+=1
            return count
        else:
            while(len(qa)>0):
                res-=qa[0]
                qa.popleft()
                if res<0:
                    return count
                count+=1
            return count

import sys
INFTY=sys.maxsize
def find(t,s):
    '''
    単調増加配列sからs[i]がtを超えない最大のiを返す
    '''
    ss=s[:]
    ss.append(INFTY)
    left=0
    right=len(ss)
    while(left<right):
        middle=(left+right)//2
        
        if ss[middle]==t:
            
            return middle
        if t<ss[middle]:
            right=middle
        else:
            left=middle+1
    else:
        return left-1

def main2(n,m,k,a,b):
    sa=[0]
    for i in range(1,n+1):
        sa.append(sa[i-1]+a[i-1])
    sb=[0]
    for i in range(1,m+1):
        sb.append(sb[i-1]+b[i-1])
    maxab=0
    nb=m+1
    for na in range(0,n+1):
        res=k-sa[na]
        if res>=0:
            nb=find(res,sb[:nb+1])
            
            maxab=max(maxab,na+nb)
    return maxab

from bisect import bisect_left
def main3(n,m,k,a,b):
    sa=[0]
    for i in range(1,n+1):
        sa.append(sa[i-1]+a[i-1])
    sb=[0]
    for i in range(1,m+1):
        sb.append(sb[i-1]+b[i-1])
    maxab=0
    nb=m+1
    for na in range(0,n+1):
        res=k-sa[na]
        if res>=0:
            nb=bisect_left(sb[:nb+1],res)
            
            maxab=max(maxab,na+nb)
    return maxab

def main4(n,m,k,a,b):
    sa=[0]
    for i in range(1,n+1):
        sa.append(sa[i-1]+a[i-1])
    sb=[0]
    for i in range(1,m+1):
        sb.append(sb[i-1]+b[i-1])
    maxab=0
    nb=m
    for na in range(0,n+1):
        res=k-sa[na]
        while res<sb[nb] and nb>=0:
            nb-=1
        else:
            if nb>=0:
                maxab=max(maxab,na+nb)
        
    return maxab

def readinput2():
    m=2*10**5
    n=2*10**5
    a=[10**9]*n
    b=[10**9]*m
    k=1
    return n,m,k,a,b

if __name__=='__main__':
    n,m,k,a,b=readinput()
    ans=main4(n,m,k,a,b)
    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,b = [0],[0]
for i in range(N):
  a.append(a[i]+A[i])
for j in range(M):
  b.append(b[j]+B[j])
ans = 0

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

  while K - a[i] < b[j]:
    j = j-1
  ans = max(ans,i+j)

print(ans)

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


def search(arr, x):
    l, r = -1, len(arr)
    while abs(r - l) > 1:
        mid = (l + r) // 2
        if arr[mid] <= x:
            l = mid
        else:
            r = mid
    return l


ans = 0
for i in range(len(a)):
    rest = k - a[i]
    if rest < 0:
        break
    bidx = search(b, rest)
    if bidx == -1 or bidx == len(b):
        continue
    ans = max(ans, i + bidx)
print(ans)

Solution (Python):


from itertools import accumulate
import bisect
import sys
input = sys.stdin.readline
N,M,K = (int(i) for i in input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
acuum_A =list(accumulate(A))
acuum_B =list(accumulate(B))


ans =[]
a =bisect.bisect_right(acuum_A,K)
remainder =K -acuum_A[a-1]
b =bisect.bisect_right(acuum_B,remainder)
ans.append(a+b)


for i in range(1,N+M):
    ai =a -i
    if ai -1 >=0:
        remainder =K -acuum_A[ai-1]
    else:
        remainder =K
    b =bisect.bisect_right(acuum_B,remainder)
    ans.append(ai+b)
    

print(max(ans))

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

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

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

print(m)

Solution (Python):

import sys
import math as mt
import bisect
input=sys.stdin.buffer.readline  
t=1

for _ in range(t):
    n,m,k=map(int,input().split())
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    pref_a=[0]*(n+1)
    pref_b=[0]*(m+1)
    for i in range(n):
        pref_a[i+1]=pref_a[i]+a[i]
    for i in range(m):
        pref_b[i+1]=pref_b[i]+b[i]
    maxi=0
    for i in range(n+1):
        if k>=pref_a[i]:
            k1=k-pref_a[i]
            pos=bisect.bisect_right(pref_b,k1)        
            maxi=max(maxi,pos+i-1)
        else:
            break
    print(maxi)

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

a_sum = [0]
b_sum = [0]
cnt = 0
best_b = m
confirm = ()

for i in a:
    a_sum.append(i + a_sum[-1])

for i in b:
    b_sum.append(i + b_sum[-1])

for a_idx in range(n + 1):
    if a_sum[a_idx] > k:
        break
    for b_idx in reversed(range(best_b + 1)):
        if b_sum[b_idx] + a_sum[a_idx] <= k:
            
            cnt = max(cnt, a_idx + b_idx)
            best_b = b_idx
            break

print(cnt)

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


def search(arr, x):
    l, r = 0, len(arr)
    while abs(r - l) > 1:
        mid = (l + r) // 2
        if arr[mid] <= x:
            l = mid
        else:
            r = mid
    return l


ans = 0
for i in range(len(a)):
    rest = k - a[i]
    if rest < 0:
        break
    bidx = search(b, rest)
    if bidx == -1 or bidx == len(b):
        continue
    ans = max(ans, i + bidx)
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()))
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])
res=0
for i in range(n+1):
  if x[i]>k:
    break
  else:
    t=k-x[i]
    bb=bisect.bisect(y,t)
    res=max(res,i+bb-1)
print(res)