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

def binsearch(times, target):
    if target < 0:
        return 0
    elif target == 0:
        return 0

    low = 0
    high = len(times) - 1
    while low <= high:
        mid = (low+high) // 2
        cur = times[mid]

        if cur == target:
            return mid
        elif cur < target:
            low = mid+1
        else:
            high = mid-1

    if 0 <= mid+1 < len(times) and times[mid+1] <= target:
        return mid+1
    elif 0 <= mid < len(times) and times[mid] <= target:
        return mid
    elif 0 <= mid-1 < len(times) and times[mid-1] <= target:
        return mid-1
    else:
        return 0

times_a = [0]
cur_time = 0
for ai in a:
    if cur_time + ai > k:
        break
    else:
        cur_time += ai
        times_a.append(cur_time)

res = binsearch(times_a, k)

if res >= 0:
    best = res
else:
    best = 0

cur_time = 0
for i, bi in enumerate(b, start=1):
    cur_time += bi

    if cur_time > k:
        break

    a_books = binsearch(times_a, k - cur_time)
    best = max(best, i + a_books)

print(best)

Submission status: Accepted

Solution code:
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
# def LF(): return [float(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n,m,k=LI()
  _a=LI()
  _b=LI()
  la=copy.deepcopy(_a)
  lb=copy.deepcopy(_b)

  for i in range(n-1):
    la[i+1]+=la[i]

  for i in range(m-1):
    lb[i+1]+=lb[i]

  # print(la)
  # print(lb)

  cntA=cntB=0
  for i,x in enumerate(la):
    if x>k:
      cntA=i
      break
  if la[-1]<=k:
    cntA=n
  for i,x in enumerate(lb):
    if x>k:
      cntB=i
      break
  if lb[-1]<=k:
    cntB=m

  # print(cntA,cntB)

  mx=max(cntA,cntB)
  # print(mx)

  i=-1
  j=cntB-1
  sm=lb[j]

  while True:

    if sm<=k:
      i+=1
      if i>=n:
        break
      sm+=_a[i]

    else:
      sm-=_b[j]
      j-=1
      if j<0:
        break

    if sm<=k:
      mx=max(mx,i+j+2)
  # print(mx)

  i=-1
  j=cntA-1
  sm=la[j]

  while True:
    if i>=m-1 or j<=0:
      break

    if sm<=k:
      i+=1
      if i>=n:
        break
      sm+=_b[i]

    else:
      sm-=_a[j]
      j-=1
      if j<0:
        break

    if sm<=k:
      mx=max(mx,i+j+2)

  return mx

# main()
print(main())

Submission status: Accepted

Solution code:
from itertools import accumulate
from bisect import bisect_right
N, M, K = map(int, input().split())
A = list(accumulate([int(i) for i in input().split()]))
B = list(accumulate([int(i) for i in input().split()]))

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

print(ans)

Submission status: Accepted

Solution code:
import sys

def read_int():
    l = sys.stdin.readline().rstrip('\n').split(' ')
    return int(l[0])

def read_int_list():
    l = sys.stdin.readline().rstrip('\n').split(' ')
    return [int(i) for i in l]

N, M, K = tuple(read_int_list())
A = read_int_list()
B = read_int_list()

ans = 0
sum_k =  0

ai = 0
bi = 0

for i in range(len(A)):
    if sum_k + A[i] > K:
        break
    sum_k += A[i]
    ai = i + 1

for i in range(len(B)):
    if sum_k + B[i] > K:
        break
    sum_k += B[i]
    bi = i + 1

ans = ai + bi

for i in range(ai - 1, -1, -1):
    sum_k -= A[i]
    ai = i
    while bi < len(B) and sum_k + B[bi] <= K:
        sum_k += B[bi]
        bi += 1
    ans = max(ans, ai+bi)
print(ans)

Submission status: Accepted

Solution code:
import bisect
N,M,K = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
A = [0]*(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] 

possible = [0]
for i in range(N+1):
    ans = i
    nokori = K - A[i]
    if nokori < 0:
        break
    else:
        j = bisect.bisect_right(B,nokori)
        ans += j-1
        possible.append(ans)

print(max(possible))

