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: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from collections import deque
from functools import lru_cache
import bisect
import re
import queue
import decimal


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [Scanner.string() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [Scanner.int() for i in range(n)]


MOD = int(1e09) + 7
INF = int(1e15)


def solve():
    N, M, K = Scanner.map_int()
    A = Scanner.map_int()
    B = Scanner.map_int()
    time = 0
    ans = 0
    i = 0
    j = 0
    while time < K:
        if i >= N and j >= M:
            break
        elif i >= N:
            if time + B[j] > K:
                break
            time += B[j]
            ans += 1
            j += 1
        elif j >= M:
            if time + A[i] > K:
                break
            time += A[i]
            ans += 1
            i += 1
        else:
            if A[i] < B[j]:
                if time + A[i] > K:
                    break

                time += A[i]
                ans += 1
                i += 1
            else:
                if time + B[j] > K:
                    break

                time += B[j]
                ans += 1
                j += 1
    print(ans)


def main():
    # sys.setrecursionlimit(1000000)
    # sys.stdin = open("sample.txt")
    # T = Scanner.int()
    # for _ in range(T):
    #     solve()
    # print('YNeos'[not solve()::2])
    solve()


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**6)
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n,m,k = na()
a = na()
b = na()
c = a+b
c.sort()
tl = 0

for i in range(n+m):
    tl += c[i]
    if tl > k:
        print(i)
        exit()

print(n+m)

Submission status: Wrong Answer

Solution code:
N, M, K = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]+[10**18]
B = [int(x) for x in input().split()]+[10**18]
A.reverse()
B.reverse()

count = 0
total = 0
while True:
	if A[-1] <= B[-1] and total+A[-1] <= K:
		total += A.pop()
		count += 1
	elif B[-1] <= A[-1] and total+B[-1] <= K:
		total += B.pop()
		count += 1
	else:
		break
print(count)

Submission status: Wrong Answer

Solution code:
N, M, K = map(int, input().split())
As = list(map(int, input().split())) + [10 ** 9 + 7]
Bs = list(map(int, input().split())) + [10 ** 9 + 7]

time_sum = 0
A_i = 0
B_i = 0
while True:
    if As[A_i] < Bs[B_i]:
        if time_sum + As[A_i] <= K:
            time_sum += As[A_i]
            A_i += 1
        else:
            break
    else:
        if time_sum + Bs[B_i] <= K:
            time_sum += Bs[B_i]
            B_i += 1
        else:
            break

print(A_i + B_i)

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n,m,k = LI()
    a = LI()
    qa = deque()
    for i in a:
        qa.append(i)
    b = LI()
    qb = deque()
    for i in b:
        qb.append(i)
    ans = 0
    while (qa or qb) and k >= 0:
        if qa:
            if qb:
                if qa[0] < qb[0]:
                    x = qa.popleft()
                else:
                    x = qb.popleft()
            else:
                x = qa.popleft()
        else:
            x = qb.popleft()
        if x > k:
            break
        k -= x
        ans += 1
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import accumulate, permutations, combinations, product, groupby, combinations_with_replacement
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

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

A = A[::-1]
B = B[::-1]

tmp = 0
ans = 0
while tmp < K:
    if A and B:       
        if A[-1] < B[-1]:
            tmp += A.pop()
        else:
            tmp += B.pop()
    elif A:
        tmp += A.pop()
    elif B:
        tmp += B.pop()
    else:
        break

    if tmp > K:
        break
    ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
N,M,K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
INF=10**12
A.append(INF)
B.append(INF)
for i in range(N+M+1):
  if A[0]<=B[0]:
    if K<A[0]:
      print(i)
      exit()
    K-=A[0]
    del A[0]
  else:
    if K<B[0]:
      print(i)
      exit()
    K-=B[0]
    del B[0]

Submission status: Wrong Answer

Solution code:
import sys, os, math, bisect, itertools, collections, heapq, queue
# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from decimal import Decimal
from collections import defaultdict

# import fractions

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)
# lcm = lambda x, y: (x * y) // fractions.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N, M, K = il()
    A = il()
    B = il()
    AB = sorted(A + B)

    ret = 0
    time = 0
    for ab in AB:
        if time + ab <= K:
            ret += 1
            time += ab
        else:
            break
    print(ret)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# import re
