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:
import numpy
import bisect
n,m,k = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
a = [0]+list(numpy.cumsum(A))
b = [0]+list(numpy.cumsum(B))
ans = 0
# print(a,b)
for i in range(len(a)):
    x = k - a[i]
    if x >= 0:
        j = bisect.bisect_right(b,x)-1
        ans = max(ans, i+j)
print(ans)

Submission status: Accepted

Solution code:
import itertools


def main():
    n, m, k = [int(x) for x in input().split()]
    array_a = [0] + list(itertools.accumulate(int(x) for x in input().split()))
    array_b = [0] + list(itertools.accumulate(int(x) for x in input().split()))

    def generate_anses():
        from_b = m
        for from_a in range(n + 1):
            if array_a[from_a] > k:
                break
            while array_b[from_b] > k - array_a[from_a]:
                from_b -= 1
            yield from_a + from_b

    print(max(generate_anses()))


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:
ans=0

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

r=0

for l in range(n):
    
        while r<m and x+b[r]<=k:
            x+=b[r]
            r+=1
            z+=1
        if x<=k:ans=max(z,ans)
        #show(a,z,r)
        x-=a.pop()
        z-=1

while r<m and x+b[r]<=k:
    x+=b[r]
    r+=1
    z+=1
ans=max(z,ans)

print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
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()))
    As = [0]*(N+1)
    Bs = sum(B)
    As[0] = 0
    for i in range(1,N+1):
        As[i] = As[i-1] + A[i-1]
    T = K
    j = M 
    ans = 0
    for i in range(N+1):
        T = K - As[i]
        if T < 0:
            break
        while Bs > T:
            j -= 1
            Bs -= B[j]
        ans = max(ans, i+j)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from itertools import accumulate
from bisect import bisect_right

N, M, K = [int(n) for n in input().split()]
A = [0]+[*accumulate([int(n) for n in input().split()])]
B = [*accumulate([int(n) for n in input().split()])]
ans = 0
a = bisect_right(A, K)
for i in range(a):
    b = bisect_right(B, K-A[i])
    ans = max(ans, i+b)
print(ans)

Submission status: Accepted

Solution code:
N,M,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
 
a,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:
import bisect
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

sa = [0 for i in range(n+1)]
sb = [0 for i in range(m+1)]

c = 0

for i in range(n):
  c += a[i]
  sa[i+1] = c
c = 0
for i in range(m):
  c += b[i]
  sb[i+1] = c
ans = 0
# if k >= sum(a)+sum(b):
#   print (n+m)
#   exit()
for i in range(n+1):
  x = k - sa[i]
  if x < 0:
    continue
  y = bisect.bisect_right(sb,x)
  y += i
  ans = max(ans, y-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()))
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())
an = list(map(int, input().split()))
bn = list(map(int, input().split()))
ls = an[::-1] + bn
l = 0
r = n
s = sum(an)
ans = 0
while r < n + m and l <= n:
    if s <= k:
        ans = max(ans, r - l)
        s += ls[r]
        r += 1
    else:
        s -= ls[l]
        l += 1
if s <= k:
    ans = max(ans, r-l)
print(ans)

Submission status: Accepted

Solution code:
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
from itertools import accumulate
from bisect import bisect_right
n,m,k=MI()
A=LI()
B=LI()
a=[0]+list(accumulate(A))
b=[0]+list(accumulate(B))
ans=0
for i in range(n+1):
  if a[i]>k:
    break
  j=bisect_right(b,k-a[i])
  ans=max(ans,i+j-1)
print(ans)

Submission status: Accepted

Solution code:
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

n,m,k=MI()
A=[0]+LI()
B=[0]+LI()
for i in range(1,n+1):
  A[i]+=A[i-1]
for i in range(1,m+1):
  B[i]+=B[i-1]
j=m
ans=0
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 = list(map(int, ("0 " + input()).split()))
b = list(map(int, ("0 " + input()).split()))

time = 0
books = 0
a_index = 0
b_index = 0

for i in range(1, n + 1):
  if time + a[i] <= k:
    time += a[i]
    books += 1
    a_index = i
  else:
    break

for i in range(1, m + 1):
  if time + b[i] <= k:
    time += b[i]
    books += 1
    b_index = i
  else:
    break

max_books = books

for i in range(a_index, 0, -1):
  time -= a[i]
  books -= 1
  for j in range(b_index + 1, m + 1):
    if time + b[j] <= k:
      time += b[j]
      books += 1
      b_index = j
      max_books = max(max_books, books)
    else:
      break

print(max_books)

Submission status: Accepted

Solution code:
import bisect

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


