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:
from bisect import bisect_right
from itertools import accumulate

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

m=0
for i in range(0,N+1):
  if K<A[i]:
    break
  m=max(m,i+bisect_right(B,K-A[i])-1)

print(m)

Submission status: Accepted

Solution code:
#in the name of allah
import sys
import math as mt
import bisect
input=sys.stdin.buffer.readline  
t=1
#t=int(input())
for _ in range(t):
    n,m,k=map(int,input().split())
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    pref_a=[0]*(n+1)
    pref_b=[0]*(m+1)
    for i in range(n):
        pref_a[i+1]=pref_a[i]+a[i]
    for i in range(m):
        pref_b[i+1]=pref_b[i]+b[i]
    maxi=0
    for i in range(n+1):
        if k>=pref_a[i]:
            k1=k-pref_a[i]
            pos=bisect.bisect_right(pref_b,k1)        
            maxi=max(maxi,pos+i-1)
        else:
            break
    print(maxi)

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]
cnt = 0
best_b = m
confirm = ()

for i in a:
    a_sum.append(i + a_sum[-1])

for i in b:
    b_sum.append(i + b_sum[-1])

for a_idx in range(n + 1):
    if a_sum[a_idx] > k:
        break
    for b_idx in reversed(range(best_b + 1)):
        if b_sum[b_idx] + a_sum[a_idx] <= k:
            # if cnt < a_idx + b_idx:
            cnt = max(cnt, a_idx + b_idx)
            best_b = b_idx
            break

print(cnt)

Submission status: Accepted

Solution code:
n, m, k = map(int, input().split())
a = [0] + list(map(int, input().split()))
b = [0] + list(map(int, input().split()))
for i in range(1, n + 1):
    a[i] += a[i - 1]
for i in range(1, m + 1):
    b[i] += b[i - 1]


def search(arr, x):
    l, r = 0, len(arr)
    while abs(r - l) > 1:
        mid = (l + r) // 2
        if arr[mid] <= x:
            l = mid
        else:
            r = mid
    return l


ans = 0
for i in range(len(a)):
    rest = k - a[i]
    if rest < 0:
        break
    bidx = search(b, rest)
    if bidx == -1 or bidx == len(b):
        continue
    ans = max(ans, i + bidx)
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()))
x=[0]
y=[0]
for i in range(n):
  x.append(x[i]+a[i])
for i in range(m):
  y.append(y[i]+b[i])
res=0
for i in range(n+1):
  if x[i]>k:
    break
  else:
    t=k-x[i]
    bb=bisect.bisect(y,t)
    res=max(res,i+bb-1)
print(res)

Submission status: Accepted

Solution code:
def is_ok(index, key):
    return B[index] <= key

def upper_bound(key): 
    ng = len(B)
    ok = -1

    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2
        if is_ok(mid, key):
            ok = mid
        else:
            ng = mid
    
    return ok 

n, m, k = map(int, input().split())
tmpA = list(map(int, input().split()))
tmpB = list(map(int, input().split()))
A = [0]
B = [0]
# 読むのにかかる時間の累計和のリスト
if A:
    for i in range(n):
        tmp = A[i] + tmpA[i]
        if tmp > k:
            break
        else:
            A.append(tmp)
if B:
    for i in range(m):
        tmp = B[i] + tmpB[i]
        if tmp > k:
            break
        else:
            B.append(tmp)

answer = 0
for i in range(len(A)):
    key = k - A[i]
    answer = max(answer, upper_bound(key) + i)

print(answer)

Submission status: Accepted

Solution code:
N, M, K = map(int, input().split())
 
A = list(map(int, input().split()))
B = list(map(int, input().split()))
 
def solve(A, N, B, M, K):
  
  def cumsum(l):
    rt = [0]
    s = 0
    for n in l:
      s += n
      rt.append(s)
    return rt
  
  preA = cumsum(A)
  preB = cumsum(B)
  
  s, e = 0, N + M
  
  def can_read(num):
    for i in range(N + 1):
      j = num - i
      if j >= 0 and j < (M + 1) and (preA[i] + preB[j]) <= K:
      	return True
    return False

  res = 0
  while s <= e:
    mid = s + (e - s) // 2
    can = can_read(mid)
    
    if can:
      res = mid
      s = mid + 1
    else:
      e = mid - 1
  
  return res