# from scipy.sparse.csgraph import connected_components  # Pythonのみ！
# ↑cf.  https://note.nkmk.me/python-scipy-connected-components/
# from scipy.sparse import csr_matrix
# import statistics # Pythonのみ
# import string
import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

def resolve():
    
    n,m,k=map(int,input().split())
    A=list(map(int,input().split()))
    B=list(map(int,input().split()))
    A+=B
    A.sort()
    val=0
    cnt=0
    for i in A:
        val+=i
        if val<=k:
                cnt+=1
    print(cnt)

        
    
resolve()

Submission status: Wrong Answer

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

a.append(1e10)
b.append(1e10)

aind = 0
bind = 0

time = 0
ans = 0
while True:
    ca = a[aind]
    cb = b[bind]

    if ca < cb:
        if time + ca <= k:
            ans += 1
            time += ca
            aind += 1
        else:
            break
    else:
        if time + cb <= k:
            ans += 1
            time += cb
            bind += 1
        else:
            break

print(ans)

Submission status: Wrong Answer

Solution code:
n,m,k=map(int,input().split())
a=list(map(int,input().split()))+[10**20]
b=list(map(int,input().split()))+[10**20]
ans=0
i=0
j=0
while k:
  if k<min(a[i],b[j]):
    break
  if a[i]<b[j]:
    k-=a[i]
    i+=1
    ans+=1
  else:
    k-=b[j]
    j+=1
    ans+=1
print(ans)

Submission status: Wrong Answer

Solution code:
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a.extend(b)
a.sort()
ans = 0
tot = 0
for i in range(n+m):
    if tot + a[i] <= k:
        ans += 1
        tot += a[i]
    else:
        break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,m,k=list(map(int, input("").split()))
a=list(map(int, input("").split()))
b=list(map(int, input("").split()))
ar=[0]
br=[0]
for i in a:
    o=ar[-1]+i
    if o > k:
        break
    else:
        ar.append(o)
for i in b:
    o=br[-1]+i
    if o > k:
        break
    else:
        br.append(o)
o=0
for i in range(len(ar)):
    for j in range(len(br)):
        abr=ar[i]+br[j]
        if abr <=k:
            m=i+j
            if m>o:
                o=m
print(o)

Submission status: Time Limit Exceeded

Solution code:
n,m,k=list(map(int, input("").split()))
a=[0]+list(map(int, input("").split()))
b=[0]+list(map(int, input("").split()))
o=0
ar=0
for i in range(n+1):
    ar+=a[i]
    abr=ar
    if abr > k:
        break
    for j in range(m+1):
        abr+=b[j]
        if abr <= k: 
            if o< i+j:
                o=i+j
        elif abr > k:
            break
print(o)

Submission status: Time Limit Exceeded

Solution code:
from itertools import accumulate

N, M, K = map(int, input().split())
A=[0]+list(accumulate(map(int,input().split())))
B=[0]+list(accumulate(map(int,input().split())))

X_sum = 0
Y_num = [0]*(N+1)

II = 0

for i in range(N+1):
    if A[i] <= K:
        II = i
        Y_num[II] += II
        for j in range(M+1):
            if K - A[II] - B[j] < 0:
                Y_num[II] += j - 1
                break
            elif j == M:
                Y_num[II] += M
    else:
        break

print(max(Y_num))

Submission status: Time Limit Exceeded

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

def f(data,sup):
  sum=0
  u=0
  for i in range(len(data)):
    if sum+data[i]<=sup:
      sum+=data[i]
      u+=1
    else:
      break
  return u

max=0
for i in range(k+1):
  if max<f(A,i)+f(B,k-i):
    max=f(A,i)+f(B,k-i)
print(max)

Submission status: Time Limit Exceeded

Solution code:
N,M,K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
a = [0]
b = [0]

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

Submission status: Time Limit Exceeded

Solution code:
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
assert len(A) == N
assert len(B) == M
# K分以内に読む数の最大化
import numpy as np
cumA = np.cumsum([0] + A)
cumB = np.cumsum( B)
maximum = 0
for maxA,timeA in enumerate(cumA):
  if timeA > K:
     break
  timeB = K - timeA
  maxB = np.sum(timeA + cumB <= K)
  maximum = max(maximum, maxA + maxB)
print(maximum)

Submission status: Time Limit Exceeded

