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

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

ca = [0]
cb = [0]

for ai in a:
    ca.append(ca[-1]+ai)

for bi in b:
    cb.append(cb[-1]+bi)

ans = 0
j = m

for i, cai in enumerate(ca):
    if cai > k:
        continue
    while True:
        if cai + cb[j] <= k:
            ans = max(ans, i + j)
            break
        if j == 0:
            break
        j -= 1

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

C=[A[0]]
for i in range(1,N):
    C.append(C[-1]+A[i])
D=[B[0]]
for i in range(1,M):
    D.append(D[-1]+B[i])
C=[0]+C
D=[0]+D
ans=0



ans=0

for i in range(N+1):
    if C[i]<=K:
        k=i
        tmp = K-C[i]
        t= bisect_right(D,tmp)
        
        if t>0:
            k +=t-1
        ans=max(ans,k)
print(ans)

Solution (Python):





import math
import fractions
import sys
import bisect
import heapq  
import collections
from collections import Counter
from collections import deque
import pprint
import itertools

sr = lambda: input()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

"""nを素因数分解"""
"""2以上の整数n => [[素因数, 指数], ...]の2次元リスト"""


def factorization(n):
    arr = []
    temp = n
    if n == 1:
        return arr

    for i in range(2, int(-(-n ** 0.5 // 1)) + 1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr.append([i, cnt])

    if temp != 1:
        arr.append([temp, 1])

    if arr == []:
        arr.append([n, 1])

    return arr



def power(a, n, mod):
    x = 1
    while n:
        if n & 1:
            x *= a % mod
        n >>= 1
        a *= a % mod
    return x % mod



def kaijo(n, l, mod):
    if n == 0:
        return 1
    a = n
    tmp = n - 1
    while (tmp >= l):
        a = a * tmp % mod
        tmp -= 1
    return a


inf = 10 ** 18
mod = 10 ** 9 + 7

n,m,k = lr()
a = lr()
b = lr()
a1 = [0]
b1 = [0]
for num in a:
    a1.append(a1[-1]+num)
for num in b:
    b1.append(b1[-1]+num)
def sumAreaA(l,r):
    return a1[r]-a1[l]
def sumAreaB(l,r):
    return b1[r]-b1[l]

ans = 0
dame = bisect.bisect_left(a1,k)
if dame < n+1:
    if a1[dame] == k:
        dame+=1
for i in range(dame):
    pre = i
    pretime = sumAreaA(0,i)
    time = k-pretime
    ind = bisect.bisect_left(b1,time)
    if ind == 0:
        ans = max(ans,pre)
    elif ind < m+1:
        if b1[ind] != time:
            ind-=1
        ans = max(ans,pre+ind)
    else:
        ans = max(ans, pre+m)
print(ans)

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

time=0
while j<m:
    time+=B[j]
    if time >k:
        time-=B[j]
        break
    j += 1
j-=1
rem=k-time
ans=j+1
cnt=ans
while True:
    while i<n and rem-A[i]>=0:
        rem-=A[i]
        i+=1
        cnt+=1
    ans=max(ans,cnt)
    
    rem+=B[j]
    j-=1
    cnt-=1
    if j<-1:
        break

print(ans)

Solution (Python):
def solve():
  N, M, K = map(int,input().split())
  AA = list(map(int,input().split()))
  BB = list(map(int,input().split()))
  A_max = 0
  AAs = [0]*N
  AAs[0] = AA[0]
  if K>=AAs[0]:
    A_max = 1
  for i in range(1,N):
    AAs[i] = AAs[i-1] + AA[i]
    if K>=AAs[i]:
      A_max+=1
  BBs = [0]*M
  BBs[0] = BB[0]
  BM = M
  for i in range(1,M):
    BBs[i] = BBs[i-1] + BB[i]
  Book_max = 0
  for i in range(A_max+1):
    b_max = 0
    if i == 0:
      B_time = K
    else:
      B_time = K - AAs[i-1]
    for j in range(BM-1,-1,-1):
      
      if B_time>=BBs[j]:
        b_max = j+1
        BM = b_max
        break
    if Book_max<b_max+i:
      Book_max = b_max+i
  print(Book_max) 
solve()

Solution (Python):
import sys
from pprint import pprint
import bisect

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

ans = 0
max_read = N + M

S_a = [0 for _ in range(N+1)]
S_b = [0 for _ in range(M+1)]
for i in range(N):
    S_a[i+1] = A[i] + S_a[i]
for i in range(M):
    S_b[i+1] = B[i] + S_b[i]



ans = 0
for i in range(N+1):
    
    sum_t = S_a[i]
    if sum_t > K:
        continue
    
    b_index = bisect.bisect_right(S_b, K - sum_t)
    
    ans = max(ans, i + b_index - 1)
    
print(ans)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
from decimal import *



N, M, K = MAP()
A = [0] + LIST()
B = LIST()

a = list(accumulate(A))
b = list(accumulate(B))

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

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



tmptime = 0
limitA = len(A) -1
for i,a in enumerate(A):
    
    if tmptime + a > K:
        limitA = i - 1 
        break
    tmptime += a

max_len = limitA
bi = 0

while limitA >= -1:
    while bi < len(B) and tmptime + B[bi] <= K:
        
        tmptime += B[bi]
        bi += 1


    max_len = max(limitA + 1 + bi, max_len)

    if limitA >= 0:
        tmptime -= A[limitA]
        limitA -= 1
    else:
        break
print(max_len)

Solution (Python):

import sys


def solve(N: int, M: int, K: int, A: "List[int]", B: "List[int]"):
    cur = sum(A)
    for i in range(N-1,-1,-1):
        if cur>K: cur -= A[i]
        else:break
    if cur==0:
        max_k = 0
        l = 0
    else:
        max_k = i+1
        l = 1
    max_pk = max_k

    p = 0
    while(p<M):
        if cur+B[p] <= K:
            cur+=B[p]
            max_k = max_pk+p+1
            p+=1
        else:
            break

    
    if l ==1:
        for q in range(i,-1,-1):
            cur -= A[q]
            while(p<M):
                if cur+B[p] <= K:
                    cur+=B[p]
                    max_k = max(q+p+1, max_k)
                    p+=1
                else:
                    break
    print(max_k)





    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    M = int(next(tokens))  
    K = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(N)]  
    B = [int(next(tokens)) for _ in range(M)]  
    solve(N, M, K, A, B)

if __name__ == '__main__':
    main()

Solution (Python):
import os
import sys
from io import BytesIO, IOBase

BUFSIZE = 8192
class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

import bisect

def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
INF = 10 ** 18
MOD = 10**9+7

Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()

n,m,k = Ri()

a = Ri()
b = Ri()

arr1 = [0]*n
arr2 = [0]*m

arr1[0] = a[0]
arr2[0] = b[0]

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

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

ans = 0
for i in range(n+1):
    
    if i == 0:
        ind = bisect.bisect_right(arr2, k)
        ind-=1
        ans=  max(ans, i+ind+1)
        
    else:
        if k -arr1[i-1] >= 0:
            ind = bisect.bisect_right(arr2, k-arr1[i-1])
            
            ind-=1
            ans=  max(ans, i+ind+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

while A[a] <= K:
    K -= A[a]
    a += 1
    if a == N:
        break
        
x = a
b = 0
while a>=0:
    if b < M:
        while B[b] <= K:
            K -= B[b]
            b += 1
            if b == M:
                break
    if a+b > x:
        x = a+b
    a -= 1
    K += A[a]

print(x)

Solution (Python):
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
max_ans = 0
time = 0
ac = 0
bc = 0
for i in range(n):
    if time + a[i] <= k:
        ans += 1
        time += a[i]
        ac += 1
    else:
        break
for i in range(m):
    if time + b[i] <= k:
        ans += 1
        time += b[i]
        bc += 1
    else:
        break
max_ans = ans
while (bc!=m) and (ac!=0):
    time -= a[ac-1]
    ac -= 1
    ans -= 1
    while time + b[bc] <= k:
        ans += 1
        time += b[bc]
        bc += 1
        max_ans = max(ans, max_ans)
        if bc == 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()))
ans1 = 0
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]
import bisect
key = bisect.bisect(a,k)
for i in range(key):
    ans = 0
    ans += key-i
    time = a[key-i-1]
    ans += bisect.bisect(b,k-time)
    ans1 = max(ans1,ans)
