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()))
a = [0]
b = [0]
for i in range(n):
    a.append(a[-1]+A[i])
for j in range(m):
    b.append(b[-1]+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:
import numpy as np

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

A = np.array(A).cumsum()
B = np.array(B).cumsum()

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

Submission status: Accepted

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

sumAL = [0]
for i in range(len(listA)):
    sumAL.append(sumAL[-1]+listA[i])
sumBL = [0]
for i in range(len(listB)):
    sumBL.append(sumBL[-1]+listB[i])
ib = len(sumBL)-1
bookMax = 0
for ia in range(len(sumAL)):
    while ib > -1:
        if sumBL[ib] <= k-sumAL[ia]:
            if bookMax < ia+ib:
                bookMax = ia+ib
            break
        ib-=1
print(bookMax)

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]*(n+1)
b = [0]*(m+1)
for i in range(n):
    a[i+1] = a[i] + A[i]
    
for i in range(m):
    b[i+1] = 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:
N, M, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
sa = [0] * (N + 1)
sb = [0] * (M + 1)
for i in range(N):
	sa[i+ 1] = sa[i] + a[i]
for i in range(M):
	sb[i + 1] = sb[i] + b[i]
r = M
books = 0
for i in range(N+1):
	if sa[i] > k: break
	while sa[i] + sb[r] > k:
		r -= 1
	books = max(books, i+r)
print(books)

Submission status: Accepted

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

max_c = 0
c = 0
i = 0
j = 0

while i < n and k - a[i] >=0:
    k -= a[i]
    i += 1
    c += 1

while j < m and k - b[j] >=0:
    k -= b[j]
    j += 1
    c += 1


max_c = c
# print(max_c, k, i, j)

i -= 1

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

Submission status: Accepted

Solution code:
import itertools
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()))

ans = 0
a_cum = [0] + list(itertools.accumulate(a))
b_cum = [0] + list(itertools.accumulate(b))

y = m  # len(b_cum) - 1
for x in range(len(a_cum)):
    if a_cum[x] > k:
        break
    while b_cum[y] > k - a_cum[x]:
        y -= 1
    ans = max(ans, x + y)

print(ans)

Submission status: Accepted

Solution code:
# Problem C - Tsundoku

# input
N, M, K = map(int, input().split())
a_nums = list(map(int, input().split()))
b_nums = list(map(int, input().split()))

# initialization
max_count = 0
b_i = M
a_cums = [0]*(N+1)
b_cums = [0]*(M+1)
for i in range(1, N+1):
    if i>1:
        a_cums[i] = a_cums[i-1] + a_nums[i-1]
    else:
        a_cums[i] = a_nums[i-1]
for i in range(1, M+1):
    if i>1:
        b_cums[i] = b_cums[i-1] + b_nums[i-1]
    else:
        b_cums[i] = b_nums[i-1]

for i in range(N+1):
    if a_cums[i]<=K:
        while b_i>0 and a_cums[i]+b_cums[b_i]>K:
            b_i -= 1
        if i+b_i>max_count:
            max_count = i+b_i

# output
print(max_count)

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

sum_A = [0]
for i,a in enumerate(A, 1):
    tmp = sum_A[i-1] + a
    if tmp <= K:
        sum_A.append(tmp)
        mark_A = i
    else:
        break
    
sum_B = [0]
for i,b in enumerate(B, 1):
    tmp = sum_B[i-1] + b
    if tmp <= K:
        sum_B.append(tmp)
        mark_B = i
    else:
        break
    
ans = 0
j = len(sum_B) - 1

for i,a in enumerate(sum_A):
    while sum_B[j] > K - a:
        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()))
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
for i in range(n+1):
  if a[i] > k:
    break
  ans = max(ans,i+bisect.bisect(b,k-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()))

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

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, = map(int, input().split())
*B, = map(int, input().split())

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

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

print(ans)

Submission status: Accepted

Solution code:
# import io
# import sys