Solution code:
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
assert len(A) == N
assert len(B) == M
# K分以内に読む数の最大化

cumA = []
cumB = []
sumA = 0
sumB = 0
for a in A:
  sumA += a
  cumA.append(sumA)
for b in B:
  sumB += b
  cumB.append(sumB)

maximum = 0
for maxA,timeA in enumerate([0]+cumA):
  if timeA > K:
     break
  timeB = K - timeA
  for maxB,timeB in enumerate([0]+cumB):
     if timeA + timeB > K:
        maxB -= 1
        break
  maximum = max(maximum, maxA + maxB)
print(maximum)

Submission status: Time Limit Exceeded

Solution code:
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
assert len(A) == N
assert len(B) == M
# K分以内に読む数の最大化

cumA = []
cumB = []
sumA = 0
sumB = 0
for a in A:
  sumA += a
  cumA.append(sumA)
for b in B:
  sumB += b
  cumB.append(sumB)

maximum = 0
for maxA,timeA in enumerate([0]+cumA):
  if timeA > K:
     break
  timeB = K - timeA
  maxB = [i for i,b in enumerate(cumB) if b <= timeB]
  maxB = max(maxB) + 1 if len(maxB) else 0
  maximum = max(maximum, maxA + maxB)
print(maximum)

Submission status: Time Limit Exceeded

Solution code:
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
assert len(A) == N
assert len(B) == M
# K分以内に読む数の最大化

cumA = []
cumB = []
sumA = 0
sumB = 0
for a in A:
  sumA += a
  cumA.append(sumA)
for b in B:
  sumB += b
  cumB.append(sumB)

maximum = 0
for maxA,timeA in enumerate([0]+cumA):
  if timeA <= K:
   timeB = K - timeA
   maxB = [i for i,b in enumerate(cumB) if b <= timeB]
   maxB = max(maxB) + 1 if len(maxB) else 0
   maximum = max(maximum, maxA + maxB)
print(maximum)

Submission status: Time Limit Exceeded

Solution code:
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
assert len(A) == N
assert len(B) == M
# K分以内に読む数の最大化

cumA = []
cumB = []
sumA = 0
sumB = 0
for a in A:
  sumA += a
  cumA.append(sumA)
for b in B:
  sumB += b
  cumB.append(sumB)

maximum = 0
for n in range(1, K+1, 1):
  timeA = n
  timeB = K - n
  maxA = [i for i,a in enumerate(cumA) if a <= timeA]
  maxB = [i for i,b in enumerate(cumB) if b <= timeB]
  maxA = max(maxA) + 1 if len(maxA) else 0
  maxB = max(maxB) + 1 if len(maxB) else 0
  maximum = max(maximum, maxA + maxB)
print(maximum)

Submission status: Time Limit Exceeded

Solution code:
N,M,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
book = [[0]*(N+1) for i in range(M+1)]
time = 0
for i in range(N):
    if time + A[i] <= K:
        time += A[i]
        book[0][i+1] = time
    else:
        a = i
        ans = i
        break
else:
    a = N
    ans = N
b = 0
for i in range(M):
    b += B[i]
    if i+1+a <= ans:
        continue
    if time + b <= K:
        book[i+1][a] = time + b
        ans = max(ans,i+a+1)
    else:
        for j in range(a,-1,-1):
            if i + j + 1 <= ans:
                a = j
                time = book[0][a]
                break
            if book[0][j] + b <= K:
                time = book[0][j]
                book[i+1][j] = time + b
                a = j
                ans = max(ans,i+1+j)
                break
        else:
            break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,M,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
book = [[0]*(N+1) for i in range(M+1)]
for i in range(N):
    time = book[0][i] + A[i]
    if time <= K:
        book[0][i+1] = time
    else:
        a = i
        ans = i
        break
else:
    a = N
    ans = N
b = 0
for i in range(M):
    b += B[i]
    if book[i][a] + B[i] <= K:
        book[i+1][a] = book[i][a] + B[i]
        ans = max(ans,i+a+1)
    else:
        for j in range(a,-1,-1):
            time = book[0][j] + b
            if time <= K:
                book[i+1][j] = time
                a = j
                ans = max(ans,i+1+j)
                break
        else:
            break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':

    n,m,k = map(int,input().split())
    A = list(map(int,input().split()))
    B = list(map(int,input().split()))
    
    AA = [0]
    for i in range(n):
        AA.append(AA[i]+A[i])

    BB = [0]
    for j in range(m):
        BB.append(BB[j]+B[j])

    tmp = 0
    ans = 0
    for a in range(n+1):
        for b in range(m+1):
            tmp0 = AA[a] + BB[b]
            #最大時間を超えたらおしまい
            if k < tmp0:
                break
            #同じ時間でも冊数が多いほうが良い
            if tmp <= tmp0:
                tmp = tmp0
                if ans <= a + b:
                    ans = a + b

    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import accumulate
