Problem:


Score : 300 points

Problem Statement

We have two desks: A and B. Desk A has a vertical stack of N books on it, and Desk B similarly has M books on it.

It takes us A_i minutes to read the i-th book from the top on Desk A (1 \leq i \leq N), and B_i minutes to read the i-th book from the top on Desk B (1 \leq i \leq M).

Consider the following action:

Choose a desk with a book remaining, read the topmost book on that desk, and remove it from the desk.

How many books can we read at most by repeating this action so that it takes us at most K minutes in total? We ignore the time it takes to do anything other than reading.

Constraints

1 \leq N, M \leq 200000

1 \leq K \leq 10^9

1 \leq A_i, B_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M K
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_M

Output

Print an integer representing the maximum number of books that can be read.

Sample Input 1

3 4 240
60 90 120
80 150 80 150

Sample Output 1

3

In this case, it takes us 60, 90, 120 minutes to read the 1-st, 2-nd, 3-rd books from the top on Desk A, and 80, 150, 80, 150 minutes to read the 1-st, 2-nd, 3-rd, 4-th books from the top on Desk B, respectively.

We can read three books in 230 minutes, as shown below, and this is the maximum number of books we can read within 240 minutes.

Read the topmost book on Desk A in 60 minutes, and remove that book from the desk.

Read the topmost book on Desk B in 80 minutes, and remove that book from the desk.

Read the topmost book on Desk A in 90 minutes, and remove that book from the desk.

Sample Input 2

3 4 730
60 90 120
80 150 80 150

Sample Output 2

7

Sample Input 3

5 4 1
1000000000 1000000000 1000000000 1000000000 1000000000
1000000000 1000000000 1000000000 1000000000

Sample Output 3

0

Watch out for integer overflows.



Sample input:
3 4 240
60 90 120
80 150 80 150


Sample output:
3

Submission status: Accepted

Solution code:
import bisect
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
a = A[0]
b = B[0]
a_lis = [0,a]
b_lis = [b]
for i in range(1,N):
    a += A[i]
    a_lis.append(a)
for j in range(1,M):
    b += B[j]
    b_lis.append(b)
ans = 0
for i in range(N+1):
    if a_lis[i] <= K:
        j = bisect.bisect_right(b_lis,K-a_lis[i])
        tmp_ans = i + j 
        if ans < tmp_ans:
            ans = tmp_ans
    else:
        break
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()))
Acum = [A[0]] + [0]*(n-1)
Bcum = [B[0]] + [0]*(m-1)
for i in range(1,n):
    Acum[i] = Acum[i-1] + A[i]
for i in range(1,m):
    Bcum[i] = Bcum[i-1] + B[i]
maxn = 0
Bcum = [0] + Bcum
for i in range(m+1):
    if Bcum[i] <= k:
        maxn = max(maxn,i + bisect.bisect_right(Acum, k-Bcum[i]))
print(maxn)

Submission status: Accepted

Solution code:
number=list(map(int,input().split()))
N,M,K=number[0],number[1],number[2]
A=list(map(int,input().split()))
B=list(map(int,input().split()))
answer=0
j=M
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[j]>K-a[i]:
        j-=1
    answer=max(answer,i+j)

print(answer)

Submission status: Accepted

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

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

from itertools import accumulate
CA = [0]+A
CB = [0]+B
CA = list(accumulate(CA))
CB = list(accumulate(CB))
#print(CA)
#print(CB)
import bisect
ans = 0
for i in range(n+1):
    t = k-CA[i]
    if t < 0:
        continue
    j = bisect.bisect_right(CB, t)
    #print(i, t, j)
    ans = max(i+j-1, 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()))

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, 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,Bそれぞれについて上から順に加算
A_min = [0]
for i in range(N):
  A_min.append(A_min[i]+A[i])
B_min = [0]
for i in range(M):
  B_min.append(B_min[i]+B[i])

#A_min,B_minを順に加算して、Kを超えていなければ冊数を更新
ans = 0
B_len = M
for i in range(len(A_min)):
  if A_min[i] > K:
    break
  while B_min[B_len] > K - A_min[i]:
    B_len -= 1
  ans = max(ans,i+B_len)
print(ans)

Submission status: Accepted

Solution code:
import sys, bisect, math, itertools, string, queue, copy
import numpy as np
import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
from fractions import gcd
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