totaltame_a=[0]
for a in list_a:
    temp_a=totaltame_a[-1]+a
    if temp_a>K:
        break
    else:
        totaltame_a.append(temp_a)


totaltime_b=[0]
for b in list_b:
    totaltime_b.append(totaltime_b[-1]+b)

ans=0

for i in range (len(totaltame_a)):
    Kb=K-totaltame_a[i]
    j=bisect.bisect_right(totaltime_b,Kb)-1
    if i+j>ans:
        ans=i+j
    else:
        pass

print(ans)

Submission status: Accepted

Solution code:
N,M,K=map(int,input().split())
A=[0]+list(map(int,input().split()))
B=list(map(int,input().split()))
x=sum(B)
y=M-1
r=0
for i in range(N+1):
    x+=A[i]
    while x>K and y>=0:
        x-=B[y]
        y-=1
    if x<=K:
        r=max(r,i+y+1)
print(r)

Submission status: Accepted

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

from pprint import pprint
from bisect import bisect_right
import math
import sys

sys.setrecursionlimit(10 ** 6)
INF = float('inf')


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

sum_A = [0] * (N+1)
for i in range(N):
    sum_A[i+1] = sum_A[i] + A[i]
sum_B = [0] * (M+1)
for i in range(M):
    sum_B[i+1] = sum_B[i] + B[i]
# print(sum_A, sum_B)

res = 0
for i in range(N+1):
    time_A = sum_A[i]
    rest = K - time_A
    if rest < 0:
        continue
    j = bisect_right(sum_B, rest) - 1
    res = max(res, i + j)
print(res)

Submission status: Accepted

Solution code:
from sys import stdin
import bisect

sysin=stdin.readline

N,M,K=[int(x) for x in sysin().rstrip().split()]
a=[int(x) for x in sysin().rstrip().split()]
b=[int(x) for x in sysin().rstrip().split()]

# N,M=3,4
# K=240
# a=[60, 90, 120]
# b=[80, 150, 80, 150]

ans=0

accum_a=[0]
for i in a:
    _a=accum_a[-1]+i
    if _a>K:
        break
    else:
        accum_a.append(_a)
accum_a.append(K)

accum_b=[0]
for i in b:
    accum_b.append(accum_b[-1]+i)

ans=0

for j in range (len(accum_a)-1):
    Kb=K-accum_a[j]
    bookB=bisect.bisect_right(accum_b,Kb)-1
    if j+bookB>ans:
        ans=j+bookB
    else:
        pass

print(ans)

Submission status: Accepted

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

from pprint import pprint
from bisect import bisect_right
import math
import sys

sys.setrecursionlimit(10 ** 6)
INF = float('inf')


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

sum_A = [0] * (N+1)
for i in range(N):
    sum_A[i+1] = sum_A[i] + A[i]
sum_B = [0] * (M+1)
for i in range(M):
    sum_B[i+1] = sum_B[i] + B[i]
# print(sum_A, sum_B)

res = 0
for i in range(N+1):
    time_A = sum_A[i]
    rest = K - time_A
    if rest < 0:
        continue
    j = bisect_right(sum_B, rest) - 1
    res = max(res, i + j)
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()))

#入力終わり


#累積和とる

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

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

#累積和計算おわり


ans = 0
for i in range(M+1):#Bを順番に増やしていくよー
    A_use = K-B_wa[i]
    if A_use<0: #Bだけでこえてる
        break
    
    ##Aは最大で何冊よめるかな
    A_MAX = 0
    start = 0
    end = N
    while  end-start>=1 : #調べるのが2個以上
        now = int((start + end+1)/2) #切り上げ
        if A_wa[now]==A_use :  #A[i]がKと一致
            A_MAX = now #ここが最大
            break
        elif A_wa[now]<A_use : #A[i]がKより小さい
            start = now #目的の値はここ以上のとこにある
            A_MAX = now #現時点での最大値
        else : #A[i]がKより大きい
            end = now-1#目的の値はここより小さい
    ##Aの最大探すの終わり
    
    #これ最大？
    if A_MAX+i>ans :
        ans=A_MAX+i

#コンソールにだす
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_right
import numpy as np
def main():
    N, M, K = map(int, input().split())
    A = np.array([0] + list(map(int, input().split())), dtype=np.int64)
    B = np.array([0] + list(map(int, input().split())), dtype=np.int64)

    As = np.cumsum(A)
    Bs = np.cumsum(B)

    count = 0

    for i, a in enumerate(As):
        if a > K:
            break
        j = bisect_right(Bs, K-a)
        #print(f"i={i} j={j} Bs={Bs} cost={K-a}")
        count = max(count, (i+j-1))

    print(count)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def search(b, s, e, t):
    if e - s == 1:
        return s

    mid = (e + s) // 2

    if b[mid] <= t:
        return search(b, mid, e, t)
    else:
        return search(b, s, mid, t)

