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.



Sample input:
3 4 240
60 90 120
80 150 80 150


Sample output:
3

Submission status: Accepted

Solution code:
production = True

import sys, math, collections

def input(input_format = 0, multi = 0):

    if multi > 0: return [input(input_format) for i in range(multi)]
    else:
        next_line = sys.stdin.readline()[:-1]

        if input_format >= 10:
            use_list = False
            input_format = int(str(input_format)[-1])
        else: use_list = True

        if input_format == 0: formatted_input = [next_line]
        elif input_format == 1: formatted_input = list(map(int, next_line.split()))
        elif input_format == 2: formatted_input = list(map(float, next_line.split()))
        elif input_format == 3: formatted_input = list(next_line)
        elif input_format == 4: formatted_input = (lambda x: [x[0], list(map(int, x[1:]))])(next_line.split())
        elif input_format == 5: formatted_input = (lambda x: [x[0], list(map(float, x[1:]))])(next_line.split())
        elif input_format == 6: formatted_input = next_line.split()
        else: formatted_input = [next_line]

        return formatted_input if use_list else formatted_input[0]


def out(output_line, output_format = 0, newline = True):

    formatted_output = ""

    if output_format == 0: formatted_output = str(output_line)
    elif output_format == 1: formatted_output = " ".join(map(str, output_line))
    elif output_format == 2: formatted_output = "\n".join(map(str, output_line))

    print(formatted_output, end = "\n" if newline else "")


def log(*args):
    if not production:
        print("$$$", end = "")
        print(*args)

enum = enumerate

#
#   >>>>>>>>>>>>>>> START OF SOLUTION <<<<<<<<<<<<<<
#


def solve():

    n, m, k = input(1)
    a = input(1)
    b = input(1)


    ap = [0]
    bp = [0]

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

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


    log(ap, bp)

    pa = 0

    while pa < len(ap) and ap[pa] <= k:
        pa += 1

    m = pa - 1
    pb = 1

    c = k - ap[pa - 1]

    for i in range(pa - 1, -1, -1):
        while pb < len(bp) and bp[pb] <= c:
            m = max(pa - 1 + pb, m)
            pb += 1
        pa -= 1
        c += a[pa - 1]

    out(m)


        
    



    return


#for i in range(input(11)): solve()
solve()

#
#   >>>>>>>>>>>>>>>> END OF SOLUTION <<<<<<<<<<<<<<<
#

Submission status: Accepted

Solution code:
from itertools import accumulate


def binary_search(key):
    bad, good = -1, len(B)
    while good - bad > 1:
        mid = (bad + good) // 2
        if B[mid] > key:
            good = mid
        else:
            bad = mid
    return good


N, M, K = map(int, input().split())
A = [0] + list(accumulate(map(int, input().split())))
B = list(accumulate(map(int, input().split())))
ans = 0
for i in range(N + 1):
    tmp = A[i]
    if tmp > K:
        break
    m = binary_search(K - tmp)
    ans = max(ans, i + m)
print(ans)

Submission status: Accepted

Solution code:
import bisect

n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
for i in range(1, n):
    a[i] += a[i - 1]
for i in range(1, m):
    b[i] += b[i - 1]
ans = 0
a.insert(0, 0)
for i in range(n + 1):
    x = k - a[i]
    if not x < 0:
        j = bisect.bisect_right(b, x)
        ans = max(ans, i + j)
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_right as br
n,m,k = map(int,input().split())
a = [int(i) for i in input().split()]
sa = [0]*(n+1)
for i in range(n): sa[i+1] = sa[i]+a[i]
b = [int(i) for i in input().split()]
sb = [0]*(m+1)
for i in range(m): sb[i+1] = sb[i]+b[i]
ans = 0
for i in range(n+1):
    cnt = sa[i]
    if cnt > k: break
    ans = max(ans,br(sb,k-cnt)-1+i)
print(ans)

Submission status: Accepted

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