res = solve(A, N, B, M, K)

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

lia, lib = [0], [0]
for i in range(n):
    lia.append(lia[i] + a[i])
for i in range(m):
    lib.append(lib[i] + b[i])

ans, j = 0, m
for i in range(n + 1):
    if lia[i] > k:
        break

    while lib[j] > k - lia[i]:
        j -= 1
    ans = max(ans, i + j)

print(ans)

Submission status: Accepted

Solution code:
n,m,k=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
a,b=[0],[0]
for i in range(n):
    a.append(a[i]+A[i])
for i in range(m):
    b.append(b[i]+B[i])

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

Submission status: Accepted

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

A_cum = [0]
B_cum = [0]
for i in range(N):
    A_cum.append(A[i] + A_cum[i])
for i in range(M):
    B_cum.append(B[i] + B_cum[i])
    
book = 0
j = M
for i in range(N+1):
    if A_cum[i] > K:
        break
    while A_cum[i] + B_cum[j] > K:
        j -= 1
    book = max(book, i+j)
    
print(book)

Submission status: Accepted

Solution code:
N, M , K = map(int, input().split())
X = [0] + list(map(int, input().split()))
Y = list(map(int, input().split()))
R,sY,j,t= 0,sum(Y),M,0
for i in range(N+1):
    t += X[i]
    if t > K: break
    while j and sY+t > K:
        j-=1
        sY -= Y[j]
    R = max(R, j+i)
print(R)

Submission status: Accepted

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

    acum = [0] * (n + 1)
    bcum = [0] * (m + 1)
    for i in range(n):
        acum[i + 1] = acum[i] + a[i]
    for i in range(m):
        bcum[i + 1] = bcum[i] + b[i]
        
    j = m
    maxv = 0
    for i in range(n+1):
        if acum[i] > k:
            break
        while acum[i] + bcum[j] > k:
            j -= 1
        maxv = max(maxv, i + j)
    print(maxv)

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

import numpy as np
import bisect

ra=[0]+list(np.cumsum(a))
rb=[0]+list(np.cumsum(b))

ans=0
for i in range(n+1):    
    capa = k - ra[i]
    if ra[i]>k:
        break
    j=bisect.bisect_right(rb,capa)


    ans=max(ans,i-1+j)
            
print(ans)

Submission status: Accepted

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

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

for Ai in A:
    accA.append(accA[-1]+Ai)

accB = [0]

for Bi in B:
    accB.append(accB[-1]+Bi)

ans = 0
j = M

for i in range(N+1):
    if accA[i]>K:
        break
    
    while accA[i]+accB[j]>K:
        j -= 1
    
    ans = max(ans, i+j)

print(ans)

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline
readall = sys.stdin.read
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prn = lambda x: print(*x, sep='\n')

def solve():
    n, m, k = nm()
    a = nl() + [k+1]
    b = nl() + [k+1]
    ai = bi = 0
    als = bs = 0
    while bs + b[bi] <= k:
        bs += b[bi]
        bi += 1
    ans = bi
    for i in range(n+1):
        als += a[i]
        if als > k:
            break
        while bs + als > k:
            bi -= 1
            bs -= b[bi]
        ans = max(ans, i + bi + 1)
    print(ans)
    return

solve()

Submission status: Accepted

Solution code:
import numpy as np
import sys
from collections import defaultdict
readline = sys.stdin.buffer.readline
# sys.setrecursionlimit(10**8)


def geta(fn=lambda s: s.decode()):
    return map(fn, readline().split())


def gete(fn=lambda s: s.decode()):
    return fn(readline().rstrip())


def main():
    n, m, k = geta(int)
    a = np.array([0] + list(geta(int)))
    b = np.array([0] + list(geta(int)))

    ca = np.cumsum(a)
    cb = np.cumsum(b)

    ans = 0
    for i, ai in enumerate(ca):
        if k < ai:
            break

        j = np.searchsorted(cb, k-ai, side="right") - 1

        if ans < i + j:
            ans = i + j

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
A,B=[0],[0]
for i in range(n):
    A.append(A[i]+a[i])
