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:
def INT():
    return int(input())

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

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

N, M, K = MI()
A = [0] + LI()
B = [0] + LI()

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

for i in range(1, M + 1):
    B[i] += B[i - 1]
    
    if B[i] <= K:
        ans = i

ans = 0

for bk_a in range(N + 1):
    time = A[bk_a]

    if time > K:
        break
        
    l, r = -1, M + 1
    while abs(l - r) > 1:
        bk_b = (l + r) // 2

        if time + B[bk_b] <= K:
            l = bk_b
        else:
            r = bk_b
    
    bk_b = max(0, l)
    ans = max(ans, bk_a + bk_b)

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float("INF")

#solve
def solve():
    n, m, k = LI()
    a = LI()
    b = LI()
    a = list(itertools.accumulate(a))
    b = list(itertools.accumulate(b))
    ans = bisect_right(a, k)
    for i, bi in enumerate(b, start=1):
        if bi > k:
            break
        ans = max(ans, i + bisect_right(a, k - bi))
    print(ans)
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Accepted

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

A = [0]
B = [0]
ans = 0

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

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

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 bisect
N,M,K=map(int,input().split())
SumA=[0]*(N+1)
SumB=[0]*(M+1)
A=list(map(int, input().split()))
B=list(map(int,input().split()))
for i in range(1,N+1):
    SumA[i]= A[i-1]+SumA[i-1]
for i in range(1,M+1):
    SumB[i] = B[i-1]+SumB[i-1]

ans = 0
for i in range(N+1):
    num = i
    rest = K-SumA[i]
    if rest<0:
        break;
    num += bisect.bisect_right(SumB, rest)-1
    ans = max(num, ans)

print(ans)

Submission status: Accepted

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

for i in range(n):
    if i == 0:
        continue
    else:
        a[i] += a[i-1]

for i in range(m):
    if i == 0:
        continue
    else:
        b[i] += b[i - 1]

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

i = 0
j = m
ans = 0

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

    ans =max(ans,i+j)
   
    i += 1
    if i > n:
        break

print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
import numpy as np
def main():
    #入力
    readline=stdin.readline
    n,m,k=map(int,readline().split())
    a=list(map(int,readline().split()))
    b=list(map(int,readline().split()))

    a=np.array(a,dtype=np.int64).cumsum()
    b=np.array(b,dtype=np.int64).cumsum()

    a=a[a<=k]
    a=k-a

    if a.size>=1:
        res=np.searchsorted(b,a,side="right")
        tmp=np.arange(1,a.size+1,dtype=np.int64)

        print(max((res+tmp).max(),b[b<=k].size))

    else:
        print(b[b<=k].size)

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]:
    #print(b[j],K - a[i])
    j -= 1
  ans = max(ans, i + j)
  #print("ans",ans)
print(ans)

Submission status: Accepted

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

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

a = [0] * (n + 1)
b = [0] * (m + 1)

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

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

ans = 0
num_b = m

for i in range(n + 1):
    if a[i] > k:
        break
    for j in range(num_b, -1, -1):
        if b[j] <= k - a[i]:
            break
        num_b -= 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()))

cuma = [0]

for a in A:
  cuma.append(cuma[-1]+a)

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

ans = 0
from bisect import bisect
for i, ca in enumerate(cuma):
  if ca > K: break
  t = K - ca
  j = bisect(cumb, t)
  ans = max(ans, i+j-1)

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()))
 
import numpy as np
a = [0] + a
b = [0] + b
sa = np.cumsum(a)
sb = np.cumsum(b)
ans = 0
r = m
for i in range(n+1):
  if sa[i] > k:
    break
  while sa[i] + sb[r] > k:
    r -= 1
  ans = max(ans, i+r)

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

most = 0
cur = 0
tot = 0
for i in range(n):
	if tot + a[i] <= k:
		cur += 1
		tot += a[i]
		if cur > most:
			most = cur
	else:
		i -= 1
		break
j = 0
while j < m:
	if tot + b[j] <= k:
		cur += 1
		tot += b[j]
		if cur > most:
			most = cur
		j += 1
	else:
		if i >= 0:
			cur -= 1
			tot -= a[i]
			i -= 1
		else:
			break
