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

wa = 0
anslist = []
a_index = 0 #読めなかった本のindex
b_index = 0 #同上

for i in range(n+1):
  if i < n and k >= wa + a[i]:
    wa +=a[i]
    anslist.append(i+1)
  else:
    a_index = i
    break

for i in range(m+1):
  if i < m and k >= wa + b[i]:
    wa +=b[i]
    anslist.append(i + 1 + a_index)
  else:
    b_index = i
    break

for _ in range(m):
  if a_index !=0:
    wa -=a[a_index - 1] #Aを捨てる
    a_index -=1
  while b_index < m and k >= wa + b[b_index] :
    wa +=b[b_index]
    b_index +=1
  anslist.append(a_index + b_index)
print(max(anslist))

Submission status: Accepted

Solution code:
import sys
from collections import deque
sys.setrecursionlimit(2147483647)
input=sys.stdin.readline

def main():
  N, M, K = map(int, input().split(' '))
  A = list(map(int, input().split(' ')))
  B = list(map(int, input().split(' ')))
  As = [0]
  Bs = [0]
  for i in range(len(A)):
    As.append(A[i] + As[i])
  for i in range(len(B)):
    Bs.append(B[i] + Bs[i])
  ans, i = 0, N
  for j in range(M+1):
    if(K < Bs[j]):
      break
    while(i >= 0 and K < Bs[j] + As[i]):
      i -= 1
    ans = max(ans, i+j)
  print(ans)

if __name__=='__main__':
  main()

Submission status: Accepted

Solution code:
import sys

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

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

ac = [0]
bc = [0]

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

ans = 0
j = M

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

Submission status: Accepted

Solution code:
from collections import deque

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

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

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

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

print(mx)

Submission status: Accepted

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

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

cumA = [0] * (N+1)
cumB = [0] * (M+1)

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

def check(X):
    res = 1e9+1
    for i in range(N+1):
        j = X - i
        if j < 0 or M < j: continue
        if(cumA[i] + cumB[j] < res):
            res = cumA[i] + cumB[j]
    return res <= K

ok = 0
ng = N + M + 1

while ng - ok > 1:
    mid = (ok + ng) // 2
    if(check(mid)): ok = mid
    else: ng = mid

print(ok)

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

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

p = n
for i in range(n + 1):
    if C[i] > k:
        p = i - 1
        break

_max = p
keep = 0
for i in range(p, -1, -1):
    for j in range(keep, m + 1):
        if C[i] + D[j] > k:
            keep = j - 1
            break
        elif _max < i + j:
            _max = i + j
    if j == m:
        break
print(_max)

Submission status: Accepted

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

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

print(ans)

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]
t=0
for i in A:
    t+=i
    A_.append(t)
t=0
for j in B:
    t+=j
    B_.append(t)
for n in range(N+1):
    if A_[n]>K:
        n-=1
        break
#print(n)
ans=n
t=0
for i in range(n,-1,-1):
    for m in range(t,M+1):
#        print(B_[m],K-A_[i])
        if B_[m] > K-A_[i]:
            m-=1
            break
    t=m
#    print(ans,i,t)
    ans=max(ans,i+t)
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()))

p, q = [0], [0]
for i in range(n):
	p.append(p[i] + a[i])
