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=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]
SA=[0 for i in range(N+1)]
SB=[0 for i in range(M+1)]
for i in range(N):
    SA[i+1]=A[i]+SA[i]
for i in range(M):
    SB[i+1]=B[i]+SB[i]
ans=0
for i in range(N+1):
    if SA[i]>K:
        break
    low=0
    high=M+1
    while(high-low>1):
        mid=(low+high)//2
        #SA[i]+SB[low]<=K<SA[i]+SB[high]
        if SA[i]+SB[mid]<=K:
            low=mid
        else:
            high=mid
    ans=max(ans,i+low)
print(ans)

Submission status: Accepted

Solution code:
n,m,k,*x=map(int,open(0).read().split())
s=sum(x[:n])
z=i=j=n
x+=k,
while~i:
	while s+x[j]<=k:s+=x[j];j+=1
	if s<=k:z=max(z,i+j)
	i-=1
	s-=x[i]
print(z-n)

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:
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] + A
A = list(accumulate(A))

B = [0] + B
B = list(accumulate(B))

ans = 0
b_cnt = m
for a_cnt in range(n+1):
    if A[a_cnt] > k: continue

    while A[a_cnt]+B[b_cnt] > k:
        b_cnt -= 1
    ans = max(ans, a_cnt+b_cnt)

print(ans)

Submission status: Accepted

Solution code:
n,m,k,*x=map(int,open(0).read().split())
s=sum(x[:n])
z=i=j=n
while~i:
	while j<n+m and s+x[j]<=k:s+=x[j];j+=1
	if s<=k:z=max(z,i+j)
	i-=1
	s-=x[i]
print(z-n)

Submission status: Accepted

Solution code:
from collections import Counter
from collections import defaultdict
from collections import deque
from functools import reduce
import math
import itertools
import heapq
#import numpy as np
import bisect
import sys
#import sympy
sys.setrecursionlimit(10**6)


def bfs(s, n, node):
    # 頂点が探索済みかどうかのチェック配列
    check = [False for _ in range(n)]
    check[s] = True
    # 次見る頂点を格納するキュー
    queue = deque([s])
    visited_num = 1

    # 問題固有の配列
    #color_check=[[True for _ in range(l)] for _ in range(n)]
    color = [-1 for _ in range(n)]
    color[s] = 0

    while visited_num < n:
        # グラフが全連結ではない場合
        if len(queue) == 0:
            # 空配列を返す
            # 現状までの計算済み配列を返してもいいと思う
            return color

        now_vertex = queue.popleft()
        # インデックスがない場合は
        for next_vertex in node[now_vertex]:

            if check[next_vertex] == True:
                continue

            queue.append(next_vertex)
            check[next_vertex] = True

            # 問題固有の計算
            color[next_vertex] = color[now_vertex]+1
            visited_num += 1

    return color


mod = 10**9+7
# n=int(input())
# n,m=list(map(int,input().split()))
# a=list(map(int,input().split()))
def ceil(x, y): return (x+y-1)//y
def input_list(): return list(map(int, input().split()))


n, m, k = list(map(int, input().split()))
t_a = list(map(int, input().split()))
t_b = list(map(int, input().split()))

a = [0]
for each in t_a:
    a.append(each+a[-1])


b = [0]
for each in t_b:
    b.append(each+b[-1])

a_ind = bisect.bisect_right(a, k)
# print(a)
# print(b)
ans = 0
for i in range(a_ind):
    zan = k-a[i]
    b_ind = bisect.bisect_right(b, zan)-1
    cal = (i+b_ind)
    ans = max(ans, cal)
    #print(i, b_ind, cal)

print(ans)

Submission status: Accepted

Solution code:
# C - Tsundoku
from bisect import bisect
from itertools import accumulate


def main():
    N, M, K, *AB = map(int, open(0).read().split())
    cum_A, cum_B = accumulate(AB[:N], initial=0), tuple(accumulate(AB[N:]))
    print(max(i + bisect(cum_B, K - a) for i, a in enumerate(cum_A) if a <= K))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# C - Tsundoku
