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

Solution (Python):
import bisect

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

A=[0]
for i in range(N):
  A.append(arr1[i]+A[-1])
B=[0]
for i in range(M):
  B.append(arr2[i]+B[-1])

ans=0
for i in range(N+1):
  if A[i]>K:
    break
  j=bisect.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])
 
num,x = 0,m
 
for i in range(len(A)):
  if A[i] > k:
    break
  while A[i] + B[x] > k:
    x -= 1
  num = max(num, i + x)
  
print(num)

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])
 
num,x = 0,m
 
for i in range(len(a)+1):
  if A[i] > k:
    break
  while A[i] + B[x] > k:
    x -= 1
  num = max(num, i + x)
  
print(num)

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

num,x = 0,m

for i in range(n+1):
  if A[i] > k:
    break
  while A[i] + B[x] > k:
    x -= 1
  num = max(num, i + x)
  
print(num)

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

acum1=[0]
for i in range(n):
  acum1.append(acum1[-1]+arr1[i])
acum2=[0]
for i in range(m):
  acum2.append(acum2[-1]+arr2[i])
ans=0
j=m
for i in range(n+1):
  if acum1[i]>k:
    break
  while acum2[j]>k-acum1[i] and j>0:
    j-=1
  ans=max(ans,i+j)
print(ans)

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

ri = lambda S: [int(v) for v in S.split()]


def rii():
    return ri(input())


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

i = j = 0

pA = [0] + list(accumulate(A))
pB = [0] + list(accumulate(B))

ans = 0
for i, a in enumerate(pA):
    br = 0
    if K >= a:
        r = K - a
        br = i
    
    if r:
        j = bisect_left(pB, r)
        if j != len(pB):
            if pB[j] > r:
                j -= 1
            br += j
    
    ans = max(ans, br)

print(ans)

Solution (Python):
N,M,K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
a = [0]
b = [0]

for i in range(N):
    a.append(a[i] + A[i])
for j in range(M):
    b.append(b[j] + B[j])
ans = 0
j = M
for i in range(N+1):
    if a[i] > K:
        break
    while 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()))
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):
from bisect import bisect


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

a_cum = [0]
cum = 0
for x in A:
  cum += x
  a_cum.append(cum)
  
b_cum = []
cum = 0
for x in B:
  cum += x
  b_cum.append(cum)
  
answer = 0

for i, a in enumerate(a_cum):
  remain = K - a
  if remain < 0:
    break

  t = bisect(b_cum, remain)
  
  answer = max(answer, i + t)
  
      
print(answer)

Solution (Python):
from sys import stdin

input = stdin.readline
from itertools import accumulate


def solve():
    n, m, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    pa = [0] + list(accumulate(a))
    pb = [0] + list(accumulate(b))
    la = len(a)
    lb = len(b)
    left = 0
    right = la + lb

    def ok(cur):
        for i in range(max(0, cur - lb), min(cur,la) + 1):
            j = cur - i
            if pa[i] + pb[j] <= k:
                return True
        return False

    while left < right:

        mid = (left + right + 1) // 2
        if ok(mid):
            left = mid
        else:
            right = mid - 1
    print(left)


if __name__ == '__main__':
    solve()

Solution (Python):
n, m, k = map(int, input().split())
aa = list(map(int, input().split()))
bb = list(map(int, input().split()))
asum = [0]
bsum = [0]

for i in range(len(aa)):
    asum.append(asum[i]+aa[i])
for i in range(len(bb)):
    bsum.append(bsum[i]+bb[i])

j = len(bsum)-1
ans = 0
for i in range(len(asum)):
    while j >= 0:
        if k >= asum[i] + bsum[j]:
            ans = max(ans, i+j)
            break
        else:
            j -= 1


print(ans)

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

Solution (Python):
from itertools import accumulate
n,m,k = map(int,input().split())
deskA = list(map(int,input().split()))
deskB = list(map(int,input().split()))
cumA = [0] + list(accumulate(deskA))
cumB = [0] + list(accumulate(deskB))
ans = 0
j = m
for i in range(n+1):
    if cumA[i] > k:
        continue
    while cumA[i] + cumB[j] > k:
        j -= 1
    ans = max(i+j,ans)
print(ans)

Solution (Python):
from itertools import accumulate
n,m,k = map(int,input().split())
deskA = list(map(int,input().split()))
deskB = list(map(int,input().split()))
cumA = list(accumulate(deskA))
cumB = list(accumulate(deskB))
cumA = [0] + cumA
cumB = [0] + cumB
ans = 0
l = m
for i in range(n+1):
    tmp = k - cumA[i]
    if tmp<0:
        continue
    j = l
    tmp -= cumB[j]
    while tmp < 0:
        tmp += cumB[j]
        j -= 1
        tmp -= cumB[j]
    l = j
    ans = max(i+j,ans)
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]*(n+1)
B = [0]*(m+1)
cnt  =0

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

