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

sum_a = [0]
sum_b = [0]
ans = 0
for i in range(1,len(A)+1):
    sum_a.append(sum_a[i - 1] + A[i - 1])
for i in range(1,len(B)+1):
    sum_b.append(sum_b[i - 1] + B[i - 1])


j = len(sum_b)-1
for i in range(len(sum_a)):
    if sum_a[i] > K: break
    while (sum_b[j] + sum_a[i] > K):
        j -= 1
    ans = max(ans, i + j)

    
print(ans)

Submission status: Accepted

Solution code:
import sys, math
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
mod = 10**9 + 7

N, M, K = rl()

A = rl()
B = rl()

s = 0
j = 0
for i, b in enumerate(B):
	if s+b > K:
		break
	s += b
	j += 1

ans = j
j -= 1
for i, a in enumerate(A):
	s += a
	while j >= 0 and s > K :
		s -= B[j]
		j -= 1
	if s > K: break
	ans = max(ans, i+1+j+1)
print(ans)

Submission status: Accepted

Solution code:
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:
import itertools as it, bisect as bs
f=lambda:map(int,input().split())
g=lambda:[0]+[*it.accumulate(f())]
n,m,k=f()
A,B=g(),g()
a,j=0,m
for i in range(n+1):
  t=bs.bisect(B,k-A[i])
  if t: a=max(a,i+t-1)
print(a)

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())
n+=1
A=list(map(int,input().split()))
A = [0]+A
B= list(map(int,input().split()))
S = sum(B)
ind = m-1
data=[0]*n
tmp = 0
for i in range(n):
  tmp += A[i]
  while tmp + S > k and ind>=-1:
    S -= B[ind]
    ind-=1
  if ind == -2:data[i] = -inf
  else:data[i] = ind+1

for i in range(n):
  ans = max(ans,i+data[i])
    
print(ans)
# print(data)

Submission status: Accepted

Solution code:
# 使うやつだけimportに変更
from bisect import bisect_left, bisect_right

N, M, K = map(int, input().split())
AA = list(map(int, input().split()))
BB = list(map(int, input().split()))
# CC、DDがKを超えない範囲の
# 初期条件をinfi=10**20に。
CC = [10 ** 20] * len(AA)
DD = [10 ** 20] * len(BB)
CC[0] = AA[0]
#
for i in range(N - 1):
    CC[i + 1] = CC[i] + AA[i + 1]
    if CC[i + 1] >= K:
        break

ans = max(0, bisect_right(CC, K))
DD[0] = BB[0]
if DD[0] <= K:
    ans = max(ans, bisect_right(CC, K - DD[0]) + 1)
for i in range(M - 1):
    DD[i + 1] = DD[i] + BB[i + 1]
    if DD[i + 1] <= K:
        ans = max(ans, bisect_right(CC, K - DD[i + 1]) + i + 2)
    else:
        break
print(ans)

Submission status: Accepted

Solution code:
import bisect

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:
from itertools import accumulate
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)

asum = list(accumulate(a))
bsum = list(accumulate(b))

ans = 0
for i, v in enumerate(asum):
    if v > k:
        break
    index = bisect.bisect_right(bsum, k-v)
    ans = max(ans, i + index-1)
print(ans)

Submission status: Accepted

Solution code:
import sys
import bisect
import itertools
import collections
import fractions
import heapq
import math
from operator import mul
from functools import reduce
from functools import lru_cache


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    N, M, K = map(int, readline().split())
    A = list(map(int, readline().split()))
    B = list(map(int, readline().split()))


    nowtime = 0

    Asum = [0]
    Bsum = [0]
    tmp = 0
    for a in A:
        tmp += a
        Asum.append(tmp)
    tmp = 0
    for b in B:
        tmp += b
        Bsum.append(tmp)

    maxbooks = 0
    before = M
    bcount = M
    for acount in range(N+1):
        if Asum[acount] > K:
            continue
        while K < Asum[acount] + Bsum[bcount]:
            bcount -= 1
        maxbooks = max(maxbooks, acount+bcount)

    print(maxbooks)