ans = 0
ans += bisect.bisect(b,k)
ans1 = max(ans,ans1)

            
print(ans1)

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

    a = 0
    b = 0

    ib = 0
    while True:
        if ib != M and b + B[ib] <= K:
            b += B[ib]
            ib += 1
        else:
            break
    ans = ib
    for ia in range(N):
        while a + A[ia] + b > K:
            if ib == 0:
                return ans
            ib -= 1
            b -= B[ib]
        a += A[ia]
        ans = max(ans,ia+ib+1)

    return ans

print(hoge())

Solution (Python):
import sys
from collections import deque
import copy
import bisect
def main():
    N, M, K = map(int, input().split())
    MAX = 10 ** 9 + 7
    a = [int(i) for i in input().split()]
    b = [int(i) for i in input().split()]
    A = []
    B = []
    A.append(0)
    B.append(0)
    for i in range(N):
        A.append(a[i] + A[i])
    for i in range(M):
        B.append(b[i] + B[i])
    B.append(B[M - 1] + MAX)
    ans = 0
    for i in range(N+1):
        if A[i] <= K:
            a = bisect.bisect_right(B, (K - A[i]))
            a -= 1
            ans = max(ans, (i + a))
    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()))

ans=0
T=0
for i in range(N):
    T+=A[i]
    if T > K:
        T-=A[i]
        break
    else:
        ans+=1
