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:

from collections import deque



# for #!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase


# region fastio

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
from bisect import bisect_left as bl
def main():

    # n = int(input())
    # l = [int(j) for j in input().split()]
    
# 3 4 240
# 60 90 120
# 80 150 80 150

    # for t in range(int(input())):

    n,m,k = [int(j) for j in input().split()]
    l1 = [0]+[int(j) for j in input().split()]
    l2 = [0]+[int(j) for j in input().split()]

    # pre = [0]*n
    i = 0
    j = 0
    for i in range(1,n+1):
        l1[i] += l1[i-1]
    for i in range(1,m+1):
        l2[i] += l2[i-1]

    # print(l1, l2)
    ans =0 
    for i in range(n+1):
        if l1[i]>k: 
            break
        x = bl(l2, k-l1[i])
        if x<=m and x>0:
            if l2[x]>k-l1[i]:
                x = x-1
        if x==m+1:
            x = m
        # print(x,i)
        ans = max(ans, x+i)
    for i in range(m+1):
        if l2[i]>k: 
            break
        x = bl(l1, k-l2[i])
        if x<=n and x>0:
            if l1[x]>k-l2[i]:
                x = x-1
        if x==n+1:
            x = n
        # print(x,i)
        ans = max(ans, x+i)
    print(ans)






# endregion

if __name__ == "__main__":
    main()

Submission status: Accepted

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


AA = [0]
BB = [0] 

x = 0
for a in A:
    x += a
    AA.append(x)

x = 0
for b in B:
    x += b
    BB.append(x)

tmp = 0
for i in range(N+1):
    a = AA[i]
    if bisect.bisect_right(BB,K -a)-1 >= 0:
        tmp = max( tmp ,  bisect.bisect_right(BB,K -a)-1 + i)

print(tmp)

Submission status: Accepted

Solution code:
import bisect

n, m , k = [int(x) for x in input().strip().split()]

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

pa = []
pb = []
s=0
for e in a:
    s+=e
    pa.append(s)

s=0
for e in b:
    s+=e
    pb.append(s)

res=0
for i in range(n):
    elem = k-pa[i]
    if elem<0:
        break
    ind = bisect.bisect_right(pb,elem)
    # print(ind, i)
    res = max(res, ind+i+1)

for i in range(m):
    elem = k-pb[i]
    if elem<0:
        break
    ind = bisect.bisect_right(pa,elem)
    # print(ind, i)
    res = max(res, ind+i+1)

ind = bisect.bisect_right(pa,k)
res = max(res, ind)

ind = bisect.bisect_right(pb,k)
res = max(res, ind)

print(res)

Submission status: Accepted

Solution code:
import bisect

n,m,k = map(int,input().split())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]

a_li = [0]*(n+1)
b_li = [0]*(m+1)

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

ans = 0
#print(b_li)
#print(a_li)
for i in range(n+1):
    #print(i)
    tmp_k = k - a_li[i]
    if tmp_k < 0:
        break
    idx = bisect.bisect_right(b_li, tmp_k)
    #print(tmp_k,idx,idx,idx)
    #print(b_li[idx])
    #if idx <= 1:
    #    continue
    ans = max(ans,i+idx-1)
    #print(ans)
    #print(k,idx,b_li[idx-1])
    
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

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

cum_a = a.cumsum()
cum_b = b.cumsum()

maximum = 0
j = 0
for i in range(n, -1, -1):
  rest = k - cum_a[i]
  if rest < 0:
    continue
  while rest - cum_b[j] >= 0:
    j += 1
    if j > m:
      break
  j -= 1
  maximum = max(maximum, i + j)
print(maximum)

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()))
s = 0
maxbooks = 0
for i in range(n):
    if s + a[i] <= k:
        s += a[i]
        continue
    else:
        i -= 1
        break
for j in range(m):
    if s + b[j] <= k:
        s += b[j]
        continue
    else:
        j -= 1
        break