liA = [0]
liB = [0]

for i in range(N):
    liA.append(liA[-1]+A[i])

for i in range(M):
    liB.append(liB[-1]+B[i])

# print(liA)
# print(liB)

ans = 0
for i in range(N+1):
    mm = K - liA[i]
    if mm<0:
        break
    fst = 0
    lst = M

    while(lst-fst>0):
        mid = ((fst+lst)//2)+1
        if mm >= liB[mid]:
            fst = mid
        elif mm<liB[mid]:
            lst = mid-1
        # print(fst,lst)
    ans = max(ans, i+fst)

print(ans)

Submission status: Accepted

Solution code:
n,m,k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
A = n
B = 0
for i in range(1,n):
    a[i] += a[i-1]
    if (a[i]>k):
        A = i
        break
for i in range(1,m):
    b[i] += b[i-1]
ans = A
if (a[0]>k):
        for i in range(m):
            if (b[i]>k):
                print(i)
                exit()
        print(len(b))
        exit()
a = [0] + a
b = [0] + b
while True:
    if (A<0):
        break
    if (B>=m):
        break
    B += 1
    #print(A,B)
    if (a[A]+b[B] >k):
        B -= 1
        A -= 1
    else:
        ans = max(ans,A+B)
print(ans)

Submission status: Accepted

Solution code:
import sys
# import re
import math
import collections
# import decimal
import bisect
import itertools
import fractions
# import functools
import copy
# import heapq
import decimal
# import statistics
import queue

# import numpy as np

sys.setrecursionlimit(10000001)
INF = 10 ** 16
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===

def main():
    n, m, k = ns()
    a = na()
    b = na()

    perm_a = [0 for _ in range(n + 1)]
    perm_b = [0 for _ in range(m + 1)]

    for i, ai in enumerate(a):
        perm_a[i + 1] = perm_a[i] + ai

    for i, bi in enumerate(b):
        perm_b[i + 1] = perm_b[i] + bi

    perm_a.append(INF)
    perm_b.append(INF)

    ans = 0
    for i, pa in enumerate(perm_a):
        bserch = k - pa
        if bserch < 0:
            continue
        if bserch == 0:
            ans = max(ans, i)
            continue

        idx = bisect.bisect_left(perm_b, bserch)
        tmp_ans = i + idx - 1
        if perm_b[idx] == bserch:
            tmp_ans += 1
        ans = max(ans, tmp_ans)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n, m, k = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
cuma = [0]
cumb  = [0]
for a in A:
    cuma.append(cuma[-1] + a)
for b in B:
    cumb.append(cumb[-1] + b)
from bisect import bisect_left
ans = 0
for i in range(n+1):
    if k < cuma[i]: continue
    idx = bisect_left(cumb, k - cuma[i] + 1)
    ans = max(ans, i + idx - 1)
for j in range(m+1):
    if k < cumb[j]: continue
    idx = bisect_left(cuma, k - cumb[j] + 1)
    ans = max(ans, j + idx - 1)
print(ans)

Submission status: Accepted

Solution code:

def main():
  n, m, k = map(int, input().split())
  A = list(map(int, input().split()))
  B = list(map(int, input().split()))
  B.insert(0, 0)

  data = [None] * n
  total = 0
  for i in range(n):
    a = A[i]
    data[i] = (total, total + a)
    total += a
  data.append((total, float('inf')))

  def bi_search(num, l, r):
    if l > r:
      return n

    i = (l + r) // 2

    tail, head = data[i]
    if tail <= num < head:
      return i

    if num < tail:
      return bi_search(num, l, i - 1)

    return bi_search(num, i + 1, r)


  ans = 0
  total = 0
  for j in range(m + 1):
    total += B[j]
    if total > k:
      break

    i = bi_search(k - total, 0, n - 1)
    if i + j > ans:
      ans = i + j

  print(ans)


main()

Submission status: Accepted

Solution code:
import numpy as np
n,m,k=map(int,input().split())
a=[0]
c=list(map(int,input().split()))
b=list(map(int,input().split()))

for _ in range(n):
    a.append(c[_])

a.append(10**10)
b.append(10**10)

ara=np.array(a)
arb=np.array(b)
la=np.cumsum(ara)
lb=np.cumsum(arb)

ans=0

i=0
while la[i]<=k:
    rest=k-la[i]
    j=np.searchsorted(lb,rest,side="right")
    ans=max(i+j,ans)
    i+=1
print(ans)

Submission status: Accepted

Solution code:
n,m,k=map(int,input().split())

a=list(map(int,input().split()))
b=list(map(int,input().split()))

suma=sum(a)
ai=n-1
sumb=0
bi=-1

ans=0

while ai>=0:
    if ai ==n-1:
        while suma+sumb<=k:
            ans=max(ans,ai+1+bi+1)

            if bi+1<m:
                bi+=1
                sumb+=b[bi]
            else:
                break


    suma-=a[ai]
    ai-=1
    while suma+sumb<=k:
        ans=max(ans,ai+1+bi+1)

        if bi+1<m:
            bi+=1
            sumb+=b[bi]
        else:
            break
    #print(ai,bi)



print(ans)

Submission status: Accepted

Solution code:
from sys import stdin, setrecursionlimit
#input = stdin.buffer.readline
setrecursionlimit(10 ** 7)

from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
from collections import deque, defaultdict, Counter
from itertools import combinations, permutations, combinations_with_replacement
from itertools import accumulate
from math import ceil, sqrt, pi, radians, sin, cos

MOD = 10 ** 9 + 7
INF = 10 ** 18

#N = int(input())
#S = input()
N, M, K = map(int, input().split())
#A = list(map(int, input().split()))
#A = [int(input()) for _ in range(N)]
A = list(map(int, input().split()))
B = list(map(int, input().split()))

AcumA = [0] + list(accumulate(A))
AcumB = [0] + list(accumulate(B))

ia, ib = 0, M 
answer = 0

while ib >= 0 and ia <= N:
    if AcumA[ia] + AcumB[ib] <= K:
        answer = max(answer, ia + ib)
        ia += 1
    else:
        ib -= 1

print(answer)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 8)
from itertools import accumulate
from itertools import permutations
from itertools import combinations
from collections import defaultdict
from collections import Counter
import fractions
import math
from collections import deque
from bisect import bisect_left
from bisect import bisect_right
from bisect import insort_left
import itertools
from heapq import heapify
from heapq import heappop
from heapq import heappush
import heapq
from copy import deepcopy
from decimal import Decimal
alf = list("abcdefghijklmnopqrstuvwxyz")
ALF = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
#import numpy as np
INF = float("inf")
#d = defaultdict(int)
#d = defaultdict(list)
N,M,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
a = []
aa = 0
bb = 0
b = []
flag = 0
for i in range(N):
    aa += A[i]
    a.append(aa)
