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:
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)
sb=[0]*(m+1)
for i in range(1,n+1):
    sa[i]=sa[i-1]+a[i-1]
for j in range(1,m+1):
    sb[j]=sb[j-1]+b[j-1]
ans=0
for x in range(n+1):
    if k-sa[x]>=0:
        k2=k-sa[x]
        l=bisect_right(sb,k2)-1
        if x+l>ans:
            ans=x+l
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
from itertools import accumulate
import numba
import numpy as np


def debug(*x):
    print(*x)


# @numba.njit
# def solve(N, M, K, sA, sB):
#     "void()"

#     last_can_read = 0
#     for i in range(N + 1):
#         for j in range(M + 1):
#             if sA[i] + sB[j] <= K:
#                 if last_can_read < i + j:
#                     last_can_read = i + j
#             else:
#                 break

#     print(last_can_read)


@numba.njit
def solve(N, M, K, sA, sB):
    "void()"

    last_can_read = 0
    last_max_j = M
    for i in range(N + 1):
        j = last_max_j
        while j >= 0:
            if sA[i] + sB[j] <= K:
                if last_can_read < i + j:
                    last_can_read = i + j
                last_max_j = j
                break
            j -= 1

    print(last_can_read)


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

    AS = list(map(int, input().split()))
    BS = list(map(int, input().split()))

    sA = np.array([0] + list(accumulate(AS)))
    sB = np.array([0] + list(accumulate(BS)))
    # debug("s:sA,sB", sA, sB)
    solve(N, M, K, sA, sB)


T1 = '''
3 4 240
60 90 120
80 150 80 150
'''

T2 = '''
3 4 730
60 90 120
80 150 80 150
'''


def _test():
    """
    >>> as_input(T1)
    >>> main()
    3
    >>> as_input(T2)
    >>> main()

    """
    import doctest
    doctest.testmod()


def as_input(s):
    "use in test, use given string as input file"
    import io
    global read, input
    f = io.StringIO(s.strip())
    input = f.readline
    read = f.read


USE_NUMBA = False
if (USE_NUMBA and sys.argv[-1] == 'ONLINE_JUDGE') or sys.argv[-1] == '-c':
    print("compiling")
    from numba.pycc import CC
    cc = CC('my_module')
    cc.export('solve', solve.__doc__.strip().split()[0])(solve)
    cc.compile()
    exit()
else:
    input = sys.stdin.buffer.readline
    read = sys.stdin.buffer.read

    if (USE_NUMBA and sys.argv[-1] != '-p') or sys.argv[-1] == "--numba":
        # -p: pure python mode
        # if not -p, import compiled module
        from my_module import solve  # pylint: disable=all
    elif sys.argv[-1] == "-t":
        _test()
        sys.exit()
    elif sys.argv[-1] != '-p' and len(sys.argv) == 2:
        # input given as file
        input_as_file = open(sys.argv[1])
        input = input_as_file.buffer.readline
        read = input_as_file.buffer.read

    main()

Submission status: Accepted

Solution code:
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
ans = 0
a_list = [0]
b_list = [0]
for i in range(n):
    a_list.append(a[i] + a_list[i])
for i in range(m):
    b_list.append(b[i] + b_list[i])

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

Submission status: Accepted

Solution code:
import bisect
n,m,k = map(int,input().split())
a_list = [0]+list(map(int,input().split()))
b_list = list(map(int,input().split()))
b_added = [0]
for b_i in b_list:
    b_added.append(b_added[-1]+b_i)
    if b_added[-1]>k:
        break
    
max_book = 0
temp_ki = 0
break_flag = False
for i in range(len(a_list)):
    temp_ki += a_list[i]
    if temp_ki > k:
        if i-1>max_book:
            max_book = i-1
        break_flag = True
        break
    temp_j = bisect.bisect(b_added,k-temp_ki)-1
    if i+temp_j > max_book:
        max_book = i+temp_j
        
