Problem:


Score : 300 points

Problem Statement

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

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

Consider the following action:

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

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

Constraints

1 \leq N, M \leq 200000

1 \leq K \leq 10^9

1 \leq A_i, B_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

3 4 240
60 90 120
80 150 80 150

Sample Output 1

3

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

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

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

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

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

Sample Input 2

3 4 730
60 90 120
80 150 80 150

Sample Output 2

7

Sample Input 3

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

Sample Output 3

0

Watch out for integer overflows.



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


Sample output:
3

Submission status: Accepted

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

a_ls = [0] + list(map(int, input().split(' ')))
b_ls = [0] + list(map(int, input().split(' ')))

for i in range(1, n+1):
    a_ls[i] += a_ls[i-1]
for i in range(1, m+1):
    b_ls[i] += b_ls[i-1]

b_cnt = m
for a_cnt in range(0, n+1):
    if a_ls[a_cnt] > k:
        continue
    while a_ls[a_cnt] + b_ls[b_cnt] > k:
        b_cnt -= 1
    ans = max(ans, a_cnt + b_cnt)

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

AccA = [0 for i in range(N+1)]
AccB = [0 for i in range(M+1)]

for i in range(1,N+1):
    AccA[i] = AccA[i-1] + A[i-1]
for j in range(1,M+1):
    AccB[j] = AccB[j-1] + B[j-1]

import bisect

Max = 0
#print(AccA)
#print(AccB)
for n in range(N+1):
    if(AccA[n] > K): break
    books = n
    time_left = K - AccA[n]
    books += bisect.bisect_right(AccB,time_left) - 1
    Max = max(Max,books)
    #print(n,bisect.bisect_right(AccB,time_left)-1)

print(Max)

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_sum = [0]
B_sum = [0]
for i in range(N):
    A_sum.append(A_sum[i]+A[i])
for i in range(M):
    B_sum.append(B_sum[i]+B[i])

num = 0
J = M
for i in range(N+1):
    if A_sum[i] > K:
        break
    for j in reversed(range(J+1)):
        if A_sum[i] + B_sum[j] <= K:
            if num < i+j:
                num = i+j
            J = j
            break

print(num)

Submission status: Accepted

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

a = [0 for _ in range(n)]
b = [0 for _ in range(m)]

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

mx = sum(a)+sum(b)

a_sum = [0 for _ in range(n)]
b_sum = [0 for _ in range(m)]

sm = 0
for i in range(n):
    sm += a[i]
    a_sum[i] = sm

sm = 0
for i in range(m):
    sm += b[i]
    b_sum[i] = sm


#print(a_sum)
def solve():
    if(k >= mx):
        print(n+m)
        return
    
    
    caca = bisect.bisect_right(b_sum,k)
    
    for i in range(n):
        a_a = a_sum[i]
        rd = k - a_a
        if(rd < 0):
            continue

        b_b = bisect.bisect_right(b_sum,rd)
        caca_tmp = i + 1 + b_b
        caca = max(caca,caca_tmp)
        #print(i,b_b,rd)
    
    print(caca)



solve()

Submission status: Accepted

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

a = [0]
b = [0]

for i in range(N):
    a.append(a[i] + A[i])

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

Submission status: Accepted

Solution code:
n,m,k=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
a=[0];b=[0];ans=0;j=m
for i in range(n):
    a.append(a[i]+A[i])
for i in range(m):
    b.append(b[i]+B[i])
for i in range(n+1):
    if a[i]>k:
        break
    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(n):
	a[i+1]+=a[i]
for i in range(m):
	b[i+1]+=b[i]
from bisect import bisect_right
ans=0
for i in range(n+1):
	if k-a[i]>=0:
		r=bisect_right(b,k-a[i])
		ans=max(r+i-1,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()))

suma = [0] * (n)
sumb = [0] * (m)
suma[0] = a[0]
sumb[0] = b[0]

for i in range(1,n):
    suma[i] = suma[i-1]+a[i]
for j in range(1,m):
    sumb[j] = sumb[j-1]+b[j]
    
suma.insert(0,0)
sumb.insert(0,0)

ans, cur = 0, m
for i in range(n+1):
  target = k - suma[i]
  if target < 0:
      break
  while sumb[cur] > target:
      cur -= 1
  ans = max(ans, i+cur)
print(ans)