from bisect import bisect
from itertools import accumulate


def main():
    N, M, K, *AB = map(int, open(0).read().split())
    cum_A, cum_B = accumulate(AB[:N], initial=0), tuple(accumulate(AB[N:]))
    candidates = [0]
    for i, a in enumerate(cum_A):
        if a > K:
            break
        candidates.append(i + bisect(cum_B, K - a))
    print(max(candidates))


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

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

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

print(ans)

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
from bisect import*
from itertools import*
n,m,k,*x=map(int,open(0).read().split());c=accumulate;b=[*c(x[n:])];print(max(i+bisect(b,k-v)for i,v in enumerate(c([0]+x[:n]))if v<=k))

Submission status: Accepted

Solution code:
from bisect import*
from itertools import*
n,m,k,*x=map(int,open(0).read().split());c=accumulate;a=c([0]+x[:n]);b=[*c(x[n:])];print(max(i+bisect(b,k-v)for i,v in enumerate(a)if v<=k))

Submission status: Accepted

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

# print(f'{n} {m} {k}')

arr1 = [0] * (n + 1)
arr2 = [0] * (m + 1)
for i in range(0, n):
    arr1[i + 1] = arr1[i] + a[i]
for i in range(0, m):
    arr2[i + 1] = arr2[i] + b[i]

# print(arr1)
# print(arr2)

ans = 0

j = n
for i in range(m + 1):
    if k < arr2[i]:
        break
    
    # print(arr1[j])
    # print(arr2[i])
    while arr1[j] > k - arr2[i]:
        # print(j)
        # print(i)
        j -= 1
    ans = max(ans, i + j)


print(ans)

Submission status: Accepted

Solution code:
import bisect
from itertools import*
n,m,k,*x=[int(v)for l in open(0) for v in l.split()];a=accumulate([0]+x[:n]);b=[*accumulate(x[n:])];print(max(i+bisect.bisect(b,k-v)for i,v in enumerate(a)if v<=k))

Submission status: Accepted

Solution code:
from itertools import accumulate
from bisect import bisect

n, m, k = map(int, input().split())
a = accumulate(map(int, input().split()), initial=0)
b = [*accumulate(map(int, input().split()))]
print(max(i + bisect(b, k - x) for i, x in enumerate(a) if x <= k))

Submission status: Accepted

Solution code:
from itertools import accumulate
from bisect import bisect

n, m, k = map(int, input().split())
a = [*accumulate(map(int, input().split()), initial=0)]
b = [*accumulate(map(int, input().split()), initial=0)]
print(max(i - 1 + bisect(b, k - x) for i, x in enumerate(a) if x <= k))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# スペース区切りの整数の入力
N,M,K = map(int, input().split())
Nnums = list(map(int, input().split()))
Mnums = list(map(int, input().split()))

Nmax=len(Nnums)
Mmax=len(Mnums)
Nsum=0
Ncnt=0

#Nのみの場合に何冊読めるか
for Ni,Nl in enumerate(Nnums):
  Nsum += Nl
  Ncnt = Ni
  if Nsum > K:
    Nsum -= Nl
    Ncnt = Ni-1
    break
    
chkSum = Nsum
chkCnt = Ncnt + 1
AnsCnt = Ncnt + 1

#Mを組み入れて最大数を探す
for Mi,Ml in enumerate(Mnums):
  chkSum += Ml
  chkCnt += 1
  if (chkSum > K):
    flg = 0
    while chkSum > K:
      if Ncnt >= 0:
        chkSum -= Nnums[Ncnt]
        chkCnt -= 1
        Ncnt -= 1
        flg = 1
      else:
        if flg == 0:
          #chkSum -= Ml
          chkCnt -= 1
          #AnsCnt -= 1
        break
  
  if (AnsCnt < chkCnt):
    AnsCnt = chkCnt
  

  
