Problem:


Score : 300 points

Problem Statement

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

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

Consider the following action:

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

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

Constraints

1 \leq N, M \leq 200000

1 \leq K \leq 10^9

1 \leq A_i, B_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

3 4 240
60 90 120
80 150 80 150

Sample Output 1

3

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

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

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

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

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

Sample Input 2

3 4 730
60 90 120
80 150 80 150

Sample Output 2

7

Sample Input 3

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

Sample Output 3

0

Watch out for integer overflows.



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


Sample output:
3

Submission status: Accepted

Solution code:
from bisect import *
def solve():
    ans = 0
    N, M, K = map(int, input().split())
    A = list(map(int, input().split()))+[float('inf')]
    B = list(map(int, input().split()))+[float('inf')]
    cumA = [0]*(N+1)
    cumB = [0]*(M+1)
    for i in range(N):
        cumA[i+1] = cumA[i]+A[i]
    for i in range(M):
        cumB[i+1] = cumB[i]+B[i]
    for i in range(N+1):
        rest = K-cumA[i]
        if rest<0:
            break
        ind = bisect_right(cumB,rest)
        ans = max(ans,i+ind-1)
    return ans
print(solve())

Submission status: Accepted

Solution code:
from itertools import accumulate
from bisect import bisect_right
 
R = lambda: map(int, input().split())
*_, k = R()
a = accumulate(R())
b = list(accumulate(R()))
r = bisect_right(b, k)
for i, x in enumerate(a, 1):
  if x > k:
    break
  r = max(r, i + bisect_right(b, k - x)) 
print(r)

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]
for i in A:
  if Asum[-1]+i>K:break
  Asum.append(Asum[-1]+i)
import numpy as np
Asum=np.array(Asum)
Bsum=[0]
for i in B:
  if Bsum[-1]+i>K:break
  Bsum.append(Bsum[-1]+i)
Bsum=np.array(Bsum)

maxval=len(Asum)-1

tmp=0
lenB=len(Bsum)
lenA=len(Asum)
for i in range(lenA):
  if lenA-1-i+lenB<maxval:break
  for j in range(lenB):
    j=tmp+j
    if Asum[-i-1]+Bsum[j]>K:
      maxval=max(maxval,len(Asum)-1-i+j-1)
      tmp=j
      break
    elif j==lenB-1:
      maxval=max(maxval,lenA-1-i+j)
      break
print(maxval)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
from itertools import accumulate
#import numpy as np

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

sum_A = list(accumulate(A))
sum_B = list(accumulate(B))


#sum_A = np.cumsum(A)
#sum_B = np.cumsum(B)

list_N = [0 for i in range(len(A)+1)]

pre_n = len(B)

for i in range(len(A)+1):
    if i == 0:
        num_B = 0
        for j in range(pre_n)[::-1]:
            if sum_B[j] <= K:
                num_B = j+1
                break
            else:
                continue
        #len(buf[buf<=K])
        pre_n = num_B
        list_N[i] = i + num_B
    else:
        if K>=sum_A[i-1]:
            num_B = 0
            for j in range(pre_n)[::-1]:
                if sum_B[j] <= (K-sum_A[i-1]):
                    num_B = j+1
                    break
                else:
                    continue
            #len(buf[buf<=(K-sum_A[i-1])])
            pre_n = num_B
            list_N[i] = i + num_B
            #print(K-sum_A[i-1])
            #print(i, num_B)
        else:
            list_N[i] = 0
    
#print(A)
#print(B)
#print(sum_A)
#print(sum_B)
#print(list_N)
print(max(list_N))

Submission status: Accepted

Solution code:
N,M,K = map(int,input().split())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
AA = [0]
BB = [0]
r = 0
for i in A:
    r += i
    AA.append(r)
r = 0
for i in B:
    r += i
    BB.append(r)
ans = 0
a = N
b = 0
while AA[a]+BB[b] > K:
    a -= 1
ans = a+b

while a >= 0:
    while b < M:
        if AA[a]+BB[b+1] <= K:
            b += 1
        else:
            break
    if ans < a+b:
        ans = a+b
    if b == M:
        break
    a -= 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()))

ans = 0
acounter = 0
bcounter = 0
ans_counter = 0

