Problem:


Score : 300 points

Problem Statement

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

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

Consider the following action:

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

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

Constraints

1 \leq N, M \leq 200000

1 \leq K \leq 10^9

1 \leq A_i, B_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

3 4 240
60 90 120
80 150 80 150

Sample Output 1

3

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

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

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

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

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

Sample Input 2

3 4 730
60 90 120
80 150 80 150

Sample Output 2

7

Sample Input 3

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

Sample Output 3

0

Watch out for integer overflows.



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


Sample output:
3

Submission status: Accepted

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

import sys
sys.setrecursionlimit(300000)


def solve(N: int, M: int, K: int, A: "List[int]", B: "List[int]"):
    a, b = [0], [0]
    for v in A:
        a.append(a[-1] + v)
    for v in B:
        b.append(b[-1] + v)

    j = M
    ret = 0
    #print(a)
    #print(b)
    for i, v in enumerate(a):
        while j > 0 and v + b[j] > K:
            j -= 1
        if v + b[j] <= K:
            #print(i, v, j, b[j])
            ret = max(ret, i + j)
    print(ret)
    return

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:
N,M,K=map(int,input().split())

array=list(map(int,input().split()))
A=[0]
pre=0
for i in array:
    pre+=i
    A.append(pre)
    
array=list(map(int,input().split()))
B=[0]
pre=0
for i in array:
    pre+=i
    B.append(pre)

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:
#(0)環境
from itertools import accumulate
import bisect
n, m, k = map(int,input().split())
#[0]足すことで上から０冊読んだという要素として加えると実装が楽に(一冊も読めんかった)
#accumulate:累積和などが求められる
a = [0] + list(accumulate(map(int,input().split())))
b = [0] + list(accumulate(map(int,input().split())))
ans =[0]

for i in range(n + 1):
    c = bisect.bisect_right(b, k - a[i]) - 1
    #k-s<0のときはインデックスがー１なので除外しなあかん
    if c != -1:
        ans.append(c + i)
