Problem:


Score : 300 points

Problem Statement

We have two desks: A and B. Desk A has a vertical stack of N books on it, and Desk B similarly has M books on it.

It takes us A_i minutes to read the i-th book from the top on Desk A (1 \leq i \leq N), and B_i minutes to read the i-th book from the top on Desk B (1 \leq i \leq M).

Consider the following action:

Choose a desk with a book remaining, read the topmost book on that desk, and remove it from the desk.

How many books can we read at most by repeating this action so that it takes us at most K minutes in total? We ignore the time it takes to do anything other than reading.

Constraints

1 \leq N, M \leq 200000

1 \leq K \leq 10^9

1 \leq A_i, B_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M K
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_M

Output

Print an integer representing the maximum number of books that can be read.

Sample Input 1

3 4 240
60 90 120
80 150 80 150

Sample Output 1

3

In this case, it takes us 60, 90, 120 minutes to read the 1-st, 2-nd, 3-rd books from the top on Desk A, and 80, 150, 80, 150 minutes to read the 1-st, 2-nd, 3-rd, 4-th books from the top on Desk B, respectively.

We can read three books in 230 minutes, as shown below, and this is the maximum number of books we can read within 240 minutes.

Read the topmost book on Desk A in 60 minutes, and remove that book from the desk.

Read the topmost book on Desk B in 80 minutes, and remove that book from the desk.

Read the topmost book on Desk A in 90 minutes, and remove that book from the desk.

Sample Input 2

3 4 730
60 90 120
80 150 80 150

Sample Output 2

7

Sample Input 3

5 4 1
1000000000 1000000000 1000000000 1000000000 1000000000
1000000000 1000000000 1000000000 1000000000

Sample Output 3

0

Watch out for integer overflows.



Sample input:
3 4 240
60 90 120
80 150 80 150


Sample output:
3

Submission status: Accepted

Solution code:
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
A=[0]
for i in range(n):
  A.append(A[i]+a[i])
B=[0]
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 itertools import accumulate
from bisect import bisect_right

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 = [0] + list(accumulate(B))

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

Submission status: Accepted

Solution code:
# 累積和, 二分探索
from itertools import accumulate
from bisect import bisect_right

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 = [0] + list(accumulate(B))

result = 0
for i in range(N + 1):
    t = K - a[i]
    if t < 0:
        break
    j = bisect_right(b, t)
    result = max(result, i + j - 1)
print(result)

Submission status: Accepted

Solution code:
from bisect import bisect_right

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

SA = [0] * (N + 1)
SB = [0] * (M + 1)
for i in range(N):
    SA[i + 1] = SA[i] + A[i]
for i in range(M):
    SB[i + 1] = SB[i] + B[i]

ans = 0
for i in range(N + 1):
    cntA = i
    rest = K - SA[i]
    if rest < 0:
        break
    cntB = bisect_right(SB, rest) - 1
    ans = max(ans, cntA + cntB)
print(ans)

Submission status: Accepted

Solution code:
import sys
import math
import heapq
from collections import defaultdict, deque
sys.setrecursionlimit(10**6)
input = sys.stdin.readline
def r():
    return int(input())
def rm():
    return map(int,input().split())
def rl():
    return list(map(int,input().split()))

'''C Tsundoku'''
n,m,k=rm()
a=rl()
b=rl()
c=[0]*(n+1)
d=[0]*(m+1)
for i in range(n):
    c[i+1]=c[i]+a[i]
for i in range(m):
    d[i+1]=d[i]+b[i]
ans=0;j=m
for i in range(n+1):
    if c[i]>k:
        break
    while c[i]+d[j]>k:
        j-=1
    ans=max(ans,i+j)
print(ans)

Submission status: Accepted

Solution code:
#Aの上からi冊、Bの上からj冊をとりi+jを条件を満たしながら最大化したい
#累積和と二分探索の練習問題
#値を固定するという概念。iが決まればjが決まる
from bisect import bisect_left
from itertools import accumulate
n,m,k=list(map(int,input().split()))
a=list(map(int,input().split()))
b=list(map(int,input().split()))
A=[0]+list(accumulate(a))
B=[0]+list(accumulate(b))

ans=0
sup_i=bisect_left(A,k+1)
for i in range(sup_i):
  ans=max(ans,i+bisect_left(B,k-A[i]+1)-1)
 