print(max_book)

Submission status: Accepted

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

a = [0]
b = [0]

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

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

ans = 0
j = M

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

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

sa=[0]
sum=0
for i in range(n):
    sum+=a[i]
    sa.append(sum)
sb=[0]
sum=0
for i in range(m):
    sum+=b[i]
    sb.append(sum)

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

print(ans)

Submission status: Accepted

Solution code:
import bisect

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

asum = [0]
for i in range(len(a)):
	if asum[i]+a[i]<=k:
		asum.append(asum[i]+a[i])
	else:
		break

bsum = [0]
for i in range(len(b)):
	if bsum[i]+b[i]<=k:
		bsum.append(bsum[i]+b[i])
	else:
		break

ans = 0
for i in range(len(asum)):
	kb = k-asum[i]
	while len(bsum)>0 and bsum[-1]>kb:
		bsum.pop()
	ans = max(ans, len(bsum)+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, j = 0, 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 = [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 j in range(1, m+1):
    b[j] += b[j-1]
    
ans = 0
bc = m

for ac in range(n+1):
    if a[ac] > k:
        continue
    
    while a[ac] + b[bc] > k:
        bc -= 1
        
    ans  = max(ans, ac+bc)

print(ans)

Submission status: Accepted

Solution code:
import sys
from itertools import accumulate

n,m,k=map(int, input().split())
a = [0] + list(accumulate(map(int,sys.stdin.readline().rstrip().split())))
b = [0] + list(accumulate(map(int,sys.stdin.readline().rstrip().split())))
ans = 0
j = m

#print(a,b)

for i,item in enumerate(a):
        if k<item:
            break
        while k<item+b[j]:
            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, 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
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

rui_A = [0 for _ in range(N)]
rui_B = [0 for _ in range(M)]



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

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

rui_A.insert(0,0)

max_book = 0
for i in range(len(rui_A)):
    nokori = K - rui_A[i]
    if nokori >= 0:
        B_posi = bisect.bisect_right(rui_B,nokori)
        if max_book < B_posi + i:
            max_book = B_posi + i
print(max_book)

Submission status: Accepted

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

from collections import deque

an = list(map(int, input().split()))

bn = list(map(int, input().split()))

accum_a = [0]
accum_b = [0]


for a_i in range(0,len(an)):

    sumCandidate = accum_a[-1] + an[a_i]
    if sumCandidate > k:
        break
    accum_a.append(sumCandidate)

for b_i in range(0,len(bn)):
    i_ = accum_b[-1] + bn[b_i]
    if i_ > k:
        break
    accum_b.append(i_)

j = len(accum_b) - 1
ans = 0
for i in range(len(accum_a)):

    if accum_a[i] > k:
        break

    while accum_b[j] > k - accum_a[i]:
        j -= 1

    ans = max(ans, i+j)
print(ans)

Submission status: Accepted

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

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

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

#print(A2, B2)
#print(bisect.bisect_left(B2, 300))
ans = 0
for i in range(N + 1):
  time = K - A2[i]
  if time < 0:
    print(ans)
    quit()
  else:
    index = bisect.bisect_right(B2, time) - 1
    #print(index, time, B2)
    ans = max(ans, i + index)
    
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_s = [0] * (len(A)+1)
b_s = [0] * (len(B)+1)
for i in range(len(A)):
    a_s[i+1] = a_s[i] + A[i]
for i in range(len(B)):
    b_s[i + 1] = b_s[i] + B[i]

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

Submission status: Accepted

Solution code:
na,nb,k = map(int, input().split())
ta = list(map(int, input().split()))
tb = list(map(int, input().split()))

a = [0]
for i in range(na):
  a.append( a[i] + ta[i] )
b = [0]
for i in range(nb):
  b.append( b[i] + tb[i] )

ans, j=0, nb
for i in range(na + 1):
  if a[i] > k:
    break
  while True:
    if a[i] + b[j] > k:
      j -= 1
    else:
      break
  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_sum, B_sum = [0], [0]
for i, a in enumerate(A):
    A_sum.append(A_sum[i] + a)
for i, b in enumerate(B):
    B_sum.append(B_sum[i] + b)

ans = 0
j = M

# 机Aの本を読む所要時間:A_sum[i] … iの値を0から増やしていく
# 机Bの本を読む所要時間:B_sum[j] … iの値を固定し、Mから減らしていく
# A_sum[i]+B_sum[j]がKに収まるiとjの値を見つけ出し、i+jの値を出す
# iの値を増やしながら上記を繰り返し、i+jの最大値が解となる
for i in range(N+1):
    if A_sum[i] > K:
        break
    while B_sum[j] > K - A_sum[i]:
        j -= 1
    ans = max(ans, i+j)

print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect

N, M, K = map(int, input().split())
A = [0]+list(map(int, input().split()))
B = list(map(int, input().split()))
R_B = [0]*(M+1)
i = 0
while i < M:
  R_B[i+1] = R_B[i]+B[i]
  i += 1 
ans = 0
i = 0
while i <= N and K >= A[i]:
  K -= A[i]
  idx = bisect(R_B, K)
  ans = max(ans, i+idx-1)
  i += 1
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()))
ans = 0
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])
for i in range(n+1):
    if a[i] > k:
        break
    while b[m] > k - a[i]:
        m -= 1
    ans = max(ans, i + m)