INF = float('inf')
N, M, K = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))

a = [0] * (N+1)
b = [0] * (M+1)
for i,x in enumerate(A):
    a[i+1] = a[i] + x

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

ans = 0
j = M
for i, va in enumerate(a):
    if va > K:
        break
    index = search(b, 0, len(b), K-va)
    ans = max(i+index, ans)
print(ans)

Submission status: Accepted

Solution code:
#atcoder template
def main():
    import sys
    imput = sys.stdin.readline
    #文字列入力の時は上記はerrorとなる。
    #ここにコード
    #input
    n, m, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))

    #output
    import itertools
    A = [0] + list(itertools.accumulate(a))
    B = [0] + list(itertools.accumulate(b))

    #A[i] + B[j] <= Kとなるi, jでi+jが最大になるもの

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

    #N = 1のときなどcorner caseを確認！
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
n,m,k=MI()
a=[0]+LI()
b=[0]+LI()
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
j=m
for i in range(n+1):
  if a[i]>k:
    continue
  while a[i]+b[j]>k:
    j-=1
  ans=max(ans,i+j)
print(ans)

Submission status: Accepted

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

    n_list = list(map(int,input().split()))
    m_list = list(map(int,input().split()))
    a, b =[0],[0]
    for i in range(n):
        a.append(a[i]+n_list[i])
    for j in range(m):
        b.append(b[j]+m_list[j])
    
    j=m
    ans = 0
    for i in range(n+1):
        if a[i]>k:
            break
        while b[j]+a[i]>k:
            j -= 1
        ans = max(ans,i+j)
    print(ans)
main()

Submission status: Accepted

Solution code:
a,b,c=map(int,input().split())
ad=list(map(int,input().split()))
bd=list(map(int,input().split()))

ae=-1
be=-1
ans=0
gen=0
nokori=c
for i in range(a):
  if nokori>=ad[i]:
    nokori-=ad[i]
    gen+=1
    ae+=1
  else:
    break

for i in range(b):
  if nokori>=bd[i]:
    nokori-=bd[i]
    gen+=1
    be+=1
  else:
    break

ans=gen
while ae!=-1:
  nokori+=ad[ae]
  gen-=1
  ae-=1
  while be<b-1:
    if nokori>=bd[be+1]:
      nokori-=bd[be+1]
      gen+=1
      be+=1
    else:
      break
    
  if ans<gen:
    ans=gen
    
print(ans)

Submission status: Accepted

Solution code:
def main():
    from itertools import chain

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

    t = sum(B)
    ans = 0
    j = M
    for i, x in enumerate(chain([0], A)):
        t += x
        while j > 0 and t > K:
            j -= 1
            t -= B[j]
        if t > K: break
        if ans < i + j:
            ans = i + j
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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

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

print(ans)

Submission status: Accepted

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

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

ans = 0
best = m
for i in range(n+1):
    a_time = A_cum[i]
    for j in range(best, -1, -1):
        if B_cum[j] <= k - a_time:
            ans = max(ans, i + j)
            break
    best = j
print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate
N, M, K = map(int, input().split())
A = [0] + list(map(int, input().split()))
B = [0] + list(map(int, input().split()))
A = list(accumulate(A))
B = list(accumulate(B))
ans = 0
j = M
for i in range(N+1):
    while(j>=0):
        if A[i] + B[j] <= K:
            ans = max(ans,i+j)
            break
        j -= 1
    """for m in reversed(range(M+1)):
        if A[n] + B[m] <= K:
            ans = max(ans, n+m)
            break
    """
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

##累積和のリストが得られる
a_cum = [0] + list(accumulate(a))
b_cum = [0] + list(accumulate(b))

ans = [0]
for i in range(n + 1):    #0~n+1
    c = bisect_right(b_cum, k-a_cum[i]) - 1 ##Aをi冊読んだ場合の残り時間 : k-a[i]  bisect_rightでbの累積時間の最大のインデックス(本の冊数)が求まる
                                 ##bisectは位置が求まるので、-1で冊数を求めている
    if c != -1: #0から始まるので、冊数が0の場合は-1となってしまう
        ans.append(c+i)

print(max(ans))

Submission status: Accepted

Solution code:
# %%

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] * (N + 1)
BB = [0] * (M + 1)
for i, v in enumerate(A):
    AA[i + 1] += v + AA[i]
for i, v in enumerate(B):
    BB[i + 1] += v + BB[i]