maxbooks = i + j + 2
nowbooks = i + j + 2
#i, j: 読めたもの
while i >= 0 and j < m:
    s -= a[i]
    i -= 1
    nowbooks -= 1
    while j + 1 < m:
        if s + b[j+1] <= k:
            s += b[j+1]
            j += 1
            nowbooks += 1
            if nowbooks > maxbooks:
                maxbooks = nowbooks
            
            continue
        else:
            break
last = 0
s = 0
for j in range(m):
    if s + b[j] <= k:
        s += b[j]
        continue
    else:
        j -= 1
        break
if j > maxbooks:
    maxbooks = j
print(maxbooks)

Submission status: Accepted

Solution code:
import bisect as bc

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

A_S = [0]*(N+2)
B_S = [0]*(M+2)

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

ans = 0
for i in range(N+2):
    tmp_K = K-A_S[i]
    if tmp_K > 0:
        j = bc.bisect_right(B_S,tmp_K) - 1
        ans = max(i+j,ans)
    if tmp_K == 0:
        ans = max(ans,i)
    
print(ans)

Submission status: Accepted

Solution code:
import sys
import heapq
import math
import fractions
import bisect
import itertools
from collections import Counter
from collections import deque
from operator import itemgetter
def input(): return sys.stdin.readline().strip()
def mp(): return map(int,input().split())
def lmp(): return list(map(int,input().split()))

n,m,k=mp()
A=lmp()
B=lmp()
a=[A[0]]
b=[B[0]]
for i in range(1,n):
    a.append(a[i-1]+A[i])
for i in range(1,m):
    b.append(b[i-1]+B[i])
a.insert(0,0)
b.insert(0,0)
a.append(float("inf"))
b.append(float("inf"))
ans=0

ind=bisect.bisect_right(a,k)
j=0
ans=ind-1
for i in range(ind-1,-1,-1):
    while a[i]+b[j]>k or a[i]+b[j+1]<=k:
        j+=1
    ans=max(ans,i+j)
print(ans)

Submission status: Accepted

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

used=0
a_read=0
for i in range(N):
    if used+A[i]>K:
        break
    else:
        used+=A[i]
        a_read+=1

b_read=0
for i in range(M):
    if used+B[i]>K:
        break
    else:
        used+=B[i]
        b_read+=1
ans=a_read+b_read
tmp=ans

for i in range(a_read):
    if b_read==M:
        break
    used-=A[a_read-1]
    a_read-=1
    tmp-=1
    while True:
        if b_read>=M:
            break
        if used+B[b_read]<=K:
            b_read+=1
            used+=B[b_read-1]
            tmp+=1
        else:
            break
    ans=max(ans,tmp)

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
for i in range(1, N+1):
  A[i] += A[i-1]
for i in range(1, M+1):
  B[i] += B[i-1]
start = 0
end = N+1
while end-start > 1:
  sep = (start+end)//2
  if A[sep] <= K:
    start = sep
  else:
    end = sep
if start==N:
  alim = start
elif A[end]>K:
  alim = start
else:
  alim = end
blim = 0
start = blim
end = M+1
ans = alim
ansalim = -1
ansblim = -1
for _ in range(alim+1):
  Bmax = K-A[alim]
  #print("Bmax" , Bmax)
  while end-start > 1:
    sep = (start+end)//2
    #print(blim,start, end, sep)
    if B[sep] <= Bmax:
      start = sep
    else:
      end = sep
  if start==M:
    blim = start
  elif B[end]>Bmax:
    blim = start
  else:
    blim = end
  
  ans = max(blim + alim, ans)
  start = sep-1
  end = M+1
  alim -= 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()))

ca = [0]
cb = [0]

for ai in a:
    ca.append(ca[-1]+ai)

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

ans = 0
j = m

for i, cai in enumerate(ca):
    if cai > k:
        continue
    while True:
        if cai + cb[j] <= k:
            ans = max(ans, i + j)
            break
        if j == 0:
            break
        j -= 1

print(ans)

Submission status: Accepted

Solution code:
#ABC172-C
from bisect import bisect_left,bisect_right
N,M,K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))

C=[A[0]]
for i in range(1,N):
    C.append(C[-1]+A[i])
