Problem:


Score : 300 points

Problem Statement

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

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

Consider the following action:

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

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

Constraints

1 \leq N, M \leq 200000

1 \leq K \leq 10^9

1 \leq A_i, B_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

3 4 240
60 90 120
80 150 80 150

Sample Output 1

3

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

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

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

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

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

Sample Input 2

3 4 730
60 90 120
80 150 80 150

Sample Output 2

7

Sample Input 3

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

Sample Output 3

0

Watch out for integer overflows.



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


Sample output:
3

Submission status: Accepted

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

alis=[]
time=0
for i in range(n):
  time+=a[i]
  alis.append(time)
blis=[]
time=0
for i in range(m):
  time+=b[i]
  blis.append(time)
  
import bisect
nowtop=0
for i in range(0,n+1):
  if i==0:
    aldtime=0
    books=0
  else:
    aldtime=alis[i-1]
    books=i
  if aldtime>k:
    break
  else:
    nokoritime=k-aldtime
    books+=bisect.bisect_right(blis,nokoritime)
    if books>nowtop:
      nowtop=books
print(nowtop)

Submission status: Accepted

Solution code:
import sys, re
from math import ceil, floor, sqrt, pi, factorial, gcd
from copy import deepcopy
from collections import Counter, deque
from heapq import heapify, heappop, heappush
from itertools import accumulate, product, combinations, combinations_with_replacement
from bisect import bisect, bisect_left, bisect_right
from functools import reduce
from decimal import Decimal, getcontext
# input = sys.stdin.readline 
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_list(N): return [s_list() for _ in range(N)]
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 6)
INF = float('inf')
MOD = 10 ** 9 + 7
num_list = []
str_list = []

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

    a_ruisekiwa = list(accumulate(a_list))
    b_ruisekiwa = list(accumulate(b_list))

    # print(a_ruisekiwa)
    # print(b_ruisekiwa)

    max_count = 0

    for i in range(n):
        if k < a_ruisekiwa[i]:
            break
        insert_num = k - a_ruisekiwa[i]
        insert_index = bisect_right(b_ruisekiwa, insert_num)
        max_count = max(max_count, i + insert_index +1)

    for i in range(m):
        if k < b_ruisekiwa[i]:
            break
        insert_num = k - b_ruisekiwa[i]
        insert_index = bisect_right(a_ruisekiwa, insert_num)
        max_count = max(max_count, i + insert_index +1)

    print(max_count)
 
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# C

import itertools
import bisect

n, m, k = map(int, input().split())
a_l = [int(x) for x in input().split()]
b_l = [int(x) for x in input().split()]

cumsuma_l = list(itertools.accumulate(a_l)) 
cumsumb_l = list(itertools.accumulate(b_l))

_idx = bisect.bisect_left(cumsumb_l, k)
if _idx == m or cumsumb_l[_idx] != k:
    _idx -= 1
ans = _idx + 1
    
for i in range(n):
    _a = cumsuma_l[i]
    if _a > k:
        break
    _tmp = k - _a
    _idx = bisect.bisect_left(cumsumb_l, _tmp)
    if _idx == m or cumsumb_l[_idx] != _tmp:
        _idx -= 1
    ans = max(ans, i + _idx + 2)
        
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()))
ruisekiA = [0] * (N + 1)
for i in range(N):
    ruisekiA[i + 1] = ruisekiA[i] + A[i]
ruisekiB = [0] * (M + 1)
for i in range(M):
    ruisekiB[i + 1] = ruisekiB[i] + B[i]
ans = 0
for i in range(N + 1):
    if ruisekiA[i] > K: break
    else:
        time = K - ruisekiA[i]
        lb = -1
        ub = M + 1
        while ub - lb > 1:
            mid = (ub + lb) // 2
            if ruisekiB[mid] <= time: lb = mid
            else: ub = mid
        ans = max(ans,i + lb)
print(ans)

Submission status: Accepted

Solution code:
import bisect

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

sa = [0]
for ai in a:
  sa.append(sa[-1]+ai)
sb = [0]
for bi in b:
  sb.append(sb[-1]+bi)
#print(sa)
#print(sb)

ans = 0
for i in range(len(sa)):
  if k < sa[i]:
    break
  r = bisect.bisect_right(sb,k-sa[i])
  ans = max(i+r-1,ans)
print(ans)

Submission status: Accepted

Solution code:
N, M, K = [int(x) for x in input().split(" ")]