while True:
  if acounter == len(a):
    break
  if ans + a[acounter] <= k:
    ans += a[acounter]
    acounter += 1
  else:
    break
while True:
  if bcounter == len(b):
    break
  if ans+b[bcounter] <= k:
    ans += b[bcounter]
    bcounter += 1
  else:
    break
ans_counter = acounter + bcounter

while acounter > 0:
  acounter -= 1
  ans -= a[acounter]
  while True:
    if bcounter == len(b):
      break
    if ans + b[bcounter] <= k:
      ans += b[bcounter]
      bcounter += 1
    else:
      break
  
  ans_counter = max(ans_counter, bcounter+acounter)
print(ans_counter)

Submission status: Accepted

Solution code:
###==================================================
### import
import sys
### stdin
def input(): return sys.stdin.readline()
def iIn(): return int(input())
def iInM(): return map(int, input().split())
def iInLH(): return list(map(int, input().split()))
### setting
sys.setrecursionlimit(10 ** 6)
###---------------------------------------------------

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

id_a = -1
t_pre = 0
for i in range(N):
    if t_pre + A[i] <= K: ## read
        id_a += 1
        t_pre += A[i]
    else:
        break

id_b = -1
for j in range(M):
    if t_pre + B[j] <= K: ## read
        id_b += 1
        t_pre += B[j]
    else:
        break

ans = (id_a + 1) + (id_b + 1)
if id_a < 0:
    print(ans)
    exit()

while (id_a >= 0):
    t_pre -= A[id_a]
    id_a -= 1
    for j in range(id_b+1, M):
        if t_pre + B[j] <= K:
            id_b += 1
            t_pre += B[j]
        else:
            break
    if ans < (id_a + 1) + (id_b + 1):
        ans = (id_a + 1) + (id_b + 1)
    #print(id_a, id_b)


print(ans)

Submission status: Accepted

Solution code:
def input_int():
    return map(int, input().split())

def one_int():
    return int(input())

def one_str():
    return input()

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

N, M, K = input_int()
A = many_int()
B = many_int()


import itertools as it
import bisect

def bins(A, B, K):
    A_it = list(it.accumulate(A))
    B_it = list(it.accumulate(B))

    A_index = bisect.bisect_right(A_it, K)
    B_index = bisect.bisect_right(B_it, K)

    A_it = [0]+A_it[:A_index]
    B_it = B_it[:B_index]

    maxs = 0
    count=0
    for i in A_it[::-1]:
        if K-i < 0:
            continue
        now = K-i
        B_index = bisect.bisect_right(B_it, now)

        maxs = max(maxs, B_index + max(A_index - count,0))
        count+=1

    return maxs

print(bins(A,B,K))

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_ruiseki = [0]
for i in range(n):
    if A_ruiseki[-1]+A[i] <= k :
        A_ruiseki.append(A_ruiseki[-1]+A[i])
    else:
        break

B_ruiseki = [0]
for i in range(m):
    if B_ruiseki[-1]+B[i] <= k :
        B_ruiseki.append(B_ruiseki[-1]+B[i])
    else:
        break

ans = len(A_ruiseki)-1

for i in range(len(A_ruiseki)):
    time = A_ruiseki[i]
    expected = max(ans-i, 0)

    if len(B_ruiseki) <= expected:
        continue

    if time + B_ruiseki[expected] > k:
        continue

    time += B_ruiseki[expected]

    for j in range(expected, len(B)):
        if time + B[j] > k:
            break

        time += B[j]
        ans =i+j+1

print(ans)

Submission status: Accepted

Solution code:
n, m, k = map(int, input().split())
aes = list(map(int, input().split()))
bs = list(map(int, input().split()))
r_aes = [0]*n
for i in range(1,n):
    aes[i] += aes[i-1]
for i in range(1,m):
    bs[i] += bs[i-1]
cnt_a = 0
for i in range(n):
    if aes[i] > k:
        break
    cnt_a += 1
cnt_b = 0
res = cnt_a
#cnt = 5
while True:
    #print("cnt_a: "+str(cnt_a))
    #print("cnt_b: " + str(cnt_b))
    if cnt_b >= m or bs[cnt_b] > k:
        break
    if cnt_a >= 1 and k-aes[cnt_a-1]>=bs[cnt_b]:
        cnt_b += 1
        res = max(res,cnt_a+cnt_b)
        continue
    if cnt_a < 1 and k>=bs[cnt_b]:
        cnt_b += 1
        res = max(res,cnt_b)
        continue
    cnt_a -= 1
    if cnt_a < -1:
        break
    #if cnt<= 0:
        #break
