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

t=0
i=0
while i<n :
    if t+a[i]>k :
        break
    t+=a[i]
    i+=1

j=0
while j<m :
    if t+b[j]>k :
        break
    t+=b[j]
    j+=1

ans=i+j
while i>0 :
    i-=1
    t-=a[i]
    while j<m and t+b[j]<=k :
        t+=b[j]
        j+=1
    ans=max(ans,i+j)

print(ans)

Submission status: Accepted

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

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 = input().split()
A = input().split()
B = input().split()
ta = 0
cnta = 0
while cnta < int(N) and ta + int(A[cnta]) <= int(K):
    ta += int(A[cnta])
    cnta+=1

tb = 0
cntb = 0
while cntb < int(M) and ta + tb + int(B[cntb]) <= int(K):
    tb += int(B[cntb])
    cntb += 1
    
ans = cnta + cntb

while cnta - 1 >= 0:
    ta -= int(A[cnta - 1])
    cnta -= 1
    while cntb < int(M) and ta + tb + int(B[cntb]) <= int(K):
        tb += int(B[cntb])
        cntb += 1
    ans = max(ans, cnta + cntb)

print(ans)

Submission status: Accepted

Solution code:
n,m,k=input().split()
a=input().split()
b=input().split()
ta=0
cnta=0
while int(cnta)<int(n) and ta+int(a[cnta])<=int(k) :
    ta= ta+ int(a[cnta])
    cnta=cnta+1
tb=0
cntb=0
while cntb<int(m) and ta+tb+int(b[cntb])<=int(k):
    tb = tb+ int(b[cntb])
    cntb=cntb+1 
ans=cnta+cntb

while cnta-1>=0:
    ta=ta-int(a[cnta-1])
    cnta=cnta-1
    while cntb<int(m) and ta+tb+int(b[cntb])<=int(k):
        tb= tb+ int(b[cntb])
        cntb=cntb+1
    ans=max(ans,cnta+cntb)
print(ans)

Submission status: Accepted

Solution code:

n,m,k=map(int,input().split(" "))
a=[-9]
b=list(a)
a.extend(map(int,input().split(" ")))
b.extend(map(int,input().split(" ")))

t=0
i=1
while i<=n :
    # print("a[{}]={}".format(i,a[i]))
    if t+a[i]>k :
        break
    t+=a[i]
    # print("t=",t)
    i+=1
i-=1
# print("i=",i)
j=1
while j<=m :
    if t+b[j]>k :
        break
    t+=b[j]
    j+=1
j-=1
# print("j=",j)
ans=i+j
for i in range(i,0,-1):
    t-=a[i]
    # print("i-1=",i-1,end='  ')
    while j+1<=m and t+b[j+1]<=k :
        j+=1
        t+=b[j]
    # print("j=",j)
    ans=max(ans,i-1+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()))
import bisect
AR=[0]
BR=[0]
for i in range(n):
	AR.append(A[i]+AR[i])
for i in range(m):
	BR.append(B[i]+BR[i])

ans=0
for i in range(n+1):
	if AR[i]>k:
		break
	s=k-AR[i]
	t=bisect.bisect(BR,s)-1
	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()))
a.insert(0,0)
b.insert(0,0)

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

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

ans = 0
for i in range(n+1):
    if sum_a[i] > k:
        break
    for j in range(m,-1,-1):
        if sum_a[i] + sum_b[j] <= k:
            ans = max(ans, i+j)
            m = j
            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
# bを全部読む
j = M

for i in range(N + 1):
    # aは0冊から始まる
    if a[i] > K:
        break

    while b[j] > K - a[i]:
        j -= 1
    ans = max(ans, i + j)
    
    
print(ans)

Submission status: Accepted

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

input = sys.stdin.readline


def S():
    return input().rstrip()


def I():
    return int(input())


def MI():
    return map(int, input().split())


