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
import sys
# import heapq


def solve(N: int, M: int, K: int, A: "List[int]", B: "List[int]"):

    sumA = [0]
    sum = 0
    for a in A:
        sum += a
        sumA.append(sum)
    sumB = [0]
    sum = 0
    for b in B:
        sum += b
        sumB.append(sum)
    
    
    # print(sumA)
    # print(sumB)
    # True - ng - ok - False
    def is_ok(mid: int, i):
        # 判定する条件
        if mid < 0:
            return True
        elif mid >= M + 1:
            return False
        # print(i, mid, sumA[i] + sumB[mid])
        return sumA[i] + sumB[mid] <= K

    def binSearch(ok: int, ng: int, i):
        # print(ok, ng)
        while abs(ok - ng) > 1:
            mid = (ok + ng) // 2
            if is_ok(mid, i):
                ok = mid
            else:
                ng = mid
            # print(ok, ng)
        return ok

    ans = 0
    for i in range(N + 1):
        j = binSearch(-1, M + 1, i)
        # print("a", i, j)
        if j < 0:
            break
        ans = max(ans, i + j)

    print(ans)
    return


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    B = [int(next(tokens)) for _ in range(M)]  # type: "List[int]"
    solve(N, M, K, A, B)

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

Submission status: Accepted

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

count = sum(a)
p = n-1
ans = 0
judge = True
for i in range(m+1):
  flag = False
  count += b[i]
  while count > k and judge:
    count -= a[p]
    p -= 1
    if p == -1:
      if count > k:
        flag = True
      else:
        judge = False
      break
  if count > k:
    break
  if flag:
    break
  sub = p+1+i
  if ans < sub:
    ans = sub
print(ans)

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

cA = list(itertools.accumulate(A))
cB = list(itertools.accumulate(B))

#print(cA)
#print(cB)

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

Submission status: Accepted

Solution code:

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

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

    ans = 0
    for i in range(n+1):
        if k-cuma[i] < 0:
            continue
        ans = max(ans, i + bisect.bisect_right(cumb, k-cuma[i])-1)
    print(ans)
resolve()

Submission status: Accepted

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


import bisect

cum_a = [0]
for a in A:
    cum_a.append(cum_a[-1] + a)

cum = [0]
for b in B:
    cum.append(cum[-1] + b)

ans = 0
for i in range(n + 1):
    t = cum_a[i]
    if t <= k:
        bnum = i
    else:
        bnum = 0
    if t <= k:
        idx = bisect.bisect_right(cum, k - t)
        bnum += idx - 1
    ans = max(ans, bnum)
print(ans)

Submission status: Accepted

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

    def accumulate(a):
        s = 0
        yield s
        for x in a:
            s += x
            yield s

    *P, = accumulate(A)
    *Q, = accumulate(B)

    ans = 0
    j = len(Q) - 1
    for i, x in enumerate(P):
        while j > 0 and x + Q[j] > K:
            j -= 1
        if x + Q[j] <= K:
            ans = max(ans, i + j)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import copy
from collections import deque
import collections
stdin = sys.stdin

mod = 10**9+7

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n, m, k = na()

a = na()
b = na()

num_books = 0
sum_time = 0
for i in a:
    if sum_time+i > k:
        break
    num_books += 1
    sum_time += i
# print(num_books)


res = num_books
ia = num_books - 1
ib = 0
if num_books==0 and b[ib] > k:
    print(0)
    exit(0)
while ib < len(b):
    if sum_time < k:
        sum_time += b[ib]
        ib += 1
        num_books += 1
    else:
        if ia == -1:
            break
        sum_time -= a[ia]
        ia -= 1
        num_books -= 1
    if sum_time <= k:
        res = max(res, num_books)

print(res)

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

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

As = [0] * (N + 1)
Bs = [0] * (M + 1)

for i in range(1, N + 1):
  As[i] = As[i - 1] + A[i - 1]
  if As[i] > K:
    i -= 1
    break

