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, math
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
mod = 10**9 + 7

N, M, K = rl()
A = rl()
B = rl()

s = 0
for i, b in enumerate(B):
	s += b
	B[i] = s

import bisect

ans = 0
s = 0
j = bisect.bisect_right(B, K)
ans = j
for i, a in enumerate(A):
	s += a
	if s > K:
		break
	j = bisect.bisect_right(B, K-s)
	#print(i, j)
	n = i+1 + j
	ans = max(ans, n)
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_right as br
n,m,t=map(int,input().split())
arr=[]
brr=[]
for i in range(1):
    arr=list(map(int,input().split()))
    
for i in range(1):
    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]

#print(arr)
ans=0
for i in range(n):
    if(arr[i]<=t):
        ind=br(brr,t-arr[i])
        #print(t-arr[i],ind)
        ans=max(ans,ind+i+1)
ind=br(brr,t)
ans=max(ans,ind)
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n,m,k = inpl()
a = inpl()
b = inpl()
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])

res = 0
for i in range(n+1):
    rem = k-A[i]
    if rem < 0:
        break
    can = bisect_right(B,rem)
    # print(i,rem,can)
    res = max(res, can + i-1)
print(res)

Submission status: Accepted

Solution code:
N,M,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
a = 0
b = 0
ans = 0
t = sum(A)
temp = 0
if t <= K:
    ans = N
j = 0
ans2 = 0
for i in range(0,N+1):
    if i != 0:
        t -= A[-i]
    while t < K and j < len(B):
        if t + B[j] <= K:
            ans2 += 1
            t += B[j]
            j += 1
        else:
            break
    if t <= K:
        ans = max(ans,len(A)-i+ans2)
print(ans)

Submission status: Accepted

Solution code:
import sys
from bisect import bisect_right


def _ia(): return map(int, sys.stdin.readline().strip().split())


def main():
    n, m, k = _ia()
    a = [0] + list(_ia())
    b = [0] + list(_ia())
    for i in range(n):
        a[i+1] += a[i]
    for i in range(1, m):
        b[i+1] += b[i]

    mx = 0
    for i, tot in enumerate(a):
        km = k - tot
        if km < 0:
            continue

        j = bisect_right(b, km) - 1
        tot += b[j]
        if tot <= k:
            mx = max(mx, i+j)

    return mx


if __name__ == "__main__":
    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()))

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

ans=0
for i in range(N+1):
    time=K-timeA[i]
    if time<0:
        break
    num=bisect.bisect_right(timeB,time)
    ans=max(ans,i+num-1)
print(ans)

Submission status: Accepted

Solution code:
# 
import sys

from sys import stdin
input = lambda: stdin.readline().rstrip()


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

a_idx = 0
b_idx = -1
ans = 0
k = 0
for i in range(len(A)):
    if k + A[i] < K:
        k = k + A[i]
        a_idx = i + 1
        ans = i
    else :
        k = k + A[i]
        a_idx = i + 1
        ans = i
        break

now = ans + 1

if a_idx == len(A):
    A.append(0)
    a_idx = a_idx + 1
    now = now + 1

try:
    for i in reversed(range(a_idx)):
        # Aを一冊どかす
        k = k - A[i]
        now = now - 1

        # Bが何冊引けるか
        b_idx_tmp = b_idx + 1
        tmp = k + B[b_idx_tmp]
        while tmp <= K:
            b_idx_tmp = b_idx_tmp + 1
            tmp = tmp + B[b_idx_tmp]

        # 最後に足されたBは足しちゃいけなかったのでマイナスする
        # スタート時と引いたあとのidxの差が新たに追加された冊数
        k = tmp - B[b_idx_tmp]
        drawed = (b_idx_tmp-1) - b_idx
        now = now + drawed
        b_idx = b_idx_tmp - 1
        ans = max(now, ans)
#        print("-------")
#        print("a_idx = " + str(i))
#        print("b_idx = " + str(b_idx))
#        print("now = " + str(now))
#        print("k = " + str(k))
#        print("b_idx_tmp = " + str(b_idx_tmp))
#        print("drawed = " + str(drawed))