# _INPUT = """\
# 1 4 120
# 100 1 1 1 1 1 1 1 1 1 1
# 80 10 10 10 10
# """
# sys.stdin = io.StringIO(_INPUT)

####################################################################
import sys
def p(*_a):
  return
  _s=" ".join(map(str,_a))
  #print(_s)
  sys.stderr.write(_s+"\n")
####################################################################

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

# 累積和
from itertools import accumulate
A = list(accumulate(a))
B = list(accumulate(b))

p(*A)
p(*B)

from bisect import bisect_right as br

ans = 0
# Aは「位置番号＝冊数」とするので、
# 「Aが0冊」の可能性を考慮し、先頭に[0]を追加
# Bは二分探索するので、挿入位置＝冊数になるから、[0]は追加しなくてOK
# (←挿入位置が先頭になれば、自然と0が戻り値になるから)
A = [0] + A
for a, ka in enumerate(A):
  if ka > K: break

  b = br(B, K - ka) #二分探索を使って、Bの取り得る最大の時間から位置番号(＝冊数)を求める
  ans = max(ans, a + b)


print(ans)

Submission status: Accepted

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

A = [0]
B = [0]

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

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

Submission status: Accepted

Solution code:
import bisect
import itertools
n,m,k = map(int,input().split())
A = [0]+list(map(int,input().split()))
B = [0]+list(map(int,input().split()))

#累積和
a_cum = list(itertools.accumulate(A))
b_cum = list(itertools.accumulate(B))

ans=0
#b_cumを固定して2部探索
#aを読んだときに、k-a残りでどこまで読めるか2ブタン
#bisect_leftならある値未満、bisect_rightならある値以下を探索
for a_i,a in enumerate(a_cum):
    if a>k:
        break
    #0を入れてるので-1する
    b_i = bisect.bisect_right(b_cum,k-a)-1
    ans = max(ans,a_i+b_i)
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=[0]*N
b=[0]*M
a[0]=A[0]
b[0]=B[0]
for i in range(N-1):
    a[i+1]=a[i]+A[i+1]
for i in range(M-1):
    b[i+1]=b[i]+B[i+1]
book=bisect_right(b,K)
for i in range(N):
    if a[i]>K:
        break
    j=bisect_right(b,K-a[i])
    book=max(book,(i+1)+j)
print(book)

Submission status: Accepted

Solution code:
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
n -= -1
m -= -1
a.insert(0, 0)
b.insert(0, 0)
for i in range(1, n): a[i] += a[i - 1]
for i in range(1, m): b[i] += b[i - 1]
ans = 0
for i in range(n):
    if a[i] > k: break
    lo, hi = 0, m - 1
    tmp = a[i]
    while hi - lo > 1:
        mid = lo + (hi - lo) // 2
        if tmp + b[mid] > k: hi = mid - 1
        else: lo = mid
    if tmp + b[hi] <= k: lo = hi
    ans = max(ans, i + lo)
print(ans)

Submission status: Accepted

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

import numpy as np
import bisect

nss = np.array([0] + ns).cumsum()
mss = np.array([0] + ms).cumsum()
mx = 0
for i in range(n + 1):
    if nss[i] > k:
        break
    j = bisect.bisect_right(mss, k - nss[i])
    if j > 0:
        j -= 1
    mx = max(mx, i + j)
print(mx)

Submission status: Accepted

Solution code:
import sys

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

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

nmax=0

Asum=list()
Asum.append(0)
Bsum=list()
Bsum.append(0)

imax_a = N
for i in range(N):
    s = Asum[i]+A[i]
    Asum.append(s)
    if s > K:
        imax_a = i+1
        break

imax_b = M
for i in range(M):
    s = Bsum[i]+B[i]
    Bsum.append(s)
    if s > K:
        imax_b = i+1
        break
        