print(res)

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

    else:
        break
    


d=0 

for i in range(1,m):
    if b[i]+b[i-1]<=k:
        b[i]+=b[i-1]
        d=i

    else:
        break

#print(c,d)
if a[0]<=k:
    mx=c+1
else:
    mx=0
j=0
while c>=0 and j<=d:
    if a[c]+b[j]<=k:
        
        j+=1
        mx=max(mx,c+1+j)
        #print(c,j,mx)
    else:
        c-=1
if b[0]<=k:
    mx=max(mx,d+1)
print(mx)

Submission status: Accepted

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

ans = 0

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

print (ans)

Submission status: Accepted

Solution code:
# Template 1.0
import sys, re
from collections import deque, defaultdict, Counter, OrderedDict
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from heapq import heappush, heappop, heapify, nlargest, nsmallest
def STR(): return list(input())
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def list2d(a, b, c): return [[c] * b for i in range(a)]
def sortListWithIndex(listOfTuples, idx):   return (sorted(listOfTuples, key=lambda x: x[idx]))
def sortDictWithVal(passedDic):
    temp = sorted(passedDic.items(), key=lambda kv: (kv[1], kv[0]))
    toret = {}
    for tup in temp:
        toret[tup[0]] = tup[1]
    return toret
def sortDictWithKey(passedDic):
    return dict(OrderedDict(sorted(passedDic.items())))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

def solve(N,M,p1,p2):
    ans = 0

    for i in range(N):
        left = k - p1[i]

        lo = 0
        hi = M - 1

        temp = (i + 1)

        if (left >= 0):
            while (lo <= hi):
                # print(lo, hi, ans)
                mid = (lo + hi) // 2

                if (p2[mid] > left):
                    hi = mid - 1
                else:
                    lo = mid + 1
                    ans = max(ans, temp + lo)
            ans = max(ans, temp + lo)


    return ans


n,m,k = MAP()

a = LIST()

b = LIST()

prefA = [0]*n
prefA[0] = a[0]

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


pref = [0]*m

pref[0] = b[0]

for i in range(1, m):
    pref[i] = pref[i-1] + b[i]
#
# print(prefA)
# print(pref)

print(max(solve(n,m,prefA,pref), solve(m,n,pref,prefA)))

Submission status: Accepted

Solution code:
n, m, k = (int(x) for x in input().split())

a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]

K = k
ans = 0

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

# print(ans)
# print(b[l])
# print(K)
while l<m and K-b[l]>=0:
    K -= b[l]
    l += 1
    ans = max(ans, 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()))
for i in range(1, len(A)):
    A[i] += A[i - 1]
for i in range(1, len(B)):
    B[i] += B[i - 1]
A.insert(0, 0)
B.insert(0, 0)
r = 0
for i, a in enumerate(A):
    min_idx = 0
    max_idx = len(B) - 1
    while min_idx < max_idx:
        j = (min_idx + max_idx + 1) // 2
        k = K - A[i] - B[j]
        if k < 0:
            max_idx = j - 1
        else:
            min_idx = j
    j = min_idx
    while j + 1 < len(B) and K - A[i] - B[j + 1] >= 0:
        j += 1
    if K - A[i] - B[j] >= 0:
        r = max(r, i + j)
print(r)

Submission status: Accepted

Solution code:
import bisect

from collections import deque

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

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

aa = [0]*(n+1)

bb = [0]*(m+1)

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

for i in range(1,m+1):
    bb[i] += bb[i-1]+b[i-1]

count = 0

for i in range(n+1):
    if k < aa[i]:
        break
    serch = k-aa[i]
    index = bisect.bisect_right(bb, serch)
    index -= 1
    count = max(index+i,count)
print(count)

Submission status: Accepted

Solution code:
import sys
n,m,k=map(int,input().split())
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]

ans = 1
c = [0]
d = [0]
for i in range(1,n+1):
    c.append(c[i-1] + a[i-1])
    if(c[i] > k):
        c.pop()
        break
