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:
#!/usr/bin/env python3
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


def sum_of_arithmetic_progression(s, d, n):
    return n * (2 * s + (n - 1) * d) // 2


def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


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


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

    tmp_time = 0
    last_i = -1
    for i in range(N):
        if tmp_time + A[i] <= K:
            tmp_time += A[i]
            last_i = i
        else:
            break

    last_j = -1
    for j in range(M):
        if tmp_time + B[j] <= K:
            tmp_time += B[j]
            last_j = j
        else:
            break

    ans = last_i + last_j + 2
    now = ans
    while last_i >= 0:
        tmp_time -= A[last_i]
        now -= 1
        last_i -= 1

        while last_j + 1 < M and tmp_time + B[last_j + 1] <= K:
            tmp_time += B[last_j + 1]
            last_j += 1
            now += 1

        ans = max(ans, now)

    print(ans)


def main():
    solve()


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from itertools import accumulate
from bisect import bisect_left,bisect_right

def main():
    n,m,k=map(int,input().split())
    a=[0]+list(accumulate(map(int,input().split())))
    b=[0]+list(accumulate(map(int,input().split())))

    result = 0
    for i in range(n+1):
        tmp = k - a[i]
        if tmp < 0:
            continue
        tmpNum = i + bisect_right(b, tmp) - 1
        if tmpNum > result:
            result = tmpNum

    print(result)       


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()))
tim, ans = sum(B), 0
r = M - 1

#Aを1冊も読まないとき
while r > -1 and tim > K:
    tim -= B[r]
    r -= 1
ans = r + 1

#Aを1冊以上読むとき
for l in range(N):
    tim += A[l]
    while r > -1 and tim > K:
        tim -= B[r]
        r -= 1
    if tim > K:
        break
    ans = max(ans, l + r + 2)
print(ans)

Submission status: Accepted

Solution code:
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]

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-


def main():
    import sys
    input = sys.stdin.readline

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

    # KeyInsight:
    # ◯: 配列の要素の合計を前計算しておく
    # △: 尺取り法に気がつけなかった。
    # △: 解説動画を見て自分で実装したら、サンプルのインデックスエラーが消えずに詰んだ。
    # See:
    # https://www.youtube.com/watch?v=v8ppNGf49Nk&feature=youtu.be
    time = 0

    for bi in b:
        time += bi

    j = m
    ans = 0

    for i in range(n + 1):
        # bの要素を選択できる、かつ、kより所要時間が長いときに、本を減らす
        while (j > 0 and time > k):
            j -= 1
            time -= b[j]

        # 全てのbの要素を除外したケースへの対応
        if time > k:
            break

        # aとbの要素数と、これまでの最大値とを比較
        ans = max(ans, i + j)

        # 実装上の工夫:
        # 配列参照外を防ぐ
        if (i == n):
            break

        # 実装上の工夫:
        # aの要素を次のループの前に計算しておく
        time += a[i]

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

ruiseki_A = [0]
ruiseki_B = [0]

for i in range(len(A)):
    ruiseki_A.append(ruiseki_A[i]+A[i])
for i in range(len(B)):
    ruiseki_B.append(ruiseki_B[i]+B[i])
#print(ruiseki_A)

ans = 0
for i in range(len(ruiseki_A)):
    tmp = K - ruiseki_A[i]
    if tmp < 0:
        continue
    r = len(ruiseki_B)
    l = 0
    while r-l > 1:
        mid = (r + l) // 2
        if ruiseki_B[mid] <= tmp:
            l = mid
        else:
            r = mid
    ans = max(ans, i+l)

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 = [0 for i in range(n+1)]
b = [0 for j in range(m+1)]
for i in range(n):
    a[i+1] = a[i]+A[i]
for j in range(m):
    b[j+1] = b[j]+B[j]

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

print(res)

Submission status: Accepted

Solution code:
import numpy as np
n,m,k = map(int,input().split())
a_ls = list(map(int, input().split()))
b_ls = list(map(int, input().split()))
a_csum = np.cumsum(a_ls)
b_csum = np.cumsum(b_ls)
def isOK(index, key, l):
    if l[index] > key:
        return True
    else:
        return False