Submission status: Accepted

Solution code:
n, m, x = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
b = list(map(int, input().split(' ')))
A = [0]
B = [0]

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

y = len(B) - 1
ans = 0
for i in range(len(A)):
	while A[i] + B[y] > x:
		y -= 1
		if y == -1:
			break
	if y == -1:
		break
	if i + y > ans:
		ans = i + y

print(ans)

Submission status: Accepted

Solution code:
#n=int(input())
n,m,k=map(int,input().split())
al=list(map(int,input().split()))
bl=list(map(int,input().split()))
#l=[list(map(int,input().split())) for i in range(n)]
import bisect
A=[0]
for i in range(n):
    A.append(A[i]+al[i])
B=[0]
for i in range(m):
    B.append(B[i]+bl[i])
#B.append(max(B[m],k)+1)
mx=0
for i in range(n+1):
    rem=k-A[i]
    if rem<0:
        continue
    bnum=bisect.bisect_right(B,rem)-1
    mx=max(mx,i+bnum)

print(mx)

Submission status: Accepted

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

A = [0]
B = [0]

for i in range(n):
    A.append(A[i]+a[i])

for i in range(m):
    B.append(B[i]+b[i])
    
ans,j = 0,m
for i in range(n+1):
    if A[i] > k:
        break
    while B[j] + 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()))
A_sum = [0]
B_sum = [0]
A_sum.extend(A)
B_sum.extend(B)
for i in range(1,N+1):
    A_sum[i]+=A_sum[i-1]
for i in range(1,M+1):
    B_sum[i]+=B_sum[i-1]
ans,j=0,M
for i in range(N+1):
    if A_sum[i]>K:
        break
    while B_sum[j]+A_sum[i]>K:
        j-=1
    ans = max(ans,i+j)
print(ans)

Submission status: Accepted

Solution code:
#n=int(input())
n,m,k=map(int,input().split())
al=list(map(int,input().split()))
bl=list(map(int,input().split()))
#l=[list(map(int,input().split())) for i in range(n)]

A=[0]
for i in range(n):
    A.append(A[i]+al[i])
B=[0]
for i in range(m):
    B.append(B[i]+bl[i])
B.append(max(B[m],k)+1)
mx=0
for i in range(n+1):
    rem=k-A[i]
    if rem<0:
        continue
    left=0;right=m+1
    while right-left>1:
        mid=(left+right)//2
        if B[mid]>rem:
            right=mid
        else:
            left=mid
    mx=max(left+i,mx)

print(mx)

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

print(ans)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random,resource

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
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 list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    n,m,k = LI()
    a = LI()
    b = LI()

    d = [0]
    for c in a:
        d.append(d[-1] + c)

    e = [0]
    for c in b:
        e.append(e[-1] + c)

    r = 0
    di = len(d) - 1
    for ei in range(len(e)):
        if e[ei] > k:
            break

        while d[di] + e[ei] > k:
            di -= 1

        t = ei + di
        if r < t:
            r = t

    return r


print(main())

Submission status: Accepted

Solution code:
if __name__ == "__main__":
    n,m,k = (int(x) for x in input().split())
    a = [int(x) for x in input().split()]
    b = [int(x) for x in input().split()]

    pre = 0
    for i in range(len(a)):
        pre += a[i]
        a[i] = pre
    
    pre = 0
    for i in range(len(b)):
        pre += b[i]
        b[i] = pre

    ans = 0
    for i in range(-1,len(a)):
        have = k
        if i >= 0:
            have -= a[i]
        if have <0:
            continue
        ans = max(ans, i+1)
        if b[0] > have:
            continue
        low, high = (0, len(b)-1)
        while low<=high:
            mid = (low+high)//2
            if have >= b[mid]:
                low = mid + 1
            else:
                high = mid - 1
        ans = max(ans, i+1 + high+1)
    print(ans)

Submission status: Accepted

Solution code:
import math

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_cnt = m
for a_cnt in range(len(a)):
    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:
"""
    Satwik_Tiwari ;) .
    4th july , 2020  - Saturday
"""

#===============================================================================================
#importing some useful libraries.
from __future__ import division, print_function

from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase


