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

Solution (Python):
import sys
import math 
import bisect

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



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

for i in range(n+1):
    tmp = k - a[i]
    if tmp < 0:
        break
    else:
        j=bisect.bisect_right(b,tmp)
        ans= max(ans,i+j-1)
print(ans)

Solution (Python):

from bisect import bisect
from itertools import accumulate


def main():
    N, M, K, *AB = map(int, open(0).read().split())
    cum_A, cum_B = tuple(accumulate([0] + AB[:N])), tuple(accumulate(AB[N:]))
    res = 0
    for i, a_time in enumerate(cum_A):
        if a_time > K:
            break
        b_time = K - a_time
        res = max(res, i + bisect(cum_B, b_time))
    print(res)


if __name__ == "__main__":
    main()

Solution (Python):
from collections import deque
trush_a, trush_b, limit_time = list(map(int,input().split()))
total = [0, 0]
count = [0, 0]
desks = [deque(),deque()]
result = 0

for i in range(2):

    for j in list(map(int,input().split())):
        temp = total[i] + j 
        if temp <= limit_time:
            total[i] += j
            count[i] += 1
            desks[i].append(j)
        else:
            break

while count[0] > 0 or count[1] > 0:
    if count[0] == count[1]:
        select = total[0] > total[1]
    else:
        select = count[0] < count[1]

    temp = desks[select].popleft()
    result += 1
    limit_time -= temp
    total[select] -= temp
    count[select] -= 1

    while total[not select] > limit_time:
        dim_time = desks[not select].pop()
        total[not select] -= dim_time
        count[not select] -= 1

print(result)

Solution (Python):

from bisect import bisect
from itertools import accumulate


def main():
    N, M, K, *AB = map(int, open(0).read().split())
    cum_A, cum_B = tuple(accumulate([0] + AB[:N])), tuple(accumulate(AB[N:]))
    candidates = [0]
    for i, a_time in enumerate(cum_A):
        if a_time > K:
            break
        b_time = K - a_time
        candidates.append(i + bisect(cum_B, b_time))
    print(max(candidates))


if __name__ == "__main__":
    main()

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):
import numpy as np
N,M,K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

a, b = [0], [0]
a.extend(np.cumsum(A, dtype="int64"))
b.extend(np.cumsum(B, dtype="int64"))

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

        j -= 1
    best = max(best, i + j)

print(best)

Solution (Python):

from bisect import bisect
from itertools import accumulate


def main():
    N, M, K, *AB = map(int, open(0).read().split())
    cum_A, cum_B = tuple(accumulate([0] + AB[:N])), tuple(accumulate(AB[N:]))
    candidates = []
    for i, a_time in enumerate(cum_A):
        if a_time > K:
            candidates.append(0)
            break
        b_time = K - a_time
        candidates.append(i + bisect(cum_B, b_time))
    print(max(candidates))


if __name__ == "__main__":
    main()

Solution (Python):
from bisect import *
n, m, k = map(int, input().split())
A = [0]
for x in map(int, input().split()):
    x += A[-1]
    if x > k: break
    A.append(x)
B = [0]
for x in map(int, input().split()):
    x += B[-1]
    if x > k: break
    B.append(x)
ans = 0
for i in range(len(A)):
    ans = max(ans, i + (bisect_right(B, k-A[i]) - 1))
print(ans)

Solution (Python):
from itertools import accumulate
from bisect import bisect
def main():
    n, m, k = map(int, input().split())
    a = tuple(map(int, input().split()))
    b = tuple(map(int, input().split()))

    aa = tuple(accumulate(a))
    ba = tuple(accumulate(b))
    r = 0
    for i1 in range(n):
        if aa[i1] > k:
            break
        else:
            t = bisect(ba, k - aa[i1])
            r = max(r, i1 + 1 + t)
    for j1 in range(m):
        if ba[j1] > k:
            break
        else:
            t = bisect(aa, k - ba[j1])
            r = max(r, j1 + 1 + t)
    print(r)
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()))

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

