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: Time Limit Exceeded

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


def solve(N: int, M: int, K: int, A: "List[int]", B: "List[int]"):
    candidateA = []
    weightA = 0
    tmp_A = N
    for idx, a in enumerate(A):
        if weightA + a > K:
            tmp_A = idx
            break
        else:
            weightA += a
    candidateA = A[:tmp_A]
    max_count = current_count = len(candidateA)

    candidateB = []
    weightB = 0
    terminated = False
    for b in B:
        if terminated == True and weightA + weightB > K:
            break

        if len(candidateA)==0 and weightA + weightB + b > K:
            break

        if weightA + weightB + b <= K:
            weightB += b
            candidateB.append(b)
            current_count += 1

        elif terminated==False:
            while weightA + weightB + b > K:
                if len(candidateA)>0:
                    weightA -= candidateA.pop()
                    current_count -= 1
                else:
                    terminated=True

            if weightA + weightB + b <= K:
                weightB += b
                candidateB.append(b)
                current_count += 1
        max_count = max(max_count, current_count)

    print(max_count)
    return


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    B = [int(next(tokens)) for _ in range(M)]  # type: "List[int]"
    solve(N, M, K, A, B)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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 = [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])

flag = False
for i in range(1, N + M + 1):
    for a_count in range(0, min([i, N])+1):
        b_count = i - a_count
        if b_count > M or b_count < 0:
            continue
        if A[a_count] + B[b_count] <= K:
            flag = True
            break
    if flag:
        flag = False
        continue
    else:
        print(i - 1)
        exit()
print(N+M)

Submission status: Time Limit Exceeded

Solution code:
[N,M,K]=list(map(int, input().split()))
alist=list(map(int, input().split()))
blist=list(map(int, input().split()))
clist=alist+blist
clist.sort()

time=0
ans=-1
ci=0
while time<=K and clist != []:
    ans+=1
    time+=clist[0]
    del clist[0]
if clist==[]:
    ans+=1


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

ma=0
for x in range(len(a)+1):
    sa=sum(a[:x])
    for y in range(len(b)+1):
        if sa+sum(b[:y])>k:break
        ma=max(ma,x+y)
        
print(ma)

Submission status: Time Limit Exceeded

Solution code:
NMK = [int(i) for i in input().split(' ')]
A = [int(a) for a in input().split(' ')]
B = [int(a) for a in input().split(' ')]

# Aの最高冊数
A_max = 0
Ksum = 0
for i in range(NMK[0]):
    Ksum += A[i]
    if Ksum <= NMK[2]:
        A_max+=1
    else:
        break

# Bの最高冊数
B_max = 0
Ksum = 0
for i in range(NMK[1]):
    Ksum += B[i]
    if Ksum <= NMK[2]:
        B_max += 1
    else:
        break

books_max = max(A_max,B_max)
for i in range(A_max+1):
    Ksum = 0
    for j in range(B_max+1):
        for_s = sum(A[0:i])+sum(B[0:j])
        if for_s <= NMK[2]:
            if i+j > books_max:
                books_max = i+j

print(books_max)

Submission status: Time Limit Exceeded

Solution code:
def count_books(books, limit):
    num_books = len(books)
    total_time = 0
    for i, time in enumerate(books):
        if total_time + time > limit:
            num_books = i
            break
        total_time += time
    return [num_books, total_time]

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

    max_num_A, total_time_A = count_books(A, K)
    read_book_nums = [0] * (max_num_A + 1)

    remain = K - total_time_A
    num_B, total_time_B = count_books(B, remain)
    read_book_nums[-1] = max_num_A + num_B

    num_A = max_num_A
    for n in range(max_num_A-1, -1, -1):
        num_A -= 1
        remain += A[n-1]
        num_B, total_time_B = count_books(B, remain)
        read_book_nums[n] = num_A + num_B

    print(max(read_book_nums))
    
main()

Submission status: Time Limit Exceeded

Solution code:
import sys


def main():
    input = sys.stdin.buffer.readline
    n, m, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    ans = 0
    num_a = 0
    while num_a <= n:
        # Aの上からi冊の和
        s = sum(a[:num_a])
        if s > k:
            break
        # Bから読める本の数
        for j in range(len(b)):
            if s + b[j] <= k:
                s += b[j]
            else:
                num_b = j
                break
        else:
            num_b = len(b)
        if num_a + num_b > ans:
            ans = num_a + num_b
        num_a += 1

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