def binary_search(l, key):
    '''
    keyより大きい値を持つ最小のインデックスを返す
    '''
    left = -1
    right = len(l)

    while (right - left) > 1:
        mid = left + (right - left) // 2
        # 直感的に記憶　
            # 「含まれている」んだったら「含むを許容（等号付き）する右側」はmidまで動かして問題ない
                # 新しいrightの位置がkeyの位置だとしても、「含む」は許容されてる
            # 「含まれていない」んだったら「含まない(<)左の壁」をそこまで動かしてもkeyを通り過ぎることはない
        if isOK(mid, key, l):
            right = mid
        else:
            left = mid
    
    # whileを抜ける時、rightとleftは一個差で、かつkeyがrightの位置にある
    return right
ans = 0
for i in range(n):
    rest = k - a_csum[i]
    if rest < 0:
        break
    ind = binary_search(b_csum, rest)
    canread_b = max(0,ind)
    canread = i+1 + canread_b
    ans = max(canread, ans)
rest = k
ind = binary_search(b_csum, rest)
canread_b = max(0,ind)
ans = max(canread_b, ans)

print(ans)

Submission status: Accepted

Solution code:
import math
import sys
import collections
import bisect
readline = sys.stdin.readline


def main():
    n, m, k = map(int, readline().rstrip().split())
    A = list(map(int, readline().rstrip().split()))
    B = list(map(int, readline().rstrip().split()))

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

    ans = 0
    j = m

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

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

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

ans, j = 0, m
for i in range(n + 1):
    if ai[i] > k:
        break
    while ai[i] + bi[j] > k:
        j -= 1
    ans = max(ans, i + j)
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()))
ruia=[0]
ruib=[0]
mx=0
for (i,j) in zip(a,range(n)):
  ruia.append(ruia[j]+i)

for (i,j) in zip(b,range(m)):
  ruib.append(ruib[j]+i)
ans=0
for i in range(n+1):
  if ruia[i] > k:
    break
  while ruib[m] > k - ruia[i]:
    m -= 1
  ans = max(ans, i + m)
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, 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)

Submission status: Accepted

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

sum_A = [0 for _ in range(n+1)] # sum_A[i]:=上からi冊の本を読むときにかかる時間
for i in range(1, n+1):
    sum_A[i] = sum_A[i-1] + A[i-1]
sum_B = [0 for _ in range(m+1)] # sum_B[i]:=上からj冊の本を読むときにかかる時間
for j in range(1, m+1):
    sum_B[j] = sum_B[j-1] + B[j-1]

ans = 0
i = 0
j = m
while i <= n and sum_A[i] <= k:
    while sum_A[i] + sum_B[j] > k:
        j -= 1
    ans = max(ans, i + j)
    i += 1
print(ans)

Submission status: Accepted

Solution code:
#C
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
A = [0]
B = [0]
#読書時間A,B
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
#Aを増やしたときA+Bがkより小さくなるようなi,jを決めて最大値を決めてる
for i in range(n + 1):
    if A[i] > k:
        break
    #K<=A[i]+B[j]となるまでjを引く>>k-A<=B
    while A[i] + B[j] > k:
        j -= 1
    ans = max(ans, i + j)
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()))
time = 0
cnt = 0
C = []
SA =[0]
SB =[0]
for a in A:
    SA.append(SA[-1] + a)
for b in B:
    SB.append(SB[-1] + b)
for x in range(N+1):
    L = K - SA[x]
    if L < 0:
        break
    y_max = bisect.bisect_right(SB, L) - 1

    C.append(y_max + x)
print(max(C))

Submission status: Accepted

Solution code:
n, m, k =map(int, input().split())
a=list(map(int, input().split()))
b=list(map(int, input().split()))
ta=sum(a)
a.append(0)
tb=0
ans=0
j=0
for i in range(n+1):
    ta -= a[n-i]
    if ta>k:
        continue
    while tb + ta<=k:

        if j ==m:
            ans=max(ans,n-i+j)
            break
        ans=max(ans,n-i+j)
        tb += b[j]
        j +=1

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

max_books = 0

sum_a = []
sum_b = []

total = 0
for i in range(n):
    total += a[i]
    sum_a.append(total)

total = 0
for j in range(m):
    total += b[j]
    sum_b.append(total)

sum_a.insert(0, 0)
sum_b.insert(0, 0)

for i in range(n+1):
    r = k - sum_a[i]
    if r == 0:
        books = i
    elif r < 0:
        books = 0
    else:
        book_b = bisect.bisect_right(sum_b, r)
        books = i + book_b - 1
    if books >= max_books:
        max_books = books