a_sum = 0
for i in range(N):
    a_sum += A[i]
    if a_sum > K:
        break
    while a_sum + b_sum > K:
        b_sum -= B[j]
        j -= 1
    result = max(result, (i + 1) + (j + 1))
print(result)

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

A_sum=[0,A[0]]
B_sum=[0,B[0]]
for i in range(1,N):
    A_sum.append(A_sum[i]+A[i])
for i in range(1,M):
    B_sum.append(B_sum[i]+B[i])

b_read=0
ans=0
for i in range(N,-1,-1):
    for j in range(b_read,M+1):
        if A_sum[i]+B_sum[j]<=K:
            ans=max(ans,i+j)
            b_read=j
        else:
            break
print(ans)

Solution (Python):
import itertools
import numpy as np 
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

a = list(map(int, readline().split()))
b = list(map(int, readline().split()))
a = [0] + a
b = [0] + b
acc = list(itertools.accumulate(a))
bcc = list(itertools.accumulate(b))





ans = 0
j = m
for i, val in enumerate(acc):
  if (val > k):
    break
  else:
    rest = k - val
    
    
    
    while (bcc[j] > rest):
      j -= 1

    ans = max(ans, j + i)


print(ans)

Solution (Python):
from bisect import bisect_left,bisect_right
n,m,k=map(int,input().split())
arr1=list(map(int,input().split()))
arr2=list(map(int,input().split()))
for i in range(1,n):
    arr1[i]=arr1[i]+arr1[i-1]
for i in range(1,m):
    arr2[i]=arr2[i]+arr2[i-1]
ma=0
for i in range(n):
    if arr1[i]<=k:
        ma=max(ma,bisect_right(arr2,k-arr1[i])+i+1)
for i in range(m):
    if arr2[i]<=k:
        ma=max(ma,bisect_right(arr1,k-arr2[i])+i+1)
print(ma)

Solution (Python):

from bisect import bisect
from itertools import accumulate


def main():
    N, M, K, *AB = map(int, open(0).read().split())
    cum_A, cum_B = tuple(accumulate(AB[:N])), tuple(accumulate(AB[N:]))
    candidates = []
    for cum_x, cum_y in ((cum_A, cum_B), (cum_B, cum_A)):
        for i, x_time in enumerate(cum_x, 1):
            if x_time > K:
                candidates.append(0)
                break
            y_time = K - x_time
            candidates.append(i + bisect(cum_y, y_time))
    print(max(candidates))


if __name__ == "__main__":
    main()

Solution (Python):
import sys
import numpy as np
N, M, K = [int(x) for x in input().split()]
A = [float(x) for x in input().split()]
B = [float(x) for x in 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, 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 numpy as np
N, M, K = map(int, input().split())
A = np.cumsum([0] + list(map(int, input().split())))
B = np.cumsum([0] + list(map(int, input().split())))

def nibu(data, value):
  left = 0
  right = len(data) - 1
  while right - left > 1:
    mid = (left + right)//2
    if data[mid] == value:
      return mid
    elif value < data[mid]:
      right = mid
    else:
      left = mid
  if value >= data[right]:
    return right
  else:
    return left
  
ans = 0
for i in range(N+1):
  sa = K - A[i]
  if sa < 0:
    break
  j = nibu(B, sa)
  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()))


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

Solution (Python):

import itertools
import bisect

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

cA = [0] + list(itertools.accumulate(A))
cB = list(itertools.accumulate(B))




ans = 0
for i in range(len(cA)):
    tmp = cA[-(i+1)]
    if tmp < k:
        indb = bisect.bisect_right(cB, k - tmp)
        
        ans = max(ans, (n-i)+indb)
    elif tmp == k:
        ans = n - i
    
print(ans)