import bisect
from heapq import *
from math import *
from collections import deque
from collections import Counter as counter  # Counter(list)  return a dict with {key: count}
from itertools import combinations as comb # if a = [1,2,3] then print(list(comb(a,2))) -----> [(1, 2), (1, 3), (2, 3)]
from itertools import permutations as permutate
from bisect import bisect_left as bl
#If the element is already present in the list,
# the left most position where element has to be inserted is returned.
from bisect import bisect_right as br
from bisect import bisect
#If the element is already present in the list,
# the right most position where element has to be inserted is returned

#==============================================================================================

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


def print(*args, **kwargs):
    """Prints the values to a stream, or to sys.stdout by default."""
    sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout)
    at_start = True
    for x in args:
        if not at_start:
            file.write(sep)
        file.write(str(x))
        at_start = False
    file.write(kwargs.pop("end", "\n"))
    if kwargs.pop("flush", False):
        file.flush()


if sys.version_info[0] < 3:
    sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout)
else:
    sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)

# inp = lambda: sys.stdin.readline().rstrip("\r\n")

#===============================================================================================
#some shortcuts

mod = 1000000007
def inp(): return sys.stdin.readline().rstrip("\r\n") #for fast input
def out(var): sys.stdout.write(str(var))  #for fast output, always take string
def lis(): return list(map(int, inp().split()))
def stringlis(): return list(map(str, inp().split()))
def sep(): return map(int, inp().split())
def strsep(): return map(str, inp().split())
# def graph(vertex): return [[] for i in range(0,vertex+1)]
def zerolist(n): return [0]*n
def nextline(): out("\n")  #as stdout.write always print sring.
def testcase(t):
    for p in range(t):
        solve()
def printlist(a) :
    for p in range(0,len(a)):
        out(str(a[p]) + ' ')
def lcm(a,b): return (a*b)//gcd(a,b)
def power(a,b):
    ans = 1
    while(b>0):
        if(b%2==1):
            ans*=a
        a*=a
        b//=2
    return ans
def ncr(n,r): return factorial(n)//(factorial(r)*factorial(max(n-r,1)))
def isPrime(n) : # Check Prime Number or not
    if (n <= 1) : return False
    if (n <= 3) : return True
    if (n % 2 == 0 or n % 3 == 0) : return False
    i = 5
    while(i * i <= n) :
        if (n % i == 0 or n % (i + 2) == 0) :
            return False
        i = i + 6
    return True

#===============================================================================================
# code here ;))
def bs(a,l,h,x):
    while(l<h):
        # print(l,h)
        mid = (l+h)//2
        if(a[mid] == x):
            return mid
        if(a[mid] < x):
            l = mid+1
        else:
            h = mid
    return l

def sieve(a): #O(n loglogn) nearly linear
    #all odd mark 1
    for i in range(3,((10**6)+1),2):
        a[i] = 1
    #marking multiples of i form i*i 0. they are nt prime
    for i in range(3,((10**6)+1),2):
        for j in range(i*i,((10**6)+1),i):
            a[j] = 0
    a[2] = 1 #special left case
    return (a)


def bfs(g,st):
    visited = [-1]*(len(g))
    visited[st] = 0
    queue = []
    queue.append(st)
    new = []
    while(len(queue) != 0):
        s = queue.pop()
        new.append(s)
        for i in g[s]:
            if(visited[i] == -1):
                visited[i] = visited[s]+1
                queue.append(i)
    return visited


def dfsusingstack(v,st):
    d = deque([])
    visited = [0]*(len(v))
    d.append(st)
    new = []
    visited[st] = 1
    while(len(d) != 0):
        curr = d.pop()
        new.append(curr)
        for i in v[curr]:
            if(visited[i] == 0):
                visited[i] = 1
                d.append(i)
    return new

# 6
# 1 -1 2 3 -5 0
def solve():
    n,m,k = sep()
    a = lis()
    b = lis()

    prea = []
    preb = []
    for i in range(n):
        if(i==0):
            prea.append(a[i])
            continue
        prea.append(a[i] + prea[i-1])
    for i in range(m):
        if(i==0):
            preb.append(b[i])
            continue
        preb.append(b[i] + preb[i-1])
    # print(prea)
    # print(preb)
    ans  = 0
    for i in range(0,n):
        if(prea[i]<=k):
            ind = bl(preb,k-prea[i])
            if(ind<m and preb[ind] == k-prea[i]):
                ans = max(ans,i+ind+2)
            else:
                ans = max(ans,i+ind+1)
            # print(i,ind)
    for i in range(m):
        if(preb[i] <=k):
            ind = bl(prea,k-preb[i])
            if(ind<n and prea[ind] == k-preb[i]):
                ans = max(ans,i+ind+2)
            else:
                ans = max(ans,i+ind+1)
                
    print(ans)