print(ans)

Submission status: Accepted

Solution code:
import bisect
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
sa = [0]*(n+1)
sb = [0]*(m+1)
for i in range(n):
    sa[i+1] = sa[i]+a[i]
for i in range(m):
    sb[i+1] = sb[i]+b[i]
ans = 0
for i in range(n+1):
    t = sa[i]
    s = i
    tl = k-t
    if tl < 0:
        continue
    s += bisect.bisect_right(sb, tl)-1
    ans = max(ans,s)
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()))
cum_A=[0]
cum_B=[0]
for i in range(N):
  cum_A.append(cum_A[-1]+A[i])
for i in range(M):
  cum_B.append(cum_B[-1]+B[i])
ans, j = 0,M
for i in range(N+1):
  if cum_A[i] > K:
    break
  while cum_B[j] > K-cum_A[i]:
    j -= 1
  ans = max(ans,i+j)
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()))

asum = [0]
bsum = [0]

for i in range(n):
    asum.append(asum[i] + a[i])
for i in range(m):
    bsum.append(bsum[i] + b[i])

best = m
answer = 0
for i in range(n+1):
    j = best
    while 1:
        if bsum[j] <= (k - asum[i]):
            best = j
            if answer < i + j:
                answer = i + j
            break
        j -= 1
        if j < 0:
            break

print(answer)

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

A = list(accumulate(A))
A.insert(0,0)
B = list(accumulate(B))
B.insert(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:
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))

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

from bisect import bisect_left,bisect

kmax=0
for i in range(n+1):
    if k<aa[i]:
        break
    kk=k-aa[i]
    k2=bisect(bb,kk)-1
#    print(i,k2,aa[i],bb[k2])
    kmax=max(kmax,k2+i)

print(kmax)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy

n,m,k = map(int,input().split())
li_a = list(map(int,input().split()))
li_b = list(map(int,input().split()))

li_A = [0] + list(numpy.cumsum(li_a))
li_B = [0] + list(numpy.cumsum(li_b))

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

sumA, sumB = [0], [0]
sa, sb = 0, 0
for a in A:
    sa += a
    sumA.append(sa)
for b in B:
    sb += b
    sumB.append(sb)


n_max = 0
nb_max = M
for na in range(0, N+1):
    for nb in range(nb_max, -1, -1):
        k = sumA[na] + sumB[nb]
        if k <= K:
            n = na + nb
            n_max = max(n_max, n)
            nb_max = nb
            break
