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:
N,M,K=list(map(int,input().split()))
A=list(map(int,input().split()))
B=list(map(int,input().split()))
cost=0
satu=0
maxi=0
for i in range(N):
    if K-cost-A[i]>=0:
        cost+=A[i]
        cost2=cost
        satu=i+1
        satu2=satu
        for j in range(M):
            if K-cost2-B[j]>=0:
                satu2+=1
                cost2+=B[j]
            else:
                break
        maxi=max(maxi,satu,satu2)
    else:break
cost=0
satu=0
for i in range(M):
    if K-cost-B[i]>=0:
        cost+=B[i]
        cost2=cost
        satu=i+1
        satu2=satu
        for j in range(N):
            if K-cost2-A[j]>=0:
                satu2+=1
                cost2+=A[j]
            else:
                break
        maxi=max(maxi,satu,satu2)
    else:break
print(maxi)

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

ai = 0
bi = 0
d = deque()
d.append((0, 1))
d.append((1, 0))

dic = {}
dic[(0,0)] = 0
ans = 0
while len(d) != 0:
  t = d.popleft()
  
  if t in dic:
    continue
    
  if (t[0]-1, t[1]) in dic:
    tmp = dic[(t[0]-1, t[1])]
    time = tmp + a[t[0]-1]
    
  else:
    tmp = dic[(t[0], t[1]-1)]
    time = tmp + b[t[1]-1]
    
  dic[t] = time
  if time <= k:
    ans = max(ans, t[0]+t[1])
    if t[0] < n:
      d.append((t[0]+1, t[1]))

    if t[1] < m:
      d.append((t[0], t[1]+1))
  
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, M, K = [int(x) for x in input().split()]

A = [0]
B = [0]

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

TIME = 0
ans = 0

Ta = 0
Tb = 0

for i in range(N + 1):
    if Ta > K:
        continue
    Ta += A[i]

    Tb = 0
    for j in range(M+1):
        Tb += B[j]
        if (Ta + Tb) > K:
            break
        else:
            ans = max(ans,i+j)
    
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

a_cum = [0] + list(itertools.accumulate(a))
b_cum = [0] + list(itertools.accumulate(b))

for x in range(len(a_cum)):
    for y in range(len(b_cum)):
        if a_cum[x] + b_cum[y] <= k:
            ans = max(ans, x + y)
        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()))
X = N + M
sum_n = 0
sum_m = 0
ans = 0
for i in range(1,X+1):
    for j in range(i+1):
        n = j
        m = i - j
        if(n <= N and m <= M):
            sum_n = sum(A[0:n])
            sum_m = sum(B[0:m])
            if((sum_m + sum_n) <= K):
                ans=i
                sum_m = 0
                sum_n = 0
                break
            sum_m = 0
            sum_n = 0
print(ans)

Submission status: Time Limit Exceeded

Solution code:
k = [int(x) for x in input().split()][2]
ab = [int(x) for x in input().split()] + [int(x) for x in input().split()]
O = 0
ab.sort()
for i in range(len(ab)):
    k -= ab[0]
    if k < 0:
        break
    ab.pop(0)
    O +=1
print(O)

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()))
t=0
for i in range(n+1):
    for j in range(m+1):
        if sum(a[0:i])+sum(b[0:j])<=k and t<=i+j:
            t=i+j
print(t)

Submission status: Time Limit Exceeded

Solution code:
def go(s,b):
    count=-1
    for i in b:
        if s>=i:
           count+=1

    return count

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

a= (map(int,input().split(" ")))
b= (map(int,input().split(" ")))

c,d=[],[]
s=0
for i in a:
   s+=i
   c.append(s)
s=0
for i in b:
   s+=i
   d.append(s)

mx=0
for i,j in enumerate(c):
    if j<=k:
        for q,w in enumerate(d):
            if j+w<=k:
                if i+q+2>mx:
                    mx=i+q+2
        if i+1>mx:
            mx=i+1
for q,w in enumerate(d):
    if w<=k:
        if q+1>mx:
            mx=1+q
