Problem:


Score : 300 points

Problem Statement

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

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

Consider the following action:

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

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

Constraints

1 \leq N, M \leq 200000

1 \leq K \leq 10^9

1 \leq A_i, B_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

3 4 240
60 90 120
80 150 80 150

Sample Output 1

3

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

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

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

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

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

Sample Input 2

3 4 730
60 90 120
80 150 80 150

Sample Output 2

7

Sample Input 3

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

Sample Output 3

0

Watch out for integer overflows.



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


Sample output:
3

Submission status: Accepted

Solution code:
from collections import deque
import numpy as np
n,m,k = map(int,input().split())
A = deque(map(int,input().split()))
B = deque(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])

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

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 for _ in range(N+1)]
for i in range(N):
    CA[i+1] = A[i]+CA[i]
CB = [0 for _ in range(M+1)]
for j in range(M):
    CB[j+1] = B[j]+CB[j]
cmax = 0
for i in range(N,-1,-1):
    if CA[i]<=K:
        b = K-CA[i]
        ind = bisect_right(CB,b)
        cmax = max(cmax,i+ind-1)
print(cmax)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

from itertools import accumulate
N, M, K = mapint()
As = [0]+list(accumulate(list(mapint())))
Bs = list(accumulate(list(mapint())))
from bisect import bisect_right

ans = 0
for i, a in enumerate(As):
    if a>K:
        break
    idx = bisect_right(Bs, K-a)
    ans = max(ans, i+idx)
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
import bisect
n,m,k = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
a = [0]+list(np.cumsum(A))
b = [0]+list(np.cumsum(B))
ans = 0
for i in range(len(a)):
    x = k-a[i]
    if x < 0: break
    I = bisect.bisect_left(b, x)
    if I >= len(b): I -= 1
    if b[I] > x:
        if I != 0: I -= 1
        else: continue
    ans = max(ans, i+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()))
from bisect import bisect_right
sa=[0]*(n+1)
sb=[0]*(m+1)
for i in range(n):
  sa[i+1]=sa[i]+a[i]
# print(sa)
for i in range(m):
  sb[i+1]=sb[i]+b[i]
# print(sb)
ans=0
for i in range(n+1):
  t=k-sa[i]
  if t<0:continue
  idx=bisect_right(sb,t)
  ans=max(ans,idx+i-1)
print(ans)

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()))
SA = [0]
for i in range(N):
  temp = SA[-1] + A[i]
  SA.append(temp)
 
SB = [0]
for j in range(M):
  temp = SB[-1] + B[j]
  SB.append(temp)
ans = 0
for i in range(N+1):
  an = i
  if SA[i] > K:
    break
  else:
    nokori = K -SA[i]
    if nokori >= SB[-1]:
      bn = M
    else:
      ok = 0
      ng = M
      while abs(ok-ng) > 1:
        mid = (ok+ng)//2
        if nokori >= SB[mid]:
          ok = mid
        else:
          ng = mid
      bn = ok
  #print(an,bn)
  ans = max(ans,an+bn)
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()))
asum=[0]
bsum=[0]
current=0
for i in range(n):
  current+=a[i]
  asum.append(current)
current=0
for i in range(m):
  current+=b[i]
  bsum.append(current)
pointer=0
for i in range(n+1):
  if asum[i]>k:
    pointer=i-1
    break
  if i==n:
    pointer=n
ans=pointer
bpointer=0
for i in range(pointer,-1,-1):
  while bpointer<m and bsum[bpointer+1]+asum[i]<=k:
    bpointer+=1
  ans=max(ans,i+bpointer)
print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate
import bisect

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, a in enumerate(A):
  if a > k:
    break
  temp = i + bisect.bisect_right(B, k-a)-1
  ans = max(ans, temp)
print(ans)

Submission status: Accepted

Solution code:
from bisect import *
N,M,K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
C=[0]
for i in range(N):
  C.append(C[i]+A[i])
D=[0]
for i in range(M):
  D.append(D[i]+B[i])
P=0
for i in range(N+1):
  if C[i]>K:
    break
  P=max(P,i+bisect_right(D,K-C[i])-1)
print(P)

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:
"https://codeforces.com/contest/1328/problem/A"
import collections, heapq, itertools, math, functools
groupby = itertools.groupby
rs  = lambda: input()
ri  = lambda: int(input())
rm  = lambda: map(int, input().split())
rai = lambda: [int(x) for x in input().split()]

from functools import reduce