j = m
for i in range(n+1):
    if A[i] > k:
        break
    while B[j] + A[i] > k:
        j -= 1
    cnt = max(cnt, i + j)

print(cnt)

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

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

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

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

A_length = len(A)
B_length = len(B)

j = 0
for i in range(A_length):

    while j < B_length:
        if A[i] + B[j] <= K:
            j += 1
        else:
            break
    j -= 1

    ans = max(ans, A_length - (i + 1) + j)
print(ans)

Solution (Python):
def sep():
    return map(int,input().strip().split(" "))
def lis():
    return list(sep())

n,m,k=sep()
a=lis()
b=lis()
from bisect import bisect_right
a_c=[a[0]]
b_c=[b[0]]
for i in range(1,n):
    a_c.append(a_c[-1]+ a[i])
for i in range(1,m):
    b_c.append(b_c[-1] + b[i])

m=-1
m=max(m,bisect_right(b_c,k))
for i in range(n):
    if a_c[i]<=k:
        t=k-a_c[i]
        t2=bisect_right(b_c,t)
        m=max(i+1+t2,m)
    else:
        break
print(max(0,m))

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




A.insert(0, 0)

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

ans = 0
for i in range(len(A)):
  rest_time = k - A[i]
  if rest_time >= 0:
    numb = bisect_right(B, rest_time)
    anstmp = i + numb 
    ans = max(ans, anstmp)
print(ans)

Solution (Python):
def getmax(x,y):
    if x>y:
        return x
    else:
        return y
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=list(map(int,input().split()))
B=list(map(int,input().split()))
sum1=[0]
sum2=[0]
for i in range(N):
    sum1.append(sum1[-1]+A[i])
for i in range(M):
    sum2.append(sum2[-1]+B[i])

ans=0
for i in range(N+1):
    if sum1[i]>K:
        break
    ans=max(ans,i+bisect.bisect_right(sum2,K-sum1[i])-1)
print(ans)

Solution (Python):
import numpy as np
n, m, k = map(int, input().split())
li_a = np.array(list(map(int, input().split())))
li_b = np.array(list(map(int, input().split())))
a_i, b_i = np.hstack([np.array([0]),li_a.cumsum()]), np.hstack([np.array([0]),li_b.cumsum()])

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

Solution (Python):

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

A = list(map(int, input().split()))
B = list(map(int, input().split()))
accum_A, accum_B = [0] * (len(A)+1), [0] * (len(B)+1)
for i in range(len(A)):
    accum_A[i+1] = accum_A[i] + A[i]
for i in range(len(B)):
    accum_B[i+1] = accum_B[i] + B[i]

ans = 0
i, j = len(A), 0
while i >= 0 and j <= len(B) :
    if accum_A[i] + accum_B[j] > K:
        i -= 1
    else:
        ans = max(ans, i+j)
        j += 1

print(ans)

Solution (Python):
n,m,k=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
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

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 = 0
j = M

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

Solution (Python):
import bisect
n, m, k = map(int, input().split())
aaa = list(map(int, input().split()))
bbb = list(map(int, input().split()))
sum_a = [0] * (n + 1)
sum_b = [0] * (m + 1)

for i in range(n):
    sum_a[i + 1] = sum_a[i] + aaa[i]

for j in range(m):
    sum_b[j + 1] = sum_b[j] + bbb[j]

ans = 0
for i in range(n + 1):
    if sum_a[i] > k:
        break
    tmp = bisect.bisect_right(sum_b, k - sum_a[i])
    ans = max(ans, i + tmp - 1)
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]

i = N
total = 0
ans = 0
for j in range(M+1):
    total += B[j]
    while i >= 0 and A[i]+total > K:
        i -= 1
    if A[i]+total <= K:
        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()))
asum=[0]
bsum=[0]
for i in range(n):
    asum.append(asum[-1]+a[i])
for i in range(m):
    bsum.append(bsum[-1]+b[i])
ans=0
for i in range(n+1):
    if asum[i]>k:
        break
    while asum[i]+bsum[m]>k and m>0:
        m-=1
    ans=max(ans,i+m)
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]*(n+1)
d = [0]*(m+1)
for i in range(n):
    c[i+1] = a[i]+c[i]