for i in range(imax_a+1):
    for j in range(imax_b, -1, -1):
        time = Asum[i]+Bsum[j]
        # print("i=",i,"j=",j,"time=",time)
        if time <= K:
            imax_b = j

            if nmax < i+j:
                nmax = i+j
                # print("i=",i," j=",j," Asum",Asum[i]," Bsum", Bsum[j], " time=",time, " nmax=", nmax)

            break

print(nmax)

Submission status: Accepted

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


def binary_search(l, r, pred):
    while l < r:
        mid = (l + r) // 2
        if pred(mid):
            r = mid
        else:
            l = mid + 1
    return l


def solve(N: int, M: int, K: int, A: "List[int]", B: "List[int]"):
    ans = 0
    cA = [0] * (N + 1)
    cB = [0] * (M + 1)
    for i, a in enumerate(A):
        cA[i + 1] = cA[i] + a
    for i, b in enumerate(B):
        cB[i + 1] = cB[i] + b
    if cA[-1] + cB[-1] <= K:
        print(N + M)
        return

    def min_time(x):
        # x個選んだときの最小時間
        return min((cA[i] + cB[x - i] for i in range(N + 1) if 0 <= x - i <= M))

    ans = binary_search(0, N + M, lambda x: min_time(x) > K) - 1
    print(ans)
    return


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    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]"
    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()))

best = 0

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()))
x,y=[0],[0]
for i in range(n):
    x.append(a[i]+x[i])
for j in range(m):
    y.append(b[j]+y[j])

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

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

# print(cumA)
# print(cumB)

ans = 0
for i, s1 in enumerate(cumA):
    j = bisect_right(cumB, K - s1) - 1
    if j >= 0:
        s2 = cumB[j]
        assert s1 + s2 <= K
        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 = [0]
b = [0]
for i in range(len(A)):
    a.append(a[i] + A[i])

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


# print(a)
# print(b)

j_n = m
ans = 0

for i_n in range(len(a)):
    if a[i_n] > k:
        break

    while b[j_n] > k - a[i_n]:
        j_n -= 1

    ans = max(ans, i_n + j_n)

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])
    if a[i]+A[i]>K:
        break

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

#print(a)
#print(b)
    
ans =0
j = M

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:
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

a, b = [0], [0]
for ai in A:
    a.append(a[-1] + ai)

for bi in B:
    b.append(b[-1] + bi)

# print(a, b)
ans = 0
bs = M
for i in range(N+1):
    if a[i] > K:
        break
    for j in reversed(range(bs+1)):
        if a[i] + b[j] <= K:
            ans = max(ans, i + j)
            bs = j
            break

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(A))
BS = list(it.accumulate(B))
AS.insert(0,0)
BS.insert(0,0)
max = 0
j = M
for i in range(N+1):
    if AS[i] > K:
        break
    while i + j > max:
        if AS[i] + BS[j] <= K:
            if max < i + j:
                max = i + j
            break
        j -= 1

print(max)

Submission status: Accepted

Solution code:
from itertools import accumulate
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=list(accumulate(A))
b=list(accumulate(B))
book=bisect_right(b,K)
for i in range(N):
    if a[i]>K:
        break
    j=bisect_right(b,K-a[i])
    book=max(book,(i+1)+j)
print(book)

Submission status: Accepted

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

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

#print(a)
#print(b)
    
ans =0
j = M

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

suba=[0]
subb=[0]

for i in range(n):
    suba.append(a[i]+suba[i])
for j in range(m):
    subb.append(b[j]+subb[j])