def solve():
    N, M, K = rm()
    A = rai()
    B = rai()
    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,b)

    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)
    return ans
print(solve())

Submission status: Accepted

Solution code:
import bisect

n, m, k = map(int, input().split())
a, b = [0], [0]
for i, x in enumerate(input().split()):
    a.append(a[i] + int(x))
for i, x in enumerate(input().split()):
    b.append(b[i] + int(x))

res = 0
a_ = 0
for i in range(n+1):
    if a[i] > k:
        break
    j = bisect.bisect(b, k-a[i]) - 1
    res = max(res, i+j)

print(res)

Submission status: Accepted

Solution code:
import bisect

n, m, k = map(int, input().split())
a = [0] + [int(x) for x in input().split()]
b = [0]
for x in input().split():
    b.append(b[-1] + int(x))

res = 0
a_ = 0
for i in range(n+1):
    a_ += a[i]
    if a_ > k:
        break
    b_max = k - a_
    b_num = bisect.bisect(b, b_max) - 1
    res = max(res, i + b_num)

print(res)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
a = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))

N = a[0]
M = a[1]
K = a[2]

count = 0 #読んだ冊数を記録
count_time = 0 #読んだ時間を記録
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 = [0]
B = [0]
for i in range(n):
    A.append(a[i] + A[i])
for i in range(m):
    B.append(b[i] + B[i])
ans = 0
j = m
for i in range(n+1):
    if A[i] > k:
        break
    while B[j] > k - A[i]:
        j -= 1
    ans = max(ans, i + j)
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
import bisect

def solve(n, m, k, a, b):
    s = np.cumsum([0]+a)
    t = np.cumsum(b)
    best = 0
    for i in range(n+1):
        if s[i] > k:
            break
        j = bisect.bisect_right(t, k - s[i])
        best = max(best, i+j)
    return best

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

Submission status: Accepted

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

c = [0]
d = [0]
for i in range(n):
    c.append(c[i] + a[i])
for i in range(m):
    d.append(d[i] + b[i])

ans = 0
j = m
for i in range(n + 1):
    if c[i] > k:
        break
    while c[i] + d[j] > k:
        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 = list(accumulate(A, initial=0))
accB = list(accumulate(B, initial=0))

ans = 0
for c, a in enumerate(accA):
    if K - a < 0:
        continue
    ans = max(ans, c + bisect_right(accB, K - a) - 1)
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
n, m, k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
cum_a = np.r_[0,np.cumsum(a)]
# cum_b = np.r_[0, np.cumsum(b)]
# cum_a = np.cumsum(a)
cum_b = np.cumsum(b)
# high = m
# low = 0
# totaltime = 0
ans = 0
for i in range(n + 1):
    # # totaltime = cum_a[i]
    # while 1:
    #     mid = (high + low) // 2
    #     print(i, low, mid, high)
    #     if cum_a[i] + cum_b[mid] >= k:
    #         high = mid
    #     else:
    #         low = mid - 1
    #     if low == high:
    #         break
    if cum_a[i] > k:
    #     ans = max(ans, i - 1)
    #     print(s, i, ans)
        # break
        i = 0
    # else:
    s = np.searchsorted(cum_b, k - cum_a[i], side = "right")

    ans = max(ans, i + s)
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()))


c = [0]
d = 0
for i in range(n):
    d += a[i]
    c.append(d)

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


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

print(ans)

Submission status: Accepted

Solution code:
import sys
from itertools import accumulate
import bisect
n, m, k = [int(i) for i in sys.stdin.readline().split()]
a_ls = [int(i) for i in sys.stdin.readline().split()]
b_ls = [int(i) for i in sys.stdin.readline().split()]
a_ls = [i for i in accumulate([0] + a_ls)]
b_ls = [i for i in accumulate([0] + b_ls)]
i = 0
best = 0
while i <= n and a_ls[i] <= k:
    res = i + bisect.bisect_right(b_ls, k - a_ls[i]) - 1
    best = max(best, res)
    i += 1
print(best)

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

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

curr_max = 0
for i in range(N + 1):
    curr = A_cumu[i]
    if curr > K:
        break
    j = bisect_right(B_cumu, K - curr) - 1
    curr_max = max(curr_max, i + j)

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

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 = 0
j = 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:
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) #a[0]=0だからbしか含まない形も考慮されてる
print(ans)

Submission status: Accepted

Solution code:
import math
import sys
import itertools
from math import gcd
from math import sqrt
from sys import stdin
def input() : return stdin.readline().rstrip()
def mips():
    return map(int,input().split())