print(most)

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()))
 
sa = [0] * (n + 1)
sb = [0] * (m + 1)
for i in range(n):
  sa[i + 1] = sa[i] + a[i]
for i in range(m):
  sb[i + 1] = sb[i] + b[i]
ans = 0
r = m
for i in range(n+1):
  if sa[i] > k:
    break
  while sa[i] + sb[r] > k:
    r -= 1
  ans = max(ans, i+r)

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()))
sa = [0] * (n + 1)
sb = [0] * (m + 1)
for i in range(n):
  sa[i + 1] = sa[i] + a[i]
for i in range(m):
  sb[i + 1] = sb[i] + b[i]
r = m
ans = 0
for i in range(n + 1):
  if sa[i] > k:
    break
  while sa[i] + sb[r] > k:
    r -= 1
  ans = max(ans, i + r)
print(ans)

Submission status: Accepted

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

tmp = [int(_) for _ in input().split(' ')]
a = [0]
for i in range(n):
    a.append(a[i] + tmp[i])
tmp = [int(_) for _ in input().split(' ')]
b = [0]
for i in range(m):
    b.append(b[i] + tmp[i])

answer = 0
i2 = m
for i1 in range(n + 1):
    while a[i1] + b[i2] > k and i2 > 0:
        i2 -= 1
    if i1 + i2 >= answer and a[i1] + b[i2] <= k:
        answer = i1 + i2

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()))
book_a = [0] * (n + 1)
book_b = [0] * (m + 1)

for i in range(n):
    book_a[i+1] = book_a[i] + a[i]

for j in range(m):
    book_b[j + 1] = book_b[j] + b[j]

r = m
ans = 0

for i in range(n + 1):
    if book_a[i] > k:
        break
    while book_a[i] + book_b[r] > k:
        r -= 1
    ans = max(ans, i + r)

print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict as dd
from collections import deque
import bisect
import heapq

def ri():
    return int(input())

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


def solve():
    n, m, k = rl()
    A = rl()
    B = rl()
    Ap = [0]
    for a in A:
        nxt = Ap[-1] + a
        if nxt > k:
            break
        Ap.append(nxt)

    best = bisect.bisect_right(Ap, k) - 1
    # print (best)
    bs = 0
    bcount = 0
    for b in B:
        bs += b
        bcount += 1
        if bs > k:
            break
        diff = k - bs
        acount = bisect.bisect_right(Ap, diff) - 1
        best = max(best, acount + bcount)
    print (best)




mode = 's'

if mode == 'T':
    t = ri()
    for i in range(t):
        solve()
else:
    solve()

Submission status: Accepted

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

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

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

ans = 0
l = m

for m in range(n+1):
    if al[m] > k:
        break

    while bl[l] > k - al[m]:
        l -= 1
           
    ans = max(ans,m+l)


print(ans)

Submission status: Accepted

Solution code:
import numpy as np
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
 

N, M, K = map(int, readline().split())
AB = np.array(read().split(), np.int32)
A = [0] + np.cumsum(AB[:N]).tolist()
B = [0] + np.cumsum(AB[N:]).tolist()
assert (N == len(A) - 1) and (M == len(B) - 1)
 
ans = 0
left = M
for i in range(len(A)):
    if A[i] > K:
        break
 
    while A[i] + B[left] > K:
        left -= 1
 
    ans = max(ans, left + i)

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
import sys
read = sys.stdin.read


N, M, K = map(int, input().split())
AB = np.array(read().split(), np.int32)
A = [0] + np.cumsum(AB[:N]).tolist()
B = [0] + np.cumsum(AB[N:]).tolist()
assert (N == len(A) - 1) and (M == len(B) - 1)

ans = 0
left = M
for i in range(len(A)):
    if A[i] > K:
        break

    while A[i] + B[left] > K:
        left -= 1

    ans = max(ans, left + i)

print(ans)

Submission status: Accepted

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

#０冊を初期値にする
A=[0]
B=[0]


for a in At:
    if A[-1]+a<=K:
        A.append(A[-1]+a)
    else:
        break
for b in Bt:
    if B[-1]+b<=K:
        B.append(B[-1]+b)
    else:
        break