testcase(1)
# testcase(int(inp()))

Submission status: Accepted

Solution code:
import sys 
import numpy as np
import bisect as bs 

def binarySearch_canread(data, target):
    if len(data) == 0:
        return None
    if len(data) == 1:
        return data[0]

    # 追加　全部読める場合と何も読めない場合
    if target > max(data):
        return len(data)
    elif target < min(data):
        return 0

    min_diff = float('inf')
    imin = 0
    imax = len(data) - 1
    closest_num = None

    while imin <= imax:
        imid = imin + (imax - imin) // 2

        if imid + 1 < len(data):
            min_diff_right = abs(data[imid + 1] - target)
        if imid > 0:
            min_diff_left = abs(data[imid - 1] - target)

        if min_diff_left < min_diff:
            min_diff = min_diff_left 
            closest_num = data[imid - 1]
        if min_diff_right < min_diff:
            min_diff = min_diff_right
            closest_num = data[imid + 1]

        if data[imid] < target:
            imin = imid + 1
        elif data[imid] > target:
            imax = imid - 1
        else:
            return list(data).index(data[imid]) + 1 # data[imid]

    # 何冊読めるかを返したい
    differ = target - closest_num
    can_read = None
    if differ > 0: # 読める時間（target）の方が大きい
        can_read = max([i for i, x in enumerate(data) if x == closest_num]) + 1
    elif differ < 0: # 読める時間の方が少ない
        can_read = min([i for i, x in enumerate(data) if x == closest_num])



    return can_read

if __name__ == '__main__':
    read = sys.stdin.buffer.read 
    readline = sys.stdin.buffer.readline
    readlines = sys.stdin.buffer.readlines 

    N, M, K = map(int, readline().split())
    A = np.array(readline().split(), np.int64)
    B = np.array(readline().split(), np.int64)

    Acum = np.zeros(N+1, np.int64)
    Acum[1:] = np.cumsum(A)
    Bcum = np.zeros(M+1, np.int64)
    Bcum[1:] = np.cumsum(B)

    # Aから1冊ずつ取っていく
    max_read = 0
    # O(N * logN)
    for i, cumNum in enumerate(Acum): # Aを1つも撮らない場合もあるので0から
        if cumNum <= K:
            limit = K - cumNum
            # O(logN)
            can_reading = i + bs.bisect_right(Bcum[1:], limit)
            if (max_read < can_reading):
                max_read = can_reading

    print(max_read)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
from collections import deque


def II(): return int(sys.stdin.readline())


def MI(): return map(int, sys.stdin.readline().split())


def LI(): return list(map(int, sys.stdin.readline().split()))


def main():
    n, m, k = MI()
    a = deque(LI())
    b = deque(LI())

    A, B = [0], [0]

    for i in range(len(a)):
        A.append(A[i]+a[i])
    for i in range(len(b)):
        B.append(B[i]+b[i])

    index = m
    ans = [0]
    for i in range(len(a)+1):
        rest = k - A[i]
        if rest < 0:
            break

        while B[index] > rest:
            index -= 1

        ans.append(i + index)

    print(max(ans))


    # oj t -c "pypy3 main.py"
    # acc s main.py -- --guess-python-interpreter pypy
main()

Submission status: Accepted

Solution code:
# ABC172 C
import sys
n, m, k = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

A_CumulativeSum = [0]
B_CumulativeSum = [0]
for i in range(n):
    A_CumulativeSum.append(A[i] + A_CumulativeSum[i])
for i in range(m):
    B_CumulativeSum.append(B[i] + B_CumulativeSum[i])

if A_CumulativeSum[n-1] + B_CumulativeSum[m-1] <= k:
    print(n+m)
    sys.exit()

ans = 0
j = m
for i in range(n+1):
    time = A_CumulativeSum[i]
    if time > k:
        break
    time_b = k - time
    while time_b < B_CumulativeSum[j]:
        j -= 1

    count = i + j
    ans = max(count, 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()))