n,m,k = map(int,input().split())
deskA = list(map(int,input().split()))
deskB = list(map(int,input().split()))
cumA = list(accumulate(deskA))
cumB = list(accumulate(deskB))
cumA = [0] + cumA
cumB = [0] + cumB
ans = 0
for i in range(n+1):
    tmp = k - cumA[i]
    if tmp<0:
        continue
    j = m
    tmp -= cumB[j]
    while tmp < 0:
        tmp += cumB[j]
        j -= 1
        tmp -= cumB[j]
    ans = max(i+j,ans)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
A = [0]*(n+1)
B = [0]*(m+1)
cnt  =0

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

for i in range(n+1):
    for j in range(m+1):
        if A[i] + B[j] <= k:
            cnt = max(i+j, cnt)

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
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] + A[i]
    if A[i] > K:
        A = A[:i]
        break
A = [0] + A

for i in range(1, M):
    B[i] = B[i - 1] + B[i]
    if B[i] > K:
        B = B[:i]
        break
B = [0] + B

A_length = len(A)
B_length = len(B)

for i in range(A_length):
    j = 0
    while j < B_length:
        if A[i] + B[j] <= K:
            j += 1
        else:
            break
    ans = max(ans, i + (j - 1))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque
imput = sys.stdin.readline

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

aa = [0]
bb = [0]

for i in range(n):
    aa.append(aa[i] + a[i])
    
for i in range(m):
    bb.append(bb[i] + b[i])
    
ans = 0

for i in range(n+1):
    time = k - aa[i]
    if time < 0:
      continue
    cnt = i
    for j in range(m+1):
        if time < bb[j]:
          cnt += j - 1
          break
        elif j == m:
          cnt += m
          break
    if ans < cnt:
        ans = cnt
        
print(ans)

Submission status: Time Limit Exceeded

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

time_sum_list = [0] * (N + 1)
i = 1
while i <= N:
    time_sum_list[i] = A_time_list[i - 1] + time_sum_list[i - 1]
    if time_sum_list[i] > K:
        break
    i += 1

count_list = [0] * i

for j in range(i):
    k = 0
    time_sum = time_sum_list[j]
    while k < M:
        time_sum += B_time_list[k]
        if time_sum > K:
            break
        k += 1
    count_list[j] = k + j

print(max(count_list))

Submission status: Time Limit Exceeded

Solution code:
n_a, n_b, time = map(int, input().split())
book_a = input().split()
book_b = input().split()

sum_time = 0
best_j = n_b
for jndex in range(n_b):
  sum_time += int(book_b[jndex])
  if sum_time > time:
    sum_time -= int(book_b[jndex])
    best_j = jndex
    break
best_books = best_j
book_b_inv = book_b[:best_j]
book_b_inv = book_b_inv[::-1]

for index in range(n_a):
  sum_time += int(book_a[index])
  
  # 入れても時間オーバーしない
  if sum_time <= time:
    best_books += 1
    continue
  
  # 減られる本がもういない
  if best_j == 0:
    best_books = best_books if best_books > index else index
    break
    
  for jndex in range(len(book_b_inv)):
    sum_time -= int(book_b_inv[jndex])
    # 本を減ったら時間内に抑えるなら（全部減った含む）
    if sum_time <= time:
      book_b_inv = book_b_inv[jndex+1:]
      best_books = best_books if best_books > len(book_b_inv) + index + 1  else len(book_b_inv) + index + 1
      break
  
  # 本を減ったら時間内に抑えないなら
  if sum_time > time:
    break

print(best_books)

Submission status: Time Limit Exceeded

Solution code:
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = [0]*(n+1)
d = [0]*(m+1)
for i in range(n):
    c[i+1] = a[i]+c[i]
for i in range(m):
    d[i+1] = b[i]+d[i]