for i in range(1,m+1):
    d.append(d[i-1] + b[i-1])
    if(d[i] > k):
        d.pop()
        break


leng = len(c)
lend = len(d)
ans = leng - 1
temp = 0
for i in range(leng):
    p = c[leng-i-1]
    t = temp
    while(t < lend and p+d[t] <= k):
        t += 1
        if(t==lend):
            print(max(ans,leng-i-2+t))
            sys.exit()
    ans = max(ans,leng-i-2+t)
    temp = t

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=0
ac=0
bc=0
max=0

for i in range(n):
  s=s+a[i]
  ac=ac+1
  if s > k:
    s=s-a[i]
    ac=ac-1
    break
for i in range(m):
  s=s+b[i]
  bc=bc+1
  if s > k:
    s=s-b[i]
    bc=bc-1
    break

max=ac+bc

flag=0
while flag==0 and ac!=0:
  ac=ac-1
  s=s-a[ac]
  while s <= k:
    bc=bc+1
    if bc > m:
      flag=1
      break
    s=s+b[bc-1]
  bc=bc-1
  if flag==0:
    s=s-b[bc]
  
  if max < ac+bc:
    max=ac+bc
    
print(max)

Submission status: Accepted

Solution code:
import bisect
import itertools
n ,m ,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
a = list(itertools.accumulate(a))
b = list(itertools.accumulate(b))
A =bisect.bisect_left(a,k)
B =bisect.bisect_left(b,k)
if B<m:
  if b[B]==k:
    B+=1


if A<n:
  if a[A]==k:
    A+=1
count=max(A,B)
if b[0]>k:
  print(A)
  exit()
  
if a[0]>k:
  B =bisect.bisect_left(b,k)
  if B<m:
    if b[B]==k:
      B+=1
  print(B)
  exit()

for i in range(A,0,-1):
  ans=k-a[i-1]
  B =bisect.bisect_left(b,ans)
  if B<m:
    if b[B]==ans:
      B+=1
  count=max(count,i+B)
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
t = 0
a = 0
b = 0

while True:
    if not t + A[a] > K:
        t += A[a]
        a += 1
        # print(a, t, A[a-1])
        if a >= N:
            break
    else:
        break

flug = True     #全探索を行うか
while True:
    if not t + B[b] > K:
        t += B[b]
        b += 1
        # print(b, t, B[b-1])
        if b >= M:
            flug = False
            break
    else:
        break

# print(a, b, flug)
ans = a + b
flug2 = True    #処理を続けるか
if flug:
    while True:
        a -= 1
        if a < 0:
            break
        t -= A[a]
        while True:
            if not t + B[b] > K:
                t += B[b]
                b += 1
                if b >= M:
                    flug2 = False
                    break
            else:
                break
        ans = max(ans, a + b)
        if not flug2:
            break
print(ans)

Submission status: Accepted

Solution code:
import bisect
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
ruiseki_a = [0]
for i in range(n):
         ruiseki_a.append(a[i] + ruiseki_a[i])
ruiseki_b = [0]
for i in range(m):
         ruiseki_b.append(b[i] + ruiseki_b[i])
ans = 0
aread = bisect.bisect_right(ruiseki_a,k) - 1
for i in range(aread,-1,-1):
         zantime = k - ruiseki_a[i]
         bread = bisect.bisect_right(ruiseki_b,zantime) - 1
         tmp = (i + bread)
         if ans < tmp:
                  ans = tmp
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()))
timea=[0]
timeb=[0]
time=0
ans=0
for i in range(N):
    time+=A[i]
    if time>K:
        break
    timea.append(time)
time=0
for i in range(M):
    time+=B[i]
    if time>K:
        break
    timeb.append(time)
for i in range(len(timea)):
    b=K-timea[i]
    if b<0:
        break
    num=bisect.bisect_right(timeb,b)
    num+=i-1
    ans=max(ans,num)
if ans==10**10:
    print(0)
else:
    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()))

a = [False]*N
b = [False]*M

ans = 0
now = 0
bf = 0

for i in range(N):
    if now + A[i] > K:
        break
    now += A[i]
    ans += 1
    a[i] = True

for i in range(M):
    if now + B[i] > K:
        break
    now += B[i]
    ans += 1
    b[i] = True
    bf = i

if all(a) and all(b):
    print(ans)
    exit()