A = [int(x) for x in input().split(" ")]
B = [int(x) for x in input().split(" ")]
amax = 0
bmax = 0
total_max = 0
current_max = 0
current_weight = 0
for i in range(len(A)):
  if current_weight + A[i] <= K:
    current_weight += A[i]
    amax += 1
  else:
    break

for i in range(len(B)):
  if current_weight + B[i] <= K:
    current_weight += B[i]
    bmax += 1
  else:
    break

total_max = amax + bmax
current = total_max
for i in range(amax-1, -1, -1):
  current_weight -= A[i]
  amax -= 1
  for j in range(bmax, len(B)):
    if current_weight + B[j] <= K:
      current_weight += B[j]
      current += 1
      bmax += 1
    else:
      break
  if amax + bmax > total_max:
    total_max = amax + bmax

print(total_max)

Submission status: Accepted

Solution code:
from itertools import accumulate
from bisect import bisect_right

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

a_cumsum = list(accumulate(a_list))
#print(a_cumsum)
#print(bisect_right(a_cumsum, 59))
b_cumsum = list(accumulate(b_list))

ans=0
for i in range(n):
  if k<a_cumsum[i]:
    continue
  b_max=k-a_cumsum[i]
  b_index=bisect_right(b_cumsum, b_max)-1
  if b_index==-1:
    koho=i+1
  else:
    koho=i+1+b_index+1
  ans = max(ans,koho)

for i in range(m):
  if k<b_cumsum[i]:
    continue
  a_max=k-b_cumsum[i]
  a_index=bisect_right(a_cumsum, a_max)-1
  if a_index==-1:
    koho=i+1
  else:
    koho=i+1+a_index+1
  ans = max(ans,koho)
  
print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate

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

accAs = list(accumulate([0]+As))
accBs = list(accumulate([0]+Bs))
#print('accAs:', accAs)
#print('accBs:', accBs)

iB = M
ans = 0
for iA in range(N+1):
    num = iA
    accA = accAs[iA]
#    print('\niA:', iA, '/ accA:', accA)
    if accA > K:
        break
    while accA + accBs[iB] > K:
        iB -= 1
    num += iB
#    print('iA:', iA, '/ iB:', iB, '/ num:', num)
    if num > ans:
        ans = num

print(ans)

Submission status: Accepted

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

from itertools import accumulate
a = list(accumulate(a))
b = list(accumulate(b))

from collections import deque
u = deque([0])
a.insert(0,0)

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

Submission status: Accepted

Solution code:
def main():
    from builtins import int, map, list, print, len
    import sys
    from bisect import bisect_right

    sys.setrecursionlimit(10 ** 6)

    input = (lambda: (sys.stdin.readline()).rstrip())
    input_list = (lambda: input().split())
    input_number = (lambda: int(input()))
    input_number_list = (lambda: list(map(int, input_list())))

    N, M, K = input_number_list()
    A = input_number_list()
    B = input_number_list()
    time_A = [A[0]]
    time_B = [B[0]]
    for i in range(1, len(A)):
        time_A.append(time_A[i - 1] + A[i])
    for i in range(1, len(B)):
        time_B.append(time_B[i - 1] + B[i])
    idx_a = bisect_right(time_A, K) - 1
    time = time_A[idx_a]
    idx_b = -1
    if len(A) - 1 == idx_a:
        idx_b = bisect_right(time_B, K - time) - 1
    ans = idx_a + idx_b + 2
    idx_b = bisect_right(time_B, K - time) - 1
    ans = max(ans, idx_a + idx_b + 2)
    while True:
        if idx_a < 0:
            break
        time -= A[idx_a]
        idx_a -= 1
        idx_b = bisect_right(time_B, K - time) - 1
        ans = max(ans, idx_a + idx_b + 2)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from itertools import accumulate
from bisect import bisect_right
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

def main():
    N,M,K = map(int, readline().split())
    A = [int(i) for i in readline().split()]
    B = [int(i) for i in readline().split()]

    acc_A = list(accumulate(A))
    acc_B = list(accumulate(B))

    ans = 0
    for i, a in enumerate(acc_A):
        tmp = i + 1
        rest = K - a
        if rest < 0:
            ans = max(ans, bisect_right(acc_B, K))
            break
        b_ind = bisect_right(acc_B, rest)
        tmp += b_ind

        ans = max(ans, tmp)

    for i, b in enumerate(acc_B):
        tmp = i + 1
        rest = K - b
        if rest < 0:
            ans = max(ans, bisect_right(acc_A, K))
            break
        a_ind = bisect_right(acc_A, rest)
        tmp += a_ind

        ans = max(ans, tmp)
    
    print(ans)
       


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from itertools import accumulate

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

