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

Submission status: Accepted

Solution code:
N, M, K = map(int, input().split(' '))
A_ls = [0] + list(map(int, input().split(' ')))
for i in range(N):
    A_ls[i + 1] += A_ls[i]
B_ls = [0] + list(map(int, input().split(' ')))
for i in range(M):
    B_ls[i + 1] += B_ls[i]

a_cnt, b_cnt, rst = 0, M, 0
for a_cnt in range(N + 1):
    if A_ls[a_cnt] > K:
        break
    while A_ls[a_cnt] + B_ls[b_cnt] > K and b_cnt > 0:
        b_cnt -= 1
    rst = max(rst, a_cnt + b_cnt)
print(rst)

Submission status: Accepted

Solution code:
from bisect import bisect_right

def bisectsearch_right(L,a):
    i=bisect_right(L,a)
    return(i)

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

Asum=[0]*N
Bsum=[0]*M
Asum[0]=A[0]
Bsum[0]=B[0]
for i in range(1,N):
    Asum[i]=Asum[i-1]+A[i]
for j in range(1,M):
    Bsum[j]=Bsum[j-1]+B[j]
# print(Asum)
# print(Bsum)
ans=[0]*(N+1)
ans[0]=bisectsearch_right(Bsum,K)
# print(ans[0])
for i in range(1,N+1):
    if Asum[i-1]>K:
        continue
    j=bisectsearch_right(Bsum,K-Asum[i-1])
    # print(j)
    ans[i]=i+j
# print(ans)
print(max(ans))

Submission status: Accepted

Solution code:
def solve():
    N,M,K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    ai = 0
    bi = 0
    s = 0

    ans = 0
    for i, a in enumerate(A):
        if s + a > K:
            ai = i-1
            ans = i
            break
        s += a
    else:
        ans = len(A)
        ai = len(A)-1

    #print(ans, ai, s)
    while ai >= -1:
        while bi < len(B):
            if s + B[bi] > K:
                break
            s += B[bi]
            bi += 1
        ans = max(ans, ai+1+bi)
        if ai != -1:
            s -= A[ai]
        ai -= 1

    print(ans)

solve()

Submission status: Accepted

Solution code:
#import sys
#import numpy as np
import math
#from fractions import Fraction
import itertools
from collections import deque
from collections import Counter
import heapq
#from fractions  import gcd
#input=sys.stdin.readline
import bisect
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
ar=[0]*(n+1)
br=[0]*(m+1)
ans=0

for i in range(n):
    ar[i+1]=ar[i]+a[i]
for i in range(m):
    br[i+1]=br[i]+b[i]
for i in range(m+1):
    sup=k-br[i]
    if sup<0:
      break
    res=bisect.bisect_right(ar,sup)+i-1
    ans=max(res,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]
B = [0]

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

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

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

    ans = max(ans, i+j)

print(ans)

Submission status: Accepted

Solution code:
N, M, K = map(int, input().split(' '))
A_ls = [0] + list(map(int, input().split(' ')))
for i in range(N):
    A_ls[i + 1] += A_ls[i]
B_ls = [0] + list(map(int, input().split(' ')))
for i in range(M):
    B_ls[i + 1] += B_ls[i]

a_cnt, b_cnt, rst = 0, M, 0
for a_cnt in range(N + 1):
    if A_ls[a_cnt] > K:
        break
    while A_ls[a_cnt] + B_ls[b_cnt] > K:
        b_cnt -=1
    rst = max(rst, a_cnt + b_cnt)
print(rst)

Submission status: Accepted

Solution code:
N, M, K = map(int, input().split())
a_books = list(map(int, input().split()))
b_books = list(map(int, input().split()))
a_time = 0
b_time = 0
a_times = [0 for _ in range(N+1)]
b_times = [0 for _ in range(M+1)]
for i, a in enumerate(a_books):
  a_time += a
  a_times[i+1] = a_time
  
for i, b in enumerate(b_books):
  b_time += b
  b_times[i+1] = b_time
  
result = 0
l = 0
for i in range(N+1):
  if l == M:
    break
  while(l <= M):
    if a_times[N-i] + b_times[l] <= K:
      result = max(result, N-i+l)
      l += 1
    else:
      break
