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

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

A = np.array(A, dtype=np.int64)
B = np.array(B, dtype=np.int64)

SA = np.cumsum(A)
SB = np.cumsum(B)

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

print(ans)

Submission status: Accepted

Solution code:
import sys

import numba as nb
import numpy as np

input = sys.stdin.readline


@nb.njit("i8(i8,i8,i8,i8[:],i8[:])")
def solve(N, M, K, A, B):
    B_cumsum = B.cumsum()
    ans = 0
    time = 0
    for i in range(N + 1):
        if time > K:
            break
        # j = binary_search(B_cumsum, K - time) + 1
        # binary search
        key = K - time
        ok = -1
        ng = len(B_cumsum)
        while (ng - ok > 1):
            mid = (ok + ng) // 2
            if B_cumsum[mid] <= key:
                ok = mid
            else:
                ng = mid
        j = ok + 1
        ans = max(ans, i + j)
        if i < N:
            time += A[i]
    return ans


def main():
    N, M, K = map(int, input().split())
    A = np.array(input().split(), dtype=np.int64)
    B = np.array(input().split(), dtype=np.int64)
    # A = tuple(map(int, input().split()))
    # B = tuple(map(int, input().split()))

    ans = solve(N, M, K, A, B)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys

import numba as nb
import numpy as np

input = sys.stdin.readline


@nb.njit("i8(i8[:], i8)")
def binary_search(a, key):
    """Meguru type binary search"""
    ok = -1
    ng = len(a)

    while (ng - ok > 1):
        mid = (ok + ng) // 2
        if a[mid] <= key:
            ok = mid
        else:
            ng = mid

    return ok


@nb.njit("i8(i8,i8,i8,i8[:],i8[:])")
def solve(N, M, K, A, B):
    B_cumsum = B.cumsum()
    ans = 0
    time = 0
    for i in range(N + 1):
        if time > K:
            break
        j = binary_search(B_cumsum, K - time) + 1
        ans = max(ans, i + j)
        if i < N:
            time += A[i]
    return ans


def main():
    N, M, K = map(int, input().split())
    A = np.array(input().split(), dtype=np.int64)
    B = np.array(input().split(), dtype=np.int64)
    # A = tuple(map(int, input().split()))
    # B = tuple(map(int, input().split()))

    ans = solve(N, M, K, A, B)
    print(ans)


if __name__ == "__main__":
    main()

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(N):
	A[i+1] += A[i]
for i in range(M):
	B[i+1] += B[i]

b = M
r = 0
for i,a in enumerate(A):
	if a > K:
		break
	while a+B[b]>K and b>=0:
		b -= 1
	r = max(r, i+b)

print(r)

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 sys

import numba as nb
import numpy as np

input = sys.stdin.readline


@nb.njit("i8(i8[:], i8)")
def binary_search(a, key):
    """Meguru type binary search"""
    ok = -1
    ng = len(a)

    while (ng - ok > 1):
        mid = (ok + ng) // 2
        if a[mid] <= key:
            ok = mid
        else:
            ng = mid

    return ok


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

    B_cumsum = np.cumsum(B)
    ans = 0
    time = 0
    for i in range(N + 1):
        if time > K:
            break
        j = binary_search(B_cumsum, K - time) + 1
        ans = max(ans, i + j)

        if i < N:
            time += A[i]

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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


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

    a_sum = b_sum = 0
    a_i = b_i = 0
    while a_i < len(a_list) and a_sum + a_list[a_i] <= k:
        a_sum += a_list[a_i]
        a_i += 1
    ans = a_i

    while a_i > 0:
        # a_sum -= a_list[a_i - 1]  # wrong!
        while b_i < len(b_list) and a_sum + b_sum + b_list[b_i] <= k:
            b_sum += b_list[b_i]
            b_i += 1
            ans = max(ans, a_i + b_i)
        a_sum -= a_list[a_i - 1]  # correct!
        a_i -= 1

    while b_i < len(b_list) and a_sum + b_sum + b_list[b_i] <= k:
        b_sum += b_list[b_i]
        b_i += 1
        ans = max(ans, a_i + b_i)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

from itertools import accumulate
import bisect

sum_a_list = list(accumulate([0] + a_list))
sum_b_list = list(accumulate([0] + b_list))

