Problem:


Score : 300 points

Problem Statement

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

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

Consider the following action:

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

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

Constraints

1 \leq N, M \leq 200000

1 \leq K \leq 10^9

1 \leq A_i, B_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

3 4 240
60 90 120
80 150 80 150

Sample Output 1

3

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

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

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

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

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

Sample Input 2

3 4 730
60 90 120
80 150 80 150

Sample Output 2

7

Sample Input 3

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

Sample Output 3

0

Watch out for integer overflows.



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


Sample output:
3

Submission status: Accepted

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

    import itertools
    import bisect
    ac = [0] + list(itertools.accumulate(A))
    bc = [0] + list(itertools.accumulate(B))

    ans = 0
    for i, a in enumerate(ac):
        if a > K:
            break
        j = bisect.bisect_right(bc, K - a)
        ans = max(ans, i+j-1)
    print(ans)

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()))
    nid = m-1
    s = sum(B)
    cnt = m
    while s > k:
        s -= B[nid]
        cnt -= 1
        nid -= 1

    ans = cnt
    for a in A:
        s += a
        cnt += 1
        while s > k and nid >= 0:
            s -= B[nid]
            cnt -= 1
            nid -= 1
        if s <= k:
            ans = max(cnt, ans)
    print(ans)

if __name__ == '__main__':
    main()

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=[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])
for i in range(N+1):
  if a[i]>K:
    break
  c=K-a[i]
  ans=max(ans,i+bisect.bisect_right(b,c)-1)
print(ans)

Submission status: Accepted

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

A, B = [0], [0]

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

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

Submission status: Accepted

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

from itertools import accumulate
sum_a_list = list(accumulate([0] + a_list))
sum_b_list = list(accumulate([0] + b_list))

ans = 0
b_idx = m
for a_idx in range(n+1):
    if sum_a_list[a_idx] > k:
        continue
 
    while sum_a_list[a_idx] + sum_b_list[b_idx] > k:
        b_idx -= 1
    
    ans = max(ans, a_idx + b_idx)
        
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()))

    import itertools
    ac = [0] + list(itertools.accumulate(A))
    bc = [0] + list(itertools.accumulate(B))
    ans, j = 0, M
    for i, a in enumerate(ac):
        if a > K:
            break
        while a + bc[j] > K:
            j -= 1
        ans = max(ans, i+j)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

aL = [0 for _ in range(n+1)]
bL = [0 for _ in range(m+1)]

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

ans = 0
j = m

for i in range(n+1):
    if aL[i] > k:
        break
    while bL[j] > k - aL[i]:
        j -= 1
    ans = max(ans, i+j)

print(ans)

Submission status: Accepted

Solution code:
import bisect

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

    a_culm = [0]
    for i in range(n):
        a_culm.append(a_culm[-1] + a[i])
    b_culm = [0]
    for i in range(m):
        b_culm.append(b_culm[-1] + b[i])

    ans = 0
    for i in range(n+1):
        rem = k - a_culm[i]
        if rem < 0:
            break
        b_count = bisect.bisect(b_culm, rem) - 1
        if i + b_count > ans:
            ans = i + b_count

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

t=sum(b)

j=m
ans=0
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 = [0] + list(map(int, input().split(' ')))
b = [0] + list(map(int, input().split(' ')))
j = m
ta, tb =0, sum(b)
r = 0
for i in range(n + 1):
    ta += a[i]
    if ta > k:
        break
    while tb > k - ta:
        tb -= b[j]
        j -= 1
    r = max(r, i + j)
print(r)

Submission status: Accepted

Solution code:
import sys
import itertools
import bisect

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()))
    Acum = list(itertools.accumulate(A))
    Bcum = list(itertools.accumulate(B))
    A = A[:bisect.bisect_right(Acum, K)]
    B = B[:bisect.bisect_right(Bcum, K)]
    base = sum(A)
    books = len(A)
    ans = max(books, len(B))
    i = 0; Bflag = True
    while True:
        if Bflag:
            if i == len(B):
                break
            tmp = base + B[i]
            if tmp > K:
                if len(A) == 0:
                    break
                Bflag = False
                continue
            base = tmp
            books += 1
            i += 1
            ans = max(books, ans)
        else:
            if len(A) == 0:
                Bflag = True
            Atmp = A.pop()
            base -= Atmp
            books -= 1
            if base <= K:
                Bflag = True
    print(ans)
        