D=[B[0]]
for i in range(1,M):
    D.append(D[-1]+B[i])
C=[0]+C
D=[0]+D
ans=0

#print(C)
#print(D)
ans=0

for i in range(N+1):
    if C[i]<=K:
        k=i
        tmp = K-C[i]
        t= bisect_right(D,tmp)
        #print(i,tmp,t)
        if t>0:
            k +=t-1
        ans=max(ans,k)
print(ans)

Submission status: Accepted

Solution code:
# input()
# int(input())
# map(int, input().split())
# list(map(int, input().split()))
# list(map(int, list(input()))) # スペースがない数字リストを読み込み
import math
import fractions
import sys
import bisect
import heapq  # 優先度付きキュー(最小値取り出し)
import collections
from collections import Counter
from collections import deque
import pprint
import itertools

sr = lambda: input()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

"""nを素因数分解"""
"""2以上の整数n => [[素因数, 指数], ...]の2次元リスト"""


def factorization(n):
    arr = []
    temp = n
    if n == 1:
        return arr

    for i in range(2, int(-(-n ** 0.5 // 1)) + 1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr.append([i, cnt])

    if temp != 1:
        arr.append([temp, 1])

    if arr == []:
        arr.append([n, 1])

    return arr


# a^n
def power(a, n, mod):
    x = 1
    while n:
        if n & 1:
            x *= a % mod
        n >>= 1
        a *= a % mod
    return x % mod


# n*(n-1)*...*(l+1)*l
def kaijo(n, l, mod):
    if n == 0:
        return 1
    a = n
    tmp = n - 1
    while (tmp >= l):
        a = a * tmp % mod
        tmp -= 1
    return a


inf = 10 ** 18
mod = 10 ** 9 + 7

n,m,k = lr()
a = lr()
b = lr()
a1 = [0]
b1 = [0]
for num in a:
    a1.append(a1[-1]+num)
for num in b:
    b1.append(b1[-1]+num)
def sumAreaA(l,r):
    return a1[r]-a1[l]
def sumAreaB(l,r):
    return b1[r]-b1[l]

ans = 0
dame = bisect.bisect_left(a1,k)
if dame < n+1:
    if a1[dame] == k:
        dame+=1
for i in range(dame):
    pre = i
    pretime = sumAreaA(0,i)
    time = k-pretime
    ind = bisect.bisect_left(b1,time)
    if ind == 0:
        ans = max(ans,pre)
    elif ind < m+1:
        if b1[ind] != time:
            ind-=1
        ans = max(ans,pre+ind)
    else:
        ans = max(ans, pre+m)
print(ans)

Submission status: Accepted

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

time=0
while j<m:
    time+=B[j]
    if time >k:
        time-=B[j]
        break
    j += 1
j-=1
rem=k-time
ans=j+1
cnt=ans
while True:
    while i<n and rem-A[i]>=0:
        rem-=A[i]
        i+=1
        cnt+=1
    ans=max(ans,cnt)
    
    rem+=B[j]
    j-=1
    cnt-=1
    if j<-1:
        break

print(ans)

Submission status: Accepted

Solution code:
def solve():
  N, M, K = map(int,input().split())
  AA = list(map(int,input().split()))
  BB = list(map(int,input().split()))
  A_max = 0
  AAs = [0]*N
  AAs[0] = AA[0]
  if K>=AAs[0]:
    A_max = 1
  for i in range(1,N):
    AAs[i] = AAs[i-1] + AA[i]
    if K>=AAs[i]:
      A_max+=1
  BBs = [0]*M
  BBs[0] = BB[0]
  BM = M
  for i in range(1,M):
    BBs[i] = BBs[i-1] + BB[i]
  Book_max = 0
  for i in range(A_max+1):
    b_max = 0
    if i == 0:
      B_time = K
    else:
      B_time = K - AAs[i-1]
    for j in range(BM-1,-1,-1):
      #print(i,j,A_max,b_max,B_time,BM)
      if B_time>=BBs[j]:
        b_max = j+1
        BM = b_max
        break
    if Book_max<b_max+i:
      Book_max = b_max+i
  print(Book_max) 
solve()

Submission status: Accepted

Solution code:
import sys
from pprint import pprint
import bisect

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

ans = 0
max_read = N + M

S_a = [0 for _ in range(N+1)]
S_b = [0 for _ in range(M+1)]
for i in range(N):
    S_a[i+1] = A[i] + S_a[i]
for i in range(M):
    S_b[i+1] = B[i] + S_b[i]
# print(S_a)
# print(S_b)

ans = 0
for i in range(N+1):
    # A を i冊読んだ時、その時よめるBの最大値を二分探索
    sum_t = S_a[i]
    if sum_t > K:
        continue
    # print(K - sum_t)
    b_index = bisect.bisect_right(S_b, K - sum_t)
    # print(i, b_index)
    ans = max(ans, i + b_index - 1)
    
print(ans)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

N, M, K = MAP()
A = [0] + LIST()
B = LIST()

a = list(accumulate(A))
b = list(accumulate(B))

ans = 0
for i in range(N+1):
	if a[i] <= K:
		ans = max(ans, i + bisect(b, K-a[i]))
print(ans)

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



tmptime = 0
limitA = len(A) -1
for i,a in enumerate(A):
    
    if tmptime + a > K:
        limitA = i - 1 
        break
    tmptime += a

max_len = limitA
bi = 0
# print(tmptime)
while limitA >= -1:
    while bi < len(B) and tmptime + B[bi] <= K:
        # print(tmptime,B[bi])
        tmptime += B[bi]
        bi += 1


    max_len = max(limitA + 1 + bi, max_len)

    if limitA >= 0:
        tmptime -= A[limitA]
        limitA -= 1
    else:
        break
print(max_len)

Submission status: Accepted

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


def solve(N: int, M: int, K: int, A: "List[int]", B: "List[int]"):
    cur = sum(A)
    for i in range(N-1,-1,-1):
        if cur>K: cur -= A[i]
        else:break
    if cur==0:
        max_k = 0
        l = 0
    else:
        max_k = i+1
        l = 1
    max_pk = max_k

    p = 0
    while(p<M):
        if cur+B[p] <= K:
            cur+=B[p]
            max_k = max_pk+p+1
            p+=1
        else:
            break

    # print(cur,max_k)
    if l ==1:
        for q in range(i,-1,-1):
            cur -= A[q]
            while(p<M):
                if cur+B[p] <= K:
                    cur+=B[p]
                    max_k = max(q+p+1, max_k)
                    p+=1
                else:
                    break
    print(max_k)





    return


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    B = [int(next(tokens)) for _ in range(M)]  # type: "List[int]"
    solve(N, M, K, A, B)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import os
import sys
from io import BytesIO, IOBase

BUFSIZE = 8192
class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

import bisect

def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
INF = 10 ** 18
MOD = 10**9+7

Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()

n,m,k = Ri()

a = Ri()
b = Ri()

arr1 = [0]*n
arr2 = [0]*m

arr1[0] = a[0]
arr2[0] = b[0]

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

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

ans = 0
for i in range(n+1):
    # if k -arr1[i-1] > 
    if i == 0:
        ind = bisect.bisect_right(arr2, k)
        ind-=1
        ans=  max(ans, i+ind+1)
        # print(ans)
    else:
        if k -arr1[i-1] >= 0:
            ind = bisect.bisect_right(arr2, k-arr1[i-1])
            # print(ind)
            ind-=1
            ans=  max(ans, i+ind+1)
    # print(ans)

print(ans)

Submission status: Accepted

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

while A[a] <= K:
    K -= A[a]
    a += 1
    if a == N:
        break
        
x = a
b = 0
while a>=0:
    if b < M:
        while B[b] <= K:
            K -= B[b]
            b += 1
            if b == M:
                break
    if a+b > x:
        x = a+b
    a -= 1
    K += A[a]

print(x)

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
max_ans = 0
time = 0
ac = 0
bc = 0
for i in range(n):
    if time + a[i] <= k:
        ans += 1
        time += a[i]
        ac += 1
    else:
        break
for i in range(m):
    if time + b[i] <= k:
        ans += 1
        time += b[i]
        bc += 1
    else:
        break
max_ans = ans
while (bc!=m) and (ac!=0):
    time -= a[ac-1]
    ac -= 1
    ans -= 1
    while time + b[bc] <= k:
        ans += 1
        time += b[bc]
        bc += 1
        max_ans = max(ans, max_ans)
        if bc == m:
            break
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()))
ans1 = 0
for i in range(n-1):
    a[i+1] = a[i]+a[i+1]
for i in range(m-1):
    b[i+1] = b[i]+b[i+1]
import bisect
key = bisect.bisect(a,k)
for i in range(key):
    ans = 0
    ans += key-i
    time = a[key-i-1]
    ans += bisect.bisect(b,k-time)
    ans1 = max(ans1,ans)
ans = 0
ans += bisect.bisect(b,k)
ans1 = max(ans,ans1)

            
print(ans1)

Submission status: Accepted

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

    a = 0
    b = 0

    ib = 0
    while True:
        if ib != M and b + B[ib] <= K:
            b += B[ib]
            ib += 1
        else:
            break
    ans = ib
    for ia in range(N):
        while a + A[ia] + b > K:
            if ib == 0:
                return ans
            ib -= 1
            b -= B[ib]
        a += A[ia]
        ans = max(ans,ia+ib+1)

    return ans

print(hoge())

Submission status: Accepted

Solution code:
import sys
from collections import deque
import copy
import bisect
def main():
    N, M, K = map(int, input().split())
    MAX = 10 ** 9 + 7
    a = [int(i) for i in input().split()]
    b = [int(i) for i in input().split()]
    A = []
    B = []
    A.append(0)
    B.append(0)
    for i in range(N):
        A.append(a[i] + A[i])
    for i in range(M):
        B.append(b[i] + B[i])
    B.append(B[M - 1] + MAX)
    ans = 0
    for i in range(N+1):
        if A[i] <= K:
            a = bisect.bisect_right(B, (K - A[i]))
            a -= 1
            ans = max(ans, (i + a))
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N,M,K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))