#  if (Ncnt < 0):
  if (chkSum > K):
    break
    
print(AnsCnt)

Submission status: Accepted

Solution code:
import numpy as np
import sys
N, M, K = [int(x) for x in input().split(" ")]
A = [0] + [int(x) for x in input().split(" ")]
B = [0] + [int(x) for x in input().split(" ")]

Acum = np.cumsum(A)
Bcum = np.cumsum(B)

# iが増えるとjは非増加であることを利用して、jはM側からdecrementしていく
ans, j = 0, M
for i in range(N+1):
    a = Acum[i]
    if a > K:
        break
    while Bcum[j] > K - a:
        j -= 1
    ans = max([i + j, ans])
print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate
from bisect import bisect

n, m, k = map(int, input().split())
a = [*map(int, input().split())]
b = [*map(int, input().split())]
ca = [*accumulate(a, initial=0)]
cb = [*accumulate(b, initial=0)]
print(max(i - 1 + bisect(cb, k - x) for i, x in enumerate(ca) if x <= k))

Submission status: Accepted

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

import io
import sys
import math


def solve(n,m,k,a_lst,b_lst):
    #机ごと、読書時間の累積をリストにする。ただし、リスト化はk時間内で読める範囲とする
    aa_lst = make_sum_list(a_lst,k)
    bb_lst = make_sum_list(b_lst,k)
    len_aa = len(aa_lst)
    len_bb = len(bb_lst)

    #A,Bいずれかが一冊も読めないケースを考慮
    if len_aa == 0:   return len_bb
    elif len_bb == 0: return len_aa
    
    #全て読めるケースを考慮
    if aa_lst[-1] + bb_lst[-1] <= k: return len_aa + len_bb 
    
    #A,Bいずれかのみ読むケースを考慮
    max_read = max(len_aa,len_bb)

    #リスト範囲でA+Bの最大値を探索する。2分探索を用いる
    for i in reversed(range(len_aa)):
        limit = k - aa_lst[i]
        read = i + 1
        if read + len_bb + 1 <= max_read:
            #以後 read は増えない
            break

        imax = len_bb-1
        imin = 0
        imid = len_bb // 2
        while True:
            if _DEB: logd(f"imid: {imid}")

            if bb_lst[imid] <= limit:
                #OK。大を調べる
                if imax - imid <= 1:
                    #探査終了
                    if bb_lst[imax] <= limit:
                        read += imax + 1
                    else:
                        read += imid + 1
                    break
                imin = imid
            else:
                #NG。小を調べる
                if imid - imin <= 1:
                    #探査完了
                    if bb_lst[imin] <= limit:
                        read += imin + 1
                    break
                imax = imid
            imid = (imin + imax) // 2
        max_read = max(read,max_read)
    return max_read

def make_sum_list(lst,max):
    sum_lst = []
    sum = 0
    for n in lst:
        sum += n
        if sum > max:
            break
        sum_lst.append(sum)
    return sum_lst

def main():
    n,m,k = map(int, input().split())
    a_lst = list(map(int, input().split()))
    b_lst = list(map(int, input().split()))
    ans = solve(n,m,k,a_lst,b_lst)
    print(ans)

### DEBUG I/O ###
_INPUT = """\
3 4 240
60 90 120
80 150 80 150
"""
_DEB = 0

def logd(str):
    if _DEB:
        print(f"[deb] {str}")

### MAIN ###
if __name__ == "__main__":
    if _DEB:
        sys.stdin = io.StringIO(_INPUT)
        print("!! Debug Mode !!")

    main()

Submission status: Accepted

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

import io
import sys
import math