except IndexError:
    # 引けなくなっちゃった場合なので、計算だけする
    # Bだけパターンは考慮しなくていい（B全て+Aを足してもKに達していないパターンなので）
    # k = tmp - B[b_idx_tmp] 足せなかったので引かなくていい
    drawed = (b_idx_tmp-1) - b_idx
    now = now + drawed
    b_idx = b_idx_tmp-1
    ans = max(now, ans)
 
print(ans)

Submission status: Accepted

Solution code:
import sys
#import numpy as np
#from collections import defaultdict
import math
#from collections import deque

#input = sys.stdin.readline
#import numpy as n
import bisect



def main():

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

    A = list(map(int,input().split()))
    B = list(map(int, input().split()))
    cumA=[0]*(len(A)+1)
    cumB = [0] * (len(B)+1)
    tempA=0
    for i in range(1,len(A)+1):
        cumA[i]=tempA+A[i-1]
        tempA+= A[i-1]
    tempB = 0
    for i in range(1,len(B)+1):
        cumB[i] = tempB + B[i-1]
        tempB+= B[i-1]
#    print(cumB)
    ans =0
    for i in range(len(A)+1):
        res = K - cumA[i]
        if res<0:
            break
        b_read = bisect.bisect_right(cumB,res)
        ans =max( i + b_read,ans)
 #       print("b_read {}// i {}//ans {}".format(b_read,i,ans))

    print(ans-1)


if __name__ == "__main__":
    main()

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()))
ai = -1
bi = -1
answer = 0
t_sum = 0
if sum(a) + sum(b) <= k:
    print(n + m)
elif sum(a) <= k:
    t_sum = sum(a)
    ai = n-1
    while True:
        bi += 1
        if bi == m:
            break
        if t_sum + b[bi] > k:
            break
        t_sum += b[bi] 
    bi -= 1
    answer = ai + bi + 2
    while ai >= 0:
        t_sum -= a[ai]
        ai -= 1
        while bi <= m-1:
            bi += 1
            if bi == m:
                break
            if t_sum + b[bi] > k:
                break
            t_sum += b[bi]
        bi -= 1
        if ai + bi +2 > answer:
            answer = ai + bi +2
    print(answer)
else:
    while True:
        ai += 1
        if t_sum + a[ai] > k:
            break
        t_sum += a[ai]
    ai -= 1
    while True:
        bi += 1
        if bi == m:
            break
        if t_sum + b[bi] > k:
            break
        t_sum += b[bi]
    bi -= 1
    answer = ai + bi + 2
    while ai >= 0:
        t_sum -= a[ai]
        ai -= 1
        while bi <= m-1:
            bi += 1
            if bi == m:
                break
            if t_sum + b[bi] > k:
                break
            t_sum += b[bi]
        bi -= 1
        if ai + bi +2 > answer:
            answer = ai + bi +2
    print(answer)

Submission status: Accepted

Solution code:
### ----------------
### ここから
### ----------------

import sys
from io import StringIO
import unittest

def yn(b):
    print("Yes" if b==1 else "No")
    return

def resolve():
    readline=sys.stdin.readline

    n,m,k=map(int, readline().rstrip().split())
    aa=list(map(int, readline().rstrip().split()))
    bb=list(map(int, readline().rstrip().split()))
    asum=[0]*n
    bsum=[0]*m
    asum[0]=aa[0]
    bsum[0]=bb[0]
    mp=n-1
    ans=0
    for i in range(1,n):
        asum[i]=(aa[i]+asum[i-1])
    for i in range(1,m):
        bsum[i]=(bb[i]+bsum[i-1])

    for i in range(m+1):
        if i==0:
            x=0
        else:
            x = bsum[i-1]
        while mp>=0:
            if asum[mp]+x <= k:
                break
            else:
                mp-=1
        if mp>=0:
            if asum[mp]+x <= k:
                ans = max(i + mp+1,ans)
        else:
            if x <= k:
                ans = max(i,ans)
    print(ans)

    return

if 'doTest' not in globals():
    resolve()
    sys.exit()

### ----------------
### ここまで 
### ----------------

Submission status: Accepted

Solution code:
import sys
from itertools import accumulate
import bisect

def input():
    return sys.stdin.readline()[:-1]

def mi():
    return map(int, input().split())

