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

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, p = list(map(int, input().split()))
# a = list(map(int, input().split()))
#
#  abc = [int(input()) for i in range(5)]
# a = int(input())
# w = list(map(int, input().split()))
# n, k = list(map(int, input().split()))
# a = list(map(int, input().split()))
# data = [list(map(int, input().split())) for i in range(n)]
# q = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
# ab_sorted = sorted(ab, key=lambda x: x[0])
# bc = [list(map(int, input().split())) for i in range(q)]
# print(min(15*n, 100*(n//10+1), 100*(n//10)+15*(n % 10)))
arui = [0]
for i in range(n):
    arui.append(arui[i]+a[i])

brui = [0]
for i in range(m):
    brui.append(brui[i]+b[i])

ans = 0
j = m
for i in range(n+1):
    if arui[i] > p:
        break
    bmax = p-arui[i]
    while brui[j] > bmax:
        j -= 1
    if ans <= i+j:
        ans = i+j


print(ans)

Submission status: Accepted

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

t = k
i = 0
asum = [0]* (n+1)
bsum = [0]* (m+1)
suma = 0
sumb = 0

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


for  i in range(m):
    sumb += b[i]
    bsum[i+1] = sumb

count = 1
ans = 0

for i in range(n+1):
    if ans < i :
        if asum[i] <= k:
            ans=i
    else:
        for j in range(ans-i,m+1,1):
            if asum[i] + bsum[j]<=k:
                if i+j>ans:
                    ans=i+j
            else:
                break

print(min(ans,m+n))

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

Submission status: Accepted

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

A_sum = [0]
B_sum = [0]

for a in A_list:
    A_sum.append(A_sum[-1] + a)

for b in B_list:
    B_sum.append(B_sum[-1] + b)

count = 0
j = M
for i, a in enumerate(A_sum):
    if a > K:
        break
    while B_sum[j] > K - a:
        j -= 1
    if count < i + j:
        count = i + j

print(count)

Submission status: Accepted

Solution code:
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
ans=0
aindex=n
bindex=m
acumsum = [0]
bcumsum = [0]

ab =0
for aa in range(n):
  ab += a[aa]
  acumsum.append(ab)# = ab
  if ab>k:
    aindex= aa
    break

ab =0
for bb in range(m):
  ab += b[bb]
  bcumsum.append(ab)
  if ab>k:
    bindex= bb
    break

j= bindex
for i in range(aindex+1):
  if acumsum[i] >k:
    break
  while bcumsum[j] > k-acumsum[i]:
    j -=1
  ans = max(ans, i+j)

print(ans)

Submission status: Accepted

Solution code:
import numpy
import bisect

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

A = numpy.cumsum(numpy.array(A))
A = list(A)
A.insert(0, 0)
B = numpy.cumsum(numpy.array(B))
B = list(B)
B.insert(0, 0)
ans = 0

for i in range(N+1):
    if K - A[i] < 0:
        break
    b = bisect.bisect(B, K - A[i])
    ans = max(ans, i+b-1)

print(ans)

Submission status: Accepted

Solution code:
import sys


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()


def calc_cumsum(l):
    cumsum = [l[0]]
    for i in l[1:]:
        cumsum.append(cumsum[-1] + i)
    return cumsum


def main():
    n, m, k = LI()
    a = LI()
    b = LI()
    costs = [float('inf')] * (n + m + 1)
    costs[0] = 0
    # a, b を i 冊読むのにかかる時間の累積
    ruiseki_a = [0] + calc_cumsum(a)
    ruiseki_b = [0] + calc_cumsum(b)
    best_b_num = 0  # 所要時間 k 以下で読める b の冊数の最大値
    for i in range(m + 1):
        if ruiseki_b[i] <= k:
            best_b_num = i

    ans = 0
    j = best_b_num
    for i in range(n + 1):
        # a の最大の冊数まで調べる
        if ruiseki_a[i] > k:
            break

        # a の冊数 i に対する b の冊数 j
        # a が 1冊増えれば b が 1冊減る
        # 全ループの合計で高々 m 回
        while ruiseki_b[j] > k - ruiseki_a[i]:
            j -= 1
        ans = max(ans, i + j)
    print(ans)


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

ans=0

asums = [0]
asum=0
for i in a:
    asum+=i
    asums.append(asum)