Submission status: Accepted

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

c=0

sa=0#s[:aa]
sb=0#s[:bb]
aa=0#0~n
bb=0#0~m

for i in range(m):
    sb+=b[i]
    if sb>k:
        sb-=b[i]
        bb=i
        break
    if i==m-1:
        bb=m



for i in range(n+1):
    aa=i
    if aa!=0:
        sa+=a[aa-1]
    if sa>k:
        break
    if sa+sb<=k:
        c=max(c,aa+bb)
    else:
        while sa+sb>k:
            sb-=b[bb-1]
            bb-=1
        c=max(c,aa+bb)


print(c)

Submission status: Accepted

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

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

m = 0
mnt = 0
num = 0
for i, a in enumerate(A):
    num = i+1
    if mnt + a > K:
        num = i
        break
    mnt += a
A = A[:num]
ans = num

j = 0
for i in range(num+1):
    while j < M and mnt+B[j] <= K:
        mnt += B[j]
        j += 1
        ans = max(ans, j+num-i)
    if i == num:
        break
    mnt -= A[num-i-1]

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()))
accu = 0
aa = [0]
for i in a:
    accu += i
    aa.append(accu)
accu = 0
bb = [0]
for i in b:
    accu += i
    bb.append(accu)
ans = 0
for i in range(n+1):
    if aa[i]>k:
        break
    tmp = i+bisect_right(bb, k-aa[i])-1
    ans = max(tmp, ans)
print(ans)

Submission status: Accepted

Solution code:
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
s = 0
t = 0
ai = -1
bi = -1
for i in range(n):
    if s + a[i] <= k:
        s += a[i]
        t += 1
        ai = i
    else:
        break
ans = t
while True:
    for i in range(bi+1, m):
        if s + b[i] <= k:
            s += b[i]
            t += 1
            bi = i
        else:
            break
    ans = max(t, ans)
    if ai == -1 or bi == m-1:
        break
    s -= a[ai]
    t -= 1
    ai -= 1
print(ans)

Submission status: Accepted

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

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 19
MOD = 10 ** 9 + 7
EPS = 10 ** -10

N, M, K = MAP()
A = LIST()
B = LIST()

acc1 = [0] + list(accumulate(A))
acc2 = [0] + list(accumulate(B))
ans = 0
for i in range(N+1):
    cur = K - acc1[i]
    if cur < 0:
        break
    j = bisect_right(acc2, cur) - 1
    ans = max(ans, i+j)
print(ans)

Submission status: Accepted

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

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

cum_A = [0]
for a in A:
    add_value = cum_A[-1] + a
    if add_value > K:
        break
    cum_A.append(add_value)

cum_B = [0]
for b in B:
    add_value = cum_B[-1] + b
    if add_value > K:
        break
    cum_B.append(add_value)

ix_B = len(cum_B) - 1
ans = 0
for ix_A in range(len(cum_A)):
    while cum_A[ix_A] + cum_B[ix_B] > K:
        ix_B -= 1
    ans = max(ans, ix_A + ix_B)

print(ans)

Submission status: Accepted

Solution code:
import bisect

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

A = [0]
sumOfA = 0
for a in map(int, input().split()):
    sumOfA += a
    A.append(sumOfA)

idxA = bisect.bisect_right(A, K) - 1
ans = idxA

sumOfB = 0
for idxB, b in enumerate(map(int, input().split())):
    K -= b
    if K < 0:
        break

    while(idxA > 0):
        if A[idxA] <= K:
            break
        
        idxA -= 1


    ans = max(ans, idxB + idxA + 1)

print(ans)

Submission status: Accepted

Solution code:
def bisection(B_sum, x):
    M = len(B_sum)
    if B_sum[0] > x:
        return 0
    elif B_sum[-1] <= x:
        return M
    else:
        low = 0
        high = M-1
        while True:
            new = (low+high)//2
            if B_sum[new]>x:
                high = new
            else:
                low = new
            if (high-low)==1:
                break
        return high


N, M, K = map(int, input().split())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]

A_sum = []
B_sum = []

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

#print(A_sum)
#print(B_sum)