for j in range(1, M + 1):
  Bs[j] = Bs[j - 1] + B[j - 1]
  if Bs[j] > K:
    j -= 1
    break

if i < j:
  As, Bs = Bs, As
  i, j = j, i

j = 0
n = i
while i:
  while j + 1 < len(Bs) and As[i] + Bs[j + 1] <= K:
    j += 1
  n = max(n, i + j)
  #print(As, Bs, i, j, n)
  i -= 1

print(n)

Submission status: Accepted

Solution code:
from collections import defaultdict
import bisect

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()

n,m,k = readInts()
a = readInts()
b = readInts()

a_s = [0]
for i in range(n):
	a_s.append(a_s[i]+a[i])
b_s = [0]
for i in range(m):
	b_s.append(b_s[i]+b[i])

ans = -1

#print(a_s)
#print(b_s)

for i in range(bisect.bisect_right(a_s,k)-1,-1,-1):
	sab = k-a_s[i]
	#print("sab",sab)
	tar = bisect.bisect_right(b_s,sab)-1
	#print(tar)
	for j in range(tar,-1,-1):
		if i+j <= ans:
			break
		if a_s[i]+b_s[j]<=k:
			ans = i+j
			#print(i,j)
			break


print(ans)

Submission status: Accepted

Solution code:
[n,m,k] = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]


a_acc = [0]
b_acc = [0]

for i in range(n):
    a_acc.append(a_acc[-1] + a[i])
for i in range(m):
    b_acc.append(b_acc[-1] + b[i])
ans = 0
#print(len(b_acc))
#0冊からn冊まで
for i in range(n+1):
    criteria = k - a_acc[i]
    if criteria < 0:
        break
    #b_accについて2分探索
    left = 0
    right = m
    #print(i,criteria)
    while left != right-1:
        #print(left,right)
        tmp = (left + right) // 2
        #if criteria == b_acc[right]:
        #    left = right
        #    break
        #el
        if criteria < b_acc[tmp]:
            right = tmp
        else:
            left = tmp
    if criteria < b_acc[right]:
        ans = max(ans,i + left)
    else:
        ans = max(ans,i + right)

print(ans)

Submission status: Accepted

Solution code:
#float型を許すな
#numpyはpythonで
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

n,m,k=MI()
lisa=LI()
lisb=LI()
u=list(accumulate(lisa))
v=list(accumulate(lisb))
u=[0]+u
v=[0]+v
for i in range(n+1):
    if u[i]>k:
        i-=1
        break
'''print(i)
print(u)
print(v)'''
j=0
count=[]
while True:
    if i<0:
        i+=1
        count.append(i+j)
        break
    while True:
        #print(i,j)
        if j>=m+1:
            j-=1
            count.append(i+j)
            break
        if u[i]+v[j]<=k:
            j+=1
        else:
            j-=1
            count.append(i+j)
            break
    i-=1
print(max(count))

Submission status: Accepted

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

class Cumulative_Sum:
    def __init__(self, a):
        la = len(a)
        self.s = [0] * (la + 1)
        for i in range(la):
            self.s[i+1] = self.s[i] + a[i]
    # s[l]からs[r]までの和
    def sum(self, l, r):
        return self.s[r] - self.s[l]
csa = Cumulative_Sum(a)
csb = Cumulative_Sum(b)
#print(csa.s)
#print(csb.s)
ans = 0
for i in range(n+1):
    t = csa.s[i]
    if t > k:
        continue
    p = k - t
    bi = bisect.bisect_left(csb.s, p)
    ans = max(ans, i)
    if csb.s[m] <= p:
        ans = max(ans, i + m)
    else:
        ans = max(ans, i + bi-1)

print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate
import bisect


def main():
    N, M, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    accumulateA = list(accumulate(A))
    accumulateB = list(accumulate(B))
    accumulateA.insert(0, 0)
    accumulateB.insert(0, 0)
    count = 0
    for i in range(N+1):
        j = bisect.bisect(accumulateB, K - accumulateA[i]) - 1
        if j >= 0:
            count = max(count, i + j)
    print(count)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