print(mx)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
import heapq
from collections import defaultdict, deque
sys.setrecursionlimit(10**6) 
input = sys.stdin.readline
def r():
    return int(input())
def rm():
    return map(int,input().split())
def rl():
    return list(map(int,input().split()))

'''C Tsundoku'''
def count(i,n,j,m,a,b,k):
    if i==n:
        if j==m:
            return 0
        if b[j]<=k:
            return 1+count(i,n,j+1,m,a,b,k-b[j])
        return 0
    elif j==m:
        if i==n:
            return 0
        if a[i]<=k:
            return 1+count(i+1,n,j,m,a,b,k-a[i])
        return 0
    else:
        if a[i]>k and b[j]>k:
            return 0
        if a[i]>k:
            return 1+count(i,n,j+1,m,a,b,k-b[j])
        if b[j]>k:
            return 1+count(i+1,n,j,m,a,b,k-a[i])
        return max(1+count(i,n,j+1,m,a,b,k-b[j]),1+count(i+1,n,j,m,a,b,k-a[i]))

n,m,k=rm()
a=rl()
b=rl()
i=0;j=0;
print(count(i,n,j,m,a,b,k))

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

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

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

maxval=len(Asum)-1

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

Submission status: Time Limit Exceeded

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

if min(A[0],B[0])>K:
  print(0)
  exit()
     
c=[0 for _ in range(N)] + [1 for _ in range(M)]
C=list(permutations(c,len(c)))

for i in range(len(C)):
  countA=0
  countB=0
  ans=0
  answer=0
  for j in range(len(c)):
    if C[i][j]==0:
      ans+=A[countA]
      countA+=1
    else:
      ans+=B[countB]
      countB+=1
    if ans>=K:
      if countA+countB>answer:
        answer=countA+countB
        break
      else:
        break
print(answer)

Submission status: Time Limit Exceeded

Solution code:
temp = [int(i) for i in input().split(" ")]
n = temp[0]
m = temp[1]
k = temp[2]

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

counter = 0
time = 0

only_a_max = 0
only_b_max = 0

hoge = 0
for i in range(len(a)):

    hoge += a[i]
    if hoge <= k:
        only_a_max += 1
    else:
        break

hoge = 0
for i in range(len(b)):
    hoge += b[i]
    if hoge <= k:
        only_b_max += 1
    else:
        break


while True:

    target = 0

    if len(a) > 0 and len(b) > 0:
        if a[0] <= b[0]:
            target = a.pop(0)
        else:
            target = b.pop(0)

    elif len(a) > 0 and len(b) == 0:
        target = a.pop(0)

    elif len(b) > 0 and len(a) == 0:
        target = b.pop(0)


    time += target
    if time <= k:
        counter += 1
    else:
        break

    if len(a) == 0 and len(b) == 0:
        break



ans = max(only_b_max, only_a_max, counter)
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()))
cntA = 0
ans = 0
p = max(m,n)
if p == n:
  q = a
  r = b
else:
  q = b
  r = a
for i in range(p):
  if cntA+q[i] > k:
    break
  ans += 1
  cntA += q[i]
indA = ans-1
nc = indA+1
for j in range(indA+1):
  temp = 0
  add = 0
  for v in r:
    add += v
    if cntA + add > k:
      break
    temp += 1
  cntA -= q[nc-1-j]
  ans = max(ans,indA+1+temp)
  indA -= 1
print(ans)

Submission status: Time Limit Exceeded

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

time_A = list(map(int, input().split()))
time_B = list(map(int, input().split()))
max_books = 0
allot_to_A = 0
allot_to_B = K

while True:
    if max_books == len(time_A) + len(time_B):
        break
    num_books = 0
    change_point = 10000000000
    time_bankA = allot_to_A
    time_bankB = allot_to_B

    for i in range(len(time_A)):
        if time_A[i] <= time_bankA:
            time_bankA -= time_A[i]
            num_books += 1
        else:
            change_point = time_A[i] - time_bankA  # Aで読める本が１冊増える時間
            break

    for j in range(len(time_B)):
        if time_B[j] <= time_bankB:
            time_bankB -= time_B[j]
            num_books += 1
        else:
            break

    max_books = max(max_books, num_books)
    allot_to_A += change_point
    allot_to_B -= change_point
    if allot_to_A < 0 or allot_to_B < 0:
        break