R=[]
R.append(ans)
n=ans
m=0
while(1):
    while(1):
        if m>=M: break
        T+=B[m]
        
        if T > K:
            T-=B[m]
            break
        else:
            ans+=1
            R.append(ans)
            m+=1
            
    n-=1
    if n<0: break
    T-=A[n]
    ans-=1
    

print(max(R))

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(1,n+1):
    sa[i]=sa[i-1]+a[i-1]
for j in range(1,m+1):
    sb[j]=sb[j-1]+b[j-1]
ans=0
for x in range(n+1):
    if k-sa[x]>=0:
        k2=k-sa[x]
        l=bisect_right(sb,k2)-1
        if x+l>ans:
            ans=x+l
for y in range(m+1):
    if k-sb[y]>=0:
        k2=k-sb[y]
        l=bisect_right(sa,k2)-1
        if y+l>ans:
            ans=y+l
print(ans)

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

s = 0
MA = -1
for i in range(N):
  s += A[i]
  if s > K:
    MA = i
    s -= A[i]
    break
if MA < 0:
  MA = N

  
ans = MA
MB = 0
while MB < M:
  while True:
    if MB < M and s + B[MB] <= K:
      s += B[MB]
      MB += 1
      ans = max(ans, MA + MB)
    else:
      break
  if MA > 0:
    s -= A[MA-1]
    MA -= 1
  else:
    break

print(ans)

Solution (Python):
import bisect
N, M, K = map(int,input().split())
ls_a = list(map(int,input().split()))
ls_b = list(map(int,input().split()))

A = [0] * (N+1)
B = [0] * (M+1)
for i in range(N):
  A[i+1] = A[i] + ls_a[i]
for i in range(M):
  B[i+1] = B[i] + ls_b[i]
  
ans = 0
for i in range(N+1):
  if A[i] <= K:
    x = bisect.bisect(B,K-A[i])
    ans = max(ans, i+x-1)
print(ans)

Solution (Python):

import sys

sysread = sys.stdin.readline
read = sys.stdin.read
sys.setrecursionlimit(10 ** 7)




import bisect





def run():
    N, M, K = map(int, sysread().split())
    A = list(map(int, sysread().split()))
    B = list(map(int, sysread().split()))

    A_sum = [0]
    for a in A:
        A_sum.append(A_sum[-1] + a)
    B_sum = [0]
    for b in B:
        B_sum.append(B_sum[-1] + b)
    

    max_val = 0
    for i, a in enumerate(A_sum):
        if a > K:
            max_val = max(i-1, max_val)
            break
        idx = bisect.bisect_left(B_sum, K-a)
        if idx < len(B_sum) and B_sum[idx] == K-a:
            idx += 1
        max_val = max(max_val, idx+i-1)
        
    print(max_val)



if __name__ == "__main__":
    run()

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

ac_A = list(accumulate(A))
ac_B = list(accumulate(B))


max_book_num = bisect.bisect_right(ac_B, k)