ans = 0
b_cnt = M
for a_cnt in range(N + 1):
    if AA[a_cnt] > K:
        continue
    while AA[a_cnt] + BB[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()))


#(2)二分探索を使った実装 O(NlogM)

# 下の配列を累積和にしとく
for i in range(1,N):
    A[i] += A[i - 1]
for i in range(1, M):
    B[i] += B[i - 1]

# O(N),O(M)かかるからあんま良くないが複数の実装をする都合でinsert()使用.
A.insert(0, 0)
B.insert(0,0)
ans = 0

#　各A[i]について考える.
for i in range(N+1):
    if K - A[i] < 0:
        break
    # 二分探索
    ok = -1
    ng = M+1
    while (abs(ok - ng) > 1):
        mid = (ok + ng) // 2
        if B[mid] <= K - A[i]: ok = mid
        else: ng = mid
        
    ans = max(ans, i + ok)
    
print(ans)

Submission status: Accepted

Solution code:
import itertools
N,M,K = map(int,input().split())
A = [0]+list(map(int,input().split()))
B = [0]+list(map(int,input().split()))
SA = list(itertools.accumulate(A))
SB = list(itertools.accumulate(B))
ans = 0
j = len(B)-1
for i in range(len(SA)):
    while(j>=0):
        if SA[i] + SB[j] <= K:
            ans = max(ans,i+j)
            break
        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()))
ans = 0

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

j = M

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

Submission status: Accepted

Solution code:
from itertools import accumulate
N, M, K = map(int, input().split())
Nlist = [0]+list(map(int, input().split()))
Mlist = [0]+list(map(int, input().split()))
Asum = list(accumulate(Nlist))
Bsum = list(accumulate(Mlist))
count = 0
j = 0
for i in range(N, -1, -1):
    if K >= Asum[i]:
        while Asum[i]+Bsum[j] <= K:
            j += 1
            if j == M+1:
                break
        if count < i+j-1:
            count = i+j-1
    if j == M+1:
        break
print(count)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_right
n , m , k = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
acc_a = [0 for i in range(n+1)]
acc_b = [0 for i in range(m + 1)]
acc_a[0] = 0
acc_b[0] = 0
for i in range(1,n+1):
    acc_a[i] = acc_a[i-1] + a[i-1]
for i in range(1,m+1):
    acc_b[i] = acc_b[i-1] + b[i-1]
ans = 0
for i in range(n + 1):
    if k >= acc_a[i]:
        index = bisect_right(acc_b, k - acc_a[i])
        ans = max(ans, i + index - 1)
    else:
        break
print(ans)

Submission status: Accepted

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


book_a = [0] * (n + 1)
for i in range(n):
    book_a[i + 1] = book_a[i] + a[i]

book_b = [0] * (m + 1) 
for j in range(m):
    book_b[j + 1] = book_b[j] + b[j]
count = 0
for read in range(n + 1):
    tmp = k - book_a[read]
    if tmp < 0:
        break
    read_b = bisect.bisect_right(book_b, tmp) -1
    count = max(count, read_b + read)

print(count)

Submission status: Accepted

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

a = [0] * (N+1)
b = [0] * (M+1)
for i,x in enumerate(A):
    a[i+1] = a[i] + x

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

ans = 0
j = M
for i, va in enumerate(a):
    if va > K:
        break
    while va + b[j] > K:
        #print(va, b[j], ans)
        j -= 1
        if j < 0:
            break
    ans = max(i+j, ans)
print(ans)

Submission status: Accepted

Solution code:
import bisect


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

# print(K)
# print(a)
# print(b)
# N,M=3,4
# K=240
# a=[60, 90, 120]
# b=[80, 150, 80, 150]

ans=0

accum_a=[0]
for i in a:
    _a=accum_a[-1]+i
    if _a>K:
        break
    else:
        accum_a.append(_a)
accum_a.append(K)

accum_b=[0]
for i in b:
    accum_b.append(accum_b[-1]+i)

ans=0

for j in range (len(accum_a)-1):
    Kb=K-accum_a[j]
    bookB=bisect.bisect_right(accum_b,Kb)-1
    if j+bookB>ans:
        ans=j+bookB
    else:
        pass

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

a = [0]
b = [0]

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

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

j = m

for i in range(n + 1):
    if a[i] > k:
        break
    while a[i] + b[j] > k:
        j -= 1
    else:
        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()))
cum_a = []
total = 0
for i in range(n):
  total += a[i]
  if total > k:
    break
  cum_a.append(total)
  
cum_b = []
total = 0
for j in range(m):
  total += b[j]
  if total > k:
    break
  cum_b.append(total)