for i in range(m):
    B.append(B[i]+b[i])
ans,j = 0,m
for i in range(n+1):
    if A[i]>k:
        break
    while B[j] >k-A[i]:
        j-=1
    ans=max(ans,i+j)
print(ans)

Submission status: Accepted

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

import numpy as np
import bisect

ra=[0]+list(np.cumsum(a))
rb=[0]+list(np.cumsum(b))

ans=0
for i in range(n+1):    
    capa = k - ra[i]
    if capa<0:
        break
    j=bisect.bisect_right(rb,capa)


    ans=max(ans,i-1+j)
            
print(ans)

Submission status: Accepted

Solution code:
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
acum1 = [0]
for i in range(N):
    acum1.append(acum1[-1] + A[i])
acum2 = [0]
for i in range(M):
    acum2.append(acum2[-1] + B[i])
ans = 0
j = M
for i in range(N + 1):
    if acum1[i] > K:
        break
    while acum2[j] > K - acum1[i] and j > 0:
        j -= 1
    ans = max(ans, i + j)
print(ans)

Submission status: Accepted

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

A = [int(x) for x in input().split()]
B = [int(y) for y in input().split()]

a = [0]
b = [0]

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

    
ans,j = 0,M

for i in range(N+1):
    if a[i] > K:
        break
    while K < a[i] + b[j]:
        j -= 1
    ans = max(ans,i+j)
    
print(ans)

Submission status: Accepted

Solution code:
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 bisect import bisect_right
ans=bisect_right(b,k)
for i in range(n):
  if a[i]>k:break
  ans=max(ans,i+1+bisect_right(b,k-a[i]))
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()))

na, nb = [0], [0]
for i in range(n):
    na.append(na[i] + a[i])
for j in range(m):
    nb.append(nb[j] + b[j])

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

Submission status: Accepted

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

P=[0]*(N+1)
Q=[0]*(M+1)

for i in range(1, N+1):
  P[i]=P[i-1]+A[i-1]
  
for i in range(1, M+1):
  Q[i]=Q[i-1]+B[i-1]
  
import bisect
ans=0
for i in range(N+1):
  t=K-P[i]
  if t>=0:
    tmp=i+bisect.bisect_right(Q, t)-1
    ans=max(ans, tmp)
  
print(ans)

Submission status: Accepted

Solution code:
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))

import numpy as np

def binarySearch(lis,key):
    ng = -1
    ok = len(lis)
    while abs(ok-ng) > 1:
        mid = (ok+ng)//2
        if isOk(lis,mid,key): ok = mid
        else: ng = mid
    return ok

def isOk(lis,index,key):
    if lis[index] > key: return True
    else: return False

n,m,k = iim()
a = iil()
cuma = np.cumsum(a)
b = iil()
cumb = np.cumsum(b)

ans = binarySearch(cumb,k)
for i,item in enumerate(cuma):
    if item > k:
        break

    res = k-item
    plus = binarySearch(cumb,res)
    ans = max(ans,i+1+plus)

print(ans)

Submission status: Accepted

Solution code:
#abc172c
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
x=[0]
y=[0]
for i in range(n):
 x.append(x[i]+a[i])
for i in range(m):
 y.append(y[i]+b[i])
res=0
j=m
for i in range(n+1):
 if x[i]>k:
  break
 while y[j]>k-x[i]:
  j-=1
 res=max(res,i+j)
print(res)

Submission status: Accepted

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

cum_A = [0] * (N + 1)
for i in range(1, N + 1):
    cum_A[i] = cum_A[i - 1] + A[i - 1]
cum_B = [0] * (M + 1)
for i in range(1, M + 1):
    cum_B[i] = cum_B[i - 1] + B[i - 1]

ans = 0
b_ub = M + 1
for i in range(N + 1):
    rest = K - cum_A[i]
    for j in range(b_ub)[::-1]:
        if cum_B[j] <= rest:
            ans = max(i + j, ans)
            b_ub = j + 1
            break