print(ans)

Submission status: Accepted

Solution code:
from collections import deque
 
def solve():
    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(len(a)):
        if a[i] > K:
            break
        while b[j] > K - a[i]:
            j -= 1
        ans = max(ans, i+j)
    
    print(ans)
            
solve()

Submission status: Accepted

Solution code:
import math

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

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

    #リスト範囲でA+Bの最大値を探索する。2分探索を用いる
    for i in reversed(range(len_aa)):
        time = aa_lst[i]
        read = i + 1
        if read + len_bb + 1 <= max_read:
            break
        imax = len_bb-1
        imin = 0
        imid = len_bb // 2
        while True:
            if time + bb_lst[imid] <= k:
                #OK。大を調べる
                if imax - imid <= 1:
                    #探査終了
                    if time + bb_lst[imax] <= k:
                        read += imax + 1
                    else:
                        read += imid + 1
                    break
                imin = imid
            else:
                #NG。小を調べる
                if imid - imin <= 1:
                    #探査完了
                    if time + bb_lst[imin] <= k:
                        read += imin + 1
                    break
                imax = imid
            imid = (imin + imax) // 2
        max_read = max(read,max_read)
    return max_read


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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
read = lambda:list(map(int, input().split()))
n, m, k = read()
a = read()
b = read()
at, bt = [0], [0]
for i in range(n):
    at.append(at[i] + a[i])
for i in range(m):
    bt.append(bt[i] + b[i])
ans, j = 0, m
for i in range(n + 1):
    if at[i] > k:
        break
    while bt[j] > k - at[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]
sb = [0]
for i in range(n):
    sa.append(sa[-1] + a[i])
for i in range(m):
    sb.append(sb[-1] + b[i])
ans = 0
for i in range(n+1):
    if sa[i] > k:
        continue
    else:
        j = bisect.bisect_right(sb, k-sa[i])
        ans = max(ans, i+j-1)
print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate
from bisect import bisect_right

n, m, k = map(int, input().split())
a = [0] + list(accumulate(list(map(int, input().split()))))
b = [0] + list(accumulate(list(map(int, input().split()))))
ans = 0

for i in range(n + 1):
    j = bisect_right(b, k - a[i])
    if a[i] + b[j - 1] <= k:
            ans = max(ans, i + (j - 1))
    
print(ans)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


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

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

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


def cumsum(a):
    """l[i] = sum(a[:i]) なるlを返す
    sum(a[i:j]) == l[j+1] - l[i]
    """
    c = 0
    l = [None] * (len(a)+1)
    for i,num in enumerate(a):
        l[i] = c
        c += num
    l[-1] = c
    return l

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

r = m-1
s = sum(b)
num = m
best = 0
while s>k and r>=0:
    s -= b[r]
    r -= 1
    num -= 1
if s<=k:
    best = max(best, num)
for l in range(n):
    num += 1
    s += a[l]
    while s>k and r>=0:
        s -= b[r]
        r -= 1
        num -= 1
    if s<=k:
        best = max(best, num)
print(best)

Submission status: Accepted

Solution code:
import sys
import math 
import bisect

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



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

for i in range(n+1):
    tmp = k - a[i]
    if tmp < 0:
        break
    else:
        j=bisect.bisect_right(b,tmp)
        ans= max(ans,i+j-1)
print(ans)

Submission status: Accepted

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


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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import deque
trush_a, trush_b, limit_time = list(map(int,input().split()))
total = [0, 0]
count = [0, 0]
desks = [deque(),deque()]
result = 0

for i in range(2):

    for j in list(map(int,input().split())):
        temp = total[i] + j 
        if temp <= limit_time:
            total[i] += j
            count[i] += 1
            desks[i].append(j)
        else:
            break

while count[0] > 0 or count[1] > 0:
    if count[0] == count[1]:
        select = total[0] > total[1]
    else:
        select = count[0] < count[1]

    temp = desks[select].popleft()
    result += 1
    limit_time -= temp
    total[select] -= temp
    count[select] -= 1

    while total[not select] > limit_time:
        dim_time = desks[not select].pop()
        total[not select] -= dim_time
        count[not select] -= 1

print(result)

Submission status: Accepted

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


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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def main():

    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)


