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:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
import math
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7

#############
# Main Code #
#############

def or_less(array, x):
    # arrayの中のx以下のものの個数
    # arrayの中のx以下のもののうちの最大値
    index = bisect_right(array, x)
    if index == 0:
        or_less_int = -float('inf')
    else:
        or_less_int = array[index - 1]
    return [index, or_less_int]

N, M, K = getNM()
A = getList()
A.insert(0, 0)
B = getList()

A_imos = copy.deepcopy(A)
for i in range(N):
    A_imos[i + 1] += A_imos[i]

B_imos = copy.deepcopy(B)
for i in range(M - 1):
    B_imos[i + 1] += B_imos[i]

ans = 0
for i in range(N + 1):
    if A_imos[i] > K:
        continue
    index = or_less(B_imos, K - A_imos[i])[0]
    ans = max(ans, i + index)
print(ans)

Submission status: Accepted

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
import math
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7

#############
# Main Code #
#############

def or_less(array, x):
    # arrayの中のx以下のものの個数
    # arrayの中のx以下のもののうちの最大値
    index = bisect_right(array, x)
    if index == 0:
        or_less_int = -float('inf')
    else:
        or_less_int = array[index - 1]
    return [index, or_less_int]

N, M, K = getNM()
A = getList()
B = getList()

A_imos = copy.deepcopy(A)
for i in range(N - 1):
    A_imos[i + 1] += A_imos[i]

B_imos = copy.deepcopy(B)
for i in range(M - 1):
    B_imos[i + 1] += B_imos[i]

ans = 0

for i in range(M):
    if B_imos[i] <= K:
        ans = (i + 1)

for i in range(N):
    if A_imos[i] > K:
        continue
    index = or_less(B_imos, K - A_imos[i])[0]
    ans = max(ans, (i + 1) + index)
print(ans)

Submission status: Accepted

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

s_a = [0]
s_b = [0]
for i in range(n):
    s_a.append(s_a[i] + a[i])
for i in range(m):
    s_b.append(s_b[i] + b[i])

ans = 0
tmp = m
for i in range(n+1):
    for j in range(tmp+1)[::-1]:
        if s_a[i] + s_b[j] <= k:
            ans = max(ans, i+j)
            tmp = j
            break
print(ans)

Submission status: Accepted

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

sum_a = [0]
sum_b = [0]

for i in range(N):
    sum_a.append(sum_a[-1] + desk_a[i])
for i in range(M):
    sum_b.append(sum_b[-1] + desk_b[i])

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

print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
N, M, K = map(int, input().split())
As = list(map(int,input().split()))
Bs = list(map(int,input().split()))
a, b = [0], [0]
for i in range(N):
  # a[i+1]: i番目のものまで読むのにかかる総時間
  a.append(a[i]+As[i]) # 累積和の考え方
for i in range(M):
  # aのBバージョン
  b.append(b[i] + Bs[i])

ans = 0
j = M # Bから読める冊数の最大値を入れておく
for i in range(N+1):
  if a[i] > K: # 読めない
    break
  while b[j] > K-a[i]: #すべてのiに対して、a[i]+b[j]<=K になるjを求めたい
    j -= 1
  ans = max(ans, i+j) # 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()))

i = 0
j = 0
count = 0
a = [0]*N
b = [0]*M
ans = 0

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

ind = bisect.bisect_right(b, K)
ans = ind

for i in range(N):
    if a[i] <= K:
        ind = bisect.bisect_right(b, K-a[i])
        ans = max(ans, ind+i+1)
    else:
        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:
n, m, k = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

i, j = 0, 0
c = 0
num = 0
while i < n and c + A[i] <= k:
    c = c + A[i]
    num += 1
    i += 1
while j < m and B[j] + c <= k:
    c = c + B[j]
    num += 1
    j += 1
for a in range(i-1, -1, -1):
    c -= A[a]
    while j < m and c + B[j] <= k:
        c = c + B[j]
        j += 1
        num = max(num, a+j)
print (num)

Submission status: Accepted

Solution code:
import sys
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]

mod = 10 ** 9 + 7