print(ans)

Submission status: Accepted

Solution code:
import sys 
import numpy as np
import bisect
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N, M, K = map(int, readline().split())
A = list(map(int,readline().split()))
B = list(map(int,readline().split()))
A_cum = [0] + list(np.cumsum(A))
B_cum = [0] + list(np.cumsum(B))
A_count = bisect.bisect_right(A_cum,K)
ans = 0
for a in range(A_count):
    t = K-A_cum[a]
    b = bisect.bisect_right(B_cum,t)
    b -=1
    ans = max(ans, a+b)
print(ans)

Submission status: Accepted

Solution code:
import sys
def rs(): return sys.stdin.readline().rstrip()
def ri(): return int(rs())
def rs_(): return [_ for _ in rs().split()]
def ri_(): return [int(_) for _ in rs().split()]

import itertools
import bisect

N, M, K = ri_()
A = [0] + list(itertools.accumulate(ri_()))
B = list(itertools.accumulate(ri_()))
ans = 0
for i in range(N + 1):
    if K - A[i] >= 0:
        j = bisect.bisect_right(B, K - A[i])
        ans = max(ans, i + j)
print(ans)

Submission status: Accepted

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

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

A, B = [0], [0]

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

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

ans = 0
j = m

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

Submission status: Accepted

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

a=[0]
b=[0]

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

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

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


print(ans)

Submission status: Accepted

Solution code:
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))

def binary_search(lis, item):
    low = 0
    high = len(lis) - 1
    inRange = high

    if lis[0] > item:
        return 0

    while low <= high:
        mid = (low + high) //2
        guess1 = lis[mid]
        if mid < inRange:
            guess2 = lis[mid+1]
        else:
            if lis[mid] <= item:
                return len(lis)
            else:
                return len(lis)-1
        if guess1 <= item < guess2:
            return mid+1
        if guess1 > item:
            high = mid-1
        else:
            low = mid+1
    return 0

n,m,k = iim()
a = iil()
cuma = []
if a[0] <= k:
    cuma.append(a[0])
    for i in range(n-1):
        num = cuma[-1]+a[i+1]
        if num > k:
            break
        else:
            cuma.append(num)
#print(len(cuma))
b = iil()
cumb = []
if b[0] <= k:
    cumb.append(b[0])
    for i in range(m-1):
        num = cumb[-1]+b[i+1]
        if num > k:
            break
        else:
            cumb.append(num)
#print(len(cumb))

ans = len(cumb)
#print(ans)
if len(cuma) > 0:
    cnt = len(cuma)
    for i,item in enumerate(cuma[::-1]):
        tmp = cnt-i
        res = k-item
        plus = binary_search(cumb,res)
        ans = max(ans,tmp+plus)
print(ans)
#print(ans)

#print(cuma)
#print(cumb)

Submission status: Accepted

Solution code:
###==================================================
### import
#import bisect
#from collections import Counter, deque, defaultdict
#from copy import deepcopy
#from functools import reduce, lru_cache
#from heapq import heappush, heappop
#import itertools
#import math
#import string
import sys
### stdin
def input(): return sys.stdin.readline()
def iIn(): return int(input())
def iInM(): return map(int, input().split())
def iInM1(): return map(int1, input().split())
def iInLH(): return list(map(int, input().split()))
def iInLH1(): return list(map(int1, input().split()))
def iInLV(n): return [iIn() for _ in range(n)]
def iInLV1(n): return [iIn()-1 for _ in range(n)]
def iInLD(n): return [iInLH() for _ in range(n)]
def iInLD1(n): return [iInLH1() for _ in range(n)]
def sInLH(): return list(input().split())
def sInLV(n): return [input().rstrip("\n") for _ in range(n)]
def sInLD(n): return [sInLH() for _ in range(n)]
### stdout
def OutH(lst): print(*lst)
def OutV(lst): print(*lst, sep="\n")
### setting
sys.setrecursionlimit(10 ** 6)
### utils
int1 = lambda x: int(x) - 1
### constants
INF = int(1e9)
MOD = 1000000007
dx = (-1, 0, 1, 0)
dy = (0, -1, 0, 1)
###---------------------------------------------------