max_book = 0
##no A
max_book = bisection(B_sum, K)
#print(max_book)
##at least one A
for i in range(N):
    if A_sum[i] > K:
        break
    book = bisection(B_sum, K-A_sum[i])
    #print(book)
    if (i+book+1) > max_book:
        max_book = i+book+1
    
print(max_book)

Submission status: Accepted

Solution code:
import bisect
N,M,K = map(int,input().split())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]

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

ans = bisect.bisect_right(bsum,K) -1
asum = 0
for n,a in enumerate(A):
    asum += a
    if asum > K:
        break
    m = bisect.bisect_right(bsum,K-asum)
    if ans < m+n:
        ans = m+ n
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

At=[0]
Bt=[0]

for i in range(N):
    At.append(At[-1]+A[i])
for j in range(M):
    Bt.append(Bt[-1]+B[j])
ans=0
for k in range(N+1):
    temp=k+bl(Bt,K-At[k])-1
    if K-At[k]>=0:
        ans=max(ans,temp)
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()))
AA=[0]
BB=[0]
a,b=0,0
for i in range(N):
  a+=A[i]
  AA.append(a)
for i in range(M):
  b+=B[i]
  BB.append(b)
import bisect
cnt=0
for i in range(N+1):
  T=K-AA[i]
  if T>=0:
    d=bisect.bisect_right(BB,T)
    D=i+d-1
    cnt=max(cnt,D)
print(cnt)

Submission status: Accepted

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

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

ans = 0
for i in range(N+1):
    if 0 <= K-A_sum[i]:
        j = bisect_right(B_sum,K-A_sum[i])
        ans = max(ans,i+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()))
sb = sum(b)
s = sb
j = m-1
while s > k:
    s -= b[j]
    j -= 1
ans = j+1
for i in range(n):
    s += a[i]
    while s > k and j >= 0:
        s -= b[j]
        j -= 1
    if s > k:
        break
    ans = max(ans, i+j+2)
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
import sys

stdin = sys.stdin
ns = lambda: stdin.readline().rstrip() # ignore trailing spaces
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))


def BinarySearch(num, List):
    if len(List) == 0:
        print("Erorr : list has no element")
        return
    left,right = 0,len(List)
    while left < right:
        mid  = (left+right)//2
        if num < List[mid]:#
            right = mid
        else:
            left = mid + 1
    return right


def main():
    n,m,k = na()
    honA = [0] + na() + [float('inf')]
    honB = [0] + na() + [float('inf')]
    honAsum = [0]
    for i in range(n+1):
        honAsum += [honAsum[-1]+honA[i+1]]
    honBsum = [0]
    for i in range(m+1):
        honBsum += [honBsum[-1]+honB[i+1]]
    rightA = BinarySearch(k, honAsum)
    rightB = BinarySearch(k-honAsum[rightA-1],honBsum)
    ans = rightA + rightB - 2
    while rightA > 0:
        if rightB == m+1:
            break
        rightA -= 1
        rightB = BinarySearch(k-honAsum[rightA-1],honBsum)
        ans = max(ans, rightA+rightB-2)
    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np
import bisect

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

a = np.array(list(map(int, input().split())))
b = np.array(list(map(int, input().split())))

sa = np.cumsum(a)
sb = np.cumsum(b)

if sa[-1] + sb[-1] <= k:
    print(len(a)+len(b))
    exit(0)

r = bisect.bisect_right(sb, k)
for i, saa in enumerate(sa):
    if saa > k:
        continue
    j = bisect.bisect_right(sb, k-saa)
    r = max(r, i+j+1)

print(r)

Submission status: Accepted

Solution code:
from bisect import bisect_right as br
N,M,K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
time=0
allB=0
AA=[0]
BB=[0]
for i in range(N):
  AA.append(A[i]+AA[-1])
for i in range(M):
  BB.append(B[i]+BB[-1])

allB=br(BB,K)-1
ans=0
for i in range(allB,-1,-1):
  #print(i)
  ans=max(ans,i+br(AA,K-BB[i])-1)
print(ans)

Submission status: Accepted

Solution code:
N,M,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
tot = 0
cur = -1
cnt = 0
for i in range(N):
    tot += A[i]
    if tot<=K:
        cur = i
        cnt += 1
    else:
        tot -= A[i]
        break