time_of_A = [A[0]]
for i in range(1, N):
  time_of_A.append(time_of_A[i-1]+A[i])
B = list(map(int, input().split()))
time_of_B = [B[0]]
for i in range(1, M):
  time_of_B.append(time_of_B[i-1]+B[i])

book = [0]
for i in range(N):
  if time_of_A[i] <= K:
    book.append(i+1)
for i in range(M):
  if time_of_B[i] <= K:
    book.append(i+1)


start = M-1
for i in range(N):
  j = start
  time = 10 ** 9 + 1
  while time > K and j >= 0:
    time = time_of_A[i] + time_of_B[j]
    j -= 1
  if time <= K:
    book.append(i+j+3)
    j +=1
    start = j
  else:
    break
print(max(book))

Submission status: Accepted

Solution code:
# coding: utf-8
#import numpy as np
import re
import math
from collections import defaultdict,deque,Counter
from fractions import Fraction
import bisect
import itertools
from itertools import accumulate
from copy import deepcopy
import heapq
import random
import time
import os
import sys
from functools import lru_cache,reduce
readline=sys.stdin.readline
sys.setrecursionlimit(100000)
alp="abcdefghijklmnopqrstuvwxyz"
numbers="0123456789"
mod=int(10**9+7)
inf=int(10**19)
class Set():
    def __init__(self,A=[]):
        self.heap=[]
        self.dic=defaultdict(bool)
        self.size=0
        for a in A:self.add(a)
    
    def __str__(self):
        return str(self.heap)
    
    def add(self,x):
        if not self.find(x):
            heapq.heappush(self.heap,x)
            self.dic[x]=True
            self.size+=1

    def remove(self,x):
        if self.find(x):
            self.dic[x]=False
            self.size-=1

    def find(self,x):
        return self.dic[x]
    
    def top(self):
        while self.heap and self.dic[self.heap[0]]==False:
            heapq.heappop(self.heap)
        if self.heap:return self.heap[0]
        else:return None

    def pop(self):
        ret=None
        if self.heap:
            ret=self.top()
            self.remove(ret)
        return ret
class multiset():
    def __init__(self,A=[]):
        self.heap=[]
        self.dic=defaultdict(int)
        self.size=0
        for a in A:self.add(a)
    
    def __str__(self):
        return str(self.heap)
    
    def add(self,x):
        heapq.heappush(self.heap,x)
        self.dic[x]+=1
        self.size+=1

    def remove(self,x):
        if self.dic[x]>0:
            self.dic[x]-=1
            self.size-=1
    
    def count(self,x):
        return self.dic[x]

    def find(self,x):
        return self.dic[x]>0
    
    def top(self):
        while self.heap and self.dic[self.heap[0]]==0:
            heapq.heappop(self.heap)
        if self.heap:return self.heap[0]
        else:return None

    def pop(self):
        ret=None
        if self.heap:
            ret=self.top()
            self.remove(ret)
        return ret
class Edge():
    def __init__(self,x,y,val=1):
        self.x=x
        self.y=y
        self.val=val
class unionfind():
    def __init__(self,n):
        self.n=n
        self.P=[a for a in range(n)]
        self.rank=[0]*n
 
    def find(self,x):
        if(x!=self.P[x]):
            self.P[x]=self.find(self.P[x])
        return self.P[x]
 
    def same(self,x,y):
        return self.find(x)==self.find(y)
 
    def link(self,x,y):
        if self.rank[x]<self.rank[y]:
            self.P[x]=y
        elif self.rank[y]<self.rank[x]:
            self.P[y]=x
        else:
            self.P[x]=y
            self.rank[y]+=1
 
    def unite(self,x,y):
        self.link(self.find(x),self.find(y))
 
    def size(self):
        S=set()
        for a in range(self.n):
            S.add(self.find(a))
        return len(S)
def yn(b):print("yes" if b else "no")
def Yn(b):print("Yes" if b else "No")
def YN(b):print("YES" if b else "NO")
def ispow(a,b):
    now=b
    while now<a:
        now*=b
    return now==a
