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=input().split()
N=int(N)
M=int(M)
K=int(K)
tmp=input().split()
A=[]
Atime=[]
for i in range(len(tmp)):
  A.append(int(tmp[i]))
  if i==0: Atime.append(A[0])
  else: Atime.append(Atime[i-1]+A[i])
tmp=input().split()
B=[]
Btime=[]
for i in range(len(tmp)):
  B.append(int(tmp[i]))
  if i==0: Btime.append(B[0])
  else: Btime.append(Btime[i-1]+B[i])

Blim=len(Btime)
for i in range(len(Btime)):
  if Btime[i]>K:
    Blim=i
    break
result=Blim
#print(Atime)
for i in range(len(Atime)):
  while Blim>0:
    if Atime[i]+Btime[Blim-1]<=K:
      if Blim+i+1>result: result=Blim+i+1
      break
    Blim-=1
  if Blim==0:
    if Atime[i]<=K:
      if i+1>result: result=i+1
print(result)

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()

def main():
    n, m, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    ans = 0
    a_time = [0]
    b_time = [0]
    for i in range(1, len(a) + 1):
        a_time.append(a_time[i - 1] + a[i - 1])
    for i in range(1, len(b) + 1):
        b_time.append(b_time[i - 1] + b[i - 1])

    a_max = len(a_time) - 1
    b_max = len(b_time) - 1
    for i in range(1, len(a_time)):
        if a_time[i] > k:
            a_max = i - 1
            break
    for i in range(1, len(b_time)):
        if b_time[i] > k:
            b_max = i - 1
            break

    for i in range(len(b_time)):
        if a_time[a_max] + b_time[i] <= k:
            ans = max(ans, a_max + i)
        else:
            if a_max > 0:
                a_max -= 1
    for i in range(len(a_time)):
        if b_time[b_max] + a_time[i] <= k:
            ans = max(ans, b_max + i)
        else:
            if b_max > 0:
                b_max -= 1

    print(ans)

if __name__ == '__main__':
    main()

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_= [0]
for a in A:
  A_.append(A_[-1] + a)
#print(A_)

b_sum = 0
ans = 0
b_cnt = 0

while True:
  if b_sum > K:
    break
  else:
    k = K - b_sum
    ans = max(bisect_right(A_, k)-1 + b_cnt, ans)
    #print(bisect_right(A_, k)-1 + b_cnt, ans,  b_cnt)
    if b_cnt == M:
      break
    b_sum += B[b_cnt]
    b_cnt += 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()))

cnt=0

#Aのみ読んだ場合を考える
for a in A:
  if K >= a:
    K-=a
    cnt+=1
  else:
    break

maxcnt=cnt
A=A[: cnt ]
#print(A,K)
for i,b in enumerate(B):
  #aの要素なくしてbを入れる
  #bがいれれるまでaをなくすよ
  while b>K and len(A)!=0:
    a = A.pop()
    K += a
    cnt -= 1
    #print('-a,',a,K)
    
  if len(A) == 0:
    _B=B.copy()[i:]
   # print(_B)
    _B.pop(0)
    #まだbが入るかもしれない…！
    while(K>=b):
      K-=b
      cnt+=1
      #print('+b(while),',b,K,cnt)
      if len(_B) == 0:
        break
      b=_B.pop(0)
    if maxcnt < cnt:
      maxcnt=cnt
    break

  K -= b
  cnt+=1
  #print('+b,',b,K,cnt)
  
  if maxcnt < cnt:
    maxcnt=cnt



print(maxcnt)

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]
A.extend(a)
B.extend(b)
time = sum(A)
ai = len(A)-1
bi = 0
con = 0
flg = None
count = 0

    

while True:
    #print("---------")
    #print(str(ai)+":"+str(bi))
    if time > K:
        if flg is not None:
            con = max(con,flg-1)
            flg = None
        
        if ai == 0:
            break
        time -=A[ai]
        ai -= 1
    else:
        #print("b")
        flg = ai+bi
        time +=B[bi]
        bi += 1
        if bi == len(B):
            if time > K:
                if flg is not None:
                    con = max(con, flg-1)
                pass
            else:
                con = max(con,ai+bi-1)
            break
    #print(flg)