def ii():
    return int(input())
sys.setrecursionlimit(10**9)

N,M,K = mips()
A = [i for i in mips()]
B = [i for i in mips()]
# N,M <= 200000
ans = 0
clock = K
A2 = [0]
B2 = [0]
for i in range(1,N+1):
    ai = A2[i-1] + A[i-1]
    A2.append(ai)
for i in range(1,M+1):
    bi = B2[i-1] + B[i-1]
    B2.append(bi)
j = M
for i in range(N+1):
    if A2[i] > K:
        break
    while (B2[j] + A2[i] > K):
        j -= 1
    ans = max(ans,i+j)
print(ans)

Submission status: Accepted

Solution code:
def main():
    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])
    bs = M
    ans = 0
    for i in range(N + 1):
        if a[i] > K:
            break
        for j in range(bs, -1, -1):
            if b[j] + a[i] <= K:
                bs = j
                break
        ans = max(ans, i + bs)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import bisect
from copy import copy,deepcopy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(map(int,input().split()))
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
INF = 10**9
mod = 10**9+7

N,M,K = I()
A = l()
B = l()
ruiseki = [-1]+list(itertools.accumulate(A))
num = 0
ans = bisect.bisect_right(ruiseki,K)-1
for i in range(M):
    num += B[i]
    if num > K:
        break
    ans = max(ans,bisect.bisect_right(ruiseki,K-num)+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()))

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


res = bisect.bisect_right(B,K)

for i in range(N):
    if K >= A[i]:
        j=bisect.bisect_right(B,K-A[i])
        res=max(res, i+1+j)

print(res)

Submission status: Accepted

Solution code:
N, M, K=map(int,input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
LA = [0]
LB = [0]
for i in range(N):
    C=LA[i]+A[i]
    if C>K:
        N=i
        break
    else:
        LA.append(C)
for j in range(M):
    C=LB[j]+B[j]
    if C>K:
        M=j
        break
    else:
        LB.append(C)
r=N
for i in range(N+1):
    u=0
    for j in range(r-N+i,M+1):
        if LA[N-i]+LB[j]>K:
            r=max(r,N-i+j-1) # 最大のrを求めたいので結果をrefineする場合だけで回す
            u=1
            break
    if u==0:
        r=max(r,N-i+M)
        break
print(r)

Submission status: Accepted

Solution code:
import itertools
n, m, k = map(int, input().split())
a = [0] + list(map(int, input().split()))
b = [0] + list(map(int, input().split()))

sa = list(itertools.accumulate(a))
sb = list(itertools.accumulate(b))

output = 0
j = m
for i in range(n + 1):
    if sa[i] > k:
        break
    while sa[i] + sb[j] > k:
        j -= 1
    if output < i + j:
        output = i + j
print(output)

Submission status: Accepted

Solution code:
from itertools import accumulate
import bisect

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

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


#累積和
a_acc =[0] + list(accumulate(A))
b_acc =list(accumulate(B))

ans = 0

for an in range(len(a_acc)):
    rem = K - a_acc[an]
    if rem < 0:
        break
    
    bn  = bisect.bisect_right(b_acc, rem)

    ans = max(ans, an+bn)
    
    
print(ans)

Submission status: Accepted

Solution code:
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()
from itertools import accumulate
A=list(accumulate(A))
B=list(accumulate(B))
ans=0
b_cnt=M
for a_cnt in range(N+1):
  if A[a_cnt]>K:
    break
  while A[a_cnt]+B[b_cnt]>K:
    b_cnt-=1
  ans=max(ans,a_cnt+b_cnt)
print(ans)

Submission status: Accepted

Solution code:
import bisect, numpy
N,M,K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
A=numpy.cumsum(A)
B=numpy.cumsum(B)
ans=bisect.bisect_right(B,K)
for i in range(N):
  if K-A[i]>=0:
    ans=max(ans,i+1+bisect.bisect_right(B,K-A[i]))
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()))

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

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

V = 0
ACount = bisect.bisect_right(A, K)
if ACount !=0:
    V = K- A[ACount-1]
else:
    V=K

AInitial = ACount

BCount = bisect.bisect_right(B, V)
final = ACount+BCount

for i in range (1, AInitial):
    V = K - A[AInitial-1-i]
    BCount = bisect.bisect_right(B, V)
    final = max(final, AInitial-i+BCount)

final = max(final, bisect.bisect_right(B,K))
print(final)

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]