an = 0
bn = 0
t = 0
for an in range(n):
  if t+a[an] <= k:
    t += a[an]
  else:
    an-=1
    break
an+=1
ans = an
while True:
  while bn < m:
    if t+b[bn] <= k:
      t += b[bn]
      bn += 1
    else:
      break
  ans = max(ans,an+bn)
  if an == 0 or bn == m:
    break
  else:
    an -= 1
    t -= a[an]
print(ans)

Submission status: Accepted

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

A = [int(x) for x in input().split()]
B = [int(x) for x in 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 math import factorial,sqrt,ceil #,gcd
# from itertools import permutations,combinations,combinations_with_replacement
# from collections import deque,Counter
# from bisect import bisect_left
# from heapq import heappush,heappop
# from numba import njit
# from functools import lru_cache # 簡単メモ化 @lru_cache(maxsize=1000)
# from fractions import gcd

# from decimal import Decimal, getcontext
# # getcontext().prec = 1000
# # eps = Decimal(10) ** (-100)

import numpy as np    # numpy.lcm()
# from scipy.sparse.csgraph import shortest_path, dijkstra, floyd_warshall, bellman_ford, johnson
# from scipy.sparse import csr_matrix
# from scipy.special import comb,perm #permはnPk
# import networkx as nx
# G = Graph()

# slist = "abcdefghijklmnopqrstuvwxyz"
MOD = 10**9 + 7
N,M,K = map(int,input().split())
# lisA = list(map(int,input().split()))
arrA = np.array([0] + list(map(int,input().split())))
arrB = np.array([0] + list(map(int,input().split())))

arrA = np.cumsum(arrA,dtype="int64")
arrB = np.cumsum(arrB,dtype="int64")

# memo = np.add.outer(arrA,arrB) <= K
j = M
ans = 0
for i in range(N+1):
    while j >= 0:
        if arrA[i]+arrB[j] <= K:
            ans = max(i+j,ans)
            break
        j -= 1

print(ans)
# for row in board:
#     print(*row,sep="")    #unpackして間にスペース入れずに出力する
# print("{:.10f}".format(ans))
# print("{:0=10d}".format(ans))

Submission status: Accepted

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

i_l, j_l = [0], [0]

best0 = M
ans = 0

for i in range(N):
    i_l.append(i_l[i] + a_l[i])

for i in range(M):
    j_l.append(j_l[i] + b_l[i])

# for j in range(M):
#     total += b_l[j]
#     if total > K:
#         best0 = j-1
#         break
#     elif total == K:
#         best0 = j
#     else:
#         continue

for k in range(N+1):
    if i_l[k] > K:
        break
    while i_l[k] + j_l[best0] > K:
        best0 -= 1
    ans = max(ans, k+best0)
print(ans)

Submission status: Accepted

Solution code:
import numpy

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

acc_a = [0]
for i in range(N):
    acc_a.append(acc_a[i] + A[i])

acc_b = [0]
for i in range(M):
    acc_b.append(acc_b[i] + B[i])

acc_a = numpy.array(acc_a)
acc_b = numpy.array(acc_b)

currentMax = 0
bnum = M
for anum in range(N + 1):
    if acc_a[anum] > K:
        break

    while True:
        if acc_b[bnum] > K - acc_a[anum]:
            bnum = bnum - 1 
        else:
            break

    currentMax = max(currentMax, anum + bnum)


print(currentMax)

Submission status: Accepted

Solution code:
import bisect
n, m, k = map(int, input().split())
*A, = map(int, input().split())
*B, = map(int, input().split())
SA, SB = [0], [0]
for i in range(n):
    SA.append(SA[i] + A[i])
for j in range(m):
    SB.append(SB[j] + B[j])
ans = 0
for i in range(n + 1):
    if SA[i] <= k:
        j = bisect.bisect_right(SB, k - SA[i]) - 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()))

sum_a = [0]
sum_b = [0]

for i in range(n):
    sum_a.append(sum_a[-1] + a[i])

for i in range(m):
    sum_b.append(sum_b[-1] + b[i])

ans = 0
j = m
for i in range(n + 1):
    while sum_a[i] + sum_b[j] > k and j >= 0:
        j -= 1

    if j < 0:
        break

    if sum_a[i] + sum_b[j] <= k:
        ans = max(ans, i + j)

print(ans)

