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:
#!/usr/bin/env python3

def ans(A, B, K):
    pA = [0]
    for a in A:
        pA += [pA[-1] + a]
    pB = [0]
    for b in B:
        pB += [pB[-1] + b]

    # print(pA, pB)

    j = len(B)

    ans = float("-inf")

    for i in range(0, len(pA)):
        # print("considering", i, j)
        while j > 0 and pA[i] + pB[j] > K: j -= 1
        if pA[i] + pB[j] <= K:
            ans = max(ans, i + j)

    return ans

N, M, K = input().split(' ')
K = int(K)

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

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

print(ans(A, B, K))

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()))
S = [0]*N
T = [0]*M
S[0] = A[0]
for k in range(1,N):
    S[k] = S[k-1] + A[k]
T[0] = B[0]
for k in range(1,M):
    T[k] = T[k-1] + B[k]

ans = bisect.bisect_right(T,K)
for k in range(N):
    if K-S[k] >= 0:
        ans = max(ans,k+1+bisect.bisect_right(T,K-S[k]))
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
N,M,K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))

sA=[0]
sB=[0]

def bin_isOK(index,key):
    if sB[index] <= key:
        return True
    else:
        return False


def binary_search(key):
    ng = len(sB)
    ok = -1

    while abs(ok - ng) >1:
        mid = (ok + ng)//2

        if bin_isOK(mid,key):
            ok = mid
        else:
            ng = mid
    
    return ok

for i in range(N):
    sA.append(sA[i]+A[i])
    
for i in range(M):
    sB.append(sB[i]+B[i])

ans=0
for i in range(len(sA)):
    if sA[i]>K:
        break
    j=binary_search(K-sA[i])
    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(map(int, input().split()))
B = list(map(int, input().split()))

a = list(accumulate(A))
b = list(accumulate(B))

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

Submission status: Accepted

Solution code:
import bisect
def resolve():
    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]

    mx = 0
    for co,v in enumerate(A):
        if v > K:
            break
        cb = bisect.bisect_right(B,K-v)
        
        mx = max(mx, cb+co-1)
    print(mx)
resolve()

Submission status: Accepted

Solution code:
from itertools import accumulate
import sys
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()))))

tmp=m
ans=0
for i in range(n+1):
    while a[i]+b[tmp]>k:
        if tmp>0:tmp-=1
        else:
            print(ans)
            sys.exit()
    ans=max(ans,i+tmp)

print(ans)

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

    m = 0
    k = 0
    ia, ib = 0, 0
    while ia < N:
        a = A[ia]
        if k + a > K:
            break
        k += a
        ia += 1

    while True:
        while ib < M:
            b = B[ib]
            if k + b > K:
                break
            k += b
            ib += 1
        m = max(m, ia + ib)
        if ia > 0:
            ia -= 1
            k -= A[ia]
        else:
            break

    return m

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

SA = [0]*(N+1)
SB = [0]*(M+1)
T = 0


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
    id = bisect.bisect_right(SB, K - SA[i]) - 1
    ans = max(ans, i + id)


print(ans)

Submission status: Accepted

Solution code:
import sys
import math
from collections import deque
import bisect

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()


def make_grid(h, w, num): return [[int(num)] * w for _ in range(h)]


def main():
    N, M, K = NMI()
    A = NLI()
    B = NLI()
    A_cum = [0]*(N+1)
    B_cum = [0]*(M+1)
    for i, a in enumerate(A):
        A_cum[i+1] = A_cum[i] + a
    for i, b in enumerate(B):
        B_cum[i+1] = B_cum[i] + b

    ans = 0
    for i, sa in enumerate(A_cum):
        if K - sa < 0:
            continue
        idx = bisect.bisect_right(B_cum, K-sa)
        ans = max(ans, i+idx)

    print(ans-1)





if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# C

import sys
from collections import defaultdict, deque
from heapq import heappush, heappop
import math
import bisect
input = sys.stdin.readline
import itertools

# 再起回数上限変更
# sys.setrecursionlimit(1000000)

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

a = [0] + list(itertools.accumulate(a))
b = [0] + list(itertools.accumulate(b))


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

print(ans)

Submission status: Accepted

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

ans = 0
pt = len(B)-1
for i,sa in enumerate(SA):
    while(pt>=0):
        if sa + SB[pt] <= K:
            ans = max(ans,i+pt)
            break
        pt-=1
print (ans)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    inf=10**10
    n,m,k=MI()
    aa=LI()
    bb=LI()

    ca=[0]
    for a in aa:ca.append(ca[-1]+a)
    cb=[0]
    for b in bb:cb.append(cb[-1]+b)

    ans=0
    j=m
    for i,a in enumerate(ca):
        while j and a+cb[j]>k:j-=1
        if a+cb[j]>k:break
        ans=max(ans,i+j)
    print(ans)