print(max_books)

Submission status: Time Limit Exceeded

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

A_sum_list = [0]
B_sum_list = [0]
total_A = 0
total_B = 0

for A_i in A_list:
  total_A += A_i
  A_sum_list.append(total_A)

for B_i in B_list:
  total_B += B_i
  B_sum_list.append(total_B)

answer = 0

for i in range(n+1):
  for j in range(m+1):
    if A_sum_list[i] + B_sum_list[j] <= k:
      if i + j > answer:
        answer = i + j
    else:
      break
print(answer)

Submission status: Time Limit Exceeded

Solution code:
import sys
def input():
    return sys.stdin.readline().rstrip('\n')
    

def calc(N, M, K, A, B):
    na = 0
    A = [0] + A
    for i in range(1, len(A)):
        A[i] += A[i - 1]
        if A[i] > K:
            break
        na += 1
    nb = 0
    B = [0] + B
    for i in range(1, len(B)):
        B[i] += B[i - 1]
        if B[i] > K:
            break
        nb += 1
    i = 0
    while True:
        for j in range(max(na - i, 0), na + 1):
            if na + nb - i - j >= 0 and na + nb - i - j <= nb and A[j] + B[na + nb - i - j] <= K:
                return na + nb - i
        i += 1


(N, M, K) = tuple([int(s) for s in input().split()])
A = [int(s) for s in input().split()]
B = [int(s) for s in input().split()]
print(calc(N,M,K,A,B))

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(100000)

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

    pre_A = [0] * (N + 1)
    pre_B = [0] * (M + 1)


    for i in range(1,N+1):
        pre_A[i] = pre_A[i-1] + A[i-1]
    for i in range(1,M+1):
        pre_B[i]= pre_B[i-1] + B[i-1]

    ans = 0

    for i in range(N+1):
        if pre_A[i] > K:
            break
        for j in range(M+1): 
            if pre_A[i] + pre_B[j] <= K:
                ans = max(ans,i+j)
            else:
                break

    return ans

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

Submission status: Time Limit Exceeded

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

max_cnt = 0
for i in range(N+1):
    for j in range(M+1):
        a, b = A[:i], B[:j]
        total = sum(a) + sum(b)
        if total <= K:
            cnt = len(a) + len(b)
            max_cnt = max(max_cnt, cnt)

print(max_cnt)

Submission status: Time Limit Exceeded

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