def solve(n,m,k,a_lst,b_lst):
    #机ごと、読書時間の累積をリストにする。ただし、リスト化はk時間内で読める範囲とする
    aa_lst = make_sum_list(a_lst,k)
    bb_lst = make_sum_list(b_lst,k)
    len_aa = len(aa_lst)
    len_bb = len(bb_lst)

    #A,Bいずれかが一冊も読めないケースを考慮
    if len_aa == 0:   return len_bb
    elif len_bb == 0: return len_aa
    
    #全て読めるケースを考慮
    if aa_lst[-1] + bb_lst[-1] <= k: return len_aa + len_bb 
    
    #A,Bいずれかのみ読むケースを考慮
    max_read = max(len_aa,len_bb)

    #リスト範囲でA+Bの最大値を探索する。2分探索を用いる
    for i in reversed(range(len_aa)):
        limit = k - aa_lst[i]
        read = i + 1
        if read + len_bb + 1 <= max_read:
            #以後 read は増えない
            break

        imax = len_bb-1
        imin = 0
        imid = len_bb // 2
        while True:
            logd(f"imid: {imid}")

            if bb_lst[imid] <= limit:
                #OK。大を調べる
                if imax - imid <= 1:
                    #探査終了
                    if bb_lst[imax] <= limit:
                        read += imax + 1
                    else:
                        read += imid + 1
                    break
                imin = imid
            else:
                #NG。小を調べる
                if imid - imin <= 1:
                    #探査完了
                    if bb_lst[imin] <= limit:
                        read += imin + 1
                    break
                imax = imid
            imid = (imin + imax) // 2
        max_read = max(read,max_read)
    return max_read

def make_sum_list(lst,max):
    sum_lst = []
    sum = 0
    for n in lst:
        sum += n
        if sum > max:
            break
        sum_lst.append(sum)
    return sum_lst

def main():
    n,m,k = map(int, input().split())
    a_lst = list(map(int, input().split()))
    b_lst = list(map(int, input().split()))
    ans = solve(n,m,k,a_lst,b_lst)
    print(ans)

### DEBUG I/O ###
_INPUT = """\
3 4 240
60 90 120
80 150 80 150
"""
_DEBUG = 0

def logd(str):
    if _DEBUG:
        print(f"[deb] {str}")

### MAIN ###
if __name__ == "__main__":
    if _DEBUG:
        sys.stdin = io.StringIO(_INPUT)
        print("!! Debug Mode !!")

    main()

Submission status: Accepted

Solution code:
from bisect import bisect_right
from sys import stdin, stdout

int_in = lambda: int(stdin.readline())
arr_in = lambda: [int(x) for x in stdin.readline().split()]
mat_in = lambda rows: [arr_in() for _ in range(rows)]
str_in = lambda: stdin.readline().strip()
out = lambda o: stdout.write("{}\n".format(o))
arr_out = lambda o: out(" ".join(map(str, o)))
bool_out = lambda o: out("YES" if o else "NO")
tests = lambda: range(1, int_in() + 1)
case_out = lambda i, o: out("Case #{}: {}".format(i, o))


def solve(n, m, k, a, b):
    prefix_sum_a = [0]
    for i in range(len(a)):
        prefix_sum_a.append(a[i] + prefix_sum_a[-1])
    prefix_sum_b = [b[0]]
    for i in range(1, len(b)):
        prefix_sum_b.append(b[i] + prefix_sum_b[-1])

    pa = bisect_right(prefix_sum_a, k) - 1
    result = 0
    while pa >= 0:
        left = k - prefix_sum_a[pa]
        pb = bisect_right(prefix_sum_b, left)
        result = max(result, pa + pb)
        pa -= 1
    return result


if __name__ == "__main__":
    n, m, k = arr_in()
    a = arr_in()
    b = arr_in()
    out(solve(n, m, k, a, b))

Submission status: Accepted

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

sumA=sum(arr)
bpointer=0
ans=0
sumB=0
for i in range(len(arr),-1,-1):
    if i<n:
        sumA-=arr[i]
    if sumA<=k:
        while bpointer<m and sumA+sumB+brr[bpointer]<=k:
            sumB+=brr[bpointer]
            bpointer+=1
        ans=max(ans,i+bpointer)
print(ans)

Submission status: Accepted

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