def ii():
    return int(input())

def main():
    N, M, K = mi()
    A = list(mi())
    B = list(mi())

    Aacc = [0]+list(accumulate(A))
    Bacc = list(accumulate(B))
    Baccz = [0]+Bacc

    m = 0

    # print(Aacc)
    # print(Bacc)

    for i in range(N+1):
        t = Aacc[i]
        j = bisect.bisect_right(Bacc, K-t)
        if Aacc[i]+Baccz[j] <= K:
            m = max(m, i+j)

    print(m)


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

Ai = 0
Bi = 0
t = 0
result = 0

while True:
    if Ai>=N:
        break
    elif t + A[Ai] <= K:
        t += A[Ai]
        Ai += 1
    else:
        break
result = Ai

while True:
    if Bi >=  M:
        break
    if t + B[Bi] <= K:
        t += B[Bi]
        Bi += 1
        if Ai+Bi > result:
            result = Ai+Bi
    else:
        if Ai < 1:
            break
        t -= A[Ai-1]
        Ai -= 1

print(result)

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_ = list(itertools.accumulate(A))
B_ = list(itertools.accumulate(B))

ans = 0
l = len(A_)
ans = max(ans, bisect.bisect_right(B_, K))
for idx, i in enumerate(A_):
  if i <= K:
          ans = max(idx+1 + bisect.bisect_right(B_, K-i), ans)
  
print(ans)

Submission status: Accepted

Solution code:
n,m,k=map(int,input().split())
a=[int(x) for x in input().split()]
b=[int(x) for x in input().split()]
t=0
for p in range(n):
    if t+a[p]>k:
        p-=1
        break
    t+=a[p]
for q in range(m):
    if t+b[q]>k:
        q-=1
        break
    t+=b[q]
ans=p+q+2
if q<m-1:
    z=ans
    q+=1
    for s in range(p,-1,-1):
        t-=a[s]
        z-=1
        while q<m and t+b[q]<=k:
            t+=b[q]
            q+=1
            z+=1
        if z>ans:
            ans=z
        if q==m:
            break
print(ans)

Submission status: Accepted

Solution code:
from collections import deque

(n, m, k), aa, bb = [list(map(int, l.split())) for l in open(0)]

from itertools import accumulate

aaa = list(zip(list(accumulate([0]+aa)), range(len(aa)+1)))
bbb = list(zip(list(accumulate([0]+bb)), range(len(bb)+1)))

i = 0
j = len(bb)

ans = 0

while i<len(aaa) and j >= 0:
    aco, abo = aaa[i]
    bco, bbo = bbb[j]
    if aco+bco > k:
        j -= 1
        continue
    else:
        ans = max(abo+bbo, ans)
        i += 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()))

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

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

max_cnt = 0
for a_cnt in range(n + 1):
    if accum_a[a_cnt] > k:
        break

    b_cnt = bisect.bisect_right(accum_b, k - accum_a[a_cnt]) - 1

    sum_cnt = a_cnt + b_cnt
    if sum_cnt > max_cnt:
        max_cnt = sum_cnt

print(max_cnt)

Submission status: Accepted

Solution code:
#import sys
MOD = 10 ** 9 + 7
INFI = 10**10
#input = sys.stdin.readline
import math
from collections import deque
import itertools
import heapq
#import bisect
from fractions import Fraction
import copy
from functools import lru_cache
from collections import defaultdict
import pprint

#oo=list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
# ko=list("abcdefghijklmnopqrstuvwxyz")

def sosuhante(n):
    for k in range(2, int(math.sqrt(n))+1):
        if n% k ==0:
            return False
    return True
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def kingaku(a,b,n):
    keta=len(str(n))
    return a*n+b*keta

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default

#    h,w,a,b = map(int, input().split())
#    c = [[0 for j in range(n)] for i in range(n)]

def ret(a):
    c=[None]*(len(a)-1)
    if len(a)==1:
        return a[0]
    elif len(a)==0:
        return 0
    for i in range(1,len(a)):
        c[i-1]=abs(a[i]-a[i-1])
    return ret(c)