for left_book_num in range(n):
    if ac_A[left_book_num] > k:
        break
    else:
        max_book_num = max(max_book_num, left_book_num + 1 + bisect.bisect_right(ac_B, k - ac_A[left_book_num]))
print(max_book_num)

Solution (Python):
def _integral(A):
    result = []
    sum_value = 0
    result.append(sum_value)
    for i in range(len(A)):
        sum_value += A[i]
        result.append(sum_value)
    return result


def _main(N, M, K, A, B):
    integral_a = _integral(A)
    integral_b = _integral(B)
    top_count = 0
    b_max = len(B)
    for a_index in range(len(A) + 1):
        a_value = integral_a[a_index]
        if K < a_value:
            break
        for b_index in range(0, min(len(B) + 1, b_max + 1))[::-1]:
            if a_value + integral_b[b_index] <= K:
                if top_count < a_index + b_index:
                    top_count = a_index + b_index
                b_max = b_index
                break
    print(top_count)


if __name__ == "__main__":
    Na, Ma, Ka = map(int, input().split())
    Aa = list(map(float, input().split()))
    Ba = list(map(float, input().split()))
    _main(Na, Ma, Ka, Aa, Ba)

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


index = 0
index2 = 0
stackedTime = 0

while(index<N and stackedTime + A[index] <= K):
  stackedTime += A[index]
  index += 1

while(index2 < M and stackedTime + B[index2] <= K):
  stackedTime += B[index2]
  index2 += 1

maxBook = index + index2
for i in range(N):
  if(index == 0 or index2 == M):
    break
  index -= 1
  stackedTime -= A[index]
  while(index2 < M and stackedTime + B[index2] <= K):
    stackedTime += B[index2]
    index2 += 1

  maxBook = max(maxBook, index + index2)

print(maxBook)

Solution (Python):
from itertools import accumulate
from collections import deque

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


    ac = list(accumulate([0] + A))
    bc = list(accumulate([0] + B))
    
    a_max = -1
    for i in range(N+1):
        if ac[i] <= K:
            a_max = i
        else:
            break
    
    ans = a_max
    
    js = 0
    for i in range(a_max+1)[::-1]:
        a = ac[i]
        for j in range(js, M+1):
            
            if a + bc[j] <= K:
                js = j
                if i + j > ans:
                    ans = i + j
            else:
                break

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
import bisect 


from itertools import accumulate 
'''
N, M , lim = mi()

A = li()
B = li()

A += [10**9+1]
B += [10**9+1]
ind_a = 0 
ind_b = 0

cnt = ans = 0

while True:
    if A[ind_a] > B[ind_b]:
        if cnt + B[ind_b] <= lim:
            ans += 1
            cnt += B[ind_b]
            if ind_b != M:
                ind_b += 1
        else:
            break

    elif A[ind_a] < B[ind_b]:
        if cnt + A[ind_a] <= lim:
            ans += 1
            cnt += A[ind_a]
            if ind_a != N:
                ind_a += 1
        else:
            break

    else:
        if ind_a == N and ind_b == M:
            break
        elif ind_a == N:
            if cnt + B[ind_b] <= lim:
                ans += 1
                cnt += B[ind_b]
                if ind_b != M:
                    ind_b += 1
            else:
                break
        elif ind_b == M:
            if cnt + A[ind_a] <= lim:
                ans += 1
                cnt += A[ind_a]
                if ind_a != N:
                    ind_a += 1
            else:
                break
        else:
            if A[ind_a+1] >= B[ind_b+1]:
                if cnt + B[ind_b] <= lim:
                    ans += 1
                    cnt += B[ind_b]
                    if ind_b != M:
                        ind_b += 1
                else:
                    break
            else:
                if cnt + A[ind_a] <= lim:
                    ans += 1
                    cnt += A[ind_a]
                    if ind_a != N:
                        ind_a += 1
                else:
                    break

print(ans)
'''
N, M , lim = mi()

A = li()
B = li()

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

for i in range(N+1):
    ind = bisect.bisect_right(B, max(lim-A[i], 0))
    if ind > 0 or lim-A[i] == 0:
        ans = max(ans, ind + i)

print(ans)

Solution (Python):
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

n,m,k = mi()
a = li()
b = li()

asum = sum(a)
acur = n-1
bsum = 0
bcur = -1