lim_a = len(cum_a)
lim_b = len(cum_b)
idx = 0
ans = lim_a
for l in range(1, lim_a + 1):
  while idx < lim_b:
    if k >= cum_a[-l] + cum_b[idx]:
      ans = max(ans, lim_a - l + idx + 2)
    else:
      break
    idx += 1
ans = max(ans, lim_b)
print(ans)

Submission status: Accepted

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

a = [0]*(N+1)
b = [0]*(M+1)

for i in range(N):
    a[i+1] = a[i] + A[i]
for j in range(M):
    b[j+1] = b[j] + B[j]

n_b = M
best = 0

for n_a in range(N+1):
    if K < a[n_a]:
        break

    while  K < a[n_a] + b[n_b]:
        n_b -= 1

    best = max(n_a + n_b,best)

print(int(best))

Submission status: Accepted

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


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

    def is_ok(bcount):
        return Asum[acount] + Bsum[bcount] <= K


    def meguru_bisect(ng, ok):
        '''
        初期値のng,okを受け取り,is_okを満たす最小(最大)のokを返す
        まずis_okを定義すべし
        ng ok は  とり得る最小の値-1 とり得る最大の値+1
        最大最小が逆の場合はよしなにひっくり返す
        '''
        while (abs(ok - ng) > 1):
            mid = (ok + ng) // 2
            if is_ok(mid):
                ok = mid
            else:
                ng = mid
        return ok

    nowtime = 0

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

    maxbooks = 0
    before = M
    ans = 0
    for acount in range(N+1):
        maxB = meguru_bisect(M+1, -1)
        if maxB < 0:
            continue
        ans = max(ans, acount+maxB)

    print(ans)



if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
N,M,K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
C=[0]
for x in range(N):
 C.append(C[-1]+A[x])
D=[0]
for x in range(M):
 D.append(D[-1]+B[x])
count=0
y=M
for x in range(N+1):
 if C[x]>K:
    break
 while D[y]>K-C[x] and y>0:
   y-=1
 count=max(count,x+y)
print(count)

Submission status: Accepted

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#

import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
n,m,k = readInts()
A = [0] + list(accumulate(readInts()))
B = list(accumulate(readInts()))
ans = 0
import bisect
#print(B)
for i in range(len(A)):
    if A[i] > k:
        continue
    x = bisect.bisect_left(B,k - A[i] + 1)
    #print(x,)
    ans = max(ans,x+i)
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(" ")))

time_A = [0]*(N+1)
time_B = [0]*(M+1)

for i in range(N):
    time_A[i+1] = time_A[i] + A[i]
for j in range(M):
    time_B[j+1] = time_B[j] + B[j]

ans = 0
l = M

for k in range(N+1):
    if time_A[k] > K:
        break
    while time_A[k] + time_B[l] > K:
        l -= 1
    ans = max(ans,k+l)

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import numpy as np


def main():
    N, M, K = map(int, input().split())
    A = np.cumsum([0] + list(map(int, input().split())))
    B = np.cumsum([0] + list(map(int, input().split())))

    ans = 0
    j = 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)


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

Submission status: Accepted

Solution code:
from sys import stdin
input = lambda: stdin.readline().rstrip("\r\n")
from collections import defaultdict as vector, deque as que
read = lambda: list(map(int,input().split()))
from bisect import bisect
from heapq import heappush as hpush,heappop as hpop

n,m,k=read()
A=read()
B=read()
a,b=[0],[0]
for i in range(n):
	a.append(a[i]+A[i])
for i in range(m):
	b.append(b[i]+B[i])
ans=0
for i in range(0,n+1):
	curr=a[i]
	if curr>k:
		break
	j=bisect(b,k-curr,0,m+1)
	if j>m or b[j]>k-curr:
		j-=1
	if j==-1:
		ans=max(0,ans)
	else:
		ans=max(ans,i+j)
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
input = lambda: stdin.readline().rstrip("\r\n")
from collections import defaultdict as vector, deque as que
read = lambda: list(map(int,input().split()))
from bisect import bisect
from heapq import heappush as hpush,heappop as hpop

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

Submission status: Accepted

Solution code:
from itertools import accumulate
from bisect import *

N, M, K = [int(n) for n in input().split()]
A = [0]+[x for x in accumulate([int(n) for n in input().split()])]
B = [0]+[x for x in accumulate([int(n) for n in input().split()])]
#print(A)
#print(B)
ans = 0
a = bisect_right(A, K)
#print(a)
for i in range(a):
  b = bisect_right(B, K-A[i])
  ans = max(ans, i+b)
#  print(ans, i, b)
print(ans-1)

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)