if __name__ == "__main__":
    main()

Submission status: Accepted

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

a, b = [0], [0]
for i in range(n):
    a.append(a[i] + A[i])
for 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:
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

t = sum(b)
j = m
ans = 0

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
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from collections import deque
def main():
    N, M, K = map(int, readline().split())
    A = [int(i) for i in readline().split()]
    B = [int(i) for i in readline().split()]

    ca = [0]
    cb = [0]
    for i in range(N):
        ca.append(ca[i] + A[i])
    for i in range(M):
        cb.append(cb[i] + B[i])

    ans = 0  
    j = M
    for i in range(N+1):
        if K < ca[i]:
            break
        while cb[j] > K-ca[i]:
            j -= 1
        ans = max(ans, i+j)
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Accepted

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

sa = [0] * (n+1)
sb = [0] * (m+1)
r = m
ans = 0

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

for i in range(n+1):
    if sa[i] > k:
        break
    while sa[i] + sb[r] > k:
        r -= 1
    ans = max(ans, i+r)
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
a_acc=list(accumulate(a))
b_acc=list(accumulate(b))
a_acc.insert(0,0)
b_acc.insert(0,0)

cnt=0

import bisect
for i in range(n+1):
    nokori=k-a_acc[i]
    if nokori<0:
        break
    b_i=bisect.bisect_right(b_acc,nokori)-1
    cnt=max(cnt,i+b_i)
    # print(nokori,i,b_i)

print(cnt)

Submission status: Accepted

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

def accumulate(l, X):
    acclist = [0]*(l+1)
    for i in range(1,l+1):
        acclist[i] = acclist[i-1] + X[i]
    return acclist

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

X = accumulate(N, A)
Y = accumulate(M, B)

maxbook = 0
j = M
for i in range(N+1):
    x = X[i]
    y = Y[j]
    while x + y > K:
        j -= 1 
        if j < 0:
            break
        y = Y[j]
    if j < 0:
        break

    maxbook = max(maxbook, i + j)

print(maxbook)

Submission status: Accepted

Solution code:
from bisect import bisect
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[-1] + A[i])
b = [0]
for i in range(m):
	b.append(b[-1] + B[i])

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

Submission status: Accepted

Solution code:
import os
import sys
from atexit import register
from io import BytesIO
sys.stdin = BytesIO(os.read(0, os.fstat(0).st_size))
sys.stdout = BytesIO()
register(lambda: os.write(1, sys.stdout.getvalue()))
input = lambda: sys.stdin.readline().rstrip('\r\n')
raw_input = lambda: sys.stdin.readline().rstrip('\r\n')

n,m,k = (int(x) for x in input().split())
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
prea = [0,]
preb = [0,]
for i in range(n):
    prea.append(prea[-1] + a[i])
for i in range(m):
    preb.append(preb[-1] + b[i])
ans = 0
j = m
for i in range(n+1):
    t = prea[i]
    while j>0 and t + preb[j] > k:
        j -= 1
    if t + preb[j] <= k:
        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]*(n+1)
b = [0]*(m+1)

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

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

import sys
import numpy as np
input = sys.stdin.readline

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


A_list.insert(0, 0)
B_list.insert(0, 0)
A_cumsum = np.cumsum(A_list)
B_cumsum = np.cumsum(B_list)

time = B_cumsum[-1]
ans, j = 0, M