print(result)

Submission status: Accepted

Solution code:
number=list(map(int,input().split()))
N,M,K=number[0],number[1],number[2]
A=list(map(int,input().split()))
B=list(map(int,input().split()))
a=[0]
b=[0]
for i in range(1,len(A)+1):
    tmp=a[i-1]+A[i-1]
    a.append(tmp)
    
for i in range(1,len(B)+1):
    tmp=b[i-1]+B[i-1]
    b.append(tmp)

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:
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()))
a = [0] + list(accumulate(A))
b = list(accumulate(B))
ans = 0
for i in range(N + 1):
    if (t := K - a[i]) >= 0:
        e = bisect.bisect_right(b, t)
        ans = max(ans, i + e)
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_sum = [0 for i in range(N+1)]
for i in range(N):
    a_sum[i+1] = a_sum[i] + A[i]

b_sum = [0 for i in range(M+1)]
for i in range(M):
    b_sum[i+1] = b_sum[i] + B[i]

ans = 0

for i in range(N+1):
    t = a_sum[i]
    l = 0
    r = len(b_sum)
    while(l+1<r):
        c = (l+r)//2
        if t+b_sum[c]<=K:
            l = c
        else:
            r = c
    if a_sum[i]+b_sum[l]<=K:
        ans = max(ans,i+l)

print(ans)

Submission status: Accepted

Solution code:
# import itertools
# import math
# import sys
import numpy as np

# N = int(input())
# S = input()
# n, *a = map(int, open(0))
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
# S = input()

# d = sorted(d.items(), key=lambda x:x[0]) # keyでsort
# all_cases = list(itertools.permutations(P))
# a = list(itertools.combinations_with_replacement([i for i in range(1, M + 1)], N))
# print(a[0][0])
# print(conditions[0])

A = np.array(A)
B = np.array(B)

cum_A = np.cumsum(A)
cum_A = np.insert(cum_A, 0, 0)
cum_B = np.cumsum(B)
cum_B = np.insert(cum_B, 0, 0)

j = M
max_num = 0
for i in range(N + 1):
    while(True):
        # j -= 1
        if j < 0:
            break
        minute = cum_A[i] + cum_B[j]
        if minute <= K:
            if i + j > max_num:
                max_num = i + j
            break
        j -= 1

print(max_num)

Submission status: Accepted

Solution code:
from bisect import *
from itertools import *
N,M,K = map(int,input().split())
A = [0]+list(accumulate(map(int,input().split())))
B = [0]+list(accumulate(map(int,input().split())))
ans = [0]

for n in range(1+N):
  C = bisect_right(B,K-A[n])-1
  if C!=-1:
    ans+=[C+n]

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

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,j+i)
  
print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate

n, m, k=map(int,input().split())
a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))

j = m
ans = 0

a_acum = [0] + list(accumulate(a_list))
b_acum = [0] + list(accumulate(b_list))

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

Submission status: Accepted

Solution code:
import numpy as np
n,m,k = list(map(int, input().split()))
a_list = np.array(input().split()).astype(int)
b_list = np.array(input().split()).astype(int)
a_sum =[0]
b_sum=[0]
for i in range(n):
    a_sum.append(a_sum[-1]+ a_list[i])
for i in range(m):
    b_sum.append(b_sum[-1] + b_list[i])
#print(a_sum, b_sum)
total = 0
num = m

for i in range(n+1):
    
    if a_sum[i] > k:
        break
    while (k - a_sum[i]) < b_sum[num]:
        num -=1
        #print(i, num)
        if num == -1:
            break
    total = max(i+num, total)
    
    
print(total)

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

print(ans)

Submission status: Accepted

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

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:
n, m, k = map(int, input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))

for i in range(1,n):
    A[i] += A[i-1]
for i in range(1,m):
    B[i] += B[i-1]
A.insert(0,0)
B.insert(0,0)

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 itertools import accumulate

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

sa = [0] + list(accumulate(a))
sb = [0] + list(accumulate(b))

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

Submission status: Accepted

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

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

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


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

print(count)

Submission status: Accepted