bsums = [0]
bsum=0
for i in b:
    bsum+=i
    bsums.append(bsum)


j=m

i=0

while i<len(asums) and k>=asums[i]:
    while k-asums[i]<bsums[j]:
        j-=1
    ans = max(ans, i+j)
    i+=1
print(ans)

Submission status: Accepted

Solution code:
import itertools
n, m, k = [int(i) for i in input().split()]
a_list = [int(i) for i in input().split()]
b_list = [int(i) for i in input().split()]

a_acc = [0]
a_acc.extend(list(itertools.accumulate(a_list)))
b_acc = [0]
b_acc.extend(list(itertools.accumulate(b_list)))

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

    while b_acc[j] > k - a_acc[i]:
        j -= 1

    ans = max(ans, i+j)

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()))
# print(N, M, K)
# print(A)
# print(B)


consumptions_A = [0]
for i in range(len(A)):
    consumptions_A.append(consumptions_A[i] + A[i])

consumptions_B = [0]
for i in range(len(B)):
    consumptions_B.append(consumptions_B[i] + B[i])

answer = 0
j = M
for i in range(len(consumptions_A)):
    if (consumptions_A[i] > K): break

    remaining_time = K - consumptions_A[i]
    while consumptions_B[j] > remaining_time:
        j -= 1
    
    num_books = i + j
    answer = max(answer, num_books)

print(answer)

Submission status: Accepted

Solution code:
from itertools import accumulate


def binary(N, T):  # 二分探索 # N:探索要素数
    l, r = -1, N
    while r - l > 1:
        if T < b[(l + r) // 2]:  # 条件式を代入
            r = (l + r) // 2
        else:
            l = (l + r) // 2
    return r - 1


n, m, k = map(int, input().split())
a = [0] + list(accumulate(map(int, input().split())))
b = [0] + list(accumulate(map(int, input().split())))

ans = 0
for i, ai in enumerate(a):
    t = k - ai
    if t >= 0:
        j = binary(m + 1, t)
        ans = max(ans, i + j)
print(ans)

Submission status: Accepted

Solution code:
import sys
from bisect import bisect_right
from itertools import accumulate

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


def solve():
    N, M, K = map(int, rl().split())
    A = list(map(int, rl().split()))
    B = list(map(int, rl().split()))
    
    accA = [0] + list(accumulate(A))
    accB = list(accumulate(B))
    ans = 0
    for i, v in enumerate(accA):
        if K < v:
            break
        ans = max(ans, i + bisect_right(accB, K - v))
    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:

import bisect
import numpy

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

cost_a = numpy.insert(numpy.cumsum(a), 0, 0)
cost_b = numpy.insert(numpy.cumsum(b), 0, 0)

ans = 0
j =  len(cost_b) -1
for i in range(len(cost_a)):
    for j in range(j, -1, -1):
        if cost_a[i] + cost_b[j] <= k:
            ans  = i + j if ans < i + j else ans
            break
print(ans)

Submission status: Accepted

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

    ans, j = 0, M
    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])
    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 = tuple(map(int, input().split()))
B = tuple(map(int, input().split()))
from itertools import accumulate
AS = [0] + list(accumulate(A))
BS = [0] + list(accumulate(B))
 
ans = 0
j = m
for i in range(n+1):
    if AS[i] > k:
        break
 
    while AS[i] + BS[j] > k:
        j -= 1
    ans = max(ans, i+j)
        
print(ans)

Submission status: Accepted

Solution code:
import numpy as np


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

cumA = np.cumsum(A)
cumB = np.cumsum(B)

cumA = cumA[cumA <= k]
cumB = cumB[cumB <= k]

candidates = np.arange(len(cumA)) + np.searchsorted(cumB, k-cumA, side="right")
print(candidates.max())

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_acc=list(accumulate(a))
b_acc=list(accumulate(b))
a_acc.insert(0,0)
b_acc.insert(0,0)

ans=0
j=m

for i in range(n+1):
    nokori=k-a_acc[i]
    if nokori<0:
        break
    while b_acc[j]>nokori:
        j-=1
    ans=max(ans,i+j)
    # print(ans, i, j)

print(ans)

Submission status: Accepted