j = 0
while j<M and tot+B[j]<=K:
    tot += B[j]
    j += 1
    cnt += 1
cmax = cnt
for i in range(cur,-1,-1):
    tot -= A[i]
    cnt -= 1
    while j<M and tot+B[j]<=K:
        tot += B[j]
        j += 1
        cnt += 1
    cmax = max(cmax,cnt)
print(cmax)

Submission status: Accepted

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

acc = [0] + list(accumulate(a))
acc1 = list(accumulate(b))
ans = 0
for i in range(n + 1):
    cnt = i
    temp = acc[i]
    left = k - temp
    if left < 0:continue 
    
    ind = bisect_right(acc1, left)
    cnt += ind
    if ans < cnt:
        ans = cnt
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect
from itertools import accumulate
n, m, k, *AB = map(int, open(0).read().split())
A, B = [0] + AB[:n], AB[n:]

A_ = list(accumulate(A))
B_ = list(accumulate(B))
ans = 0
for i in range(n+1):
    if k < A_[i]:
        break
    t = i + bisect.bisect(B_, k-A_[i])
    ans = max(ans, t)
print(ans)

Submission status: Accepted

Solution code:

def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def s3(n):return [input().split() for _ in range(n)]
def s4(n):return [[x for x in s] for s in s2(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
def t3(n):return [tuple(int(x) for x in input().split()) for _ in range(n)]
def p0(b,yes="Yes",no="No"): print(yes if b else no)
# from sys import setrecursionlimit
# setrecursionlimit(1000000)
# from collections import Counter,deque,defaultdict
import itertools
# import math
# import networkx as nx
from bisect import bisect_left,bisect_right
# from heapq import heapify,heappush,heappop
n,m,k=n1()
A=n1()
B=n1()

AA=list(itertools.accumulate(A))
BB=list(itertools.accumulate(B))

ans=bisect_right(BB,k)

for i in range(n):
    s=k-AA[i]
    if s<0:
        continue
    tmp=bisect_right(BB,s)
#     print(i,tmp,ans)
    ans=max(ans,i+1+tmp)
print(ans)

Submission status: Accepted

Solution code:
import sys 
input = lambda:sys.stdin.readline().strip()
import bisect
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
ans = 0
for i in range(1,n):
    a[i]+=a[i-1]
for i in range(1,m):
    b[i]+=b[i-1]
for i in range(n):
    rem = k-a[i]
    if rem<0:
        break 
    ind = bisect.bisect_right(b,rem)
    ans = max(ans,i+1+ind)
    # print(ans,ind,rem)
for i in range(m):
    rem = k-b[i]
    if rem<0:
        break 
    ind = bisect.bisect_right(a,rem)
    ans = max(ans,i+1+ind)
print(ans)

Submission status: Accepted

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

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


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

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

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

    ans = 0
    for i in range(N + 1):
        tmp = i
        ta = time_A[i]
        if ta <= K:
            tb = K - ta
            j = bisect_right(time_B, tb)
            tmp += j - 1

            if ans < tmp:
                ans = tmp

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    import sys
    from bisect import bisect_right

    N,M,K=list(map(int,sys.stdin.readline().split()))
    A=list(map(int,sys.stdin.readline().split()))
    B=list(map(int,sys.stdin.readline().split()))

    AS=[0]*N
    BS=[0]*M
    for i,a in enumerate(A):
        if i:
            AS[i]=AS[i-1]+a
        else:
            AS[i]=a

    for i,b in enumerate(B):
        if i:
            BS[i]=BS[i-1]+b
        else:
            BS[i]=b

    mx=bisect_right(BS,K)
    for i,sa in enumerate(AS):
        if K-sa>=0:
            p=bisect_right(BS,K-sa)
            mx=max(mx,i+p+1)

    print(mx)


main()

Submission status: Accepted

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

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

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

    print(res)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

tmpA = [0]*(N+1)
tmpB = [0]*(M+1)
ans = 0

import bisect
for i in range(1,N+1):
    tmpA[i] = tmpA[i-1] + A[i-1]
for i in range(1,M+1):
    tmpB[i] = tmpB[i-1] + B[i-1] 
for i in range(N+1):
    tmp = tmpA[i]
    if tmp > K:
        break
    index = bisect.bisect_right(tmpB,K-tmp)
    ans = max(i+index-1,ans)
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()))

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 = [0] + A
B = [0] + B

