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

if __name__ == "__main__":
    main()

Solution (Python):
import sys
import math
import itertools
import bisect
from copy import copy,deepcopy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(map(int,input().split()))
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
INF = 10**9
mod = 10**9+7

N,M,K = I()
A = l()
B = l()
ruiseki = [-1]+list(itertools.accumulate(A))
num = 0
ans = bisect.bisect_right(ruiseki,K)-1
for i in range(M):
    num += B[i]
    if num > K:
        break
    ans = max(ans,bisect.bisect_right(ruiseki,K-num)+i)
print(ans)

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

import bisect
for i in range(1,N):
    A[i] += A[i-1]
for i in range(1,M):
    B[i] += B[i-1]


res = bisect.bisect_right(B,K)

for i in range(N):
    if K >= A[i]:
        j=bisect.bisect_right(B,K-A[i])
        res=max(res, i+1+j)

print(res)

Solution (Python):
N, M, K=map(int,input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
LA = [0]
LB = [0]
for i in range(N):
    C=LA[i]+A[i]
    if C>K:
        N=i
        break
    else:
        LA.append(C)
for j in range(M):
    C=LB[j]+B[j]
    if C>K:
        M=j
        break
    else:
        LB.append(C)
r=N
for i in range(N+1):
    u=0
    for j in range(r-N+i,M+1):
        if LA[N-i]+LB[j]>K:
            r=max(r,N-i+j-1) 
            u=1
            break
    if u==0:
        r=max(r,N-i+M)
        break
print(r)

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

output = 0
j = m
for i in range(n + 1):
    if sa[i] > k:
        break
    while sa[i] + sb[j] > k:
        j -= 1
    if output < i + j:
        output = i + j
print(output)

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_acc =[0] + list(accumulate(A))
b_acc =list(accumulate(B))

ans = 0

for an in range(len(a_acc)):
    rem = K - a_acc[an]
    if rem < 0:
        break
    
    bn  = bisect.bisect_right(b_acc, rem)

    ans = max(ans, an+bn)
    
    
print(ans)

Solution (Python):
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

N,M,K=MI()
A=[0]+LI()
B=[0]+LI()
from itertools import accumulate
A=list(accumulate(A))
B=list(accumulate(B))
ans=0
b_cnt=M
for a_cnt in range(N+1):
  if A[a_cnt]>K:
    break
  while A[a_cnt]+B[b_cnt]>K:
    b_cnt-=1
  ans=max(ans,a_cnt+b_cnt)
print(ans)

Solution (Python):
import bisect, numpy
N,M,K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
A=numpy.cumsum(A)
B=numpy.cumsum(B)
ans=bisect.bisect_right(B,K)
for i in range(N):
  if K-A[i]>=0:
    ans=max(ans,i+1+bisect.bisect_right(B,K-A[i]))
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()))

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

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

V = 0
ACount = bisect.bisect_right(A, K)
if ACount !=0:
    V = K- A[ACount-1]
else:
    V=K

AInitial = ACount

BCount = bisect.bisect_right(B, V)
final = ACount+BCount

for i in range (1, AInitial):
    V = K - A[AInitial-1-i]
    BCount = bisect.bisect_right(B, V)
    final = max(final, AInitial-i+BCount)

final = max(final, bisect.bisect_right(B,K))
print(final)

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]

best=0
j=M
for i in range(0,N+1):
  while j>0:
    if A[i]+B[j]<=K:
      break
    else: j-=1
  if A[i]+B[j]<=K:
    best=max(best,i+j)
  pass
print(best)

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

total = 0
idx_a = idx_b = 0
while idx_a < n and total + A[idx_a] <= k:
    total += A[idx_a]
    idx_a += 1
while idx_b < m and total + B[idx_b] <= k:
    total += B[idx_b]
    idx_b += 1

ans = idx_a + idx_b
while idx_a > 0:
    idx_a -= 1
    total -= A[idx_a]
    while idx_b < m and total + B[idx_b] <= k:
        total += B[idx_b]
        idx_b += 1
    ans = max(ans, idx_a + idx_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(" ")))

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]