print(max_books)

Submission status: Accepted

Solution code:
import bisect
from itertools import accumulate

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

A_cumsum = list(accumulate(A))
B_cumsum = list(accumulate(B))

max_books = 0
for a, A_sum in enumerate([0] + A_cumsum):
    if A_sum > K:
        break
    else:
        b = bisect.bisect(B_cumsum, K - A_sum)
        max_books = max(a + b, max_books)

print(max_books)

Submission status: Accepted

Solution code:
import sys

N, M, K = [int(s) for s in sys.stdin.readline().split()]
n = 0
A = [0]
for s in sys.stdin.readline().split():
    n = n + int(s)
    A.append(n)

n = 0
B = [0]
for s in sys.stdin.readline().split():
    n = n + int(s)
    B.append(n)

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

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 = [0]
sumB = [0]
tmp = 0
answer = 0
st = 0

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

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

for i in range(N, -1, -1):
    booktmp = 0

    for j in range(st, M + 1):

        if sumA[i] + sumB[j] <= K:
            booktmp = i + j
        else:
            st = j
            break
    answer = max(answer, booktmp)
    if j == M:
        break


print(answer)

Submission status: Accepted

Solution code:

def main():
        
    N_satu, M_satu, K_fun = map(int, input().split())

    A_fun = list(map(int, input().split()))
    B_fun = list(map(int, input().split()))

    SumOf_A = [0]
    SumOf_B = [0]

    for i in range(N_satu):
        next_val = SumOf_A[i] + A_fun[i]
        SumOf_A.append(next_val)

    for i in range(M_satu):
        next_val = SumOf_B[i] + B_fun[i]
        SumOf_B.append(next_val)

    index_b = M_satu
    max_cnt = 0

    for i in range(N_satu + 1):
        if (SumOf_A[i] > K_fun):
            break
        
        while (SumOf_A[i] + SumOf_B[index_b] > K_fun):
            index_b -= 1
        
        max_cnt = max(max_cnt, i + index_b)
        

    print(max_cnt)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import time
import math
import itertools as it
def inpl():
    return list(map(int, input().split()))
st = time.perf_counter()
# ------------------------------

N, M, K = inpl()
A = inpl()
B = inpl()
ib = M
a = 0
b = sum(B)
ans = 0
for i in range(N+1):
    if i != 0:
        a += A[i-1]
    if a > K:
        break
    while a+b > K:
        if ib > 0:
            ib -= 1
            b -= B[ib]
        else:
            break
    ans = max(ans, i+ib)
print(ans)

# ------------------------------
ed = time.perf_counter()
print('time:', ed-st, file=sys.stderr)

Submission status: Accepted

Solution code:
from bisect import bisect

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

def make_cum(ary):
    cum = [0]
    for v in ary:
        cum.append(cum[-1] + v)
    return cum


def solve():
    ans = 0
    cuma = make_cum(A)
    cumb = make_cum(B)
    for ia in range(N+1):
        if cuma[ia] > K:
            break
        ib = bisect(cumb, K-cuma[ia]) - 1
        ans = max(ans, ia + ib)
    return ans
        

if __name__ == "__main__":
    print(solve())

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

Submission status: Accepted

Solution code:
import numpy as np
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()))
a = np.cumsum(a)
b = np.cumsum(b)

ans = 0
for a_num, time in enumerate(a):
    if time > k:
        continue
    b_num = bisect_right(b, k - time)
    num = a_num + b_num - 1
    if num > ans:
        ans = num
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

N, M, K = map(int, input().split())
result = 0 # max
a_time = list(map(int, input().split()))
b_time = list(map(int, input().split()))

sum = 0

a_num = 0
for i in range(N):
    sum += a_time[i]
    a_num += 1
    if sum > K:
        sum -= a_time[i]
        a_num -= 1
        break
i = a_num
j = 0
while i >= 0:
    while sum < K and j < M:
        sum += b_time[j]
        j += 1
    if sum > K:
        j -= 1
        sum -= b_time[j]
    if result < i + j:
        result = i + j
    i -= 1
    sum -= a_time[i]

print(result)

Submission status: Accepted

Solution code:
from itertools import accumulate

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