ans=0
j=m
for i in range(n+1):
        #print(i,j)
    if k<suba[i]:
        break

    while subb[j]>k-suba[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 = [0]+list(map(int,input().split()))
K2 =K
count = 0
numa = 0
numb = 0
count2 = 0
for i in range(N+1):
    count += A[i]
    A[i] = count
#累積和
B = [0]+list(map(int,input().split()))
count = 0
for i in range(M+1):
    count += B[i]
    B[i] = count
#累積和

for i in range(N+1):#Aに対してBを探す
    K = K2
    if A[i] <= K:
        K -= A[i]
        if count2 < i + bisect.bisect_right(B, K):
            count2 = i + bisect.bisect_right(B, K)-1
    else:
        break
print(count2)

Submission status: Accepted

Solution code:
import bisect


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

    ans = 0
    A_s = [0]
    B_s = [0]
    for a in A:
        A_s.append(A_s[-1]+a)
    tmp = 0
    for b in B:
        B_s.append(B_s[-1]+b)

    ans = 0
    for i in range(0, N+1):
        if A_s[i] > K:
            continue
        tmp = K - A_s[i]
        pos = bisect.bisect(B_s, tmp) - 1 + i
        if pos > ans:
            ans = pos
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from itertools import accumulate
from bisect import bisect_left

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

result = 0
for i in range(N + 1):
    t = K - a[i]
    if t < 0:
        break
    j = bisect_left(b, t)
    if j == M + 1:
        result = max(result, i + M)
    elif b[j] == t:
        result = max(result, i + j)
    else:
        result = max(result, i + j - 1)
print(result)

Submission status: Accepted

Solution code:
import sys, math, itertools, collections, bisect 
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8') 
inf = float('inf') ;mod = 10**9+7 
mans = inf ;ans = 0 ;count = 0 ;pro = 1

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

cnt, j = 0, m#ここをfor文に入れなくていい。
for i, a_ in enumerate(A):
    d = k-a_
    t = 0
    if d < 0:
        break
    while d < B[j]:
        j-=1
    t = i+j
    cnt = max(cnt, t)
print(cnt)

Submission status: Accepted

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

nss = [0]
for i, v in enumerate(ns):
    nss.append(nss[i] + ns[i])

mss = [0]
for i, v in enumerate(ms):
    mss.append(mss[i] + ms[i])

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

    while mss[j] > k - nss[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()))
la, lb = [0], [0]
for i in range(n):
    la.append(la[i] + a[i])
for i in range(m):
    lb.append(lb[i] + b[i])
ans, j = 0, m
for i in range(n + 1):
    if la[i] > k:break
    while lb[j] > k - la[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 = [0]*(n+1)
b = [0]*(m+1)
for i in range(n):
    a[i+1] = a[i] + A[i]
    
for i in range(m):
    b[i+1] = b[i] + B[i]

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

Submission status: Accepted

Solution code:
import sys
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(A))
BS = list(it.accumulate(B))
AS.insert(0,0)
BS.insert(0,0)
max = 0
j = M
for i in range(N+1):
    if AS[i] > K:
        break
    while j >= 0:
        if AS[i] + BS[j] <= K:
            if max < i + j:
                max = i + j
            break
        j -= 1

print(max)

Submission status: Accepted

Solution code:
import random
n, m, k = map(int, input().split())
# n, m, k = random.randint(1, 10**5), random.randint(1, 10**5), random.randint(10**8, 10**9)
a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))
# a_list = [random.randint(1, 10**3) for i in range(n)]
# b_list = [random.randint(1, 10**3) for i in range(m)]
# print(n,m,k)
# print(a_list[0], b_list[0])
a_sum = [0]
b_sum = [0]
max_a = 0
for i in range(1, max(n+1, m+1)):
    if i < n+1:
        a_sum.append(a_sum[i-1] + a_list[i-1])
        if a_sum[i] <= k:
            max_a = i
    if i < m+1:
        b_sum.append(b_sum[i-1] + b_list[i-1])

ans = 0
j = m
for i in range(0, max_a+1):
    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()))
 
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
n,m,k = map(int,input().split())

a =[int(x) for x in input().split()]
b = [int(x) for x in input().split()]

prefa = [0]
prefb =[0]

for i in range(n):
    prefa.append(prefa[-1] + a[i])
for j in range(m):
    prefb.append(prefb[-1] + b[j])
i,j = 1, 1
ans =0 
#print("A")
while i <= n:
    if prefa[i] > k:
        break 
    else:
        p = k - prefa[i] 
        aux = bisect_right(prefb , p)  - 1
        #print(i, aux)
        ans = max(ans, i + aux)
        i+=1 