Solution (Python):
import numpy as np
N, M, K = map(int, input().split())
A = np.cumsum([0] + list(map(int, input().split())))
B = np.cumsum([0] + list(map(int, input().split())))

def nibu(data, value):
  left = 0
  right = len(data) - 1
  while right - left > 1:
    mid = (left + right)//2
    if data[mid] == value:
      return mid - 1
    elif value < data[mid]:
      right = mid
    else:
      left = mid
  if value >= data[right]:
    return right
  else:
    return left
  
ans = 0
for i in range(N+1):
  sa = K - A[i]
  if sa < 0:
    break
  j = nibu(B, sa)
  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()))

b = [0] * (m+1)
for i in range(m):
  b[i+1] = b[i] + B[i]

from bisect import bisect_right

result = 0
a = k
for i in range(n+1):
  index = bisect_right(b, a)
  hon = (index - 1) + (i)
  result = max(result, hon)
  if i < n:
    a -= A[i]
  if a < 0:
    break
print(result)

Solution (Python):
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


def cumsum(a):
    """l[i] = sum(a[:i]) なるlを返す
    sum(a[i:j]) == l[j+1] - l[i]
    """
    c = 0
    l = [None] * (len(a)+1)
    for i,num in enumerate(a):
        l[i] = c
        c += num
    l[-1] = c
    return l

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)

def sub(x):
    res = float("inf")
    for i in range(x+1):
        if i>n or (x-i)>m:
            continue
        res = min(res, ca[i] + cb[x-i])
    return res<=k
l = 0
r = n+m+1
while l+1<r:
    mm = (l+r)//2
    if sub(mm):
        l = mm
    else:
        r = mm
print(l)

Solution (Python):
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])
inf = 1001001001
sb = [-inf, 0]
for i in range(m):
    sb.append(sb[-1] + b[i])
sb.append(inf)
ans = 0
for i in range(n+1):
    s = sa[i]
    l = 0
    r = len(sb) - 1
    while l+1 < r:
        m = (l+r) //2
        if s + sb[m] <= k:
            l = m
        else:
            r = m
    if l == 0:
        continue
    else:
        ans = max(ans, i+l-1)
print(ans)

Solution (Python):
from bisect import *
n, m, k = map(int, input().split())
A = [0]
for x in map(int, input().split()):
    if A[-1]+x > k: break
    A.append(A[-1]+x)
B = [0]
for x in map(int, input().split()):
    if B[-1]+x > k: break
    B.append(B[-1]+x)
ans = 0
for i in range(len(A)):
    j = bisect_right(B, k-A[i]) - 1
    ans = max(ans, i+j)
print(ans)

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

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

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

Solution (Python):
from bisect import *
n, m, k = map(int, input().split())
A = [0]
for x in map(int, input().split()):
    if A[-1]+x > k: break
    A.append(A[-1]+x)
B = [0]
for x in map(int, input().split()):
    if B[-1]+x > k: break
    B.append(B[-1]+x)

ans = 0
for i in range(len(A)):
    j = bisect_right(B, k-A[i]) - 1
    ans = max(ans, i+j)




print(ans)

Solution (Python):
N,M,K=map(int,input().split())
a=input().split()
A=[int(c) for c in a]
b=input().split()
B=[int(c) for c in b]
answers=[0]
As=[0]
Bs=[0]
wholeA=0
wholeB=0
bs = [M]
for a in range(N):
    wholeA+=A[a]
    As.append(wholeA)
for b in range(M):
    wholeB+=B[b]
    Bs.append(wholeB)
for a in range(N+1):
    for b in range(bs[-1],-1,-1):
        whole = As[a]
        whole+=Bs[b]
        if whole<=K:
            answers.append(a+b)
            bs.append(b)
            break
print(max(answers))