maxn = 0
q = deque([(0, 0, 0)])
s = set()
while q:
    a, b, t = q.popleft()
    if (a, b) in s:
        continue
    s.add((a, b))
    maxn = max(maxn, a + b)
    if a < n and t + aa[a] <= k:
        q.append((a + 1, b, t + aa[a]))
    if b < m and t + bb[b] <= k:
        q.append((a, b + 1, t + bb[b]))

print(maxn)

Submission status: Time Limit Exceeded

Solution code:
import math
from queue import Queue

n, m, k = map(int, input().split(" "))
aLi = list(map(int, input().split(" ")))
bLi = list(map(int, input().split(" ")))

if aLi[0] > k and bLi[0] > k:
    print(0)
    exit()

q = Queue()
q.put({"s": aLi[0], "na": 1, "nb": 0})
q.put({"s": bLi[0], "na": 0, "nb": 1})

_min = float("inf")

while True:
    cp = q.get()
    _sum = cp["s"]
    nextA = cp["na"]
    nextB = cp["nb"]

    if nextA + 1 == n and nextB + 1 == m:
        break

    if _sum >= k:
        _min = min(_min, nextA + nextB)
        break

    if n > nextA:
        q.put({"s": _sum + aLi[nextA], "na": nextA + 1, "nb": nextB})

    if m > nextB:
        q.put({"s": _sum + bLi[nextB], "na": nextA, "nb": nextB + 1})

if _min > 100000000:
    _min = n + m

print(_min)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

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

ans = 0

flag = True

for i in range(len(A) + 1):
    a_sum = sum(A[:i])
    if a_sum > K:
        break
    b_sum = 0
    for j in range(len(B)):
        b_sum += B[j]
        if a_sum + b_sum > K:
            flag = False
            tmp = i + j
            if ans < tmp:
                ans = tmp
            break
if flag:
    ans = len(A) + len(B)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(100000)
from functools import lru_cache

INF = 10**9 + 1

@lru_cache(None)
def get_max(A,B,rest,cur):
    if len(A) > 0:
        a = A[0]
    else:
        a = INF
    if len(B) > 0:
        b = B[0]
    else:
        b = INF
    if rest < a and rest < b:
        return cur

    if rest >= a:
        tmp_a = get_max(tuple(list(A)[1:]),B,rest-a,cur+1)
    else:
        tmp_a = 0
    if rest >= b:
        tmp_b = get_max(A,tuple(list(B)[1:]),rest-b,cur+1)
    else:
        tmp_b = 0

    return max(tmp_a,tmp_b)



def main():
    N,M,K = [int(i) for i in input().strip().split()]
    A = tuple([int(i) for i in input().strip().split()])
    B = tuple([int(i) for i in input().strip().split()])


    ans = get_max(A,B,K,0)

    return ans

if __name__ == "__main__":
    print(main())

Submission status: Time Limit Exceeded

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+1):
    if i >= 1:
        A[i] = A[i] + A[i-1]

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

cntA = 0
cntB = 0
cnt = 0
ans = 0

if A[N] + B[M] <= K:
    print(N+M)
    exit()
for i in range(N+1):
    if A[i] > K:
        break
    for j in range(M+1):
        if A[i] + B[j] > K:
            ans = max(ans, i+j-1)
            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()))
i = 0
j = 0
count = 0
for i in range(N+1):
	for j in range(M+1):
		if sum(A[:i]) + sum(B[:j]) <= K:
			tmp = i + j
			count = max(tmp, count)
print(count)

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.sort()
b.sort()
count = 0

while (a != []) or (b != []):
    if not a:
        k -= b[0]
        if k < 0: break
        del b[0]
    elif not b:
        k -= a[0]
        if k < 0: break
        del a[0]
    elif a[0] <= b[0]:
        k -= a[0]
        if k < 0: break
        del a[0]
    else:
        k -= b[0]
        if k < 0: break
        del b[0]
    count += 1

print(count)

Submission status: Time Limit Exceeded

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

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


