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())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

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

cum_b = [0]
for i in range(m):
    cum_b.append(cum_b[i] + b[i])

ans = 0
val = m
for i in range(n + 1):
    if cum_a[i] > k:
        break

    while cum_b[val] > k - cum_a[i]:
        val -= 1

    ans = max(ans, i + val)

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()))
a.insert(0, 0)
b.insert(0, 0)
ans = 0
num_a = n
num_b = 0
flg = True

total_time = sum(a)
if total_time <= k:
    ans = n

for i in range(n+1):
    for j in range(num_b, m):
        if total_time + b[num_b+1] > k:
            break
        else:
            total_time += b[num_b+1]
            num_b += 1
        if num_a + num_b > ans:
            ans = num_a + num_b
    total_time -= a[num_a]
    num_a -= 1

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()))
cumA, cumB = [0], [0]

for i in range(len(A)):
    cumA.append(cumA[-1]+A[i])
for i in range(len(B)):
    cumB.append(cumB[-1]+B[i])

ans = 0
for i in range(n+1):
    tmp = cumA[i]
    if tmp > k:
        break
    else:
        rest = k-tmp
        ans = max(ans, i+bisect_right(cumB, rest)-1)
print(ans)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline

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

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

for i in range(1, len(B)):
  B[i] += B[i - 1]
  
ans = 0
b_ind = len(B) - 1
for i in range(len(A)):
  if A[i] > K:
    continue
  while b_ind >= 1 and A[i] + B[b_ind] > K:
    b_ind -= 1
    
  if i + b_ind > ans:
    ans = i + b_ind
    
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()))
ea=[0]
for i in range(n):
    ea.append(ea[i]+a[i])
eb=[0]
for i in range(m):
    eb.append(eb[i]+b[i])
if n<m:
    ea,eb=eb,ea
    n,m=m,n
tb=m
p=0
for i in range(n+1):
    while ea[i]+eb[tb]>k:
        if tb==0:
            break
        tb-=1
    if ea[i]+eb[tb]<=k:
        p=max(i+tb,p)
print(p)

Submission status: Accepted

Solution code:
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
ea=[0]
for i in range(n):
    ea.append(ea[i]+a[i])
eb=[0]
for i in range(m):
    eb.append(eb[i]+b[i])
if n>m:
    ea,eb=eb,ea
    n,m=m,n
tb=m
p=0
for i in range(n+1):
    while ea[i]+eb[tb]>k:
        if tb==0:
            break
        tb-=1
    if ea[i]+eb[tb]<=k:
        p=max(i+tb,p)
print(p)

Submission status: Accepted

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

def get_input() -> tuple:
    """
    標準入力を取得する.

    Returns:\n
        tuple: 標準入力
    """
    N, M, K = list(map(int, input().split()))
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    return N, M, K, A, B


def main(N: int, M: int, K: int, A: list, B: list) -> None:
    """
    メイン処理.

    Args:\n
        N (int): 机Aの本の数（1 <= N <= 200000）
        M (int): 机Bの本の数（1 <= M <= 200000）
        K (int): 合計所要時間の上限（1 <= K <= 10^9）
        A (list): 机Aの本を読むための時間
        B (list): 机Bの本を読むための時間
    """
    # 求解処理
    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])

    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)


if __name__ == "__main__":
    # 標準入力を取得
    N, M, K, A, B = get_input()

    # メイン処理
    main(N, M, K, A, B)

Submission status: Accepted

Solution code:
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
ea=[0]
for i in range(n):
    ea.append(ea[i]+a[i])
eb=[0]
for i in range(m):
    eb.append(eb[i]+b[i])
tb=m
p=0
for i in range(n+1):
    while ea[i]+eb[tb]>k:
        if tb==0:
            break
        tb-=1
    if ea[i]+eb[tb]<=k:
        p=max(i+tb,p)
print(p)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline

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

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

for i in range(1, len(B)):
  B[i] += B[i - 1]
  