Solution (Python):
from fractions import gcd
from collections import Counter, deque, defaultdict
from heapq import heappush, heappop, heappushpop, heapify, heapreplace, merge
from bisect import bisect_left, bisect_right, bisect, insort_left, insort_right, insort
from itertools import accumulate, product, permutations, combinations

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

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

    res = 0

    for i in range(N+1):
        if i == 0:
            for j in range(M, -1, -1):
                if cumsumB[j] <= K:
                    max_j = j
                    res = max(res, i + j)
                    break
        else:
            for j in range(max_j, -1, -1):
                time = cumsumA[i] + cumsumB[j]
                if  time <= K:
                    max_j = j
                    res = max(res, i + j)
                    break
    
    print(res)

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]

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

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

mx=0
for i in range(n+1):
  if a[i]>k:continue
  
  idx=min(bisect_left(b,k-a[i]),m)
  
  if b[idx]>k-a[i]:idx-=1
  if a[i]+b[idx]>k:continue
  mx=max(mx,i+idx)
print(mx)

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

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

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

print(ans)

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

C = [0] * (len(A) + 1)
for i in range(1, len(C)):
    C[i] = C[i-1] + A[i-1]
D = [0] * (len(B) + 1)
for j in range(1, len(D)):
    D[j] = D[j-1] + B[j-1]

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

Solution (Python):
import numpy as np


def main():
    N, M, K = map(int, input().split())
    A = np.array([0] + [int(i) for i in input().split()])
    B = np.array([0] + [int(i) for i in input().split()])

    A.cumsum(out=A)
    B.cumsum(out=B)

    ans = 0
    j = 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)


if __name__ == '__main__':
    main()

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

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

sumA = [0]
sumB = [0]


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

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

ans = 0
t = M

for i in range(N + 1):
    for j in range(t, -1, -1):
        if K < sumA[i] + sumB[j]:
            t -= 1
            continue
        else:
            ans = max(ans, i + 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()))
c=0
A.insert(0,0)
B.insert(0,0)
import numpy as np
A=np.array(A).cumsum()
B=np.array(B).cumsum()

for i in range(N+1):
  d=K-A[i]
  for j in reversed(range(M+1)):
    if d-B[j]>=0:
      if c<i+j:
        c=i+j
      M=j
      break
print(c)

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

input = sys.stdin.readline


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

    time = sum(A)
    for i in reversed(range(N)):
        if time <= K:
            i_A = i
            break
        time -= A[i]

    if time == 0:
        ans = 0
        for i in range(M):
            time += B[i]
            if time > K:
                break
            ans += 1
    else:
        ans = i_A + 1
        cnt = i_A + 1
        for i in range(M):
            time += B[i]
            cnt += 1
            if time > K:
                for j in reversed(range(i_A + 1)):
                    time -= A[i_A]
                    cnt -= 1
                    i_A -= 1
                    if time <= K:
                        break
                if time > K:
                    break
            else:
                if cnt > ans:
                    ans = cnt

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
N, M, K = map(int, input().split())
times_a = list(map(int, input().split()))
times_b = list(map(int, input().split()))
read_time = 0
read_time_a = [0]
read_time_b = [0]
for i in range(N):
    read_time_a.append(read_time_a[i]+times_a[i])
for i in range(M):
    read_time_b.append(read_time_b[i]+times_b[i])

ans = 0
for a_count in range(N+1):
    if read_time_a[a_count] > K:
        continue
    a_read_time = read_time_a[a_count]
    max_b_count = 0
    r = len(read_time_b)
    
    
    while max_b_count + 1 < r:
        c = (max_b_count + r) // 2
        
        if a_read_time + read_time_b[c] <= K:
            max_b_count = c
        
        else:
            r = c
    
    ans = max(ans, a_count + max_b_count)
print(ans)

Solution (Python):
N, M, K = map(int, input().split())
times_a = list(map(int, input().split()))
times_b = list(map(int, input().split()))
read_time = 0
read_time_a = [0]
read_time_b = [0]
for i in range(N):
    read_time_a.append(read_time_a[i]+times_a[i])