main()

Submission status: Accepted

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

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

from itertools import accumulate
import bisect

SA=[0]+list(accumulate(A))
SB=[0]+list(accumulate(B))

ANS=0
for i in range(N+1):
    if K-SA[i]<0:
        break
    x=bisect.bisect_right(SB,K-SA[i])
    ANS=max(ANS,i+x-1)
print(ANS)

Submission status: Accepted

Solution code:
def main():
    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
    ac = 0
    for ai in a:
        if ac + ai > k:
            break
        c += 1
        ac += ai
    m = (c, 0)
    d = 0
    for bi in b:
        while ac + bi > k:
            if c <= 0:
                break
            ac -= a[c - 1]
            c -= 1
        if ac + bi > k:
            break
        d += 1
        ac += bi
        if c + d > sum(m):
            m = (c, d)
    print(sum(m))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import numpy as np
import random
from decimal import Decimal
import itertools
import re
import bisect
from collections import deque, Counter
from functools import lru_cache

sys.setrecursionlimit(10**9)
INF = 10**13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
def SERIES(n): return np.fromstring(sys.stdin.buffer.read(), dtype=np.int32, sep=' ')
def GRID(h,w): return np.fromstring(sys.stdin.buffer.read(), dtype=np.int32, sep=' ').reshape(h,-1)[:,:w]
def GRIDfromString(h,w): return np.frombuffer(sys.stdin.buffer.read(), 'S1').reshape(h,-1)[:,:w]
MOD = 1000000007

def main():
	n, m, k = LI()
	a_list = LI()
	b_list = LI()

	count = 0
	books = n
	time = sum(a_list)

	i, j = n-1, 0
	while True:
		if time <= k:
			count = max(count, books)
			if j == m:
				break
			time += b_list[j]
			books += 1
			j += 1
		else:
			if i == -1:
				break
			time -= a_list[i]
			books -= 1
			i -= 1
	print(count)



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from bisect import bisect
import numpy as np

N, M, K = map(int, input().split(' '))
A = np.array([0, *map(int, input().split(' '))])
B = np.array([0, *map(int, input().split(' '))])

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

ans = 0
for i in range(N + 1):
    k = A[i]
    if k > K:
        break

    j = bisect(B, K - k)
    ans = max(ans, i + j - 1)

print(ans)

Submission status: Accepted

Solution code:
def examA():
    N = I()
    ans = N + N**2 + N**3
    print(ans)
    return

def examB():
    S = SI()
    T = SI()
    ans = 0
    for s,t in zip(S,T):
        if s!=t:
            ans += 1
    print(ans)
    return

def examC():
    N, M, K = LI()
    A = LI()
    B = LI()
    SA = 0
    b = 0
    SB = 0
    for i in range(M):
        if SB+B[i]>K:
            break
        SB += B[i]
        b += 1
    #print(b,SB)
    ans = 0
    ans = max(ans,b)
    for i in range(N):
        SA += A[i]
        if SA+SB>K:
            while(b>=0):
                if SA+SB<=K:
                    break
                SB -= B[b-1]
                b -= 1
        if b==-1:
            break
        #print(i,b,SA,SB)

        ans = max(ans,b+i+1)
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