stackA = [int(x) for x in input().split()]
stackB = [int(x) for x in input().split()]

indexB = 0
indexA = 0

books = 0
mostBooks = 0
time = 0

for x in stackB:
    if time + x > K:
        break

    else:
        time += x
        indexB += 1
        books += 1

indexB -= 1

mostBooks = books

for x in stackA:
    time += x
    books += 1

    while time > K and indexB >= 0:
        time -= stackB[indexB]
        indexB -= 1
        books -= 1

    if indexB < 0 and time > K:
        break

    mostBooks = max(mostBooks, books)

print(mostBooks)

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()]
ReadA = [0]*(N+1)
ReadB = [0]*(M+1)
for TA in range(0,N):
    ReadA[TA+1] = ReadA[TA]+A[TA]
for TB in range(0,M):
    ReadB[TB+1] = ReadB[TB]+B[TB]
MAX = 0
MAXTB = M
for TA in range(0,N+1):
    if ReadA[TA]>K:
        break
    else:
        for TB in range(MAXTB,-1,-1):
            if ReadA[TA]+ReadB[TB]<=K:
                MAXTB = TB
                if MAX<TA+TB:
                    MAX = TA+TB
                break
print(MAX)

Submission status: Accepted

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

import io
import sys
import math


def solve(n,m,k,a_lst,b_lst):
    #机ごと、読書時間の累積をリストにする。ただし、リスト化はk時間内で読める範囲とする
    aa_lst = make_sum_list(a_lst,k)
    bb_lst = make_sum_list(b_lst,k)
    len_aa = len(aa_lst)
    len_bb = len(bb_lst)

    #A,Bいずれかが一冊も読めないケースを考慮
    if len_aa == 0:   return len_bb
    elif len_bb == 0: return len_aa
    
    #全て読めるケースを考慮
    if aa_lst[-1] + bb_lst[-1] <= k: return len_aa + len_bb 
    
    #A,Bいずれかのみ読むケースを考慮
    max_read = max(len_aa,len_bb)

    #リスト範囲でA+Bの最大値を探索する。2分探索を用いる
    for i in reversed(range(len_aa)):
        limit = k - aa_lst[i]
        read = i + 1
        if read + len_bb + 1 <= max_read:
            #以後 read は増えない
            break

        imax = len_bb-1
        imin = 0
        imid = len_bb // 2
        while True:
            if bb_lst[imid] <= limit:
                #OK。大を調べる
                if imax - imid <= 1:
                    #探査終了
                    if bb_lst[imax] <= limit:
                        read += imax + 1
                    else:
                        read += imid + 1
                    break
                imin = imid
            else:
                #NG。小を調べる
                if imid - imin <= 1:
                    #探査完了
                    if bb_lst[imin] <= limit:
                        read += imin + 1
                    break
                imax = imid
            imid = (imin + imax) // 2
        max_read = max(read,max_read)
    return max_read

def make_sum_list(lst,max):
    sum_lst = []
    sum = 0
    for n in lst:
        sum += n
        if sum > max:
            break
        sum_lst.append(sum)
    return sum_lst

def main():
    n,m,k = map(int, input().split())
    a_lst = list(map(int, input().split()))
    b_lst = list(map(int, input().split()))
    ans = solve(n,m,k,a_lst,b_lst)
    print(ans)

### DEBUG I/O ###
_INPUT = """\
3 4 730
60 90 120
80 150 80 150
"""
_DEBUG = 0

if __name__ == "__main__":
    if _DEBUG:
        sys.stdin = io.StringIO(_INPUT)
        print("!! Debug Mode !!")
    main()

Submission status: Accepted

Solution code:
from bisect import *
n, m, k = map(int, input().split())
A = [*map(int, input().split())]
B = [*map(int, input().split())]
tot = sum(B)
ans, j = 0, m
for i in range(n+1):
    while j > 0 and tot > k:
        j -= 1
        tot -= B[j]
    if tot > k: break
    if i < n: tot += A[i]
    ans = max(ans, i+j)