if asum > k:
    while asum > k:
        asum -= a[acur]
        acur -= 1
    while 1:
        if asum + bsum + b[bcur+1] <= k:
            bsum += b[bcur+1]
            bcur += 1
        else:
            break
        if bcur == m-1:
            break
else:
    while 1:
        if asum + bsum + b[bcur+1] <= k:
            bsum += b[bcur+1]
            bcur += 1
        else:
            break
        if bcur == m-1:
            break

read_max = acur + 1 + bcur + 1









for i in range(n):
    acur -= 1
    if acur == -2:
        break
    
    asum -= a[acur+1]
    if bcur == m-1:
        break

    while 1:
        if asum + bsum + b[bcur+1] <= k:
            bsum += b[bcur+1]
            bcur += 1
        else:
            break
        if bcur == m-1:
            break

    

    read_max = max(read_max, acur + 1 + bcur + 1)

    if bcur == m-1:
        break
print(read_max)

Solution (Python):
import numpy as np
from functools import reduce

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


def get_cum(arr):
    cum_arr = [0]
    for e in arr:
        tot = cum_arr[-1] + e
        if tot > K:
            return cum_arr
        cum_arr.append(tot)
    return cum_arr


cumA = get_cum(As)
cumB = get_cum(Bs)

max_count = 0
a_thresh = len(cumA) - 1
a_count = a_thresh
b_count = 0
b_thresh = len(cumB) - 1
while a_count >= 0 and b_count <= b_thresh:
    while b_count <= b_thresh and cumA[a_count] + cumB[b_count] <= K:
        b_count += 1
    b_count = max(0, b_count - 1)
    if cumA[a_count] + cumB[b_count] <= K:
        max_count = max(max_count, a_count + b_count)
    a_count -= 1

print(max_count)

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

tmp=0
ca=[0]
for ai in a:
  tmp+=ai
  ca.append(tmp)
tmp=0
cb=[0]
for bi in b:
  tmp+=bi
  cb.append(tmp)
import bisect
s=bisect.bisect_right(ca,k)-1
ans=ca[s]
now_a=s
z=k-ca[now_a]
now_b=bisect.bisect_right(cb,z)-1
t=ca[now_a]+cb[now_b]

ans=now_a+now_b
for i in range(s):
  now_a-=1
  t-=a[now_a]
  while now_b<m and t+b[now_b]<=k:
    t+=b[now_b]
    now_b+=1
  ans=max(ans,now_a+now_b)
print(ans)

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

sa=[]
ka=0
sb=[]
kb=0
for i in range(n):
  if ka+a[i]<=k:
    ka+=a[i]
    sa.append(a[i])
  else:
    break
for i in range(m):
  if kb+b[i]<=k:
    kb+=b[i]
    sb.append(b[i])
  else:
    break
ans=max(len(sa),len(sb))
tb=[]
for i in range(len(sa)):
  if sa[i]>k:
    break
  while kb+sa[i]>k:
    kb-=sb[-1]
    sb.pop(-1)
  tb.append(sa[i])
  kb+=sa[i]
  if ans<(len(sb)+len(tb)):
    ans=(len(sb)+len(tb))
print(ans)

Solution (Python):
n, m, k = [int(v) for v in input().split()]
alist = [int(v) for v in input().split()]
blist = [int(v) for v in input().split()]

consumed = 0
max_count = 0

i = 0
while i < len(alist) and consumed + alist[i] <= k:
    consumed += alist[i]
    i += 1

j = 0
while j < len(blist) and consumed + blist[j] <= k:
    consumed += blist[j]
    j += 1

count = i + j
max_count = count

i -= 1

while i >= 0:
    consumed -= alist[i]
    count -= 1
    while j < len(blist) and consumed + blist[j] <= k:
        consumed += blist[j]
        j += 1
        count += 1
        if count > max_count:
            max_count = count
    i -= 1

print(max_count)

Solution (Python):