print(con)

Submission status: Accepted

Solution code:
from itertools import accumulate
from bisect import bisect_right

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

ans = 0
m = [0] + list(accumulate(A))
l = list(accumulate(B))
for i, x in enumerate(m):
    if x > K:
        break        
    else:
        ans = max(ans, i + bisect_right(l, K-x))
print(ans)

Submission status: Accepted

Solution code:
import sys
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
prog=0
a_idx=0
b_idx=0

while a_idx<n:
    if a[a_idx]+prog>k:
        break
    prog+=a[a_idx]
    a_idx+=1

if a_idx==n:
    while b_idx<m:
        if b[b_idx]+prog>k:
            break
        prog+=b[b_idx]
        b_idx+=1

mx=a_idx+b_idx

if mx==n+m:
    print(mx)
    sys.exit()

for i in range(a_idx+1):
    while b_idx<m and b[b_idx]+prog<=k:
        prog+=b[b_idx]
        b_idx+=1
    if a_idx+b_idx>mx:
        mx=a_idx+b_idx
    if b_idx==m:
        break
    if a_idx==0:
        break
    a_idx-=1
    prog-=a[a_idx]

print(mx)

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

# N, M, K = [1, 1, 10]
# A = [5]
# B = [5]

tmp = 0
for i in range(N):
    A[i], tmp = A[i] + tmp, tmp + A[i]
A = [0] + A

tmp = 0
for i in range(M):
    B[i], tmp = B[i] + tmp, tmp + B[i]
B = [0] + B

re = 0
for i, element in enumerate(A):
    K_tmp = K - element
    if K_tmp < 0:
        pass
    else:
        idx_B = bisect.bisect_right(B, K_tmp)
        re_tmp = i  + idx_B - 1
        re = max(re, re_tmp)
print(re)

Submission status: Accepted

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

MAX = 10 ** 18

# 累積和
accA, accB = [0] * (n + 1), [0] * (m + 1)
accA[1], accB[1] = A[0], B[0]
for i in range(2, n + 1):
    accA[i] = accA[i - 1] + A[i - 1]
for i in range(2, m + 1):
    accB[i] = accB[i - 1] + B[i - 1]
accA.append(MAX)
accB.append(MAX)

from bisect import bisect_left, bisect_right

res = 0
for i in range(n + 1):
    tmp = accA[i]
    idx = bisect_right(accB, k - tmp) - 1
    if tmp + accB[idx] <= k:
        res = max(res, i + idx)
print(res)

Submission status: Accepted

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

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

waB = [0]
for j in range(m):
    waB.append(waB[-1] + b[j])

j = 0
mx = 0
for i in reversed(range(n + 1)):
    while j != m + 1 and waA[i] + waB[j] <= k:
        if mx < i + j:
            mx = i + j
        j += 1

print(mx)

Submission status: Accepted

Solution code:
import math
import bisect
#map(int,input().split())
#int(x) for x in input().split()
#for _ in range(int(input())):
n,m,k=map(int,input().split())
a=[int(x) for x in input().split()]
b=[int(x) for x in input().split()]
pa=[0]*n
pb=[0]*m
pa[0]=a[0]
pb[0]=b[0]
for i in range(1,n):
	pa[i]=pa[i-1]+a[i]
for i in range(1,m):
	pb[i]=pb[i-1]+b[i]

ans=0
for i in range(n):
	if pa[i]<=k:
		p=bisect.bisect_right(pb,k-pa[i])
		ans=max(ans,i+1+p)

p=bisect.bisect_right(pb,k)
ans=max(ans,p)
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

from itertools import accumulate

n, m, k = map(int, input().split())
a = [0]
for i in accumulate(map(int, input().split())):
    if i <= k:
        a.append(i)
b = [0]
for i in accumulate(map(int, input().split())):
    if i <= k:
        b.append(i)

ans = 0
a_index = 0
b_index = len(b) - 1
for a_index in range(len(a)):
    while a[a_index] + b[b_index] > k:
        b_index -= 1
        if b_index < 0:
            break
    if b_index < 0:
        break
    else:
        ans = max(ans, a_index + b_index)
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()))
ans = 0
amax = 0
samax = 0