if __name__ == '__main__':
    main()

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, b = [0], [0]
a.extend(np.cumsum(A, dtype="int64"))
b.extend(np.cumsum(B, dtype="int64"))

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

        j -= 1
    best = max(best, i + j)

print(best)

Submission status: Accepted

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


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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from itertools import accumulate
from bisect import bisect
def main():
    n, m, k = map(int, input().split())
    a = tuple(map(int, input().split()))
    b = tuple(map(int, input().split()))

    aa = tuple(accumulate(a))
    ba = tuple(accumulate(b))
    r = 0
    for i1 in range(n):
        if aa[i1] > k:
            break
        else:
            t = bisect(ba, k - aa[i1])
            r = max(r, i1 + 1 + t)
    for j1 in range(m):
        if ba[j1] > k:
            break
        else:
            t = bisect(aa, k - ba[j1])
            r = max(r, j1 + 1 + t)
    print(r)
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()))

b_sum = sum(B)
for i in range(M - 1, -1, -1):
    if b_sum <= K:
        j = i
        break
    b_sum -= B[i]
else:
    j = -1
result = j + 1

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

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,A[0]]
B_sum=[0,B[0]]
for i in range(1,N):
    A_sum.append(A_sum[i]+A[i])
for i in range(1,M):
    B_sum.append(B_sum[i]+B[i])

b_read=0
ans=0
for i in range(N,-1,-1):
    for j in range(b_read,M+1):
        if A_sum[i]+B_sum[j]<=K:
            ans=max(ans,i+j)
            b_read=j
        else:
            break
print(ans)

Submission status: Accepted

Solution code:
import itertools
import numpy as np 
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
# -------------------
n, m, k = map(int, readline().split())
#print(n)
a = list(map(int, readline().split()))
b = list(map(int, readline().split()))
a = [0] + a
b = [0] + b
acc = list(itertools.accumulate(a))
bcc = list(itertools.accumulate(b))

#acc = np.array(acc)
#print(acc)
#bcc = np.array(bcc)

ans = 0
j = m
for i, val in enumerate(acc):
  if (val > k):
    break
  else:
    rest = k - val
    #print(rest)
    #j = m
    #一ループ目がJの最大になるので、外側に置いてOK
    while (bcc[j] > rest):
      j -= 1

    ans = max(ans, j + i)


print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left,bisect_right
n,m,k=map(int,input().split())
arr1=list(map(int,input().split()))
arr2=list(map(int,input().split()))
for i in range(1,n):
    arr1[i]=arr1[i]+arr1[i-1]
for i in range(1,m):
    arr2[i]=arr2[i]+arr2[i-1]
ma=0
for i in range(n):
    if arr1[i]<=k:
        ma=max(ma,bisect_right(arr2,k-arr1[i])+i+1)
for i in range(m):
    if arr2[i]<=k:
        ma=max(ma,bisect_right(arr1,k-arr2[i])+i+1)
print(ma)

Submission status: Accepted

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


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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
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())))

def nibu(data, value):
  left = 0
  right = len(data) - 1
  while right - left > 1:
    mid = (left + right)//2
    if data[mid] == value:
      return mid
    elif value < data[mid]:
      right = mid
    else:
      left = mid
  if value >= data[right]:
    return right
  else:
    return left
  
ans = 0
for i in range(N+1):
  sa = K - A[i]
  if sa < 0:
    break
  j = nibu(B, sa)
  ans = max(ans,i+j)
print(ans)

Submission status: Accepted

Solution code:
#import 
#=input()
#=int(input())
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
#=[list(map(int,input().split())) for _ in range()]

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

Submission status: Accepted

Solution code:
#C
import itertools
import bisect

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

cA = [0] + list(itertools.accumulate(A))
cB = list(itertools.accumulate(B))

#print(cA)
#print(cB)

ans = 0
for i in range(len(cA)):
    tmp = cA[-(i+1)]
    if tmp < k:
        indb = bisect.bisect_right(cB, k - tmp)
        #print(indb)
        ans = max(ans, (n-i)+indb)
    elif tmp == k:
        ans = n - i
    
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
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())))

def nibu(data, value):
  left = 0
  right = len(data) - 1
  while right - left > 1:
    mid = (left + right)//2
    if data[mid] == value:
      return mid - 1
    elif value < data[mid]:
      right = mid
    else:
      left = mid
  if value >= data[right]:
    return right
  else:
    return left
  