n, m, k = MI()
a = np.array(list(MI()))
b = np.array(list(MI()))

cuma = np.cumsum(a)
cuma = cuma[cuma <= k]
cumb = np.cumsum(b)
cumb = cumb[cumb <= k]
lena = len(cuma)
lenb = len(cumb)

a = a[lena - 1 :: -1]
b = b[:lenb]
ab = np.concatenate([a, b], axis=0)
n = len(ab)

ans = lena
left = 0
right = lena
sum = a.sum()
for left in range(n):
    while right < n and sum + ab[right] <= k:
        sum += ab[right]
        right += 1

    ans = max(ans, right - left)

    if left == right:
        right += 1
    else:
        sum -= ab[left]

print(ans)

Submission status: Accepted

Solution code:
import sys
import itertools as it

input = sys.stdin.readline

N, M, K = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]

AS = list(it.accumulate([0]+A))
BS = list(it.accumulate([0]+B))
max_value = 0
for i in range(N+1):
    if AS[i] > K:
        continue
    ok, ng = 0, M+1
    while ng - ok > 1:
        mid = (ok + ng) // 2
        time = AS[i] + BS[mid]
        if time <= K:
            ok = mid
        else:
            ng = mid
    num = i + ok
    if num > max_value:
        max_value = num
print(max_value)

Submission status: Accepted

Solution code:


def resolve():
    import sys
    input = sys.stdin.readline
    nmk = [int(x) for x in input().rstrip().split(" ")]
    n = nmk[0]
    m = nmk[1]
    k = nmk[2]
    a_list = [int(x) for x in input().rstrip().split(" ")]
    b_list = [int(x) for x in input().rstrip().split(" ")]

    from collections import deque
    qa = deque()
    qb = deque()
    total_min = 0
    for i in range(len(a_list)):
        if a_list[i] + total_min <= k:
            qa.append(a_list[i])
            total_min += a_list[i]
        else:
            break
    

    
    max_book = len(qa)

    for i in range(len(qb), len(b_list)):
        if b_list[i] + total_min <= k:
            qb.append(b_list[i])
            total_min += b_list[i]
        else:
            break
    if len(qa) + len(qb) > max_book:
        max_book = len(qa) + len(qb)

    while len(qa) > 0:
        poped = qa.pop()
        total_min -= poped
    
        for i in range(len(qb), len(b_list)):
            if b_list[i] + total_min <= k:
                qb.append(b_list[i])
                total_min += b_list[i]
            else:
                break
        if len(qa) + len(qb) > max_book:
            max_book = len(qa) + len(qb)
    
    print(max_book)
        

if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
import bisect


def main():
    N, M, K = map(int, input().split())
    A = [int(v) for v in input().split()]
    B = [int(v) for v in input().split()]
    ruisekiA = [0 for _ in range(N+1)]
    ruisekiB = [0 for _ in range(M+1)]
    for i in range(N):
        ruisekiA[i + 1] = ruisekiA[i] + A[i]
    for i in range(M):
        ruisekiB[i + 1] = ruisekiB[i] + B[i]

    ans = 0
    # A を i 個 使う
    for i in range(N+1):
        tans = i
        tk = K - ruisekiA[i]
        if tk < 0:
            break
        idx = bisect.bisect_left(ruisekiB, tk)
        if M+1 == idx or ruisekiB[idx] != tk:
            idx -= 1
        tans += idx
        ans = max(ans, tans)
    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()))
Atime = [0]
Btime = [0]

idxA = 0
for a in A:
    Atime.append(a+Atime[idxA])
    idxA += 1

idxB = 0
for b in B:
    Btime.append(b+Btime[idxB])
    idxB += 1

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

print(ans)

Submission status: Accepted

Solution code:
from itertools import zip_longest, count, accumulate
from bisect import bisect, bisect_left
 
def LIST(): return list(map(int, input().split()))
 
n, m, k = map(int, input().split())
a = [0] + list(accumulate(LIST()))
b = list(accumulate(LIST()))
 