import bisect
ans = 0
for i in range(len(A)):
  # Aをi冊読んだとき
  rest = K - A[i]
  if rest < 0:
    continue
  # 残りrest分ある
  b_read = bisect.bisect_right(B, rest) - 1
  if i + b_read > ans:
    ans = i + b_read
    
print(ans)

Submission status: Accepted

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

lia = [0]
lib = [0]
j = M
ans = 0

for i in range(N):
    lia.append(lia[i]+A[i])
for i in range(M):
    lib.append(lib[i]+B[i])

for i in range(N+1):
    if lia[i] > K:
        break

    while lib[j] > K-lia[i]:
        j -= 1
    ans = max(ans, i+j)

print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate
from bisect import bisect
n,m,k=map(int,input().split())
A=list(map(int,input().split()))
A=list(accumulate(A,initial=0))
B=list(map(int,input().split()))
B=list(accumulate(B))

ans = 0
for i,a in enumerate(A):
  if a > k: break
  ans = max(ans,i + bisect(B,k-a))
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[0]、1冊読んだ時の時間がa[1]・・となるような配列
    a.append(a[i] + A[i])
for i in range(M):
    b.append(b[i] + B[i])

ans = 0
j = M
# Aの机からi冊(0~N)読んだ時に、Bの机からj冊読めるか
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()))

lia = [0]
lib = [0]
tmp = M
ans = 0

for i in range(N):
    lia.append(lia[i]+A[i])
for i in range(M):
    lib.append(lib[i]+B[i])

for i in range(N+1):
    kari = 0
    t = K - lia[i]
    kari += i

    if t<0:
        break
    elif t==0:
        ans = max(ans, i)
        break
    else:
        pass

    for j in range(tmp,-1,-1):
        if t<lib[j]:
            continue
        else:
            tmp = j
            kari += j
            ans = max(ans, kari)
            break

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from itertools import permutations, accumulate, combinations, combinations_with_replacement
from math import sqrt, ceil, floor, factorial
from bisect import bisect_left, bisect_right, insort_left, insort_right
from copy import deepcopy
from operator import itemgetter
from functools import reduce, lru_cache  # @lru_cache(None)
from fractions import gcd
import sys  # def input(): return sys.stdin.readline().rstrip() # sys.setrecursionlimit(10**6)

n, m, k = (int(x) for x in input().split())
A = list(int(x) for x in input().split())
B = list(int(x) for x in input().split())

accA = list(accumulate(A))
accB = list(accumulate(B))
ans = 0
for i in range(n):
    if accA[i] > k:
        break
    ans = max(ans, i + 1 + bisect_right(accB, k - accA[i]))
ans = max(ans, 0 + bisect_right(accB, k))
print(ans)

Submission status: Accepted

Solution code:
n, m, k = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
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 
for i in range(n + 1):
    if a[i] > k:
        break
    while  a[i] + b[m] > k:
        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()))
'''
with open('./b09.txt') as f:
    N, M, K = map(int, f.readline().split())
    A = list(map(int, f.readline().split()))
    B = list(map(int, f.readline().split()))
'''
A_cnt = 0
B_cnt = 0
ans = 0
nowK = K
for A_idx in range(N):
    if nowK < A[A_idx]:
        break
    ans += 1
    nowK -= A[A_idx]
    A_cnt += 1
for B_idx in range(M):
    if nowK < B[B_idx]:
        break
    ans += 1
    nowK -= B[B_idx]
    B_cnt += 1