def getbin(num,size):
    A=[0]*size
    for a in range(size):
        if (num>>(size-a-1))&1==1:
            A[a]=1
        else:
            A[a]=0
    return A
def getfacs(n,mod_=0):
    A=[1]*(n+1)
    for a in range(2,len(A)):
        A[a]=A[a-1]*a
        if(mod_>0):A[a]%=mod_
    return A
def comb(n,r,mod,fac):
    if(n-r<0):return 0
    return (fac[n]*pow(fac[n-r],mod-2,mod)*pow(fac[r],mod-2,mod))%mod
def nextcomb(num,size):
    x=num&(-num)
    y=num+x
    z=num&(~y)
    z//=x
    z=z>>1
    num=(y|z)
    if(num>=(1<<size)):return False
    else:return num
def getprimes(n):
    if n==0:return []
    A=[True]*(n+1)
    A[0]=False
    A[1]=False
    for a in range(2,n+1):
        if A[a]:
            for b in range(a*2,n+1,a):
                A[b]=False
    ret=[]
    for a in range(n+1):
        if(A[a]):ret.append(a)
    return ret
def isprime(num):
    if(num<=1):return False
    i=2
    while i*i<=num:
       if(num%i==0):return False
       i+=1
    return True
def ifelse(b,t,f):return t if b else f
def factorize(n):
    b = 2
    ret=defaultdict(int)
    while b * b <= n:
        while n % b == 0:
            n //= b
            ret[b]+=1
        b+=1
    if n > 1:ret[n]+=1
    return ret
def inputintlist(row=1): 
    if row==1:
        ret=list(map(int,input().split()))
    else:
        ret=[None]*row
        for a in range(row):
            ret[a]=int(input())
    return ret
def inputfloatlist(row=1): 
    if row==1:
        ret=list(map(float,input().split()))
    else:
        ret=[None]*row
        for a in range(row):
            ret[a]=float(input())
    return ret
def istanchouzouka(A):
    f=True
    prev=min(A)-1
    for a in A:
        if prev>=a:
            f=False
        prev=a
    return f
def istanchouhigensyou(A):
    f=True
    prev=min(A)-1
    for a in A:
        if prev>a:
            f=False
        prev=a
    return f
def getallsum(A):
    s=sum(A)
    dp=defaultdict(int)
    dp[0]=1
    for a in range(0,len(A)):
        for b in range(s,-1,-1):
            if b-A[a]>=0:
                dp[b]+=dp[b-A[a]]
    return dp
def yuukoutomukou(edges):
    ret=deepcopy(edges)
    for edge in edges:
        if not edge.x==edge.y:
            ret.append(Edge(edge.y,edge.x,edge.val))
    return ret
def dijkstra(edges,V,start):
    mincost=[inf]*V
    G=[[] for a in range(V)]
    for edge in edges:
        G[edge.x].append([edge.val,edge.y])
    Q=PriorityQueue()
    Q.put([0,start])#[cost,x]
    while not Q.empty():
        nowcost,nowx=Q.get()
        if mincost[nowx]==inf:
            mincost[nowx]=nowcost
            for cost,y in G[nowx]:
                newcost=nowcost+cost
                Q.put([newcost,y])
    return mincost
def warshallfloyd(edges,V):
    mincost=[[inf for b in range(V)] for a in range(V)]
    for a in range(V):mincost[a][a]=0
    for edge in edges:
        mincost[edge.x][edge.y]=min(mincost[edge.x][edge.y],edge.val)#x→yが複数ある場合のためにmin()する
    for k in range(V):
        for s in range(V):
            for t in range(V):
                if mincost[s][k]==inf or mincost[k][t]==inf:continue
                mincost[s][t]=min(mincost[s][t],mincost[s][k]+mincost[k][t])
    return mincost
def bellemanford(edges,V,start):
    mincost=[inf]*V
    mincost[start]=0
    for _ in range(V):
        for edge in edges:
            if mincost[edge.x]==inf:continue
            mincost[edge.y]=min(mincost[edge.y],mincost[edge.x]+edge.val)
    return mincost