for i in range(m):
	q.append(q[i] + b[i])

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

	while q[j] > k - p[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 = [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(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の合計時間 (最初はN冊全部)
sum_a = sum(A)
# Bの合計時間 (最初は0冊)
sum_b = 0
# Bの冊数=Bの次の一冊のインデックス (最初は0冊)
b_next_ind = 0
# A,B合計冊数の最大値
ans = 0

# Aは上からi冊使う; i=N,...,1,0
for i in reversed(range(N + 1)):
	# KからAの合計を除いたのがBに使える時間
	sum_b_max = K - sum_a
	# それが負だったら何もしない
	if sum_b_max >= 0:
		# !!! 前回のBの状態 (sum_b, b_next_ind) から調べ始める !!!
		# まだBに残りがあって、
		# 今の合計に次の一冊を足してもまだsum_b_max以内なら読む
		# それを可能な限り繰り返す
		while b_next_ind < M and sum_b + B[b_next_ind] <= sum_b_max:
			sum_b += B[b_next_ind]
			b_next_ind += 1
		# Aの冊数=i
		# Bの冊数=b_next_ind
		# この和が解の候補、最大値が答え
		ans = max(ans, i + b_next_ind)
	# 次のループのため、Aの一番下を消す
	# 最後の一回(i=0)では消すものが無いため何もしない
	# (実はpythonは A[-1] は後ろから1番目の意味になるため範囲外エラーは起こさないが。。)
	if i > 0:
		sum_a -= A[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()))

sum_a = sum(A)
sum_b = 0
b_next_ind = 0
ans = 0

# 下からi冊減らす; i=0..N
for i in range(N + 1):
	sum_b_max = K - sum_a
	if sum_b_max >= 0:
		while b_next_ind < M and sum_b + B[b_next_ind] <= sum_b_max:
			sum_b += B[b_next_ind]
			b_next_ind += 1
		ans = max(ans, (N - i) + b_next_ind)
	sum_a -= A[N - 1 - i]

print(ans)

Submission status: Accepted

Solution code:
from itertools import groupby, accumulate, product, permutations, combinations
def solve():
    ans = 0
    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))
    b = M
    for a in range(N+1):
        if A[a]>K:
            break
        while A[a]+B[b]>K:
            b -= 1
        ans = max(a+b,ans)
    return ans
print(solve())

Submission status: Accepted

Solution code:
'''
a = a1 + a2 + a3 ・・・ai
b = b1 + b2 + b3 ・・・bj
a + b <= K なれば良い
そして、本の数の最大数を求めるから、
つまり、i + j の最大の値を求める

１，まず、考えられる解法が全検索である。
しかし、N=M=200000 の場合には間に合わない。

２，次にiについてのみ考える。
i = 0,1・・・N
そして、bj <= K-ai
であるようなjの最大値を求める。

i=0 のときのjの最大値は、 j = M, M-1 
このように降順に走査すると、最初に bj <= K-ai 左の式が成立したjを求める。
このjをbest0とする

i=1 のときに選択可能なjの最大値を求める。
この最大値はbest0以下であるため、ｊ＝best0から始めることができる
'''
N, M, K = map(int,input().split())

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

#リストだと、0から始まるため、i + jの最初を０にしている
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

'''
aのリストは低い数字からみて、bのリストは高い数字から見る
i=0 のときのjの最大値は、 j = M, M-1 
このように降順に走査すると、最初に bj <= K-ai 左の式が成立したjを求める。
このjをbest0とする

i=1 のときに選択可能なjの最大値を求める。
この最大値はbest0以下であるため、ｊ＝best0から始めることができる
'''
# N + 1 している理由は、0がリストの最初にあるため。
#そして、0から始まる

for i in range(N+1):
    if a[i] > K:
        break
    # print(a[i])
    while b[j] > K -a[i]:
        #降順なので−１している
        j -= 1
        # print(b[j])
    #ansにi+jの最大値を返している
    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=[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])

i=0
j=m
ans=0
tmp=0
while(i<n+1 and j>=0):
    if(k>=A[i]):
        tmp=k-A[i]
        if(tmp-B[j]>=0):
            ans=max(ans,i+j)
            i+=1
        else:
            j-=1
    else:
        break
print(ans)

Submission status: Accepted

Solution code:
i = list(map(int, input().split()))
n,m,k = i[0], i[1], i[2]

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 bisect import bisect_right
from itertools import accumulate

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

a_ac=[0]+list(accumulate(a))
b_ac=[0]+list(accumulate(b))

ans=0
for i in range(n+1):
  remain=k-a_ac[i]
  if remain<0:
    break
  t_ans=i
  t_ans+=bisect_right(b_ac,remain)-1
  ans=max(ans,t_ans)

print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_right
from itertools import accumulate

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

a_ac=[0]+list(accumulate(a))
b_ac=[0]+list(accumulate(b))

ans=0
for i in range(n+1):
  remain=k-a_ac[i]
  if remain<0:
    continue
  t_ans=i
  t_ans+=bisect_right(b_ac,remain)-1
  ans=max(ans,t_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()))

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

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

#print(a_ac)
#print(b_ac)

ans=0
for i in range(n+1):
  remain=k-a_ac[i]
  if remain<0:
    continue
  t_ans=i
  t_ans+=bisect_right(b_ac,remain)-1
  ans=max(ans,t_ans)

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()))
a = [0]*(N+1)
b = [0]*(M+1)