acc1 = list(accumulate(A))
acc2 = list(accumulate(B))

def is_ok(a, b):
    if acc1[a] + acc2[b] <= K:  return 1
    return 0

def binary_search(i, left, right):
    while (abs(right - left) > 1):
        mid = (left + right) // 2
        if is_ok(i, mid):  right = mid
        else:  left = mid
    return right

ans = 0
for i in range(N):
    ret = binary_search(i, M, -1)
    if ret == -1:  
      if acc1[i] <= K:  ans = max(i + 1, ans)
    else:
      ans = max(ans,  i + ret + 2)
      
acc1, acc2 = acc2, acc1
N, M = M, N
      
for i in range(N):
    ret = binary_search(i, M, -1)
    if ret == -1:  
      if acc1[i] <= K:  ans = max(i + 1, ans)
    else:
      ans = max(ans,  i + ret + 2)
      
print(ans)

Submission status: Accepted

Solution code:
n, m, k = map(int, input().split())
a = list(map(int, input().split())) # list()でリスト化、それを変数に代入。
b = list(map(int, input().split())) # list()でリスト化、それを変数に代入。
kazu = 0
c_a = 0
c_b = 0
time = 0

a = [0]+a
b = [0]+b

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

for i in range(0,n+1):
    for j in range(max(0,kazu-i+1),m+1):
        time = a[i] + b[j]
        if time > k:
            break
        kazu = i + j

print(kazu)

Submission status: Accepted

Solution code:
# 幅優先探索（行きがけ）
import collections
import sys
import copy
import re
import bisect


def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())


def main():
    N, M, K = LI()
    AArray = LI()
    BArray = LI()

    aSum = [AArray[0]]
    bSum = [BArray[0]]

    cnt = 0

    for i in range(1, N):
        aSum.append(aSum[i-1]+AArray[i])
    for i in range(1, M):
        bSum.append(bSum[i-1]+BArray[i])

    for i in range(N):
        a = aSum[i]
        if a > K:
            break
        if a == K:
            cnt = max(cnt, i+1)
            break
        sa = K-a
        b = bisect.bisect_right(bSum, sa)
        cnt = max(cnt, i+1+b)

    for i in range(M):
        b = bSum[i]
        if b > K:
            break
        if b == K:
            cnt = max(cnt, i+1)
            break
        sa = K-b
        a = bisect.bisect_right(aSum, sa)
        cnt = max(cnt, i+1+a)

    print(cnt)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

mx_a=0
c=0
ai = 0
for i,a in enumerate(als):
    if mx_a+a<=k:
        mx_a+=a
        c+=1
    else:
        break
    ai+=1

available_als=als[:ai]
available_als.reverse()

k -= mx_a
bi=0
for i,b in enumerate(bls):
    if b<=k:
        k-=b
        c+=1
    else:
        bi = i
        break

max_c=c
for a in available_als:
    c-=1
    k+=a
    while True:
        try:
            b = bls[bi]
            if b<=k:
                c+=1
                k-=b
                bi+=1
            else:
                if c>max_c:
                    max_c=c
                break
        except:
            if c>max_c:
                max_c=c
            break
print(max_c)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
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
aacc = [0] + a
aacc = list(accumulate(aacc))
bacc = [0] + b
bacc = list(accumulate(bacc))

from bisect import bisect_right
ans = 0
for cnta, ac in enumerate(aacc):
    if k-ac>=0:
        cntb = bisect_right(bacc, k-ac) - 1
        ans = max(ans, cnta+cntb)

print(ans)

Submission status: Accepted

Solution code:
a=input()
b=input()
c=input()

N,M,K=map(int, a.split())
A = list(map(int, b.split()))
B = list(map(int, c.split()))

kotae=N
Kotae=0
m=0
wa=sum(A)
for i in range(N+1):

    n=N-i-1
    
    if wa<=K:
        while wa+B[m]<=K:
            wa+=B[m]
            m+=1
            kotae+=1
            
            if m==M:
                m-=1
                break
        if kotae>Kotae:
            Kotae=kotae       
    wa-=A[n]
    kotae-=1
print(Kotae)

Submission status: Accepted

Solution code:
n,m,k = map(int,input().split())
al=list(map(int,input().split()))
bl=list(map(int,input().split()))
if sum(al)+sum(bl)<=k:
  print(n+m)
  exit()