print(n_max)

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

len_B = M+1
res = []
for i, a_ in enumerate(a):
    for j in range(len_B)[::-1]:
        # print(i+1, j+1, a, B[j], a+B[j], K)
        if a_ + b[j] <= K:
            res.append(i+j)
            break
    len_B = j+1

print(0 if len(res) < 1 else max(res))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
from itertools import accumulate

def Main():
    n,m,k = map(int,sys.stdin.readline().rstrip().split())
    a = [0] + list(accumulate(map(int,sys.stdin.readline().rstrip().split())))
    b = [0] + list(accumulate(map(int,sys.stdin.readline().rstrip().split())))

    ans = 0
    j = m
    for i,item in enumerate(a):
        if k<item:  break
        while k<item+b[j]:
            j -= 1
        ans = max(ans,i+j)
    print(ans)
    return

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

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

ans = 0
for i in range(N+1):
    if a[i] <= K:
        ans = max(ans, i + bisect.bisect_right(b, K - a[i])-1)

print(ans)

Submission status: Accepted

Solution code:
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate, product, combinations_with_replacement
import sys
import bisect
import string
import math
import time


def I(): return int(input())
def S(): return input()
def MI(): return map(int, input().split())
def MS(): return map(str, input().split())
def LI(): return [int(i) for i in input().split()]
def LI_(): return [int(i)-1 for i in input().split()]
def StoI(): return [ord(i)-97 for i in input()]
def ItoS(nn): return chr(nn+97)
def input(): return sys.stdin.readline().rstrip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def print_matrix(mat):
    for i in range(len(mat)):
        print(*['IINF' if v == IINF else "{:0=4}".format(v) for v in mat[i]])


yn = {False: 'No', True: 'Yes'}
YN = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**19
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
show_flg = False
# show_flg = True


def main():
    N, M, K = MI()
    A = LI()
    B = LI()
    ans = 0

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

    for i in range(N, -1, -1):
        last = A[i]
        r = K - last
        if r < 0:
            continue
        bidx = bisect.bisect_right(B, r)
        ans = max(ans, i + bidx - 1)
        # print(r, bidx)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

def solve(N: int, M: int, K: int, A: "List[int]", B: "List[int]"):
    weightA = 0
    len_A = N
    for idx, a in enumerate(A):
        if weightA + a > K:
            len_A = idx
            break
        else:
            weightA += a
    max_count = current_count = len_A

    weightB = 0
    for b in B:
        if len_A==0 and weightA + weightB + b > K:
            break

        elif weightA + weightB + b <= K:
            weightB += b
            current_count += 1

        else:
            while weightA + weightB + b > K:
                len_A -= 1
                weightA -= A[len_A]
                current_count -= 1
                if len_A == 0:
                    break

            if weightA + weightB + b <= K:
                weightB += b
                current_count += 1
        max_count = max(max_count, current_count)

    print(max_count)
    return


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    N, M, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    solve(N, M, K, A, B)

if __name__ == '__main__':
    main()

Submission status: Accepted

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


n,m,k = map(int, input().split())
al = list(map(int, input().split()))
bl = list(map(int, input().split()))

al_sums = list(accumulate(al))
bl_sums = list(accumulate(bl))

ans = 0
for i in range(0,n+1):
    if i == 0: 
        a_sum = 0
    else:
        a_sum = al_sums[i-1]
    if a_sum > k: 
        continue
    cnt = bisect_right(bl_sums, k-a_sum)
    curr_ans = i+cnt
    ans = max(ans,curr_ans)

print(ans)

Submission status: Accepted

Solution code:
# C - Tsundoku

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

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

Bsum = [0]
for j in range(M):
    Bsum.append(Bsum[j] + B[j])