ans=0
T=0
for i in range(N):
    T+=A[i]
    if T > K:
        T-=A[i]
        break
    else:
        ans+=1
R=[]
R.append(ans)
n=ans
m=0
while(1):
    while(1):
        if m>=M: break
        T+=B[m]
        
        if T > K:
            T-=B[m]
            break
        else:
            ans+=1
            R.append(ans)
            m+=1
            
    n-=1
    if n<0: break
    T-=A[n]
    ans-=1
    

print(max(R))

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()))
sa=[0]*(n+1)
sb=[0]*(m+1)
for i in range(1,n+1):
    sa[i]=sa[i-1]+a[i-1]
for j in range(1,m+1):
    sb[j]=sb[j-1]+b[j-1]
ans=0
for x in range(n+1):
    if k-sa[x]>=0:
        k2=k-sa[x]
        l=bisect_right(sb,k2)-1
        if x+l>ans:
            ans=x+l
for y in range(m+1):
    if k-sb[y]>=0:
        k2=k-sb[y]
        l=bisect_right(sa,k2)-1
        if y+l>ans:
            ans=y+l
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()]

s = 0
MA = -1
for i in range(N):
  s += A[i]
  if s > K:
    MA = i
    s -= A[i]
    break
if MA < 0:
  MA = N

  