print(max(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 = [0]*(N+1)
b = [0]*(M+1)
for n in range(N):
    a[n+1] = a[n] + A[n]
for n in range(M):
    b[n+1] = b[n] + B[n]
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,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, N, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

ruisekiA = [0]
ruisekiB = [0]
tmpA=0
tmpB=0
for i in range(len(A)):
    tmpA += A[i]
    ruisekiA.append(tmpA)
for i in range(len(B)):
    tmpB += B[i]
    ruisekiB.append(tmpB)
ans = 0
for i in range(len(ruisekiA)):
    #tmp = ruisekiA[i]
    l = 0
    r = len(ruisekiB)
    if ruisekiA[i] > K:
        continue
    while r - l > 1:
        mid = (r + l)//2
        tmp = ruisekiA[i]+ruisekiB[mid]
        if tmp <= K:
            l = mid
        else:
            r = mid

    ans = max(ans, i+l)
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] + a[i]
for i in range(1, M+1):
    b[i] = b[i-1] + b[i]

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import itertools
import random
from decimal import *

input = sys.stdin.readline

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
def inputStr(): return input()[:-1]

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

def main():
	N,M,K = inputMap()
	A = inputList()
	B = inputList()

	sumA = []
	for i,val in enumerate(A):
		if i == 0:
			sumA.append(val)
		else:
			sumA.append(val + sumA[i-1])

	sumB = []
	for i,val in enumerate(B):
		if i == 0:
			sumB.append(val)
		else:
			sumB.append(val + sumB[i-1])

	ans = 0
	for i,val in enumerate(sumA):
		if val > K:
			break
		amari = K - val
		indexs = bisect_right(sumB, amari)
		tmp = (i+1) + indexs
		#print(tmp)
		if ans < tmp:
			ans = tmp

	for i,val in enumerate(sumB):
		if val > K:
			break
		amari = K - val
		indexs = bisect_right(sumA, amari)
		tmp = (i+1) + indexs
		#print(tmp)
		if ans < tmp:
			ans = tmp

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

ptr = m
ans = 0

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

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

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

Submission status: Accepted

Solution code:
from bisect import bisect_left
N, M ,K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
SA =[A[0]]
for i in A[1:]:
    SA.append(SA[-1] + i)
SB =[0]
for i in B:
    SB.append(SB[-1] + i)

i = bisect_left(SA, K)
SA = SA[: i+1]
SA.append(2*10**9)
i = bisect_left(SB, K)
SB = SB[: i+1]
if SB[-1] > K: SB.pop()

i, ii = 0, 0
j = len(SB) - 1
ans = [j]
while j >= 0:
    diff = K - SB[j]
    jdg = False
    for ii in range(i, len(SA)):
        if SA[ii] > diff:
            jdg = True
            break
    i = ii
    ans.append(i + j)
    j -= 1
print(max(ans))

Submission status: Accepted

Solution code:
from sys import stdin,stdout
import bisect as bs
n,m,s=map(int, stdin.readline().split())
dn=list(map(int, stdin.readline().split()))
dm=list(map(int, stdin.readline().split()))
pre=[dm[0]];s1=0
for v in dm[1:]:
    pre+=[pre[-1]+v]
m=bs.bisect_right(pre,s)
for i in range(n):
    s1+=dn[i]
    if s-s1>=0:m=max(m,i+1+bs.bisect_right(pre,s-s1))
print(m)

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 Cum(X):
    temp = 0
    Cum = [0]*len(X)
    for i in range(len(X)):
        temp += X[i]
        Cum[i] += temp
    return Cum

a = Cum(A)
a.insert(0,0)
b = Cum(B)
b.insert(0,0)

na = 0
for i in range(N+1):
    if i == N and a[i]<=K:
        na = N
    elif a[i]<=K and a[i+1]>K:
        na = i

ans = na

nb = 0
for i in range(na,-1,-1):
    temp = K-a[i]
    for j in range(nb,M+1):
        if j == M and b[j] <= temp:
            nb = M
            ans = max(ans,i+nb)
        elif b[j]<=temp and b[j+1]>temp:
            nb = j
            ans = max(ans,i+nb)
            break
        
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(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, 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,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 = j-1
    Ans = max(Ans,i+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()))


i = j = 0
t = 0
while i < N and t + A[i] <= K:
	t += A[i]
	i += 1

best = i

while True:
	while j < M and t + B[j] <= K:
		t += B[j]
		j += 1

	best = max(best, i + j)

	if not i: break

	i -= 1
	t -= A[i]

print(best)

Submission status: Accepted

Solution code:
import itertools
import functools
import math
from collections import Counter
from itertools import combinations
import re



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

    A_len = len(A)
    B_len = len(B)

    Asum = [0] * (A_len + 1)
    Bsum = [0] * (B_len + 1)

    maxA = len(A) + 1
    maxB = len(B) + 1

    add = 0
    for i in range(1,A_len+1):
        Asum[i] = A[i-1] + Asum[i-1]

    add = 0
    for i in range(1,B_len+1):
        Bsum[i] = B[i-1] + Bsum[i-1]

    ans = 0
    prev = 0
    for i in reversed(range(maxA)):
        for j in range(prev,maxB):
            if Asum[i] + Bsum[j] <= K:
                prev = j
                if ans < i + j:
                    ans = i + j
                if j == (maxB - 1):
                    print(ans)
                    exit()
            else:
                break

    print(ans)




main_sum()

Submission status: Accepted

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

A = cumsum([0] + A)
B = cumsum([0] + B)
n_read = 0
l = len(B) - 1
for i, a in enumerate(A):
    if a > k:
        break
    for j, b in enumerate(B[l::-1]):
        if a + b <= k:
            l -= j
            n_read = max(n_read, i + l)
            break
print(n_read)

Submission status: Accepted

Solution code:
from itertools import chain


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

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

    answer = 0

    sum_b = sum(B)
    idx_b = M
    remain = K

    for i, a in enumerate(chain([0], A)):
        remain -= a
        if remain < 0:
            break

        while sum_b > remain and idx_b >= 0:
            idx_b -= 1
            b = B[idx_b]
            sum_b -= b

        i += idx_b
        answer = i if answer < i else answer

    return answer


def main():
    print(solve())


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from itertools import chain


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

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

    answer = 0

    sum_b = sum(B)
    idx_b = M
    remain = K

    for i, a in enumerate(chain([0], A)):
        remain -= a
        if remain < 0:
            break

        while sum_b > remain and idx_b >= 0:
            idx_b -= 1
            b = B[idx_b]
            sum_b -= b
        
        i += idx_b
        answer = i if answer < i else answer

    return answer


def main():
    print(solve())


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def main():
    from itertools import chain
    N, M, K = map(int, input().split())

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

    answer = 0

    sum_b = sum(B)
    idx_b = M
    remain = K

    for i, a in enumerate(chain([0], A)):
        remain -= a
        if remain < 0:
            break

        while sum_b > remain and idx_b >= 0:
            idx_b -= 1
            b = B[idx_b]
            sum_b -= b

        i += idx_b
        answer = i if answer < i else answer

    print(answer)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from itertools import chain


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

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

    answer = 0

    sum_b = sum(B)
    idx_b = M
    remain = K

    for i, a in enumerate(chain([0], A)):
        remain -= a
        if remain < 0:
            break

        while sum_b > remain and idx_b >= 0:
            idx_b -= 1
            b = B[idx_b]
            sum_b -= b

        i += idx_b
        answer = i if answer < i else answer

    print(answer)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from itertools import chain


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

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

    answer = 0

    sum_b = sum(B)
    idx_b = M
    remain = K

    for i, a in enumerate(chain([0], A)):
        remain -= a
        if remain < 0:
            break

        while sum_b > remain:
            if idx_b < 0:
                break
            idx_b -= 1
            b = B[idx_b]
            sum_b -= b

        answer = max(answer, i + idx_b)

    return answer


def main():
    print(solve())


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import bisect

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

# print(N, M, K)
# print(A)
# print(B)

def cuml(li):
    rlis = [0]

    for i in range(len(li)):
        rlis.append(rlis[-1] + li[i])

    return rlis


A_cuml = cuml(A)
B_cuml = cuml(B)

res = 0

for a in range(len(A_cuml)):
    rest = K - A_cuml[a]
    if rest < 0:
        break
    # for b in range(len(B_cuml)):
    #     if A_cuml[a] + B_cuml[b] > K:
    #         break

    # bisect search
    b = bisect.bisect_right(B_cuml, K - A_cuml[a])

    res = max(res, a + b - 1)

print(res)

Submission status: Accepted

Solution code:
from itertools import chain


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

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

    answer = 0

    sum_b = sum(B)
    idx_b = M
    remain = K

    for i, a in enumerate(chain([0], A)):
        remain -= a
        if remain < 0:
            break

        while sum_b > remain:
            if idx_b < 0:
                break
            idx_b -= 1
            b = B[idx_b]
            sum_b -= b

        answer = max(answer, i + idx_b)

    return answer


def main():
    print(solve())


if __name__ == "__main__":
    main()

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())
varl = [0] + list(accumulate(a))
varl2 = [0] + list(accumulate(b))
ans, i = 0, n
for iterater in range(m+1):
    while varl[i]+varl2[iterater] > k and i>0:
        i -= 1
    if i==0 and varl2[iterater] > k:
        break
    ans = max(ans, i+iterater)
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 = sum(A)
i = N
ans = 0