def getmd(x1,y1,x2,y2):return abs(x1-x2)+abs(y1-y2)
def geted(x1,y1,x2,y2):return math.sqrt((x1-x2)**2+(y1-y2)**2)
class fordfulkerson():
    def __init__(self,edges,V,s,t):
        self.V=V
        self.used=[False]*V
        self.G=[[] for a in range(V)]
        self.s=s
        self.t=t
        for edge in edges:
            self.G[edge.x].append({"x":edge.x,"y":edge.y,"cap":edge.val,"rev":len(self.G[edge.y])})
            self.G[edge.y].append({"x":edge.y,"y":edge.x,"cap":0,"rev":len(self.G[edge.x])-1})
    def dfs(self,v,t,f=inf):
        if v==t:return f
        self.used[v]=True
        for a in range(len(self.G[v])):
            x=self.G[v][a]["x"]
            y=self.G[v][a]["y"]
            cap=self.G[v][a]["cap"]
            rev=self.G[y][self.G[x][a]["rev"]]
            if self.used[y] or cap==0:continue
            f2=self.dfs(y,t,min(f,cap))
            if f2>0:
                self.G[v][a]["cap"]-=f2
                rev["cap"]+=f2
                return f2
        return 0
    def maxflow(self):
        flow=0
        while True:
            self.used=[False]*V
            zouka=self.dfs(self.s,self.t)
            if zouka==0:break
            flow+=zouka
        return flow
def zipsort(*args):
    ziplist=sorted(zip(*args))
    for a in range(len(args)):
        for b in range(len(ziplist)):
            args[a][b]=ziplist[b][a]
def zipreverse(*args):
    ziplist=list(zip(*args))
    rev=[]
    for a in range(len(ziplist)-1,-1,-1):
        rev.append(ziplist[a])
    for a in range(len(args)):
        for b in range(len(rev)):
            args[a][b]=rev[b][a]

######################################################################################################
N,M,K=inputintlist()
A=inputintlist()
B=inputintlist()
RA=[0]*(N+1)
RB=[0]*(M+1)
for a in range(N):
    RA[a+1]=RA[a]+A[a]
for a in range(M):
    RB[a+1]=RB[a]+B[a]

ans=0
for a in range(0,N+1):
    n=RA[a]
    if K<n:break
    k=K-n
    i=bisect.bisect_right(RB,k)-1
    ans=max(ans,a+i)
print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate
from bisect import bisect_right
N, M, K = [int(x) for x in input().split()]
A = [0] + [int(x) for x in input().split()]
B = [0] + [int(x) for x in input().split()]

cumA = list(accumulate(A))
cumB = list(accumulate(B))

ans = 0
for i in range(N + 1):
    if cumA[i] > K: break
    Tb = K - cumA[i]
    temp = i + bisect_right(cumB, Tb) - 1
    ans = max(ans, temp)

print(ans)

Submission status: Accepted

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

import sys
import pprint

sys.setrecursionlimit(10 ** 6)

class Logger:
	def __init__(self, debug):
		self.debug = debug
	def print(self, *args):
		if self.debug:
			pprint.pprint(args)

def upper_case(lst, key):
	ng = -1
	ok = len(lst)
	while (abs(ok - ng) > 1):
		mid = (ok + ng) // 2
		if lst[mid] > key:
			ok = mid
		else:
			ng = mid
	return ok




def main():
	log = Logger(0)
	n, m, k = map(int, sys.stdin.readline().split())
	a = list(map(int, sys.stdin.readline().split()))
	b = list(map(int, sys.stdin.readline().split()))
	for i in range(1, n):
		a[i] += a[i-1]
	for i in range(1, m):
		b[i] += b[i-1]
	log.print(a, b)
	ans = upper_case(b, k)
	log.print(ans, b, k)
	for i in range(-1, n):
		if a[i] > k:
			continue
		ans = max(ans, i+1+upper_case(b, k-a[i]))
		log.print(i, k-a[i], upper_case(b, k-a[i]))
	print(ans)