ans = MA
MB = 0
while MB < M:
  while True:
    if MB < M and s + B[MB] <= K:
      s += B[MB]
      MB += 1
      ans = max(ans, MA + MB)
    else:
      break
  if MA > 0:
    s -= A[MA-1]
    MA -= 1
  else:
    break

print(ans)

Submission status: Accepted

Solution code:
import bisect
N, M, K = map(int,input().split())
ls_a = list(map(int,input().split()))
ls_b = list(map(int,input().split()))

A = [0] * (N+1)
B = [0] * (M+1)
for i in range(N):
  A[i+1] = A[i] + ls_a[i]
for i in range(M):
  B[i+1] = B[i] + ls_b[i]
  
ans = 0
for i in range(N+1):
  if A[i] <= K:
    x = bisect.bisect(B,K-A[i])
    ans = max(ans, i+x-1)
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
import sys
# from operator import itemgetter
sysread = sys.stdin.readline
read = sys.stdin.read
sys.setrecursionlimit(10 ** 7)
#from heapq import heappop, heappush
#from collections import OrderedDict, defaultdict
#import math
#from itertools import product, accumulate, combinations, product
import bisect# lower_bound etc
#import numpy as np
#from copy import deepcopy
#from collections import deque
#import numba

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

    A_sum = [0]
    for a in A:
        A_sum.append(A_sum[-1] + a)
    B_sum = [0]
    for b in B:
        B_sum.append(B_sum[-1] + b)
    #print(A_sum, B_sum)

    max_val = 0
    for i, a in enumerate(A_sum):
        if a > K:
            max_val = max(i-1, max_val)
            break
        idx = bisect.bisect_left(B_sum, K-a)
        if idx < len(B_sum) and B_sum[idx] == K-a:
            idx += 1
        max_val = max(max_val, idx+i-1)
        #print(i, idx)
    print(max_val)