for i in range(N+1):
    if A_cumsum[i] > K:
        break
    while B_cumsum[j] > K - A_cumsum[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:
import bisect
import sys
from itertools import accumulate

input = sys.stdin.readline


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

    B_cumsum = tuple(accumulate(B))
    ans = 0
    time = 0
    for i in range(N + 1):
        if time > K:
            break
        j = bisect.bisect_right(B_cumsum, K - time)
        ans = max(ans, i + j)

        if i < N:
            time += A[i]

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import bisect
import sys
from itertools import accumulate

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

    B_cumsum = list(accumulate(B))
    ans = 0
    time = 0
    for i in range(N + 1):
        if time > K:
            break
        j = bisect.bisect_right(B_cumsum, K - time)
        ans = max(ans, i + j)

        if i < N:
            time += A[i]

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import deque
def readinput():
    n,m,k=list(map(int,input().split()))
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    return n,m,k,a,b

def main(n,m,k,a,b):
    qa=deque(a)
    qb=deque(b)
    count=0
    res=k
    while(len(qa)>0 and len(qb)>0):
        if qa[0] < qb[0]:
            res-=qa[0]
            qa.popleft()
        else:
            res-=qb[0]
            qb.popleft()
        if res<0:
            return count
        count+=1
    else:
        if len(qa)==0:
            while(len(qb)>0):
                res-=qb[0]
                qb.popleft()
                if res<0:
                    return count
                count+=1
            return count
        else:
            while(len(qa)>0):
                res-=qa[0]
                qa.popleft()
                if res<0:
                    return count
                count+=1
            return count

import sys
INFTY=sys.maxsize
def find(t,s):
    '''
    単調増加配列sからs[i]がtを超えない最大のiを返す
    '''
    ss=s[:]
    ss.append(INFTY)
    left=0
    right=len(ss)
    while(left<right):
        middle=(left+right)//2
        #print(left,middle,right,t,ss)
        if ss[middle]==t:
            #print(middle, ss)
            return middle
        if t<ss[middle]:
            right=middle
        else:
            left=middle+1
    else:
        return left-1

def main2(n,m,k,a,b):
    sa=[0]
    for i in range(1,n+1):
        sa.append(sa[i-1]+a[i-1])
    sb=[0]
    for i in range(1,m+1):
        sb.append(sb[i-1]+b[i-1])
    maxab=0
    nb=m+1
    for na in range(0,n+1):
        res=k-sa[na]
        if res>=0:
            nb=find(res,sb[:nb+1])
            #print(na,nb,sa,sb)
            maxab=max(maxab,na+nb)
    return maxab

from bisect import bisect_left, bisect_right
def find_le(a, x):
    'Find rightmost index less than or equal to x'
    i = bisect_right(a, x)
    if i:
        return i-1
    raise ValueError

def main3(n,m,k,a,b):
    sa=[0]
    for i in range(1,n+1):
        sa.append(sa[i-1]+a[i-1])
    sb=[0]
    for i in range(1,m+1):
        sb.append(sb[i-1]+b[i-1])
    maxab=0
    nb=m+1
    for na in range(0,n+1):
        res=k-sa[na]
        if res>=0:
            nb=find_le(sb,res)
            #print(na,nb,res,sa,sb)
            maxab=max(maxab,na+nb)
    return maxab

def main4(n,m,k,a,b):
    sa=[0]
    for i in range(1,n+1):
        sa.append(sa[i-1]+a[i-1])
    sb=[0]
    for i in range(1,m+1):
        sb.append(sb[i-1]+b[i-1])
    maxab=0
    nb=m
    for na in range(0,n+1):
        res=k-sa[na]
        while res<sb[nb] and nb>=0:
            nb-=1
        else:
            if nb>=0:
                maxab=max(maxab,na+nb)
        #print(na,nb)
    return maxab

def readinput2():
    m=2*10**5
    n=2*10**5
    a=[10**9]*n
    b=[10**9]*m
    k=1
    return n,m,k,a,b

if __name__=='__main__':
    n,m,k,a,b=readinput()
    ans=main3(n,m,k,a,b)
    print(ans)

Submission status: Accepted

Solution code:
#cited from hahho
from itertools import accumulate

N,M,K = map(int,input().split())
A, B = [0], [0]
A.extend(list(accumulate(map(int,input().split()))))
B.extend(list(accumulate(map(int,input().split()))))

def it():
    j = M
    for i ,a in enumerate(A):
        while j >= 0 and a + B[j] > K:
            j -= 1
        if j >= 0:
            yield i + j
            
print(max(it()))

Submission status: Accepted

Solution code:
import sys
#import string
#from collections import defaultdict, deque, Counter
#import bisect
#import heapq
#import math
#from itertools import accumulate
#from itertools import permutations as perm
#from itertools import combinations as comb
#from itertools import combinations_with_replacement as combr
#from fractions import gcd
#import numpy as np

stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)
MIN = -10 ** 9
MOD = 10 ** 9 + 7
INF = float("inf")
IINF = 10 ** 18