N, M, K = iInM()
A = iInLH()
B = iInLH()

a, b = [0], [0]
for i in range(N):
    a.append(a[i]+A[i])
for j in range(M):
    b.append(b[j]+B[j])

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

print(ans)

Submission status: Accepted

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

b=[0 for i in range(len(B)+1)]
for i in range(1,len(B)+1):
    b[i]=b[i-1]+B[i-1]

j=M
ans=0
for i in range(len(A)+1):

    if(K<a[i]):
        break
    #print(a[i]+b[j])
    while(a[i]+b[j]>K):
        j=j-1
    ans=max(ans,i+j)

print(ans)

Submission status: Accepted

Solution code:
#172C
#Tsundoku

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

a,b=[0],[0]

for i in range(N):
    a.append(a[i]+A[i])
for j in range(M):
    b.append(b[j]+B[j])

ans=0
j=M

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

Submission status: Accepted

Solution code:
import sys
from collections import deque
import copy
sys.setrecursionlimit(2147483647)
input = sys.stdin.readline

if __name__ == '__main__':
  n,m,k=map(int,input().strip("\n").split())
  A=list(map(int,input().strip("\n").split()))
  B=list(map(int,input().strip("\n").split()))
  t=sum(B)
  j=m
  ans=0
  for i in range(n+1):
    while j>0 and t>k:
      j-=1
      t-=B[j]
    if t>k:
      break
    ans=max(ans,i+j)
    if i==n:
      break
    t+=A[i]
  print(ans)

Submission status: Accepted

Solution code:
import sys
from io import StringIO
import unittest
import os
import bisect

# 再帰処理上限(dfs作成時に設定するのが面倒なので限度近い値を組み込む)
sys.setrecursionlimit(999999999)


def cumsum(target: []):
    result = []
    for cnt, a in enumerate(target):
        result.append(a) if cnt == 0 else result.append(result[cnt - 1] + a)
    return result


# 実装を行う関数
def resolve(test_def_name=""):
    n, m, k = map(int, input().split())
    a_s = list(map(int, input().split()))
    b_s = list(map(int, input().split()))

    sum_a_s = [0] + cumsum(a_s)
    sum_b_s = [0] + cumsum(b_s)

    # k=100として AをX冊読んだ場合の残時間のリストを作る。
    # つまり・・0=100 1=80 2=40 という感じ。
    remain_time_s = []
    for sum_a in sum_a_s:
        if sum_a > k:
            break
        remain_time_s.append(k - sum_a)

    ans = 0
    b_start = 0
    sum_b_s_rev = list(reversed(list(enumerate(sum_b_s))))

    # 最適解を求める。
    for cnt_a, remain_time in enumerate(remain_time_s):
        # Bは逆から見ていく。
        for i in range(b_start, len(sum_b_s_rev)):
            if sum_b_s_rev[i][1] > remain_time:
                b_start += 1
                continue
            ans = max(ans, cnt_a + sum_b_s_rev[i][0])
            break

    print(ans)


class TestClass(unittest.TestCase):
    def assertIO(self, assert_input, output):
        stdout, sat_in = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(assert_input)
        resolve(sys._getframe().f_back.f_code.co_name)
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, sat_in
        self.assertEqual(out, output)

    def test_input_1(self):
        test_input = """3 4 240
60 90 120
80 150 80 150"""
        output = """3"""
        self.assertIO(test_input, output)

    def test_input_2(self):
        test_input = """3 4 730
60 90 120
80 150 80 150"""
        output = """7"""
        self.assertIO(test_input, output)

    def test_input_3(self):
        test_input = """5 4 1
1000000000 1000000000 1000000000 1000000000 1000000000
1000000000 1000000000 1000000000 1000000000"""
        output = """0"""
        self.assertIO(test_input, output)

    # 自作テストパターンのひな形(利用時は「tes_t」のアンダーバーを削除すること
    def test_origin_1(self):
        test_input = """2 3 100
90 10
10 10 10"""
        output = """3"""
        self.assertIO(test_input, output)