if not any(a) and not any(b):
    print(ans)
    exit()

n = K - now
ans2 = ans
for i in reversed(range(N)):
    if a[i]:
        a[i] = False
        n += A[i]
        ans2 -= 1
        for j in range(bf,M):
            if not b[j]:
                if n - B[j] < 0:
                    break
                b[j] = True
                bf = j
                n -= B[j]
                ans2 += 1
    if ans2 > ans:
        ans = ans2

print(ans)

Submission status: Accepted

Solution code:
import bisect

def cumsum(a):
    s = 0
    sums = [0]
    for a_i in a:
        s+=a_i
        sums.append(s)

    return sums

def main():
    n,m,k = tuple([int(t)for t in input().split()])

    a = [int(t)for t in input().split()]
    b = [int(t)for t in input().split()]

    acusum = cumsum(a)
    bcusum = cumsum(b)

    a_index = bisect.bisect(acusum,k)
    b_index = bisect.bisect(bcusum,k-acusum[a_index-1])

    ans = a_index+b_index-2

    while a_index > 0:
        a_index -= 1
        b_index = bisect.bisect(bcusum,k-acusum[a_index-1])

        ans = max(a_index+b_index-2,ans)
    
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import bisect
anum, bnum, time= map(int, input().split(" "))
a = list(map(int, input().split(" ")))
b = list(map(int, input().split(" ")))
tempa = [0]
tempb = [0]
total = 0
for i in range(anum):
    tempa.append(tempa[-1] + a[i])
    if total > time:
        break
for i in range(bnum):
    tempb.append(tempb[-1] + b[i])
    if total > time:
        break
maxi = 0
starta = bisect.bisect_right(tempa, time) - 1
for i in range(starta, -1, -1):
    startb = bisect.bisect_right(tempb, time - tempa[i]) - 1
    if i + startb > maxi:
        maxi = i + startb
print(maxi)

Submission status: Accepted

Solution code:
from collections import deque

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

cnt = 0
cnt_max = 0

t = 0

#aの本を全部読む場合を考える
#bを1冊ずつ増やしたときに最大になるかどうかを判定する

c = []

for i in a:
  t += i
  if t <= k:
    c.append(i)
    cnt += 1
  else:
    t -= i
    break

#print(c)
cnt_max = cnt
tmp_cnt = cnt

t_max = t

c.reverse()
for i in range(len(c) + 1):
  d = []
  
  if i == 0:
    pass
  else:
    t_max -= c[i-1]
    tmp_cnt -= 1
    
  #print(i, t_max, tmp_cnt, c[i-1])
  
  t = t_max
  cnt = tmp_cnt
  
  for j in b:
    t += j
    #print('j:', j, t)
    if t <= k:
      cnt += 1
      d.append(j)
      t_max = t
      if cnt > cnt_max:
        cnt_max = cnt
        #print('cnt', cnt, 'max', cnt_max)
    else:
      break
      
  for i in range(len(d)):
    b.popleft()
    tmp_cnt += 1
      
print(cnt_max)

Submission status: Accepted

Solution code:
from collections import deque
import bisect
N,M,K = list(map(int, input().split()))
As = list(map(int, input().split()))
Bs = list(map(int, input().split()))

#if(sum(As) + sum(Bs) <= K):
#    print(N + M)
#    exit()


Asruisekiwa = []
Bsruisekiwa = []

Asruisekiwa.append(As[0])
Bsruisekiwa.append(Bs[0])

for i, j in enumerate(As[1:]):
    Asruisekiwa.append(Asruisekiwa[-1]+j)

for i, j in enumerate(Bs[1:]):
    Bsruisekiwa.append(Bsruisekiwa[-1] + j)

#print(Asruisekiwa)
#print(Bsruisekiwa)

ans = 0
#print(Asruisekiwa)
#print(Bsruisekiwa)

if(Asruisekiwa[0] >= Bsruisekiwa[0]):
    tmp = Asruisekiwa
    Asruisekiwa = Bsruisekiwa
    Bsruisekiwa = tmp

Asruisekiwa.insert(0,0)
Bsruisekiwa.insert(0,0)

#print(Asruisekiwa)
#print(Bsruisekiwa)