for i in range(M):
    bb += B[i]
    b.append(bb)
MAX = 0
for i in range(N):
    t = a[i]
    if t > K:
        break
    else:
        tt = K-t
        index = bisect_left(b,tt)
        if index == M:
            MAX = max(MAX,i+1+M)
        elif b[index] == tt:
            MAX = max(MAX,i+1+1+index)
        else:
            MAX = max(MAX,i+1+index)
for i in range(M):
    t = b[i]
    if t > K:
        break
    else:
        tt = K-t
        index = bisect_left(a,tt)
        if index == N:
            MAX = max(MAX,i+1+N)
        elif a[index] == tt:
            MAX = max(MAX,i+1+1+index)
        else:
            MAX = max(MAX,i+1+index)


        
print(MAX)

Submission status: Accepted

Solution code:
import sys
from collections import deque
import bisect

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_nl = lambda: list(map(int, readline().split()))
in_na = lambda: map(int, read().split())
in_s = lambda: readline().rstrip().decode('utf-8')


def main():

    N, M, K = in_nn()
    A = in_nl()
    B = in_nl()

    Asum = [0] * (N + 1)
    Bsum = [0] * (M + 1)

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

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

    ans = 0
    for i in range(N + 1):
        time = K - Asum[i]
        if time < 0:
            break
        else:
            ans = max(ans, i + bisect.bisect_right(Bsum, time) - 1)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n, m, k = map(int, input().split())