tmp_ans = ans
finish = False
for i in range(A_cnt, 0, -1):
    nowK += A[i - 1]
    tmp_ans -= 1
    for j in range(B_cnt, M):
        if finish or nowK < B[j]:
            break
        nowK -= B[j]
        tmp_ans += 1
        B_cnt += 1
    ans = max(tmp_ans, 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, SB = [0], [0]

for i in range(n):
    SA.append(SA[-1] + A[i])
for i in range(m):
    SB.append(SB[-1] + B[i])

res = bisect_right(SA, k) - 1

#print(SA,SB)
for i, sa in enumerate(SA):
    if k - sa > 0:
        tmp = i + bisect_right(SB, k - sa) - 1
        res = max(res, tmp)
    else:
        break
print(res)

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

a = []
a.append(0)
for i in range(n):
    a.append(a[i]+a_list[i])
    
b = []
b.append(0)
for i in range(m):
    b.append(b[i]+b_list[i])
    

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

    sum_A = [0]
    sum_B = [0]

    for i in range(n):
        sum_A.append(sum_A[i] + A[i])

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

    # R = []

    # b = m - 1
    # flag = False
    # while b > -1:
    #     if sum_B[b] <= k:
    #         R.append(b + 1)
    #         flag = True
    #         break
    #     else:
    #         b -= 1

    # if not flag:
    #     R.append(0)

    r = 0
    j = m

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

        while j > -1:
            if sum_B[j] > k - a:
                j -= 1
            else:
                r = max(r, i + j)
                break

    print(r)
        # a = sum_A[i]

        # # b = m - 1 ←これがいらないのが肝
        # flag = False
        # while b > -1:
        #     if sum_B[b] <= k - a:
        #         R.append(i + 1 + b + 1)
        #         flag = True
        #         break
        #     else:
        #         b -= 1

        # if not flag:
        #     if 0 <= k - a:
        #         R.append(i + 1)
        #     else:
        #         R.append(0)

    # print(max(R))


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

    sum_A = [A[0]]
    sum_B = [B[0]]

    for i in range(n - 1):
        sum_A.append(sum_A[i] + A[i + 1])

    for i in range(m - 1):
        sum_B.append(sum_B[i] + B[i + 1])

    R = []

    b = m - 1
    flag = False
    while b > -1:
        if sum_B[b] <= k:
            R.append(b + 1)
            flag = True
            break
        else:
            b -= 1

    if not flag:
        R.append(0)

    for i in range(n):
        a = sum_A[i]

        flag = False
        while b > -1:
            if sum_B[b] <= k - a:
                R.append(i + 1 + b + 1)
                flag = True
                break
            else:
                b -= 1

        if not flag:
            if 0 <= k - a:
                R.append(i + 1)
            else:
                R.append(0)

    print(max(R))
    # while k > 0 and (a < n or b < m):
    #     if a == n:
    #         time = B[b]
    #     elif b == m:
    #         time = A[a]
    #     else:
    #         time = min(A[a], B[b])

    #     if k - time < 0:
    #         break

    #     r += 1
    #     k -= time
    #     print(k)

    #     if a == n:
    #         b += 1
    #     elif b == m:
    #         a += 1
    #     else:
    #         if A[a] >= B[b]:
    #             b += 1
    #         else:
    #             a += 1

    # print(r)


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

acA = [0]
for i in range(n):
  acA.append(acA[i] + A[i])

acB = [0]
for i in range(m):
  acB.append(acB[i] + B[i])

ans = 0
for a in range(n+1):
  if k-acA[a] < 0: break
  b = bisect.bisect(acB, k-acA[a]) - 1
  ans = max(ans, a+b)
    
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

for i in range(n + 1):
    if a[i] > k:
        break
    while  a[i] + b[m] > k:
        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:
import itertools
def main():
    n,m,k=map(int, input().split())
    a=[int(i) for i in input().split()]
    b=[int(i) for i in input().split()]
    a = [0]+list(itertools.accumulate(a))
    b = [0]+list(itertools.accumulate(b))
    res = 0
    j = m
    best=0
    i=0

    while(i<n+1):
        if(a[i]>k):
            break
        while(b[j]+a[i]>k):
            j-=1
        res = max(res,i+j)
        i+=1
    print(res)



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_sum = [0]
for i in range(len(a)):
    a_sum.append(a_sum[len(a_sum)-1]+a[i])

b_sum = [0]
for i in range(len(b)):
    b_sum.append(b_sum[len(b_sum)-1]+b[i])

ans = 0
b_i = len(b_sum)-1

for i in range(len(a_sum)):
    if a_sum[i] > k:
        break
    else:
        if b_i > -1:
            while b_sum[b_i] + a_sum[i] > k and b_i > -1:
                b_i -= 1
            if b_i > -1 and ans < i+b_i:
                ans = i+b_i
        else:
            if a_i > ans:
                ans = a_i

print(ans)

Submission status: Accepted

Solution code:
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
 
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:
import itertools
def main():
    n,m,k=map(int, input().split())
    a=[int(i) for i in input().split()]
    b=[int(i) for i in input().split()]
    a = [0]+list(itertools.accumulate(a))
    b = [0]+list(itertools.accumulate(b))
    res = 0
    j = m
    best=0
    for i in range(n+1):
        if(a[i]>k):
            break
        while(b[j] > k-a[i]):
            j-=1
        res = max(res,i+j)
    print(res)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#create date: 2020-06-27 22:08

import sys
stdin = sys.stdin
from itertools import accumulate
from bisect import bisect_left, bisect_right

def ns(): return stdin.readline().rstrip()
def ni(): return int(ns())
def na(): return list(map(int, stdin.readline().split()))

def main():
    n, m, k = na()
    a = na()
    b = na()
    acum = list(accumulate(a))
    bcum = list(accumulate(b))
    i1, i2 = bisect_right(acum, k), 0
    ans = i1
    ssum = sum(a[:i1])
    #print(ans, i1, i2, ssum)
    while i1 > -1:
        if i2 != m:
            while ssum + b[i2] <= k:
                ssum += b[i2]
                i2 += 1
                if i2 == m:
                    break
        ans = max(ans, i1+i2)
        #print(ans, i1, i2)
        ssum -= a[i1-1]
        i1 -= 1
        
    print(ans)



if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from sys import stdin
import bisect

def main():
    input = stdin.readline
    n, m, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    c = [0] * (n + 1)
    for i in range(n):
        c[i + 1] = c[i] + a[i]
    d = [0] * (m + 1)
    for i in range(m):
        d[i + 1] = d[i] + b[i]
    cnt = 0
    for i in range(n + 1):
        if c[i] > k:
            break
        e = bisect.bisect_right(d, k - c[i])
        if e > m and cnt < i + m:
            cnt = i + m
        if e <= m and cnt < i + e - 1:
            cnt = i + e - 1
    print(cnt)
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]
for i in range(N):
    a.append(a[i] + A[i])
    