ans = 0
for i in range(N+1):
    if Asum[i] > K:
        continue
    tmp = K - Asum[i]
    j = bisect.bisect_right(Bsum, tmp) - 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_added=[0]
b_added=[0]
for i in range(n):
    a_added.append(a_added[i]+a[i])
for i in range(m):
    b_added.append(b_added[i]+b[i])

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

Submission status: Accepted

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

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n,m,k = LI()
a = LI()
b = LI()
a = [0] + list(accumulate(a))
b = [0] + list(accumulate(b))
ans = 0
for i in range(n+1):
    r = br(b, k-a[i])
    if r == 0 and a[i] <= k:
        ans = max(ans, i + r - 1)
    elif r != 0:
        ans = max(ans, i + r - 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 = [0]
b = [0]
for i in range(N):
  a.append(a[i] + A[i])
for i in range(M):
  b.append(b[i] + B[i])
  
ans = 0
j = M
for i in range(N+1):
  if a[i] > K:
    break
  while b[j] > K - a[i]:
    j -= 1
  ans = max(ans,i+j)
print(ans)

Submission status: Accepted

Solution code:
A_len, B_len, K = map(int, input().split(' '))
#print("Aは{0}冊. Bは{1}冊. 上限{2}分".format(A_len, B_len, K))

A = list(map(int, input().split(' ')))
B = list(map(int, input().split(' ')))
#print(A, B)

# A,Bの累積和の配列を出して、A_acc,B_accの要素番地が最大となるような組み合わせを出す

A_acc = []
for idx_A in range(A_len): 
  if(idx_A == 0):
    A_acc.append(A[0])
  else:
    A_acc.append(A_acc[idx_A - 1] + A[idx_A])
A_acc.insert(0, 0)
    
B_acc = []
for idx_B in range(B_len):
  if(idx_B == 0):
    B_acc.append(B[0])
  else:
    B_acc.append(B_acc[idx_B - 1] + B[idx_B])
B_acc.insert(0, 0)

#print(A_acc, B_acc)

max_num = 0
left_time = K
b_num = B_len
for a_num in range(A_len + 1):
  left_time = K - A_acc[a_num]
  #print("Aから{0}冊読んだ場合はそもそも残り{1}分".format(a_num, left_time))
  if(left_time < 0):
    break
  
  while(left_time - B_acc[b_num] < 0):
    #print("Bから{0}冊読んでる場合,残り時間{1}".format(b_num, left_time - B_acc[b_num]))
    
    b_num -= 1
    
  #print("結果,A棚から{0}冊、B棚から{1}冊で合計{2}冊で経過時間{3}"
  #      .format(a_num, b_num, a_num + b_num, A_acc[a_num] + B_acc[b_num]))
  max_num = max(max_num, (a_num + b_num))
  #print("現在max{0}".format(max_num))
  
print(max_num)

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:
from itertools import accumulate
import bisect
n,m,k=map(int,input().split())
a=[int(i) for i in input().split()]
b=[int(i) for i in input().split()]

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

j,ans=m,0
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
N, M, K = map(int, input().split())
As = list(map(int, input().split()))
Bs = list(map(int, input().split()))
sAs = [0]
s = 0
for A in As:
  s += A
  sAs.append(s)
sBs = [0]
s = 0
for B in Bs:
  s += B
  sBs.append(s)
rs = []
for n in range(0, N+1):
  kA = sAs[n]
  if kA > K:
    continue
  kB = K-kA
  m = bisect.bisect(sBs, kB)-1
  #print(n, kA, m, kB)
  r = n+m
  rs.append(r)
r = max(rs)
print(r)

Submission status: Accepted

Solution code:
# import sys
N, M, K = map(int, input().split())
A = [0] + list(map(int, input().split())) + [10**13]
B = [0] + list(map(int, input().split())) + [10**13]
sum_t = 0
cumsumA = [0]
cumsumB = [0]
ansA = N
ansB = 0
tmp = 0
for i in range(1, N+1):
    tmp = tmp + A[i]
    if tmp > K:
        ansA = i-1
        break
    cumsumA += [tmp]
tmp = 0
for i in range(1, M+1):
    tmp = tmp + B[i]
    if tmp > K:
        break
    cumsumB += [tmp]
j = 0
ans = 0
for i in range(ansA, -1, -1):
    t = cumsumA[i] + cumsumB[j]
    while True:
        tmp = B[j+1]
        if tmp + t <= K:
            t += tmp
            j += 1
        else:
            break
    ans = max(ans, i + j)
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

N, M, K = list(map(int, input().split()))
A = list(map(int, [0] + input().split()))
B = list(map(int, [0] + input().split()))
cost = 0
cumsum_a = np.cumsum(A)
cumsum_b = np.cumsum(B)

num_book = 0
max_a = 0
ind_b = 0
max_b = 0
for i in range(1, N + 1):
    if cumsum_a[i] <= K:
        max_a = i
    else:
        break

for i in range(1, M + 1):
    if cumsum_b[i] <= K:
        max_b = i
    else:
        break

for i in range(max_a, -1, -1):
    for j in range(ind_b, max_b+1):
        if cumsum_b[j] + cumsum_a[i] <= K:
            # print("ok")
            num_book = max(i + j, num_book)
            ind_b = j
        else:
            break
print(num_book)

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]*n
ba=[0]*m
aa[0]=a[0]
ba[0]=b[0]
for i in range(1,n):
  aa[i]=aa[i-1]+a[i]