for i in range(M):
    read_time_b.append(read_time_b[i]+times_b[i])

ans = 0
for a_count in range(N+1):
    a_read_time = read_time_a[a_count]
    max_b_count = 0
    r = len(read_time_b)
    
    
    while max_b_count + 1 < r:
        c = (max_b_count + r) // 2
        
        if a_read_time + read_time_b[c] <= K:
            max_b_count = c
        
        else:
            r = c
    
    if read_time_a[a_count] + read_time_b[max_b_count] <= K:
        ans = max(ans, a_count + max_b_count)
print(ans)

Solution (Python):
import sys

input = sys.stdin.readline


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

    i_A = -1
    time = sum(A)
    for i, a in enumerate(reversed(A)):
        if time <= K:
            i_A = N - 1 - i
            break
        time -= a

    ans = i_A + 1
    cnt = i_A + 1
    for b in range(M):
        time += B[b]
        cnt += 1
        while True:
            if time <= K:
                break
            if i_A == -1:
                break
            time -= A[i_A]
            cnt -= 1
            i_A -= 1
        if time <= K and cnt > ans:
            ans = cnt

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):




import sys
input = sys.stdin.readline



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

    sumA = [0] * (N+1)
    sumB = [0] * (M+1)

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

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

    for i in range(M,-1,-1):
        if sumB[i] <= K:
            endB = i
            break

    for i in range(N,-1,-1):
        if sumA[i] <= K:
            endA = i
            break

    res = 0

    for i in range(0,endA+1):
        for j in range(endB,-1,-1):
            if sumA[i] + sumB[j] <= K:
                endB = j
                res = max(res,i+j)
                break

    print(res)



main()

Solution (Python):
import sys
import itertools as it
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(A))
BS = list(it.accumulate(B))
AS.insert(0,0)
BS.insert(0,0)
max, j = 0, M
for i in range(N+1):
    if AS[i] > K:
        break
    while i + j > max:
        if AS[i] + BS[j] <= K:
            max = i + j
            break
        j -= 1

print(max)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
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

N, M , W = MAP()
A = LIST()
B = LIST()

sa = sum(A)
sb = sum(B)
A.reverse()
C = A + B

ans = 0
left_wa = 0
right_wa = 0


ruiseki = C.copy()
ruiseki = [0] + ruiseki
for i in range(len(ruiseki)-1):
    ruiseki[i+1] = ruiseki[i] + ruiseki[i+1]

left = 0


idx = 0
wa = 0
ans = 0

for right in range(len(A), len(ruiseki)):
    
    
    wa = ruiseki[right] - ruiseki[left]
    
    while wa > W:
        left += 1
        wa = ruiseki[right] - ruiseki[left]
    if left <= len(A):
        tmp = right - left
        
        ans = max(tmp, ans)
    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()))
ans = 0
j = m

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

for i in range(m):
    bb.append(bb[i] + b[i])

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

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()]
timeA, timeB = 0, 0
numA, numB = 0, 0
for i in range(N):
    if timeA + A[i] <= K:
        numA += 1
        timeA += A[i]
    else:
        break
for i in range(M):
    if timeB + B[i] <= K - timeA:
        numB += 1
        timeB += B[i]
    else:
        break
ans = numA + numB
t = numA
for i in reversed(range(numA)):
    t -= 1
    timeA -= A[i]
    while numB < M:
        if timeB + B[numB] <= K - timeA:
            timeB += B[numB]
            numB += 1
        else:
            break
    ans = max(ans, t + numB)
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]
B_sum = [0]

A = A + [10 ** 10]
B = B + [10 ** 10]

for i in range(0,N):
    if A_sum[i] + A[i] > K:
        break
    A_sum.append(A_sum[i] + A[i])

for i in range(0,M):
    if B_sum[i] + B[i] > K:
        break
    B_sum.append(B_sum[i] + B[i])