ans = 0
for i in range(len(A)):
    if K - A[i] > 0:
        ans = max(ans,i + bisect_right(B,K - A[i]) - 1)

for i in range(len(B)):
    if K - B[i] > 0:
        ans = max(ans,i + bisect_right(A,K - B[i]) - 1)

print(ans)

Submission status: Accepted

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

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

Acum = [0]*n
Bcum = [0]*m
Acum[0] = A[0]
Bcum[0] = B[0]

for i in range(1,n):
  Acum[i] = Acum[i-1] + A[i]  
for i in range(1,m):
  Bcum[i] = Bcum[i-1] + B[i]

mx = 0

import bisect
for i in range(n+1):
  cnt = 0
  if i==0:
    rest = k
  else:
    rest = k - Acum[i-1]
    cnt = i
    if rest<0:continue
  
  idx = bisect.bisect_right(Bcum, rest)
  cnt += idx

  if cnt>mx:
    mx = cnt
    
print(mx)
#print(bisect.bisect_right([0,1], 0))

Submission status: Accepted

Solution code:
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 itr, a in enumerate(A):
  AA[itr + 1] = a + AA[itr]

for itr, b in enumerate(B):
  BB[itr + 1] = b + BB[itr]

ans = 0
for itr, aa in enumerate(AA):
  if aa <= k:
    pos = k - aa
    # にぶたん
    ok = 0
    ng = m + 1
    while abs(ok - ng) > 1:
      mid = (ok + ng) // 2
      if aa + BB[mid] > k:
        ng = mid
      else:
        ok = mid
    ans = max(ans, ok + itr)
  else:
    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()))

tmp = 0

flag = True
for i in range(N):
    if tmp + A[i] <= K:
        tmp += A[i]
    else:
        flag = False
        break

left = K - tmp #残りの使える時間

total = i
if flag:
    i += 1
    total += 1

ans = 0
flag = True
for j in range(M):
    if B[j] <= left:
        total += 1
        left -= B[j]
    else:
        flag = False
        break
if flag:
    print (total)
    exit()

ans = total
while i >= 1:
    total -= 1
    left += A[i - 1]
    i -= 1
    while j < M:
        if B[j] <= left:
            total += 1
            left -= B[j]
            j += 1
        else:
            break
    ans = max(ans, total)

print (ans)

Submission status: Accepted

Solution code:
from bisect import bisect_right

n, m, k = list(map(int, input().split()))
a = list(map(int, input().split()))
asum = [0]*n
asum[0] = a[0]
b = list(map(int, input().split()))
bsum = [0]*m
bsum[0]=b[0]
for i in range(1, n):
  asum[i]=asum[i-1]+a[i]
for i in range(1, m):
  bsum[i]=bsum[i-1]+b[i]

ans = 0
for i in range(n):
  if asum[i] <= k:
    temp = i+1
    nokori = k-asum[i]
    #print("i " + str(i) + " nokori " + str(nokori))
    temp += bisect_right(bsum, nokori)
    ans = max(ans, temp)

ans = max(ans, bisect_right(bsum, k))
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()))
ca = 0
cb = 0
s = 0
for i in range(N):
  if s+A[i] <= K:
    ca += 1
    s += A[i]
  else:
    break
#print(ca)
CA = ca
for i in range(M):
  if s+B[i] <= K:
    cb += 1
    s += B[i]
  else:
    break
ans = ca+cb
#print(ans)
for i in reversed(range(CA)):
  ca -= 1
  s -= A[i]
  for i in range(cb,M):
    if s+B[i] <= K:
      cb += 1
      s += B[i]
    else:
      break
  ans = max(ans,ca+cb)
print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate
from bisect import bisect_left,bisect
n, m , k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
ruia = [0]
ruia.extend(list(accumulate(a)))
ruib = list(accumulate(b))
ans = 0
for i in range(n+1):
    nokori = k - ruia[i]
    if nokori < 0:
        break
    yomeru = bisect(ruib,nokori)
    ans = max(ans,i+yomeru)