num = 0
 
for i, a in enumerate(a):
    time_rest = k - a
    
    if time_rest < 0:
        continue
    
    num = max(num, i + bisect(b, time_rest))
 
print(num)

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

DP_1 = [0]
DP_2 = [0]
Max = 0

Sum = 0
for i in range(len(A)):
    Sum += A[i]
    if Sum <= K:
        DP_1.append(Sum)
    else:
        break

Sum = 0
for i in range(len(B)):
    Sum += B[i]
    if Sum <= K:
        DP_2.append(Sum)
    else:
        break

for i in range(len(DP_1)):

    j = bisect.bisect_left(DP_2,K-DP_1[i]+1) - 1

    a = i + j

    Max = max(Max,a)
    #print(Max,i,j,DP_1[i],DP_2[j])

#print(DP_1,DP_2)
print(Max)

Submission status: Accepted

Solution code:
import sys
import heapq
sys.setrecursionlimit(10 ** 8)

def Z(): return int(input())
def ZZ(): return [int(_) for _ in input().split()]

def main():
    N, M, K = ZZ()
    A = ZZ()
    B = ZZ()
    k = 0
    for i in range(N):
        if k + A[i] <= K: k += A[i]
        else: break
    else: i += 1
    ans = i
    for j in range(M):
        if k + B[j] <= K: k += B[j]
        else: break
    else: j += 1
    ans += j
    for ii in range(1, i+1):
        k -= A[i-ii]
        while j < M and k + B[j] <= K:
            k += B[j]
            j += 1
        ans = max(ans, i+j-ii)
    print(ans)

    return

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np
import sys
from collections import defaultdict
readline = sys.stdin.buffer.readline
# sys.setrecursionlimit(10**8)


def geta(fn=lambda s: s.decode()):
    return map(fn, readline().split())


def gete(fn=lambda s: s.decode()):
    return fn(readline().rstrip())


def main():
    n, m, k = geta(int)
    ca = np.array([0] + list(geta(int))).cumsum()
    cb = np.array([0] + list(geta(int))).cumsum()

    ans = 0
    for i in range(n+1):
        if ca[i] > k:
            break
        j = np.searchsorted(cb, k-ca[i], side="right") - 1
        if ans < i + j:
            ans = i + j

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

    Asum, Bsum = [0], [0]

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

    ans, j = 0, M

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

aa = [0]
bb = [0]

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

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

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

Submission status: Accepted

Solution code:
from bisect import *

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

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

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

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

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

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

ans = 0
j = M

for i in range (N+1):
    if sum_A[i] > K:
        break
    while sum_B[j] > K - sum_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()))

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の累積和
cumA = [0]*(n+1)
for i in range(n):
    cumA[i+1] = cumA[i] + A[i]
#Bの累積和
cumB = [0]*(m+1)
for i in range(m):
    cumB[i+1] = cumB[i] + B[i]

ans = 0
j = m

for i in range(n+1):
    if cumA[i]>k: #Ai>kであれば、どのようにjを選んでもAi+Bj<=kとなることはない
        break
    while cumB[j] > k-cumA[i] and j>0: #各iについて Bj <= k - Aiを満たす最大の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:
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

check_A = [0]
check_B = [0]

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

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

ans = 0
j = M

for i in range(N+1):
    if check_A[i] > K:
        break
    while check_B[j] > K - check_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 = [0]
B = [0]
for i in range(n):
    A.append(A[i] + a[i])
for i in range(m):
    B.append(B[i] + b[i])
ans = 0
for i in range(n+1):
    if A[i] > k:
        break
    while B[m] > k-A[i]:
        m -= 1
    ans = max(ans,i+m)
print(ans)

Submission status: Accepted

Solution code:
import bisect

N, M, K = input().split()

desk1 = list(map(int, input().split()))
desk2 = list(map(int, input().split()))