if __name__ == '__main__':
    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=[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
  while b[j] > K - a[i]:
    j -= 1
  ans = max(ans, i + j)
print(ans)

Submission status: Accepted

Solution code:
#!usr/bin/env pypy3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)

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

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

    ans = 0

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

    print(ans)

main()

Submission status: Accepted

Solution code:
def resolve():
  N, M, K = map(int, input().split(" "))
  A = [int(x) for x in input().split(" ")]
  B = [int(x) for x in input().split(" ")]

  if A[0] > K and B[0] > K:
    print(0)
    return True 

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

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

  ans = 0
  B_index = M
  for A_index in range(N + 1):
    if integraA[A_index] > K:
      break

    # print(A_index, ":", B_index)
    # print(integraB[B_index], "<", K - integraA[A_index])
    while integraB[B_index] > K - integraA[A_index]:
      B_index -= 1
    ans = max(ans, A_index + B_index)
  print(ans)

if __name__ == "__main__":
  resolve()

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]
atemp = 0
btemp = 0
for i in range(N):
    atemp += A[i]
    Asum.append(atemp)
for i in range(M):
    btemp += B[i]
    Bsum.append(btemp)

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

print(ans)

Submission status: Accepted

Solution code:
def bs(L, key):
    inc, exc = -1, len(L)
    while abs(exc-inc) > 1:
        mid = (exc+inc)//2
        if L[mid] <= key:
            inc = mid
        else:
            exc = mid
    return inc


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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
#import numpy as np
#from collections import defaultdict
import math
#from collections import deque

#input = sys.stdin.readline
#import numpy as n
import bisect



def main():

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

    A = list(map(int,input().split()))
    B = list(map(int, input().split()))
    cumA=[0]*(len(A)+1)
    cumB = [0] * (len(B)+1)
    tempA=0
    for i in range(1,len(A)+1):
        cumA[i]=tempA+A[i-1]
        tempA+= A[i-1]
    tempB = 0
    for i in range(1,len(B)+1):
        cumB[i] = tempB + B[i-1]
        tempB+= B[i-1]
#    print(cumB)
    ans =0
    j = M
    for i in range(len(A)+1):
        res = K - cumA[i]
        if res<0:
            break
        while cumB[j] + cumA[i] > K:
            j -=1
        ans = max(ans, i + j)
 #       b_read = bisect.bisect_right(cumB,res)
#        ans =max( i + b_read,ans)
 #       print("b_read {}// i {}//ans {}".format(b_read,i,ans))

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import bisect

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

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

book_cnt=0
for a_cnt in range(n+1):
  if a[a_cnt]>k:
    continue
  rest=k-a[a_cnt]
  b_cnt=bisect.bisect(b,rest)-1
  book_cnt=max(book_cnt,a_cnt+b_cnt)

print(book_cnt)

Submission status: Accepted

Solution code:
import sys, os, math, bisect, itertools, collections, heapq, queue
# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from decimal import Decimal
from collections import defaultdict, deque

# import fractions

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)
# lcm = lambda x, y: (x * y) // fractions.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N, M, K = il()
    # 累積和
    A = list(itertools.accumulate([0] + il()))
    B = list(itertools.accumulate(([0] + il())))

    ret = 0
    for n in range(N + 1):
        if A[n] > K:
            break

        # 二分探索
        b = bisect.bisect_right(B, K - A[n])
        ret = max(ret, n + b - 1)
    print(ret)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import bisect

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
for i in range(n+1):
  if acum1[i]>k:
    break
  j=bisect.bisect_right(acum2,k-acum1[i])-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 = np.array([0] + list(map(int, input().split())),dtype=np.int64)
b = np.array([0] + list(map(int, input().split())),dtype=np.int64)

asum = a.cumsum()
bsum = b.cumsum()

ans = 0
for ind,i in enumerate(asum):
    if i>K:
        break
    mx = np.searchsorted(bsum, K-i ,side='right') - 1
    ans = max(ans, ind + mx,)

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でどこまで行けるか。
Acount=N
Ajikan=0
for i in range(N):
    Ajikan += A[i]
    if Ajikan > K:
        Acount=i
        Ajikan=Ajikan-A[i]
        break
#print('Acount,Ajikan:', Acount,Ajikan)