ans = 0
for i in range(N+1):
  sa = K - A[i]
  if sa < 0:
    break
  j = nibu(B, sa)
  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()))

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

from bisect import bisect_right

result = 0
a = k
for i in range(n+1):
  index = bisect_right(b, a)
  hon = (index - 1) + (i)
  result = max(result, hon)
  if i < n:
    a -= A[i]
  if a < 0:
    break
print(result)

Submission status: Accepted

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


def cumsum(a):
    """l[i] = sum(a[:i]) なるlを返す
    sum(a[i:j]) == l[j+1] - l[i]
    """
    c = 0
    l = [None] * (len(a)+1)
    for i,num in enumerate(a):
        l[i] = c
        c += num
    l[-1] = c
    return l

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

ca = cumsum(a)
cb = cumsum(b)

def sub(x):
    res = float("inf")
    for i in range(x+1):
        if i>n or (x-i)>m:
            continue
        res = min(res, ca[i] + cb[x-i])
    return res<=k
l = 0
r = n+m+1
while l+1<r:
    mm = (l+r)//2
    if sub(mm):
        l = mm
    else:
        r = mm
print(l)

Submission status: Accepted

Solution code:
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):
    sa.append(sa[-1] + a[i])
inf = 1001001001
sb = [-inf, 0]
for i in range(m):
    sb.append(sb[-1] + b[i])
sb.append(inf)
ans = 0
for i in range(n+1):
    s = sa[i]
    l = 0
    r = len(sb) - 1
    while l+1 < r:
        m = (l+r) //2
        if s + sb[m] <= k:
            l = m
        else:
            r = m
    if l == 0:
        continue
    else:
        ans = max(ans, i+l-1)
print(ans)

Submission status: Accepted

Solution code:
from bisect import *
n, m, k = map(int, input().split())
A = [0]
for x in map(int, input().split()):
    if A[-1]+x > k: break
    A.append(A[-1]+x)
B = [0]
for x in map(int, input().split()):
    if B[-1]+x > k: break
    B.append(B[-1]+x)
ans = 0
for i in range(len(A)):
    j = bisect_right(B, k-A[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, 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:
from bisect import *
n, m, k = map(int, input().split())
A = [0]
for x in map(int, input().split()):
    if A[-1]+x > k: break
    A.append(A[-1]+x)
B = [0]
for x in map(int, input().split()):
    if B[-1]+x > k: break
    B.append(B[-1]+x)

ans = 0
for i in range(len(A)):
    j = bisect_right(B, k-A[i]) - 1
    ans = max(ans, i+j)

# print(k)
# print(A)
# print(B)
print(ans)

Submission status: Accepted

Solution code:
N,M,K=map(int,input().split())
a=input().split()
A=[int(c) for c in a]
b=input().split()
B=[int(c) for c in b]
answers=[0]
As=[0]
Bs=[0]
wholeA=0
wholeB=0
bs = [M]
for a in range(N):
    wholeA+=A[a]
    As.append(wholeA)
for b in range(M):
    wholeB+=B[b]
    Bs.append(wholeB)
for a in range(N+1):
    for b in range(bs[-1],-1,-1):
        whole = As[a]
        whole+=Bs[b]
        if whole<=K:
            answers.append(a+b)
            bs.append(b)
            break
print(max(answers))

Submission status: Accepted

Solution code:
from fractions import gcd
from collections import Counter, deque, defaultdict
from heapq import heappush, heappop, heappushpop, heapify, heapreplace, merge
from bisect import bisect_left, bisect_right, bisect, insort_left, insort_right, insort
from itertools import accumulate, product, permutations, combinations

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

    cumsumA = [0] + list(accumulate(A))
    cumsumB = [0] + list(accumulate(B))

    res = 0

    for i in range(N+1):
        if i == 0:
            for j in range(M, -1, -1):
                if cumsumB[j] <= K:
                    max_j = j
                    res = max(res, i + j)
                    break
        else:
            for j in range(max_j, -1, -1):
                time = cumsumA[i] + cumsumB[j]
                if  time <= K:
                    max_j = j
                    res = max(res, i + j)
                    break
    
    print(res)

if __name__ == '__main__':
    main()

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

Submission status: Accepted

Solution code:
from itertools import accumulate
from bisect import bisect_left
 
n,m,k=map(int,input().split())
a=list(accumulate([0]+list(map(int,input().split()))))
b=list(accumulate([0]+list(map(int,input().split()))))
#print(b)
mx=0
for i in range(n+1):
  if a[i]>k:continue
  #print('a[i];'+str(a[i]))
  idx=min(bisect_left(b,k-a[i]),m)
  #print(idx)
  if b[idx]>k-a[i]:idx-=1
  if a[i]+b[idx]>k:continue
  mx=max(mx,i+idx)
print(mx)

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 = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]

C = [0] * (len(A) + 1)
for i in range(1, len(C)):
    C[i] = C[i-1] + A[i-1]
D = [0] * (len(B) + 1)
for j in range(1, len(D)):
    D[j] = D[j-1] + B[j-1]

ans = 0
j = M
for i in range(N+1):
    if C[i] > K:
        break
    while D[j] > K - C[i]:
        j -= 1
    ans = max(ans, i+j)
    # jの初期化は不要 <- しゃくとり法
print(ans)

Submission status: Accepted

Solution code:
import numpy as np


def main():
    N, M, K = map(int, input().split())
    A = np.array([0] + [int(i) for i in input().split()])
    B = np.array([0] + [int(i) for i in input().split()])

    A.cumsum(out=A)
    B.cumsum(out=B)

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

sumA = [0]
sumB = [0]


for i in range(N):
    sumA.append(sumA[i] + A[i])

for i in range(M):
    sumB.append(sumB[i] + B[i])

ans = 0
t = M

for i in range(N + 1):
    for j in range(t, -1, -1):
        if K < sumA[i] + sumB[j]:
            t -= 1
            continue
        else:
            ans = max(ans, i + j)
            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()))