#累積和の計算
for i in range(1, N+1):
    a[i] = a[i-1] + A[i-1]
for j in range(1, M+1):
    b[j] = b[j-1] + B[j-1]

ans = 0
# j = M
for i in range(N+1):
#     left = 0
#     right = j
    if K - a[i] < 0:
        break
#     while left < right:
#         mid = (left + right)//2
#         if b[mid] < K - a[i]:
#             left = mid + 1
#         elif b[mid] > K - a[i]:
#             right = mid - 1
#     if b[left] <= K - a[i]:
#         j = left
#     else:
#         j = left - 1
    j = bisect.bisect_right(b, K - a[i]) - 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()))

sa = [0]
sb = [0]

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

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

ans = 0
j = m

#しゃくとり
for i in range(n+1):
    if sa[i] > k:
        break
    while sa[i]+sb[j] > k:
        j -= 1
    ans = max(ans, i+j)

print(ans)

Submission status: Accepted

Solution code:
#n=int(input())
n,m,k=map(int,input().split())
al=list(map(int,input().split()))
bl=list(map(int,input().split()))
#l=[list(map(int,input().split())) for i in range(n)]

acum=[0]
bcum=[0]

for i in range(n):
    acum.append(acum[i]+al[i])

for i in range(m):
    bcum.append(bcum[i]+bl[i])