b_i = M
max_count = 0
for i in range(N+1):
  a = sumA[i]
  if a > K:
    continue
  if b_i>0:
    while a + sumB[b_i] > K:
      b_i -= 1
      if b_i==0:
        break
  max_count = max(max_count, i + b_i)
print(max_count)

Solution (Python):



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


a_sum_list = [0]*(N+1)
b_sum_list = [0]*(M+1)
for i in range(N):
    a_sum_list[i+1] = a_sum_list[i] + a_nums[i]
for i in range(M):
    b_sum_list[i+1] = b_sum_list[i] + b_nums[i]
max_count = 0
b_i = M


for i in range(N+1):
    a_time = a_sum_list[i]

    
    if a_time>K:
        continue

    if b_i>0:
        while a_time+b_sum_list[b_i]>K:
            b_i -= 1
            if b_i==0:
                break
    
    max_count = max(max_count, i + b_i)


print(max_count)

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

a = [0 for _ in range(N+1)]
b = [0 for _ in range(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]
  
num = M
ans = 0
for k in range(N+1):
  if a[k] > K:
    break
  while b[num] > K - a[k]:
    num -= 1
  ans = max(ans, k+num)
  
print(ans)

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

a_=[0]
b_=[0]
tmp = 0
for i in a:
    tmp += i
    a_.append(tmp)
tmp = 0
for i in b:
    tmp += i
    b_.append(tmp)

ans=0
j = m
for i in range(n+1):
    if a_[i] > k:
        continue
    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()))

ans=0
a=[0]*(n+1)
b=[0]*(m+1)

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

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

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


ans = 0
b_count = M
for a_count in range(N+1):
  if A[a_count] > K:
    continue

  while A[a_count] + B[b_count] > K:
    b_count -= 1

  ans = max(ans, a_count + b_count)
print(ans)

Solution (Python):


from queue import Queue
from queue import LifoQueue as Stack
from math import sqrt, floor, ceil, log2, log10, pi
from fractions import gcd
from itertools import permutations, combinations
from operator import itemgetter
from functools import cmp_to_key


INF=1001001001
__MOD__=(10**9)+7
yn = 'YNeos'
judge = False
cnt = 0
ans = None


def lcm(a, b):
    return (a*b) // gcd(a,b)

def intinput():
    return int(input())

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

def lineinputs(func=intinput):
    datas = []
    while True:
        try:
            datas.append(func())
        except EOFError:
            break
    return datas