n,m,k = inpm()
a = [0] + inpl()
b = [0] + inpl()
aa = [0]
bb = [0]
for i in range(1,len(a)):
  aa.append(a[i] + aa[i-1])
for i in range(1,len(b)):
  bb.append(b[i] + bb[i-1])

ans = 0
for i in range(n+1):
    zan = k - aa[i]
    if zan < 0:
        break
    bcnt = bisect.bisect(bb,zan)
    ans = max(ans,i+bcnt-1)

print(ans)

Submission status: Accepted

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

N, M, K = map(int, input().split())
A_temp = [int(i) for i in input().split()]
B_temp = [int(i) for i in input().split()]

A, B = [0]*(N+1), [0]*(M+1)
for i in range(1, N+1):
    A[i] = A[i-1] + A_temp[i-1]
for i in range(1, M+1):
    B[i] = B[i-1] + B_temp[i-1]

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

Submission status: Accepted

Solution code:
N, M, K = map(int, input().split())
A_temp = [int(i) for i in input().split()]
B_temp = [int(i) for i in input().split()]

A, B = [0]*(N+1), [0]*(M+1)
for i in range(1, N+1):
    A[i] = A[i-1] + A_temp[i-1]
for i in range(1, M+1):
    B[i] = B[i-1] + B_temp[i-1]

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

Submission status: Accepted

Solution code:
import itertools

numOfA,numOfB,target = map(int, input().split())

timetoreadA = list(map(int,input().split()))
timetoreadB = list(map(int,input().split()))

timetoreadAacc = list(itertools.accumulate(timetoreadA))

booknumonlyA = 0
totaltime = 0
for time in timetoreadAacc:
	if target < time:
		break
	booknumonlyA += 1
	totaltime = time

bookreadA = booknumonlyA
bookread = [bookreadA]

for posB in range(numOfB):
	bookreadB = posB + 1
	newtime = totaltime + timetoreadB[posB]
	if newtime <= target:
		bookread.append(bookreadA + bookreadB)
		totaltime = newtime
	else:
		while newtime > target and bookreadA > 0:
			bookreadA -= 1
			newtime = newtime - timetoreadA[bookreadA]

		if newtime <= target:
			bookread.append(bookreadA + bookreadB)

		totaltime = newtime

print(max(bookread))

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

i = 0
ans = 0
j = m
while(i < len(a)):
    if a[i] > k:
        break

    else:
        while(b[j] > k-a[i]):
            j = j-1

        ans = max(ans,i+j)

    i = i+1

print(ans)

Submission status: Accepted

Solution code:
import sys
from bisect import bisect_left, bisect_right
N,M,K=map(int, sys.stdin.readline().split())
A=map(int, sys.stdin.readline().split())
B=map(int, sys.stdin.readline().split())

RUISEKI_A=[0]
RUISEKI_B=[0]
for a in A: RUISEKI_A.append(RUISEKI_A[-1]+a)
for b in B: RUISEKI_B.append(RUISEKI_B[-1]+b)

ans=0
for i in range(N+1):
    nokori=K-RUISEKI_A[i]
    if 0<=nokori:
        ans=max(ans,i+bisect_right(RUISEKI_B,nokori)-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]
  
import bisect

ans = 0
for i in range(len(A)):
  rest = K - A[i]
  if rest < 0:
    continue
  if ans < i:
    ans = i
  b_read = bisect.bisect_right(B, rest)
  if ans < i + b_read - 1:
    ans = i + b_read - 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 n in range(N + 1):
  if A[n] > K:
    break
  while B[j] > K - A[n]:
      j -= 1
  ans = max(ans, n + j)
print(ans)

Submission status: Accepted

Solution code:
N, M, K = map(int, input().split())
A_temp = [int(i) for i in input().split()]
B_temp = [int(i) for i in input().split()]

A, B = [0]*(N+1), [0]*(M+1)
for i in range(1, N+1):
    A[i] = A[i-1] + A_temp[i-1]
    
for j in range(1, M+1):
    B[j] = B[j-1] + B_temp[j-1]
    
ans = 0
for a in range(N+1):
    if A[a] > K:
        break
    for b in range(ans-a+1, M+1):
        if A[a] + B[b] > K:
            break
        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 , 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)

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

SumA = [0]
SumB = [0]
cnt = 0
for i in range(N):
    SumA.append(SumA[i] + A[i])
for i in range(M):
    SumB.append(SumB[i] + B[i])