best=0
j=M
for i in range(0,N+1):
  while j>0:
    if A[i]+B[j]<=K:
      break
    else: j-=1
  if A[i]+B[j]<=K:
    best=max(best,i+j)
  pass
print(best)

Submission status: Accepted

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

total = 0
idx_a = idx_b = 0
while idx_a < n and total + A[idx_a] <= k:
    total += A[idx_a]
    idx_a += 1
while idx_b < m and total + B[idx_b] <= k:
    total += B[idx_b]
    idx_b += 1

ans = idx_a + idx_b
while idx_a > 0:
    idx_a -= 1
    total -= A[idx_a]
    while idx_b < m and total + B[idx_b] <= k:
        total += B[idx_b]
        idx_b += 1
    ans = max(ans, idx_a + idx_b)
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(" ")))

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]

b_i = M
max_count = 0
for i in range(N+1):
  a = sumA[i]
  if a > K:
    continue
  if b_i>0:
    while a + sumB[b_i] > K:
      b_i -= 1
      if b_i==0:
        break
  max_count = max(max_count, i + b_i)
print(max_count)

Submission status: Accepted

Solution code:
# Problem C - Tsundoku

# input
N, M, K = map(int, input().split())
a_nums = list(map(int, input().split()))
b_nums = list(map(int, input().split()))

# initialization
a_sum_list = [0]*(N+1)
b_sum_list = [0]*(M+1)
for i in range(N):
    a_sum_list[i+1] = a_sum_list[i] + a_nums[i]
for i in range(M):
    b_sum_list[i+1] = b_sum_list[i] + b_nums[i]
max_count = 0
b_i = M

# count
for i in range(N+1):
    a_time = a_sum_list[i]

    # a check
    if a_time>K:
        continue

    if b_i>0:
        while a_time+b_sum_list[b_i]>K:
            b_i -= 1
            if b_i==0:
                break
    # update
    max_count = max(max_count, i + b_i)

# output
print(max_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()))