a_ls = list(map(int, input().split()))
b_ls = list(map(int, input().split()))
sub = 0
a_ls_cnt = 0
a_ls_len = len(a_ls)
now_time = 0
#aのみを選択した場合
#このwhile終了後はa_ls_cntは読んだaの本数になる
while now_time < k:
    now_time+=a_ls[a_ls_cnt]
    if now_time > k:
        now_time-=a_ls[a_ls_cnt]
        break
    a_ls_cnt+=1
    if a_ls_len == a_ls_cnt:
        break
#print(a_ls_cnt)
b_ls_cnt = 0
b_ls_len = len(b_ls)
#a全部をよんだ段階で，bで読めるだけ
while now_time < k:
    now_time+=b_ls[b_ls_cnt]
    if now_time > k:
        now_time-=b_ls[b_ls_cnt]
        break
    b_ls_cnt+=1
    if b_ls_len == b_ls_cnt:
        break

sub = a_ls_cnt+b_ls_cnt
break_flg = 0
#print(sub)
#少しずつaで読んだ本数削る
while a_ls_cnt >= 1:
    if break_flg == 1:
        break
    now_time-=a_ls[a_ls_cnt-1]
    a_ls_cnt-=1
    while now_time < k:
        #print("b_ls",b_ls_cnt, b_ls_len)
        if b_ls_cnt == b_ls_len:
            break_flg = 1
            sub = max(sub, a_ls_cnt+b_ls_cnt)
            break
        now_time+=b_ls[b_ls_cnt]
        if now_time > k:
            now_time-=b_ls[b_ls_cnt]
            break
        b_ls_cnt+=1
    sub = max(sub, a_ls_cnt+b_ls_cnt)
    #print(a_ls_cnt, b_ls_cnt, sub)
print(sub)

Submission status: Accepted

Solution code:
import itertools
import bisect

def main():
    N,M,K  = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    AS = list(itertools.accumulate(A))
    BS = list(itertools.accumulate(B))
    Amaxin = bisect.bisect_right(AS, K)
    Bmaxin = bisect.bisect_right(BS, K)
    ans = max(Amaxin-1,Bmaxin-1)
    index = 0
    for i in reversed(range(0,Amaxin)):
        while index<M and AS[i] + BS[index] <=K:
            index+=1
        ans = max(i+index+1,ans)
    index = 0
    for i in reversed(range(0,Bmaxin)):
        while index<N and BS[i] + AS[index] <=K:
            index+=1
        ans = max(i+index+1,ans)

    print(max(ans,0))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from itertools import accumulate
from bisect import bisect_right
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

def ruiseki(lst):
    return [0]+list(accumulate(lst))

ar = ruiseki(A)
br = ruiseki(B)
ans = 0
for i in range(1+N):
    left = K - ar[i]
    if left < 0:
        continue
    bidx = bisect_right(br, left) - 1
    ans = max(ans,i+bidx)

print(ans)

Submission status: Accepted

Solution code:
import bisect
n,m,k = map(int,input().split())
a= list(map(int,input().split()))
b= list(map(int,input().split()))
at=[0]
bt=[]
temp=0
for i in a:
    temp+=i
    at.append(temp)
temp=0
for i in b:
    temp+=i
    bt.append(temp)
ans=0
for i in range(len(at)):
    if at[i]>k:
        break
    ind= bisect.bisect_right(bt,k-at[i])
    ans= max(ans, ind+i)