Solution code:
N, M, K = map(int, input().split(' '))
A_ls = [0] + list(map(int, input().split(' ')))
B_ls = [0] + list(map(int, input().split(' ')))

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

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

a_cnt, b_cnt, rst = 0, M, 0
while a_cnt < N + 1:
    if A_ls[a_cnt] > K:
        break
    while A_ls[a_cnt] + B_ls[b_cnt] > K and b_cnt > 0:
        b_cnt -= 1
    rst = max(rst, a_cnt + b_cnt)
    a_cnt += 1
print(rst)

Submission status: Accepted

Solution code:
N, M, K = map(int, input().split())
A = [0] + list(map(int, input().split()))
B = [0] + list(map(int, input().split()))
ans, Bi, Asum, Bsum = 0, M, 0, sum(B)
for Ai in range(N + 1):
    Asum += A[Ai]
    while Asum + Bsum > K and Bi > 0:
        Bsum -= B[Bi]
        Bi -= 1
    if Asum + Bsum <= K:
        ans = max(ans, Ai + Bi)
print(ans)

Submission status: Accepted

Solution code:
import math
import collections
import fractions
import itertools
import functools
import operator
import bisect

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

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()))
a_s=[0]
b_s=[0]
for i in range(N):
  if a_s[i]+a[i]>K:
    break
  a_s.append(a[i]+a_s[i])
for i in range(M):
  if b_s[i]+b[i]>K:
    break
  b_s.append(b[i]+b_s[i])
bn=len(b_s)-1
ans=0
for i in range(len(a_s)):
  while a_s[i]+b_s[bn]>K:
    bn-=1
  ans=max(ans,i+bn)
  
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()))
x = [0]
y = [0]
for i in range(n):
    x.append(a[i] + x[i])
for j in range(m):
    y.append(b[j] + y[j])
ans = 0
q = m
for p in range(n + 1):
    if x[p] > k:
        break
    while y[q] > k - x[p]:
        q = q - 1
    ans = max(ans, p + q)
print(ans)

Submission status: Accepted

Solution code:
N, M, K = map(int, input().split(' '))
A_ls = [0] + list(map(int, input().split(' ')))
B_ls = [0] + list(map(int, input().split(' ')))

for i in range(1, N + 1):
    A_ls[i] += A_ls[i - 1]
for i in range(1, M + 1):
    B_ls[i] += B_ls[i - 1]

b_cnt, rst = M, 0
for a_cnt in range(N + 1):
    if A_ls[a_cnt] > K:
        break
    while A_ls[a_cnt] + B_ls[b_cnt] > K:
        b_cnt -= 1
    rst = max(rst, a_cnt + b_cnt)
print(rst)

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:
#!/usr/bin/python3
# -*- coding:utf-8 -*-

import numpy

def main():
  _, _, k = map(int, input().strip().split())
  la = numpy.cumsum([0] + list(map(int, input().strip().split())))
  lb = numpy.cumsum([0] + list(map(int, input().strip().split())) + [10**9+5])
  
  def bs(x):
    s, t = 0, len(lb) - 1
    while t > s + 1:
      m = (s + t) // 2
      if lb[m] == x:
        return m
      if lb[m] > x:
        t = m
      else:
        s = m
    return s
  
  ans = 0
  for i, a in enumerate(la):
    if k - a >= 0:
      ans = max(ans, i + bs((k - a)))
  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()))

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

Submission status: Accepted

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

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

sum = 0
for i in b:
    sum += i

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

print(ans)

Submission status: Accepted

Solution code:
import numpy as np

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

# cumsum
i = 0
A_sum = [0]
while (i < len(A)) and (A_sum[i] + A[i] <= K):
    A_sum.append(A_sum[i] + A[i])
    i += 1
    
i = 0
B_sum = [0]
while (i < len(B)) and (B_sum[i] + B[i] <= K):
    B_sum.append(B_sum[i] + B[i])
    i += 1

# count
max_book = 0
ind = len(B_sum) - 1
for i in range(len(A_sum)):
    while (A_sum[i] + B_sum[ind] > K) and (ind > 0):
        ind -= 1
    if i + ind > max_book:
        max_book = i + ind

print(max_book)

Submission status: Accepted