def solve():
    #n = int(stdin.readline().rstrip())
    N,M,K = map(int, stdin.readline().rstrip().split())
    A = list(map(int, stdin.readline().rstrip().split()))
    B = list(map(int, stdin.readline().rstrip().split()))
    #numbers = [[int(c) for c in l.strip().split()] for l in sys.stdin]
    #word = [stdin.readline().rstrip() for _ in range(n)]
    #number = [[int(c) for c in stdin.readline().rstrip()] for _ in range(n)]
    #zeros = [[0] * w for i in range(h)]
    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, 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__':
    solve()

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()))
ans = 0
b_sum=[0]
cnt = 0
for i,b in enumerate(b_list):
  b_sum.append(b_sum[i] + b)
  if b_sum[i+1]<=K:
    cnt+=1
    ans=max(ans,cnt)

a_sum=0
for i,a in enumerate(a_list):
  a_sum+=a
  pos = K - a_sum
  books = i+1
  if pos<0:
    break
  
  left = 0
  right = len(b_sum)-1
  while left<=right:
    mid = (left+right)//2

    if b_sum[mid]>pos:
        right = mid - 1
    else:
        left = mid + 1
  books+=right
  ans = max(ans, books)

print(ans)

Submission status: Accepted

Solution code:
n, m, k = map(int, input().split())
a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))
new_a_list = [[0, 0]]
a_sum = 0
for i in range(0, n):
    a_sum += a_list[i]
    if a_sum <= k:
        new_a_list.append([i + 1, a_sum])
    else:
        break

best = len(new_a_list) - 1
b_sum = 0
a_sum = new_a_list[-1][1]
a_count = new_a_list[-1][0]
flag = 1
for i in range(0, m):
    b_sum += b_list[i]
    while True:
        if b_sum <= k - a_sum:
            if best < i + 1 + a_count:
                best = i + 1 + a_count
            break
        if a_count == 0:
            flag = 0
            break
        a_count -= 1
        a_sum = new_a_list[-(len(new_a_list) - a_count)][1]
    if flag == 0:
        break
print(best)

Submission status: Accepted

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

ta, tb = 0, sum(B)
ptr = M
ans = 0
for i in range(N+1):
    while ptr > 0 and ta + tb > K:
        ptr -= 1
        tb -= B[ptr]
    if ta + tb <= K:
        ans = max(ans, i + ptr)
    if i < N:
        ta += A[i]
print(ans)

Submission status: Accepted

Solution code:
from collections import deque
def readinput():
    n,m,k=list(map(int,input().split()))
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    return n,m,k,a,b

def main(n,m,k,a,b):
    qa=deque(a)
    qb=deque(b)
    count=0
    res=k
    while(len(qa)>0 and len(qb)>0):
        if qa[0] < qb[0]:
            res-=qa[0]
            qa.popleft()
        else:
            res-=qb[0]
            qb.popleft()
        if res<0:
            return count
        count+=1
    else:
        if len(qa)==0:
            while(len(qb)>0):
                res-=qb[0]
                qb.popleft()
                if res<0:
                    return count
                count+=1
            return count
        else:
            while(len(qa)>0):
                res-=qa[0]
                qa.popleft()
                if res<0:
                    return count
                count+=1
            return count