class ModInt():
    def __init__(self, x):
        self.__x = (x % __MOD__)
    def __add__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x + other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x + other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __radd__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((other.__x + self.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((other.__x + self.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")

    def __sub__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x - other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x - other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __rsub__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((other.__x - self.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((other.__x - self.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")

    def __mul__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x * other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x * other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __rmul__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((other.__x * self.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((other.__x * self.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")

    def __truediv__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x * other.__modinv())%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x * other.__modinv())%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __rtruediv__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((other.__x * self.__modinv())%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((other.__x * self.__modinv())%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")

    def __pow__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__(pow(self.__x, other.__x, __MOD__)))
        elif type(other) == ModInt:
            return self.__class__(pow(self.__x, other.__x, __MOD__))
        else:
            raise Exception("Not Int or Not ModInt")
    def __rpow__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__(pow(other.__x, self.__x, __MOD__)))
        elif type(other) == ModInt:
            return self.__class__(pow(other.__x, self.__x, __MOD__))
        else:
            raise Exception("Not Int or Not ModInt")

    def __modinv(self, m=__MOD__):
        a = self.__x
        if a == 0:
            raise ZeroDivisionError()
        if gcd(a, m) != 1:
            raise Exception("%sの逆数は求まりません。" % a)
        b, u, v = m, 1, 0
        while b != 0:
            t = a//b;
            a -= t*b
            a, b = b, a
            u -= t * v
            u, v = v, u
        u %= m
        if u < 0:
            u += m
        return u
    def __int__(self):
        return self.__x
    def __str__(self):
        return str(self.__x)



if __name__ == '__main__':
    N, M, K = mulinputs()
    an = list(map(int, input().split()))
    bm = list(map(int, input().split()))

























































    
    
    ans, aidx, bidx = 0, 0, 0
    
    while True:
        if aidx == len(an):
            break
        if ans+an[aidx] <= K:
            ans += an[aidx]
            aidx +=1
        else:
            break
    while True:
        if bidx == len(bm):
            break
        if ans + bm[bidx] <= K:
            ans += bm[bidx]
            bidx += 1
        else:
            break
    ansidx = aidx + bidx
    
    while aidx > 0:
        aidx -=1
        ans -= an[aidx]
        while True:
            if bidx == len(bm):
                break
            if ans + bm[bidx] <= K:
                ans += bm[bidx]
                bidx += 1
            else:
                break
        ansidx = max(ansidx, aidx+bidx)
    print(ansidx)

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

DEBUG = False
if DEBUG:
    f=open("202006_5th/C_input.txt")
else:
    f=sys.stdin

M,N,K=map(int,f.readline().split())
ans=0
__=N
M_shelf=list(map(int,f.readline().split()))
N_shelf=list(map(int,f.readline().split()))
M_partial=[]
N_partial=[]
M_partial.append(0)
N_partial.append(0)

for _ in range(M):
    M_partial.append(M_partial[_]+M_shelf[_])
for _ in range(N):
    N_partial.append(N_partial[_]+N_shelf[_])
for _ in range(M+1):
    R=K-M_partial[_]
    if R<0:
        break
    while R<N_partial[__]:
        __-=1
    ans=max(ans,_+__)
print(ans)
f.close()

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

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

Solution (Python):
import bisect

n,m,k = map(int,input().split())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]

A_sum = [0]*(n+1)
B_sum = [0]*(m+1)

for i in range(n):
    A_sum[i+1] = A_sum[i] + A[i]

for i in range(m):
    B_sum[i+1] = B_sum[i] + B[i]

A_limit = bisect.bisect_right(A_sum,k) - 1
mc = 0
B_i = 0
for i in range(A_limit,-1,-1):
    remain_k = k - A_sum[i]
    while((remain_k-B_sum[B_i])>=0):
        B_i += 1
        if(B_i==(m+1)):
            break
    B_i -= 1
    mc = max(mc,(i+B_i))

print(mc)

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

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

Solution (Python):
import sys
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 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 a[i] + b[j] > k:
      j -= 1
  ans = max(ans, i + j)
    
print(ans)

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

Solution (Python):
def main4():
    N, M, K = map(int, input().split())
    A = [int(a) for a in input().split()]
    B = [int(b) for b in input().split()]
    Bsum = sum(B)
    ans = Asum = 0
    idx_b = len(B) - 1

    for i in range(N + 1):

        if i == 0:
            Asum += 0
        else:
            Asum += A[i - 1]

        while Asum + Bsum > K:
            if idx_b >= 0:
                Bsum -= B[idx_b]
                idx_b -= 1
            else:
                break

        if Asum + Bsum <= K:
            ans = max(ans, i + idx_b + 1)

    print(ans)

if __name__ == "__main__":
    main4()

Solution (Python):
def main3():
    N, M, K = map(int, input().split())
    A = [int(a) for a in input().split()]
    B = [int(b) for b in input().split()]
    Bsum = sum(B)
    ans = Asum = 0
    idx_b = len(B) - 1

    for i in range(N + 1):
        if i == 0:
            if Asum + Bsum <= K:
                ans = max(ans, i + idx_b + 1)

            else:
                while Asum + Bsum > K:
                    if idx_b >= 0:
                        Bsum -= B[idx_b]
                        idx_b -= 1
                    else:
                        break
                    
                if Asum + Bsum <= K:
                    ans = max(ans, i + idx_b + 1)

        else:
            Asum += A[i - 1]

            while Asum + Bsum > K:
                if idx_b >= 0:
                    Bsum -= B[idx_b]
                    idx_b -= 1
                else:
                    break
            if Asum + Bsum <= K:
                ans = max(ans, i + idx_b + 1)

    print(ans)

if __name__ == "__main__":
    main3()

Solution (Python):
from bisect import bisect_right

def cumsum(a):
    res = [0] * (len(a) + 1)
    for i, v in enumerate(a):
        res[i + 1] = res[i] + v
    return res

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

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

Solution (Python):
N, M, K = map(int, input().split())
arr_a = list(map(int, input().split()))
arr_b = list(map(int, input().split()))
ans_a =0
ans_b =0
time =0

for i in range(N):
    if time + arr_a[i] > K:
        break
    else:
        ans_a +=1
        time += arr_a[i]
for i in range(M):
    if time + arr_b[i] > K:
        break
    else:
        ans_b +=1
        time += arr_b[i]

ans = ans_a + ans_b
ans_koho = ans_a + ans_b
cnt_b = ans_b

for i in range(ans_a):
    ans_koho -= 1
    time -= arr_a[ans_a-i-1]

    while True:
        if cnt_b == M:
            break
        elif time + arr_b[cnt_b] > K:
            break
        else:
            time += arr_b[cnt_b]
            cnt_b += 1
            ans_koho += 1
    ans = max(ans, ans_koho)

print(ans)

Solution (Python):
import numpy as np

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

ak=np.cumsum(a,dtype=np.int64)
bk=np.cumsum(b,dtype=np.int64)
ak=ak[ak<=k]
bk=bk[bk<=k]
lak=len(ak)
lbk=len(bk)

an=lak
j=0
for i in range(lak-1,-1,-1):
    while bk[j]+ak[i]<=k:
        an=max(an,i+j+2)
        j+=1
        if j>=lbk:
            j-=1
            break
an=max(an,lbk)

print(an)

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

i=0
sb=0
while i<m:
    sb+=b[i]
    i+=1
    if sb>k:
        i-=1
        sb-=b[i]
        break

ans=i
j=0
sa=0
while j<n:
    sa+=a[j]
    while sa+sb>k:
        if i>0:
            sb-=b[i-1]
            i-=1
        else:
            print(ans)
            exit()
    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()))
na,nb = [0],[0]
for i in range(n):
  na.append(na[i]+a[i])
for j in range(m):
  nb.append(nb[j]+b[j])
ans = 0
l = m
for i in range(n+1):
  if na[i] > k:
    break
  while nb[l] > k - na[i]:
    l -= 1
  ans = max(ans,i+l)
print(ans)

Solution (Python):
from bisect import bisect_left, bisect_right

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

c = bisect_right(a, k)-1
ans = 0

for i in range(c+1)[::-1]:
    r = k-a[i]
    j = bisect_right(b, r)-1
    ans = max(ans, i+j)

print(ans)

Solution (Python):
from itertools import accumulate
 
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
 
a_accum = [0] + list(accumulate(a))
b_accum = [0] + list(accumulate(b))
 
j = m
for i in range(n+1):
  while k < a_accum[i] + b_accum[j] and j >= 1:
    j -= 1
  if k >= a_accum[i] + b_accum[j] and ans < i + j:
    ans = i + j
print(ans)

Solution (Python):
import sys
import itertools as it
from bisect import bisect_left, bisect_right

input = sys.stdin.readline

N, M, K = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]

AS = list(it.accumulate([0]+A))
BS = list(it.accumulate([0]+B))
max_value = 0
for i in range(N+1):
    if AS[i] > K:
        continue
    max_b = K - AS[i]
    ng = bisect_right(BS, max_b)
    ok = ng - 1
    max_value = max(i + ok, max_value)
print(max_value)

Solution (Python):





























def answer():
    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__':
    answer()

Solution (Python):





























def answer():
    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__':
    answer()

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

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

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

Solution (Python):
import sys
import math
import collections
import decimal
import itertools
from collections import deque
from functools import reduce
import heapq

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

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

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

    while ((k - a[i]) < b[p]):
        p -= 1

    ans = max(ans, i+p)

print(ans)

Solution (Python):
from itertools import accumulate

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

a_accum = [0] + list(accumulate(a))
b_accum = [0] + list(accumulate(b))

j = m
for i in range(n+1):
  while k < a_accum[i] + b_accum[j] and j >= 1:
    j -= 1
  if k >= a_accum[i] + b_accum[j] and ans < i + j:
    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] + A
B = [0] + B
for i in range(N):
    A[i + 1] += A[i]
for i in range(M):
    B[i + 1] += 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 itertools import accumulate
import bisect

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

for i in range(n + 1):
  ren = k - a1[i]
  if ren < 0:
    break
  j = bisect.bisect_right(b1, ren)
  x = max (x, i + j)	
print (x)

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_acc = [0] + list(accumulate(a))  
b_acc = list(accumulate(b))  

ans = 0
for an in range(n + 1):
    rem = k - a_acc[an]
    if rem < 0:
        
        break

    bn = bisect.bisect_right(b_acc, rem)
    ans = max(ans, an + bn)

print(ans)

Solution (Python):
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A_sum = [0]
for i in range(N):
    A_sum.append(A[i] + A_sum[i])
B_sum = [0]
for i in range(M):
    B_sum.append(B[i] + B_sum[i])   
    
b_idx = 0
for i in range(1, M+1):
    if B_sum[i] <= K:
        b_idx = i
ans = 0
for i in range(N+1):
    if b_idx > 0:
        while A_sum[i] + B_sum[b_idx] > K:
            b_idx -= 1
            if b_idx < 0:
                b_idx = 0
                break
    if A_sum[i] + B_sum[b_idx] <= K:
        ans = max(ans, i+b_idx)
    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_sum = [0]
for i in range(N):
    A_sum.append(A[i] + A_sum[i])
B_sum = [0]
for i in range(M):
    B_sum.append(B[i] + B_sum[i])   
    
b_idx = 0
for i in range(1, M+1):
    if B_sum[i] <= K:
        b_idx = i
ans = 0
for i in range(N+1):
    if b_idx > 0:
        while A_sum[i] + B_sum[b_idx] > K:
            b_idx -= 1
            if b_idx < 0:
                b_idx = 0
                break
    if A_sum[i] + B_sum[b_idx] <= K:
        ans = max(ans, i+b_idx)
print(ans)

Solution (Python):
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A_sum = [0]
for i in range(N):
    A_sum.append(A[i] + A_sum[i])
B_sum = [0]
for i in range(M):
    B_sum.append(B[i] + B_sum[i])    
import bisect
ans = 0
for i in range(N+1):
    a = A_sum[i]
    if K-a >=0:
        b_idx = bisect.bisect(B_sum, K-a) - 1
        if b_idx  < 0:
            b_idx = 0
        ans = max(ans, i+b_idx)
print(ans)

Solution (Python):
def A(x):
  return x + x**2 + x**3


def B(s, t):
  ans = 0
  for i in range(len(s)):
    if s[i] != t[i]:
      ans += 1
  return ans


def C(n, m, k, a, b):

  def _get_acc(x):
    acc = [0]
    for idx in range(len(x)):
      acc.append(x[idx] + acc[idx])
    return acc

  acc_a = _get_acc(a)
  acc_b = _get_acc(b)

  pa = 0
  pb = 0

  while pa <= n and acc_a[pa] <= k:
    pa += 1
  pa -= 1

  max_num = 0
  while True:
    if pa < 0 or pb > m:
      break
    if acc_a[pa] + acc_b[pb] <= k:
      if max_num < pa + pb:
        max_num = pa + pb
      pb += 1
    else:
      pa -= 1

  return max_num


def main():
  n, m, k = map(int, input().split())
  a = list(map(int, input().split()))
  b = list(map(int, input().split()))
  print(C(n, m, k, a, b))


if __name__ == '__main__':
  main()

Solution (Python):
import bisect

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

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

A_sum = A.copy()
B_sum = B.copy()

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

A_sum = [0] + A_sum

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


max_ = 0
for i, n in enumerate(A_sum):
    if n > K:
        continue

    target = K - n
    v = bisect.bisect_left(B_sum, target)

    if v != M and B_sum[v] == target:
        v += 1

    max_ = max(max_, i + v)

print(max_)