for i in range(n):
    if samax + a[i]<=k:
        amax += 1
        samax += a[i]
    else:
        break
        
book = amax
ans = book
samax = sum(a[0:amax])
sbmax = 0

for j in range(m):
    sbmax += b[j]
    while samax + sbmax > k and amax >= 1:
        amax -= 1
        samax = samax - a[amax]
        book -= 1
    if samax + sbmax <= k:
        book += 1
        if book > ans:
            ans = book
    else:
        break

print(ans)

Submission status: Accepted

Solution code:
n,m,k = map(int, input().split())
la = list(map(int, input().split()))
lb = list(map(int, input().split()))
lt = []
x = 0
a_max = 0
t = 0
for i in range(n):
  t += la[i]
  if t > k:
    a_max = i-1
    t -= la[i]
    x = i
    lt.append(x)
    break
  if i == n-1:
    x = n
    a_max = n-1
    lt.append(x)

j = 0
for i in range(m):
  t += lb[i]
  x += 1
  if t > k:
    lt.append(x-1)
    if j <= a_max:
      t -= la[a_max-j]
      x -= 1
      j += 1
    else:
      break
  if i == m-1:
    lt.append(x)

print(max(lt))

Submission status: Accepted

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


lim = n+m
i = 0
cnt_a = 0
loop = len(a)
while k > 0 and i < loop:
    if k - a[i] >= 0:
        k -= a[i]
        i += 1
        cnt_a += 1
    else:
        break

i = 0
cnt_b = 0
loop = len(b)
while k > 0 and i < loop:
    if k - b[i] >= 0:
        k -= b[i]
        i += 1
        cnt_b += 1
    else:
        break

cnt = cnt_a + cnt_b
max_cnt = cnt

for j in range(cnt_a-1,-1,-1):
    if max_cnt == lim:
        break
    cnt -= 1
    k += a[j]
    while k > 0 and i < loop:
        if k - b[i] >= 0:
            k -= b[i]
            i += 1
            cnt += 1
        else:
            break
    if max_cnt < cnt:
        max_cnt = cnt

print(max_cnt)

Submission status: Accepted

Solution code:
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
s=0
pa,pb=[0,a[0]],[0,b[0]]
for i in range(1,n):
    pa.append(pa[-1]+a[i])
for i in range(1,m):
    pb.append(pb[-1]+b[i])
ac,bc=0,0
x,y=0,0
while ac<=n and pa[ac]<=k:
    ac+=1
    x=1

if x: ac-=1

while bc<=m and  pa[ac]+pb[bc]<=k:
    bc+=1
    y=1
if y: bc-=1
ans=ac+bc

for i in range(ac-1,-1,-1):
    for j in range(bc+1,m+1):
        if pa[i]+pb[j]>k: break
        bc=j
        ans=max(ans,i+j)
print(ans)

Submission status: Accepted

Solution code:
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
s = 0
b_count = 0
for i in range(m):
    if s + b[i] <= k:
        s += b[i]
        ans += 1
        b_count = i+1
    else:
        break
a_count = 0
while True:
    if a_count < n and s+a[a_count] <= k:
        s += a[a_count]
        a_count += 1
        ans += 1
    else:
        break
now = ans
for i in range(b_count-1, -1, -1):
    s -= b[i]
    now -= 1
    while True:
        if a_count < n and s +a[a_count] <= k:
            s += a[a_count]
            a_count += 1
            now += 1
        else:
            break
    ans = max(now, ans)
print(ans)

Submission status: Accepted

Solution code:
from collections import deque

N, M, K = 3, 4, 240
ARR = [60, 90, 120]
BRR = [80, 150, 80, 150]
#
# N, M, K = 3, 4, 730
# ARR = [60, 90, 120]
# BRR = [80, 150, 80, 150]

N, M, K = 5, 4, 1
ARR = [1000000000, 1000000000, 1000000000, 1000000000, 1000000000]
BRR = [1000000000, 1000000000, 1000000000, 1000000000]

N, M, K = 7, 4, 14
ARR = [1, 6, 1, 1, 1, 7, 2]
BRR = [5, 1, 5, 6, 7]


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