sum_A = np.cumsum(A)
sum_B = np.cumsum(B)
list_N = [0 for i in range(len(A)+1)]

pre_n = len(B)

for i in range(len(A)+1):
    if i == 0:
        buf = sum_B[:pre_n]
        num_B = len(buf[buf<=K])
        pre_n = num_B
        list_N[i] = i + num_B
    else:
        if K>=sum_A[i-1]:
            buf = sum_B[:pre_n]
            num_B = len(buf[buf<=(K-sum_A[i-1])])
            pre_n = num_B
            list_N[i] = i + num_B
        else:
            list_N[i] = 0
    
print(np.max(list_N))

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**7)

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

max = 0

def dfs(i,j, time, count):
    global max
    if time > k:
        return 
    else:
        if count >= max:
            max = count
    if i >= n and j >= m:
        return 
    elif i >= n:
        dfs(i, j+1, time+b[j], count+1)
    elif j >= m:
        dfs(i+1, j, time+a[i], count+1)
    else:
        dfs(i, j+1, time+b[j], count+1)
        dfs(i+1, j, time+a[i], count+1)

dfs(0,0,0,0)
print(max)

Submission status: Time Limit Exceeded

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

a_li = [0] + a_li
b_li = [0] + b_li

time = 0
ans = 0
a = 0
temp = 0

a_wa = [0] * (n+10)
b_wa = [0] * (m+10)

s = 0
for i in range(n+1):
    s += a_li[i]
    a_wa[i] = s

s = 0
for i in range(m+1):
    s += b_li[i]
    b_wa[i] = s
    

for i in range(n+1):
    for j in range(m+1):
        time = a_wa[i] + b_wa[j]
        if time <= k:
            ans = max(ans, i+j)
        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()))

max_A_num = 0
for A_num in range(N+1):
    if sum(A[:A_num]) <= K:
        max_A_num = A_num
    else:
        break
max_book_count = 0
max_B_num = 0
for A_num_2 in range(A_num,-1,-1):
    for B_num in range(max_B_num, M+1):
        if sum(A[:A_num_2]) + sum(B[:B_num]) <= K:
            max_B_num = B_num
            if max_book_count < A_num_2 + B_num:
                max_book_count = A_num_2 + B_num
        else:
            break
print(max_book_count)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n,m,k = (int(x) for x in input().split())
a =np.array([int(x) for x in input().split()])
b =np.array([int(x) for x in input().split()])

count = 0
for i in range(n+1):
    for j in range(m+1):
        if i+j>count:
            if np.sum(a[:i])+np.sum(b[:j]) <= k:
                count = i+j

print(count)

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()))
NM = N + M
result = 0
for i in range(2 ** NM):
    t = 0
    kk = 0
    aa = 0
    bb = 0
    for j in range(NM):
        if ((i >> j) & 1 and aa < len(A)) or bb >= len(B):
            kk += A[aa]
            aa += 1
            if (kk > K):
                break
        else:
            kk += B[bb]
            bb += 1
            if (kk > K):
                break
        t += 1
    if result < t:
        result = t
    if result >= NM:
        break
print(result)

Submission status: Time Limit Exceeded

Solution code:
gen_input = list(map(int, input().split()))
time_a = list(map(int, input().split()))
time_b = list(map(int, input().split()))

length_a = gen_input[0]
length_b = gen_input[1]
time_const = gen_input[2]

if sum(time_a) + sum(time_b) == time_const:
    print(length_a + length_b)
elif time_const < min(time_a[0], time_b[0]):
    print(0)
else:
    num_read_books = 0
    while True:
        if (length_a > 0) & (length_b > 0):
            if time_a[0] >= time_b[0]:
                length_b -= 1
                time_const -= time_b[0]
                if length_b > 1:
                    time_b = time_b[1:]
            else:
                length_a -= 1
                time_const -= time_a[0]
                if length_a > 1:
                    time_a = time_a[1:]

            num_read_books += 1

        elif (length_a == 0) & (length_b > 0):
            length_b -= 1
            time_const -= time_b[0]
            if length_b > 1:
                time_b = time_b[1:]

            num_read_books += 1
        elif (length_a > 0) & (length_b == 0):
            length_a -= 1
            time_const -= time_a[0]
            if length_a > 1:
                time_a = time_a[1:]
            num_read_books += 1
        else:
            break

        if time_const < 0:
            break
    print(num_read_books - 1)

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()))
 