ans = 0
p = m
for i in range(n + 1):
    if sa[i] > k:
        break
    for j in range(p, -1, -1):
        if sa[i] + sb[j] <= k:
            p = j
            ans = max(ans, i + j)
            break
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()))

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

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

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

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


print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
import sys

n,m,k = [int(x) for x in stdin.readline().rstrip().split()]
A = [int(x) for x in stdin.readline().rstrip().split()]
B = [int(x) for x in stdin.readline().rstrip().split()]

cc = []
count = 0
sumA = 0

for i in range(n):
    sumA_ = sumA + A[i]
    if sumA_ > k: break

    sumA = sumA_
    count = count + 1

maxA = count

sumB = 0
j = 0

for i in range(maxA+1):
    if i != 0:
        count = count - 1
        sumA = sumA - A[maxA-i]

    while True:
        sumB_ = sumB + B[j]

        if sumA + sumB_ > k:
            cc.append(count)
            break

        sumB = sumB_
        count = count + 1
        j = j + 1

        if j == m:
            cc.append(count)
            break

    if j == m:
        break


if cc == []:
    print (0)
else:
    print (max(cc))

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

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.insert(0,0)
b.insert(0,0)
b_num = sum(b)
b_best = M
j = M
a_num = 0
maxmize = 0
num = 0
for i in range(N+1):
  a_num +=a[i]
  flag = 1
  while flag == 1:
    num = b_num+a_num
    if num<=K:
      maxmize = max(M+i,maxmize)
      break
    else:
      if  not b:
        break
      else:
        j = b.pop()
        M-=1
        b_num = b_num-j
print(maxmize)

Submission status: Accepted

Solution code:
import itertools
import bisect
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=[0]+list(map(int,input().split()))
a=list(itertools.accumulate(a))
b=list(itertools.accumulate(b))
ans=0
for i in range(m+1):
  x=k-b[i]
  y=bisect.bisect_right(a,x)
  z=i+y
  if x<0:
      z=0
  ans=max(ans,z)
print(ans)

Submission status: Accepted

Solution code:
def search(a, s):
    min_i = 0
    max_i = len(a)-1
    i = int(len(a)/2)

    if a[min_i] < s and s < a[max_i]:
        while True:
            if a[i] <= s and s < a[i+1]:
                break
            elif s < a[i]:
                max_i = i
                i = min_i + int((max_i - min_i)/2)
            else:
                min_i = i
                i = min_i + int((max_i - min_i)/2)

        return i
    elif s <= a[min_i]:
        return min_i
    else:
        return max_i

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

A_SUM = [0]
B_SUM = [0]

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

for j in range(M):
    B_SUM.append(B[j] + B_SUM[j])

count = 0
for i in range(N+1):
    if K >= A_SUM[i]:
        a = K - A_SUM[i]
        j = search(B_SUM, a)
        if count < i + j:
            count = i + j

print(count)

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

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, b = [0], [0]
for i in range(n):
    a += [a[i]+A[i]]
for i in range(m):
    b += [b[i]+B[i]]

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

import numpy as np
A0 = [0] + A
B0 = [0] + B
AA = np.cumsum(A0)
BB = np.cumsum(B0)

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

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

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

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

print(ans)

Submission status: Accepted

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

a=[0]
b=[0]

for i in range(n):a.append(a[i]+aa[i])
for i in range(m):b.append(b[i]+bb[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)

Submission status: Accepted

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

l = m

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

print(books)

Submission status: Accepted

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

totalA = [0]
totalB = [0]
sumA = 0
for i in range(N):
    sumA += A[i]
    totalA.append(sumA)
sumB = 0
for i in range(M):
    sumB += B[i]
    totalB.append(sumB)

iB = M
countMax = 0
for iA in range(N+1):
  if totalA[iA]>K:
    break
  else:
    while totalA[iA] + totalB[iB] > K:
      iB -= 1
  countMax = max(countMax,iA+iB)

print(countMax)

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_num, b_num, a_time, b_time = 0, 0, 0, 0

for n,b in enumerate(B):
  b_time += b
  b_num += 1
  if b_time > K:
    b_time -= b
    b_num -= 1
    break
    
ans = b_num

for n,a in enumerate(A):
  a_time += a
  a_num += 1
  time = a_time + b_time
  while time > K and b_num > 0:
    b_time -= B[b_num - 1]
    b_num -= 1
    time = a_time + b_time
  if time > K:
    break
  ans = max(ans, a_num + b_num)

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()))
answer = 0
b_ind = 0

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

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