for i in range(m):
    d[i+1] = b[i]+d[i]
for i in range(m+1)[::-1]:
    if d[i] <= k:
        x = i
        break
ans = x
for i in range(1,n+1):
    while c[i] + d[x] > k:
        if x >= 1:
            x -= 1
        else:
            break
    if c[i] + d[x] <= k:
        ans = max(ans,i+x)
print(ans)

Solution (Python):
from sys import stdin
nii=lambda:map(int,stdin.readline().split())
lnii=lambda:list(map(int,stdin.readline().split()))
from bisect import bisect_right
from itertools import accumulate

n,m,k=nii()
a=lnii()
b=lnii()
ar=[0]+list(accumulate(a))
br=list(accumulate(b))

ans=0
for i in range(n+1):
  time=ar[i]
  if time>k:
    break
  t_ans=i
  inx=bisect_right(br,k-time)
  t_ans+=inx
  ans=max(ans,t_ans)
print(ans)

Solution (Python):
def main():
    line1 = input()
    line2 = input()
    line3 = input()
    n, m, k = list(map(int, line1.split()))
    a = [0] + list(map(int, line2.split()))
    b = [0] + list(map(int, line3.split()))
    mark1, mark2 = 0, 0
    for i in range(1, len(a)):
        a[i] += a[i-1]
        if a[i] > k:
            mark1 = i
            break
    for i in range(1, len(b)):
        b[i] += b[i-1]
        if b[i] > k:
            mark2 = i
            break
    if mark1:
        a = a[:mark1]
    if mark2:
        b = b[:mark2]
    ans = 0
    for i in range(len(a)):
        target = k - a[i]
        tmp = 0
        low, high = 0, len(b)
        while low < high:
            mid = (low + high) // 2
            if b[mid] <= target:
                tmp = mid
                low = mid + 1
            else:
                high = mid - 1
        ans = max(ans, i + tmp)
    print(ans)
    return

if __name__ == "__main__":
    main()

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

from itertools import accumulate
import bisect
cumsum_a = list(accumulate(a))
cumsum_b = list(accumulate(b))

ans = 0
for i in range(1, n+1):
    if cumsum_a[i-1] > k:
        break
    
    cnt_b = bisect.bisect(cumsum_b, k - cumsum_a[i-1])
    ans = max(ans, i + cnt_b)

for i in range(1, m+1):
    if cumsum_b[i-1] > k:
        break
    
    cnt_a = bisect.bisect(cumsum_a, k - cumsum_b[i-1])
    ans = max(ans, i + cnt_a)

print(ans)

Solution (Python):
import numpy as np
import bisect
N,M,K= map(int, input().split())
A=list(map(int, input().split()))
B=list(map(int, input().split()))
if A[0]>K and B[0]>K:print(0);exit()

Asum=[0]
for i in range(N):
  if Asum[-1]+A[i]<=K:
    Asum.append(Asum[-1]+A[i])  
  else:break
Bsum=[0]
for j in range(M):
  if Bsum[-1]+B[j]<=K:
    Bsum.append(Bsum[-1]+B[j])
  else:break

C=[]
for i in range(len(Asum)):
  C.append(bisect.bisect(Bsum,K-Asum[i])+i-1)
print(max(C))

Solution (Python):
import numpy as np
import bisect
N,M,K= map(int, input().split())
A=list(map(int, input().split()))
B=list(map(int, input().split()))
if A[0]>K and B[0]>K:print(0);exit()

Asum=[0]
for i in range(N):
  if Asum[-1]+A[i]<=K:
    Asum.append(Asum[-1]+A[i])  
  else:break
    
Bsum=[0]
for j in range(M):
  if Bsum[-1]+B[j]<=K:
    Bsum.append(Bsum[-1]+B[j])
  else:break
    







C=[]
for i in range(len(Asum)):
  C.append(bisect.bisect(Bsum,K-Asum[i])+i-1)





print(max(C))

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

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

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

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

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


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

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

print(ans)

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

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

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

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

    total_T_in_A = [0] * (N+1)
    total_T_in_B = [0] * (M+1)

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

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

    result = 0
    for i in range(N+1):
        
        i_A_T = total_T_in_A[i]
        
        if K < i_A_T:
            continue

        if K == i_A_T:
            result = max(result, i)
            continue

        rem_T = K - i_A_T

        
        
        first = total_T_in_B[1]
        last = total_T_in_B[M]
        if rem_T < first:
            result = max(result, i)
            continue
        if last <= rem_T:
            result = max(result, i + M)
            continue

        
        first = 1
        last = M
        while first < last:
            if abs(last - first) <= 1:
                break
            mid = (first + last) // 2
            if rem_T < total_T_in_B[mid]:
                last = mid
            else:
                first = mid
        result = max(result, i + first)

    print(result)