T_A = [0, A[0]]
T_B = [0, B[0]]
for i in range(1, max(M,N)):
  if i < len(A):
    T_A.append(T_A[i] + A[i])
  if i < len(B):
    T_B.append(T_B[i] + B[i])
      
book = 0
for i in range(len(T_A)):
  for j in range(len(T_B)):
    if K < T_A[i] + T_B[j]:
      break
    else:
      if i + j  > book:
        book = i + j
      else:
        pass
    
print(book)

Submission status: Time Limit Exceeded

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

ans = 0
i = 0
while i <= n:
  suma = k - sum(aes[:i])
  if suma < 0:
    break
  i += 1
  j = 0
  while j <= m:
    sumall = suma - sum(bs[:j])
    if sumall < 0:
      break
    j += 1
  
  ans = max(ans, i + j - 2)
  # print(i-1, j-1, ans)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, M, K = map(int, input().split())
tempA = [int(i) for i in input().split()]
tempB = [int(i) for i in input().split()]
A = [0] * (N+1)
for i in range(1, N+1):
    A[i] = A[i-1] + tempA[i-1]
B = [0] * (M+1)
for i in range(1, M+1):
    B[i] = B[i-1] + tempB[i-1]

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
from queue import Queue

n, m, k = map(int, input().split(" "))
aLi = list(map(int, input().split(" ")))
bLi = list(map(int, input().split(" ")))

if aLi[0] > k and bLi[0] > k:
    print(0)
    exit()

q = Queue()
q.put({"s": aLi[0], "na": 1, "nb": 0})
q.put({"s": bLi[0], "na": 0, "nb": 1})

_min = float("inf")

while True:
    cp = q.get()
    _sum = cp["s"]
    nextA = cp["na"]
    nextB = cp["nb"]

    if nextA + 1 == n and nextB + 1 == m:
        break

    if _sum >= k:
        _min = min(_min, nextA + nextB)

    if n > nextA:
        q.put({"s": _sum + aLi[nextA], "na": nextA + 1, "nb": nextB})

    if m > nextB:
        q.put({"s": _sum + bLi[nextB], "na": nextA, "nb": nextB + 1})

if _min > 100000000:
    _min = n + m

print(_min)

Submission status: Time Limit Exceeded

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

n_max = n
for i in range(n):
  if sum(ls_a[:i+1]) > k:
    n_max = i
    break
    
readable_max = 0
pre_max = 0
for i in range(n_max, -1, -1):
  m_time = k - sum(ls_a[:i])
  
  m_max = m
  for j in range(pre_max, 200000):
    if sum(ls_b[:j+1]) > m_time:
      m_max = j
      pre_max = j
      break
  if i + m_max > readable_max:
    readable_max = i + m_max
    
print(readable_max)

Submission status: Time Limit Exceeded

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

from collections import deque

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

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

an = an
bn = bn

vMax = 10**9+10

import itertools

accum_a = [0]
accum_b = [0]

dp = [[0]*200003]*200003
candidate = []
for a_i in range(0,len(an)):


    sumCandidate = accum_a[-1] + an[a_i]
    if sumCandidate > k:
        break
    accum_a.append(sumCandidate)

for b_i in range(0,len(bn)):
    accum_b.append(accum_b[-1]+ bn[b_i])

countMax = 0
for i, aSum in enumerate(accum_a):
    rest = k - aSum

    bIndex = 0
    for j, accB in enumerate(accum_b):
        if accB > rest:
            bIndex = max(0, j-1)
            break
        bIndex = j

    countMax = max(countMax, i + bIndex)

print(countMax)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**6)


def main(input, print):
    n, m, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    a.insert(0, 0)
    b.insert(0, 0)
    ans = 0
    at = 0
    for i in range(n+1):
        at += a[i]
        if at > k:
            break
        bt = at
        for j in range(m+1):
            if bt + b[j] > k:
                break
            bt += b[j]
            if ans < i+j:
                ans = i+j
    print(ans)


if __name__ == '__main__':
    main(sys.stdin.readline, print)

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()))
SUM_A=[0 for _ in range(N+1)]
for i in range(N):
    SUM_A[i+1] = A[i] + SUM_A[i]
    if SUM_A[i+1] > K:
        N=i
        break

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