def is_ok(arg):
    # 条件を満たすかどうか？問題ごとに定義
    if Ak+B[arg]<=K:
        return True
    else:
        return False


def meguru_bisect(ng, ok):
    '''
    めぐる2分探査
    okはBの冊数たりうる値
    ngはBの冊数たり得ない値を入れる。
    '''

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


Ak=0

maxBooks=0
for Ai in range(len(A)): 
    Ak=A[Ai]
    Bi=meguru_bisect(len(B),0) #len(B)は配列の要素数として使われるとき最大＋１
    if maxBooks<(Ai+Bi):
        maxBooks=Ai+Bi
print( maxBooks )

Submission status: Accepted

Solution code:
#: Author - Soumya Saurav
import sys,io,os,time
from collections import defaultdict
from collections import OrderedDict
from collections import deque
from itertools import combinations
from itertools import permutations
import bisect,math,heapq
alphabet = "abcdefghijklmnopqrstuvwxyz"

input = sys.stdin.readline

########################################

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

from bisect import bisect_left, bisect_right

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

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

# print('cumsum_b =', cumsum_b)
ans = -1
for i in range(n+1):
    res = k-cumsum_a[i]
#    print('cumsum_a[{}] ='.format(i), cumsum_a[i])
#    print('res =', res)
    if res < 0:
        break
    bl = bisect_right(cumsum_b, res) - 1 
#    print('i =', i)
#    print('bl =', bl)
    tmp = i + bisect_right(cumsum_b, res) - 1 
    if ans <= tmp:
        ans = tmp 

print(ans)

Submission status: Accepted

Solution code:
"""
// Author : snape_here - Susanta Mukherjee
     
 """
 
from __future__ import division, print_function
 
import os,sys
from io import BytesIO, IOBase
 
if sys.version_info[0] < 3:
    from __builtin__ import xrange as range
    from future_builtins import ascii, filter, hex, map, oct, zip
 
 
def ii(): return int(input())
def fi(): return float(input())
def si(): return input()
def mi(): return map(int,input().split())
def li(): return list(mi())
 
 
def read():
    sys.stdin = open('input.txt', 'r')  
    sys.stdout = open('output.txt', 'w') 

def isPrime(n):
    if (n <= 1) : return False
    if (n <= 3) : return True
    if (n % 2 == 0 or n % 3 == 0) : return False
    i = 5
    while(i * i <= n) : 
        if (n % i == 0 or n % (i + 2) == 0) : 
            return False
        i = i + 6
    return True

def gcd(x, y):
    while y:
        x, y = y, x % y
    return x

mod=1000000007

from math import *

import bisect

from decimal import *

getcontext().prec = 50

abc="abcdefghijklmnopqrstuvwxyz"

pi=3.141592653589793238

def main():

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


# region fastio
 
BUFSIZE = 8192
 
 
class FastIO(IOBase):
    newlines = 0
 
    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None
 
    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()
 
    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()
 
    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)
 
 
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")
 
 
def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()
 
 
if sys.version_info[0] < 3:
    sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
    sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
 
input = lambda: sys.stdin.readline().rstrip("\r\n")
 
# endregion
 
 
if __name__ == "__main__":
    #read()
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import bisect

N,M,K = list(map(int, input().rstrip().split()))
A_list = list(map(int, input().rstrip().split()))
B_list = list(map(int, input().rstrip().split()))
#-----

cum_sum_A = [0]
cum_sum_B = [0]

for i in range(len(A_list)):
    cum_sum_A.append( cum_sum_A[i] + A_list[i] )

for i in range(len(B_list)):
    cum_sum_B.append( cum_sum_B[i] + B_list[i] )


max_num_book = 0

for i_a in range(len(cum_sum_A)):
    if cum_sum_A[i_a] > K:
        break
    
    remain = K - cum_sum_A[i_a]
    i_b = bisect.bisect_right(cum_sum_B, remain)
    
    num_book = i_a + (i_b - 1)
    max_num_book = max(max_num_book, num_book)


print(max_num_book)

Submission status: Accepted

Solution code:
import bisect
n,m,k=map(int,input().split())
s=list(map(int,input().split()))
t=list(map(int,input().split()))
ds=[0]
for i in range(n):
    ds.append(ds[-1]+s[i])