def soinsubunkai(n):
    a = []
    i = 1
    while i*i <= n:
        if n % i == 0 and i!=1:
            a.append(i)
            n=n//i

        if n% i !=0 or i==1:
            i += 1
    nokori=[n]
    return a + nokori


def main():
    n,m,k=map(int,input().split())
    aa=list(map(int,input().split()))
    bb=list(map(int,input().split()))
    a=[0]
    a.extend(aa)
    b=[0]
    b.extend(bb)
    maxcount=0
    totaltime=0
    maxa=0
    maxb=0
    for i in range(n+1):
        if totaltime+a[i]<=k:
            totaltime+=a[i]

        else:
            maxa=i-1
            break
        maxa = i
    totaltime=0
    maxb=0
    nowatotal=sum(a[0:maxa+1])
    nowbtotal=sum(b[0:maxb+1])
    for i in range(maxa,-1,-1):
#        nowbtotal = sum(b[0:maxb + 1])
        for j in range(maxb,m+1):
            nowcount=i+j
            if nowatotal+nowbtotal+b[j]<=k:
                maxcount=max(maxcount,nowcount)
                maxb+=1
            else:
                break
            nowbtotal+=b[j]
        nowatotal-=a[i]
    print(maxcount)

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

cumA, cumB = [0], [0]
sa, sb = 0, 0

for a in A:
    sa += a
    cumA.append(sa)
for b in B:
    sb += b
    cumB.append(sb)

ans = 0

for i in range(N, -1, -1):
    a = cumA[i]
    if a > K:
        continue

    b = K - a

    low = 0
    high = M

    while low <= high:
        mid = (low + high) //2
        guess = cumB[mid]
        if guess <= b:
            if mid == M:
                break
            elif cumB[mid+1] > b:
                break

        if guess > b:
            high = mid -1
        else:
            low = mid + 1

    """
    for j in range(M, -1, -1):
        if i + j == ans:
            break
        if a + cumB[j] <= K:
            break
    """
    ans = max(i + mid, ans)   

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

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

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

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

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]*n
sB=[0]*m
sA[0]=A[0]
sB[0]=B[0]
for i in range(1,n):
  sA[i]=sA[i-1]+A[i]
for i in range(1,m):
  sB[i]=sB[i-1]+B[i]

ans=bisect.bisect_right(sB,k)
for i in range(n):
  if k-sA[i]<0:break
  j=bisect.bisect_right(sB,k-sA[i])
  ans=max(ans,j+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()))

tmp = []
s = 0
for i in a:
    if s + i <= k:
        tmp.append(i)
        s += i
    else:
        break
a = tmp
tmp = []
s = 0
for i in b:
    if s + i <= k:
        tmp.append(i)
        s += i
    else:
        break
b = tmp
line = b[::-1] + a

ans = 0
right = 0
sum_ = 0
for left in range(len(line)):
    while right < len(line) and sum_ + line[right] <= k:
        sum_ += line[right]
        right += 1
    l = right -left 
    if l > ans:
        ans = l

    if right == left: right += 1
    else: sum_ -= line[left]

print(ans)

Submission status: Accepted

Solution code:
M,N,K = [int(hoge) for hoge in input().split()]
A = [int(hoge) for hoge in input().split()]
B = [int(hoge) for hoge in input().split()]
S = 0
Abook = 0
Bbook = 0
curbook = 0
for a in A:
    if S + a <= K:
        Abook += 1
        curbook += 1
        S += a
    else:
        break
nextB = 0
for n,b in enumerate(B):
    if S + b <= K:
        Bbook += 1
        curbook += 1
        S += b
    else:
        nextB = n
        break
if Abook == len(A) and Bbook == len(B):
    print(len(A) + len(B))
    exit()

ans = curbook
for i in range(Abook):
    Abook -= 1
    curbook -= 1
    S -= A[Abook]
    for b_index in range(nextB,len(B)):
        if S + B[b_index] <= K:
            Bbook += 1
            curbook += 1
            S += B[b_index]
            nextB += 1
        else:
            nextB = b_index
            break
    ans = max(ans,curbook)
print(ans)

Submission status: Accepted

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

v = 0
for i in range(N):
    if v + A[i] <= K:
        v += A[i]
    else:
        mi = i-1
        break
else:
    mi = N-1