ans = 0
used = 0
indexa = indexb = -1
for i in range(n):
  if used+al[i] <= k:
    used += al[i]
    indexa=i
    ans += 1
  else:
    break
for i in range(m):
  if used+bl[i] <= k:
    used += bl[i]
    indexb = i
    ans+=1
  else:
    break

tmp = ans
for i in range(indexa,-1,-1):
  used -= al[i]
  tmp -= 1
  while indexb<m-1 and bl[indexb+1]+used <=k:
    used += bl[indexb+1]
    indexb += 1
    tmp += 1
  ans = max(tmp,ans)
print(ans)

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 inputint(row=1): 
    if row==1:
        ret=list(map(int,input().split()))
        if len(ret)==1:ret=ret[0]
    else:
        ret=[None]*row
        for a in range(row):
            ret[a]=int(input())
    return ret
def inputfloat(row=1): 
    if row==1:
        ret=list(map(float,input().split()))
        if len(ret)==1:ret=ret[0]
    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=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
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:
n,m,k = map(int,input().split())
a = [0] + list(map(int,input().split()))
b = [0] + list(map(int,input().split()))

x = 0
for i in range(1,n+1) :
    a[i] = a[i-1] + a[i]
    if a[i] <= k :
        x = i

y = 0
for j in range(1,m+1) :
    b[j] = b[j-1] + b[j]
    if b[j] <= k-a[x] :
        y = j
        
cmax = x + y

while x>0 :
    x = x-1
    ynow = y
    for j in range(y,m+1) :
        if b[j] <= k - a[x] :
            ynow = j
        else :
            break
    if x + ynow > cmax :
        cmax = x + ynow
    y = ynow

print(cmax)

Submission status: Accepted

Solution code:
n,m,k = [int(x) for x in input().split(' ')]

t1 = [int(x) for x in input().split(' ')]
t2 = [int(x) for x in input().split(' ')]

s1,s2 = 0,0

s1_s = []

s1_s.append(0)
for i in range(n):
  if s1 + t1[i] > k:
    break
  s1 += t1[i]
  s1_s.append(s1)
  
  
  
p2 = 0
max_b = 0
# print(s1_s, t2)
for i in range(len(s1_s)):
  av = k - s1_s[len(s1_s)-i-1]
  if (p2 >= m): break
  while (s2 + t2[p2]) <= av:
    s2 += t2[p2]
    p2 += 1
    if (p2 >= m): 
        # print(p2)
        break
    

#   print(s1_s[len(s1_s)-i-1], s2)
    
  
  all_b = len(s1_s)-i-1 + p2
  
  if max_b < all_b:
    max_b = all_b

print(max_b)

Submission status: Accepted

Solution code:
import sys
import math
from bisect import bisect_left
def II():
	return int(sys.stdin.readline())

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

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

def SI():
	return sys.stdin.readline().strip()
def bs(a,n,x): 
    i = bisect_left(a, x) 
    if i<n and a[i] == x:
    	return i
    elif i:
    	return (i-1) 
    else: 
        return -1
n,m,k = MI()
a = LI()
b = LI()
for i in range(1,n):
	a[i]+=a[i-1]
for i in range(1,m):
	b[i]+=b[i-1]
ans = max(bs(a,n,k),bs(b,m,k))+1
for i in range(n):
	if k<a[i]:
		break
	else:
		temp = i+1+bs(b,m,k-a[i])+1
		ans = max(ans,temp)
for i in range(m):
	if k<b[i]:
		break
	else:
		temp = i+1+bs(a,n,k-b[i])+1
		ans = max(ans,temp)
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()))
aa = [0] 
bb = [0]
for i in range(m):
    bb.append(bb[-1]+B[i])
for i in range(n):
    aa.append(aa[-1]+A[i])
aa.append(aa[-1]+1)
bb.append(bb[-1]+1)

#print(aa)

ans = 0

for i in range(n+1):
    temp = k-aa[i]
    if temp<0:
        break
    s = 0
    l = m+1
    mid = l//2
    while l-s>1:
        if temp>bb[mid]:
            s = mid
            mid = (s+l)//2
        elif temp==bb[mid]:
            s = mid
            break
        else:
            l = mid
            mid = (s+l)//2
    #print(s)
    #print(ans, s+i)
    ans = max(ans, s+i)
print(ans)

Submission status: Accepted

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

a_sum = [0]
b_sum = [0]
for i in range(1, n+1):
  a_sum.append(a_sum[i-1] + an[i-1])
for j in range(1, m+1):
  b_sum.append(b_sum[j-1] + bn[j-1])