print(ans)

Submission status: Accepted

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

def judge(x):
    for i in range(x+1):
        if i<=N and x-i<=M and accA[i]+accB[x-i]<=K:
            return True
    
    return False

def binary_search():
    l, r = 0, N+M
    
    while l<=r:
        m = (l+r)//2
        
        if judge(m):
            l = m+1
        else:
            r = m-1
    
    return r

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

for Ai in A:
    accA.append(accA[-1]+Ai)

accB = [0]

for Bi in B:
    accB.append(accB[-1]+Bi)

print(binary_search())

Submission status: Accepted

Solution code:
import itertools
import bisect

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

Ac = list(itertools.accumulate(A))

iA = bisect.bisect_right(Ac,K)

A_ok = Ac[:iA][::-1]+[0]
Alen = len(A_ok)

ans = 0
SUM = 0
cnt = 0
for i in range(Alen):
    K2 = K - A_ok[i]
    k = cnt
    for j in range(M-k):
        if SUM + B[j+k] <= K2:
            cnt += 1
            SUM += B[j+k]
        else:
            break
    ans = max(ans,(Alen-i-1+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()))
arr1, arr2 = [0], [0]
for i in range(n):
	arr1.append(arr1[i] + A[i])
for i in range(m):
	arr2.append(arr2[i] + B[i])

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

Submission status: Accepted

Solution code:
import bisect
from itertools import accumulate
n,m,k=map(int,input().split())
a=list(accumulate(map(int,input().split())))
b=list(accumulate(map(int,input().split())))
ans=0
j=bisect.bisect_right(b,k)
ans=max(ans,j)
for i,x in enumerate(a):
    y=k-x
    if y>0:
        j=bisect.bisect_right(b,y)
        ans=max(ans,i+j+1)
i=bisect.bisect_right(a,k)
ans=max(ans,i)
for j,y in enumerate(b):
    x=k-y
    if x>0:
        i=bisect.bisect_right(a,x)
        ans=max(ans,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()))
i=0
mx=0
lol=0
while i<n and a[i]<=k:
    k-=a[i]
    i+=1
    mx+=1
    lol=1
j=0
while j<m and b[j]<=k:
    k-=b[j]
    j+=1
    mx+=1
cou=mx

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

Submission status: Accepted

Solution code:
from bisect import bisect_right

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

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

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

b_cnt = M

ans = 0
for a_cnt in range(N+1):
    if A[a_cnt] > K:
        continue

    rest = K - A[a_cnt]

    while A[a_cnt] + B[b_cnt] > K:
        b_cnt -= 1

    ans = max(ans, a_cnt + b_cnt)

print(ans)

Submission status: Accepted

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

a=[0]
b=[0]

for i in range(N):
    a.append(a[i]+A[i])
for i in range(M):
    b.append(b[i]+B[i])
j=M
ans=0
for i in range(N+1):#aは小さい方から
    if a[i]>K:    #a[0]=0なのでいきなりbreakすることはない
        break
    while b[j]>K-a[i] : #bは大きい方から
        j-=1            #j,ansの値は次のループに引き継がれる
    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()))
ac = [0] + list(accumulate(a))
bc = [0] + list(accumulate(b))

ans = 0
for i in range(bisect.bisect(ac, k)):
    bi = bisect.bisect(bc, k - ac[i])
    ans = max(ans, i+bi-1)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)

N, M, K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
Asum = [0]
Bsum = [0]
count = 0
for p in A:
    count += p
    Asum.append(count)
count = 0
for p in B:
    count += p
    Bsum.append(count)
best = 0
Blast = M
for t in range(N+1):
    if Asum[t] > K:
        break
    if best < t:
        best=t
    for s in reversed(range(0,Blast+1)):
        if Asum[t]+Bsum[s]<=K:
            if best < t+s:
                best=t+s
            Blast = s
            break
print(best)

Submission status: Accepted