Solution code:
import sys
from itertools import accumulate
from bisect import bisect_right

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, M, K, *AB = map(int, read().split())
    A = AB[:N]
    B = AB[N:]

    time_A = [0]
    time_A.extend(accumulate(A))

    time_B = [0]
    time_B.extend(accumulate(B))

    ans = 0
    j = M
    for i, ta in enumerate(time_A):
        while j > 0 and ta + time_B[j] > K:
            j -= 1

        if ta + time_B[j] > K:
            break

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

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import itertools as it
def main():
    n,m,k=map(int,input().split())
    a = [0]
    a.extend(it.accumulate(map(int, input().split())))
    b = [0]
    b.extend(it.accumulate(map(int, input().split())))
    
    ans = 0
    M = m
    for A in range(n+1):
        aa = a[A]
        Kaa = k-aa
        if Kaa < 0:
            break
        for B in range(M,-1,-1):
            if b[B] <= Kaa:
                ans = max(ans, A+B)
                M = B
                break
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
NMK=input().split(" ")
N=int(NMK[0])
M=int(NMK[1])
K=int(NMK[2])

def list2int(List):
    out=[]
    for i in range(len(List)):
        out.append(int(List[i]))
    return out

A=list2int(input().split(" "))
B=list2int(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])   

result=0
j=M

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

print(result)

Submission status: Accepted

Solution code:
from bisect import bisect_right
from itertools import accumulate

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

for n in range(1+N):
  C = bisect_right(B,K-A[n])-1
  if C!=-1:
    ans.append(C+n)

print(max(ans))

Submission status: Accepted

Solution code:
from itertools import accumulate

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

# 累積和
a = [0] + list(accumulate(A))
b = [0] + list(accumulate(B))

ans = 0
j = M
for i in range(N + 1):
    # Aで既にKを超えていたらBから取れない
    if a[i] > K:
        break
    # 選択可能な最大のjを探す
    # 最大のjは前回のj以下
    while b[j] > K - a[i]:
        j -= 1
    ans = max(ans, i + j)

print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
n, M, k = map(int,input().split())
a_l = list(map(int,input().split()))
b_l = list(map(int,input().split()))
read_a = [0]*n
read_b = [0]*M
time = 0
ans = 0
memo = -1
for j in range(n):
    if time + a_l[j] <= k:
        time += a_l[j]
        read_a[j] = 1
        memo = j
        ans = j+1
    else:
        break

kotae = [ans]


def dfs(i,a,t,m):
    global kotae
    if i < M:
        if b_l[i] + t <= k:
            tt = t + b_l[i]
            aa = a + 1
            kotae.append(aa)
            ii = i+1
            mm = m
            #print(aa)
            dfs(ii,aa,tt,mm)
            
        else:
            if m >= 0:
                tt = t - a_l[m]
                mm = m - 1
                aa = a - 1
                ii = i
                dfs(ii,aa,tt,mm)
            


dfs(0,ans,time,memo)


print(max(kotae))

Submission status: Accepted

Solution code:
import sys, os, math, bisect, itertools, collections, heapq, queue
# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from decimal import Decimal
from collections import defaultdict, deque

# import fractions

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)
# lcm = lambda x, y: (x * y) // fractions.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N, M, K = il()
    A = list(itertools.accumulate([0] + il()))
    B = list(itertools.accumulate(([0] + il())))

    ret, m = 0, M
    for n in range(N+1):
        if A[n] > K:
            break

        while B[m] > K - A[n]:
            m -= 1
        ret = max(ret, n+m)
    print(ret)




if __name__ == '__main__':
    main()

Submission status: Accepted

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

sum_alist=[0]
for a in alist:
  sum_alist.append(sum_alist[-1]+a)
sum_blist=[0]
for b in blist:
  sum_blist.append(sum_blist[-1]+b)
  
index_b=M
answer=0
for i in range(N+1):
  sum_a=sum_alist[i]
  if sum_a>K:
    break
    
  while sum_a+sum_blist[index_b]>K:
    index_b-=1
    
  #print(i,index_b)
  answer=max(answer,i+index_b)
  
print(answer)

Submission status: Accepted

Solution code:
import bisect

n, m, k = map(int, input().split())
INF = 10**18
A = list(map(int, input().split()))
B = list(map(int, input().split()))
cum_a = [0] * (n+1)
cum_b = [0] * (m+1)
for i in range(n):
    cum_a[i+1] = cum_a[i] + A[i]