max_time = int(K)
item1 = [0]
item2 = [0]
for i in desk1:
    item1.append(item1[-1] + i)
for i in desk2:
    item2.append(item2[-1] + i)

ret = 0
for i, t in enumerate(item1):
    remain_time = max_time - t
    if remain_time >= 0:
        j = bisect.bisect(item2, remain_time) - 1
        ret = max(ret, i + j)

print(ret)

Submission status: Accepted

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

def psa(s: list) -> list:
    res = [0]
    for i in range(len(s)): res.append(res[i] + s[i])
    return res
(n, m, k), a, b = map(int, input().split()), list(map(int, input().split())), list(map(int, input().split()))
a, b, res, j = psa(a), psa(b), 0, m
for i in range(n + 1):
    if a[i] > k: break
    while b[j] > k - a[i]: j -= 1
    res = max(res, j + i)
print(res)

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

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

j = M
ans = 0

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

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

print(ans)

Submission status: Accepted

Solution code:
# aで読む冊数を固定して、bで何冊読めるか考える
# 累積和にしておく、読めるbの冊数は2分探索で求める
# 計算時間はO(N*logM)


import itertools
import bisect

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

# aとbの累積和を計算
# accumulateオブジェクトをリストに変換
a_cumsum = list(itertools.accumulate(a))
b_cumsum = list(itertools.accumulate(b))


# 読める本の冊数を記録しておくリスト
books = []


# aの冊数を0冊~最大までそれぞれ固定して、bの冊数を求める
for i in range(len(a_cumsum)):
    minutes = 0
    numbers = 0

    a_minutes = a_cumsum[i]  
    minutes += a_minutes
    
    if minutes <= k:

        # aの冊数を計算
        numbers += i

        # 残り時間
        rest_minutes = k - a_minutes
        
        # bの冊数を計算
        if b_cumsum[-1] == rest_minutes:
            b_books = len(b_cumsum) - 1
        else:
            # bisect.bisect_rightは、リストのどこに数値を入れ込めるかのインデックスを返す（右の）
            b_books = bisect.bisect_right(b_cumsum, rest_minutes)
            b_books -= 1
    
        numbers += b_books
    
    books.append(numbers)

print(max(books))

Submission status: Accepted

Solution code:
import itertools
from bisect import bisect
N, M, K = map(int, input().split())
A = tuple(map(int, input().split()))
B = tuple(map(int, input().split()))
A_sum = [0]
for a in A:
    A_sum.append(A_sum[-1]+a)
B_sum = [0]
for b in B:
    B_sum.append(B_sum[-1]+b)
ans = 0
for i, num in enumerate(A_sum):
    target = K - num
    if target < 0:
        break
    j = bisect(B_sum, target)
    ans = max(ans, i+j-1)
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

n, m, k = map(int, input().split(' '))
A = list(map(int, input().split(' ')))
B = list(map(int, input().split(' ')))

cum_a = np.r_[[0], np.cumsum(A)]
cum_b = np.r_[[0], np.cumsum(B)]

ans = 0
b_best = m
for a in range(0, n+1):
  ng = False
  for b in range(b_best, -2, -1):
    if b == -1:
      ng = True
      break
    # print(a, b)
    if cum_a[a] + cum_b[b] <= k:
      b_best = b
      ans = max(ans, a + b)
      break
  if ng:
    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:
# -*- coding: utf-8 -*-
from sys import stdin

import numpy as np

# from itertools import accumulate
# from bisect import bisect_right

# import sys
# sys.setrecursionlimit(10**4)


def _li():
    return list(map(int, stdin.readline().split()))


def _li_():
    return list(map(lambda x: int(x) - 1, stdin.readline().split()))


def _lf():
    return list(map(float, stdin.readline().split()))


def _ls():
    return stdin.readline().split()


def _i():
    return int(stdin.readline())


def _f():
    return float(stdin.readline())