Solution code:
N,M,K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
import bisect
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=0
for i in range(N+1):
    if A_sum[i]>K:
        continue
    b_read=bisect.bisect_right(B_sum,K-A_sum[i])-1
    ans=max(ans,i+b_read)
print(ans)

Submission status: Accepted

Solution code:
n, m, k = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
a = [0]
b = [0]
for i in range(n):
    a.append(a[i] + A[i])
for i in range(m):
    b.append(b[i] + B[i])
ans = 0
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:
"""
AtCoder :: Beginner Contest 172 :: C :: Tsundoku
https://atcoder.jp/contests/abc172/tasks/abc172_c

"""
import sys
from collections import deque


def solve(A, B, K):
    """Solve puzzle."""
    time_read = 0

    # Read as many books as you can from A
    a_books_read = []
    while A:
        if A[0] + time_read <= K:
            time_read += A[0]
            a_books_read.append(A[0])
            A.popleft()
        else:
            break

    # Then read as many books as you can from B
    b_books_read = []
    while B:
        if B[0] + time_read <= K:
            b_books_read.append(B[0])
            time_read += B[0]
            B.popleft()
        else:
            break

    max_books = len(a_books_read) + len(b_books_read)

    # One at a time, put the last read book from A back and
    # see how many more books from B you can read.
    while a_books_read:
        time_read -= a_books_read[-1]
        a_books_read.pop()
        while B:
            if B[0] + time_read <= K:
                b_books_read.append(B[0])
                time_read += B[0]
                B.popleft()
            else:
                break
        max_books = max(max_books, len(a_books_read) + len(b_books_read))

    return max_books


def main():
    """Main program."""
    N, M, K = (int(i) for i in sys.stdin.readline().split())
    A = deque(int(i) for i in sys.stdin.readline().strip().split())
    B = deque(int(i) for i in sys.stdin.readline().strip().split())
    print(solve(A, B, K))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math

def solve(n,m,k,a_lst,b_lst):
    #机ごと、読書時間の累積をリストにする。ただし、リスト化はk時間内で読める範囲とする
    aa_lst = make_sum_list(a_lst,k)
    bb_lst = make_sum_list(b_lst,k)
    len_aa = len(aa_lst)
    len_bb = len(bb_lst)

    #A,Bいずれかが一冊も読めないケースを考慮
    if len_aa == 0:   return len_bb
    elif len_bb == 0: return len_aa
    
    #全て読めるケースを考慮
    if aa_lst[-1] + bb_lst[-1] <= k: return len_aa + len_bb 
    
    #A,Bいずれかのみ読むケースを考慮
    max_read = max(len_aa,len_bb)

    #リスト範囲でA+Bの最大値を探索する。2分探索を用いる
    for i in reversed(range(len_aa)):
        limit = k - aa_lst[i]
        read = i + 1
        if read + len_bb + 1 <= max_read:
            #以後 read は増えない
            break

        imax = len_bb-1
        imin = 0
        imid = len_bb // 2
        while True:
            if bb_lst[imid] <= limit:
                #OK。大を調べる
                if imax - imid <= 1:
                    #探査終了
                    if bb_lst[imax] <= limit:
                        read += imax + 1
                    else:
                        read += imid + 1
                    break
                imin = imid
            else:
                #NG。小を調べる
                if imid - imin <= 1:
                    #探査完了
                    if bb_lst[imin] <= limit:
                        read += imin + 1
                    break
                imax = imid
            imid = (imin + imax) // 2
        max_read = max(read,max_read)
    return max_read

def make_sum_list(lst,max):
    sum_lst = []
    sum = 0
    for n in lst:
        sum += n
        if sum > max:
            break
        sum_lst.append(sum)
    return sum_lst

n,m,k = map(int, input().split())
a_lst = list(map(int, input().split()))
b_lst = list(map(int, input().split()))
ans = solve(n,m,k,a_lst,b_lst)
print(ans)

Submission status: Accepted

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

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

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

print(count)