cnt = N
for i in range(N+1)[::-1]:
    V=K-SUM_A[i]
    j = 0
    while j < M+1 and SUM_B[j] <= V:
        j += 1
    j-=1
    if i+j > cnt:
        cnt = i+j
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()))
a = 0
b = 0
ans = 0
t = sum(A)
temp = 0
if t <= K:
    ans = N
for i in range(0,N+1):
    if i != 0:
        t -= A[-i]
    temp = t
    j = 0
    ans2 = 0
    while temp < K and j < len(B):
        if temp + B[j] <= K:
            ans2 += 1
            temp += B[j]
            j += 1
        else:
            break
    if temp <= K:
        ans = max(ans,len(A)-i+ans2)
print(ans)

Submission status: Time Limit Exceeded

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

A = [int(s) for s in input().split()]
B = [int(s) for s in input().split()]

init = (K, 0, 0)
res = 0
q = deque()
q.append(init)
while q:
    deadline, ai, bi = q.popleft()
    res = max(res, ai+bi)
    if  ai < N and deadline - A[ai] >= 0:
        q.append((deadline - A[ai], ai+1, bi))
    if bi < M and deadline - B[bi] >= 0:
        q.append((deadline - B[bi], ai, bi+1))
    
print(res)

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

SA = []
for idx in range(N + 1):
    SA.append(sum(A[i] for i in range(idx)))

SB = []
for idx in range(M + 1):
    SB.append(sum(B[i] for i in range(idx)))

for i in range(len(A) + 1):
    if SA[i] > K:
        i -= 1
        break
    max_a = i

max_num = []
for i in range(max_a + 1):
    for j in range(M + 1):
        if SA[i] + SB[j] > K:
            j -= 1
            break
    max_num.append(i+j)

print(max(max_num))

Submission status: Time Limit Exceeded

Solution code:
a = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))

time = 0
books = 0
if min(A[0], B[0]) > a[2]:
    books = 0
else:
    while True:
        if len(A) == 0 and len(B) == 0:
            break
        if time > a[2]:
            books -= 1
            break
        if len(A) == 0 and len(B) != 0:
            books += 1
            time += B[0]
            B.pop(0)
        elif len(A) != 0 and len(B) == 0:
            books += 1
            time += A[0]
            A.pop(0)
        else:
            if A[0] < B[0]:
                books += 1
                time += A[0]
                A.pop(0)
            elif A[0] > B[0]:
                books += 1
                time += B[0]
                B.pop(0)
    
print(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()))
max_c = 0
t = 0
cnt = 0
flag = 0

for i in range(n+1):
  for j in range(m+1):
    t = sum(a[:i]) + sum(b[:j])
    if t > k:
      break
    else :
      if i+j > max_c:
        max_c = i+j
print(max_c)

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

ma=0
for x in range(len(a)+1):
    for y in range(len(b)+1):
        if sum(a[:x])+sum(b[:y])>k:break
        ma=max(ma,x+y)
        
print(ma)

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

n_read = [0]
for i in range(len(A)+1):
  nokori = K-sum(A[:i])
  if nokori<0:
    break
  
  for b in range(len(B)+1):
    #print(i, b, sum(A[:i]), sum(B[:b]), K-(sum(A[:i]) + sum(B[:b])))
    
    if K>=sum(A[:i]) + sum(B[:b]):
      n_read.append(i+b)

print(max(n_read))

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()))
li = []
ans,tmp = 0,0
while 1:
    if A and B:
        if A[0] < B[0]:
            li.append(A.pop(0))
        else:
            li.append(B.pop(0))
    elif A:
        while A:
            li.append(A.pop(0))
    elif B:
        while B:
            li.append(B.pop(0))

    else:
        break
for i in li:
    if i+tmp <= K:
        tmp += i
        ans += 1
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()))
ans = 0

a.insert(0, 0)
b.insert(0, 0)

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
#import sys
MOD = 10 ** 9 + 7
INFI = 10**10
#input = sys.stdin.readline
import math
from collections import deque
import itertools
import heapq
#import bisect
from fractions import Fraction
import copy
from functools import lru_cache
from collections import defaultdict
import pprint