ans = mi+1

for j in range(M):
    while mi > -1 and v + B[j] > K:
        v -= A[mi]
        mi -= 1
    v += B[j]
    if v <= K:
        ans = max(ans, mi+1+ j+1)

print(ans)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
from math import floor, sqrt, factorial, hypot, log  # log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter, defaultdict, deque
from itertools import accumulate, permutations, combinations, product, combinations_with_replacement
from bisect import bisect_left, bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint


def ceil(a, b): return (a + b - 1) // b


inf = float('inf')
mod = 10 ** 9 + 7


def pprint(*A):
    for a in A:     print(*a, sep='\n')


def INT_(n): return int(n) - 1


def MI(): return map(int, input().split())


def MF(): return map(float, input().split())


def MI_(): return map(INT_, input().split())


def LI(): return list(MI())


def LI_(): return [int(x) - 1 for x in input().split()]


def LF(): return list(MF())


def LIN(n: int): return [I() for _ in range(n)]


def LLIN(n: int): return [LI() for _ in range(n)]


def LLIN_(n: int): return [LI_() for _ in range(n)]


def LLI(): return [list(map(int, l.split())) for l in input()]


def I(): return int(input())


def F(): return float(input())


def ST(): return input().replace('\n', '')


def main():
    N, M, K = MI()
    A = LI()
    B = LI()
    ans = 0
    total = 0
    anow = -1
    for a in A:
        if a + total <= K:
            ans += 1
            total += a
            anow += 1
        else:
            break
    maxans = ans
    for b in B:
        total += b
        isOver = False
        while total > K:
            if anow == -1:
                isOver = True
                break
            total -= A[anow]
            anow -= 1
            ans -= 1
        if isOver:
            break
        ans += 1
        maxans = max(maxans, ans)
    print(maxans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from bisect import *
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]
for i in a:
    c+=[c[-1]+i]
d=[0]
for i in b:
    d+=[d[-1]+i]

ans=j=bisect(d,k)-1

for i in range(n+1):
    while c[i]+d[j]>k and c[i]<k:
        j-=1
    if c[i]+d[j]<=k:    
        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()))
cuma, cumb = [0], [0]
for i in range(1, n+1):
    cuma.append(cuma[i-1] + a[i-1])
for i in range(1, m+1):
    cumb.append(cumb[i-1] + b[i-1])
ans = [0]
for i in range(n+1):
    if cuma[i] <= k:
        t = k - cuma[i]
        j = bisect.bisect_right(cumb, t)
        ans.append(i+j-1)
print(max(ans))

Submission status: Accepted

Solution code:
def Binary_Search_Count(A,x,sort=True,equal=False):
    """2分探索によって,x未満の要素の個数を調べる.

    A:リスト
    x:調べる要素
    sort:ソートをする必要があるかどうか(Trueで必要)
    equal:Trueのときはx"未満"がx"以下"になる
    """
    if sort:
        A.sort()
        
    N=len(A)
    if A[-1]<=x:
        return N
    elif x<A[0] or (not equal and x==A[0]):
        return 0

    L,R=0,N
    while R-L>1:
        C=L+(R-L)//2
        if x<A[C] or (not equal and x==A[C]):
            R=C
        else:
            L=C
    return R

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


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

X=[0]*N
U=0
for i in range(N):
    U+=A[i]
    X[i]=U

Y=[0]*M
U=0
for i in range(M):
    U+=B[i]
    Y[i]=U    

R=Binary_Search_Count(Y,K,False,True)
for i in range(N):
    if K-X[i]>=0:
        R=max(R,i+1+Binary_Search_Count(Y,K-X[i],False,True))
print(R)

Submission status: Accepted

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

cumA=[0]*(N+1)
cumB=[0]*(M+1)

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

ans=0
for i in range(N+1):
    r=K-cumA[i]
    if r<0:
        break
    b=bisect_left(cumB,r)
    if b==M+1:
        ans=max(ans,i+M)
    else:
        if cumB[b]!=r:
            b-=1
        ans=max(ans,i+b)
print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate
import bisect
N,M,K = map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))