from decimal import getcontext,Decimal as dec
import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def I(): return int(input())
def LI(): return list(map(int,sys.stdin.readline().split()))
def DI(): return dec(input())
def LDI(): return list(map(dec,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = dec("0.000000000001")
alphabet = [chr(ord('a') + i) for i in range(26)]
alphabet_convert = {chr(ord('a') + i): i for i in range(26)}

getcontext().prec = 28

sys.setrecursionlimit(10**7)

if __name__ == '__main__':
    examC()

"""
142
12 9 1445 0 1
asd dfg hj o o
aidn
"""

Submission status: Accepted

Solution code:


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

    a_sum = [0]
    b_sum = [0]

    for e in a:
        a_sum.append(a_sum[-1] + e)
    for e in b:
        b_sum.append(b_sum[-1] + e)

    
    def calc(x):
        """
        aの上からx読んだ場合に、トータル何冊読めるか？
        """
        if a_sum[x] > k:
            return -1

        rest = k - a_sum[x]
        l = 0
        r = m
        if rest >= b_sum[r]:
            return x + r
    
        while l + 1 < r:
            mid = (l + r) // 2
            if rest >= b_sum[mid]:
                l = mid
            else:
                r = mid
        return x + l

    ans = 0
    for i in range(0, n + 1):
        c = calc(i)
        if ans < c:
            ans = c

    print(ans)

        

submit()

Submission status: Accepted

Solution code:
n,m,k = list(map(int,input().split()))
arr = list(map(int,input().split()))
brr = list(map(int,input().split()))
for i in range(1,n):
    arr[i]+=arr[i-1]
for i in range(1,m):
    brr[i]+=brr[i-1]
ans = 0
t = 0
for i in range(n):
    if arr[i]<=k:
        t = i+1
ans = max(ans,t)
for i in range(m):
    if brr[i]<=k:
        t = i+1
ans = max(t,ans)

for i in range(n):
    t = arr[i]
    rem = k-t
    if rem>=0:
        lo = 0
        hi = m-1
        while lo<hi:
            mid = (lo+hi)//2
            if brr[mid]<=rem:
                lo = mid
            else:
                hi = mid
            if hi-lo==1:
                break
        if brr[hi]<=rem:
            ans = max(ans,hi+1+i+1)
        elif brr[lo]<=rem:
            ans = max(ans,lo+1+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()))

prea=[a[0]]*n
preb=[b[0]]*m

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

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


ans=0

tmp=k
ta=0
i=0
while(i<n):
	if(tmp<a[i]):
		break
	tmp-=a[i]
	ta+=1
	i+=1

ans=max(ans,ta)


tmp=k
ta=0
i=0
while(i<m):
	if(tmp<b[i]):
		break
	tmp-=b[i]
	ta+=1
	i+=1

ans=max(ans,ta)

tmp=k

for i in range(n):
	ta=i+1
	rem=tmp-prea[i]
	if(rem<b[0]):
		continue

	low=0
	high=m-1
	val=-1

	while(low<=high):
		mid=(low+high)//2
		if(preb[mid]<=rem):
			val=mid
			low=mid+1
		else:
			high=mid-1
	ta+=val+1
	ans=max(ans,ta)

print(ans)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10**6)
from itertools import accumulate as ac
from bisect import bisect_right as bl
n,m,k=map(int,input().split())
a=[int(i) for i in input().split()]
b=[int(i) for i in input().split()]
A=[0]+list(ac(a))
B=list(ac(b))
ans=0
#print(A,B)
for i in range(n+1):
    nokori=k-A[i]
    if nokori<0:
        print(ans)
        exit()
    ind=bl(B,nokori)
    ans=max(ans,i+ind)
print(ans)

Submission status: Accepted

Solution code:
from bisect import *
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,a in enumerate(sa):
    if a > k:break
    will = bisect_right(sb,k-a)
    ans = max(i + will - 1, 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()))
ta = tb = 0
m = 0
while m<M and tb+B[m]<=K:
    tb += B[m]
    m += 1
ans = m

for n in range(1,N+1):
    ta += A[n-1]
    while ta+tb>K and m>0:
        m -= 1
        tb -= B[m]
    if ta+tb>K:
        break
    ans = max(ans, n+m)

print(ans)

Submission status: Accepted

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

from bisect import bisect_right

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

AA = [0]
for a in A:
    AA.append(AA[-1]+a)
BB = [0]
for b in B:
    BB.append(BB[-1]+b)

ans = 0
for i, aa in enumerate(AA):
    if aa > K: break
    ind = bisect_right(BB, K-aa)-1
    ans = max(ans, ind+i)

print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_right
from numpy import cumsum

n, m, k = map(int, input().split())
alst = list(map(int, input().split()))
blst = list(map(int, input().split()))
a_cum = cumsum(alst)
b_cum = cumsum(blst)
ans = bisect_right(b_cum, k)
for i, num in enumerate(a_cum, start = 1):
    if num > k:
        break
    tmp = k - num
    ans = max(ans, bisect_right(b_cum, tmp) + i)
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right
from itertools import accumulate

n, m, k = map(int, input().split())
la = [0] + list(map(int, input().split()))
lb = list(map(int, input().split()))
# n,m,k=3,4,240
# la = [60, 90, 120]
# lb = [80, 150, 80, 150]

la = list(accumulate(la))
lb = list(accumulate(lb))


# x =bisect_right(lb, 600)
# print(lb)
# print(x)


answer = 0
for ia, a in enumerate(la):
    if a <= k:
        ib = bisect_right(lb, k - a)
        answer = max(answer, ia + ib)


print(answer)

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

Submission status: Accepted

Solution code:
from bisect import bisect_right

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

aca = [0]*(N+1)
acb = [0]*(M+1)

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