Solution code:
from collections import deque
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()]
da = deque()
i = 0
tmpK = 0
while tmpK <= K and i < N:
    tmpK += A[i]
    da.append(A[i])
    i += 1
# 一冊多い場合がある
if (tmpK > K):
    i -= 1
    tmpK -= da.pop()

# Bを増やしていく
db = deque()
j = 0
while tmpK <= K and j < M:
    tmpK += B[j]
    db.append(B[j])
    j += 1
# 一冊多い場合がある
if (tmpK > K):
    j -= 1
    tmpK -= db.pop()
tmptot = i+j
while len(da) > 0:
    tmpK -= da.pop()
    i -= 1
    while tmpK <= K and j < M:
        tmpK += B[j]
        db.append(B[j])
        j += 1
    # 一冊多い場合がある
    if (tmpK > K):
        j -= 1
        tmpK -= db.pop()
    if tmptot < i+j:
        tmptot = i+j
print(tmptot)

Submission status: Accepted

Solution code:
from collections import Counter
import sys

sys.setrecursionlimit(10 ** 6)

mod = 1000000007
inf = int(1e18)

dx = [0, 1, 0, -1]
dy = [1, 0, -1, 0]


def inverse(a):
    return pow(a, mod - 2, mod)


def main():
    n, m, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    c = [0] * (m + 1)
    for i in range(m):
        c[i+1] = c[i] + b[i]

    s = 0
    ans = 0
    for i in range(n+1):
        lft = 0
        rgt = m+1
        while rgt - lft > 1:
            mid = (lft + rgt) // 2
            if s + c[mid] <= k:
                lft = mid
            else:
                rgt = mid
        ans = max(ans, i + lft)
        if i < n:
            s += a[i]
        if s > k:
            break
    print(ans)

main()

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()))
ruisekiwa_of_a = [0]
ruisekiwa_of_b = [0]
for a in A:
    ruisekiwa_of_a.append(ruisekiwa_of_a[-1]+a)
for b in B:
    ruisekiwa_of_b.append(ruisekiwa_of_b[-1]+b)
# print(ruisekiwa_of_a)
# print(ruisekiwa_of_b)
ans = 0
for i in range(N+1):
    if ruisekiwa_of_a[i] > K:
        continue
    tmp = i
    tmp += bisect_right(ruisekiwa_of_b, K-ruisekiwa_of_a[i])-1
    ans = max(ans, tmp)
print(ans)

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


#Aだけのときのmax

s = 0
j = 0
for i in range(n):
    if s + a[i] > k:
        break
    else:
        s += a[i]
        j += 1


ans = [j]

for l in range(m):
    flag = True
    s += b[l]
    while s > k:
        s -= a[j-1]
        j -= 1
        if j < 0:
            flag = False
            break
    
    if not flag:
        break
    else:
        ans.append(l + 1 + j)
        #print(ans)

print(max(ans))

Submission status: Accepted

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

A = list(map(int,input().split()))
A.insert(0, 0)
B = list(map(int,input().split()))
a = []
b = []