N, M, K = LI()
desk = LIR(2)
a, b = [0], [0]

# a = [0, 0 + A1, 0 + A1 + A2, ....]
# b = [0, 0 + B1, 0 + B1 + B2, ....]
for i in range(N):
    a.append(a[i] + desk[0][i])
for j in range(M):
    b.append(b[j] + desk[1][j])

# a + b <= K を求める　⇒　変形してb >= K - a とすることで、
# aは全件トライ、bのみ選択可能な最大値を算出することで計算量をO(N + M)に抑える
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()))
asum = [0]*(n+1)
bsum = [0]*(m+1)
for i in range(n):
	asum[i+1] = a[i] + asum[i]
for j in range(m): 
	bsum[j+1] = b[j] + bsum[j]
j = m
res = 0
for i in range(n+1):
	if asum[i] > k:
		break
	while asum[i] + bsum[j] > k:
		j-=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()))

sum_a = [0]
sum_b = [0]

for i in range(n):
    sum_a.append(sum_a[i] + a[i])
for j in range(m):
    sum_b.append(sum_b[j] + b[j])

temp = 0
j = m
ans = 0
for i in range(n+1):
    if sum_a[i] > k:
        temp = i -1
        if temp > ans:
            ans = temp
        break
    else:
        while k - sum_a[i] < sum_b[j]:
            j -= 1
        temp = i + j
        if temp > ans:
            ans = temp
            
print(ans)

Submission status: Accepted

Solution code:
A,B,K=map(int,input().split())
List_A = list(map(int, input().split()))
List_B = list(map(int, input().split()))
SumListA=[]
SumListA.append(0)
SumListB=[]
SumListB.append(0)
res =0
a=0
for i in range(A):
  SumListA.append(SumListA[i]+List_A[i])
for i in range(B):
  SumListB.append(SumListB[i]+List_B[i])

j=B
for i in range(A+1):
  if SumListA[i] > K:
    break
  while K-SumListA[i] < SumListB[j]:
    j += -1
  res = max(i+j,res)
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 = [0]
b = [0]
aa = 0
bb = 0
for i in A:
    aa += i
    a.append(aa)
for j in B:
    bb += j
    b.append(bb)

ans = 0
lb = len(b)
j = len(b)-1
for i in range(len(a)):
    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:
import sys
readline = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
#mod = 998244353
INF = 10**18
eps = 10**-7

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

csuma = [0]
csumb = [0]
for ai in a:
    csuma.append(csuma[-1]+ai)
for bi in b:
    csumb.append(csumb[-1]+bi)


ans = 0
for i,csumai in enumerate(csuma):
    ng = m+1
    ok = -1
    while ng - ok > 1:
        mid = (ok + ng)//2
        if csumb[mid] <= k-csumai:
            ok = mid
        else:
            ng = mid
    if ok >= 0:
        ans = max(ans,i+ok)
print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

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

import bisect

ans=0
for i,r in enumerate(ruia):
    nokori =K-r
    if nokori>=0:
        n_nokori = bisect.bisect(ruib,nokori)
        ans = max(ans,i+n_nokori)
#     print(i,r,nokori,n_nokori,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()))

SumA=[0 for _ in range(N+1)]
SumB=[0 for _ in range(M+1)]
for i in range(1,N+1):
    SumA[i]=SumA[i-1]+A[i-1]
for i in range(1,M+1):
    SumB[i]=SumB[i-1]+B[i-1]

def binary_search(Time,IndexA):
    if Time>K:
        return 0 
    l=0
    r=M
    c=(l+r)//2
    while r-l>1:
        if Time+SumB[c]>K: r=c
        if Time+SumB[c]<=K: l=c
        c=(l+r)//2
    if Time+SumB[r]>K: IndexB=l
    else: IndexB=r
    NumOfBooks=IndexA+IndexB
    return NumOfBooks

MaxNumOfBooks=0
for IndexA in range(N+1):
    NumOfBooks=binary_search(SumA[IndexA],IndexA)
    if MaxNumOfBooks<NumOfBooks: MaxNumOfBooks=NumOfBooks