import sys
INFTY=sys.maxsize
def find(t,s):
    '''
    単調増加配列sからs[i]がtを超えない最大のiを返す
    '''
    ss=s[:]
    ss.append(INFTY)
    left=0
    right=len(ss)
    while(left<right):
        middle=(left+right)//2
        #print(left,middle,right,t,ss)
        if ss[middle]==t:
            #print(middle, ss)
            return middle
        if t<ss[middle]:
            right=middle
        else:
            left=middle+1
    else:
        return left-1

def main2(n,m,k,a,b):
    sa=[0]
    for i in range(1,n+1):
        sa.append(sa[i-1]+a[i-1])
    sb=[0]
    for i in range(1,m+1):
        sb.append(sb[i-1]+b[i-1])
    maxab=0
    nb=m+1
    for na in range(0,n+1):
        res=k-sa[na]
        if res>=0:
            nb=find(res,sb[:nb+1])
            #print(na,nb,sa,sb)
            maxab=max(maxab,na+nb)
    return maxab

from bisect import bisect_left, bisect_right
def main3(n,m,k,a,b):
    sa=[0]
    for i in range(1,n+1):
        sa.append(sa[i-1]+a[i-1])
    sb=[0]
    for i in range(1,m+1):
        sb.append(sb[i-1]+b[i-1])
    maxab=0
    nb=m+1
    for na in range(0,n+1):
        res=k-sa[na]
        if res>=0:
            nb=bisect_right(sb,res)-1
            #print(na,nb,res,sa,sb)
            maxab=max(maxab,na+nb)
    return maxab

def main4(n,m,k,a,b):
    sa=[0]
    for i in range(1,n+1):
        sa.append(sa[i-1]+a[i-1])
    sb=[0]
    for i in range(1,m+1):
        sb.append(sb[i-1]+b[i-1])
    maxab=0
    nb=m
    for na in range(0,n+1):
        res=k-sa[na]
        while res<sb[nb] and nb>=0:
            nb-=1
        else:
            if nb>=0:
                maxab=max(maxab,na+nb)
        #print(na,nb)
    return maxab

def readinput2():
    m=2*10**5
    n=2*10**5
    a=[10**9]*n
    b=[10**9]*m
    k=1
    return n,m,k,a,b

if __name__=='__main__':
    n,m,k,a,b=readinput()
    ans=main3(n,m,k,a,b)
    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 = list(filter(lambda x: x <= k, accumulate(A)))
B = list(filter(lambda x: x <= k, accumulate(B)))
ans = 0
for i, a in enumerate(A):
    j = bisect_right(B, k-a)
    ans = max(ans, i+j+1)
ans = max(ans, len(B))
print(ans)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from collections import deque
def main():
    N, M, K = map(int, readline().split())
    A = [int(i) for i in readline().split()]
    B = [int(i) for i in readline().split()]

    #方針:A,Bそれぞれ前からi,j番目までとったときの合計がK以内であるときのi+jを求める
    #A,Bそれぞれの山の一番上の本だけを比較して時間が小さい方を選ぶことを繰り返すのは間違い。

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

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

    mx = 0
    for i in range(N+1):
        # r:= Aの山の上からi番目までの本を読むとき、残りの時間
        r = K-csumA[i]
        if r < 0:
            break
        left = -1
        right = M+1
        while right-left>1: 
            mid = (left+right)//2
            if r >= csumB[mid]:
                left = mid
            else:
                right = mid

        mx = max(mx, i+left)
    print(mx)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from collections import deque
def main():
    N, M, K = map(int, readline().split())
    A = [int(i) for i in readline().split()]
    B = [int(i) for i in readline().split()]

    #方針:A,Bそれぞれ前からi,j番目までとったときの合計がK以内であるときのi+jを求める
    #A,Bそれぞれの山の一番上の本だけを比較して時間が小さい方を選ぶことを繰り返すのは間違い。

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

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

    mx = 0
    for i in range(N+1):
        # r:= Aの山の上からi番目までの本を読むとき、残りの時間
        if K < csumA[i]:
            break
        left = -1
        right = M+1
        while right-left>1: 
            mid = (left+right)//2
            if K - csumA[i] >= csumB[mid]:
                left = mid
            else:
                right = mid
        mx = max(mx, i+left)
    print(mx)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import itertools