def calculate(n, m, k, arr, brr):
    q = deque()

    result = []
    total = 0

    for i in range(n):
        if (total + arr[i]) > k:
            break
        total += arr[i]
        q.append(arr[i])

    result.append(len(q))

    remain = k - total

    q2 = deque(brr)
    sss = 0

    while (len(q2) > 0) and ((sss + q2[0]) <= remain):
        v = q2.popleft()
        sss += v

    result.append(len(q) + (m - len(q2)))

    while len(q) > 0:
        right = q.pop()
        remain += right

        while (len(q2) > 0) and ((sss + q2[0]) <= remain):
            v = q2.popleft()
            sss += v

        result.append(len(q) + (m - len(q2)))

    print(max(result))


calculate(N, M, K, ARR, BRR)

Submission status: Accepted

Solution code:
import sys
from itertools import accumulate
import bisect

input = lambda: sys.stdin.readline().rstrip()
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
# 累積和を利用
a_accum = list(accumulate(a))
b_accum = list(accumulate(b))

# print(a_accum, b_accum)

ans = 0
for i in range(len(a_accum)):
    nokori = k - a_accum[i]
    if nokori < 0:
        break
    # にぶたん
    b_pos = bisect.bisect(b_accum, nokori) - 1
    ans = max(ans, i + b_pos)
    # print(nokori,ans,b_pos)
print(ans)

Submission status: Accepted

Solution code:
def count_books(books, limit):
    num_books = len(books)
    total_time = 0
    for i, time in enumerate(books):
        if total_time + time > limit:
            num_books = i
            break
        total_time += time
    return [num_books, total_time]

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

    max_num_A, total_time_A = count_books(A, K)

    read_book_nums = [0] * (max_num_A + 1)

    remain = K - total_time_A
    num_B, total_time_B = count_books(B, remain)
    read_book_nums[-1] = max_num_A + num_B

    num_A = max_num_A
    m = 0
    total_time_B = 0
    for n in range(max_num_A-1, -1, -1):
        num_A -= 1
        remain += A[n]

        while m < M and total_time_B + B[m] <= remain:
            total_time_B += B[m]
            m += 1
        num_B = m

        read_book_nums[n] = num_A + num_B

    print(max(read_book_nums))
    
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()))

at = [0]
for i in range(len(A)):
    at.append(A[i])
    if i > 0:
        at[-1] += at[i]

bt = [0]
for i in range(len(B)):
    bt.append(B[i])
    if i > 0:
        bt[-1] += bt[i]


n = 0
time = 0


for i, a in enumerate(at):
    if a > K:
        break
    l = 0
    r = len(bt)-1
    while r - l > 1:
        m = (l+r)//2
        if bt[m] < K-a:
            l = m
        else:
            r = m
    if bt[r] <= K -a:
        b = bt[r]
    else:
        r -= 1
        b = bt[r]
    if i + r >= n:
        n = i + r

print(n)

Submission status: Accepted

Solution code:
# import sys
# import math #sqrt,gcd,pi
# import decimal
# import queue # queue
# import bisect
# import heapq # priolity-queue
# import time
# from itertools import product,permutations,\
#     combinations,combinations_with_replacement
# 重複あり順列、順列、組み合わせ、重複あり組み合わせ
# import collections # deque
# from operator import itemgetter,mul
# from fractions import Fraction
# from functools import reduce

mod = int(1e9+7)
# mod = 998244353
INF = 1<<50

def readInt():
    return list(map(int,input().split()))

def main():
    n,m,k = readInt()
    a = readInt()
    b = readInt()
    a_sum = [0]
    for i in range(n):
        a_sum.append(a_sum[i] + a[i])
    b_sum = [0]
    for i in range(m):
        b_sum.append(b_sum[i] + b[i])
    b_sum.append(INF)
    ans = 0
    for i in range(n+1):
        t = a_sum[i]
        l = 0
        r = m+2
        while l+1<r:
            c = (l + r) // 2
            if t+b_sum[c]<=k:
                l = c
            else:
                r = c
        if a_sum[i]+b_sum[l]<=k:
            ans = max(ans,i+l)
    print(ans)
    return

if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
import bisect