A = [0] + A
B = [0] + B

na = len(A)
nb = len(B) 

if A[-1] + B[-1] <= K:
    answer = len(A) + len(B) - 2
    print(answer)

else:
    for i in range(na):
        for j in range(nb-b_ind-1, -1, -1):
            if A[i] + B[j] <= K:
                if answer < i+j:
                    answer = i+j
                b_ind = nb-j-1
                break
    print(answer)

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=[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]
idx=m

for i in range(n+1):
    x=k-a[i]
    if x<0:
        break
    while b[idx]>x:
        idx-=1
    ANS.append(i+idx)
print(max(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()))
asum = [0]*(n+1)
for i in range(n):
    asum[i+1] = asum[i] + a[i]
for i in range(1,m):
    b[i] += b[i-1]
ans = 0
from bisect import bisect_right
for i in range(n+1):
    tim = k - asum[i]
    if tim < 0:break
    tmp = bisect_right(b,tim)
    ans = max(ans,i+tmp)
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()))
na = len(A)
nb = len(B)
total_A = [0]
total_B = [0]
for i in range(1,na+1):
  ta = total_A[i-1] + A[i-1]
  total_A.append(ta)
for i in range(1,nb+1):
  tb = total_B[i-1] + B[i-1]
  total_B.append(tb)
ans = 0
b_start = 0
a = 0
total = 0
nna = na + 1
nnb = nb + 1
for i in range(nna):
  a = total_A[i]
  for j in range(b_start,nnb):
    b = total_B[nnb-1-j]
    if a+b <= K:
      b_start = j
      total = i + (nnb-1-j)
      ans = max(ans,total)
      break
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 = [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)

Submission status: Accepted

Solution code:
def main():
    import bisect
    n,m,k = map(int,input().split())
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))
    a_sum,b_sum = [0], [0]
    for i in range(n):
        a_sum.append(a_sum[i]+a[i])
    for i in range(m):
        b_sum.append(b_sum[i]+b[i])
    ans = 0
    for i in range(n+1):
        cst = a_sum[i]
        if cst > k:
            break
        cst = k-cst
        j = bisect.bisect_right(b_sum,cst)
        if i+j-1>ans:
            ans = i+j-1
    print(ans)
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def get_sum_arr(arr):
    sum_arr = [0]
    N = len(arr)
    for i in range(1,N+1):
        sum_arr.append(sum_arr[i-1] + arr[i-1])
    return sum_arr

def get_max_b(left_time,j,B_sum_arr):
    # [0,1,2,3,4,5]
    while B_sum_arr[j] > left_time and j > 0:
        j -= 1
    return j
def get_max_book_num(A,B,K):
    A_sum_arr,B_sum_arr = get_sum_arr(A),get_sum_arr(B)
    count,i,j = 0,0,len(B_sum_arr)-1
    for i in range(0,len(A_sum_arr)):
        if K < A_sum_arr[i]:
            break
        else:
            left_time = K - A_sum_arr[i]
            j = get_max_b(left_time,j,B_sum_arr)
        count = max(count,(i+j))
    return count
if __name__ == "__main__":
    N,M,K = list(map(int,input().split()))
    A = list(map(int,input().split()))
    B = list(map(int,input().split()))
    print(get_max_book_num(A,B,K))

Submission status: Accepted

Solution code:
from itertools import accumulate
from bisect import bisect_right, bisect_left
# Kは読める時間。この時間を最大に使ってたくさん本を読みたい
N, M, K = map(int, input().split())

a_books = list(map(int, input().split()))
b_books = list(map(int, input().split()))
a_books = list(accumulate(a_books))
b_books = list(accumulate(b_books))
a_books.insert(0, 0)
b_books.insert(0, 0)
a_index = bisect_right(a_books, K)

# print(a_books)
# print(b_books)


max_nums = 0
for i in range(a_index):
    a_cost = a_books[i]
    remain_cost = K - a_cost
    if remain_cost < 0:
        continue
    j = bisect_right(b_books, remain_cost)
    # print(i, j)
    max_nums = max(max_nums, i + j - 1)
print(max_nums)

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 = [0]
b = [0]
total = 0
for Ai in A:
    total += Ai
    a.append(total)
total = 0
for Bi in B:
    total += Bi
    b.append(total)

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

print(ans)