for i in range(m):
    cum_b[i+1] = cum_b[i] + B[i]
INF = 10**18
ans = -INF
for i in range(n+1):
    mod = k - cum_a[i]
    b = bisect.bisect(cum_b, mod)
    if b == 0:
        ans = max(ans, i)
        break
    ans = max(ans, b+i)
ans -= 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:
from itertools import accumulate
from bisect import bisect_right

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

acca = [0] + list(accumulate(a))
accb = list(accumulate(b))
ans = 0

for i in range(n + 1):
    if k < acca[i]:
        break
    t = k - acca[i]
    ans = max(ans, bisect_right(accb, t) + i)
print(ans)

Submission status: Accepted

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

read=bisect(a,k)
time=a[read-1]
ans=read

for i in range(read):
    extra=k-time
    r=bisect(b,extra)
    ans=max(ans,read+r)
    read-=1
    time=a[read-1]
read=bisect(b,k)
ans=max(ans,read)
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)

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から走査すれば良い（毎回Mから始める必要はない）
        j -= 1
    ans = max(ans, i + j)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools as it
def main():
    n,m,k=map(int,input().split())
    Aa = list(map(int,input().split()))
    Bb = list(map(int, input().split()))
    a, b = [0],[0]
    for i in range(n):
        a.append(a[i] + Aa[i])
    for i in range(m):
        b.append(b[i] + Bb[i])
    M = m
    ans = 0
    for A in range(n+1):
        aa = a[A]
        Kaa = k-aa
        if Kaa < 0:
            break
        for B in range(M,-1,-1):
            if b[B] <= Kaa:
                ans = max(ans, A+B)
                M = B
                break
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import bisect

n, m, k = map(int, input().split())
INF = 10**18
A = list(map(int, input().split()))
B = list(map(int, input().split()))
cum_a = [0] * (n+1)
cum_b = [0] * (m+1)
for i in range(n):
    cum_a[i+1] = cum_a[i] + A[i]
for i in range(m):
    cum_b[i+1] = cum_b[i] + B[i]
INF = 10**18
ans = -INF
for i in range(n+1):
    mod = k - cum_a[i]
    b = bisect.bisect(cum_b, mod)
    if b == 0:
        ans = max(ans, i)
        break
    ans = max(ans, b+i)
print(ans-1)

Submission status: Accepted

Solution code:
from itertools import accumulate

N, M, K = map(int, input().split())
*A, = map(int, input().split())
*B, = map(int, input().split())
cumsum_A, cumsum_B = [0] + list(accumulate(A)), [0] + list(accumulate(B))
ans = 0
j = M
for i in range(N+1):
    if cumsum_A[i] > K: break
    while cumsum_A[i] + cumsum_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()))

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

#片方全部見ていくともう片方が簡単に求まる
ans = 0
for i in range(n+1):
    if sa[i] > k:
        break
    while sb[m] > k - sa[i]:
        m -= 1
    ans = max(ans,i+m)
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:
#!/usr/bin/env python3
# input
import sys
import bisect

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
cum_a = [0] * (1 + n)
cum_b = [0] * (1 + m)
for i in range(1, n + 1):
    cum_a[i] = cum_a[i - 1] + a[i - 1]
for i in range(1, m + 1):
    cum_b[i] = cum_b[i - 1] + b[i - 1]
# cum_a で k 以下のものを全探索する
ans = 0  # INF
for i in range(n + 1):
    if cum_a[i] > k:
        break  # owari
    ans = max(ans, -1 + i + bisect.bisect_right(cum_b, k - cum_a[i]))
if ans != n + m + 1:
    print(ans)
else:
    print(0)

Submission status: Accepted

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

n,m,k = map(int,input().split())
a =[0] + list(map(int,input().split())) 
b =[0] + list(map(int,input().split()))

if min(a[1],b[1]) > k:
    print(0)
    exit()

asum = [a[0]]
bsum = [b[0]]

for i in range(1,n+1):
    ain = asum[-1]+a[i]
    asum.append(ain)
for i in range(1,m+1):
    bsum.append(bsum[-1]+b[i])

for i in range(n+1):
    if asum[i] > k:
        aind = i-1
        break
else:
    aind = n

for i in range(m+1):
    if bsum[i] > k:
        bind = i-1
        break
else:
    bind = m