for j in range(M + 1):
  while i > 0 and t > K:
    i -= 1
    t -= A[i]
  if t > K:
    break
  ans = max(ans, i + j)
  if j == M:
    break
  t += B[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]
for i in range(n):
    a.append(a[i]+a_[i])

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

j = m
ans = 0
for i in range(n+1):
    if a[i] > k:
        break
    while 1:#b[j] > k - a[i]:
        if b[j] <= k - a[i]:
            break
        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())
list_A = list(map(int, input().split()))
list_B = list(map(int, input().split()))

list_AS = []
list_BS = []
a = 0
b = 0
list_AS.append(0)
for i in list_A:
    a += i
    list_AS.append(a)
list_BS.append(0)
for i in list_B:
    b += i
    list_BS.append(b)
ans = 0
for i in range(n+1):
    j = bisect(list_BS, k-list_AS[i])
    if list_AS[i] + list_BS[j-1] <= k:
        ans = max(ans, i+j-1)

print(ans)

Submission status: Accepted

Solution code:
n,m,k=map(int,input().split())
al=[int(i) for i in input().split()]
bl=[int(i) for i in input().split()]
aa,bb=[0],[0]
for i in range(n):
    aa.append(aa[i]+al[i])
for i in range(m):
    bb.append(bb[i]+bl[i])