ans = 0
for i in range(N+1):
    pos = bisect_right(acb, K-aca[i])-1
    if pos>=0:
        ans = max(ans, i+pos)

print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate, chain

N,M,K = map(int,input().split())
A = tuple(chain((0,),accumulate(map(int,input().split()))))
B = tuple(chain((0,),accumulate(map(int,input().split()))))

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

print(max(it()))

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()]
SA=[0 for i in range(N+1)]
SB=[0 for i in range(M+1)]
for i in range(N):
    SA[i+1]=A[i]+SA[i]
for i in range(M):
    SB[i+1]=B[i]+SB[i]
ans=0
for i in range(N+1):
    if SA[i]>K:
        break
    low=0
    high=M+1
    while(high-low>1):
        mid=(low+high)//2
        #SA[i]+SB[low]<=K<SA[i]+SB[high]
        if SA[i]+SB[mid]<=K:
            low=mid
        else:
            high=mid
    ans=max(ans,i+low)
print(ans)

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

P = [0]
Q = [0]
for i in range(N):
    P.append(P[-1] + A[i])

for i in range(M):
    Q.append(Q[-1] + B[i])

ans = 0
for i in range(N+1):
    if K - P[i] >= 0:
        ans = max(ans, i + bisect_right(Q, K - P[i]) - 1)

print(ans)

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

Submission status: Accepted

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

a_tot = [0] * (n+1)
b_tot = [0] * (m+1)

for i,ai in enumerate(a,1):
    a_tot[i] = a_tot[i-1] + ai

for i,bi in enumerate(b,1):
    b_tot[i] = b_tot[i-1] + bi

# print(a_tot)

b_num = m
ans = 0
for a_num in range(n+1):
    rem = k - a_tot[a_num]
    if(rem < 0):
        break
    if(b_num==0):
        ans = max(ans,a_num)
    else:
        while(b_tot[b_num] > rem):
            b_num -= 1
        ans = max(ans,a_num+b_num)

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

cumA = [0]
cumB = []

a_sum = 0
for a in A:
    a_sum += a
    cumA.append(a_sum)

b_sum = 0
for b in B:
    b_sum += b
    cumB.append(b_sum)


candidates = []
for i, aa in enumerate(cumA):
    if aa > k:
        break

    residue = k - aa
    j = bisect.bisect_right(cumB, residue)
    candidates.append(i + j)

print(max(candidates))

Submission status: Accepted

Solution code:
import sys
import heapq, math
from itertools import zip_longest, permutations, combinations, combinations_with_replacement
from itertools import accumulate, dropwhile, takewhile, groupby
from functools import lru_cache
from copy import deepcopy

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

a = N
b = 0

while a >= 0 and b <= M:
    while a >= 0 and A[a] + B[b] > K:
        a -= 1

    while a >= 0 and b <= M and A[a] + B[b] <= K:
        ans = max(ans, a + b)
        b += 1

print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N, M, K = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

csa = [0] * (N + 1)
csb = [0] * (M + 1)
for i in range(N): csa[i + 1] = csa[i] + a[i]
for i in range(M): csb[i + 1] = csb[i] + b[i]

def check(x):
  for i in range(N + 1):
    if i > x: break
    j = x - i
    if j > M: continue
    if csa[i] + csb[j] <= K: return True
  return False

ok = 0
ng = N + M + 1
while ng - ok > 1:
  m = (ok + ng) // 2
  if check(m): ok = m
  else: ng = m
print(ok)

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()))
    idxA = 0
    idxB = 0
    for i in range(N):
        if A[i] <= K:
            K -= A[i]
            idxA += 1
        else:
            break
    ans = 0
    while idxA >= 0:
        while idxB < M and B[idxB] <= K:
            K -= B[idxB]
            idxB += 1
        ans = max(ans, idxA + idxB)
        idxA -= 1
        K += A[idxA]
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    from itertools import accumulate
    import bisect
    n, m, k = map(int, readline().split())
    a = [0] + list(map(int, readline().split()))
    b = [0] + list(map(int, readline().split()))
    a_cum = list(accumulate(a))
    b_cum = list(accumulate(b))

    ans = 0

    for ai in range(n+1):
        cur = a_cum[ai]
        rem = k - cur
        if rem < 0:
            break
        else:
            bi = bisect.bisect_right(b_cum, rem)
        ans = max(ans, ai + bi - 1)

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

a_cum = [0 for _ in range(n+1)]
b_cum = [0 for _ in range(m+1)]

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

ans = 0
for i in range(n+1):
    if a_cum[i] > k:
        break
    x = bisect.bisect_right(b_cum, k - a_cum[i])
    ans = max(ans, i + x - 1)

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