print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate
import bisect
n,m,k=map(int,input().split())
a=[int(i) for i in input().split()]
b=[int(i) for i in input().split()]

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

ans = max(bisect.bisect_right(B,k),bisect.bisect_right(A,k))

for i in range(len(a)):
    r = (k-A[i])
    if r<0:
        break
    #print(r,bisect.bisect_left(B,r))
    #print(B[bisect.bisect_left(B,r)])
    ans=max(ans,i+1+bisect.bisect_right(B,r))
    #print(i+1,r,i+1+bisect.bisect_right(B,r))
    
for i in range(len(b)):
    r = (k-B[i])
    if r<0:
        break
    #print(r,bisect.bisect_left(B,r))
    #print(B[bisect.bisect_left(B,r)])
    ans=max(ans,i+1+bisect.bisect_right(A,r))
    #print(i+1,r,i+1+bisect.bisect_right(A,r))
    
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**9)

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

    a.insert(0, 0)
    b.insert(0, 0)

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

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

    b_sum = b_sum[::-1]

    i_b = 0
    ans = 0
    for i_a in range(n+1):
        while i_b <= m:
            if k < a_sum[i_a] + b_sum[i_b]:
                i_b += 1
                continue
            else:
                ans = max(ans, i_a+(m-i_b))
                break

    print(ans)
    return


if __name__ == "__main__":
    main()
    sys.exit(0)

Submission status: Accepted

Solution code:
import itertools
import copy
from collections import deque
import heapq
def binary_search(List, item):
    low = 0
    high = len(List) - 1
    while low <= high:
        mid = (low + high) //2
        guess = List[mid]
        if guess == item:
            return mid
        if guess > item:
            high = mid -1
        else:
            low = mid + 1
    return high
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
p = [0]
q = []
c = 0
d = 0
for i in a:
    if i + c > k:
        break
    c += i
    p.append(c)
for i in b:
    if i + d > k:
        break
    d += i
    q.append(d)
ans = len(p)-1
for i in range(1,len(p)+1):
    check = p[-1*i]
    s = k - check
    c_ans = len(p)-i
    c_ans += binary_search(q,s)+1
    #print(binary_search(q,s)+1,q,s,c,check)
    ans = max(ans,c_ans)
    #print(ans,c_ans)
print(ans)

Submission status: Accepted

Solution code:
def main():
    import sys
    read = sys.stdin.buffer.read
    readline = sys.stdin.buffer.readline
    readlines = sys.stdin.buffer.readlines
    sys.setrecursionlimit(10 ** 7)

    from itertools import accumulate
    from bisect import bisect_right

    n, m, k = map(int, readline().split())
    a = list(map(int, readline().split()))
    b = list(map(int, readline().split()))
    a = [0] + list(accumulate(a))
    b = list(accumulate(b)) + [float('inf')] * 2
    ans = 0
    for i, aa in enumerate(a):
        if k < aa:
            break
        memo = bisect_right(b, k - aa)
        if b[memo + 1] == k - aa:
            memo += 1
        ans = max(ans, memo + i)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from bisect import bisect_right
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(m):
    b[i+1] += b[i]


def f(time, r):
    ret = bisect_right(r, time) - 1
    return ret


def fa(time):
    return f(time, a)


def fb(time):
    return f(time, b)

ans = 0

for i, ai in enumerate(a):
    k -= ai
    if k < 0:
        break
    ans = max(ans, i + fb(k))

print(ans)

Submission status: Accepted

Solution code:
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
from bisect import bisect_right
dpa = [0]
for a in A:
    dpa.append(dpa[-1]+a)
dpb = [0]
for b in B:
    dpb.append(dpb[-1]+b)
ans = 0
for i in range(N+1):
    t = dpa[i]
    if t > K:
        break
    j = bisect_right(dpb, K-t)
    ans = max(ans, i + j - 1)
print(ans)

Submission status: Accepted