for index, i in enumerate(Asruisekiwa):

    if(i > K):
        break

    tmp = i
    maxcheck = K - i


    hoge = bisect.bisect_right(Bsruisekiwa, maxcheck)

    ans = max(ans, index-1 + hoge)

#    print(ans, tmp, hoge, maxcheck)

print(ans)

Submission status: Accepted

Solution code:
#dt = {} for i in x: dt[i] = dt.get(i,0)+1
import sys;input = sys.stdin.readline
inp,ip = lambda :int(input()),lambda :[int(w) for w in input().split()]

from bisect import bisect_right as bs
n,m,k = ip()
a = ip()
b = ip()
x = [0]*(n+1)
for i in range(n):
    x[i+1] = x[i]+a[i]
y = [0]*(m+1)
for i in range(m):
    y[i+1] = y[i]+b[i]
ans = 0
for i in range(n+1):
    if x[i] <= k:
        ans = max(ans,i+bs(y,k-x[i])-1)
    else:
        break
print(ans)

Submission status: Accepted

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

p1, p2 = n, 0
f = [0]
s = [0]
for i in range(n):
    f.append(f[i]+arr1[i])
for i in range(m):
    s.append(s[i]+arr2[i])
highest = 0
while p1 >= 0:
    while p2 < m+1 and f[p1] + s[p2] <= k:
        highest = max(highest, p1+p2)
        p2 += 1
    p1 -= 1
    if p2 > 0:
        p2 -= 1
print(highest)

Submission status: Accepted

Solution code:

import sys

for a0 in range(1):
    n,m,x = input().strip().split(' ')
    n,m,x = [int(n),int(m),int(x)]
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    a.reverse()
    b.reverse()
    total = 0
    atmp = []
    for i in range(n):
        val = a.pop()
        if total + val > x:
            break
        total += val 
        atmp.append(val)

    max_count = len(atmp)
    cur_count = max_count
    while m:
        if total + b[-1] <= x:
            total += b.pop() 
            m -= 1
            cur_count += 1
            if cur_count > max_count:
                max_count = cur_count
            continue
        if not len(atmp):
            break
        aval = atmp.pop()
        total -= aval
        cur_count -= 1
    print (max_count)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
N, M, K = [int(s) for s in input().split()]
A = [int(s) for s in input().split()]
B = [int(s) for s in input().split()]
i = 0
j = M-1

B_SUM = [0] * M
A_SUM = [0] * N
B_SUM[0] = B[0]
A_SUM[0] = A[0]
max_book = 0

if B_SUM[0] <= K:
    max_book = 1

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

A_SUM = 0
b_index = M - 1

for i in range(N):
    A_SUM += A[i]
    while (b_index >= 0):
        B_SUM[b_index]
        if A_SUM + B_SUM[b_index] <= K:
            max_book = max(max_book, b_index + 1 + i + 1)
            break
        b_index -= 1
    if (A_SUM <= K):
        max_book = max(max_book, i + 1)
print(max_book)

Submission status: Accepted

Solution code:
from sys import stdin
#, setrecursionlimit, stdout
#setrecursionlimit(1000000)
#from collections import deque
#from math import sqrt, floor, ceil, log, log2, log10, pi, gcd, sin, cos, asin
#from heapq import heapify, heappop, heappush, heappushpop, heapreplace
def ii(): return int(stdin.readline())
def fi(): return float(stdin.readline())
def mi(): return map(int, stdin.readline().split())
def fmi(): return map(float, stdin.readline().split())
def li(): return list(mi())
def si(): return stdin.readline().rstrip()
def lsi(): return list(si())
#mod=1000000007
res=['NET', 'DA']
############# CODE STARTS HERE #############

def bin_search(a, val):
    pos, l, r=0, 0, len(a)-1
    while l<=r:
        m=(l+r)//2
        if a[m]<=val:
            pos=m+1
            l=m+1
        else:
            r=m-1
    return pos


test_case=1
while test_case:
    test_case-=1


    n, m, k=mi()
    a=li()
    b=li()
    for i in range(1, n):
        a[i]+=a[i-1]
    for i in range(1, m):
        b[i]+=b[i-1]
    ans=0
    a=[0]+a
    for i in range(n+1):
        if a[i]<=k:
            c=bin_search(b, k-a[i])
            ans=max(ans, c+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()))