ans=0
j=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:
from bisect import bisect_right


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

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

    sum_as = [0] * (N+1)
    sum_bs = [0] * (M+1)

    for i, a in enumerate(A):
        sum_as[i+1] = sum_as[i] + a

    for i, b in enumerate(B):
        sum_bs[i+1] = sum_bs[i] + b
    sum_bs = sum_bs[1:]

    answer = 0
    for i, sa in enumerate(sum_as):
        remain = K - sa
        if remain < 0:
            continue
        
        idx = bisect_right(sum_bs, remain)
        answer = max(answer, i + idx)

    return answer


def main():
    print(solve())


if __name__ == "__main__":
    main()

Submission status: Accepted

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

A_sum = [0]
for i in range(N):
    A_sum.append(A_sum[i] + A[i])
B_sum = [0]
for i in range(M):
    B_sum.append(B_sum[i] + B[i])
B_sum.append(10**10)

res = 0
for i in range(len(A_sum)):
    low = 0
    high = len(B_sum)
    while high - low > 1:
        mid = (low + high) // 2
        if A_sum[i] + B_sum[mid] <= K:
            low = mid
        else:
            high = mid
    if A_sum[i] + B_sum[low] <= K:
        res = max(res, i + low)

print(res)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)
INF = float('inf')

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

A_sum = [0]
B_sum = [0]
for i in range(N):
    A_sum.append(A_sum[-1] + A[i])
for i in range(M):
    B_sum.append(B_sum[-1] + B[i])

ans = 0
j_max = M
for i in range(N+1):
    for j in range(j_max, -1, -1):
        time = A_sum[i] + B_sum[j]
        if time > K:
            continue
        j_max = j
        ans = max(i + j, ans)
        break
print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate
from bisect import bisect

N, M, K = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A_ = list(accumulate(A))
B_ = list(accumulate(B))
A_all = bisect(A_, K)
B_all = bisect(B_, K)
if B_all > A_all:
    A_, B_ = B_, A_
    A_all, B_all = B_all, A_all
ans = 0
for i in range(A_all):
    ans = max(ans, i+1 + bisect(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()))
arui = [0] * (N + 1)
brui = [0] * (M + 1)
for i in range(N):
    arui[i + 1] = a[i] + arui[i]
for i in range(M):
    brui[i + 1] = b[i] + brui[i]

ans = 0
bcnt = M
for i in range(N + 1):
    if arui[i] > K:
        break

    while brui[bcnt] + arui[i] > K:
        bcnt -= 1
    ans = max(ans, i + bcnt)

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()))
arui = [0] * (N + 1)
brui = [0] * (M + 1)
for i in range(N):
    arui[i + 1] = a[i] + arui[i]
for i in range(M):
    brui[i + 1] = b[i] + brui[i]


ans, j = 0, M
for i in range(N + 1):
    if arui[i] > K:
        break
    while brui[j] > K - arui[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, 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])

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

Submission status: Accepted

Solution code:
from bisect import bisect_right
from itertools import accumulate

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

A = [0] + list(accumulate(A))
B = [0] + list(accumulate(B))

ans = 0
for i, a in enumerate(A):
    if K - a < 0:
        break
    ans = max(ans, bisect_right(B, K - a) + i - 1)

print(ans)

Submission status: Accepted

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

dpA = [0]*(N+1)
dpB = [0]*(M+1)

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

for i in range(1,M+1):
    dpB[i] = dpB[i-1] + B[i-1]

ans = 0
for i in range(N+1):
    if dpA[i] > K:
        continue
    indexB = bisect.bisect_right(dpB, K - dpA[i])
    ans = max(ans, i + indexB - 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 = [0]
B = [0]
for i in range(n):
	A.append(A[i]+a[i])
for j in range(m):
	B.append(B[j]+b[j])

#print(A,B)

maxij = 0
l = m
for i in range(n+1):
	for j in range(m):
#		print(i,l,A[i],B[l],k)
		if(l == -1):
			break
		elif((A[i]+B[l]) <= k):
		 	maxij=max(maxij,i+l)
#		 	print("mm",maxij)
		 	break
		else:
		 	l-=1
print(maxij)

Submission status: Accepted

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

ruisekiA = [0]
ruisekiB = [0]
tmpA=0
tmpB=0
for i in range(len(A)):
    tmpA += A[i]
    ruisekiA.append(tmpA)
for i in range(len(B)):
    tmpB += B[i]
    ruisekiB.append(tmpB)
ans = 0
for i in range(len(ruisekiA)):
    #tmp = ruisekiA[i]
    l = -1
    r = len(ruisekiB)
    while r - l > 1:
        mid = (r + l)//2
        tmp = ruisekiA[i]+ruisekiB[mid]
        if tmp <= K:
            l = mid
        else:
            r = mid
    if l == -1:
        continue
    ans = max(ans, i+l)
print(ans)

Submission status: Accepted

Solution code:
"""everything has a cost!"""

from math import *
import random, functools, collections, sys

__author__ = 'p7x'


# sys.stdin = open('in.txt', 'r')
# sys.stdout = open('out.txt', 'w')
# sys.stderr = open('err.txt', 'w')


if __name__ == '__main__':
    n,m,k = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))

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

    j = len(b)-1
    while j > -1 and b[j] > k:
        j -= 1
    ans = j+1
    i = len(a)-1
    while i > -1 and a[i] > k:
        i -= 1
    ans = max(i+1,ans)

    for inx in range(i+1):
        while j > -1 and a[inx] + b[j] > k:
            j -= 1
        ans = max(inx+j+2,ans)
    print(ans)