a = [0 for _ in range(N+1)]
b = [0 for _ in range(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]
  
num = M
ans = 0
for k in range(N+1):
  if a[k] > K:
    break
  while b[num] > K - a[k]:
    num -= 1
  ans = max(ans, k+num)
  
print(ans)

Submission status: Accepted

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

a_=[0]
b_=[0]
tmp = 0
for i in a:
    tmp += i
    a_.append(tmp)
tmp = 0
for i in b:
    tmp += i
    b_.append(tmp)

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

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

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

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

Submission status: Accepted

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


ans = 0
b_count = M
for a_count in range(N+1):
  if A[a_count] > K:
    continue

  while A[a_count] + B[b_count] > K:
    b_count -= 1

  ans = max(ans, a_count + b_count)
print(ans)

Submission status: Accepted

Solution code:
# /usr/bin/python3
# -*- coding: utf-8 -*-
from queue import Queue
from queue import LifoQueue as Stack
from math import sqrt, floor, ceil, log2, log10, pi
from fractions import gcd
from itertools import permutations, combinations
from operator import itemgetter
from functools import cmp_to_key


INF=1001001001
__MOD__=(10**9)+7
yn = 'YNeos'
judge = False
cnt = 0
ans = None


def lcm(a, b):
    return (a*b) // gcd(a,b)

def intinput():
    return int(input())

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

def lineinputs(func=intinput):
    datas = []
    while True:
        try:
            datas.append(func())
        except EOFError:
            break
    return datas

class ModInt():
    def __init__(self, x):
        self.__x = (x % __MOD__)
    def __add__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x + other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x + other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __radd__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((other.__x + self.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((other.__x + self.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")

    def __sub__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x - other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x - other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __rsub__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((other.__x - self.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((other.__x - self.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")

    def __mul__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x * other.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x * other.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __rmul__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((other.__x * self.__x)%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((other.__x * self.__x)%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")

    def __truediv__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((self.__x * other.__modinv())%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((self.__x * other.__modinv())%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")
    def __rtruediv__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__((other.__x * self.__modinv())%__MOD__))
        elif type(other) == ModInt:
            return self.__class__((other.__x * self.__modinv())%__MOD__)
        else:
            raise Exception("Not Int or Not ModInt")

    def __pow__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__(pow(self.__x, other.__x, __MOD__)))
        elif type(other) == ModInt:
            return self.__class__(pow(self.__x, other.__x, __MOD__))
        else:
            raise Exception("Not Int or Not ModInt")
    def __rpow__(self, other):
        if type(other) == int:
            other = self.__class__(other)
            return int(self.__class__(pow(other.__x, self.__x, __MOD__)))
        elif type(other) == ModInt:
            return self.__class__(pow(other.__x, self.__x, __MOD__))
        else:
            raise Exception("Not Int or Not ModInt")

    def __modinv(self, m=__MOD__):
        a = self.__x
        if a == 0:
            raise ZeroDivisionError()
        if gcd(a, m) != 1:
            raise Exception("%sの逆数は求まりません。" % a)
        b, u, v = m, 1, 0
        while b != 0:
            t = a//b;
            a -= t*b
            a, b = b, a
            u -= t * v
            u, v = v, u
        u %= m
        if u < 0:
            u += m
        return u
    def __int__(self):
        return self.__x
    def __str__(self):
        return str(self.__x)



if __name__ == '__main__':
    N, M, K = mulinputs()
    an = list(map(int, input().split()))
    bm = list(map(int, input().split()))

#     # i, j, la, lb, t = 0, 0, len(an), len(bm), 0
#     # l = max(la, lb)
#     # while True:
#     #     if i < la and j < lb:
#     #         a, b = an[i], bm[j]
#     #     elif i < la:
#     #         a, b = an[i], INF
#     #     elif j < lb:
#     #         a, b = INF, bm[j]
#     #     else:
#     #         break
#     #     c = 0
#     #     if a <= b:
#     #         c = a
#     #         i += 1
#     #     else:
#     #         c = b
#     #         j += 1
#     #     if t + c <= K:
#     #         t += c
#     #         cnt += 1
#     #     else:
#     #         break
#     # # t = 0
#     # # while len(an) != 0 or len(bm) != 0:
#     # #     a, b = INF, INF
#     # #     if len(an) > 0:
#     # #         a = an.pop()
#     # #     if len(bm) > 0:
#     # #         b = bm.pop()
#     # #     if t + min(a, b) > K:
#     # #         break
#     # #     if a >= b:
#     # #         t += a
#     # #         cnt += 1
#     # #         bm.append(b)
#     # #     else:
#     # #         t += b
#     # #         cnt += 1
#     # #         an.append(a)
# # 
#     # # 出力
#     # print(cnt)
# 
# 
#     la, lb = len(an), len(bm)
#     def dfs(i, j, t, c):
#         a = INF if i >= la else an[i]
#         b = INF if j >= lb else bm[j]
#         if t + min(a,b) > K:
#             return c
#         return max(dfs(i+1, j, t+a, c+1), dfs(i, j+1, t+b, c+1), c)
# 
#     print(dfs(0,0,0,0))


    # 復習
    # 参考：https://atcoder.jp/contests/abc172/submissions/14775432
    ans, aidx, bidx = 0, 0, 0
    # 何も考えずにaから、bから条件を満たす本を収集する
    while True:
        if aidx == len(an):
            break
        if ans+an[aidx] <= K:
            ans += an[aidx]
            aidx +=1
        else:
            break
    while True:
        if bidx == len(bm):
            break
        if ans + bm[bidx] <= K:
            ans += bm[bidx]
            bidx += 1
        else:
            break
    ansidx = aidx + bidx
    # 上で求めたものから、どれくらいaの本を少なくすればbの本を読めるか、を計算する
    while aidx > 0:
        aidx -=1
        ans -= an[aidx]
        while True:
            if bidx == len(bm):
                break
            if ans + bm[bidx] <= K:
                ans += bm[bidx]
                bidx += 1
            else:
                break
        ansidx = max(ansidx, aidx+bidx)
    print(ansidx)

Submission status: Accepted

Solution code:
N, M, K = map(int, input().split())
A = list(map(int, ('0 ' + input()).split()))
B = list(map(int, ('0 ' + 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]
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:
import sys
#DEBUG = True
DEBUG = False
if DEBUG:
    f=open("202006_5th/C_input.txt")
else:
    f=sys.stdin
#----
M,N,K=map(int,f.readline().split())
ans=0
__=N
M_shelf=list(map(int,f.readline().split()))
N_shelf=list(map(int,f.readline().split()))
M_partial=[]
N_partial=[]
M_partial.append(0)
N_partial.append(0)
#----
for _ in range(M):
    M_partial.append(M_partial[_]+M_shelf[_])
for _ in range(N):
    N_partial.append(N_partial[_]+N_shelf[_])
for _ in range(M+1):
    R=K-M_partial[_]
    if R<0:
        break
    while R<N_partial[__]:
        __-=1
    ans=max(ans,_+__)
print(ans)
f.close()

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)