ans = 0
b_idx = 0
for a_idx in range(n+1):
    if sum_a_list[a_idx] > k:
        continue

    time_rest = k - sum_a_list[a_idx]
    b_idx = bisect.bisect_right(sum_b_list,time_rest)

    ans = max(ans, a_idx + b_idx - 1)

print(ans)

Submission status: Accepted

Solution code:
import sys
# from collections import Counter, deque
# from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
# from heapq import heapify, heappop, heappush
# from math import floor, ceil,pi,factorial
# from operator import itemgetter
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
inf = 10**17
mod = 10**9 + 7

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

aa = list(accumulate([0]+a))
bb = list(accumulate([0]+b))

ans = 0
for i in range(len(aa)):
    sub = k-aa[i]
    cnt = 0
    if 0 <= sub:
        cnt += i
        t = bisect_right(bb, sub)
        if 0 < t: cnt += t-1
    ans = max(ans, cnt)

print(ans)

Submission status: Accepted

Solution code:
def bisect(x):
    l,r=-1,M+1
    while(r-l>1):
        mid=(l+r)//2
        if cumB[mid]>=x:
            r=mid
        else:
            l=mid
    return l

N,M,K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
cumB=[0]*(M+1)
for i in range(M):
    cumB[i+1]=cumB[i]+B[i]
c=0
ans=0
for i in range(N+1):
    if c>K:
        break
    r=K-c
    b=bisect(r+1)
    ans=max(ans,i+b)
    if i!=N:
        c+=A[i]
print(ans)

Submission status: Accepted

Solution code:
N,M,K = list(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(N):
    sumA[i+1] = sumA[i] + A[i]
for i in range(M):
    sumB[i+1] = sumB[i] + B[i]

ans = 0
import bisect
for i in range(N+1):
    if K - sumA[i] >= 0:
        idx = bisect.bisect_right(sumB,K-sumA[i])
        ans = max(ans,i + idx - 1)
#         print(idx-1)
print(ans)

Submission status: Accepted

Solution code:
import sys
from itertools import accumulate

input = sys.stdin.readline


def binary_search(a, key):
    """Meguru type binary search"""
    ok = -1
    ng = len(a)

    while (ng - ok > 1):
        mid = (ok + ng) // 2
        if a[mid] <= key:
            ok = mid
        else:
            ng = mid

    return ok


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

    B_cumsum = tuple(accumulate(B))
    ans = 0
    time = 0
    for i in range(N + 1):
        if time > K:
            break
        j = binary_search(B_cumsum, K - time) + 1
        if ans < i + j:
            ans = i + j
        if i < N:
            time += A[i]

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
from itertools import accumulate

input = sys.stdin.readline


def binary_search(a, key):
    """Meguru type binary search"""
    ok = -1
    ng = len(a)

    while (ng - ok > 1):
        mid = (ok + ng) // 2
        if a[mid] <= key:
            ok = mid
        else:
            ng = mid

    return ok


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

    B_cumsum = tuple(accumulate(B))
    ans = 0
    time = 0
    for i in range(N + 1):
        if time > K:
            break
        j = binary_search(B_cumsum, K - time) + 1
        ans = max(ans, i + j)

        if i < N:
            time += A[i]

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
from itertools import accumulate

input = sys.stdin.readline


def binary_search(a, key):
    """Meguru type binary search"""
    ok = -1
    ng = len(a)

    while (abs(ok - ng) > 1):
        mid = (ok + ng) // 2
        if a[mid] <= key:
            ok = mid
        else:
            ng = mid

    return ok


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

    B_cumsum = tuple(accumulate(B))
    ans = 0
    time = 0
    for i in range(N + 1):
        if time > K:
            break
        j = binary_search(B_cumsum, K - time) + 1
        ans = max(ans, i + j)

        if i < N:
            time += A[i]

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
from itertools import accumulate

input = sys.stdin.readline


def is_ok(a, key, idx):
    if a[idx] <= key:
        return True
    else:
        return False


def binary_search(a, key):
    """Meguru type binary search"""
    ok = -1
    ng = len(a)

    while (abs(ok - ng) > 1):
        mid = (ok + ng) // 2
        if is_ok(a, key, mid):
            ok = mid
        else:
            ng = mid

    return ok


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

    B_cumsum = tuple(accumulate(B))
    ans = 0
    time = 0
    for i in range(N + 1):
        if time > K:
            break
        j = binary_search(B_cumsum, K - time) + 1
        ans = max(ans, i + j)

        if i < N:
            time += A[i]

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from bisect import bisect_right


def main():
    n, m, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    answer = 0
    is_changed = False
    if a[0] <= k or b[0] <= k:
        answer = 1
    elif a[0] + b[0] <= k:
        answer = 2
    for i in range(1, n):
        a[i] += a[i - 1]
        if k < a[i] and not is_changed:
            answer = i
            is_changed = True
    if a[-1] <= k:
        answer = n
    is_changed = False
    for i in range(1, m):
        b[i] += b[i - 1]
        if k < b[i] and not is_changed:
            answer = max(answer, i)
            is_changed = True
    if b[-1] <= k:
        answer = max(answer, m)
    if a[-1] + b[-1] <= k:
        answer = n + m
    elif all(k < x for x in [a[0], b[0], a[0] + b[0]]):
        answer = 0
    else:
        for i in range(n):
            l = bisect_right(b, k - a[i])
            if l == 0 or (l == m and b[l - 1] + a[i] > k):
                continue
            answer = max(answer, i + 1 + l)
    print(answer)


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()))
sum_a = [0]+list(accumulate(a))
b = list(map(int,input().split()))
sum_b = list(accumulate(b))
ma = 0
#print(sum_a,sum_b)
for i in range(len(sum_a)):
    x = sum_a[i]
    if k<x:
      index = bisect_right(sum_b,k)
      ma = max(index,ma)
    else:
      k -= x
      index = bisect_right(sum_b,k)
      ma = max(i+index,ma)
      k += x
    #  print(x,k,index,ma)