ans = 0
BM = len(B_sum) - 1
for i in range(len(A_sum)):
    for j in range(BM, -1, -1):
        if A_sum[i] + B_sum[j] <= K:
            ans = max(ans, i + j)
            BM = j
            break

print(ans)

Solution (Python):


def II(): return int(input())
def MII(): return map(int, input().split())
def LII(): return list(map(int, input().split()))

def main():
    N, M, K = MII()
    A = LII()
    B = LII()

    AA = [0]
    for i in range(N):
        AA.append(AA[i]+A[i])

    BB = [0]
    for i in range(M):
        BB.append(BB[i]+B[i])

    ans = 0
    j = M

    for i in range(N+1):
        if AA[i] > K:
            break
        while BB[j] > K - AA[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()))
c=0
k=M
import numpy as np
A=np.array(A).cumsum()
B=np.array(B).cumsum()
for i in range(N):
  d=K-A[i]
  if d<0:
    for j in reversed(range(M)):
      if K-B[j]>=0:
        if c<j+1:
          c=j+1
        break
    break
  else:
    for j in reversed(range(k)):
      if d>=B[j]:
        if c<i+j+2:
          c=i+j+2
        k=j+1
        break
    if d<B[0]:
      if c<i+1:
        c=i+1
      k=0
for j in reversed(range(M)):
  if K-B[j]>=0:
    if c<j+1:
      c=j+1
print(c)

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

b_sum = 0
for i in range(M):
    b_sum += B[i]
    if b_sum > K:
        b_sum -= B[i]
        j = i - 1
        break
else:
    j = M - 1
result = j + 1

a_sum = 0
for i in range(N):
    a_sum += A[i]
    if a_sum > K:
        break
    while a_sum + b_sum > K:
        b_sum -= B[j]
        j -= 1
    result = max(result, (i + 1) + (j + 1))
print(result)

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 main():
    from itertools import accumulate as ac
    from bisect import bisect_left as bl
    n, m, k = map(int, input().split())
    a = [0]+list(map(int, input().split()))
    b = [0]+list(map(int, input().split()))
    aa = list(ac(a))
    bb = list(ac(b))
    ans = 0
    for i in range(n+1):
        if aa[i] > k:
            break
        ans = max(ans, i+bl(bb, k-aa[i]+0.1)-1)
    print(ans)


main()

Solution (Python):
import sys
from itertools import accumulate


def main():
    input = sys.stdin.buffer.readline
    n, m, k = map(int, input().split())
    a = [0] + list(map(int, input().split()))
    b = [0] + list(map(int, input().split()))
    a = list(accumulate(a))
    b = list(accumulate(b))
    ans = 0
    num_b = m
    for num_a in range(n + 1):
        sum_a = a[num_a]
        if sum_a > k:
            break
        while sum_a + b[num_b] > k:
            num_b -= 1
        ans = max(num_a + num_b, ans)
    print(ans)


if __name__ == '__main__':
    main()

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

ans = 0
for i in range(n+1):
    cnt = i
    num = k - a_lst[i]
    if num < 0:
        continue
    b_yomeru = bisect.bisect_right(b_lst,num)
    cnt += b_yomeru
    if ans < cnt:
        ans = cnt

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
j = m

aa, bb = [0], [0]

for i in range(n):
    aa.append(aa[i] + a[i])

for i in range(m):
    bb.append(bb[i] + b[i])

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

Solution (Python):
import sys
sys.setrecursionlimit(10**8)
def line_to_int(): return int(sys.stdin.readline())
def line_to_each_int(): return map(int, sys.stdin.readline().split())
def line_to_list(): return list(map(int, sys.stdin.readline().split()))
def line_to_list_in_iteration(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]



from itertools import accumulate 
import bisect 

n,m,k = line_to_each_int()
A = line_to_list()
B = line_to_list()

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

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)