a = -1
b = -1
total = 0
ans = 0
while a<len(A)-1:
    if total+A[a+1] > K:
        break
    a+=1
    total+=A[a]
    
while b<len(B)-1:
    if total+B[b+1] > K:
        break
    b+=1
    total+=B[b]
    
ans = a+b+2
subs = K - total
while a>=0:
    subs+=A[a]
    a-=1
    for i in range(b+1, len(B)):
        if subs < B[i]:
            break
        subs-=B[i]
        b = i
    ans = max(ans,a+b+2)
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()))
sum_a = [a_list[0]]
sum_b = [b_list[0]]
for i in range(1,n):
  sum_a.append(sum_a[-1] + a_list[i])
  
for i in range(1,m):
  sum_b.append(sum_b[-1] + b_list[i])
import bisect
result = [0]
for i in range(n):
  border = k - sum_a[i]
  if border < 0:
    break
  index = bisect.bisect_right(sum_b, border)
  result.append(i+1+index)
  
for i in range(m):
  border = k - sum_b[i]
  if border < 0:
    break
  index = bisect.bisect_right(sum_a, border)
  result.append(i+1+index)
print(max(result))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log,gcd
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')

#Pythonを用いる場合
#numpy 1.18.0, scipy 1.4.1, scikit-learn 0.22, numba 0.47.0, networkx 2.4
 
import numpy as np
from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson
from scipy.sparse import csr_matrix

 
#整数input
def ii(): return int(sys.stdin.readline().rstrip()) #int(input())
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) #list(map(int,input().split()))
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]
#文字列input
def ss(): return sys.stdin.readline().rstrip() #input()
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) #list(input().split())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]

#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？

n,m,k=mii()
arra=[0]+limii()
arrb=[0]+limii()

cuma=np.cumsum(arra)
cumb=np.cumsum(arrb)
ans=0
cnt=0
tmp=0
placea=0
for i in cuma:
    if i<=k:
        tmp=i
        placea+=1
        ans+=1
        cnt+=1
    else:
        continue
#print(cnt,tmp,ans,placea)
sumb=0
for i in arrb:
    #print(cnt,sumb+i,cuma[placea-1],cuma[placea-1],k,i)
    #print(i)
    if tmp+sumb+i<=k:
        tmp+=i
        sumb+=i
        cnt+=1
        #print(1111111)
    else:

        while sumb+i+cuma[placea-1]>k:
            placea-=1
            cnt-=1
            
            if placea<1:
                break
        if sumb+i<=k:
            tmp+=i
            sumb+=i
            cnt+=1
        #print(1111)
    ans=max(ans,cnt)
    if placea<1:
        break
print(ans-2)

Submission status: Accepted

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

def main():
    n, m, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    ans = 0
    a_time = [0]
    b_time = [0]
    for i in range(1, len(a) + 1):
        a_time.append(a_time[i - 1] + a[i - 1])
    for i in range(1, len(b) + 1):
        b_time.append(b_time[i - 1] + b[i - 1])

    a_max = len(a_time) - 1
    b_max = len(b_time) - 1
    for i in range(1, len(a_time)):
        if a_time[i] > k:
            a_max = i - 1
            break
    for i in range(1, len(b_time)):
        if b_time[i] > k:
            b_max = i - 1
            break

    for i in range(len(b_time)):
        if a_time[a_max] + b_time[i] <= k:
            ans = max(ans, a_max + i)
        else:
            if a_max > 0:
                a_max -= 1
    # for i in range(len(a_time)):
    #     if b_time[b_max] + a_time[i] <= k:
    #         ans = max(ans, b_max + i)
    #     else:
    #         if b_max > 0:
    #             b_max -= 1

    print(ans)

if __name__ == '__main__':
    main()

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

from itertools import accumulate
from collections import deque
A=deque(list(accumulate(A)))
B=deque(list(accumulate(B)))
A.appendleft(0)
B.appendleft(0)
ans1=0
for i in range(N+1):
    if A[i]>K:
        ans1=i-1
        break
else:
    ans1=N
ans2=0
ans3=0
# print(ans1)
for i in range(ans1+1):
#     print(i)
    while i+ans2<=M and A[ans1-i]+B[i+ans2]<=K:
        ans2+=1
#         print(ans1,ans2,i)
    ans3=max(ans3,ans1+ans2-1)
print(ans3)