c=0
A.insert(0,0)
B.insert(0,0)
import numpy as np
A=np.array(A).cumsum()
B=np.array(B).cumsum()

for i in range(N+1):
  d=K-A[i]
  for j in reversed(range(M+1)):
    if d-B[j]>=0:
      if c<i+j:
        c=i+j
      M=j
      break
print(c)

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 = A
BB = B
for i in range(1,N):
    AA[i] += AA[i-1]
for i in range(1,M):
    BB[i] += BB[i-1]
AA = [0] + AA
BB = [0] + BB
import bisect
c = bisect.bisect_right(AA,K)
ans = 0
for i in range(c):
    ans = max(ans, i + bisect.bisect_right(BB,K-AA[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()))

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

input = sys.stdin.readline


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

    time = sum(A)
    for i in reversed(range(N)):
        if time <= K:
            i_A = i
            break
        time -= A[i]

    if time == 0:
        ans = 0
        for i in range(M):
            time += B[i]
            if time > K:
                break
            ans += 1
    else:
        ans = i_A + 1
        cnt = i_A + 1
        for i in range(M):
            time += B[i]
            cnt += 1
            if time > K:
                for j in reversed(range(i_A + 1)):
                    time -= A[i_A]
                    cnt -= 1
                    i_A -= 1
                    if time <= K:
                        break
                if time > K:
                    break
            else:
                if cnt > ans:
                    ans = cnt

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N, M, K = map(int, input().split())
times_a = list(map(int, input().split()))
times_b = list(map(int, input().split()))
read_time = 0
read_time_a = [0]
read_time_b = [0]
for i in range(N):
    read_time_a.append(read_time_a[i]+times_a[i])
for i in range(M):
    read_time_b.append(read_time_b[i]+times_b[i])

ans = 0
for a_count in range(N+1):
    if read_time_a[a_count] > K:
        continue
    a_read_time = read_time_a[a_count]
    max_b_count = 0
    r = len(read_time_b)
    # 消化時間の配列はソートされている状態だから二分探索
    # 計算量をlog(M)に削減
    while max_b_count + 1 < r:
        c = (max_b_count + r) // 2
        # bがインデックスcで時間がaの時間+bの時間でK以下ならmax_b_countを二分探索の開始位置に更新する。
        if a_read_time + read_time_b[c] <= K:
            max_b_count = c
        # 超えちゃったら二分探索の前方に探索領域の終端を絞る
        else:
            r = c
    # 積読消化時間がKより小さく、a_count + max_b_countの最大数が最大になるものをansに代入し続ける
    ans = max(ans, a_count + max_b_count)
print(ans)