main()

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

    total_T_in_A = [0] * (N+1)
    
    
    total_T_in_B = [0] * (M+1)

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

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

    result = 0
    for i in range(N+1):
        
        i_A_T = total_T_in_A[i]
        
        if K < i_A_T:
            continue

        if K == i_A_T:
            result = max(result, i)
            continue

        rem_T = K - i_A_T

        
        
        first = total_T_in_B[1]
        last = total_T_in_B[M]
        if rem_T < first:
            result = max(result, i)
            continue
        if last <= rem_T:
            result = max(result, i + M)
            continue

        
        first_i = 1
        last_i = M
        while first_i < last_i:
            if abs(last_i - first_i) <= 1:
                break
            mid_i = (first_i + last_i) // 2
            if rem_T < total_T_in_B[mid_i]:
                last_i = mid_i
            else:
                first_i = mid_i
        result = max(result, i + first_i)

    print(result)

main()

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

    total_T_in_A = [0] * (N+1)
    total_T_in_B = [0] * M
    total_T_in_B[0] = B[0]

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

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

    result = 0
    for i in range(N+1):
        
        i_A_T = total_T_in_A[i]
        
        if K < i_A_T:
            continue

        if K == i_A_T:
            result = max(result, i)
            continue

        rem_T = K - i_A_T

        
        
        first = total_T_in_B[0]
        last = total_T_in_B[M-1]
        if rem_T < first:
            result = max(result, i)
            continue
        if last <= rem_T:
            result = max(result, i + M)
            continue

        
        first_i = 0
        last_i = M - 1
        while first_i < last_i:
            if abs(last_i - first_i) == 1:
                break
            mid_i = (first_i + last_i) // 2
            if rem_T < total_T_in_B[mid_i]:
                last_i = mid_i
            else:
                first_i = mid_i
        result = max(result, i + first_i + 1)

    print(result)

main()

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

    total_T_in_A = [0] * (N+1)
    total_T_in_A[1] = A[0]
    total_T_in_B = [0] * M
    total_T_in_B[0] = B[0]

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

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

    result = 0
    for i in range(N+1):
        
        i_A_T = total_T_in_A[i]
        
        if K < i_A_T:
            continue

        if K == i_A_T:
            result = max(result, i)
            continue

        rem_T = K - i_A_T

        
        
        first = total_T_in_B[0]
        last = total_T_in_B[M-1]
        if rem_T < first:
            result = max(result, i)
            continue
        if last <= rem_T:
            result = max(result, i + M)
            continue

        
        first_i = 0
        last_i = M - 1
        while first_i < last_i:
            if abs(last_i - first_i) == 1:
                break
            mid_i = (first_i + last_i) // 2
            if rem_T < total_T_in_B[mid_i]:
                last_i = mid_i
            else:
                first_i = mid_i
        result = max(result, i + first_i + 1)

    print(result)

main()

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

    total_T_in_A = [0] * (N+1)
    total_T_in_A[1] = A[0]
    total_T_in_B = [0] * M
    total_T_in_B[0] = B[0]

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

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

    result = 0
    for i in range(N+1):
        
        i_A_T = total_T_in_A[i]
        
        if K < i_A_T:
            continue

        if K == i_A_T:
            result = max(result, i)
            continue

        rem_T = K - i_A_T

        
        
        first = total_T_in_B[0]
        last = total_T_in_B[M-1]
        if rem_T < first:
            result = max(result, i)
            continue
        if last <= rem_T:
            result = max(result, i + M)
            continue

        
        first_i = 0
        last_i = M - 1
        while first_i < last_i:
            if abs(last_i - first_i) == 1:
                break
            mid_i = (first_i + last_i) // 2
            if rem_T < total_T_in_B[mid_i]:
                last_i = mid_i
            else:
                first_i = mid_i
        result = max(result, i + first_i + 1)

    print(result)

main()

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

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

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

Solution (Python):
"""path = "/Users/hemmishinichi/PycharmProjects/AtCoder/ABC/ABC172/b08.txt"
with open(path) as f:
    l_strip = [s.strip() for s in f.readlines()]
    N, M, K = map(int, l_strip[0].split())
    A = [int(i) for i in l_strip[1].split()]
    B = [int(i) for i in l_strip[2].split()]
"""
N, M, K = map(int, input().split())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]

time = 0
num_A = 0

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