main()

Submission status: Accepted

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

A = []
B = []

tmp = 0
for i in AStr:
    tmp += int(i)
    if (tmp > K):
        break
    A.append(int(i))

tmp = 0
maxB = 0
for i in BStr:
    tmp += int(i)
    if (tmp > K):
        break
    B.append(int(i))
    maxB = tmp

num = 0
tmp = 0
maxNum = len(A)
if maxNum < len(B):
    maxNum = len(B)

numB = len(B)
tmpj = 0
for i in range(len(A)+1):
    if i != 0:
        tmpj += A[i-1]

    while (tmpj + maxB > K):
        a = B.pop()
        maxB -= a
        numB -= 1

    if i + numB > maxNum:
        maxNum = i + numB

print(maxNum)

Submission status: Accepted

Solution code:
n, m, k = [int(e) for e in input().split()]
a = [int(e) for e in input().split()]
b = [int(e) for e in input().split()]

i = 0
j = 0

total_max = 0

while i < n:
    if a[i] > k:
        break
    k -= a[i]
    i += 1
    
while j < m:
    if b[j] > k:
        break
    k -= b[j]
    j += 1

total_max = i + j
    
while i > 0 and j < m:
    k += a[i - 1]
    i -= 1
    while j < m:
        if b[j] > k:
            break
        k -= b[j]
        j += 1
    if total_max < i + j:
        total_max = i + j
    
print(total_max)

Submission status: Accepted

Solution code:
import sys
## io ##
def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
def MIIZ(): return list(map(lambda x: x-1, MII()))
from itertools import accumulate as acc
#======================================================#
def main():
    n,m,k = MII()
    aa = [0]+MII()
    bb = [0]+MII()
    aa = [a for a in list(acc(aa)) if a <= k]
    bb = [b for b in list(acc(bb)) if b <= k]
    maxcnt = 0
    for i in range(len(aa)):
        a = aa[i]
        left = 0
        right = len(bb)-1
        maxv = 0
        while left <= right:
            mid = (left+right)//2
            if a+bb[mid] > k:
                right = mid-1
            else:
                maxv = max(maxv, a+bb[mid])
                left = mid+1
        maxcnt = max(maxcnt, i+right)
    print(maxcnt)

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_sum = [0]
b_sum = [0]
for i in range(n):
    a_sum.append(a[i]+a_sum[i])
    if a_sum[i]>k:
        break
for i in range(m):
    b_sum.append(b[i]+b_sum[i])
    if b_sum[i]>k:
        break
        
ans = 0
y = len(b_sum)-1
for i in range(len(a_sum)):
    if a_sum[i]>k:
        break
    time = k - a_sum[i]
    for j in range(y,-1,-1):
        if time>=b_sum[j]:
            y = j
            break
    if time>=b_sum[len(b_sum)-1]:
        j = len(b_sum)-1
    ans = max(ans,i+j)
    
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import numpy as np
import sys
from collections import deque
from collections import defaultdict
import heapq
import collections
import itertools
import bisect
from itertools import accumulate


def zz():
    return list(map(int, sys.stdin.readline().split()))


def z():
    return int(sys.stdin.readline())


def S():
    return sys.stdin.readline()


def C(line):
    return [sys.stdin.readline() for _ in range(line)]


N, M, K = zz()
A = zz()
B = zz()
# 累積和を見てみる
A_sum = [0]
A_sum.extend(list(accumulate(A)))
B_sum = [0]
B_sum.extend(list(accumulate(B)))

# a_idx + b_idx を最大化
# K >= A_sum[a_idx+1] + B_sum[b_idx+1]
a_idx = bisect.bisect(A_sum, K) - 1
# b_tmp = bisect.bisect(A_sum, K)
# print(A_sum)
# print(B_sum)
# print('a_idx', a_idx)
ans = a_idx
if (a_idx == 0):
    b_idx = bisect.bisect(B_sum, K) - 1
    print(b_idx)
    exit()