def nibun(L,l,r,target):

  wrk = -(-(l+r)//2)
  
#  print(l,r,target,wrk)
  
  if r == l:
    if r == len(L):
      return 0
    else:
      return wrk + 1
  
  if l == (len(L)-1):
    if L[wrk-1] > target:
      return wrk
    else:
      return 0

  if L[wrk] == target:
    return wrk+1

  elif L[wrk] > target:
    return nibun(L,l,wrk-1,target)

  else:
    return nibun(L,wrk,r,target)

  
s = 0
for i,e in enumerate(B):
  s += e 
  b.append(s)

s = 0
ans = 0
for i in range(len(A)):
#  print(i,'kaime')
#  print(s,i,A[i])
  s += A[i]
  if s > K:
    break
  else:  
    wrk = nibun(b,-1,len(b),K-s)
    
    ans=max(ans,i+wrk)
#    print('koko',s,wrk,ans)

print(ans)

Submission status: Accepted

Solution code:
#k = int(input())
import bisect
n, m, k = map(int, input().split())
al = list(map(int, input().split()))
bl = list(map(int, input().split()))
#al=[list(input()) for i in range(h)]
acum = [0]
for i in range(n):
    acum.append(al[i]+acum[i])

bcum = [0]
for i in range(m):
    bcum.append(bl[i]+bcum[i])
ans = 0
for i in range(n+1):
    rsd = k-acum[i]
    if rsd < 0:
        break
    #if rsd == 0:
    #    ans = max(ans, i)
    #    continue
    ind = bisect.bisect_right(bcum, rsd)
    ans = max(ans, i+ind-1)
print(ans)

Submission status: Accepted

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

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

ans = 0
b_cnt = m

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

print(ans)

Submission status: Accepted

Solution code:
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
t, ans = sum(B), 0
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:
N,M,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
from itertools import accumulate
accumulated1 = [0] + list(accumulate(A))
accumulated2 = [0] + list(accumulate(B)) + [10**10]
def check(x,now):
    if accumulated1[now] + accumulated2[x] <= K: return True
    else: return False

# [True, .. ,True ,False,.. , False ] のときは探索区間を半開区間で持つ
#[l,r)

def bisect(l,r,now):
    if r - l == 1: return  l
    mid = (r+l)//2
    # l <= r ならば自明に l <= mid <= r  でこれの仮定は呼び出し1,2のいずれでも成立するので始めの呼び出しさえ正しければ永遠に成立.

    # //2 するので mid * 2 == r + l  か  mid * 2 ==  r + l + 1 のどっちか

    # 引数が l == r って時あるか? 
    # いま引数 l, rが l < r として,次の呼び出し1,2を考える.

    # mid == r  <=>  r - l == 0 (仮定より無い)か r - l == 1 (始めのifでカットするので無い)
    # mid == l  <=>  r - l == 0 (仮定より無い)か l - r == 1 (l > r なので無い)

    # 結局 一番始めの呼び出しのl,rを l < r とすれば 
    # l < mid < r となりずーっと l < r が保たれる.
    # l != mid かつ mid != r だから 1,2のどっちを呼び出されても更新される(無限ループとはならない)のでそのうち r - l == 1 になって止まる.

    if check(mid,now):
        #呼び出し1
        return bisect(mid,r,now)
    else:
        #呼び出し2
        return bisect(l,mid,now)


ans = 0
for i in range(N+1):
    if accumulated1[i] > K: pass
    else: ans = max(ans,bisect(0,M+1,i) + 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()))
A=[0 for i in range(n)]
B=[0 for i in range(m)]
A[0]=a[0]
B[0]=b[0]
for i in range(n-1):
    A[i+1]=a[i+1]+A[i]

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

ans=0
A.insert(0,0)
B.insert(0,0)

for i in range(n+1):
    x=0
    y=m
    j=(x+y)//2
    if A[i]+B[m]<=k:
        ans=max(ans,i+m)
    elif not A[i]+B[0]>k:
        
        
        while y-x>1:
            if A[i]+B[j]<=k:
                x=j
            else:
                y=j
            j=(x+y)//2
        ans=max(ans,i+x)
    
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()))
ans = 0
A_cum = [0]
B_cum = [0]
A_cum[0] = A[0]
for j in range(len(A)-1):
    A_cum.append(A_cum[j]+ A[j+1])
B_cum[0] = B[0]
for l in range(len(B)-1):
    B_cum.append(B_cum[l] + B[l+1])
for i in range(len(A_cum)):
    k = A_cum[i]
    if k >K:
        break
    b = bisect.bisect(B_cum,K-k)
    check = i+b+1
    if check>ans:
        ans = check
check = bisect.bisect(B_cum,K)
if check>ans:
    ans = check    
print(ans)

Submission status: Accepted

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

ruisekiwa_of_A = [0]
ruisekiwa_of_B = [0]
for a in A:
    ruisekiwa_of_A.append(ruisekiwa_of_A[-1]+a)
for b in B:
    ruisekiwa_of_B.append(ruisekiwa_of_B[-1]+b)
# ansの最大値をとる
ans = 0
for i in range(N+1):
    tmp = 0
    if K >= ruisekiwa_of_A[i]:
        tmp = i
    else:
        continue
    tmp += bisect_right(ruisekiwa_of_B, K-ruisekiwa_of_A[i])-1
    ans = max(ans, tmp)
print(ans)