def m():
    N, M, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    ans = 0
    AR = [0] * (N + 1)
    BR = [0] * (M + 1)
    for i in range(N):
        AR[i + 1] = AR[i] + A[i]
    for i in range(M):
        BR[i + 1] = BR[i] + B[i]

    ai = bisect.bisect_right(AR, K)
    for a in range(ai-1, -1, -1):
        bk = K - AR[a]
        bi = bisect.bisect_right(BR, bk)

        ans = max(a + bi - 1, ans)
    return ans


print(m())

Submission status: Accepted

Solution code:
from itertools import accumulate


def binary(N, T):  # 二分探索 # N:探索要素数
    l, r = -1, N + 1
    while r - l > 1:
        if T < b[(l + r) // 2]:  # 条件式を代入
            r = (l + r) // 2
        else:
            l = (l + r) // 2
    return r - 1


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

ans = 0
for i, ai in enumerate(a):
    t = k - ai
    if t >= 0:
        j = binary(m, t)
        ans = max(ans, i + j)
print(ans)

Submission status: Accepted

Solution code:
import sys
import itertools
import bisect

N, M, K = map(int, input().split())
A = list(map(int, sys.stdin.readline().rsplit()))
B = list(map(int, sys.stdin.readline().rsplit()))

sA = [0] + list(itertools.accumulate(A))
sB = [0] + list(itertools.accumulate(B))

res = 0
for i, a in enumerate(sA):
    if K < a:
        break
    j = bisect.bisect_right(sB, K - a) - 1
    res = max(res, i + j)

print(res)

Submission status: Accepted

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

from itertools import accumulate
from bisect import bisect_right
aacc = list(accumulate(As))
bacc = list(accumulate(Bs))

# print(aacc)
ans = 0
for i in range(n):
    a = aacc[i]
    if k - a < 0:continue
    # print(a, k-a)
    n = bisect_right(bacc, k-a)
    # print("index a",i+1, "b",n)
    ans = max(i+1+n,ans)
# 逆も然り(めんどいので)
for i in range(m):
    a = bacc[i]
    if k - a < 0:continue
    n = bisect_right(aacc, k-a)
    ans = max(i+1+n,ans)


print(ans)

Submission status: Accepted

Solution code:
#!python3

import sys
iim = lambda: map(int, sys.stdin.readline().rstrip().split())

def resolve():
    it = map(int, sys.stdin.read().split())
    N, M, K = next(it), next(it), next(it)

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

    lo = 0
    hi = 2*(N+M)+1
    while lo < hi:
        mid = (lo+hi)//2
        for j in range(max(0, mid-M), min(mid, N)+1):
            if A[j] + B[mid-j] <= K:
                lo = mid+1
                break
        else:
            hi = mid

    print(lo-1)


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()))
t = 0
num = 0
bt = 0
bi = 0
for i in range(n):
    t += a[i]
    if t > k:
        t -= a[i]
        break
    else:
        num += 1

for j in range(m):
    t += b[j]
    if t > k:
        t -= b[j]
        break
    else:
        bt += b[j]
        bi += 1

inum = num
num += bi
cnum = num

for i in range(inum):
    t -= a[inum - i - 1]
    cnum -= 1
    while bi < m:
        t += b[bi]
        if t > k:
            t -= b[bi]
            break
        else:
            bi += 1
            cnum += 1
    if num < cnum:
        num = cnum
print(num)

Submission status: Accepted

Solution code:
import itertools

N, M, K = map(int, input().split())
a = list(input().split())
b = list(input().split())
A = list(itertools.accumulate([int(a[i]) for i in range(N)]))
B = list(itertools.accumulate([int(b[i]) for i in range(M)]))

count = 0
tmp = K

for i in range(N+1):
    low = 0
    high = M - 1
    while low <= high:
        mid = int((low + high) // 2)
        guess = B[mid]
        if low == high or guess == tmp:
            break
        elif guess > tmp:
            high = mid - 1
        else:
            low = mid + 1
    count = max(count, (i+mid+1 if B[mid] <= tmp else i+mid))
    if i == N:
        break
    else:
        tmp = K - A[i]
        if tmp < 0:
            break

print(count)

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, gcd, sqrt
from operator import mul
from functools import reduce
from operator import mul
from pprint import pprint



sys.setrecursionlimit(2147483647)
INF = 10 ** 20
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
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)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, m, k = LI()
A = LI()
B = LI()
a_acc = [0] + list(accumulate(A))
b_acc = [0] + list(accumulate(B))
ans = 0
for i in range(n + 1):
    if a_acc[i] > k:
        continue
    idx = bisect_right(b_acc, k - a_acc[i])
    ans = max(ans, i + idx - 1)