ans = 0
for i in range(n+1)[::-1]:
    for j in range(m+1)[::-1]:
        if c[i] + d[j] <= k:
            ans = max(ans,i+j)
            break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = [0]*(n+1)
d = [0]*(m+1)
for i in range(n):
    c[i+1] = a[i]+c[i]
for i in range(m):
    d[i+1] = b[i]+d[i]
ans = 0
for i in range(n+1)[::-1]:
    for j in range(m+1)[::-1]:
        if c[i] + d[j] <= k:
            ans = max(ans,i+j)
            break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

ans = 0

for books in itertools.product(range(2), repeat=(n + m)):
    if len([i for i in books if i == 0]) == n:
        time = 0
        a_cnt = n
        b_cnt = m
        for book in books:
            if book == 0 and (time + a[n - a_cnt]) <= k:
                time += a[n - a_cnt]
                a_cnt -= 1
            elif book == 1 and (time + b[m - b_cnt]) <= k:
                time += b[n - b_cnt]
                b_cnt -= 1
            else:
                break
        if (n + m - a_cnt - b_cnt) > ans:
            ans = n + m - a_cnt - b_cnt
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    line1 = input()
    line2 = input()
    line3 = input()
    n, m, k = list(map(int, line1.split()))
    a = [0] + list(map(int, line2.split()))
    b = [0] + list(map(int, line3.split()))
    mark1, mark2 = 0, 0
    for i in range(1, len(a)):
        a[i] += a[i-1]
        if a[i] > k:
            mark1 = i
            break
    for i in range(1, len(b)):
        b[i] += b[i-1]
        if b[i] > k:
            mark2 = i
            break
    if mark1:
        a = a[:mark1]
    if mark2:
        b = b[:mark2]
    ans = 0
    for i in range(len(a)):
        j = 0
        flag = 0
        while j < len(b):
            if b[j] > k - a[i]:
                flag = 1
                j += 1
                break
            j += 1
        ans = max(ans, j - flag + i - 1)
        # print(ans, i, j)
    print(ans)
    return

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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
import bisect
cumsum_a = list(accumulate(a))
cumsum_b = list(accumulate(b))

ans = 0
for i in range(n+1):
    cnt_a = bisect.bisect(cumsum_a[:i], k)
    time = k - cumsum_a[cnt_a - 1]
    cnt_b = bisect.bisect(cumsum_b, time)
    
    ans = max(ans, cnt_a + cnt_b)

for i in range(m+1):
    cnt_b = bisect.bisect(cumsum_b[:i], k)
    time = k - cumsum_b[cnt_b - 1]
    cnt_a = bisect.bisect(cumsum_a, time)
    
    ans = max(ans, cnt_a + cnt_b)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
memo = {}

def get(self, a: list, b: list):
    pass

def main():
    line1 = input()
    line2 = input()
    line3 = input()
    
    n, m, k = list(map(int, line1.split()))
    a = list(map(int, line2.split()))
    b = list(map(int, line3.split()))
    memo[(0,0)] = 0
    for i in range(len(a)):
        memo[(i+1,0)] = memo[(i,0)] + a[i]
    for i in range(len(b)):
        memo[(0,i+1)] = memo[(0,i)] + b[i]
    for i in range(len(a)):
        for j in range(len(b)):
            memo[(i+1,j+1)] = memo[(0,j+1)] + memo[(i+1,0)]
    book = 0
    for i in range(len(a)+1):
        for j in range(len(b)+1):
            if i + j > book and memo[(i,j)] <= k:
                book = i + j
    print(book)
    return

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

    total_T_in_A = [0] * (N+1)
    # total_T_in_B = [0] * M
    # total_T_in_B[0] = B[0]
    total_T_in_B = [0] * (M+1)

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

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

    result = 0
    for i in range(N+1):
        # A から i 冊読むときにかかる時間
        i_A_T = total_T_in_A[i]
        
        if K < i_A_T:
            continue

        if K == i_A_T:
            result = max(result, i)
            continue

        rem_T = K - i_A_T

        # total_T_in_B は累積和を格納した、ソート済の配列
        # B_i <= rem_T < B_i+1 となるような B_i を二分探索によって探す

        total_T_in_B_with_INF = total_T_in_B + [10**15]

        first = 0
        last = M + 1
        # total_T_in_B_with_INF[first] <= rem_T < total_T_in_B_with_INF[last] となるような first を二分探索によって探す
        while first < last:
            if abs(last - first) == 1:
                break
            mid = (first + last) // 2
            if rem_T < total_T_in_B_with_INF[mid]:
                last = mid
            else:
                first = mid
        result = max(result, i + first)

    print(result)