jikan=0
Bcount=0
count=0
j=0
life = K - Ajikan
while Acount>=0 and Bcount<=M :
    if j>=M:
        break

    elif B[j] <= life:
        count = max(Acount+Bcount, count)
        Bcount+=1
        life -= B[j]
        j+=1

    else:
        count = max(Acount+Bcount, count)
        if Acount<=0:
            break
        life += A[Acount-1]
        Acount-=1
    count = max(Acount+Bcount, count)

print(count)

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

def ruisekiwa(List):
    for i in range(1, len(List)):
        List[i] += List[i - 1]
    return List

arui = ruisekiwa(a)
brui = ruisekiwa(b)
arui.insert(0, 0)
brui.insert(0, 0)
ans = 0

for i in range(n + 1):
    if arui[i] > k:
        break
    j = bisect_right(brui, k - arui[i])
    ans = max(ans, i + j - 1)
print(ans)

Submission status: Accepted

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

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

ans, j = 0, M
for i in range(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:
from bisect import bisect_right
from itertools import accumulate

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

a_book = [0] + list(accumulate([int(x) for x in input().split()]))
b_book = [0] + list(accumulate([int(x) for x in input().split()]))

check = {}

max_books = 0

for i, a in enumerate(a_book):
    if a > K:
        break

    b_pos = bisect_right(b_book, K - a)

    if max_books < b_pos - 1 + i:
        max_books = b_pos - 1 + i

print(max_books)

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()))
Acc=list(accumulate(B))
ans=0
time=0
a_cnt=0
for a in A:
    remain=k-time
    if remain<0:
        break
    b_cnt=bisect_right(Acc,remain)
    ans=max(ans,a_cnt+b_cnt)
    time+=a
    a_cnt+=1
remain=k-time
if remain>=0:
    b_cnt=bisect_right(Acc,remain)
    ans=max(ans,a_cnt+b_cnt)
print(ans)

Submission status: Accepted

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

asum=[0]
bsum=[0]

for i in range(n):
  asum.append(asum[i]+A[i])
  
for j in range(m):
  bsum.append(bsum[j]+B[j])

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

Submission status: Accepted

Solution code:
n,m,k=[int(x) for x in input().rstrip().split()]
a=[int(x) for x in input().rstrip().split()]
b=[int(x) for x in input().rstrip().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=0

for i in range(n+1):
  # j=0
  if A[i]>k:
    break
  nokori=k-A[i]

  while(B[j]<=nokori and j<m):
    j+=1

  if nokori<B[j]:
    j-=1
    
  ans=max(ans,i+j)
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_right


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

    CUSUM_A = [0] * (N+1)
    CUSUM_B = [0] * (M+1)

    for i, a in enumerate(A):
        CUSUM_A[i+1] = CUSUM_A[i] + a

    for i, b in enumerate(B):
        CUSUM_B[i+1] = CUSUM_B[i] + b


    ans = 0
    for i, a in enumerate(CUSUM_A):
        rest = K - a
        if rest < 0:
            continue

        j = bisect_right(CUSUM_B, rest) - 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()))
t = 0
sum_a = [0]
for i in range(n):
  if t + a[i] <= k:
    t += a[i]
    sum_a.append(t)
  else:
    break
sum_b = [0]
t = 0
for i in range(m):
  if t + b[i] <= k:
    t += b[i]
    sum_b.append(t)
  else:
    break
ans = []
j = 0
for i in range(len(sum_a)):
  while sum_a[i] + sum_b[len(sum_b) - j - 1] > k:
    j += 1
  ans.append(i + len(sum_b) - j - 1)