for i in range(M):
    b.append(b[i] + B[i])
totaltime = 0
count,j = 0, M
 
for i in range(N+1):
    if a[i] > K:
        break
    while a[i] + b[j] > K:
        j -= 1
    count = max(count,i + j)
 
print(count)

Submission status: Accepted

Solution code:
def resolve():
    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)
    
resolve()

Submission status: Accepted

Solution code:
from collections import deque
import numpy as np
import bisect

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

print(ans)

Submission status: Accepted

Solution code:
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
asum = 0
maxtemp = 0
bj = m
bsum = sum(b)
for i in range(n):
    if(asum >k):
        temp = i-1
        if(maxtemp < temp):
            maxtemp = temp
        break
    for j in range(bj):
        if(asum + bsum <= k):
            bj = bj - j
            temp = i + bj
            break
        bsum -= b[bj-1 - j]
    else:
        bj = 0
        temp = i
    if(maxtemp < temp):
        maxtemp = temp
    asum +=a[i]
for j in range(bj):
    if(asum + bsum <= k):
        bj = bj - j
        temp = n + bj
        break
    bsum -= b[bj-1 - j]
else:
    if(asum <= k):
        temp = n
if(maxtemp < temp):
    maxtemp = temp
print(maxtemp)

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()))
INF = 10 ** 18