Submission status: Accepted

Solution code:
import sys; input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**7)
from collections import defaultdict
import bisect
con = 10 ** 9 + 7; INF = float("inf")

def getlist():
	return list(map(int, input().split()))

#処理内容
def main():
	N, M, K = getlist()
	A = getlist()
	B = getlist()
	Asum = [0] * (N + 1)
	Bsum = [0] * (M + 1)
	for i in range(N):
		Asum[i + 1] = Asum[i] + A[i]

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

	ans = 0
	for i in range(N + 1):
		val = i
		cnt = K - Asum[i]
		if cnt < 0:
			continue

		ind = bisect.bisect_left(Bsum, cnt + 1) - 1
		ans = max(ans, val + ind)

	print(ans)



if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N, M, K = mapint()
As = [0]+list(mapint())
Bs = [0]+list(mapint())
from itertools import accumulate
from bisect import bisect_right
ac = list(accumulate(As))
bc = list(accumulate(Bs))

ans = 0
for i in range(N+1):
    now = ac[i]
    rest = K-now
    if rest<0:
        break
    idx = bisect_right(bc, rest)
    ans = max(ans, i+idx-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()))
AX = [0];BX = [0]

for a in A:
    AX.append(AX[-1]+a)
for b in B:
    BX.append(BX[-1]+b)    

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

Submission status: Accepted

Solution code:
import sys
input=lambda: sys.stdin.readline().rstrip()
n,m,k=map(int,input().split())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]
AA=[0]
for i in range(n):
  AA.append(AA[-1]+A[i])
BB=[0]
for i in range(m):
  BB.append(BB[-1]+B[i])
ans=0
import bisect
for i in range(n+1):
  if AA[i]>k:
    break
  else:
    ans=max(ans,i+bisect.bisect_right(BB,k-AA[i])-1)
print(ans)

Submission status: Accepted

Solution code:
import sys
from itertools import accumulate
import bisect

sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inm = lambda: map(int, sys.stdin.readline().split())
inl = lambda: list(inm())
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))

N, M, K = inm()
A = inl()
B = inl()


def solve():
    acum = accumulate(A)
    bcum = list(accumulate(B))
    ans = 0
    for i, b in enumerate(bcum):
        if b <= K:
            ans = i + 1
    for i, x in enumerate(acum):
        if x > K:
            break

        j = bisect.bisect_right(bcum, K - x)
        s = i + 1 + j
        if ans < s:
            ans = s

    return ans


print(solve())

Submission status: Accepted

Solution code:
from bisect import *
N,M,K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
INF=10**12
C=[0]
D=[0]
for i in range(N):
  C.append(C[i]+A[i])
for i in range(M):
  D.append(D[i]+B[i])
P=0
for i in range(N+1):
  if K<C[i]:
    break
  P=max(P,i+bisect_right(D,K-C[i])-1)
print(P)

Submission status: Accepted

Solution code:
from sys import stdin
n,m,k = map(int,stdin.readline().split())
a = list(map(int,stdin.readline().split()))
b = list(map(int,stdin.readline().split()))
su = sum(b)
su1 = 0
ans = m
fin = 0
for i in range(n+1):
    if i:
        su1 += a[i-1]
    while su+su1 > k and ans != 0:
        su -= b[ans-1]
        ans -= 1
    if su+su1 <= k:
        fin = max(fin,ans+i)
print(fin)

Submission status: Accepted

Solution code:
N, M, K = map(int, input().split())
A = [int(a) for a in input().split()]
B = [int(a) for a in input().split()]
s = 0
for i, a in enumerate(A):
    if s + a > K:
        break
    s += a
else:
    i += 1
for j, b in enumerate(B):
    if s + b > K:
        break
    s += b
else:
    j += 1

ans = i + j
while i:
    i -= 1
    s -= A[i]
    while j < M and s + B[j] <= K:
        s += B[j]
        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))
ans = 0

for i in range(N + 1):
    remain = K - A[i]
    if remain < 0:
        continue
    bi = max(0, bisect_right(B, remain) - 1)
    ans = max(ans, i + bi)

print(ans)

Submission status: Accepted

Solution code:
import bisect

n,m,k=map(int,input().split())
arr1=list(map(int,input().split()))
arr2=list(map(int,input().split()))
acum1=[0]
for i in range(n):
  acum1.append(acum1[-1]+arr1[i])
acum2=[0]
for i in range(m):
  acum2.append(acum2[-1]+arr2[i])
ans=0
for i in range(n+1):
  if acum1[i]>k:
    break
  ans=max(ans,i+bisect.bisect_right(acum2,k-acum1[i])-1)
print(ans)