Solution code:
import bisect
from itertools import accumulate

n, m, k = map(int, input().split())
a = tuple(accumulate(map(int, input().split())))
b = tuple(accumulate(map(int, input().split())))

cnt = 0
for i, j in enumerate(a):
    if j <= k:
        cnt = max(cnt, i + bisect.bisect_right(b, k - j) + 1)

for i, j in enumerate(b):
    if j <= k:
        cnt = max(cnt, i + bisect.bisect_right(a, k - j) + 1)

print(cnt)

Submission status: Accepted

Solution code:
from itertools import accumulate
n,m,k = map(int, input().split())
a = list(map(int,input().split()))[::-1]
b = list(map(int,input().split()))

l = [0] + a + b
acc = list(accumulate(l))

# print(acc)

ans = 0
l = 0
for r in range(1, n+m+1):
    while acc[r] - acc[l] > k:
        l += 1
    # print(l,r)
    if l <= n and r >= n:
        ans = max(ans,r-l)
    # print(ans)
print(ans)

Submission status: Accepted

Solution code:
import sys
from bisect import bisect_right, bisect_left

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

ap = [0 for i in range(n)]
bp = [0 for j in range(m)]

for i in range(n):
	if i == 0:
		ap[i] = a[i]
	else: 
		ap[i] = a[i] + ap[i - 1]

for i in range(m):
	if i == 0:
		bp[i] = b[i]
	else: 
		bp[i] = b[i] + bp[i - 1]

bestRes = 0

#print(bp)

def search(x):
	left = 0 
	right = m - 1
	while right - left > 1:
		mid = left + (right - left) // 2
		if bp[mid] == x:
			return mid

		if bp[mid] < x:
			right = mid - 1
		else:
			left = mid + 1


	if bp[right] <= x:
		return right
	elif bp[left] <= x:
		return left
	else:
		return left - 1

for i in range(n):
	#print(i)
	if ap[i] > k:
		break

	rest = k - ap[i]

	#print(rest)

	if bp[0] > rest:
		bestRes = max(bestRes, i + 1)

	else:
		count = bisect_right(bp, rest)
		#print(i, rest, count)

		bestRes = max(bestRes, i + count + 1)


#print(bp)
#bisect_right(bp, k)

if k > bp[0]:
	count = bisect_right(bp, k)
	bestRes = max(bestRes, count)

print(bestRes)

Submission status: Accepted

Solution code:
def iteraccumulate(l):
    from itertools import accumulate
    return list(accumulate(l))


def resolve():
    from bisect import bisect_right
    inf = 10 ** 10
    n, m, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    aa = [0] + iteraccumulate(a) + [inf]
    bb = iteraccumulate(b) + [inf]
    lbb = len(bb)
    ans = 0
    for index, i in enumerate(aa):
        j = bisect_right(bb, k - i)
        if j == 0 and i <= k:
            ans = max(ans, index)
        elif j > 0 and i + bb[j - 1] <= k:
            ans = max(ans, index + j)
    print(ans)


if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
def check(A, mid, K):
    return A[mid] <= K


def binMax(A, K):
    ok = -1  # maxVal when minimize
    ng = len(A)  # maxVal when maximize
    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2
        if check(A, mid, K):
            ok = mid
        else:
            ng = mid
    return ok


def resolve():
    N, M, K = [int(i) for i in input().split()]
    A = [int(i) for i in input().split()]
    B = [int(i) for i in input().split()]
    presumA = [0 for _ in range(N + 1)]
    for i in range(N):
        presumA[i + 1] = presumA[i] + A[i]
    presumB = [0 for _ in range(M + 1)]
    for i in range(M):
        presumB[i + 1] = presumB[i] + B[i]
    startA = binMax(presumA, K)
    maxA = 0
    for ai in range(startA, -1, -1):
        maxA = max(maxA, ai + binMax(presumB, K - presumA[ai]))
    print(maxA)

resolve()