Submission status: Accepted

Solution code:
def get_cusum(lst):
    cusum = [0] + lst
    for i in range(1, len(cusum)):
        cusum[i] = cusum[i] + cusum[i-1]
    return cusum


def main():

    n, m, k = map(int, input().split(" "))
    book_a = get_cusum(list(map(int, input().split(" "))))
    book_b = get_cusum(list(map(int, input().split(" "))))
    ans = 0
    top = m

    # print(book_a)
    # print(book_b)

    for i in range(n + 1):
        if book_a[i] > k:
            break

        while book_b[top] > k - book_a[i]:
            top -= 1

        ans = max(ans, i+top)

    print(ans)


main()

Submission status: Accepted

Solution code:
n, m, k = map(int, input().split())
a_books = list(map(int,input().split()))
b_books = list(map(int,input().split()))
sum1 = [0]
sum2 = [0]
for i in range(n):
 sum1.append(sum1[-1]+a_books[i])
for i in range(m):
 sum2.append(sum2[-1]+b_books[i])
ans=0
import bisect
for i in range(n+1):
    if sum1[i] > k:
        break
    j = bisect.bisect_right(sum2, k-sum1[i])-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
for i in range(n+1):
  if a[i]>k:
    break
  for j in range(m,-1,-1):
    if a[i]+b[j]<=k:
      ans=max(ans,i+j)
      m=j
      break
print(ans)

Submission status: Accepted

Solution code:
#k分たったときには机Aの本を上から何冊か、机Bの本を上から何冊か読んでることになる
#つまり机A,Bの方で読んだ本の合計時間は累積和で計算可能
#あとはその範囲内で全探索すればいい
#その際Aの読む数を決めておけばBの方は2分探索で決められる
n,m,k=list(map(int,input().split()))
a=list(map(int,input().split()))
b=list(map(int,input().split()))
from itertools import accumulate
a=[0]+list(accumulate(a))
b=[0]+list(accumulate(b))
from bisect import bisect_right
c=bisect_right(a,k)
ans=0
for i in range(c):
  ans=max(ans,i+bisect_right(b,k-a[i])-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()))

x = [0] * (N + 1)
y = [0] * (M + 1)
for i in range(N):
    x[i + 1] = x[i] + A[i]

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

j = M
ans = 0
for i in range(N + 1):
    if x[i] > K:
        continue
    while j >= 0 and x[i] + y[j] > K:
        j -= 1
    ans = max(ans, i + j)
print(ans)

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
enum = enumerate
INF = 1001001001

import collections
import random

def linput(ty=int, cvt=list):
	return cvt(map(ty,input().split()))

def gcd(a: int, b: int):
	while b: a, b = b, a%b
	return a

def lcm(a: int, b: int):
	return a * b // gcd(a, b)

def dist(x1,y1,x2,y2):
	return abs(x1-x2)+abs(y1-y2)

def ran():
	vRan = [random.randint(1,5) for i in range(8)]
	return vRan

def main():
	if 1:
		N,M,K = linput()
		vA = linput()
		vB = linput()
	else:
		#vA=[5, 3, 3, 5, 3, 4, 1, 4]
		#vB=[5, 1, 4, 1, 1, 2, 1, 2]
		#4
		N,M,K = 0,0,8
		vA = ran()
		vB = ran()
		print(vA,vB,sep="\n")###
	
	res = 0
	
	cnt = 0
	t = 0
	for a in vA:
		if t+a<=K:
			t += a
			cnt += 1
		else: break
	
	res = max(res,cnt)
	vJ = vA[0:cnt]
	for b in vB:
		while t+b>K:
			if not vJ: break
			j = vJ.pop()
			t -= j
			cnt -= 1
		if t+b<=K:
			t += b
			cnt += 1
			res = max(res,cnt)
		else:
			break
		#print(res,cnt)
	
	print(res)
	#sT = "No Yes".split()
	#print(sT[res])
	

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

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

bb = [0]*(m+1)
for i in range(m):
    bb[i+1] = bb[i] + b[i]

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

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

tmp = 0
bb = [0]*(m+1)
for i in range(m):
    tmp += b[i]
    bb[i+1] = tmp

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

print(ans)