print(ma)

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の累積和
a_sum = [0]
for i in range(n):
    a_sum.append(a_sum[i] + a[i])
# Bの累積和
b_sum = [0]
for i in range(m):
    b_sum.append(b_sum[i] + b[i])
b_sum.append(10**20)
ans = 0
# Aを固定する
for i in range(n+1):
    t = a_sum[i]
    l = 0
    r = len(b_sum)
    # Bのうち何冊目まで読めるか二分探索する
    while l+1<r:
        # c:探索範囲の中央
        c = (l + r) // 2
        # 中央がK以下の場合は、求めたい位置が右にあるので
        # 探索範囲の左端を中央にする
        if t+b_sum[c]<=k:
            l = c
        # 中央がKよりも大きい場合は、求めたい位置が左にあるので
        # 探索範囲の右端を中央にする
        else:
            r = c
    # 合計がKを超えないか確認する
    if a_sum[i]+b_sum[l]<=k:
        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,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 bisect
import itertools
N, M, K = [int(_) for _ in input().split()]
A = list(itertools.accumulate([0] + [int(_) for _ in input().split()]))
B = list(itertools.accumulate([0] + [int(_) for _ in input().split()]))
ans = 0
for i, a in enumerate(A):
    if a > K:
        break
    ans = max(ans, i + bisect.bisect(B, K - a) - 1)
print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate
from bisect import bisect_right
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

def main():
    N,M,K = map(int, readline().split())
    A = [int(i) for i in readline().split()]
    B = [int(i) for i in readline().split()]

    acc_A = [0] + list(accumulate(A))
    acc_B = [0] + list(accumulate(B))

    ans = 0
    b_ind = M
    for i, a in enumerate(acc_A):
        remain = K - a
        if remain < 0:
            break
        while acc_B[b_ind] > remain:
            b_ind -= 1

        tmp = i + b_ind
        ans = max(ans, tmp)
   
    print(ans)
       


if __name__ == "__main__":
    main()

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()))
sa,sb = [0],[0]
ans = 0
for i in range(n):
    sa.append(sa[i]+a[i])
for i in range(m):
    sb.append(sb[i]+b[i])


for i in range(n+1):
    sai = sa[i]
    x = k-sai
    if x>=0:
        ans = max(ans,bisect.bisect_right(sb,x)-1+i)