else:
    for b_idx in range(1, M+1):
        # print(B_sum[b_idx] + A_sum[a_idx], a_idx, b_idx)
        while (B_sum[b_idx] + A_sum[a_idx] > K):
            a_idx -= 1
            if (a_idx < 0):
                # print(a_idx, b_idx)
                print(ans)
                exit()
        ans = max(ans, a_idx+b_idx)
# print(a_idx, b_idx)

print(max(ans, 0))

Submission status: Accepted

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

ans=0
flag=0
a_now=-1
b_now=m-1
num_a=0
num_b=sum(b)

while b_now>=-1 and a_now<n:
    if (num_a+num_b<=k):
        ans=max(ans,a_now+b_now+2)
        a_now+=1
        if (a_now==n):
          break
        num_a+=a[a_now]
    else:
        b_now-=1
        if (b_now==-2):
          break
        if (b_now==-1):
          num_b=0
        else:
          num_b-=b[b_now+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()))
for i in range(1, len(A)):
  A[i] += A[i - 1]
for i in range(1, len(B)):
  B[i] += B[i - 1]
#print(A, B)
res = 0
for i in range(len(A)):
  temp = k - A[i]
  bcan = bisect.bisect_right(B, temp)
  if temp >= 0:
    res = max(res, (i + 1 + bcan))
bcan = bisect.bisect_right(B, k)
res = max(res, bcan)
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()))

ruiA, ruiB = [0], [0]

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

for j in range(M):
    ruiB.append(ruiB[j] + B[j])

t = False
for i in range(N + M + 1):
    if i <= N and ruiA[i] <= K:
        continue
    if i <= M and ruiB[i] <= K:
        continue
    if i > N and ruiA[N] + ruiB[i - N] <= K:
        continue
    if i > M and ruiA[i - M] + ruiB[M] <= K:
        continue

    t = False
    for j in range(0, i + 1):
        if j > M or i - j > N:
            continue
        if ruiA[i - j] + ruiB[j] <= K:
            t = True
            break

    if not t:
        print(i - 1)
        exit()

print(N + M)

Submission status: Accepted

Solution code:
import bisect
def LI():
  return list(map(int,input().split()))

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

cumA = [0]
for a in A:
  cumA.append(cumA[-1]+a)

cumB = [0]
for b in B:
  cumB.append(cumB[-1]+b)

res = 0
for i in range(N+1):
  cuma = cumA[i]
  if cuma > K: continue
  rem = K-cuma
  deskb = bisect.bisect_right(cumB, rem)-1

  res = max(res, i+deskb)

print(res)

Submission status: Accepted

Solution code:
from collections import *
import sys
from bisect import *
input=sys.stdin.readline
from math import *
t=1
while(t):
    t-=1
    n,m,k=map(int,input().split())
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    prea=[a[0]]
    preb=[b[0]]
    for i in range(1,n):
        prea.append(prea[-1]+a[i])
    for j in range(1,m):
        preb.append(preb[-1]+b[j])
    r=0
    for i in range(n):
        if(k-prea[i]<0):
            break
        re=k-prea[i]
        rr=bisect_right(preb,re)
        r=max(r,i+1+rr)
    for i in range(m):
        if(k-preb[i]<0):
            break
        re=k-preb[i]
        rr=bisect_right(prea,re)
        r=max(r,i+1+rr)
    print(r)

Submission status: Accepted

Solution code:
from itertools import accumulate

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

c = [0] + list(accumulate(a))
d = [0] + list(accumulate(b))

ans = 0
i = 0
j = len(b)

while True:
    num = c[i] + d[j]
    if num > k and j >= 1:
        while True:
            j -= 1
            if j == 0 or c[i] + d[j] <= k:
                break
    if c[i] + d[j] <= k:
        ans = max(ans, i + j)
    if i == len(a):
        break
    while True:
        i += 1
        if i == len(a) or c[i] + d[j] > k:
            i -= 1
            break
    if c[i] + d[j] <= k:
        ans = max(ans, i + j)
    i += 1

print(ans)