if __name__ == "__main__":
    run()

Submission status: Accepted

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

ac_A = list(accumulate(A))
ac_B = list(accumulate(B))

#left = 0
max_book_num = bisect.bisect_right(ac_B, k)

for left_book_num in range(n):
    if ac_A[left_book_num] > k:
        break
    else:
        max_book_num = max(max_book_num, left_book_num + 1 + bisect.bisect_right(ac_B, k - ac_A[left_book_num]))
print(max_book_num)

Submission status: Accepted

Solution code:
def _integral(A):
    result = []
    sum_value = 0
    result.append(sum_value)
    for i in range(len(A)):
        sum_value += A[i]
        result.append(sum_value)
    return result


def _main(N, M, K, A, B):
    integral_a = _integral(A)
    integral_b = _integral(B)
    top_count = 0
    b_max = len(B)
    for a_index in range(len(A) + 1):
        a_value = integral_a[a_index]
        if K < a_value:
            break
        for b_index in range(0, min(len(B) + 1, b_max + 1))[::-1]:
            if a_value + integral_b[b_index] <= K:
                if top_count < a_index + b_index:
                    top_count = a_index + b_index
                b_max = b_index
                break
    print(top_count)


if __name__ == "__main__":
    Na, Ma, Ka = map(int, input().split())
    Aa = list(map(float, input().split()))
    Ba = list(map(float, input().split()))
    _main(Na, Ma, Ka, Aa, Ba)

Submission status: Accepted

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


index = 0
index2 = 0
stackedTime = 0

while(index<N and stackedTime + A[index] <= K):
  stackedTime += A[index]
  index += 1

while(index2 < M and stackedTime + B[index2] <= K):
  stackedTime += B[index2]
  index2 += 1

maxBook = index + index2
for i in range(N):
  if(index == 0 or index2 == M):
    break
  index -= 1
  stackedTime -= A[index]
  while(index2 < M and stackedTime + B[index2] <= K):
    stackedTime += B[index2]
    index2 += 1

  maxBook = max(maxBook, index + index2)

print(maxBook)

Submission status: Accepted

Solution code:
from itertools import accumulate
from collections import deque

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


    ac = list(accumulate([0] + A))
    bc = list(accumulate([0] + B))
    # print(ac, bc)
    a_max = -1
    for i in range(N+1):
        if ac[i] <= K:
            a_max = i
        else:
            break
    
    ans = a_max
    # print(a_max)
    js = 0
    for i in range(a_max+1)[::-1]:
        a = ac[i]
        for j in range(js, M+1):
            # print(a + bc[j], i, j)
            if a + bc[j] <= K:
                js = j
                if i + j > ans:
                    ans = i + j
            else:
                break

    print(ans)

if __name__ == '__main__':
    main()