print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate
from bisect import bisect_right
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

def main():
    N,M,K = map(int, readline().split())
    A = [int(i) for i in readline().split()]
    B = [int(i) for i in readline().split()]

    acc_A = [0] + list(accumulate(A))
    acc_B = [0] + list(accumulate(B))

    ans = 0
    for i, a in enumerate(acc_A):
        tmp = i
        rest = K - a
        if rest < 0:
            ans = max(ans, bisect_right(acc_B, K) - 1)
            break
        b_ind = bisect_right(acc_B, rest) - 1
        tmp += b_ind

        ans = max(ans, tmp)
   
    print(ans)
       


if __name__ == "__main__":
    main()

Submission status: Accepted

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


# 入力を整数に変換して受け取る
def input_int():
    return int(input())


# マイナス1した値を返却
def int1(x):
    return int(x) - 1


# 半角スペース区切り入力をIntに変換してMapで受け取る
def input_to_int_map():
    return map(int, input().split())


# 半角スペース区切り入力をIntに変換して受け取る
def input_to_int_tuple():
    return tuple(map(int, input().split()))


# 半角スペース区切り入力をIntに変換してマイナス1した値を受け取る
def input_to_int_tuple_minus1():
    return tuple(map(int1, input().split()))

import itertools
def main():
    n, m, k = input_to_int_map()
    a_list = input_to_int_tuple()
    b_list = input_to_int_tuple()


    a_sums = [0]
    temp_sums = 0
    for temp in a_list:
        temp_sums += temp
        a_sums.append(temp_sums)
    temp_sums = 0
    b_sums = [0]
    for temp in b_list:
        temp_sums += temp
        b_sums.append(temp_sums)

    ans = 0
    j = m
    for i in range(n + 1):
        now_a_sum = a_sums[i]
        if now_a_sum > k:
            continue

        while b_sums[j] > k - now_a_sum:
            j -= 1

        ans = max(ans, i + j)

    return ans

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

Submission status: Accepted

Solution code:
import numpy as np
from bisect import bisect_right

# from bisect import bisect_left

N, M, K = map(int, input().split())
A = [0] + list(map(int, input().split()))
B = [0] + list(map(int, input().split()))

cum_A = np.cumsum(A)
cum_B = np.cumsum(B)

# print(cum_A, cum_B)

ans = 0

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

    b = K - cum_A[idx_a]
    idx_b = max(0, bisect_right(cum_B, b) - 1)
    if cum_A[idx_a] + cum_B[idx_b] > K:
        idx_b -= 1
    if idx_b < 0:
        break
    ans = max(ans, idx_a + idx_b)
    # print(b, idx_a, idx_b, ans)

# for idx_a in range(N + 1):
#     if A[idx_a] > K:
#         break

#     b = K - cum_A[idx_a]
#     idx_b = min(bisect_left(cum_B, b), M)
#     if cum_A[idx_a] + cum_B[idx_b] > K:
#         idx_b -= 1
#     if idx_b < 0:
#         break
#     ans = max(ans, idx_a + idx_b)
#     # print(b, idx_a, idx_b, ans)

print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_right

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

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

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

ans = 0
for i in range(N+1):
    r = K - Sa[i]
    if r < 0:
        break
    j = bisect_right(Sb, r) - 1
    ans = max(ans, i + j)

print(ans)

Submission status: Accepted

Solution code:
def main():
    from itertools import chain

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

    t = sum(B)
    ans = 0
    j = M
    for i, x in enumerate(chain([0], A)):
        t += x
        while j > 0 and t > K:
            j -= 1
            t -= B[j]
        if t > K: break
        if ans < i + j:
            ans = i + j
    print(ans)


if __name__ == '__main__':
    main()

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

    SUMA = [0]
    for a in A:
        if SUMA[-1] + a > K: break
        SUMA.append(SUMA[-1] + a)
    SUMB = [0]
    for b in B:
        if SUMB[-1] + b > K: break
        SUMB.append(SUMB[-1] + b)

    ans = 0
    j = len(SUMB) - 1
    for i, suma in enumerate(SUMA):
        while suma + SUMB[j] > K:
            j -= 1
        ans = max(ans, i + j)

    print(ans)


if __name__ == '__main__':
    main()