print(MaxNumOfBooks)

Submission status: Accepted

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

SumA=[0 for _ in range(N+1)]
SumB=[0 for _ in range(M+1)]
for i in range(1,N+1):
    SumA[i]=SumA[i-1]+A[i-1]
for i in range(1,M+1):
    SumB[i]=SumB[i-1]+B[i-1]

def binary_search(Time,IndexA):
    if Time>K:
        return 0 
    l=0
    r=M
    c=(l+r)//2
    while r-l>1:
        if Time+SumB[c]>K: r=c
        if Time+SumB[c]<=K: l=c
        c=(l+r)//2
    if Time+SumB[r]>K: IndexB=l
    else: IndexB=r
    NumOfBooks=IndexA+IndexB
    return NumOfBooks

MaxNumOfBooks=0
for IndexA in range(N+1):
    NumOfBooks=binary_search(SumA[IndexA],IndexA)
    if MaxNumOfBooks<NumOfBooks: MaxNumOfBooks=NumOfBooks
print(MaxNumOfBooks)

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
N,M,K = list(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 = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))

index_a = -1
index_b = 0
num_books = 0
now_times = 0

for a in A :
  if now_times + a <= K :
    num_books += 1
    now_times += a
    index_a += 1
  else :
    break

temp_books = num_books
temp_times = now_times
  
while index_a > -2 :
  while index_b < len(B) and temp_times + B[index_b] <= K :
    temp_books += 1
    temp_times += B[index_b]
    index_b += 1
    if temp_books > num_books :
      num_books = temp_books
  temp_books -= 1
  temp_times -= A[index_a]
  index_a -= 1
      
print(num_books)

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 sys
#import time
#from collections import deque, Counter, defaultdict
#from fractions import gcd
import bisect
#import heapq
#import math
import itertools
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
inf = 10**18
MOD = 1000000007
ri = lambda : int(input())
rs = lambda : input().strip()
rl = lambda : list(map(int, input().split()))

n,m,k = rl()
a = rl()
b = rl()
sa=[0]
sb=[]
tmp=0
for i in a:
    tmp+=i
    sa.append(tmp)
tmp=0
for i in b:
    tmp+=i
    sb.append(tmp)
ans=[]
for i,e in enumerate(sa):
    if k-e>=0:
        ans.append(i+bisect.bisect_right(sb, k-e))
print(max(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, 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(n+1):
    if a[i]>k:
        break
    for j in reversed(range(m+1)):
        if a[i]+b[j]<=k:
            ans = max(ans, i+j)
            m=j
            break
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
# import copy
from collections import deque
# import heapq
# import numpy as np
# import scipy
# import numba
# import sklearn
sys.setrecursionlimit(1000000000)
# MOD = 10 ** 9 + 7


def main():
    n, m, k = map(int, input().split())
    A = deque(map(int, input().split()))
    B = deque(map(int, input().split()))

    cnt_a = 0
    time = 0
    readed_a = deque([])

    for a in A:
        if time + a > k:
            break
        cnt_a += 1
        time += a
        readed_a.append(a)

    readed_b = deque([])
    cnt_ab = cnt_a
    maxcnt_b = cnt_ab
    for b in B:
        while True:
            if time + b <= k:       # 読める
                readed_b.append(b)
                cnt_ab += 1
                if maxcnt_b < cnt_ab:
                    maxcnt_b = cnt_ab
                time += b
                break
            else:                   # 読めない
                if len(readed_a) > 0:   # Aを戻せる
                    cnt_ab -= 1
                    time -= readed_a.pop()
                else:                   # Aがもう無い
                    print(max(cnt_a, maxcnt_b))
                    return
    print(max(cnt_a, maxcnt_b))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n,m,k=map(int,input().split())
arr1=list(map(int,input().split()))
arr2=list(map(int,input().split()))
acum1=[0]
for i in range(n): #累積和ai,bjを求める
 acum1.append(acum1[-1]+arr1[i])
acum2=[0]
for i in range(m):
 acum2.append(acum2[-1]+arr2[i])
ans=0
j=m
for i in range(n+1):
 if acum1[i]>k: #ai>Kであれば、どのようにjを選んでもai+bj<=Kとなることはないので探索を終了する
   break
 while acum2[j]>k-acum1[i] and j>0: #各iについて、bj<=K-aiを満たす最大のjを求める
   j-=1
 ans=max(ans,i+j)
print(ans)

Submission status: Accepted

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

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

    asum = [0]*(n+1)
    bsum = [0]*(m+1)

    for i in range(n):
        asum[i+1] = asum[i] + a[i]

    for i in range(m):
        bsum[i+1] = bsum[i] + b[i]

    # jを小さくしていく発想がなかった
    j = m
    res = 0
    for i in range(n+1):
        # asumの分岐をさせてからbsumとの和を考えていく
        if asum[i] > k:
            break
        while asum[i] + bsum[j] > k:
            j -= 1
        
        res = max(res, j+i)
    print(res)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

time = sum(A)
count = len(A)
ans = 0
j=0
for i in range(N,-1,-1):
    if i != N:
        time -= A[i]
        count -= 1
    if time>K:
        continue
    if time <= K and j<M:
        while time+B[j] <=K:
            time +=B[j]
            j +=1
            count +=1
            if j >=M:
                break
    ans = max(ans,count)
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

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])
b_set = set(b)
b_array = np.array(b)
max_cnt = 0