for i in range(N+1):
    if SumA[i] > K:
        break
    tmpcnt = bisect.bisect_right(SumB, K - SumA[i]) - 1
    #print([SumA,SumB])
    #print(SumA[i])
    #print(K - SumA[i])
    #print(tmpcnt)
    #print([i,tmpcnt])
    cnt = max(cnt, i + tmpcnt)
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()))

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])
j = M
ans = []
for i in range(N + 1):
    if a[i] > K:
        break
    while b[j] > K - a[i]:
        j -= 1
    ans.append(i + j)
ans_max = max(ans)
print(ans_max)

Submission status: Accepted

Solution code:
n, m, k = map(int, input().split(' '))
a_books = [0] + list(map(int, input().split(' ')))
b_books = [0] + list(map(int, input().split(' ')))
result = 0

for i in range(1, n+1):
    a_books[i] += a_books[i-1]
for j in range(1, m+1):
    b_books[j] += b_books[j-1]

b_cnt = m
for a_cnt in range(n+1):
    if a_books[a_cnt] > k:
        break
    while a_books[a_cnt] + b_books[b_cnt] > k:
        b_cnt -= 1
    result = max(result, a_cnt + b_cnt)
print(result)

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

acc_A = [0]+list(itertools.accumulate(A))
acc_B = [0]+list(itertools.accumulate(B))
    
ans, j = 0, m
for i in range(n+1):
    if acc_A[i] > k:
        break
    while acc_A[i] + acc_B[j] > k:
        j -= 1
    ans = max(ans, i+j)
print(ans)

Submission status: Accepted

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

a,b = [0], [0]

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

Submission status: Accepted

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

a = [0]
b = [0]

# Aの机だけを上から読んでいく時のそれぞれの読了時間(Bは考慮しない)
for i in range(N):
    a.append(a[i] + A_list[i])
# 同じくBの読了時間
for i in range(M):
    b.append(b[i] + B_list[i])

ans, j = 0, M
# Aの机にある本を読まない場合からスタート
for i in range(N + 1):
    if a[i] > K:
        break
    # Kに収まるようにjを上から見ていく(単調増加)
    while b[j] + 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()))
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 = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]


def cusum(array):
    for i in range(len(array) - 1):
        array[i + 1] += array[i]


cusum(A)
cusum(B)
A = [0] + A
B = [0] + B
tsundoku = 0
ia = 0
ib = M

for ia in range(N + 1):
    if A[ia] > K:
        break
    while A[ia] + B[ib] > K and ib >= 0:
        ib -= 1
    tsundoku = max(ia + ib, tsundoku)

print(tsundoku)

Submission status: Accepted

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

sa = [0]
for i, val in enumerate(A):
  sa.append(sa[i] + val)

sb = [0]
for i, val in enumerate(B):
  sb.append(sb[i] + val)

max_num = 0
ind_max = M
for i in range(N+1):
  if(sa[i]>K):
    break
  c = 0
  for k in range(ind_max+1):
    if(sb[ind_max-k]<=K-sa[i]):
      break
    c += 1
  ind_max -= c
  max_num = max(max_num,i+ind_max)

      
      
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 = 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:
INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))
def do(): 
    n,m,k=INTM()
    A=LIST()
    B=LIST()
    As=[0]*(n+1)
    Bs=[0]*(m+1)
    ans=0
    for i in range(n):
        As[i+1]=As[i]+A[i]
    A=As
    for i in range(m):
        Bs[i+1]=Bs[i]+B[i]
    B=Bs
    #print(A,B)

    if A[-1]+B[-1]<=k:
        ans=n+m

    b_s=m

    for i in range(n+1):

        if A[i]>k:
            #print(i)
            temp=i-1
            ans=max(ans,temp)
            break
        for i2 in range(b_s,-1,-1):
            #print(i,i2)
            #print(A[i]+B[i2])
            if A[i]+B[i2]<=k:
                temp=i+i2
                b_s=i2
                #print(ans,temp)
                ans=max(ans,temp)
                break
        

    
    print(ans)

if __name__ == '__main__':
    do()

Submission status: Accepted

Solution code:
n,m,k = map(int,input().split())
nlist = list(map(int,input().split()))
mlist = list(map(int,input().split()))
result = 0

# mlistが読める残り時間。ncount(nが読める冊数)が増えるたびに減少していく
mrest = k

# mlistが読める冊数。最初はの最大冊数だが、ncount(nが読める冊数)が増えるたびに減少していく
mcount = m