exit()

Submission status: Accepted

Solution code:
import numpy as np

from bisect import bisect_left

N, M, K = map(int, input().split())
A = [0] + list(map(int, input().split()))
B = [0] + list(map(int, input().split()))

cum_A = np.cumsum(A)
cum_B = np.cumsum(B)

# print(cum_A, cum_B)

ans = 0
# idx_b = M

# for idx_a in range(N + 1):


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

    b = K - cum_A[idx_a]
    idx_b = min(bisect_left(cum_B, b), M)
    if cum_A[idx_a] + cum_B[idx_b] > K:
        idx_b -= 1
    if idx_b < 0:
        break
    ans = max(ans, idx_a + idx_b)
    # print(b, idx_a, idx_b, ans)

print(ans)

Submission status: Accepted

Solution code:
import bisect

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

    # cumsum
    A_cum = [0] * (N+1)
    B_cum = [0] * (M)
    ap = 0
    for i, a in enumerate(A_list):
        A_cum[i+1] = ap + a
        ap += a
    bp = 0
    for i, b in enumerate(B_list):
        B_cum[i] = bp + b
        bp += b

    max_cnt = 0
    for i in range(N+1):
        a = A_cum[i]
        if a > K:
            break
        bi = bisect.bisect_right(B_cum, K-a)
        max_cnt = max(max_cnt, i+bi)

    print(max_cnt)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

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

max_bi = 0
max_count = 0
for j in reversed(range(0, max_ai + 1)):
    for k in range(max_bi + 1, M + 1):
        if sum_m + B[k - 1] <= K:
            sum_m += B[k - 1]
            max_bi = k
        else:
            break
    if j + max_bi > max_count:
        max_count = j + max_bi
    
    sum_m -= A[j - 1] if j >= 0 else 0

print(max_count)

Submission status: Accepted

Solution code:
ii = lambda:int(input())
mi = lambda:list(map(int,input().split()))
ix = lambda x:list(input() for _ in range(x))
mix = lambda x:list(mi() for _ in range(x))
iix = lambda x:list(int(input()) for _ in range(x))
##########

def resolve():
    n,m,k = mi()
    a = mi()
    b = mi()
    
    cum_a = create_cum(a)
    cum_b = create_cum(b)
    
    res, j = 0, m
    for i in range(n+1):
        if cum_a[i] > k:
            break
        while cum_a[i] + cum_b[j] > k:
            j -= 1
        res = max(res, i+j)

    print(res)

def create_cum(x):
    res = [0]
    for i in range(len(x)):
        res.append(res[i]+x[i])
    return res

if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
import numpy as np
N, M, K = map(int, input().split())
A = [0]
B = [0]
A += list(map(int, input().split()))
B += list(map(int, input().split()))
A_cumsum = np.cumsum(A, dtype='int64')
B_cumsum = np.cumsum(B, dtype='int64')

def binary_search(k):
    if B_cumsum[-1] <= k:
        return M
    right = M
    left = 0
    while right != left:
        mid = (left + right) // 2
        if B_cumsum[mid] > k:
            right = mid
        else:
            left = mid + 1
    return left - 1

ans = 0
for i in range(N+1):
    count = 0
    if A_cumsum[i] <= K:
        count += i
    else:
        break
    count += binary_search(K - A_cumsum[i])
    if ans < count:
        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()))
aa=[0]
bb=[0]
a_sum=0
b_sum=0

for i in range(n):
    if (a_sum+a[i])<=k:
        a_sum+=a[i]
        aa.append(a_sum)
    else:
        break
for i in range(m):
    if (b_sum+b[i])<=k:
        b_sum+=b[i]
        bb.append(b_sum)
    else:
        break

#print(aa)
#print(bb)
cnt=len(bb)-1
ans=0
for i in range(len(aa)):
    for j in range(cnt,-1,-1):
        if bb[j]<=(k-aa[i]):
            cnt=j
            ans=max(ans,(i+j))
            break
print(ans)

Submission status: Accepted