# print(a)
# print(b)

for i in range(N+1):
    temp = K - a[i]
    if temp >= 0:
        cnt = 0
        if temp in b_set:
            # print(b_array.searchsorted(temp))
            cnt = i + b_array.searchsorted(temp)
        else:
            # print(b_array.searchsorted(temp)-1)
            cnt = i + b_array.searchsorted(temp) - 1
        if max_cnt < cnt:
            max_cnt = cnt
print(max_cnt)

Submission status: Accepted

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

cs_A = [0] #si=[0,i)の和 [a,b)の和=sb−sa
for i in A:
    cs_A.append(cs_A[-1] + i)
cs_B = [0] #si=[0,i)の和 [a,b)の和=sb−sa
for i in B:
    cs_B.append(cs_B[-1] + i)

def is_ok(mid, v):
    if v + cs_B[mid] <= K:
        return True
    else:
        return False

def meguru_bisect(v, 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, v):
            ok = mid
        else:
            ng = mid
    return ok
ans = 0
for i in range(0, N+1):
    v = cs_A[i]
    idx = meguru_bisect(v, M+1, -1)
    if 0 <= idx <= M:
        if v + cs_B[idx] <= K:
            ans = max(ans, i + idx) 
print(ans)

Submission status: Accepted

Solution code:
n, m, k = map(int, input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
a = [0] + a
seen = []
runSum = 0
for i, val in enumerate(b):
    runSum += val
    seen.append((runSum, i))
seen.sort()
def findLessEqual(target):
    l, r = 0, len(seen) - 1
    result = -1
    resultVal = 0
    while l <= r:
        mid = (l + r) // 2
        if seen[mid][0] == target:
            return seen[mid][0], seen[mid][1]
        elif seen[mid][0] < target:
            resultVal, result = seen[mid][0], seen[mid][1]
            l = mid + 1
        else:
            r = mid - 1
    return resultVal, result

runSum = 0
result = 0
for i, val in enumerate(a):
    runSum += val
    if runSum <= k:
        complement = k - runSum
        compVal, compI = findLessEqual(complement)
        if compVal + runSum <= k:
            result = max(result, i + compI + 1)
        else:
            result = max(result, i)
print(result)

Submission status: Accepted

Solution code:
from itertools import accumulate
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
N,M,K=MI()
A=[0]+LI()
A=list(accumulate(A))
B=[0]+LI()
B=list(accumulate(B))
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, 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()))

t = 0
ans = 0

for i in range(m):
    t += b[i]

j = m

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

print(ans)

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import collections
import heapq
import re
import numpy as np
from functools import reduce

rr = lambda: sys.stdin.readline().rstrip()
rs = lambda: sys.stdin.readline().split()
ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')
mod = 10**9 + 7