idx = len(bn)
ans = 0
for i in range(n+1):
  while a_sum[i] + b_sum[idx] > k:
    idx -= 1
    if idx == -1:
      break
  if idx == -1:
    break
  ans = max(ans, i + idx)

print(ans)

Submission status: Accepted

Solution code:
N, M, K = map(int, input().split())
INF = int(1e18)
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A = A[:]
B = B[:]
i, j = 0, 0
mcnt = 0
tmp = M+1
ccnt = 0
while tmp != 0:
  ccnt += 1
  tmp //= 2
LA = [0 for _ in range(N+1)]
LB = [0 for _ in range(M+1)]
for i in range(N):
  LA[i+1] = LA[i] + A[i]
for i in range(M):
  LB[i+1] = LB[i] + B[i]
for i in range(N+1):
  l, r = 0, M+1
  d = (l + r) // 2
  if LA[i] <= K:
    if LB[M] + LA[i] <= K:
      mcnt = max(i + M, mcnt)
    else:
      for j in range(ccnt + 1):
        if LB[d] + LA[i] <= K:
          l = d
          d = (l + r) // 2
        else:
          r = d
          d = (l + r) // 2
      mcnt = max(i + d, mcnt)
  else:
    break
print(mcnt)

Submission status: Accepted

Solution code:
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
suma=[a[0]]
sumb=[b[0]]
for i in range(1,n):
    suma.append(suma[-1]+a[i])
for i in range(1,m):
    sumb.append(sumb[-1]+b[i])
tot=0
ans=0
ma=0
j=m-1
for i in range(n):
    if(suma[i]>k):
        break
    else:
        tot=i+1
        sub=k-suma[i]
        while(j>=0):
            if(sumb[j]<=sub):
                break
            else:
                j=j-1
        ans=max(tot+j+1,ans)
j=n-1
for i in range(m):
    if(sumb[i]>k):
        break
    else:
        tot=i+1
        sub=k-sumb[i]
        while(j>=0):
            if(suma[j]<=sub):
                break
            else:
                j=j-1
        ans=max(tot+j+1,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()))
C=[0 for i in range(N+1)]
D=[0 for j in range(M+1)]
C[0]=0
for i in range(1,N+1):
    C[i]=A[i-1]+C[i-1]
D[0]=0
for i in range(1,M+1):
    D[i]=B[i-1]+D[i-1]
count=0
for i in range(N+1):
    rest_time=K-C[i]+0.5
    if rest_time>0:
      j=bisect.bisect_left(D,rest_time)-1
      count=max(count,i+j)
print(count)

Submission status: Accepted

Solution code:
import sys
def input():return sys.stdin.readline()[:-1]
def N(): return int(input())
def NM():return map(int,input().split())
def L():return list(NM())
def LN(n):return [N() for i in range(n)]
def LL(n):return [L() for i in range(n)]
n,m,k=NM()
a=[0]+L()
b=[0]+L()
ans=0
INF=10**9+7
from bisect import bisect, bisect_left, bisect_right
for i in range(1,n+1):
    a[i]+=a[i-1]
    if a[i]>INF:
        a[i]=INF
for i in range(1,m+1):
    b[i]+=b[i-1]
    if b[i]>INF:
        b[i]=INF
for i in range(n+1):
    x=k-a[i]
    if x<0:
        continue
    ans=max(ans,i+bisect_right(b,x)-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_max = 0
sum = 0
for i in range(n):
  sum += A[i]
  if sum > k:
    sum -= A[i]
    break
  a_max += 1
  
ans = a_max
kotae =ans
j = 0


for i in range(m):
  sum += B[i]
  ans += 1
  while sum > k and j < a_max:
    sum -= A[a_max-1-j]
    ans -= 1
    j += 1
  if sum > k:
    print(kotae)
    exit()
  kotae = max(kotae, ans)

  
print(kotae)

Submission status: Accepted

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

N, M, K = map(int, input().split())
tempA = [int(i) for i in input().split()]
tempB = [int(i) for i in input().split()]
A = [0] * (N+1)
for i in range(1, N+1):
    A[i] = A[i-1] + tempA[i-1]
B = [0] * (M+1)
for i in range(1, M+1):
    B[i] = B[i-1] + tempB[i-1]

ans = 0
for a in range(N+1):
    if A[a] > K:
        break
    for b in range(ans-a+1, M+1):
        if A[a] + B[b] <= K:
            ans = max(ans, a+b)
        else:
            break

print(ans)