print(max(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 # ans=0, j=M
for i in range(N+1):
    if a[i]>K:
        break
    # Aがi冊の時、最大でBがj冊読める
    # Aが0冊(0分)、1冊(60分)、2冊(150分)の時、...
    # Bが4冊(460分)×、3冊(310分)×、2冊(230分)←Aが0冊なら○、...
    # => i:0 j:2(計2冊), i:1 j:1(計2冊), i:2 j:1(計3冊(最大)),
    #    i=3はKより大きくなりbreakとなる
    while b[j]>K-a[i]:
        j-=1
    # 今までのループでi+j冊の最大値を求める
    ans=max(ans, i + j)
print(ans)

Submission status: Accepted

Solution code:
n,m,k=[int(x) for x in input().rstrip().split()]
a=[int(x) for x in input().rstrip().split()]
b=[int(x) for x in input().rstrip().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):
  # j=m
  if A[i]>k:
    break
  nokori=k-A[i]

  while(nokori<B[j] and 0<=j):
    j-=1

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

Submission status: Accepted

Solution code:
import copy

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

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


ans = 0
nb = copy.deepcopy(m)
for na in range(n+1):
    res = k - asum[na]
    if res < 0:
        break
    flg = True
    while flg:
        if bsum[nb] > res:
            nb -= 1
            continue
        ans = max(ans, na+nb)
        break
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_right
import numpy as np
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())))

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

cumsum_A = A.cumsum()
cumsum_B = B.cumsum()
# print('cusmum_A', cumsum_A)
# print('cusmum_B', cumsum_B)

ans = 0
for num_A in range(len(cumsum_A)):
    t = cumsum_A[num_A]
    if t > K:
        continue
    num_B = bisect_right(cumsum_B, K - t) - 1
    # print('num_A, num_B', num_A, num_B)
    ans = max(ans, num_A + num_B)
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]+a
for i in range(1,n+1):
    a[i] += a[i-1]
b = [0]+b
for i in range(1,m+1):
    b[i] += b[i-1]

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:
# 尺取法ver

n, m, k = list(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):
  if k < a[i] + asum[i]:
    break
  asum.append(a[i] + asum[i])
for i in range(m):
  if k < b[i] + bsum[i]:
    break
  bsum.append(b[i] + bsum[i])

ans = 0
b_count = len(bsum)-1
for a_count in range(len(asum)):
  while asum[a_count] + bsum[b_count] > k:
    b_count -= 1
  ans = max(ans, a_count + b_count)
print(ans)

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

asum, bsum = [0], [0]
for i in range(n):
  if k < a[i] + asum[i]:
    break
  asum.append(a[i] + asum[i])
for i in range(m):
  if k < b[i] + bsum[i]:
    break
  bsum.append(b[i] + bsum[i])

ans = 0
for i in range(len(asum)):
  ans = max(ans, i + bisect.bisect(bsum, k-asum[i]) - 1)
print(ans)

Submission status: Accepted

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

a_sum=[0]
for i in range(N):
    a_sum.append(a_sum[-1]+As[i])

b_sum=[0]
for i in range(M):
    b_sum.append(b_sum[-1]+Bs[i])

j=M
ans=0
for i in range(N+1):
    if a_sum[i] > K:
        break

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

Submission status: Accepted

Solution code:

from itertools import accumulate

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

cost = 0
cnt = 0

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

ans = 0

b_cnt = m

for a_cnt in range(n+1):
    if a[a_cnt] > k:
        continue

    while b[b_cnt] > k - a[a_cnt]:
        b_cnt -= 1

    ans = max(ans, a_cnt + b_cnt)

print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate
from bisect import bisect_right
def solve():
    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))

    ans = 0
    for i in range(N+1):
        if a[i] > K:
            break
        b_max = K - a[i]
        cnt = i + bisect_right(b,b_max) - 1
       
        ans = max(ans,cnt)
        
    print(ans)
    
if __name__ == '__main__':
    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=0,j=M
ans, j = 0, M

#Aの本を0冊読むとき、Bは何冊読めるか → N冊まで繰り返す
for i in range(N+1):
    if K<a[i]: break
    while b[j]>K-a[i]:
        j-=1
    ans = max(ans,i+j)
print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate

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

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:
from bisect import bisect_right
from time import time

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

a_book = [0]
for index, i in enumerate(input().split(), 1):
    if a_book[index-1] + int(i) <= K:
        a_book.append(a_book[index-1] + int(i))
    else:
        N = index - 1
        break

b_book = [0]
for index, i in enumerate(input().split(), 1):
    if b_book[index-1] + int(i) <= K:
        b_book.append(b_book[index-1] + int(i))
    else:
        M = index - 1
        break

check = {}

max_books = 0

for i, a in enumerate(a_book):
    b_pos = bisect_right(b_book, K - a)

    if max_books < b_pos - 1 + i:
        max_books = b_pos - 1 + i