print(ans)

Submission status: Accepted

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

import itertools
al_su = list(itertools.accumulate(al))
bl_su = list(itertools.accumulate(bl))

import bisect
ka = bisect.bisect(al_su,k)

kc = 0

for i in range(ka):
    kk = k - al_su[i]
    kb = bisect.bisect(bl_su,kk)
    kc = max(kc,i+1+kb)

kb = bisect.bisect(bl_su,k)

for j in range(kb):
    kk = k - bl_su[j]
    ka = bisect.bisect(al_su,kk)
    kc = max(kc,j+1+ka)

print(kc)

Submission status: Accepted

Solution code:
from collections import deque
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
ex=0
inda=-1
t=0
for i in range(len(a)):
    ex+=a[i]
    if ex<=k:
        inda=i
        t+=a[i]
    else:
        break
if inda!=-1:
    ans=inda+1
else:
    ans=0
aq=deque(a[:inda+1])
bq=deque(b)
now=ans
while len(aq)!=0 and len(bq)!=0:
    if t+bq[0]<=k:
        t+=bq.popleft()
        now+=1
        ans=max(ans,now)
    else:
        t+=bq.popleft()
        t-=aq.pop()
if len(bq)==0:
    print(ans)
else:
    while len(bq)!=0:
        t+=bq.popleft()
        if t<=k:
            now+=1
            ans=max(ans,now)
        else:
            break
    print(ans)

Submission status: Accepted

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

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

b.append(k+100)
cnt = 0
ans1, ans2 = 0, 0
for i in range(n-1):
    a[i+1] += a[i]
    if a[i+1] <= k:
        ans1 = i+2
for j in range(m-1):
    b[j+1] += b[j]
    if b[j+1] <= k:
        ans2 = j+2
# print(a, b)
ans = max(ans1, ans2)
for i in range(n):
    nokori = k-a[i]
    r, l = 0, m
    if nokori < 0:
        break
    if b[0] > nokori:
        ans = max(i+1, ans)
        continue

    while(l-r > 1):
        if b[(r+l)//2] <= nokori:
            r = (r+l)//2
        else:
            l = (r+l)//2
        # print(l, r)
    ans = max(i+l+1, ans)
    # print(i, r, l, nokori)
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_time = [0]
time = 0
for i in range(N):
    time += A[i]
    A_time.append(time)
B_time = [0]
time = 0
for i in range(M):
    time += B[i]
    B_time.append(time)

def binarySearch(key):
    left = -1
    right = len(B_time)
    
    while right - left > 1:
        mid = left + (right - left)//2
        if B_time[mid] > key:
            right = mid
        else:
            left = mid
    return left

Max = 0
for i in range(len(A_time)):
    j = binarySearch(K - A_time[i])
    Max = max(Max, i+j)
    if j < 0:
        break

print(Max)

Submission status: Accepted

Solution code:
#!/usr/bin python3
# -*- coding: utf-8 -*-

from bisect import bisect_left, bisect_right

def main():
    N, M, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(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]
    ret = 0
    for i in range(N):
        if A[i]<=K:
            l = bisect_right(B, K-A[i])    # 5が入るべき境目のうち最も右側の境目を返す
            ret = max(ret,i+l+1)
    ret = max(ret,bisect_right(B, K))
    print(ret)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
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())))

cA = np.cumsum(A)
cB = np.cumsum(B)
rcA = (K - cA)[::-1]
ind = N - np.searchsorted(rcA,cB,side='left')
cand = np.arange(M+1) + ind
cand *= (ind >= 0)
print(np.max(cand))

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をi冊読む
sum_a = [0]*n
sum_b = [0]*m
sum_a[0] = a[0]
sum_b[0] = b[0]
for i in range(1, n):
    sum_a[i] = sum_a[i-1]+a[i]