acc_a = [0] + list(accumulate(a))
acc_b = [0] + list(accumulate(b))
j = m
ans = 0
for i, e in enumerate(acc_a):
    while j >= 0 and acc_b[j] > k - acc_a[i]:
        j -= 1

    if j == -1:
        break

    ans = max(ans, i + j)

print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate
from bisect import bisect_right 

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

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

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

Submission status: Accepted

Solution code:
import sys
import numpy as np

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

N,M,K = map(int, readline().split())
AB = np.array(read().split(), np.int64)

A = AB[:N]
B = AB[N:]

Acum = np.zeros(N+1, np.int64)
Acum[1:] = np.cumsum(A)
Bcum = np.zeros(M+1, np.int64)
Bcum[1:] = np.cumsum(B)

Acum = Acum[Acum <= K]
Bcum = Bcum[Bcum <= K]

x = np.searchsorted(Acum, K - Bcum, side='right') - 1
x += np.arange(len(Bcum))
print(x.max())

Submission status: Accepted

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

from itertools import accumulate
AS = [0] + list(accumulate(A))
BS = [0] + list(accumulate(B))

from bisect import bisect
ans = 0
for i in range(n+1):
    if AS[i] > k:
        break
    # kb = bisect(BS, k - AS[i])-1
    # ans = max(ans, kb+i)
    ans = max(ans, bisect(BS, k - AS[i])-1+i)

print(ans)

Submission status: Accepted

Solution code:
N,M,K=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
A=[0]
B=[0]
ans=0
j=M
for i in range(N):
  A.append(A[i]+a[i])
for i in range(M):
  B.append(B[i]+b[i])
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 numpy as np
import sys
input = sys.stdin.buffer.readline

N, M, K = map(int, input().split())
cumA = np.zeros(N+1, dtype=np.int64)
cumA[1:] = np.array(input().split(), dtype=np.int64).cumsum()
cumB = np.zeros(M+1, dtype=np.int64)
cumB[1:] = np.array(input().split(), dtype=np.int64).cumsum()
# print(cumA)
# print(cumB)

cumA = cumA[cumA <= K]
cumB = cumB[cumB <= K]
ans = np.searchsorted(cumA, K - cumB, side='right') - 1
ans += np.arange(len(cumB))
print(ans.max())

Submission status: Accepted

Solution code:
import numpy as np
import sys
input = sys.stdin.buffer.readline

N, M, K = map(int, input().split())
cumA = np.zeros(N+1, dtype=np.int64)
cumA[1:] = np.array(input().split(), dtype=np.int64).cumsum()
cumB = np.zeros(M+1, dtype=np.int64)
cumB[1:] = np.array(input().split(), dtype=np.int64).cumsum()
# print(cumA)
# print(cumB)

ans = 0
for i in range(N, -1, -1):
    if cumA[i] > K: continue
    j = np.searchsorted(cumB, K-cumA[i], side='right')
    ans = max(ans, i+j-1)
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.buffer.readline

import bisect
N, M, K = map(int, input().split())
A = list(map(int, (input().split())))
B = list(map(int, (input().split())))
cumA, cumB = [0], [0]
for a in A:
    cumA.append(cumA[-1]+a)
for b in B:
    cumB.append(cumB[-1]+b)
ans = 0
for i in range(N, -1, -1):
    if cumA[i] > K: continue
    j = bisect.bisect_right(cumB, K-cumA[i])
    ans = max(ans, i+j-1)
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.buffer.readline

N, M, K = map(int, input().split())
A = list(map(int, (input().split())))
B = list(map(int, (input().split())))
t = sum(B)
# 尺取法
j = M   # bはmから減らしていく
ans = 0
for i in range(N+1):    # aは0からnまで増やしていく
    if i: t += A[i-1]   # i個までの累計になるように足す 0個は0
    while (j>0 and t>K):    # t<=k または j==0 で抜ける
        j -= 1
        t -= B[j]
    if t>K: break   # bが0番まで行っても　t>k の場合はだめ
    ans = max(ans, i+j)
print(ans)

Submission status: Accepted