print(max_books)

Submission status: Accepted

Solution code:
n, m, k = map(int, input().split())
*A, = map(int, input().split())
*B, = map(int, input().split())
SA, SB = [0], [0]
for i in range(n):
    SA.append(SA[i] + A[i])
for j in range(m):
    SB.append(SB[j] + B[j])
ans, tmp = 0, m
for i in range(n + 1):
    if SA[i] > k:
        break
    for j in range(tmp + 1)[::-1]:
        if SA[i] + SB[j] <= k:
            break
    ans = max(ans, i + j)
    tmp = j
print(ans)

Submission status: Accepted

Solution code:
import bisect

def cumsum(a):
   r = [0]
   for v in a:
      r.append(r[-1] + v)
   return r

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

# 累積和をとる
a_cumsum = cumsum(a)
b_cumsum = cumsum(b)

ans = 0

# a_numはindex、vの部分aの値（中味）
for a_num, v in enumerate(a_cumsum):
    tmp = 0
    if k >= v:
        b_num = bisect.bisect_right(b_cumsum, k-v) -1
        # -1は「0」先頭に入れてるからね
        tmp = a_num + b_num
    ans = max(ans,tmp)

print(ans)

Submission status: Accepted

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

from collections import deque

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

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

accum_a = [0]
accum_b = [0]

for a_i in range(0, len(an)):
    a_i = accum_a[-1] + an[a_i]
    if a_i > k:
        break
    accum_a.append(a_i)

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

ans = 0
for a_i in range(len(accum_a)):

    sumCandidate = accum_a[a_i]
    rest = k - sumCandidate

    j = (len(accum_b)) // 2
    finalBIndex = j
    left = -1
    right = len(accum_b)
    while right - left > 1:
        mid = left + (right - left) // 2

        if accum_b[mid] > rest:
            right = mid
        else:
            left = mid

    ans = max(ans, a_i + right - 1)

print(ans)

Submission status: Accepted

Solution code:
n, m, k = map(int, input().split())
a_list = list(map(int, input().strip().split()))
b_list = list(map(int, input().strip().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 sys, os, math, bisect, itertools, collections, heapq, queue
# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from decimal import Decimal
from collections import defaultdict, deque

# import fractions

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)
# lcm = lambda x, y: (x * y) // fractions.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N, M, K = il()
    # 累積和
    A = list(itertools.accumulate([0] + il()))
    B = list(itertools.accumulate(([0] + il())))

    # 尺取り法
    ret, m = 0, M
    for n in range(N + 1):
        if A[n] > K:
            break

        while B[m] > K - A[n]:
            m -= 1
        ret = max(ret, n + m)
    print(ret)


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=[0]+A
B=[0]+B
import numpy as np
a=np.cumsum(A)
b=np.cumsum(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:
#C
from itertools import accumulate
from collections import deque
import bisect

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

cnt = 0

a_c = list(accumulate(list(a)))
b_c = list(accumulate(list(b)))
result = []

for i in range(n+1):
    if a_c[i] > k:
        continue
    b_idx = bisect.bisect_left(b_c,k-a_c[i]+1)
    result.append(i+b_idx-1)
    
print(max(result))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
import bisect
def main():
    N, M, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    cum_a = [0] * (N + 1)
    for i in range(N):
        cum_a[i+1] = cum_a[i] + A[i]
    cum_b = [0] * (M + 1)
    for i in range(M):
        cum_b[i+1] = cum_b[i] + B[i]
    ans = 0
    for i in range(N+1):
        rest = K - cum_a[i]
        if rest < 0:break
        b_idx = bisect.bisect_right(cum_b, rest)
        ans = max(ans, i + b_idx - 1)
    print(ans)
if __name__ == "__main__":
    main()

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

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

asum, bsum = [0], [0]
for i in range(n):
  if k < a[i] + asum[i]:
    break
  asum.append(a[i] + asum[i])
for i in range(m):
  if k < b[i] + bsum[i]:
    break
  bsum.append(b[i] + bsum[i])

ans = 0
for i in range(len(asum)):
  tmp = i + bisect.bisect(bsum, k-asum[i]) - 1
  if ans < tmp:
    ans = tmp
print(ans)