ans=0
p=0
for i in range(m+1):
    if i>0:
        p+=t[i-1]
    if p>k:
        break
    si=bisect.bisect_left(ds,k-p+1)
    ans=max(ans,si+i-1)
print(ans)

Submission status: Accepted

Solution code:
n, m, k = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
a, b =[0], [0]
for i in range(n):
    a.append(a[i] + A[i]) #append関数はリストの末尾に要素を追加する
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:
A, B, C = map(int, input().split())
D = list(map(int, input().split()))
E = list(map(int, input().split()))

a, b = [0], [0]
for i in range(A):
    a.append(a[i] + D[i])
for i in range(B):
    b.append(b[i] + E[i])

ans, j = 0, B
for i in range(A + 1):
    if a[i] > C:
        break

    while b[j] > C - a[i]:
        j -= 1
    ans = max(ans, i + j)
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin

n, m, k = map(int, stdin.readline().rstrip().split())

a = list(map(int, stdin.readline().rstrip().split()))
b = list(map(int, stdin.readline().rstrip().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]  > k- B[j]:
        j -= 1
    ans = max(ans, i + j)

print(ans)

Submission status: Accepted

Solution code:
from sys import stdin

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


a_pre,b_pre=[0],[0]

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

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

ans,j=0,m
for i in range(n+1):
	if a_pre[i]>k:
		break
	while b_pre[j]>k-a_pre[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()))
t, ans = sum(B), 0
j = M
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:
from bisect import bisect_right
n,m,k = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))

cA = [0]*(n+1)
cB = [0]*(m+1)

for i in range(n):
  cA[i+1] = cA[i]+A[i]
  
for i in range(m):
  cB[i+1] = cB[i]+B[i]
  
ans = bisect_right(cA,k) - 1 
ai = ans
bi = 0
while ai>=0 :
  r = k - cA[ai]
  tmp = bisect_right(cB,r) - 1 
  ans = max(ans,tmp+ai)
  ai -= 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, 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 = [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]
i, j, z = 0, m, 0
while i <= n and a[i] <= k:
    while b[j] > k - a[i]:
        j -= 1
    z = max(z, i + j)
    i += 1
print(str(z))

Submission status: Accepted

Solution code:
def main():
    n,m,k=map(int,input().split())
    aa=list(map(int,input().split()))
    bb=list(map(int,input().split()))
    na=nb=0
    while na < n and aa[na] <= k:
        k-=aa[na]
        na+=1
    while nb < m and bb[nb] <= k:
        k-=bb[nb]
        nb+=1
    nmax=na+nb
    while na > 0:
        na-=1
        k+=aa[na]
        while nb < m and bb[nb] <= k:
            k-=bb[nb]
            nb+=1
        if na+nb > nmax:
            nmax=na+nb
    print(nmax)
main()

Submission status: Accepted

Solution code:
from sys import stdin, stdout
input = stdin.readline
print = stdout.write
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]
i, j, z = 0, m, 0
while i <= n and a[i] <= k:
    while b[j] > k - a[i]:
        j -= 1
    z = max(z, i + j)
    i += 1
print(str(z))

Submission status: Accepted

Solution code:
from sys import stdin, stdout
input = stdin.readline
print = stdout.write
n, m, k = map(int, input().split())
a, b = [0], [0]
a.extend(list(map(int, input().split())))
b.extend(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]
i, j, z = 0, m, 0
while i <= n and a[i] <= k:
    while b[j] > k - a[i]:
        j -= 1
    z = max(z, i + j)
    i += 1
print(str(z))

Submission status: Accepted

Solution code:
from sys import stdin, stdout
input = stdin.readline
print = stdout.write
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(1, n + 1):
    a[i] += a[i - 1]
for i in range(1, m + 1):
    b[i] += b[i - 1]
i, j, z = 0, m, 0
while i <= n and a[i] <= k:
    while b[j] > k - a[i]:
        j -= 1
    z = max(z, i + j)
    i += 1
print(str(z))

Submission status: Accepted

Solution code:
from sys import stdin, stdout
input = stdin.readline
print = stdout.write 
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]
i, j, z = 0, m, 0
while i <= n and a[i] <= k:
    while b[j] > k - a[i]:
        j -= 1
    z = max(z, i + j)
    i += 1