i = 0
while i < M:
    
    while i < M and time + B[i] <= K:
        time += B[i]
        i += 1
    ans = max(num_A+i, ans)
    if i == M-1:
        break
    elif num_A > 0:
        num_A -= 1
        time -= A[num_A]
    else:
        break

print(ans)

Solution (Python):
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
import numpy as np
def main():
    n, m, k = map(int, input().split())
    a = np.array(readline().split(), np.int64)
    b = np.array(readline().split(), np.int64)
    aa = np.zeros(n + 1, np.int64)
    ba = np.zeros(m + 1, np.int64)
    aa[1:] = a.cumsum()
    ba[1:] = b.cumsum()
    aa = aa[aa <= k]
    ba = ba[ba <= k]
    rs = np.searchsorted(ba, k - aa, side='right') - 1
    rs += np.arange(len(aa))
    r = rs.max()
    print(r)

if __name__ == '__main__':
    main()

Solution (Python):
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
import numpy as np
def main():
    n, m, k = map(int, input().split())
    a = np.array(readline().split(), np.int64)
    b = np.array(readline().split(), np.int64)
    aa = a.cumsum()
    ba = b.cumsum()
    r = np.searchsorted(ba, k, side='right')    
    for i1, aae in enumerate(aa):
        if k < aae:
            break
        r = max(r, np.searchsorted(ba, k - aae, side='right') + i1 + 1)
    print(r)

if __name__ == '__main__':
    main()

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

    for i1, ae in enumerate(a):
        if k < ae:
            break
        r = max(r, bisect(b, k - ae) + i1 + 1)
    print(r)

if __name__ == '__main__':
    main()

Solution (Python):

from math 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, combinations_with_replacement


n, m, k = map(int, input().split())
A = list(accumulate(map(int, input().split())))
B = list(accumulate(map(int, input().split())))
A=[0]+A
B=[0]+B
ans = []
for i in range(n+1):
    c = bisect_right(B, k-A[i])-1
    if c!=-1:
        ans.append(c+i)
print(max(ans))

Solution (Python):
from bisect import bisect_right

n, m, k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
cnt = 0
a_cum = [0] * (n + 1)
b_cum = [0] * (m + 1)

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

for i in range(n+1):
    a_cnt = i
    rest = k - a_cum[i]
    if rest < 0:
        break
    b_cnt = bisect_right(b_cum,rest) - 1    
    cnt = max(cnt, a_cnt + b_cnt)

print(cnt)

Solution (Python):
def judge(n,array1,array2,K):
    for i in range(n+1):
        if len(array1) > i and len(array2) > n-i:
            sum = array1[i] + array2[n-i]
            if sum <= K:
                return True
    return False

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

cum_sum_As = [0]
cum_sum_Bs = [0]

sum_As = 0
for i in range(N):
    sum_As += As[i]
    cum_sum_As.append(sum_As)

sum_Bs = 0
for i in range(M):
    sum_Bs += Bs[i]
    cum_sum_Bs.append(sum_Bs)

l = 0
r = N + M
while(r-l>1):
    mid = (l+r)//2
    if judge(mid,cum_sum_As,cum_sum_Bs,K):
        l = mid
    else:
        r = mid

if judge(r,cum_sum_As,cum_sum_Bs,K):
    print(r)
else:
    print(l)

Solution (Python):
if __name__ == '__main__':
    N, M, K = map(int, input().split()) 
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    
    acc_A = []
    acc_B = []
    acc_A.append(0)
    acc_B.append(0)
    
    sum_a = 0
    sum_b = 0

    for a in A:
        sum_a += a
        acc_A.append(sum_a)

    for b in B:
        sum_b += b
        acc_B.append(sum_b)

    large_ar = []
    small_ar = []

    if N > M:
        large_ar = acc_A
        small_ar = acc_B
    else:
        large_ar = acc_B
        small_ar = acc_A
    
    ans = 0
    for i in range(len(small_ar)):
        for j in range(ans-i+1, len(large_ar)):
            if large_ar[j] + small_ar[i] <= K:
                ans = max(ans, i+j)
            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()))

b = 0
tb = 0

for i in range(M):
    if tb + B[i] > K:
        break
    tb += B[i]
    b = i+1

ans = [0, b]
m = b
a = 0
ta = 0

for i in range(N):
    if ta + A[i] > K:
        break
    a = i+1
    ta += A[i]
    while True:
        if ta + tb > K:
            if b == 0:break
            b -= 1
            tb -= B[b]
        else:
            if a + b > m:
                m = a + b
                ans = [a, b]
            break

print(ans[0]+ans[1])