def _s():
    return stdin.readline()[:-1]


N, M, K = _li()
A_list = _li()
B_list = _li()

# cum_a = [0] + list(accumulate(A_list))
# cum_b = list(accumulate(B_list))
cum_a = np.cumsum([0] + A_list)
cum_b = np.cumsum(B_list)
#
# ans = 0
# for i, a in enumerate(cum_a):
#     if a > K:
#         cur = 0
#     else:
#         cur = i
#     cand = bisect_right(cum_b, K - a)
#     ans = max(cur + cand, ans)
ans = 0
for i, a in enumerate(cum_a):
    if a > K:
        cur = 0
    else:
        cur = i
    cand = np.searchsorted(cum_b, K - a, side="right")
    ans = max(cur + cand, ans)

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from sys import stdin

# import numpy as np
from itertools import accumulate
from bisect import bisect_right

# import sys
# sys.setrecursionlimit(10**4)


def _li():
    return list(map(int, stdin.readline().split()))


def _li_():
    return list(map(lambda x: int(x) - 1, stdin.readline().split()))


def _lf():
    return list(map(float, stdin.readline().split()))


def _ls():
    return stdin.readline().split()


def _i():
    return int(stdin.readline())


def _f():
    return float(stdin.readline())


def _s():
    return stdin.readline()[:-1]


def search(cum1, cum2, iter_max, limit):
    ans = 0
    for (i,) in range(iter_max):
        if cum1[i] > limit:
            cur = 0
        else:
            cur = i
        cand = bisect_right(cum2, limit - cum1[i])
        ans = max(cur + cand, ans)
    return ans


N, M, K = _li()
A_list = _li()
B_list = _li()

cum_a = [0] + list(accumulate(A_list))
cum_b = list(accumulate(B_list))

ans = 0
for i, a in enumerate(cum_a):
    if a > K:
        cur = 0
    else:
        cur = i
    cand = bisect_right(cum_b, K - a)
    ans = max(cur + cand, ans)

print(ans)

Submission status: Accepted

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

def solve():
    N, M, K = list(map(int, input().split()))
    As = list(map(int, input().split()))
    Bs = list(map(int, input().split()))

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

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

    i = 0
    while True:
        if i==(N+1) or sumAs[i] > K:
            break
        i += 1
    i = max(0, i-1)

    j = 0
    while True:
        if j==(M+1) or sumBs[j] + sumAs[i] > K:
            break
        j += 1
    j = max(0, j-1)

    ans = i+j

    # iを少しずつ減らし、jをその分増やし、最適解を見つける
    for n in range(i, -1, -1):
        while True:
            if j==(M+1) or sumBs[j] + sumAs[n] > K:
                break
            j += 1
        j = max(0, j-1)
        ans = max(ans, n+j)

    print(ans)


if __name__ == "__main__":
    solve()

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

idx=N
ans=0
for i in range(N):
    if A[i]>K:
        idx=i-1
        break

idx2=0
for i in range(idx,-1,-1):
    while idx2<M and A[i]+B[idx2+1]<=K:
        idx2+=1
    ans=max(ans,i+idx2)

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

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

t=0
for i in range(n+1):
    if asum[i]>k:
        break
    now=k-asum[i]
    while bsum[m]>now:
        m-=1
    if t<i+m:
        t=i+m
        
print(t)

Submission status: Accepted

Solution code:
nums=[int(e) for e in input().split()]
A=[int(e) for e in input().split()]
B=[int(e) for e in input().split()]

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

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

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

ans = 0
for i in range(N+1):
    n = K - Ac[i]
    if n < 0: break
    tmp = bisect.bisect_right(Bc, n) - 1
    ans = max(ans, i + tmp)
    #print(i, tmp, ans, n, Ac, Bc)
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:
#　変数 n, m, k, arr1, arr2, acum1, acum2, ans