# 実装orテストの呼び出し
if __name__ == "__main__":
    if os.environ.get("USERNAME") is None:
        # AtCoder提出時の場合
        resolve()

    else:
        # 自PCの場合
        unittest.main()

Submission status: Accepted

Solution code:
from bisect import bisect_right

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

sA=[0]
for i in range(n):
    sA.append(sA[-1]+A[i])
sB=[B[0]]
for i in range(1,m):
    sB.append(sB[-1]+B[i])

ans=0
for i in range(n+1):
    temp=k-sA[i]
    if temp<0:
        break
    add=bisect_right(sB,temp)
    ans=max(ans,i+add)

print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
import sys
import math
import random
from decimal import Decimal


N, M, K = [int(x) for x in stdin.readline().rstrip().split()]
A = list(map(int, input().split()))
A1 = [0]
for i, v in enumerate(A):
    x = A1[i]+v
    A1.append(x)
B = list(map(int, input().split()))
B1 = [0]
for i, v in enumerate(B):
    x = B1[i]+v
    B1.append(x)
c1 = 0
c2 = 0
ans = 0
temp = M

for i in range(N+1):
    if A1[i] > K:
        break
    while B1[temp] > K-A1[i]:
        temp -= 1
    ans = max(ans, i+temp)
    # for k in range(temp, 0, -1):
    #     if A1[i]+B1[k] <= K:
    #         ans = max(ans, i+k)
    #         temp = k
    #         break
# print(c1, c2)

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
import bisect

n, m, k = map(int, input().split())
A = [0] + list(map(int,input().split())) # '0'冊目も入れとく
B = [0] + list(map(int,input().split())) # 同上

a = np.cumsum(A) # 'A'の累積
b = np.cumsum(B) # 'B'同上

ans = 0
for i in range(n+1): # 二分探索_方針: A側で読む本の冊数を固定する！
    time = k - a[i] # ↑で固定した時の残り時間
    if time < 0: # 残り時間がない場合は探索中止
        break
    tmp = bisect.bisect_right(b, time) - 1 # 残りタイム内で読めるB側の本のmax冊数を探索_0冊目とbisect右/左を考慮して'-1'
    ans = max(ans, tmp + i)

print(ans)

Submission status: Accepted

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

a = [0]
b = [0]

for i in range(N):
  a.append(a[i] + AList[i])
for j in range(M):
  b.append(b[j] + BList[j])

ans = 0
j = M

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

Submission status: Accepted

Solution code:
import itertools
import bisect

n,m,k=map(int,input().split())
a=[0]+list(map(int,input().split()))
b=list(map(int,input().split()))
aa=list(itertools.accumulate(a))
bb=list(itertools.accumulate(b))
r=0
for i,ax in enumerate(aa):
    bis=bisect.bisect_right(bb,k-ax)
    if bis==0 and ax>k:continue
    r=max(r,i+bis)
print(r)

Submission status: Accepted

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

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

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

a = [0] + A
b = [0] + B

for i in range(len(a)-1):
    a[i+1] += a[i]

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

ans = 0
j = M

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

Submission status: Accepted

Solution code:
#!python3

import sys
iim = lambda: map(int, sys.stdin.readline().rstrip().split())
from bisect import bisect

def resolve():
    it = map(int, sys.stdin.read().split())
    N, M, K = next(it), next(it), next(it)

    A = [0] * (N+1)
    B = [0] * (M+1)
    for i in range(1, N+1):
        A[i] = A[i-1] + next(it)
    for i in range(1, M+1):
        B[i] = B[i-1] + next(it)

    ax = min(bisect(A, K), N)
    bx = min(bisect(B, K), M)

    ans = 0
    for i in range(ax+1):
        rest = K - A[i]
        for j in range(bx, -1, -1):
            if B[j] <= rest:
                bx = j
                ans = max(ans, i+j)
                break

    print(ans)


if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
import sys
def input():
    return sys.stdin.readline()