n, m, k = rm()
a = rl()
b = list(itertools.accumulate(rl()))
if a[0] > k:
    ans = 0
else:
    left = 0
    right = m
    while right - left > 1:
        j = (left + right) // 2
        if b[j] > k:
            right = j
        else:
            left = j
    ans = left + 1
t = 0
b0 = b[0]
for cnt, i in enumerate(a, 1):
    t += i
    if t > k:
        break
    if b0 > k-t:
        ans = max(ans, cnt)
        continue
    left = 0
    right = m
    while right - left > 1:
        j = (left + right) // 2
        if b[j] > k-t:
            right = j
        else:
            left = j
    ans = max(ans, cnt + left + 1)
print(ans)

Submission status: Accepted

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

a = [0]
b = [0]
for i in range(N):
    a.append(a[i]+A_list[i])
for i in range(M):
    b.append(b[i]+B_list[i])

j = M
while b[j] > K:
    j -= 1
max = j
i = 0
while a[i]<=K :
    while b[j]> K-a[i]:
        j -= 1
    if max <= i+j:
        max = i+j
    if i < N:
        i += 1
    else:
        break

print(max)

Submission status: Accepted

Solution code:
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
import itertools
N,M,K=MI()
A=[0]+LI()
B=[0]+LI()
A=list(itertools.accumulate(A))
B=list(itertools.accumulate(B))
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,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
best = m
for i in range(n+1):
    if a[i]>k:
        break
    else:
        for j in range(best, -1, -1):
            if b[j]<=k-a[i]:
                best = j
                break
    ans = max(ans, i+j)
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)
# input = sys.stdin.readline    ####
def int1(x): return int(x) - 1
def II(): return int(input())
def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())
def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def MS(): return input().split()
def LS(): return list(input())
def LLS(rows_number): return [LS() for _ in range(rows_number)]
def printlist(lst, k=' '): print(k.join(list(map(str, lst))))
INF = float('inf')
# from math import ceil, floor, log2
# from collections import deque
from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product, permutations
# from heapq import heapify, heappop, heappush
# import numpy as np    # cumsum
from bisect import bisect_left, bisect_right

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

    A = list(accumulate(A))
    B = list(accumulate(B))
    
    # print(A)
    # print(B)
    ans = bisect_right(B, K)
    for i, a in enumerate(A):
        th = K - a
        if th < 0: break
        ib = bisect_right(B, th)
        ans = max(i+1+ib, ans)
    print(ans)
    

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import itertools
import math
import fractions
import functools
import copy
from collections import deque
from functools import reduce
from decimal import Decimal


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

    a_sum = [0]
    b_sum = [0]
    for i in range(len(a)):
        a_sum.append(a_sum[i]+a[i])
    for i in range(len(b)):
        b_sum.append((b_sum[i]+b[i]))

    ans = 0
    j = m
    for i in range(n+1):
        if a_sum[i] > k:
            break
        while b_sum[j] > k - a_sum[i]:
            j -= 1
        ans = max(ans, (i+j))

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# https://qiita.com/u2dayo/items/d83ef1c642302abaf89e#c%E5%95%8F%E9%A1%8Ctsundoku
from itertools import accumulate
import bisect

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

a_acc = [0] + list(accumulate(a))  # aを0冊読む=1冊も読まない場合があるので、[0]を先頭に追加します
b_acc = list(accumulate(b))  # bisect_rightでは、x以下の要素がいくつあるか？を求めるので、こちらに0は入れません

ans = 0
for an in range(N + 1):
    rem = K - a_acc[an]
    if rem < 0:
        # aをこれ以上読めません
        break

    bn = bisect.bisect_right(b_acc, rem)
    ans = max(ans, an + bn)

print(ans)

Submission status: Accepted

Solution code:
def main():
    import bisect
    n,m,k = map(int,input().split())
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))
    A,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(n+1):
        cost = A[i]
        c = i
        if cost > k:
            continue
        cost = k-cost
        c += bisect.bisect_right(B,cost)-1
        if c>ans:
            ans = c
    print(ans)

if __name__ == "__main__":
    main()