for i in range(1,m):
  ba[i]=ba[i-1]+b[i]
aa=[0]+aa
ba=[0]+ba
import bisect as bi
ax=bi.bisect_right(aa,k)-1
ans=0
for i in range(ax+1):
  kk=k-aa[ax-i]
  bx=bi.bisect_right(ba,kk)-1
  t=ax+bx-i
  ans=max(ans,t)
print(ans)

Submission status: Accepted

Solution code:
from collections import deque
import sys
N, M, K = map(int,input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
a, b  = [0], [0]
for n in range(N):
    a.append(a[n]+A[n])
    #print(a)
for m in range(M):
    b.append(b[m]+B[m])
    #print(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:
import sys
from itertools import accumulate
from bisect import bisect_right

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n, m, k = 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))

    init = bisect_right(A_, k)
    res = init - 1
    for i in range(init):
        t = k - A_[init - i - 1]
        idx = bisect_right(B_, t)
        res = max(res, init - i - 1 + idx - 1)

    print(res)


if __name__ == '__main__':
    resolve()

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

da = [0 for _ in range(N+1)]
for i in range(N):
    da[i+1] = da[i] + a[i]
db = [0 for _ in range(M+1)]
for i in range(M):
    db[i+1] = db[i] + b[i]

ans = 0; j = M
for i in range(N+1):
    if da[i] > K:
        break
    while db[j] > K - da[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()))
def ruiseki(n, A):
    "長さnの数列Aの累積和を返す　ただし[0] = 0"
    res = [0]*(n+1)
    for i in range(n):
        res[i+1] = res[i] + A[i]
    return res
sa = ruiseki(n,A)
sb = ruiseki(m,B)

ans = 0
j = m
for i,a in enumerate(sa):
    
    while j >= 0 and a+sb[j] > k:
        j -= 1
    if j < 0:break
    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_cs = [0] * (n + 1)
b_cs = [0] * (m + 1)

for i in range(n):
    a_cs[i + 1] = a_cs[i] + a[i]
for i in range(m):
    b_cs[i + 1] = b_cs[i] + b[i]

ans = 0
j = m
for i in range(n + 1):
    while j > 0 and a_cs[i] + b_cs[j] > k:
        j -= 1

    if a_cs[i] + b_cs[j] <= k:
        ans = max(ans, i + j)

print(ans)