# mcount分読むのにかかる時間。最初は最大冊数分持っているか、mcountが減るたびに減少していく
mtime = sum(mlist)

# ncount分読むのにかかる時間。ncountが増えるたびに増加していく
ntime = 0

# ncount(nが読める冊数)を1ずつ増やしていく
for ncount in range(len(nlist) + 1):
  # ncountが1以上の場合は、1冊読んだので、mrestをその時間分減らす
  if ncount > 0:
    mrest -= nlist[ncount - 1]
    ntime += nlist[ncount - 1]
  # もしntimeが最大時間を超えてしまったら終了する
  if ntime > k:
    break;
  # mlistが読める残り時間も冊数もあれば、mlistから本を取っていく
  # もしmlistが読める残り時間が最大時間を超えていたら後ろから諦めていく
  while mcount > 0 and mtime > mrest:
    mcount -= 1
    mtime -= mlist[mcount]
  # このループで読める冊数はncount+mcount。ループごとの最大値が答え
  result = max(result, ncount + mcount)
  # nlistの本を全部読み終わっていたら処理を終了する
  if ncount == n:
    break;
print(result)

Submission status: Accepted

Solution code:
N, M, K = [int(v) for v in input().strip().split(" ")]
A = [int(v) for v in input().strip().split(" ")]
B = [int(v) for v in input().strip().split(" ")]

a, b = [0], [0]
max_sum = 0

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

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

j = len(B)
for i in range(len(A)+1):
    if a[i] > K:
        continue
    else:
        while b[j] > K - a[i]:
            j -= 1
        max_sum = max(max_sum, i + j)

print(max_sum)

Submission status: Accepted

Solution code:
s = list(map(int,input().split()))
N = s[0]
M = s[1]
K = s[2]

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

asum = [0]
bsum = [0]
apsum = 0
bpsum = 0
for times in a:
  apsum += times
  asum.append(apsum)
for times in b:
  bpsum += times
  bsum.append(bpsum)

nums = []
i = 0
j = M
for i in range(N+1):
  if asum[i]>K:
    pass
  else:
    while asum[i]+bsum[j] > K:
      j += -1
    nums.append(i+j)
print(max(nums))

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

Submission status: Accepted

Solution code:
n,m,k=map(int,input().split())
a=[int(i) for i in input().split()]
b=[int(i) for i in input().split()]
ans=chk=0
w=-1
for i in range(n):
    if k>=a[i]:
        chk+=1
        k-=a[i]
        w=i
        ans=max(ans,chk)
    else:
        break

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

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

Solution code:
N, M, K = map(int, input().split())
A = [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]

# bisect
import bisect
ans = 0
for i in range(N+1):
    if A[i] <= K:
        rest = K - A[i]
        b_cnt = bisect.bisect_right(B,rest)-1
        ans = max(ans,i+b_cnt)
    else:
        break
print(ans)

Submission status: Accepted

Solution code:
N,M,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
A_num = len(A)
B_num = len(B)
Ali = [0]
Bli = [0]
sumA = 0
sumB = 0
for a in range(A_num):
  sumA += A[a]
  Ali.append(sumA)
for b in range(B_num):
  sumB += B[b]
  Bli.append(sumB)
b_start = 0
time = 0
ans = 0
Bli.reverse()
for i in range(A_num+1):
  for j in range(b_start,B_num+1):
    time = Ali[i] + Bli[j]
    if time <= K:
      #print("i:",i,"j:",(B_num - j),"time:",time)
      b_start = j
      ans = max(ans,i+(B_num - j))
      break
print(ans)

Submission status: Accepted

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

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

if a[0] > k:
    res = 0
    for i in range(m):
        if b[i] <= k:
            k -= b[i]
            res += 1
        else:
            break
    print(res)
else:
    cnt = 0
    a_idx = 0
    for i in range(n):
        if a[i] <= k:
            k -= a[i]
            cnt += 1
            a_idx = i
        else:
            break
    
    b_idx = -1
    for i in range(m):
        if b[i] <= k:
            k -= b[i]
            cnt += 1
            b_idx = i
        else:
            break
    
    res = cnt

    for i in range(a_idx,-1,-1):
        k += a[i]
        cnt -= 1
        fi = b_idx
        for j in range(b_idx+1,m):
            if b[j] <= k:
                k -= b[j]
                cnt += 1
                fi = j
            else:
                break
        b_idx = fi
        res = max(res,cnt)
    print(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()))