n,m,k = map(int,input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a.append(1)
b.append(1)
ans = 0

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

shoki_time = 0
shoki_count = 0
shoki_i = 0
shoki_j = 0
dfs(shoki_time,shoki_count,shoki_i,shoki_j)
print(ans)

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

if sum(A) + sum(B) < K:
    print(N + M)
    exit()

ans = 0

while K > 0:
    if len(A) != 0:
        # if len(B) == 0 or A[0] <= B[0]:
        if len(B) == 0 or sum(A) <= sum(B):
            if A[0] <= K:
                K -= A.popleft()
                ans += 1
                continue
    if len(B) != 0:
        # if len(A) == 0 or A[0] >= B[0]:
        if len(A) == 0 or sum(A) >= sum(B):
            if B[0] <= K:
                K -= B.popleft()
                ans += 1
                continue

print(ans)

Submission status: Time Limit Exceeded

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

N = list()
M = list()
N.append(0)
for i in range(info[0]):
  N.append(N[i]+A[i])
M.append(0)
for i in range(info[1]):
  M.append(M[i]+B[i])

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

Submission status: Time Limit Exceeded

Solution code:
N,M,K=map(int,input().split())
A=list(map(int,input().split()))
B=[0]+list(map(int,input().split()))
C=[0]
cnt=0
a=0
ans=0
for i in A:
    a+=i 
    if a<=K:
        C.append(a)
        cnt+=1
    else:break
for j in range(cnt+1):
    b=C[j]
    for k in range(M+1):
        b+=B[k]
        if b<=K:
            ans=max(ans,j+k)
        else: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

a_cum = [0] + list(itertools.accumulate(a))
b_cum = [0] + list(itertools.accumulate(b))

for x in range(len(a_cum)):
    for y in range(len(b_cum)):
        if a_cum[x] + b_cum[y] <= k:
            ans = max(ans, x + y)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# from collections import deque
import numpy as np
from itertools import accumulate
def main():
    n, m, k = map(int, input().split())
    A = np.array([0] + list(map(int, input().split())))
    B = np.array([0] + list(map(int, input().split())))
    X = np.cumsum(A)
    Y = np.cumsum(B)
    P = X[X <= k]
    Q = Y[Y <= k]
    n = len(P)
    m = len(Q)
    # print(P,Q)

    cnt = 0
    # Aをi個、Bをj個選ぶ
    for i in range(n):
        for j in range(m):
            # print(i,j,X[i],Y[j])
            if X[i] + Y[j] <= k:
                cnt = max(cnt, i + j)
    print(cnt)



if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N, M, K = map(int, input().split())
A = input().split()
B = input().split()
for i in range(len(A)):
  A[i] = int(A[i])
for i in range(len(B)):
  B[i] = int(B[i])
A_cumsum = np.array(A).cumsum()
B_cumsum = np.array(B).cumsum()
max_count = 0
for i in range(M):
  if B_cumsum[i] <= K and max_count <= B_cumsum[i]:
    max_count = i + 1
  else:
    break
for i in range(N):
  if A_cumsum[i] <= K and max_count <= A_cumsum[i]:
    max_count = i + 1
  else:
    break
for i in range(N):
  if A_cumsum[i] > K:
    break
  for j in range(M):
    if A_cumsum[i] + B_cumsum[j] <= K and max_count <= A_cumsum[i] + B_cumsum[j]:
      max_count = i + j + 2
    else:
      break
print(max_count)

Submission status: Time Limit Exceeded

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

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

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


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

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

pre_n = len(B)

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

Submission status: Time Limit Exceeded

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

cumlA = [0]
for i in range(len(A)):
    cumlA.append(cumlA[-1]+A[i])

cumlB = [0]
for i in range(len(B)):
    cumlB.append(cumlB[-1]+B[i])

max = 0

a = list(filter(lambda x: x <= K, cumlA))

for i in range(len(a)):
    l = list(filter(lambda x: x <= K-cumlA[i], cumlB))
    if max < i + len(l) - 1:
        max = i + len(l) - 1

print(max)

Submission status: Time Limit Exceeded

Solution code:

def ABC172_C():
    import sys
    sys.setrecursionlimit(4100000)

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

    if k < a[0] or k < b[0]:
        print(0)
        return

    minamo = []
    def saiki(i,j,sum,mx):
        if sum > k:
            mx = min(i+j,mx)
            return mx

        elif sum == k:
            mx = min(i+j,mx)
            return mx

        if i+j >= mx:
            return mx

        if i+1 <= n:
            mx = saiki(i+1,j,sum+a[i],mx)
        if j+1 <= m:
            mx = saiki(i,j+1,sum+b[j],mx)

        return mx

    mls = saiki(0,0,0,pow(10,5))
    print(mls)

ABC172_C()

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(200000000)

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

max_num = 0


def dfs(curr_a, curr_b, choice, time_required, count):
    global max_num
    if choice == 0:
        if curr_a >= N - 1:
            if max_num < count:
                max_num = count
            return
        else:
            curr_a += 1
            time_required += a[curr_a]
    else:
        if curr_b >= M - 1:
            if max_num < count:
                max_num = count
            return
        else:
            curr_b += 1
            time_required += b[curr_b]
    if time_required > K:
        return
    count += 1
    if max_num < count:
        max_num = count
    dfs(curr_a, curr_b, 0, time_required, count)
    dfs(curr_a, curr_b, 1, time_required, count)

curr_a = -1
curr_b = -1
time_required = 0
count = 0
dfs(curr_a, curr_b, 0, time_required, count)
curr_a = -1
curr_b = -1
time_required = 0
count = 0
dfs(curr_a, curr_b, 1, time_required, count)

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



Asum=[0]
for i in A:
  if Asum[-1]+i>K:break
  Asum.append(Asum[-1]+i)
    
Bsum=[0]
for i in B:
  if Bsum[-1]+i>K:break
  Bsum.append(Bsum[-1]+i)

maxval=len(Asum)-1

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

Submission status: 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]
A.extend(a)
B.extend(b)
time = sum(A)
ai = len(A)-1
bi = 0
con = 0
flg = None
count = 0
if len(a) == 1 or len(b)==1:
    time = 0
    while True:
        if len(a) == 0 and len(b)==0:
            break
        elif len(a) == 0:
            time += b[0]
            b.pop(0)
        elif len(b) == 0:
            time += a[0]
            a.pop(0)
        elif a[0] > b[0]:
            time += b[0]
            b.pop(0)
        else:
            time += a[0]
            a.pop(0)   
        if time >K:
            break
        count += 1
    print(count)