main()

Submission status: Time Limit Exceeded

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

    total_T_in_A = [0] * (N+1)
    # total_T_in_B = [0] * M
    # total_T_in_B[0] = B[0]
    total_T_in_B = [0] * (M+1)

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

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

    result = 0
    for i in range(N+1):
        # A から i 冊読むときにかかる時間
        i_A_T = total_T_in_A[i]
        
        if K < i_A_T:
            continue

        if K == i_A_T:
            result = max(result, i)
            continue

        rem_T = K - i_A_T

        # total_T_in_B は累積和を格納した、ソート済の配列
        # B_i <= rem_T < B_i+1 となるような B_i を二分探索によって探す

        total_T_in_B_with_INF = total_T_in_B + [10**18]

        first = 0
        last = M + 1
        # total_T_in_B_with_INF[first] <= rem_T < total_T_in_B_with_INF[last] となるような first を二分探索によって探す
        while first < last:
            if abs(last - first) == 1:
                break
            mid = (first + last) // 2
            if rem_T < total_T_in_B_with_INF[mid]:
                last = mid
            else:
                first = mid
        result = max(result, i + first)

    print(result)

main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N,M,K= map(int, input().split())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
Asummax=sum(A)
Bsummax=sum(B)
if Asummax+Bsummax<=K:
  print(N+M)
  exit()

Asum=[0]*(N+1)
for i in range(N+1):
  Asum[i]=sum(A[0:i])
  if Asum[i]>K:
    Asum[i]=0
    Amax=i-1
    break
  Amax=i

Bsum=[0]*(M+1)
for i in range(M+1):
  Bsum[i]=sum(B[0:i])
  if Bsum[i]>K:
    Bsum[i]=0
    Bmax=i-1
    break
  Bmax=i

Csum=[0]*(Amax+1)*(Bmax+1)
for i in range(Amax+1):
  for j in range(Bmax+1):
    if Asum[i]+Bsum[j]>K:
      Csum[i*(Bmax+1)+j]=0
    else:Csum[i*(Bmax+1)+j]=Asum[i]+Bsum[j]

Cmax=max(Csum)
D=np.array([i for i, x in enumerate(Csum) if x == Cmax])
D=D//(Bmax+1)+D%(Bmax+1)
print(max(D))

Submission status: Time Limit Exceeded

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

Asum=[0]*(N+1)
for i in range(N+1):
  Asum[i]=sum(A[0:i])
  if Asum[i]>K:
    Asum[i]=0
    Amax=i-1
    break
  Amax=i

Bsum=[0]*(M+1)
for i in range(M+1):
  Bsum[i]=sum(B[0:i])
  if Bsum[i]>K:
    Bsum[i]=0
    Bmax=i-1
    break
  Bmax=i
  
Csum=[0]*(Amax+1)*(Bmax+1)
for i in range(Amax+1):
  for j in range(Bmax+1):
    if Asum[i]+Bsum[j]>K:
      Csum[i*(Bmax+1)+j]=0
    else:Csum[i*(Bmax+1)+j]=Asum[i]+Bsum[j]

Cmax=max(Csum)
D=np.array([i for i, x in enumerate(Csum) if x == Cmax])
D=D//(Bmax+1)+D%(Bmax+1)
print(max(D))

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
    N, M, K = map(int, input().split()) 
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    
    acc_A = []
    acc_B = []
    acc_A.append(0)
    acc_B.append(0)
    
    sum_a = 0
    sum_b = 0

    for a in A:
        sum_a += a
        acc_A.append(sum_a)

    for b in B:
        sum_b += b
        acc_B.append(sum_b)

    large_ar = []
    small_ar = []

    if N > M:
        large_ar = acc_A
        small_ar = acc_B
    else:
        large_ar = acc_B
        small_ar = acc_A
    
    ans = 0
    for i in range(len(small_ar)):
        for j in range(len(large_ar)):
            if large_ar[j] + small_ar[i] <= K:
                ans = max(ans, i+j)
            else: break
    print(ans)