#累積和二つ
#aを回して
#bから条件をみたさないものを引いていく

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: #なぜwhile?

            j -= 1
    ans = max(ans,i+j)

print(ans)

Submission status: Accepted

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

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

answer = 0

currA = sum(A)
currB = 0
b_index = 0

for a_index in range(N, -1, -1):
    currA -= A[a_index]
    currK = K - currA
    if currK < 0: continue
    
    while currB <= currK and b_index <= (M - 1):
        nextB = currB + B[b_index]
        if nextB > currK: break
        
        currB = nextB
        b_index += 1
    
    answer = max(answer, a_index + b_index)
    
print(answer)

Submission status: Accepted

Solution code:
anum,bnum,acc = map(int,input().split())
alist = list(map(int,input().split()))
blist = list(map(int,input().split()))

asum = [0]
bsum = [0]
ans = 0
for i in range(anum):
    asum.append(asum[i]+alist[i])
for i in range(bnum):
    bsum.append(bsum[i]+blist[i])

b = bnum
for a in range(anum+1):
    if asum[a] > acc:
        break
    while asum[a] + bsum[b] > acc:
        b -=1
    ans = max(ans,a+b)

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()))
A = [0] * (n+1)
B = [0] * (m+1)
for i in range(1, n+1):
    A[i] = A[i-1] + a[i]
for i in range(1, m+1):
    B[i] = B[i-1] + b[i]
ans = 0
j = m
for i in range(n+1):
    while j >= 0:
        if A[i] + B[j] <= k:
            ans = max(ans, i + j)
            break
        else:
            j -= 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()))
 
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
 
for i in range(n+1):
    if sa[i] > k:
        break
    mb = 0
    lb = m+1
    for j in range(100):
        mid = (mb+lb)//2
        if sb[mid] + sa[i] <= k:
            mb = mid
        else:
            lb = mid
    ans = max(ans, i + mb)
 
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:
from bisect import bisect
import itertools
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))

sum_a = list(itertools.accumulate(a))
sum_b = list(itertools.accumulate(b))

nokori = k
counter = 0
ans = [0]

sum_a = [0] + sum_a #Bだけ取る場合に対応する

for i in sum_a:
    if i > k:
        break
    ans.append(counter + bisect(sum_b, k - i))
    counter +=1
print(max(ans))

Submission status: Accepted

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

N = int(N)
M = int(M)
K = int(K)

A = list(input().split())
A = [int(c) for c in A]

Sum_A = [0]
ele_A = 0
a = 0

while Sum_A[-1] + A[a] <= K:
    ele_A += A[a]
    a += 1
    Sum_A.append(ele_A)
    if a == len(A):
        break

B = list(input().split())
B = [int(c) for c in B]

Sum_B = [0]
ele_B = 0
b = 0

while Sum_B[-1] + B[b] <= K:
    ele_B += B[b]
    b += 1
    Sum_B.append(ele_B)
    if b == len(B):
        break

h = len(Sum_A) - 1
i = 0
j = 0

while h >= 0:
    while Sum_A[h] + Sum_B[i] <= K:
        j = max(j,h + i)
        if i == len(Sum_B) - 1:
            break
        i += 1
    if h == 0:
        break
    h -= 1

print(j)

Submission status: Accepted

Solution code:
import numpy as np
from bisect import bisect_right

N, M, K = map(int, input().split())
A: np.ndarray = np.array([0] + list(map(int, input().split())))
B: np.ndarray = np.array([0] + list(map(int, input().split())))

if A.sum() + B.sum() <= K:
    print(N + M)
    exit()

A_cumsum = A.cumsum()
B_cumsum = B.cumsum()

# print(f'{A_cumsum=}')
# print(f'{B_cumsum=}')

ans = 0
for i in range(N + 1):
    A_time = A_cumsum[i]
    if A_time > K:
        continue
    j = bisect_right(B_cumsum, K - A_time) - 1
    ans = max(ans, i + j)
print(ans)