Asum = [0] + list(accumulate(A))
Bsum = [0] + list(accumulate(B))
ans=0
#print(Asum)
#print(Bsum)
for i in range(len(Asum)):
    if K >= Asum[i]:
        #print("a=",i)
        #print("b=",bisect.bisect_right(Bsum, K-Asum[i])-1)
        ans=max(ans,i-1+bisect.bisect_right(Bsum, K-Asum[i]))
print(ans)

Submission status: Accepted

Solution code:
import sys
from numpy import cumsum
from bisect import bisect_right
input = sys.stdin.readline
sys.setrecursionlimit(2 * 10**6)


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


def main():
    N, M, K = inpl()
    A = inpl()
    B = inpl()
    cumA = cumsum(A)
    cumB = cumsum(B)
    ans = bisect_right(cumB, K)
    for i, ca in enumerate(cumA):
        k = K - ca
        if k < 0:
            break
        tb = bisect_right(cumB, k)
        # print(k, i, tb, cumB)
        if ans < i + tb + 1:
            ans = i + tb + 1
    print(ans)

    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
n,m,k=map(int,input().split())
alist=list(map(int,input().split()))
blist=list(map(int,input().split()))

salist=[]
sblist=[]

tmp=0
for a in alist:
    tmp+=a
    salist.append(tmp)
tmp=0
for b in blist:
    tmp+=b
    sblist.append(tmp)

outlist=set()
outlist.add(0)
bnow=m
while bnow > 0:
    if sblist[bnow-1] > k:
        bnow -= 1
    else:
        outlist.add(bnow)
        break

for i,sa in enumerate(salist):
    if sa > k:
        break
    while sa + sblist[bnow-1] > k and bnow > 0:
        bnow -= 1
    outlist.add(i+1+bnow)
print(max(outlist))

Submission status: Accepted

Solution code:
import bisect

N,M,K = map(int,input().split())
AList = list(map(int,input().split()))
BList = list(map(int,input().split()))

AList.append(10**10)
BList.append(10**10)

ASList = [AList[0]]
for i in range(1,N+1):
  a = ASList[-1] + AList[i]
  if a > 10 **10:
    a = 10 **10
  ASList.append(a)

BSList = [0,BList[0]]
for i in range(1,M+1):
  a = BSList[-1] + BList[i]
  if a > 10 **10:
    a = 10 **10
  BSList.append(a)  
  
ans = 0
for i in range(M+2):
  b = BSList[i]
  c = int( K-b )
  if c > 0:
    d = bisect.bisect_left(ASList,c)
    if ASList[d] == c:
      d +=1 
    ans = max(ans,i+d)
  elif c == 0:
    ans = max(ans,i)
  else:
    d = bisect.bisect_left(ASList,K)
    ans = max(ans,d)
  
print(int(ans))

Submission status: Accepted

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


numbers = input().split(" ")
n = int(numbers[0])
m = int(numbers[1])
k = int(numbers[2])



numbers = input().split(" ")
a = [int(x) for x in numbers]
numbers = input().split(" ")
b = [int(x) for x in numbers]

#結果
#aを(i + 1)冊目まで(i冊)読んだときの冊数
r = [0] * (n + 1)

time_a = 0
time_b = 0
books_a = 0
books_b = 0

i = 0
while books_a < n:
    time_a += a[books_a]
    books_a += 1

    if time_a > k:
        books_a -= 1
        time_a -= a[books_a]
        break



while books_a >= 0:
    # if books_a == 0:
    #     print(time_a)
    while books_b < m:
        time_b += b[books_b]
        books_b += 1
        if time_b + time_a > k:
            books_b -= 1
            time_b -= b[books_b]
            break

    r[books_a] = books_a + books_b

    books_a -= 1
    time_a -= a[books_a]


print(max(r))

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

ans = 0
ans0 = 0
ans1 = 0
for i in range(n):
    if A[i] + ans0 <= k:
        ans0 += A[i]
        ans1 += 1
    else:
        break
ind = ans1-1

jnd = 0
for i in range(m):
    if B[i] + ans0 <= k:
        ans0 += B[i]
        ans1 += 1
        jnd += 1
    else:
        break


ans = ans1
# print(ans,ans0)
# print("jnd",jnd)

