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:
N, M, K = map(int, input().split())
times_a = list(map(int, input().split()))
times_b = list(map(int, input().split()))
read_time = 0
read_time_a = [0]
read_time_b = [0]
for i in range(N):
    read_time_a.append(read_time_a[i]+times_a[i])
for i in range(M):
    read_time_b.append(read_time_b[i]+times_b[i])

ans = 0
for a_count in range(N+1):
    a_read_time = read_time_a[a_count]
    max_b_count = 0
    r = len(read_time_b)
    # 消化時間の配列はソートされている状態だから二分探索
    # 計算量をlog(M)に削減
    while max_b_count + 1 < r:
        c = (max_b_count + r) // 2
        # bがインデックスcで時間がaの時間+bの時間でK以下ならmax_b_countを二分探索の開始位置に更新する。
        if a_read_time + read_time_b[c] <= K:
            max_b_count = c
        # 超えちゃったら二分探索の前方に探索領域の終端を絞る
        else:
            r = c
    # 積読消化時間がKより小さく、a_count + max_b_countの最大数が最大になるものをansに代入し続ける
    if read_time_a[a_count] + read_time_b[max_b_count] <= K:
        ans = max(ans, a_count + max_b_count)
print(ans)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


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

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

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

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# import numpy as np
# import math
# import copy
# from collections import deque
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(10000)


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

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

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

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

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

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

    res = 0

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

    print(res)



main()

Submission status: Accepted

Solution code:
import sys
import itertools as it
input = sys.stdin.readline

N, M, K = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
AS = list(it.accumulate(A))
BS = list(it.accumulate(B))
AS.insert(0,0)
BS.insert(0,0)
max, j = 0, M
for i in range(N+1):
    if AS[i] > K:
        break
    while i + j > max:
        if AS[i] + BS[j] <= K:
            max = i + j
            break
        j -= 1

print(max)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

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

sa = sum(A)
sb = sum(B)
A.reverse()
C = A + B
# print(C)
ans = 0
left_wa = 0
right_wa = 0


ruiseki = C.copy()
ruiseki = [0] + ruiseki
for i in range(len(ruiseki)-1):
    ruiseki[i+1] = ruiseki[i] + ruiseki[i+1]
# print(ruiseki)
left = 0
# for right in range(1, len(C)+1):
#     ruiseki[left:right]
idx = 0
wa = 0
ans = 0
# print(len(ruiseki))
for right in range(len(A), len(ruiseki)):
    # idx += 1
    # print("right:", right)
    wa = ruiseki[right] - ruiseki[left]
    # if
    while wa > W:
        left += 1
        wa = ruiseki[right] - ruiseki[left]
    if left <= len(A):
        tmp = right - left
        # print(tmp)
        ans = max(tmp, ans)
    else:
        break
    # print("left, right, :", left, right)

print(ans)

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

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

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

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

Submission status: Accepted

Solution code:
N, M, K = map(int, input().split())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
timeA, timeB = 0, 0
numA, numB = 0, 0
for i in range(N):
    if timeA + A[i] <= K:
        numA += 1
        timeA += A[i]
    else:
        break
for i in range(M):
    if timeB + B[i] <= K - timeA:
        numB += 1
        timeB += B[i]
    else:
        break
ans = numA + numB
t = numA
for i in reversed(range(numA)):
    t -= 1
    timeA -= A[i]
    while numB < M:
        if timeB + B[numB] <= K - timeA:
            timeB += B[numB]
            numB += 1
        else:
            break
    ans = max(ans, t + numB)
print(ans)

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_sum = [0]
B_sum = [0]

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

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

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

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

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

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

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

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

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

    ans = 0
    j = M

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N,M,K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
c=0
k=M
import numpy as np
A=np.array(A).cumsum()
B=np.array(B).cumsum()
for i in range(N):
  d=K-A[i]
  if d<0:
    for j in reversed(range(M)):
      if K-B[j]>=0:
        if c<j+1:
          c=j+1
        break
    break
  else:
    for j in reversed(range(k)):
      if d>=B[j]:
        if c<i+j+2:
          c=i+j+2
        k=j+1
        break
    if d<B[0]:
      if c<i+1:
        c=i+1
      k=0