Solution code:
# でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
from itertools import accumulate
import sys
def main(N, M, K, A, B):
    *AA, = accumulate(A, initial=0)
    *BB, = accumulate(B, initial=0)

    def f(c):
        res = 10 ** 18
        for a in range(max(0, c - M), min(c, N) + 1):
            res = min(res, AA[a] + BB[c - a])
        return res

    l = 0
    r = N + M + 1
    while r - l > 1:
        m = (l + r) // 2
        if f(m) > K:
            r = m
        else:
            l = m
    print(l)

if __name__ == '__main__':
    input = sys.stdin.readline
    N, M, K = map(int, input().split())
    *A, = map(int, input().split())
    *B, = map(int, input().split())
    main(N, M, K, A, B)

Submission status: Accepted

Solution code:
import sys

def resolve():
    lines = sys.stdin.readlines()

    n, m, k = tuple(map(int, lines[0].strip().split(' ')))
    A = list(map(int, lines[1].strip().split(' ')))
    B = list(map(int, lines[2].strip().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])

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

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**8)
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)))


INF = float("inf")
MOD = 10**9 + 7


def main():
    from bisect import bisect_right
    from itertools import accumulate
    N, M, K = MAP()
    A = LIST()
    B = LIST()

    A = [0] + list(accumulate(A)) + [INF]
    B = [0] + list(accumulate(B)) + [INF]

    ans = 0
    for i in range(N + 1):
        j = bisect_right(B, K - A[i]) - 1
        if j < 0:
            continue
        ans = max(i + j, ans)
    print(ans)
    return


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

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]

ans = 0
for i in range(len(a)):
    rest = k - a[i]
    if rest < 0:
        break
    left, right = -1, len(b)
    while right - left > 1:
        mid = (left + right) // 2
        if rest - b[mid] >= 0:
            left = mid
        else:
            right = mid
    # print(f"{rest}を越えない最大のBiは{b[left]}")
    ans = max(ans, i + left)
print(ans)

Submission status: Accepted

Solution code:
from numpy import*
n,m,k,*a=int_(open(0).read().split())
c=cumsum
b=c([0]+a[:n])
print(max(range(len(b:=b[b<=k]))+searchsorted(c(a[n:]),k-b+1)))

Submission status: Accepted

Solution code:
def main():
    n, m, k = [int(e) for e in input().split()]
    A = [int(e) for e in input().split()]
    B = [int(e) for e in input().split()]

    AS = [0]
    for i in range(n):
        AS.append(AS[i] + A[i])

    BS = [0]
    for i in range(m):
        BS.append(BS[i] + B[i])

    ans = 0
    bi = 0
    for ai in range(n, -1, -1):
        if bi == m + 1:
            break

        if AS[ai] > k:
            continue

        while bi < m + 1 and AS[ai] + BS[bi] <= k:
            bi += 1

        ans = max(ai + bi - 1, ans)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from numpy import*
n,m,k,*a=int_(open(0).read().split())
c=cumsum
b=c([0]+a[:n])
print(max(range(len(b:=b[b<=k]))+searchsorted(c(a[n:]),k-b,'r')))

Submission status: Accepted

Solution code:
from bisect import*;from itertools import*;n,_,k,*x=map(int,open(0).read().split());c=accumulate;*b,=c(x[n:]);print(max(i+bisect(b,k-v)for i,v in enumerate(c([0]+x[:n]))if v<=k))

Submission status: Accepted

Solution code:
from bisect import*
from itertools import*
n,_,k,*x=map(int,open(0).read().split());c=accumulate;*b,=c(x[n:]);print(max(i+bisect(b,k-v)for i,v in enumerate(c([0]+x[:n]))if v<=k))

Submission status: Accepted

Solution code:
from numpy import*
n,m,k,*a=int_(open(0).read().split())
c=cumsum
b=c(a[n:])
a=c([0]+a[:n])
print(max(range(len(a:=a[a<=k]))+searchsorted(b,k-a,'r')))