for i in range(ind, -1, -1):
    ans1 -= 1
    ans0 -= A[i]
    # print("m",ans0)
    # print("jnd",jnd)
    while jnd < m and ans0 + B[jnd] <= k:
        ans0 += B[jnd]
        
        jnd += 1
        ans1 += 1
    # print(ans1, ans0)
    ans = max(ans, ans1)
print(ans)
# print("jnd",jnd)

Submission status: Accepted

Solution code:
from itertools import accumulate
from bisect import *

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

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


cum_A = list(accumulate([0] + A))
cum_B = list(accumulate([0] + B))

start = bisect(cum_A, K) - 1       # K を超えない最大のcum_Aの要素を導くindex = 和がK以下になるようなAの要素の個数
res = [start]
k = 0

for i in reversed(range(start+1)):
    while cum_A[i] + cum_B[k] <= K:
        if k == M:
            res.append(i + k)
            break
        k += 1
    res.append(i + k-1)


print(max(res))


'''

3 3 3
1 1 1
10 2 3

'''

Submission status: Accepted

Solution code:
import sys, io, os, re
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from pprint import pprint
from math import sin, cos, pi, radians, sqrt, floor, ceil
from copy import copy, deepcopy
from collections import deque
from fractions import gcd
from functools import reduce
from itertools import groupby
from heapq import heapify, heappush, heappop

# sys.setrecursionlimit(5000)

n1 = lambda: int(sys.stdin.readline().strip())
nn = lambda: list(map(int, sys.stdin.readline().strip().split()))
f1 = lambda: float(sys.stdin.readline().strip())
fn = lambda: list(map(float, sys.stdin.readline().strip().split()))
s1 = lambda: sys.stdin.readline().strip()
sn = lambda: list(sys.stdin.readline().strip().split())
nl = lambda n: [n1() for _ in range(n)]
fl = lambda n: [f1() for _ in range(n)]
sl = lambda n: [s1() for _ in range(n)]
nm = lambda n: [nn() for _ in range(n)]
fm = lambda n: [fn() for _ in range(n)]
sm = lambda n: [sn() for _ in range(n)]

def array1(n, d=0): return [d] * n
def array2(n, m, d=0): return [[d] * m for x in range(n)]
def array3(n, m, l, d=0): return [[[d] * l for y in xrange(m)] for x in xrange(n)]
def linc(A, d=1): return map(lambda x: x + d, A)
def ldec(A, d=1): return map(lambda x: x - d, A)

N, M, K = nn()
A = nn()
B = nn()

a = [0]
sum_a = 0
for i in A:
    sum_a += i
    a.append(sum_a)

b = [0]
sum_b = 0
for i in B:
    sum_b += i
    b.append(sum_b)

ans = 0
for i in range(len(a)):
    ai = a[i]
    if ai > K:
        break

    tmp = K - ai
    j = bisect_right(b, tmp) - 1
    r = i + j

    #print(i, j, a, b, ai)   
    if r > ans:
        ans = r

if ans >= 0:
    print(ans)
else:
    print(0)

Submission status: Accepted

Solution code:
import bisect
#aaa = [0, 80, 230, 310, 460]
#print(bisect.bisect_left(aaa, 460))
#print(bisect.bisect_left(aaa, 459))

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

# 累積和
sa = [0] * (n+1)
for i in range(n):
  sa[i+1] = sa[i] + a[i]
sb = [0] * (m+1)
for i in range(m):
  sb[i+1] = sb[i] + b[i]

#print(sa)
#print(sb)
ans = 0
for ac in range(n+1):
  bc = 0
  # i = Aの机からの冊数
  rk = k - sa[ac] # bに使える時間
  if rk <= 0:
    if rk == 0:
      ans = max(ans, ac)
    else:
      ans = max(ans, ac-1)
    break
  if rk == sb[m]:
    bc = m
  else:
    bc = bisect.bisect_left(sb, rk) - 1
  #print(rk, bc)
  ans = max(ans, ac+bc)
print(ans)

Submission status: Accepted

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

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

ans = 0
for i in range(n+1):
    rest = k - sa[i]
    if rest < 0:break
    a_s = i
    b_s = bisect.bisect(sb,rest) 
    ans = max(ans, a_s + b_s)
print(ans)