Solution code:
def main():
    n, m, k = map(int, input().split())
    a = [int(x) for x in input().split()]
    b = [int(x) for x in input().split()]

    a_sum = [0]
    b_sum = [0] 

    max_counter = 0

    j = m

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

    for i in range(n + 1):
        if a_sum[i] > k:
            break

        while k < a_sum[i] + b_sum[j]:
            j -= 1
        
        max_counter = max(max_counter, i + j)

    print(max_counter)

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

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

j = len(b)-1
ans = 0
for i in range(len(a)):
  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:
def main():
    N, M, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    SUMA = [0]
    for a in A:
        if SUMA[-1] + a > K: break
        SUMA.append(SUMA[-1] + a)
    SUMB = [0]
    for b in B:
        if SUMB[-1] + b > K: break
        SUMB.append(SUMB[-1] + b)

    if len(SUMA) == 1:
        ans = len(SUMB) - 1
    elif len(SUMB) == 1:
        ans = len(SUMA) - 1
    else:
        ans = 0
        i = len(SUMA)
        j = 1
        while i:
            i -= 1
            while SUMA[i] + SUMB[j] <= K:
                j += 1
                if j == len(SUMB): break
            j -= 1
            ans = max(ans, i + j)

    print(ans)


if __name__ == '__main__':
    main()

exit()

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]
tmp = 0
for i in range(N):
  tmp += A[i]
  a.append(tmp)
tmp = 0
for i in range(M):
  tmp += B[i]
  b.append(tmp)

total = 0
count = []
best_j = len(b)-1
for i in range(len(a)):
  if a[i] <= K:
    for j in range(best_j, -1, -1):
      total = a[i] + b[j]
      if total <= K:
        count.append(i+j)
        best_j = j
        break
if len(count) == 0:
  print(0)
else:
  print(max(count))

Submission status: Accepted

Solution code:
n,m,k=map(int,input().split())
arr1=list(map(int,input().split()))
arr2=list(map(int,input().split()))
acum1=[0]
#累積和ai,bjを求める
for i in range(n): 
	# acum[-1]で一番最後のリストの値を参照している
    # うしろからみている
 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):
    #各iについて、bj<=K-aiを満たす最大のjを求める
    #bj>K-aiの間、ループ。jを大きい方から順に調べていく
    #限界ギリギリを調べるのだから、後ろから調べた方がよいという感じかな
 if acum1[i]>k:
   break
       #各iについて、bj<=K-aiを満たす最大のjを求める
    #bj>K-aiの間、ループ。jを大きい方から順に調べていく
    #限界ギリギリを調べるのだから、後ろから調べた方がよいという感じかな
 while acum2[j]>k-acum1[i] and j>0: 
   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.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(a[i] + b[j] > K):
    j -= 1
  ans = max(ans, i+j)
print(ans)

Submission status: Accepted

Solution code:
from bisect import*

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

sa=[0]+[0]*N
i=0
while i<N:
    sa[i+1]=sa[i]+A[i]
    i+=1

sb=[0]+[0]*M
j=0
while j<M:
    sb[j+1]=sb[j]+B[j]
    j+=1

i=bisect_right(sa,K)-1
ans=0
while 0<=i:
    j=bisect_right(sb,K-sa[i])-1
    ans=max(ans,i+j)
    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()))

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

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

Submission status: Accepted

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

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

a_cumsum = [0] + list(accumulate(a))
b_cumsum = [0] + list(accumulate(b))
j = m
res = 0
for i in range(n+1):
    if a_cumsum[i] > k:
        break
    while a_cumsum[i] + b_cumsum[j] > k:
        j -= 1
    if (v := i + j) > res:
        res = v
print(res)

Submission status: Accepted

Solution code:
import bisect

def Csum(a):
    b,c=[0],0
    for i in range(len(a)):
        c+=a[i]
        b.append(c)
    return b

n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c,d=Csum(a),Csum(b)
d.pop(0)
ans=0
for i in range(n+1):
    if k-c[i]<0:
        break
    ans=max(ans,i+bisect.bisect_right(d,k-c[i]))
print(ans)

Submission status: Accepted

Solution code:
def main():
    import sys
    sys.setrecursionlimit(10**9)
    input = sys.stdin.readline

    INF = 10**18
    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):
        A.append(a[i] + A[i])
    B = [0]
    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 k < A[i] + B[j]:
            j -= 1
        ans = max(ans, i + j)
    print(ans)

main()