for j in reversed(range(M)):
  if K-B[j]>=0:
    if c<j+1:
      c=j+1
print(c)

Submission status: Accepted

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

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

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

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


main()

Submission status: Accepted

Solution code:
import sys
from itertools import accumulate


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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

print(ans)

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()))
ans = 0
j = m
# aa[i], bb[i]は、i冊目まで読むのにかかる時間
aa, bb = [0], [0]

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

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

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**8)
def line_to_int(): return int(sys.stdin.readline())
def line_to_each_int(): return map(int, sys.stdin.readline().split())
def line_to_list(): return list(map(int, sys.stdin.readline().split()))
def line_to_list_in_iteration(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
# def dp(init, i, j): return [[init]*i for i2 in range(j)]
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from collections import Counter # a = Counter(A).most_common()
from itertools import accumulate #A = [0]+list(accumulate(A))
import bisect #bisect.bisect_left(B, a), bisect.bisect_right(B,a)

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

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

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

Submission status: Accepted

Solution code:
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = [0]
d = [0]
for i in range(n):
    c.append(a[i]+c[i])
for i in range(m):
    d.append(b[i]+d[i])
e = 0
for i in range(n+1):
    if c[i] > k:
        break;
    while c[i]+d[m] > k:
        m -= 1
    e = max(e, i+m)
print(e)

Submission status: Accepted

Solution code:
import numpy as np
n,m,k=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
ans=0
a=np.cumsum(A)
b=np.cumsum(B)
a=np.insert(a,0,0)
b=np.insert(b,0,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:
import sys
import math 
from collections import deque

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,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, b = [0], [0]
for idx, num in enumerate(A):
    a.append(a[idx] + num)
for idx, num in enumerate(B):
    b.append(b[idx] + num)

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:
# from collections import deque
import numpy as np
def main():
    n, m, k = map(int, input().split())
    A = np.array([0] + list(map(int, input().split())))
    B = np.array([0] + list(map(int, input().split())))
    X = np.cumsum(A)
    Y = np.cumsum(B)
    # print(P,Q)

    cnt = 0
    j = m
    # Aをi個、Bをj個選ぶ
    for i, x in enumerate(X):
        if x > k:
            break

        while x + Y[j] > k:
            j -= 1
        cnt = max(cnt, i + j)

    print(cnt)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

logging.basicConfig(level=logging.INFO)
logging.info('info')


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

    a = [0]
    b = [0]

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

    for i in range(M):
        b.append(b[i] + B_list[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:
from itertools import *
f=lambda:map(int,input().split())
g=lambda:[0]+[*accumulate(f())]
n,m,k=f()
A,B=g(),g()
c,j=0,m
for i,a in enumerate(A):
  while j and a+B[j]>k: j-=1
  if a+B[j]<=k: c=max(c,i+j)
print(c)

Submission status: Accepted

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

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

Submission status: Accepted

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

    ans = 10 ** 10
    t = 0

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

resolve()

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 bisect
import itertools
import collections
import fractions
import heapq
import math
from operator import mul
from functools import reduce
from functools import lru_cache


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    N, M, K = map(int, readline().split())
    A = list(map(int, readline().split()))
    B = list(map(int, readline().split()))


    nowtime = 0

    Asum = [0]
    Bsum = [0]
    tmp = 0
    for a in A:
        tmp += a
        Asum.append(tmp)
    tmp = 0
    for b in B:
        tmp += b
        Bsum.append(tmp)

    maxbooks = 0
    before = M
    bcount = M
    for acount in range(N+1):
        if Asum[acount] > K:
            continue
        while K < Asum[acount] + Bsum[bcount]:
            bcount -= 1
        maxbooks = max(maxbooks, acount+bcount)

    print(maxbooks)

if __name__ == '__main__':
    solve()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import itertools

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

sumA = list(itertools.accumulate(A))
sumB = list(itertools.accumulate(B))
j=M-1
while K<sumB[j] and j>=0:
    j-=1
ans=j+1
for i in range(N):
    if sumA[i]>K:
        break
    while K<sumA[i]+sumB[j] and j>=0:
        j-=1
    ans=max(ans,i+j+2)
print(ans)

Submission status: Accepted

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

def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()
def deb(text): print("-------\n{}\n-------".format(text))

INF = 10**20
def main():
    N,M,K=mi()
    A=[0]+list(mi())
    B=[0]+list(mi())

    v = [0]*(N+1)
    u = [0]*(M+1)

    for i in range(N):
        v[i+1] = A[i+1] + v[i]
        
    for i in range(M):
        u[i+1] = B[i+1] + u[i]

    # print(u,v)
    ans = 0
    for i in range(M+1):
        if K-u[i] < 0: continue
        j = bisect.bisect_left(v,K-u[i])

        if j == 0 and K-u[i] < A[0]: continue

        if j<len(v) and v[j] != K-u[i]:
            j -= 1
        
            
        ans = max(ans,i+j)

    print(ans)



if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#関数リスト
def main():
    import sys
    input = sys.stdin.readline

    def I(): return int(input())
    def MI(): return map(int, input().split())
    def LI(): return list(map(int, input().split()))
    def ruiseki(mydata):
        res = [0]*(len(mydata)+1)
        for i in range(len(mydata)):
            res[i+1] = res[i] + mydata[i]
        return res

    n, k , m = MI()
    a = ruiseki(LI())
    b = ruiseki(LI())

    result = 0
    index = k
    for i, ii in enumerate(a):
        if ii > m:
            break
        while b[index] + ii > m:
            index -= 1
        result = max(result, i+index)
    print(result)
    
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()))

A_accumulate = list(accumulate([0] + A))
B_accumulate = list(accumulate([0] + B))

ans = 0

for i in range(n+1):
    remain = k - A_accumulate[i]

    if remain < 0:
        continue

    j = bisect_right(B_accumulate, remain)

    ans = max(ans, i+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()))
ans = 0
A_cumulative_sum = [0]
B_cumulative_sum = [B[0]]
for i in range(N):
    A_cumulative_sum.append(A[i] + A_cumulative_sum[i])
for i in range(1, M):
    B_cumulative_sum.append(B[i] + B_cumulative_sum[i - 1])

for i in range(N + 1):
    if K < A_cumulative_sum[i]:
        remaining_time = K
        idx = bisect.bisect_right(B_cumulative_sum, remaining_time)
        ans = max(ans, idx)
        break
    else:
        remaining_time = K - A_cumulative_sum[i]
        idx = bisect.bisect_right(B_cumulative_sum, remaining_time)
        ans = max(ans, i + idx)
print(ans)

# 113897

Submission status: Accepted

Solution code:
#関数リスト
import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def ruiseki(mydata):
    res = [0]*(len(mydata)+1)
    for i in range(len(mydata)):
        res[i+1] = res[i] + mydata[i]
    return res

n, k , m = MI()
a = ruiseki(LI())
b = ruiseki(LI())

result = 0
index = k
for i, ii in enumerate(a):
    if ii > m:
        break
    while b[index] + ii > m:
        index -= 1
    result = max(result, i+index)
print(result)

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.reverse()
c = a + b
d = [0]
for i in range(n + m):
    d.append(d[-1] + c[i])
start = 0
end = n
ans = 0
while end <= n + m:
    if start >= n + 1:
        break
    while d[end] - d[start] > k:
        start += 1
    else:
        ans = max(ans, end - start)
        end += 1
print(ans)

Submission status: Accepted

Solution code:
#関数リスト
import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def ruiseki(mydata):
    res = [0]*(len(mydata)+1)
    for i in range(len(mydata)):
        res[i+1] = res[i] + mydata[i]
    return res

n, k , m = MI()
a = tuple(ruiseki(LI()))
b = tuple(ruiseki(LI()))

result = 0
index = k
for i, ii in enumerate(a):
    if ii > m:
        break
    while b[index] + ii > m:
        index -= 1
    result = max(result, i+index)
print(result)

Submission status: Accepted

Solution code:
#関数リスト
import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def ruiseki(mydata):
    res = [0]*(len(mydata)+1)
    for i in range(len(mydata)):
        res[i+1] = res[i] + mydata[i]
    return res

n, k , m = MI()
a = ruiseki(LI())
b = ruiseki(LI())
a = a
b = b

result = 0
index = k
for i, ii in enumerate(a):
    if ii > m:
        break
    while b[index] + ii > m:
        index -= 1
    result = max(result, i+index)
print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import os
import sys
from collections import defaultdict, Counter, deque
from itertools import product, permutations,combinations, accumulate
from operator import itemgetter
from bisect import bisect_left,bisect
from heapq import heappop,heappush,heapify
from math import ceil, floor, sqrt
from copy import deepcopy

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

    a_list = [0]
    b_list = [0]

    ans = 0
    num = 0

    for i in range(n):
        a_list.append(a_list[i] + n_list[i])

    for i in range(m):
        b_list.append(b_list[i] + m_list[i])


    
    for i in range(n+1):
        if k < a_list[i]:
            break
        while(b_list[m] > k - a_list[i]):
            m -= 1
        ans = max(ans, i+m)
    print(ans)
            

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(300000)
from itertools import accumulate

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def MI0(): return map(lambda s: int(s) - 1, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
def LMI0(): return list(map(lambda s: int(s) - 1, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')


N, M, K = MI()
A = LMI()
B = LMI()

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

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:
#!/usr/bin/env python3
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 j > 0 and k - A[i] < B[j]:
        j -= 1
    ans = max(ans, i + j)

print(ans)

Submission status: Accepted

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

INF = 10**11


def main(input, print):
    n, m, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    at = [0] * (n+1)
    bt = [0] * (m+1)
    for i in range(1, n+1):
        at[i] = at[i-1]+a[i-1]
    for i in range(1, m+1):
        bt[i] = bt[i-1]+b[i-1]

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

    print(ans)


if __name__ == '__main__':
    main(sys.stdin.readline, print)

Submission status: Accepted

Solution code:
from sys import exit
import copy
# import numpy as np
from collections import deque

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

cnt = 0


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


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

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

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


print(cnt)

Submission status: Accepted

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

seq_A = []
seq_B = []

summation = 0
seq_A.append(0)
seq_B.append(0)

for i in range(0, N):
    summation += A[i]
    seq_A.append(summation)
summation = 0
for i in range(0, M):
    summation += B[i]
    seq_B.append(summation)

#print(seq_A)
#print(seq_B)

ans = 0
best = len(seq_B)-1
for i in range(len(seq_A)):
    for j in range(best, -1, -1):
        #print(j)
        if seq_A[i] + seq_B[j] <= K:
            #print(i, j)
            ans = max(ans, i+j)
            best = j
            break

print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from collections import Counter # a = Counter(A).most_common()
from itertools import accumulate #list(accumulate(A))

N, M, lim = mi()
A = li()
B = li()

A = [0] + list(accumulate(A))
B = [0] + list(accumulate(B))
ans = 0
'''
for i in range(N+1):
    if lim < A[i]:
        continue
    #right = 0
    cnt = 0
    wa = 0

    while right < M and wa + B[right] <= lim - A[i]:
        wa += B[right]
        right += 1

    cnt = right - left

    if cnt > 0:
        ans = max(ans, cnt+i)

print(ans)
'''
right = M

for i in range(N+1):
    if A[i] > lim:
        continue

    while True:
        if right < 0 or A[i] + B[right] <= lim:
            break
        right -= 1
    
    #print(i, right)
    ans = max(ans, i + right)
    
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 = [0] + list(accumulate(map(int, readline().split())))
    b = list(accumulate(map(int, readline().split())))
    ans = 0
    for i, aa in enumerate(a):
        if k < aa:
            break
        ans = max(ans, bisect_right(b, k - aa) + i)
    print(ans)


if __name__ == '__main__':
    main()