for i in range(n-1):
    a[i+1] = a[i] + a[i+1]
a.insert(0, 0)
for i in range(m-1):
    b[i+1] = b[i] + b[i+1]
b.insert(0, 0)

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

Submission status: Accepted

Solution code:
if __name__ == "__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, 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(len(a)):
        if a[i] > K:
            break
        while a[i] + b[j] > K:
            j -= 1
        ans = max(ans, i + j)

    print(ans)

Submission status: Accepted

Solution code:
N, M, K = map(int, input().split())
A = [int(i) for i in input().split()]
B = [int(i) for i in 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(len(a)):
    if a[i] > K:
        break
    while a[i] + b[j] > K:
        j -= 1
    ans = max(ans, i + j)

print(ans)

Submission status: Accepted

Solution code:
def main():
    N, M, K = map(int, input().split())
    A = [int(i) for i in input().split()]
    B = [int(i) for i in 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(len(a)):
        if a[i] > K:
            break
        while a[i] + b[j] > K:
            j -= 1
        ans = max(ans, i + j)

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys


def input(): return sys.stdin.readline().strip()


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, 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(len(a)):
        if a[i] > K:
            break
        while a[i] + b[j] > K:
            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()))

# A,Bそれぞれの累積和を作る
# 初期値は0冊目まで読んだときの値（ここでは0）を入れておく
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

# Bは全て（=M冊目まで）読めることを初期値とする
# Aの山で読む本を増やすにつれて、jは（広義）単調減少する
j = M

# Aの山について、何冊目まで読むか
for i in range(N + 1):

    # Aの山だけでK分オーバーの場合
    # これ以上iを増やしても無意味なので打ち切る
    if a[i] > K:
        break

    # a[i]+b[j] > K　ならjをデクリメント
    # Bの山を全て読めると仮定した後、下の方から一冊ずつ諦める
    # jはiの増加に対して広義単調減少（しゃくとり法）、Mから0まで一回走査するだけで良い
    while b[j] > K - a[i]:
        j -= 1

    # 冊数はi+j、全てのiに対してi+jを比較、最大のものが答え
    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 i in range(1,m+1):
    b[i]+=b[i-1]

b_cnt=m
ans=0

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

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

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

s2 = [0]
for i in range(m):
    s2.append(s2[-1]+b[i])
ans = 0
j = m
for i in range(n+1):
    
    if k-s1[i]<0:
        break
    while k-s1[i]-s2[j]<0 and j>0:
            j -= 1 
    ans = max(i+j,ans)
print(ans)

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]"):
    a = 0
    a_n = [a]
    for a_i in A:
        a += a_i
        a_n.append(a)

    b = 0
    b_n = [b]
    for b_i in B:
        b += b_i
        b_n.append(b)

    ans = 0
    j = M
    for i, a in enumerate(a_n):
        if a > K:
            break
        while b_n[j] > (K-a):
            j -= 1
        ans = max(ans, i+j)

    return ans


# 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():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    B = [int(next(tokens)) for _ in range(M)]  # type: "List[int]"
    print(solve(N, M, K, A, B))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:

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

a, b = [0], [0]
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
from bisect import bisect_right
N, M, K = map(int, input().split())
A = [0] + list(map(int, input().split()))
B = list(map(int, input().split()))
SA = list(accumulate(A))
SB = list(accumulate(B))

ans = 0
for n in range(len(SA)):
    tmp = K-SA[n]
    if tmp < 0:
        break
    m = bisect_right(SB, tmp)
    ans = max(ans, n+m)
print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate

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

a_acc = [0] + list(accumulate(a))  # aを0冊読む=1冊も読まない場合があるので
b_acc = [0] + list(accumulate(b))  # 今度は[0]が必要です

ans = 0
for an in range(n + 1):
    rem = k - a_acc[an]
    if rem < 0:
        break

    # 最初は 0 <= x < m + 1 の範囲で、この範囲を狭めていきます
    ok = 0
    ng = m + 1   
    # ng = ok + 1のとき、ok <= x < ng は ok <= x < ok + 1で、x = okに確定します
    while abs(ng - ok) > 1:
        mid = (ok + ng) // 2
        # bをmid冊読めるか？
        if b_acc[mid] <= rem:
        # 読めるならok
            ok = mid
        else:
        # 読めないならng
            ng = mid

    bn = ok
    ans = max(ans, an + bn)

print(ans)

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, b = [0], [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, 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 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 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 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)