Submission status: Accepted

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

a=[0]
b=[0]
for i in range(N):
    a.append(a[i]+A[i])
for j in range(M):
    b.append(b[j]+B[j])
ans = 0
l = M
for k in range(N+1):
    if a[k] > K:
        break
    while b[l] > K - a[k]:
        l -= 1
    ans = max(ans,k+l)
print(ans)

Submission status: Accepted

Solution code:
N,M,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
a, b = [0], [0]
for i in range(N):
  a.append(a[i] + A[i])
for i in range(M):
  b.append(b[i] + B[i])
res = 0
output = 0
j = M
num = 0
for i in range(N + 1):
  if K < a[i]:
    break
  else:
    res = K - a[i]
  while b[j] > res:
    j -= 1
  num = i + j 
  output = max(output, num)
print(output)

Submission status: Accepted

Solution code:
from itertools import accumulate
n,m,k=map(int,input().split())
A=list(accumulate(map(int,input().split()),initial=0))
B=list(accumulate(map(int,input().split()),initial=0))
r,j=0,m
for i in range(n+1):
  if A[i]>k:
    break
  while A[i]+B[j]>k:
    j-=1
  r=max(r,i+j)
print(r)

Submission status: Accepted

Solution code:
# C - Tsundoku
def calc_cusum(n):
	cusum = [0] * (n + 1)
	arr = input().split()
	
	for i in range(n):
		cusum[i + 1] = cusum[i] + int(arr[i])
	
	return cusum


def get_max_book(cusum):
  global k
  
  for i in range(len(cusum)):
    if cusum[i] > k:
      return i -1
  
  return len(cusum) - 1


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

ans = 0
j = get_max_book(b)

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

print(ans)

Submission status: Accepted

Solution code:
# C - Tsundoku
def calc_cusum(n):
	cusum = [0] * (n + 1)
	arr = input().split()
	
	for i in range(n):
		cusum[i + 1] = cusum[i] + int(arr[i])
	
	return cusum


def identify_position(i, j):
  global a
  global b
  global k
  global m
  
  if a[i] + b[j] > k:
    while j > 0:
      j -= 1
      
      if a[i] + b[j] <= k:
        break
  else:
    while j < m and a[i] + b[j+1]<= k:
      j += 1
  
  return j


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

ans = 0
j = 0

for i in range(n + 1):
  j = identify_position(i, j) 
  
  #print(a[i])
  #print(b[j])
  #print(i + j)
  
  if a[i] + b[j] <= k:
    ans = max(ans, i+j)

print(ans)

Submission status: Accepted

Solution code:
import itertools

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


A_accum = [0] + list(itertools.accumulate(A))
B_accum = [0] + list(itertools.accumulate(B))

book_num = 0

b_best = M
for n in range(N + 1):
    for m in range(b_best, -1, -1):
        if A_accum[n] + B_accum[m] <= K:
            b_best = m
            if book_num < n + m:
                book_num = n + m
            break

print(book_num)

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 = 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:
N, M, K = input().split()
N, M, K = [int(N), int(M), int(K)]
A = list(map(int, input().split()))
B = list(map(int, input().split()))
SA = [0]
for i in range(N):
    SA.append(SA[i] + A[i])
SB = [0]
for i in range(M):
    SB.append(SB[i] + B[i])
ans = 0
j = M
for i in range(N + 1):
    while j > 0 and SA[i] + SB[j] > K:
        j -= 1
    if SA[i] + SB[j] <= K:
        ans = max(ans, i + j)
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()))

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

MAX = 0
j = M

for i in range(N+1):
    if ruiA[i] > K:
        break
    while ruiA[i] + ruiB[j] > K:
        j -= 1
    MAX = max(MAX, i+j)

print(MAX)

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

aa = list(accumulate(a))
aa.insert(0,0)
bb = list(accumulate(b))
bb.insert(0,0)

ans = 0

for i in range(n+1):
    if aa[i] > k:
        break
    nokori = k-aa[i]
    kazu = bisect.bisect_right(bb, nokori)
    ans = max(ans,i+kazu-1)



print(ans)

Submission status: Accepted

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

a, b = [0], [0]
for i in range(N):
        a.append(a[i]+A[i])
for i in range(M):
        b.append(b[i]+B[i])

ans, j = 0, M

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

print(ans)