ans=0;j=m
for i in range(n+1):
    if acum[i]>k:
        break
    while acum[i]+bcum[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_wa = [0]
for i in range(N) :
    A_wa.append(A_wa[i]+A[i])

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

a = N
ans = 0
for b in range(M+1):
  A_lim = K - B_wa[b]
  if A_lim < 0:
    break
  while A_wa[a] > A_lim and a > 0:
    a -= 1
  ans = max(a + b, ans)
print(ans)

Submission status: Accepted

Solution code:
from bisect import *

N,M,K,*f = map(int, open(0).read().split())
A = f[:N]
B = f[N:]
csa = [0]
ans = 0
for i in range(N):
    csa.append(csa[i]+A[i])
csb = [0]
for i in range(M):
    csb.append(csb[i]+B[i])
for i in range(N+1):
    if csa[i] <= K:
        bs = bisect_right(csb,K-csa[i])
        ans = max(ans,i+bs-1)
    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,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:
# AtCoder
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, b_cnt = 0, M
for a_cnt in range(N+1):
    if A[a_cnt] > K:
        continue
    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, arr1, arr2, acum1, acum2, ans

n, m, k = map(int, input().split())
arr1 = list(map(int, input().split()))
arr2 = list(map(int, input().split()))

acum1 = [0]
for i in range(n):
    acum1.append(acum1[-1] + arr1[i])
acum2 = [0]
for i in range(m):
    acum2.append(acum2[-1] + arr2[i])

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

Submission status: Accepted

Solution code:


def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def solve():
    N, M, K = read_ints()
    A = read_ints()
    B = read_ints()
    A_read = 0
    B_read = M
    B_sum = sum(B)
    A_sum = 0
    max_read = 0
    for A_read in range(N+1):
        if A_read > 0:
            A_sum += A[A_read-1]
        while B_read > 0 and A_sum+B_sum > K:
            B_read -= 1
            B_sum -= B[B_read]
        if A_sum+B_sum <= K:
            max_read = max(max_read, A_read+B_read)
    return max_read


if __name__ == '__main__':
    print(solve())

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:
#!/usr/bin/env python
import os
import operator
from collections import defaultdict
import sys
from io import BytesIO, IOBase
import bisect

# def power(x, p):
#     res = 1
#     while p:
#         if p & 1:
#             res = res * x % 1000000007
#         x = x * x % 1000000007
#         p >>= 1
#     return res;

def main():
    n,m,k=map(int,input().split())
    A=[int(k) for k in input().split()]
    B=[int(k) for k in 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(i,j)
    print(ans)

    # for _ in range(int(input())):
        # n=int(input())
        # arr=[int(k) for k in input().split()]
        # odd=[]
        # even=[]
        # for i in range(2*n):
        #     if arr[i]%2==0:
        #         even.append(i+1)
        #     else:
        #         odd.append(i+1)
        # if len(odd)%2!=0 and len(even)%2!=0:
        #     odd.pop()
        #     even.pop()
        #     for i in range(0,len(odd),2):
        #         print(odd[i],odd[i+1])
        #     for i in range(0,len(even),2):
        #         print(even[i],even[i+1])
        # else:
        #     if len(odd)!=0 and len(even)!=0:
        #         odd.pop()
        #         odd.pop()
        #         for i in range(0, len(odd), 2):
        #             print(odd[i], odd[i + 1])
        #         for i in range(0, len(even), 2):
        #             print(even[i], even[i + 1])
        #     else:
        #         for i in range(0,2*n-2,2):
        #             print(i+1,i+2)








BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

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:
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 in range(n):
  a_sum.append(a_sum[i]+a[i])
for i in range(m):
  b_sum.append(b_sum[i]+b[i])
ans, j = 0, m
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:
n, m, k = map(int, input().split())
a = list(map(int, input().split())) 
b = list(map(int, input().split()))

from bisect import bisect_right
"""
if n>m:
    a, b = b, a
    n, m = m, n
"""
c = [b[0]]
for i in range(1, m):
    c.append(c[-1]+b[i])
a.insert(0,0)
x = 0
result = [0]
for i in range(n+1):
    x += a[i]
    if x > k:
        break
    y = k - x
    z = bisect_right(c, y)
    result.append(z+i)
print(max(result))

Submission status: Accepted

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

from bisect import bisect_right
"""
if n>m:
    a, b = b, a
    n, m = m, n
"""
c = [b[0]]
for i in range(1, m):
    c.append(c[-1]+b[i])
a.insert(0,0)
x = 0
result = [0]
for i in range(n+1):
    x += a[i]
    if x > k:
        break
    y = k - x
    z = bisect_right(c, y)
    result.append(z+i)
print(max(result))

Submission status: Accepted

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

from bisect import bisect_right
if n>m:
    a, b = b, a
    n, m = m, n
c = [b[0]]
for i in range(1, m):
    c.append(c[-1]+b[i])
a.insert(0,0)
x = 0
result = [0]
for i in range(n+1):
    x += a[i]
    if x > k:
        break
    y = k - x
    z = bisect_right(c, y)
    result.append(z+i)
print(max(result))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
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 j in range(m):
    b.append(b[j] + b_list[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:

import numpy as np
a,b,k=map(int,input().split())
n=list(map(int,input().split()))
m=list(map(int,input().split()))
n.insert(0,0)
m.insert(0,0)
n=np.array(n)
m=np.array(m)
n=n.cumsum()
m=m.cumsum()
ans=0
j=len(m)-1
for i in range(len(n)):
    if n[i]>k:
        break
    while m[j]>k-n[i]:
        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]

b_cnt = m
res = 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
    res = max(res, a_cnt+b_cnt)

print(res)

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

honn=bisect.bisect(B,K)
#print(honn)
for i in range(len(A)):
    s=K-A[i]
    if s>=0:
        
        a=bisect.bisect(B,s)
        #print(s)
        if i+1+a>honn:
            honn=i+1+a
print(honn)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
#一度も取らなかったときを考えて0を代入しておく
asum,bsum=[0],[0]
#a,bの合計値の全パターンをリストに入れておく
for i in range(n):
  asum.append(asum[i]+a[i])
for i in range(m):
  bsum.append(bsum[i]+b[i])
#カウント数とbの冊数の最大値
ans,j=0,m
for i in range(n+1):
  if asum[i]>k:
    break
  while bsum[j]>k-asum[i]:
    j-=1
  ans=max(ans,i+j)
print(ans)

Submission status: Accepted

Solution code:
import collections as cc
import itertools as it
import bisect as bi
I=lambda:list(map(int,input().split()))
n,m,k=I()
a=I()
b=I()
a=[0]+list(it.accumulate(a))
b=[0]+list(it.accumulate(b))
ans=0
for i in range(n+1):
	if k-a[i]<0:
		break
	x=bi.bisect_right(b,k-a[i])
	ans=max(ans,i+x-1)
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()))

a = [0]
b = [0]
for i in range(N):
    a.append(a[-1] + A[i])
for i in range(M):
    b.append(b[-1] + 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 = 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 = []
t = 0
for n in map(int, input().split()):
  if t + n > K:
    break
  t += n
  A.append(n)
N = len(A)
A = iter(A[::-1])
diff, top, i = 0, 0, 0
for m in map(int, input().split()):
  if t + m <= K:
    t += m
    diff += 1
  else:
    if i >= N:
      break
    for n in A:
      i += 1
      diff -= 1
      t -= n
      if t + m <= K:
        t += m
        diff += 1
        break
  if diff > top:
    top = diff
print(N+top)

Submission status: Accepted

Solution code:
import sys
import numpy as np
from numba import jit, njit
input = lambda: sys.stdin.buffer.readline().rstrip()

@njit(cache=True)
def main(n,m,k,a,b):
  sa = np.cumsum(a)
  sb = np.cumsum(b)
  rx = 0
  rb = m
  for ra in range(0,n+1): 
    while (sb[rb] + sa[ra]) > k and rb>0 :
      rb-=1
    if (sb[rb] + sa[ra]) <= k:
      rx = max(rx,ra+rb)
  return(rx)

n,m,k = map(int, input().split())
a = np.array([0]+list(map(int, input().split())))
b = np.array([0]+list(map(int, input().split())))
print(main(n,m,k,a,b))

Submission status: Accepted

Solution code:
import sys
import numpy as np
from numba import jit, njit
input = lambda: sys.stdin.buffer.readline().rstrip()

@njit
def main(n,m,k,a,b):
  sa = np.cumsum(a)
  sb = np.cumsum(b)
  rx = 0
  rb = m
  for ra in range(0,n+1): 
    while (sb[rb] + sa[ra]) > k and rb>0 :
      rb-=1
    if (sb[rb] + sa[ra]) <= k:
      rx = max(rx,ra+rb)
  return(rx)

n,m,k = map(int, input().split())
a = np.array([0]+list(map(int, input().split())))
b = np.array([0]+list(map(int, input().split())))
print(main(n,m,k,a,b))

Submission status: Accepted

Solution code:
import sys
import numpy as np
from numba import jit, njit
input = lambda: sys.stdin.buffer.readline().rstrip()

@njit
def main(n,m,k,sa,sb):
  rx = 0
  rb = m
  for ra in range(0,n+1): 
    while (sb[rb] + sa[ra]) > k and rb>0 :
      rb-=1
    if (sb[rb] + sa[ra]) <= k:
      rx = max(rx,ra+rb)
  return(rx)

n,m,k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
sa = np.cumsum([0]+a)
sb = np.cumsum([0]+b)
print(main(n,m,k,sa,sb))

Submission status: Accepted

Solution code:
import numpy as np

def main():
  n,m,k=map(int,input().split())
  a=list(map(int,input().split()))
  b=np.array(list(map(int,input().split())))
  b=np.cumsum(b)
  k0=k+1
  b0=np.searchsorted(b,k0)
  r=b0
  i=0
  for i, ia in enumerate(a):
    k0=k0-ia
    if k0<=0:
      break
    b0=np.searchsorted(b,k0)
    b0+=i+1
    if b0 > r:
      r=b0
  print(r)
main()

Submission status: Accepted

Solution code:
n,m,k = map(int,input().split())
arr0 = list(map(int,input().split()))
arr1 = list(map(int,input().split()))
acc0 = [0]

for i in range(n):
  acc0.append(acc0[-1] + arr0[i])
acc1 = [0]
for i in range(m):
  acc1.append(acc1[-1] + arr1[i])
ans = 0
j = m
for i in range(n+1):
  if acc0[i] > k:
    break
  while acc1[j] > k - acc0[i] and j > 0:
    j -= 1
  ans = max(ans, i + j)

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
a,b,k=map(int,input().split())
n=list(map(int,input().split()))
m=list(map(int,input().split()))
n.insert(0,0)
m.insert(0,0)
n=np.array(n)
m=np.array(m)
n=n.cumsum()
m=m.cumsum()
ans=0
j=len(m)-1
for i in range(len(n)):
    if n[i]>k:
        break
    while m[j]>k-n[i]:
        j-=1
    ans=max(ans,i+j)
print(ans)

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 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 = [0]
b = [0]
T = 0
ans = 0

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

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

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)