print(ans)

Submission status: Accepted

Solution code:
import bisect

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

asum = [0]
for a in A:
    asum.append(asum[-1] + a)

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

ans = 0
for i, a in enumerate(asum):
    btime = K - a
    if btime < 0:
        break
    bindex = bisect.bisect_right(bsum, btime)
    ans = max(ans, i + (bindex-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()))

sumA = [0]
s = 0
for a in A:
    s += a
    sumA.append(s)

s = 0
sumB = []
for b in B:
    s += b
    sumB.append(s)
# print(sumA,sumB)
ans = 0
for i in range(len(sumA)):
    if sumA[i] <= k:
        diff = k - sumA[i]
        j = bisect.bisect_right(sumB,diff)
        # print(sumA[i],diff,j)
        ans = max(i+j,ans)

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


SA, SB = [0]*(N+1), [0]*(M+1)
for i, a in enumerate(A):
    SA[i + 1] = SA[i] + a

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

piv = M
ans = 0
flg = False
for i, a in enumerate(SA):
    while SB[piv] + SA[i] > K:
        piv -= 1
        if piv < 0:
            flg = True
            break
    if flg:
        break
    ans = max(ans, piv+i)
print(ans)

Submission status: Accepted

Solution code:
import bisect
import itertools
n, m, k = list(map(int, input().split()))
al = list(map(int, input().split()))
bl = list(map(int, input().split()))

rui_al = list(itertools.accumulate(al))
rui_bl = list(itertools.accumulate(bl))


ans = 0

for i in range(n):
    sum_a = rui_al[i]
    if sum_a >= k:
        continue
    j = bisect.bisect_right(rui_bl, k-sum_a)
    ans = max(ans, i+j+1)

for i in range(m):
    sum_b = rui_bl[i]
    if sum_b >= k:
        continue
    j = bisect.bisect_right(rui_al, k-sum_b)
    ans = max(ans, i+j+1)

j = bisect.bisect_right(rui_al, k)
ans = max(ans, j)
j = bisect.bisect_right(rui_bl, k)
ans = max(ans, j)

print(ans)

Submission status: Accepted

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

sa = [0]
for i in range(n):
    sa.append(sa[-1] + a[i])
sb = [0]
for i in range(m):
    sb.append(sb[-1] + b[i])

ans = []
for i in range(n+1):
    if k < sa[i]:
        break
    goal = k - sa[i]

    # Binary Search
    start = 0
    stop = m
    while start < stop:
        center = (start + stop + 1) // 2
        if sb[center] == goal:
            start = center
            stop = center
        elif sb[center] < goal:
            start = center
        else:
            stop = center - 1
    
    ans.append(i + start)

print(max(ans))

Submission status: Accepted

Solution code:
from bisect import bisect_right
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(N):
    A[i+1] += A[i]
for i in range(M):
    B[i+1] += B[i]
ans = 0
for i in range(N+1):
    if A[i] > K:
        break
    ans = max(i+bisect_right(B, K-A[i])-1, 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()))

ruiseki_a = [0]
ruiseki_b = [0]

a_index = N+1
for i in range(N):
  if K < ruiseki_a[-1] + A[i]:
    a_index = i+1
    break
  ruiseki_a.append(ruiseki_a[-1] + A[i])
b_index = M+1
for i in range(M):
  if K < ruiseki_b[-1] + B[i]:
    b_index = i+1
    break
  ruiseki_b.append(ruiseki_b[-1] + B[i])
lis = [0 for _ in range(a_index)]
for i in range(a_index):
  index = bisect.bisect_left(ruiseki_b, K-ruiseki_a[i]+1)
  lis[i] = i + max(0, index - 1)
print(max(lis))

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

x=k
ans=0
for i in range(m):
  if x>=0:
    ans1=bisect_right(aa,x)+i
    ans=max(ans1,ans)
    x-=b[i]
if x>=0:
  ans1=bisect_right(aa,x)+m
  ans=max(ans1,ans)
print(ans)