N, M, K = map(int, input().split())
*A, = map(int, input().split())
*B, = map(int, input().split())
a = [0] * (N+1)
b = [0] * (M+1)
for i in range(N):
    a[i+1] = a[i] + A[i]
for i in range(M):
    b[i+1] = b[i] + B[i]
ans = 0
j = M
for i in range(N+1):
    tmp = K - a[i]
    if tmp < 0:
        break
    while b[j] > tmp:
        j -= 1
    ans = max(ans, i + j)
print(ans)

Submission status: Accepted

Solution code:
N, M, K = map(int, input().split())
*A, = map(int, input().split())
*B, = map(int, input().split())
a = [0] * (N+1)
b = [0] * (M+1)
for i in range(N):
    a[i+1] = a[i] + A[i]
for i in range(M):
    b[i+1] = b[i] + B[i]
ans = 0
j = M
for i in range(N+1):
    tmp = K - a[i]
    if tmp < 0:
        break
    while b[j] > tmp:
        j -= 1
    ans = max(ans, i + j)
print(ans)

Submission status: Accepted

Solution code:
import sys
import numpy as np
import math
import collections
from collections import deque 
from functools import reduce

# input = sys.stdin.readline
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a_t, b_t = [0], [0]
for i in range(n):
    a_t.append(a_t[i] + a[i])
for i in range(m):
    b_t.append(b_t[i] + b[i])
ans = 0
j = m
for i in range(n+1):
    if (a_t[i] > k):
        break
    while(a_t[i] + b_t[j] > k):
        j -= 1
    ans = max(ans, i + j)
print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate
from bisect import bisect

n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a = [0] + list(accumulate(a))
b = [0] + list(accumulate(b))
ans = 0
j = m
for i in range(n+1):
    if a[i] > k:
        break
    while b[j] > k - a[i]:
        j -= 1
    ans = max(ans, i+j)
print(ans)

Submission status: Accepted

Solution code:
n,m,k = map(int,input().split())
a_li = [0] + list(map(int,input().split()))
b_li = [0] + list(map(int,input().split()))

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

ans = 0
b_cnt = m
for a_cnt in range(n+1):
    if a_li[a_cnt] > k:
        continue
    while a_li[a_cnt] + b_li[b_cnt] > k:
        b_cnt -= 1
    ans = max(ans,a_cnt+b_cnt)
print(ans)

Submission status: Accepted

Solution code:
N, M, K = map(int, input().split())
*A, = map(int, input().split())
*B, = map(int, input().split())
a = [0] * (N+1)
b = [0] * (M+1)
for i in range(N):
    a[i+1] = a[i] + A[i]
for i in range(M):
    b[i+1] = b[i] + B[i]
ans = 0
j = M
for i in range(N+1):
    if a[i] > K:
        break
    while b[j] > K - a[i]:
        j -= 1
    ans = max(ans, i + j)
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect
N, M, K = map(int, input().split())
*A, = map(int, input().split())
*B, = map(int, input().split())
a = [0] * (N+1)
b = [0] * (M+1)
for i in range(N):
    a[i+1] = a[i] + A[i]
for i in range(M):
    b[i+1] = b[i] + B[i]
ans = 0
for i in range(N+1):
    tmp = K - a[i]
    if tmp < 0:
        break
    ans = max(ans, i + bisect(b, tmp) - 1)
print(ans)

Submission status: Accepted

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

csum_a = [0]
csum_b = [0]

for i in range(n):
    csum_a.append(csum_a[i] + a_i[i])
    
for i in range(m):
    csum_b.append(csum_b[i] + b_i[i])
    
b_num = m + 1
ans = 0

for i in range(n + 1):
    for j in reversed(range(b_num)):
        if csum_a[i] + csum_b[j] <= k:
            ans = max(ans, i + j)
            b_num = j + 1
            break

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
a = [0]
b = [0]
for i in range(N):
    a.append(a[i] + A[i])
for i in range(M):
    b.append(b[i] + B[i])
ans = 0
j = M
for i in range(N+1):
    if a[i] > K:
        break
    while b[j] + a[i] > K:
        j -= 1
    ans = max(ans, i + j)
print(ans)