Submission status: Accepted

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

for i in range(n):
  c[i+1] = c[i]+a[i]
for i in range(m):
  d[i+1] = d[i]+b[i]
  
cnt = 0
  
for i in range(n+1):
  if c[i] > k:
    continue
  e = bisect.bisect_right(d, k-c[i])
  if cnt < i + e -1:
    cnt =  i + e -1
print(cnt)

Submission status: Accepted

Solution code:
# Begin Header {{{
from math import gcd
from collections import Counter, deque, defaultdict
from heapq import heappush, heappop, heappushpop, heapify, heapreplace, merge
from bisect import bisect_left, bisect_right, bisect, insort_left, insort_right, insort
from itertools import accumulate, product, permutations, combinations, combinations_with_replacement
# }}} End Header
# _________コーディングはここから！！___________

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 in range(n+1):
    c = bisect_right(b, k-a[i])-1
    if c!=-1: ans.append(c+i)
print(max(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()))

bs = 0
ans = 0

i = 0
while i < n:
    if a[i] <= k:
        k -= a[i]
        bs += 1
        ans = max(ans, bs)
    else:
        break
    i += 1
i -= 1

j = 0
while j < m:
    while i >= 0 and b[j] > k:
        k += a[i]
        bs -= 1
        i -= 1
    if b[j] <= k:
        k -= b[j]
        bs += 1
        ans = max(ans, bs)
    else:
        break
    j += 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 = [0]
b = [0]

for i in range(n):
    a.append(A[i] + a[-1])
for i in range(m):
    b.append(B[i] + b[-1])

best = 0
ans = 0

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

       if j == m:
           book = i + j
           ans = max(book, ans)
           best = j - 1
           break


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()))
if K >= sum(A) + sum(B):
    print(M + N)
    exit()
acc = 0
ans = 0
for j, b in enumerate(B):
    acc += b
    if K >= acc:
        ans = j + 1
for i in range(N):
    acc += A[i]
    if K >= acc:
        ans = max(ans, i + j + 2)
        continue
    else:
        while (K < acc) & (j > -1):
            acc -= B[j]
            j -= 1
        if K >= acc:
            ans = max(ans, i + j + 2)
print(ans)

Submission status: Accepted

Solution code:
N,M,K = map(int,input().split())
a=list(map(int, input().split()))
b=list(map(int, input().split()))
a.insert(0,0)
b.insert(0,0)
b_num = sum(b)
b_best = M
j = M
a_num = 0
maxmize = 0
for i in range(N+1):
  #print(i)
  X = 0
  a_num += a[i]
  if a_num>K:
    break
  X +=a_num
  handan = True
  j = b_best
  while handan ==True:
    #print('max',maxmize)
    X +=b_num
    #print(X,K)
    if X<=K:
      b_best = j
      maxmize = max(i+j,maxmize)
      handan=False
    else:
      X -=b_num
      b_num -=b[j]
      j-=1
    #print('max1',maxmize)
    
print(maxmize)

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の累積和を取る
sumA = [0]*(n+1)
sumB = [0]*(m+1)
for i in range(n):
    sumA[i+1] = sumA[i] + A[i]
for i in range(m):
    sumB[i+1] = sumB[i] + B[i]

ans = 0
# しゃくとり法
j = m
for i,a in enumerate(sumA):
    while j >= 0 and a + sumB[j] > k:
        j -= 1
    if j < 0:break
    ans = max(ans,i+j)
print(ans)