#oo=list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
# ko=list("abcdefghijklmnopqrstuvwxyz")

def sosuhante(n):
    for k in range(2, int(math.sqrt(n))+1):
        if n% k ==0:
            return False
    return True
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def kingaku(a,b,n):
    keta=len(str(n))
    return a*n+b*keta

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default

#    h,w,a,b = map(int, input().split())
#    c = [[0 for j in range(n)] for i in range(n)]

def ret(a):
    c=[None]*(len(a)-1)
    if len(a)==1:
        return a[0]
    elif len(a)==0:
        return 0
    for i in range(1,len(a)):
        c[i-1]=abs(a[i]-a[i-1])
    return ret(c)

def soinsubunkai(n):
    a = []
    i = 1
    while i*i <= n:
        if n % i == 0 and i!=1:
            a.append(i)
            n=n//i

        if n% i !=0 or i==1:
            i += 1
    nokori=[n]
    return a + nokori


def main():
    n,m,k=map(int,input().split())
    aa=list(map(int,input().split()))
    bb=list(map(int,input().split()))
    a=[0]
    a.extend(aa)
    b=[0]
    b.extend(bb)
    maxcount=0
    totaltime=0
    maxa=0
    maxb=0
    for i in range(n+1):
        if totaltime+a[i]<=k:
            totaltime+=a[i]

        else:
            maxa=i-1
            break
        maxa = i
    totaltime=0
    for i in range(m+1):
        if totaltime+b[i]<=k:
            totaltime+=b[i]

        else:
            maxb=i-1
            break
        maxb = i
    nowatotal=sum(a[0:maxa+1])
    nowbtotal=sum(b[0:maxb+1])
    for i in range(maxa,-1,-1):
        nowbtotal = sum(b[0:maxb + 1])
        for j in range(maxb,-1,-1):
            nowcount=i+j
            if nowatotal+nowbtotal<=k:
                maxcount=max(maxcount,nowcount)
            nowbtotal-=b[j]
        nowatotal-=a[i]
    print(maxcount)

if __name__ == "__main__":

    main()

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(len(A)+1):
    for j in range(len(B)+1):
        if sum(A[:i])+sum(B[:j])<=k:
            ans=max(ans,i+j)
        else:
            break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,m,k = map(int, input().split())
a_lst = [int(i) for i in input().split()]
b_lst = [int(i) for i in input().split()]

sum_lst = sorted(a_lst + b_lst)
count = 0
time = 0

for i in range(len(a_lst)+len(b_lst)):
    if time >= k:
        break
    count += 1
    time += sum_lst.pop(0)
if time > k:
    print(count-1)
else:
    print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**6)


def DFS(point,left,right,cnt):
    global ans
    if point<=k:
        if cnt>ans:
            ans=cnt

    if left<n:
        if point+A[left]<=k:DFS(point+A[left],left+1,right,cnt+1)
    if right<m:
        if point+B[right]<=k:DFS(point+B[right],left,right+1,cnt+1)


n,m,k=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
ans=0
DFS(0,0,0,0)
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()))
number=0
s=0
c=a+b
c.sort()
while len(c)!=0:
  if s+c[0]<=k:
    number+=1
    s=s+c[0]
    c.pop(0)
  else:
    break
print(number)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

ans = 0

for bit in range(1 << (N+M)):
    cnt = 0
    time = 0

    Ai = 0
    Bi = 0

    for i in range(N+M):
        if bit & (1 << i):
            if Ai < N:
                time += A[Ai]
                Ai += 1
            else:
                break
        else:
            if Bi < M:
                time += B[Bi]
                Bi += 1
            else:
                break
        if time > K:
            break
        cnt += 1

    ans = max(cnt, ans)

print(ans)

Submission status: Time Limit Exceeded

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

n_max = n
for i in range(n):
  if sum(ls_a[:i+1]) > k:
    n_max = i
    break
    
readable_max = 0
for i in range(n_max, -1, -1):
  m_time = k - sum(ls_a[:i])
  
  m_max = m
  for j in range(200000):
    if sum(ls_b[:j+1]) > m_time:
      m_max = j
      break
  if i + m_max > readable_max:
    readable_max = i + m_max
    
print(readable_max)