#print("B")
while j <= m:
    if prefb[j] > k:
        break 
    else:
        p = k - prefb[j]
        aux = bisect_right(prefa , p) -1
        #print(j , aux)
        ans = max(ans, j + aux)
        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()))
from itertools import accumulate
ar=[0]+list(accumulate(a))
bru=[0]+list(accumulate(b))
from bisect import bisect_left as bl,bisect_right as br
ans=0

for i in range(n+1):
    g=ar[i]
    if g>k:break
    if k-g>=bru[-1]:ans=max(ans,i+m);continue
    j=br(bru,k-g)
    ans=max(ans,i+j-1)
    #print(i,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()))

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

print(ans)

Submission status: Accepted

Solution code:
import random
n, m, k = map(int, input().split())
# n, m, k = random.randint(1, 10**5), random.randint(1, 10**5), random.randint(10**8, 10**9)
a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))
# a_list = [random.randint(1, 10**3) for i in range(n)]
# b_list = [random.randint(1, 10**3) for i in range(m)]
# print(n,m,k)
# print(a_list[0], b_list[0])
a_sum = [0]
b_sum = [0]
max_a = 0
for i in range(1, max(n+1, m+1)):
    if i < n+1:
        a_sum.append(a_sum[i-1] + a_list[i-1])
        if a_sum[i] <= k:
            max_a = i
    if i < m+1:
        b_sum.append(b_sum[i-1] + b_list[i-1])

ans = 0
j = m
for i in range(0, max_a+1):
    while b_sum[j] > k - a_sum[i]:
        j -= 1
    ans = max(ans, i+j)
        
print(ans)
# print(n+m)

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 = [0]
b_sum = [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())
lsta = list(map(int, input().split()))
lstb = list(map(int, input().split()))
a = [0]
b = [0]

for _ in range(n):
    a.append(lsta[_] + a[_])
    
for _ in range(m):
    b.append(lstb[_] + b[_])
    
ans, j = 0, m
for i in range(n+1):
    if a[i] > k:
        break
    while b[j] > k - a[i]:
        j -= 1
    ans = max(ans, i+j)
    
    
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
import sys

l = stdin.readline().rstrip().split(' ')
A = list(map(int, input().split()))
B = list(map(int, input().split()))
k = int(l[2])
n = int(l[0])
m = int(l[1])

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

b = [0]
for i in range(m):
    sum += B[i]
    b.append(sum)
#print(b)

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),*A,=[list(map(int,i.split())) for i in open(0)]
a=A[0]
b=A[1]

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

ans=0

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

Submission status: Accepted

Solution code:
def solve():
    a = [0]
    b = [0]
    for i in range(n):
        a.append(a[i]+deska[i])
    for j in range(m):
        b.append(b[j]+deskb[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)
    return ans

if __name__ == '__main__':
    n, m, k = map(int, input().split())
    deska = list(map(int, input().split()))
    deskb = list(map(int, input().split()))
    print(solve())

Submission status: Accepted

Solution code:
import bisect
N,M,K=list(map(int,input().split()))
A=list(map(int,input().split()))
B=list(map(int,input().split()))
bka=[0]*N
bkb=[0]*M
bka[0]=A[0]
bkb[0]=B[0]

for i in range(1,N):
    bka[i]+=bka[i-1]+A[i]
for j in range(1,M):
    bkb[j]+=bkb[j-1]+B[j]
ans=0
c=bisect.bisect_left(bkb,K)
if c != M:
    if bkb[c] == K:
        c +=+ 1
ans = max(ans, c)
for i in range(N):
    count=i+1
    k=K-bka[i]
    if k<0:continue
    c = bisect.bisect_left(bkb,k)
    b=c
    if c!=M:
        if bkb[c]==k:
            c+=count+1
        else:c+=count
    else:c+=count
    ans=max(ans,c)
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 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)