else:
    while True:
        #print("---------")
        #print(str(ai)+":"+str(bi))
        if time > K:
            if flg is not None:
                con = max(con,flg-1)
                flg = None
            
            if ai == 0:
                break
            time -=A[ai]
            ai -= 1
        else:
            #print("b")
            flg = ai+bi
            time +=B[bi]
            bi += 1
            if bi == len(B):
                if time > K:
                    pass
                else:
                    con = max(con,ai+bi-1)
                break
        #print(flg)
    print(con)

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

Submission status: Time Limit Exceeded

Solution code:
import copy

nmk = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
n = nmk[0]
m = nmk[1]
k = nmk[2]

def dp(a, b, k):
  buf_a = copy.deepcopy(a)
  buf_b = copy.deepcopy(b)
  ak = 0
  bk = 0
  #print(a, b, k)
  if (len(a)==0 and len(b)==0) or k<=0:
    return 0;
  if (len(a)>0 and a[0]<=k):
    ak = dp(buf_a, b, k-buf_a.pop(0)) + 1
  if (len(b)>0 and b[0]<=k):
    bk = dp(a, buf_b, k-buf_b.pop(0)) + 1
  return max([ak, bk])

ans = dp(a, b, k)
print(ans)

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()))
Q = deque([[0, 0, 0]])
ans = 0

while len(Q) > 0:
    a, b, t = Q.popleft()
    #print([a, b, t])
    if a < n:
        t1 = t + A[a]
        a1 = a + 1
        if t1 <= k:
            Q.append([a1, b, t1])
        else:
            ans = max(ans, a+b)
    else:
        pass
    if b < m:
        t2 = t + B[b]
        b2 = b + 1
        if t2 <= k:
            Q.append([a, b2, t2])
        else:
            ans = max(ans, a+b)
    else:
        pass
    if a == n and b == m:
        ans = n + m
    #print(Q)
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()))

if a[0]>k:
  ans=0
  for i in range(m):
    ans+=b[i]
    if ans>k:
      print(i)
      break
elif b[0]>k:
  ans=0
  for i in range(n):
    ans+=a[i]
    if ans>k:
      print(i)
      break
else:
  asum=0
  bsum=0
  for i in a:
    asum+=i
  for i in b:
    bsum+=i
  ans=0
  bsumc=bsum
  if asum+bsum<k:
    print(n+m)
  else:
    for i in range(n+1):
      bsum=bsumc
      for j in range(m+1):
        if asum+bsum<=k and n+m-(i+j)>ans:
          ans=n+m-i-j
          break
        if j==m:
          break
        bsum-=b[-1-j]
      if i==n or asum+bsumc<k:
        break
      asum-=a[-1-i]
  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()))
AS=[0]
BS=[0]
r=0
for i in A:
  AS.append(i+AS[-1])
  if AS[-1]>k:
    break
for i in B:
  BS.append(i+BS[-1])
  if BS[-1]>k:
    break