for i in range(1, m):
    sum_b[i] = sum_b[i-1]+b[i]

def binary_search(list, item):
    low = 0
    high = len(list) - 1
    if list[high] <= item:
        return high+1
    if list[low] > item:
        return low
    while True:
        mid = (low + high) //2
        guess = list[mid]
        if high - low == 1:
            return high
        if guess > item:
            high = mid
        else:
            low = mid

ans = 0
for i in range(n):
    tmp_ans = i+1
    tmp_k = k-sum_a[i]
    # print(i, tmp_k)
    if tmp_k < 0:
        pass
    else:
        b_index = binary_search(sum_b, tmp_k)
        tmp_ans += b_index
        # print('a', i, b_index)
        if tmp_ans > ans:
            ans = tmp_ans

tmp_ans = 0
if sum_b[0] <= k:
    tmp_ans = binary_search(sum_b, k)
if tmp_ans > ans:
    ans = tmp_ans
print(ans)

Submission status: Accepted

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

    A_ac = [0]
    B_ac = [0]
    a_ac = 0
    b_ac = 0
    for a in A:
        a_ac+=a
        A_ac.append(a_ac)
    for b in B:
        b_ac+=b
        B_ac.append(b_ac)
    
    i_mx = bisect.bisect_right(A_ac,k)
    i = i_mx-1
    ct = [0]

    while i >= 0:
        k_left = k-A_ac[i]
        j = bisect.bisect_right(B_ac,k_left)-1
        ct.append(i+j)
        i-=1
    

    print(max(ct))
main()

Submission status: Accepted

Solution code:
import sys

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

ls = 0
la = 0
for i in range(n):
    ls += a[i]
    la = i+1
    if(ls > k):
        ls -= a[i]
        la -= 1
        break

samu = ls
ans = la

lb = 0

for j in range(m):
    samu += b[lb]
    lb += 1
    while(True):
        if(samu <= k):
            break
        if(la == 0 and samu > k):
            print(ans)
            sys.exit()
        samu -= a[la-1]
        la -= 1
    if(la+lb > ans):
        ans = la + lb

print(ans)

Submission status: Accepted

Solution code:
N, M , K = map(int, input().split())
X = list(map(int, input().split()))
Y = list(map(int, input().split()))
t = 0
R = 0
tmp = 0
for i in range(M):
    if tmp + Y[i] > K:
        break
    R += 1
    tmp += Y[i]

sY = sum(Y)
j = M
for i in range(N):
    t += X[i]
    if t > K:
        break
    while j:
        if sY + t <= K:
            break
        j-=1
        sY -= Y[j]
    R = max(R, j+i+1)
print(R)

Submission status: Accepted

Solution code:

from itertools import *
from bisect import bisect_right
n,m,time = 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(bisect_right(A, time)-1,-1,-1):
    ans = max(ans,i+bisect_right(B, time-A[i])-1)
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_right as br
n,m,k = map(int,input().split())
a = [int(i) for i in input().split()]
sa = [0]*(n+1)
for i in range(n): sa[i+1] = sa[i]+a[i]
b = [int(i) for i in input().split()]
sb = [0]*(m+1)
for i in range(m): sb[i+1] = sb[i]+b[i]
ans = 0
for i in range(n+1):
    if sa[i] > k: break
    ans = max(ans,br(sb,k-sa[i])-1+i)
print(ans)

Submission status: Accepted

Solution code:
import bisect
n, m, k = map(int, input().split())
aList = list(map(int, input().split()))
bList = list(map(int, input().split()))

cList = [0]
dList = [0]
for a in aList:
    t = a + cList[-1]
    if t <= k:
        cList.append(a + cList[-1])
    else:
        break
for b in bList:
    t = b + dList[-1]
    if t <= k:
        dList.append(b + dList[-1])
    else:
        break
cList.pop(0)
dList.pop(0)
n = len(cList)
m = len(dList)

if len(cList) == 0:
    print(bisect.bisect_right(dList, k))
else:
    a = 0
    for i in range(0, n+1):
        if i == n:
            lim = k
        else:
            lim = k - cList[n-1-i]
        a = max(a, n - i + bisect.bisect_right(dList, lim))
    print(a)