import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from collections import deque
from functools import lru_cache
import bisect
import re
import queue
import decimal


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [Scanner.string() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [Scanner.int() for i in range(n)]


MOD = int(1e09) + 7
INF = int(1e15)


def solve():
    N, M, K = Scanner.map_int()
    A = Scanner.map_int()
    B = Scanner.map_int()
    time = 0
    ans = 0
    for a in A:
        if time + a > K:
            break
        time += a
        ans += 1
    x = ans
    j = 0
    while time <= K and j < M:
        if time + B[j] > K:
            break
        time += B[j]
        j += 1
        ans += 1
    tmp = ans
    for i in reversed(range(x)):
        time -= A[i]
        tmp -= 1
        while time <= K and j < M:
            if time + B[j] > K:
                break
            time += B[j]
            j += 1
            tmp += 1
        ans = max(ans, tmp)
    print(ans)


def main():
    
    
    
    
    
    
    solve()


if __name__ == "__main__":
    main()

Solution (Python):








import sys

N,M,K = map(int,sys.stdin.readline().split())
a = tuple(map(int,sys.stdin.readline().split())) 
b = tuple(map(int,sys.stdin.readline().split())) 

cum_a = [0]*(N+1)
for i in range(N):
    cum_a[i+1] = cum_a[i]+a[i]

cum_b = [0]*(M+1)
for i in range(M):
    cum_b[i+1] = cum_b[i]+b[i]

ok = 0
ng = N+M+1

def isOK(mid):
    for i in range(N+1):
        if 0 <= mid-i and mid-i <=M and cum_a[i]+cum_b[mid-i]<=K:
            return True
    return False

while ng-ok > 1:
    mid = (ok+ng)//2
    if isOK(mid):
        ok = mid
    else:
        ng = mid
print(ok)

Solution (Python):
N,M,K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
As=[0]*(N+1)+[float('inf')]
Bs=[0]*(M+1)+[float('inf')]
Amax=N
Bmax=M
for i in range(1,N+1):
    As[i]=As[i-1]+A[i-1]
    if As[i]>K:
        Amax=i
        break
for i in range(1,M+1):
    Bs[i]=Bs[i-1]+B[i-1]
    if Bs[i]>K:
        Bmax=i
        break

b=0
ans=0
for i in range(Amax,-1,-1):
    while As[i]+Bs[b]<=K:
        b+=1
    ans=max(ans,i+b-1)
print(ans)

Solution (Python):
import bisect

n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
sa = [0]
for i in range(n):
    sa.append(sa[-1] + a[i])
sb = [0]
for i in range(m):
    sb.append(sb[-1] + b[i])
ans = 0
for i, s in enumerate(sa):
    if s > k:
        break
    ans = max(ans, i + max(0, bisect.bisect_left(sb, k - s + 1) - 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_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]
ans = -1
for i in range(n + 1):
    if a_sum[i] <= k:
        ans = i
    else:
        break
tmp = ans
for i in range(1, m + 1):
    if b_sum[i] > k:
        break
    for j in range(tmp, -1, -1):
        if b_sum[i] + a_sum[j] <= k:
            tmp = j
            ans = max(ans, i + j)
            break
print(ans)

Solution (Python):
def solve():
    tmp = 0
    a_sum = [0]
    for a in A:
        tmp += a
        a_sum.append(tmp)

    tmp = 0
    b_sum = [0]
    for b in B:
        tmp += b
        b_sum.append(tmp)

    b_idx = 0
    a_idx = N
    ret = 0
    while 0 <= a_idx:
        if K < a_sum[a_idx]:
            a_idx -= 1
            continue
        while b_idx < M and a_sum[a_idx] + b_sum[b_idx + 1] <= K:
            b_idx += 1
        ret = max(ret, a_idx + b_idx)
        a_idx -= 1
    print(ret)

if __name__ == "__main__":
    N, M, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    solve()

Solution (Python):

n, m, k = map(int, input().split())
from bisect import bisect_right

a = list(map(int, input().split()))
b = list(map(int, input().split()))
total_a = [0] * n
total_b = [0] * m

def set_data(data, a, x):
    data[0] = a[0]
    for i in range(1, x):
        data[i] = data[i - 1] + a[i]

set_data(total_a, a, n)
set_data(total_b, b, m)

def cnt(a, b, n, m):
    ans = 0
    for i in range(n - 1, -1, -1):
        if a[i] <= k:
            index = bisect_right(b, k - a[i], 0, m)
            ans = max(ans, i + index + 1)
    return ans

print(max(cnt(total_a, total_b, n, m), cnt(total_b, total_a, m, n)))