for i in range(len(AS)):
  for j in range(len(BS)):
    if AS[i]+BS[j]<=k:
      if i+j>r:
        r=i+j
print(r)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

num_books = 0
remaining_time = K

A_ = ['A'] * len(A)
B_ = ['B'] * len(B)
AB_ = A_ + B_
# print(AB_)

import itertools
patterns = []
for v in itertools.permutations(AB_, len(AB_)):
    patterns.append(''.join(v))
patterns = list(set(patterns))
# print(patterns, len(patterns))

answer = 0
for pattern in patterns:
    remaining_time = K
    num_books = 0

    table = {
        'A': deque(A),
        'B': deque(B),
    }
    for book in pattern:
        if len(table[book]) <= 0: break

        remaining_time -= table[book].popleft()
        if remaining_time < 0: break
        
        num_books += 1
    # print(pattern, num_books)
    
    answer = max(answer, num_books)

print(answer)

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



Asum=[0]
for i in A:
  if Asum[-1]+i>K:break
  Asum.append(Asum[-1]+i)
    
Bsum=[0]
for i in B:
  if Bsum[-1]+i>K:break
  Bsum.append(Bsum[-1]+i)

maxval=len(Asum)-1

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

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
import sys

l = stdin.readline().rstrip().split(' ')
a = stdin.readline().rstrip().split(' ')
b = stdin.readline().rstrip().split(' ')
k = int(l[2])
n = int(l[0])
m = int(l[1])
ia = 0
ib = 0
b_sum = 0
count = 0
a_sum = 0
res = []

for bt in b:
    b_sum += int(bt)
    if b_sum <= k:
        count += 1
    else:
        break
res.append(count)
count = 0
for at in a:
    a_sum = a_sum + int(at)
    if k < a_sum:
        #res.append(count)
        print(max(res))
        sys.exit()
    else:
        count = ia + 1
    b_sum = a_sum
    for bt in b:
        b_sum += int(bt)
        if b_sum <= k:
            count += 1
        else:
            break
    ia += 1
    res.append(count)
print(max(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()))

ans = 0
dp = [[0 for _ in range(M+1)] for _ in range(N+1)]
for i in range(N+1):
    for j in range(M+1):
        # if dp[i][j] + A[i] <= K:
        if i+1 < N+1:
            dp[i+1][j] = dp[i][j] + A[i]
            if dp[i+1][j] <= K:
                ans = max(ans, i+1+j)
        # if dp[i][j] + B[j] <= K:
        if j+1 < M+1:
            dp[i][j+1] = dp[i][j] + B[j]
            if dp[i][j+1] <= K:
                ans = max(ans, i+1+j)
        # if dp[i][j] + A[i] > K and dp[i][j] + B[j] > K:
        #     break
# print(dp)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(1000000)

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

ai = 0
bi = 0

def read(ai, bi, count, rest):
    if rest <= 0:
        return count
    else:
        if ai >= N:
            a_c = count
        else:
            if rest >= A[ai]:
                a_c = read(ai+1, bi, count+1, rest - A[ai])
            else:
                a_c = count

        if bi >= M:
            b_c = count
        else:
            if rest >= B[bi]:
                b_c = read(ai, bi+1, count+1, rest - B[bi])
            else:
                b_c = count

        return max(a_c, b_c)

res = read(ai,bi,0,K)
print(res)

Submission status: Time Limit Exceeded

Solution code:
import numpy


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

max=0

A.insert(0, 0)
A = numpy.cumsum(A)
B.insert(0,0)



for i in range(N+1):
    hp=K
    hp-=A[i]

    if hp < 0:
        break

    for j in range(M+1):
        hp-=B[j]

        if hp < 0:
            break

        if max < i+j :
            max = i+j

print(max)

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)
memo = set()
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 and (ai+1,bi) not in memo:
        q.append((deadline - A[ai], ai+1, bi))
        memo.add((ai+1, bi))
    if bi < M and deadline - B[bi] >= 0 and (ai,bi+1) not in memo:
        q.append((deadline - B[bi], ai, bi+1))
        memo.add((ai, bi+1))
    
print(res)