print(str(z))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M,K = map(int,input().split())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
sum_AB = 0
cnt = 0
idx_A = -1
idx_B = -1
for i,a in enumerate(A):
    if sum_AB + a > K:
        break
    else:
        sum_AB += a
        cnt += 1
        idx_A = i
for i,b in enumerate(B):
    if sum_AB + b > K:
        break
    else:
        sum_AB += b
        cnt += 1
        idx_B = i
ans = cnt
A = A[0:idx_A+1][::-1]
from collections import deque
B = deque(B[idx_B+1:])
for a in A:
    sum_AB -= a
    cnt -= 1
    while B:
        if sum_AB + B[0] > K:
            break
        else:
            b = B.popleft()
            sum_AB += b
            cnt += 1
    ans = max(cnt,ans)
print(ans)

Submission status: Accepted

Solution code:
#ABC172 C Tsundoku 22:34
from itertools import accumulate

n,m,k = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
s_A = [0]+list(accumulate(A))
s_B = [0]+list(accumulate(B))

ans,j = 0, m
for i in range(n+1):
    if s_A[i] > k: break
    while s_B[j] > k - s_A[i]:
        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())

dp = [0]*(N+1)
tot, lb = sum(B), M
while tot > K: 
    tot -= B.pop(-1)
    lb -= 1
dp[0] = lb

for i in range(N):
    K -= A[i]
    if K < 0:
        continue
    while tot > K and lb > 0: 
        tot -= B.pop(-1)
        lb -= 1
    dp[i+1] = i+1 + lb

print(max(dp))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import collections
from decimal import Decimal # 10進数で考慮できる

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))

"""
dp
time / books_no?
10 0 0
20 0 0
30 0 0
40 0 0
50 0 0
60 1 0
70 1 0
80 0 1




"""



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

"""
片方に注目して探索すればよい　

単純にfor,forだと計算量がまずいが，前回の値の部分から探索すればいいのでそこまで
計算量は増えない

"""

a = [0]
b = [0]

for i in range(N):
    a.append(a[-1] + A[i])

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

# print(a)
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:
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

n,m,k=nm()
a=nl()
b=nl()

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

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

Submission status: Accepted

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

aarr = [0]
total = 0
for a in arr:
    total+=a
    aarr.append(total)
bbrr = [0]
total = 0
for b in brr:
    total+=b
    bbrr.append(total)

ans = 0
bi = len(brr)
for ai in range(len(arr)+1):
    while aarr[ai]+bbrr[bi]>k:
        if bi == 0:
            break
        bi -= 1

    if bi==0 and aarr[ai]+bbrr[bi]>k:
        break
    ans = max(ans,ai+bi)

print(ans)

Submission status: Accepted

Solution code:
from collections import deque

N, M, K = list(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(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()))

a1 = [0]
b1 = [0]

for i in range(n):
    a1.append(a1[-1]+a[i])

for i in range(m):
    b1.append(b1[-1]+b[i])

cnt = m
ans = 0

for i in range(n+1):
    while a1[i] + b1[cnt] > k:
        if cnt == 0:
            break
        cnt -= 1
    if a1[i] + b1[cnt] <= k:
        ans = max(i + cnt, ans)
    else:
        break

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding:utf-8
import itertools
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
j = m

aa = [0] + list(itertools.accumulate(a))
bb = [0] + list(itertools.accumulate(b))
# print(aa)
# print(bb)


# しゃくとり法 / Aの山だけ読みつつ、そこにBからいくつ取り出すかで考える
for i, a_item in enumerate(aa):
    if a_item > k:
        break
    while a_item + bb[j] > k:
        j -= 1
    ans = max(i + j, ans)
print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate
import bisect
n,m,k = map(int, input().split())
alist= list(map(int, input().split()))
blist = list(map(int, input().split()))


alist = list(accumulate(alist))
blist = list(accumulate(blist))

alist.insert(0,0)
blist.insert(0,0)

ans = 0
j = m
for i in range(0,n+1):
  if alist[i] > k:
    pass
  else:
    while blist[j] > k - alist[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)