import bisect

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

a_sum = [0] + list(itertools.accumulate(A))
b_sum = [0] + list(itertools.accumulate(B))

ans= 0

for i in range(n+1):
    if a_sum[i] > k:
        break
    tmp = bisect.bisect_right(b_sum, k - a_sum[i]) -1
    if i + tmp > ans:
        ans = i + tmp

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()))
suma = [0]
sumb = [0]
for i in range(n):
    suma.append(suma[-1] + a[i])
for i in range(m):
    sumb.append(sumb[-1] + b[i])
ans = 0
for anum in range(n+1):
    remainder = k - suma[anum]
    if remainder < 0:
        break
    bnum = bisect.bisect_right(sumb, remainder)
    ans = max(ans, anum + bnum - 1)
print(ans)

Submission status: Accepted

Solution code:
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8') 
inf = float('inf') ;mod = 10**9+7 
mans = inf ;ans = 0 ;count = 0 ;pro = 1

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])
#print(a, b)
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:
# -*- coding: utf-8 -*-
import bisect
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

# Bの本を上から読んでいった時の累積時間をリストに入れる
B_acc = [0] * M
B_acc[0] = B[0]
for i in range(1, M):
  B_acc[i] = B_acc[i-1] + B[i]

A_acc = 0
count_max = bisect.bisect(B_acc, K - A_acc)
for i in range(N):
  A_acc += A[i]
  if A_acc <= K:
    count = (i + 1) + bisect.bisect(B_acc, K - A_acc)
    count_max = max(count_max, count)
  else:
    break
print(count_max)

Submission status: Accepted

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

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

num=0
bmax=M+1
for n in range(N+1):
    if a[n]>K:
        break
    for m in reversed(range(bmax)):
        if b[m]<=K-a[n]:
            num=max(num,n+m)
            bmax=m+1
            break
print(num)

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=[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
for a_cnt in range(n+1):
    if A[a_cnt]>k:
        continue
    start=0
    goal=m+1
    while start!=goal:
        b_cnt=(start+goal)//2
        if A[a_cnt]+B[b_cnt]>k:
            goal=b_cnt
        else:
            start=b_cnt+1
            ans=max(ans,a_cnt+b_cnt)
print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate
from bisect import bisect_left,bisect_right
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 i in range(n+1):
    c=bisect_right(b,k-a[i])-1
    if c!=-1:
        ans.append(c+i)
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,j=0,M
for i in range(N+1):
   if a[i]>K:
      break
   while K-a[i]<b[j]:
      j-=1
   ans=max(ans,i+j)
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
data = stdin.readlines()

n = int(data[0].split()[0])
m = int(data[0].split()[1])

k = int(data[0].split()[2])

A = [int(s) for s in data[1].split()]
B = [int(s) for s in data[2].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 a[i]+b[j] > k:
            j -= 1
        ans = max(ans, i+j)
print(ans)

Submission status: Accepted

Solution code:
n,m,k,*a=map(int,open(0).read().split())
s=sum(a[:n])
x=i=j=n
a+=k,
while~i:
 while s+a[j]<=k:s+=a[j];j+=1
 if s<=k:x=max(x,i+j)
 i-=1;s-=a[i]
print(x-n)

Submission status: Accepted

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

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

AD = [0]
for i in range(N):
    AD.append(AD[i]+A[i])
BD = [0]
for i in range(M):
    BD.append(BD[i]+B[i])

ans = 0
for i in range(N+1):
    if K < AD[i]:
        break
    else:
        while BD[M] > K - AD[i]:
            M -= 1
        ans = max(ans, i + M)

print(ans)