# print(aind,bind)
while asum[aind] + bsum[bind] > k:
    bind -= 1
    if bind == 0:
        break



ans = aind + bind
# print(ans,aind,bind)
for i in range(aind-1,-1,-1):
    while asum[i] + bsum[bind] <= k:
        bind += 1
        if bind == m+1:
            bind -= 1
            ans = max(ans,i+bind)
            break
    else:
        bind -= 1
        ans = max(ans,i+bind)

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()))
 
ai = [0]
bi = [0]
 
for i in range(n):
    ai.append(ai[i] + a[i])
for i in range(m):
    bi.append(bi[i] + b[i])

ans, j = 0, m
for i in range(n+1):
    if ai[i] > k:
        break
    while bi[j] > k - ai[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])
# print(a)
# print(b)

ans, j = 0, m

# 
for i in range(n+1):
    if a[i] > k:
        break

    # bのパイルから何冊読めない？
    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()))

Sa = [0]*(N+1)
Sb = [0]*(M+1)
Sa[0] = 0
Sb[0] = 0
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
for i in range(N+1):
    if Sa[i] > K:
        break
    for j in range(M, -1, -1):
        if Sa[i]+Sb[j] <= K:
            ans = max(ans, i+j)
            M = j
            break
    else:
        ans = max(ans, i)
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
import numpy as np


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

    sumA = np.hstack([0,np.cumsum(A)])
    sumB = np.hstack([0,np.cumsum(B)])
    
    #print(sumA)
    #print(sumB)

    books = 0
    j = M
    for i in range(N+1):
        if sumA[i] > K:
            break
        while sumB[j] > K - sumA[i]:
            j -= 1
        books = max(books, i+j)

    print(books)
    return


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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    import bisect
    import sys
    input=sys.stdin.readline
    n,m,k=map(int,input().split())
    A=[0]+list(map(int,input().split()))
    B=list(map(int,input().split()))
    for i in range(n):
        A[i+1]+=A[i]
    for i in range(m-1):
        B[i+1]+=B[i]
    ans=0
    for i in range(n+1):
        if k<A[i]:
            break
        r=k-A[i]
        j=bisect.bisect_right(B,r)
        ans_=i+j
        ans=max(ans,ans_)
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline
from bisect import bisect_right

def main():
    N, M, K = map(int, readline().rstrip().split())
    A = list(map(int, readline().rstrip().split()))
    B = list(map(int, readline().rstrip().split()))

    a_acc, b_acc = A[:], B[:]
    for i in range(1, N):
        a_acc[i] += a_acc[i-1]
    
    for i in range(1, M):
        b_acc[i] += b_acc[i-1]
    
    res = 0
    for i, a in enumerate(a_acc):
        if a > K:
            break
        left = K - a
        j = bisect_right(b_acc, left)
        res = max(res, i + 1 + j)
    
    res = max(res, bisect_right(a_acc, K))
    res = max(res, bisect_right(b_acc, K))

    print(res)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque
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:
def resolve():
    import numpy as np

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

    A = np.insert(np.cumsum(A), 0, 0)
    B = np.insert(np.cumsum(B), 0, 0)

    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)

resolve()

Submission status: Accepted

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

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

S_A = [0] * (N + 1)
S_B = [0] * (M + 1)
for i in range(N):
  S_A[i+1] = S_A[i] + A[i]

for i in range(M):
  S_B[i+1] = S_B[i] + B[i]
  
ans = 0
a = 0
for i in range(M, -1, -1):
  s = S_B[i]
  if s>K:
    continue
  while a<N:
    if s + S_A[a + 1]<=K:
      a += 1
    else:
      break
  ans = max(ans, i + a)
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 = 0
j = m
for i in range(0, 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:
# -*- coding: utf-8 -*-

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


new_A = [0]
pre = 0
for a in A:
    if a > K:
        break
    pre += a
    if pre > K:
        break
    new_A.append(pre)

new_B = [0]
pre = 0
for b in B:
    if b > K:
        break
    pre += b
    if pre > K:
        break
    new_B.append(pre)

ans = 0
j = len(new_B) - 1

for i, a in enumerate(new_A):
    while new_B[j] > K - a:
        j -= 1
    ans = max(ans, i+j)

print(ans)

Submission status: Accepted

Solution code:
if __name__ == "__main__":
    (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(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 itertools import accumulate

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


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)