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 = map(int,input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

import sys #追加
sys.setrecursionlimit(500*500) #追加


ans = [0]
memo = set()
import copy

def dfs(now_sum,i,j,cnt,memo1):
    # print(now_sum)
    # if ans[0] <= cnt:
    #     return
    if now_sum > K:
        # print("a")
        cnt -= 1
        ans[0] = max(ans[0],cnt)
        return

    
    
    # if i == N or j== M:
        # print("b")
        # print(now_sum,i,j)
    # ans[0] = max(ans[0],cnt)
        # return

    if i < N:
        memo_c = copy.deepcopy(memo1)
        if (memo_c[0],memo_c[1]+1) in memo:
            return
        memo.add((memo_c[0]+1,memo_c[1]))

        dfs(now_sum + A[i],i+1,j,cnt+1,(memo_c[0]+1,memo_c[1]))
    if j < M:
        memo_d = copy.deepcopy(memo1)
        if (memo_d[0],memo_d[1]+1) in memo:
            return
        memo.add((memo_d[0],memo_d[1]+1))
        dfs(now_sum + B[j],i,j+1,cnt+1,(memo_d[0],memo_d[1]+1))
    ans[0] = max(ans[0],cnt)

    


dfs(0,0,0,0,(0,0))

# print(memo)

print(ans[0])

Submission status: Time Limit Exceeded

Solution code:
# C - Tsundoku
import sys
sys.setrecursionlimit(10**9)

N, M, K = map(int, input().split())
A = list(int(a) for a in input().split())
B = list(int(b) for b in input().split())

la = 0
t = 0
for i in range(N):
    if A[i] + t <= K:
        la += 1
        t += A[i]

lb = 0
t = 0
for i in range(M):
    if B[i] + t <= K:
        lb += 1
        t += B[i]

T = 0
r = 0
ans = 0
def dfs(i, j, T, r):
    global ans
    ans = max(ans, r)
    if i < la and T+A[i] <= K:
        dfs(i+1, j, T+A[i], r+1)
    if j < lb and T+B[j] <= K:
        dfs(i, j+1, T+B[j], r+1)
    return

dfs(0, 0, T, 0)
print(ans)

Submission status: Time Limit Exceeded

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

n,m,k=map(int,input().split())
a=[0]
b=[0]
a.extend(list(map(int,input().split())))
b.extend(list(map(int,input().split())))
#print(a)
#print(b)
#exit()
lenab=n+m #len(a)+len(b)

def func(ii,jj,kk):
  if kk<0:
    r=-1
  elif ii+1<n+1 and jj+1<m+1:
    r=max(func(ii+1,jj,kk-a[ii+1]),func(ii,jj+1,kk-b[jj+1]))
    r+=1    
  elif ii+1<n+1:
    r=func(ii+1,jj,kk-a[ii+1])
    r+=1
  elif jj+1<m+1:
    r=func(ii,jj+1,kk-b[jj+1])
    r+=1
  else:
    r=0
  return r

print(func(0,0,k))

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)


answer = 0
consumption_A = 0
for i in range(len(A) + 1):
    consumption_A += A[i - 1] if i >= 1 else 0
    if (consumption_A > K): break

    consumption_B = 0
    for j in range(len(B) + 1):
        consumption_B += B[j - 1] if j >= 1 else 0
        consumption = consumption_A + consumption_B
        if (consumption > K): break
    
        num_books = i + j
        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()))
ans = 0
sm = 0
end = m
for j, b in enumerate(B):
    sm += b
    if sm > k:
        end = j
        sm -= b
        break
ans = end
for i, a in enumerate(A):
    sm += a
    if sm <= k:
        ans = max(end+i+1, ans)
        continue
    else:
        for j, b in enumerate(B[:end][::-1]):
            sm -= b
            if sm <= k:
                end -= j+1
                ans = max(end+i+1, ans)
                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()))
ans = 0

for i in range(n + 1):
    for j in range(m + 1):
        s = sum(a[:i]) + sum(b[:j])
        if k >= sum(a[:i]) + sum(b[:j]):
            ans = max(ans, i + j)
        else:
            break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque
import copy
sys.setrecursionlimit(2147483647)
input = sys.stdin.readline

if __name__ == '__main__':
  n,m,k=map(int,input().strip("\n").split())
  A=deque()
  mapA=map(int,input().strip("\n").split())
  for a in mapA:
    A.append(a)
  B=deque()
  mapB=map(int,input().strip("\n").split())
  for b in mapB:
    B.append(b)
  #A=list(map(int,input().strip("\n").split()))
  #B=list(map(int,input().strip("\n").split()))
#  A.append(10**9+1)
#  B.append(10**9+1)

  #def dfs(a,b,k,s):
  def dfs(A,B,k,s):
    if k<0:
      return s-1
    if k==0:
      return s
    ansA=s
    ansB=s
    if len(A)>0:
      x=copy.deepcopy(A)
      y=copy.deepcopy(B)
      tmp=x.popleft()
      ansA=dfs(x,y,k-tmp,s+1)
    #if a<n:
    #  ansA=dfs(a+1,b,k-A[a],s+1)
    #else:
    #  ansA=s
    if len(B)>0:
      #print(A)
      x=copy.deepcopy(A)
      y=copy.deepcopy(B)
      tmp=y.popleft()
      ansB=dfs(x,y,k-tmp,s+1)
    #if b<m:
    #  ansB=dfs(a,b+1,k-B[b],s+1)
    #else:
    #  ansB=s
    return max(ansA,ansB)

  x=copy.deepcopy(A)
  y=copy.deepcopy(B)
  ans=dfs(x,y,k,0)
  #ans=max(dfs(1,0,k-A[0],1),dfs(0,1,k-B[0],1))
  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])

n = 2
while True:
  if N[int(info[0]/n)] > info[2]:
    n += 1
  else:
    break

t = int(info[0]/(n-1))
  
ans = 0
for i in range(t, -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 = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

suma = [0 for i in range(len(a)+1)]
sumb = [0 for i in range(len(b)+1)]

for i in range(1, len(a)+1):
    suma[i] = suma[i-1] + a[i-1]

for i in range(1, len(b)+1):
    sumb[i] = sumb[i-1] + b[i-1]

mnum = 0
for i in range(len(suma)):
    num = 0
    for j in range(len(sumb))[::-1]:
        if suma[i] + sumb[j] <= k:
            num = i + j
            break
    if num > mnum:
        mnum = num

print(mnum)

Submission status: Time Limit Exceeded

Solution code:
N, M, K = input().strip().split()
NMK = [int(N), int(M), int(K)]
A= input().strip().split()
A = [int(s) for s in A]

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

max_val=0
val=0
sum_l=0
for i in range (0,NMK[0]+1,1):
  for j in range (0,NMK[1]+1,1):
    sum_l=sum(A[0:i])+sum(B[0:j])
    if sum_l<=NMK[2]:
      if (i+j)>max_val:
        max_val=i+j

        
print(max_val)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N,M,K=map(int,input().split())
costA=list(map(int,input().split()))
costB=list(map(int,input().split()))
time=0
count=0
cost=sorted(costA+costB)
while time<=K:
    time+=cost[0]
    count+=1
    del cost[0]
    if len(cost)==0:
        count+=1
        break
print(count-1)

Submission status: Time Limit Exceeded

Solution code:
a,b,c = map(int,input().split())
d = list(map(int,input().split()))
e = list(map(int,input().split()))
f = c
count = 0

while c > 0:
  if(len(d) > 0):
    if((c-d[0]) >= 0):
      c = c - d[0]
      del d[0]
      count += 1
  if(len(e) > 0):
    if((c-e[0]) >= 0):
      c = c - e[0]
      del e[0]
      count += 1
  if(f == c):
    break
    
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()))

import sys #追加
sys.setrecursionlimit(500*500) #追加


ans = [0]
memo = set()
import copy

def dfs(now_sum,i,j,cnt,memo1):
    # print(now_sum)
    # if ans[0] <= cnt:
    #     return
    if now_sum > K:
        # print("a")
        cnt -= 1
        ans[0] = max(ans[0],cnt)
        return

    
    
    # if i == N or j== M:
        # print("b")
        # print(now_sum,i,j)
    # ans[0] = max(ans[0],cnt)
        # return

    if i < N:
        memo_c = copy.deepcopy(memo1)
        memo.add((memo_c[0]+1,memo_c[1]))
        dfs(now_sum + A[i],i+1,j,cnt+1,(memo_c[0]+1,memo_c[1]))
    if j < M:
        memo_d = copy.deepcopy(memo1)
        memo.add((memo_d[0],memo_d[1]+1))
        dfs(now_sum + B[j],i,j+1,cnt+1,(memo_d[0],memo_d[1]+1))
    ans[0] = max(ans[0],cnt)
    if memo1 in memo:
        # print("a")
        return
    


dfs(0,0,0,0,(0,0))

# print(memo)

print(ans[0])

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 = m
  while j >= 0:
    sumall = suma - sum(bs[:j])
    if sumall >= 0:
      break
    j -= 1
  
  ans = max(ans, i + j -1)
  # print(i-1, j-1, ans)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

a_book = [0]
for index, i in enumerate(input().split(), 1):
    a_book.append(a_book[index-1] + int(i))

b_book = [0]
for index, i in enumerate(input().split(), 1):
    b_book.append(b_book[index-1] + int(i))

search = deque()
check = {}


def search_count(v):
    search.appendleft(v)
    while search:
        v = search.popleft()

        if not v in check:
            check[v] = v[0] + v[1]

            if not (v[0] + 1, v[1]) in check:
                if v[0] + 1 <= N:
                    if a_book[v[0] + 1] + b_book[v[1]] <= K:
                        search.appendleft((v[0] + 1, v[1]))

            if not (v[0], v[1] + 1) in check:
                if v[1] + 1 <= M:
                    if a_book[v[0]] + b_book[v[1] + 1] <= K:
                        search.appendleft((v[0], v[1] + 1))


search_count((0, 0))

print(max(check.values()))

Submission status: Time Limit Exceeded

Solution code:
# def helper(i, j, a, b, k, dp):
# 	n = len(a)
# 	m = len(b)

# 	if dp[i][j][k-1] != -1:
# 		return dp[i][j][k-1]
# 	# if i == n:
# 	# 	dp[i][j][k-1] = 0
# 	# 	s = 0
# 	# 	for jj in range(j, m):
# 	# 		if b[jj] + s > k:
# 	# 			return dp[i][j][k-1]
# 	# 		dp[i][j][k-1] += 1
# 	# 		s += b[jj]
# 	# 	return dp[i][j][k-1]
# 	# if j == m:
# 	# 	dp[i][j][k-1] = 0
# 	# 	s = 0
# 	# 	for ii in range(i, n):
# 	# 		if a[ii] + s > k:
# 	# 			return dp[i][j][k-1]
# 	# 		dp[i][j][k-1] += 1
# 	# 		s += a[ii]
# 	# 	return dp[i][j][k-1]

# 	# if min(a[i], b[j]) > k:
# 	# 	dp[i][j][k-1] = 0
# 	# 	return dp[i][j][k-1]
# 	dp[i][j][k-1] = 0
# 	print (i, j)
# 	if i < n:
# 		if a[i] < k:
# 			dp[i][j][k-1] = max(dp[i][j][k-1], helper(i+1, j, a, b, k-a[i], dp) + 1)
# 	if j < m:
# 		if b[j] < k:
# 			dp[i][j][k-1] = max(dp[i][j][k-1], helper(i, j+1, a, b, k-b[j], dp) + 1)
# 	return dp[i][j][k-1]

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

# dp = [[[-1 for _ in range(k)] for _ in range(m+1)] for _ in range(n+1)]

if k >= sum(a) + sum(b):
	print (m+n)
# r = helper(0, 0, a, b, k, dp)
else:
	r = 0
	stack = [(0, 0, k)]
	dic = {}
	dic[(0, 0)] = k

	while len(stack) != 0:
		i, j, kk = stack.pop()
		if i == n:
			# if j < m:
			# 	if b[j] > kk:
			# 		r = max(r, i + j)
			# 		continue
			# 	if b[j] <= kk:
			# 		if (i, j+1) not in dic:
			# 			stack.append((i, j+1, kk - b[j]))
			# 			dic[(i, j+1)] = kk - b[j]
			# 		elif (kk - b[j]) > dic[(i, j+1)]:
			# 			stack.append((i, j+1, kk - b[j]))
			# 			dic[(i, j+1)] = kk - b[j]
			while j < m:
				if b[j] > kk:
					r = max(r, i + j)
					break
				if (i, j+1) in dic and dic[(i, j+1)] > kk - b[j]:
					break
				dic[(i, j+1)] = kk - b[j]
				kk -= b[j]
				j += 1
					


		elif j == m:
			# if i < n:
			# 	if a[i] > kk:
			# 		r = max(r, i+j)
			# 		continue
			# 	if a[i] <= kk:
			# 		if (i+1, j) not in dic:
			# 			stack.append((i+1, j, kk - a[i]))
			# 			dic[(i+1, j)] = kk - a[i]
			# 		elif (kk - a[i]) > dic[(i+1, j)]:
			# 			stack.append((i+1, j, kk - a[i]))
			# 			dic[(i+1, j)] = kk - a[i]
			while i < n:
				if a[i] > kk:
					r = max(r, i + j)
					break
				if (i+1, j) in dic and dic[(i+1, j)] > kk - a[i]:
					break
				dic[(i+1, j)] = kk - a[i]
				kk -= a[i]
				i += 1
		else:
			if min(a[i], b[j]) > kk:
				r = max(r, i + j)
			if a[i] <= kk:
				if (i+1, j) not in dic:
					stack.append((i+1, j, kk-a[i]))
					dic[(i+1, j)] = kk - a[i]
				elif dic[(i+1, j)] < (kk - a[i]):
					stack.append((i+1, j, kk-a[i]))
					dic[(i+1, j)] = kk - a[i]
			if b[j] <= kk:
				if (i, j+1) not in dic:
					stack.append((i, j+1, kk - b[j]))
					dic[(i, j+1)] = kk - b[j]
				elif dic[(i, j+1)] < (kk - b[j]):
					stack.append((i, j+1, kk - b[j]))
					dic[(i, j+1)] = kk - b[j]

	print (str(r))

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
sum_B=0
for i in range(N):
    sum_A +=A[i]
    if sum_A>K:
        del A[i:]
        break
for i in range(M):
    sum_B +=B[i]
    if sum_B>K:
        del B[i:]
        break
while sum(A)+sum(B)>K:
    if A[-1]>B[-1]:
        del A[-1]
    elif A[-1]<B[-1]:
        del B[-1]
    elif A[-1] == B[-1]:
        del A[-1]
        del B[-1]
        if sum(A)+sum(B)<=K:
            A.append(1)
print(len(A)+len(B))

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# FileName: 	C_fix
# CreatedDate:  2020-06-27 21:25:14 +0900
# LastModified: 2020-06-27 22:35:20 +0900
#


import os
import sys
# import numpy as np
# import pandas as pd
from itertools import accumulate as ac

def main():
    n, m, k = map(int, input().split())
    a = list(ac(list(map(int, input().split()))))
    b = list(ac(list(map(int, input().split()))))
    a_j = [a_ for a_ in a if a_ <= k]
    b_j = [b_ for b_ in b if b_ <= k]
    cnt = 0
    if len(a_j)<len(b_j):
        cnt = len(b_j)
    else:
        cnt = len(a_j)
    for i in range(len(a_j)):
        for j in range(len(b_j)):
            if a_j[i]+b_j[j] <= k and i+1+j+1 > cnt:
                cnt = i+1+j+1
    print(cnt)


    
    

    


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
from queue import deque

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

A = input().split()
B = input().split()

que = deque([(0, K, 0, 0)])

max_count = 0

while que:
    count, k, a_index, b_index = que.popleft()
    max_count = max(max_count, count)
    
    if a_index < len(A) and 0 <= k-int(A[a_index]):
        que.appendleft((count+1, k-int(A[a_index]), a_index+1, b_index))
    if b_index < len(B) and 0 <= k-int(B[b_index]):
        que.appendleft((count+1, k-int(B[b_index]), a_index, b_index+1))

print(max_count)

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

seq = a + b
aaa = list(itertools.permutations(seq))
#print("{}".format(aaa))

ret = 0
for c in aaa:
    _time = 0
    _num = 0
    for i in c:
        _time += i
        if _time <= k:
            _num += 1
        else:
            break
    if ret < _num:
        ret = _num

print("{}".format(ret))

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()))
 
count=0
while(1):
  len_a = len(A)
  len_b = len(B)
  if K == 0:
    break
  if len_a != 0 and len_b  != 0:
    if K - A[0] < 0 and K - B[0] < 0:
      break
  elif len_a == 0 and len_b  != 0:
    if K - B[0] < 0:
      break
  elif len_a  != 0 and len_b == 0:
    if K - A[0] < 0:
      break
  elif len_a  == 0 and len_b == 0:
    break
  
  if len_a  != 0 and len_b != 0:
    if sum(A)/len_a < sum(B)/len_b:
      K = K - A.pop(0)
      count=count+1
    elif sum(A)/len_a > sum(B)/len_b:
      K = K - B.pop(0)
      count=count+1
  elif len_a == 0 and len_b != 0:
    K = K - B.pop(0)
    count=count+1
  elif len_a != 0 and len_b == 0:
    K = K - A.pop(0)
    count=count+1
    
print(count)

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

sum_A = [0] * (N+1)
mark_A = 0
for i,a in enumerate(A, 1):
    tmp =  sum_A[i-1] + a
    if tmp <= K:
        sum_A[i] = tmp
        mark_A = i
    else:
        break

sum_B = [0] * (M+1)
mark_B = 0
for i,b in enumerate(B, 1):
    tmp = sum_B[i-1] + b
    if tmp <= K:
        sum_B[i] = tmp
        mark_B = i
    else:
        break
        
import numpy as np
C = np.array([np.array(sum_A[:mark_A+1]) + x for x in sum_B[:mark_B+1]])
# C = np.tile(sum_A[:mark_A+1], (mark_B+1,1)) + np.array(sum_B[:mark_B+1]).reshape(-1, 1)
print(max(np.where(C <= K)[0] + np.where(C <= K)[1]))

Submission status: Time Limit Exceeded

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

t = k
i = 0
asum = [0]* (n+1)
bsum = [0]* (m+1)
suma = 0
sumb = 0

for  i in range(n):
    suma += a[i]
    asum[i+1]= suma


for  i in range(m):
    sumb += b[i]
    bsum[i+1] = sumb

count = 1
ans = 0

for i in range(n+1):

    for j in range(m+1):
        if asum[i] + bsum[j]<=k and i+j>ans:
            ans=i+j

print(min(ans,m+n))

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]
dict = {}

def dp(a, b, k):
  sstr = "".join(map(str, a))
  sstr += ','
  sstr += "".join(map(str, b))
  sstr += ','
  sstr += str(k)
  buf_a = copy.deepcopy(a)
  buf_b = copy.deepcopy(b)
  ak = 0
  bk = 0
  if (len(a)==0 and len(b)==0) or k<=0:
    return 0;
  if sstr in dict:
    return dict[sstr]
  if (len(a)>0 and a[0]<=k):
    ak = dp(buf_a, b, k-buf_a.pop(0)) + 1
    dict[sstr] = ak
  if (len(b)>0 and b[0]<=k):
    bk = dp(a, buf_b, k-buf_b.pop(0)) + 1
    dict[sstr] = bk
  return max([ak, bk])

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

Submission status: Time Limit Exceeded

Solution code:
n, m, k = map(int, input().split())
A = tuple(map(int, input().split()))
B = tuple(map(int, input().split()))
import numpy as np
from itertools import accumulate
AS = np.array([0] + list(accumulate(A)))
BS = np.array([0] + list(accumulate(B)))

ans = 0
for i in range(n+1):
    if AS[i] > k:
        break
    for j in range(m+1):
        if AS[i] + BS[j] > k:
            break
        ans = max(ans, i+j)
        
print(ans)

Submission status: Time Limit Exceeded

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

    a_sum = [0 for _ in range(len(a) + 1)]
    b_sum = [0 for _ in range(len(b) + 1)]
    for i in range(len(a)):
        a_sum[i + 1] = a_sum[i] + a[i]
    for i in range(len(b)):
        b_sum[i + 1] = b_sum[i] + b[i]

    a_sum = list(filter(lambda x: x <= k, a_sum))
    b_sum = list(filter(lambda x: x <= k, b_sum))

    ans = 0
    for i in range(len(a_sum)):
        for j in range(len(b_sum)):
            if a_sum[i] + b_sum[j] <= k:
                ans = max(ans, i + j)

    print(ans)


if 'unittest' not in globals():
    resolve()

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=0
y=0
z = 0
while True:
    if a == [] and b == []:
        break
    if x > k:
        break
    else:
        if a == []:
            x += b[0]
            del b[0]
            if x <= k:
                y = x
                z += 1
            if x > k:
                break
            continue
        if b == []:
            x += a[0]
            del a[0]
            if x <= k:
                y = x
                z += 1
            if x > k:
                break
            continue
        if a[0] > k or b[0] > k:
            break
        if a != [] and b != []:
            if a[0] < b [0]:
                x += a[0]
                del a[0]
                if x <= k:
                    y = x
                    z += 1
                if x > k:
                    break
                continue
            if a[0] > b [0]:
                x += b[0]
                del b[0]
                if x <= k:
                    y = x
                    z += 1
                if x > k:
                    break
                continue
    
print(z)

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

print(max(a + b for a in range(N + 1) for b in range(M + 1) if SA[a] + SB[b] <= K))

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)


answer = 0
for i in range(len(A) + 1):
    consumption_A = sum(A[:i])
    if (consumption_A > K): break

    for j in range(len(B) + 1):
        consumption_B = sum(B[:j])
        consumption = consumption_A + consumption_B
        if (consumption > K): break
    
        num_books = i + j
        answer = max(answer, num_books)

print(answer)

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

t = info[0]
n = 2
while True:
  if N[int(info[0]/n)] > info[2]:
    n += 1
  else:
    break
if n != 2:
  t = int(info[0]/n)
  
ans = 0
for i in range(t, -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:
import numpy as np
#from numba import njit

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

#@njit

for i in range(1,len(A)):
    A[i] += A[i-1]
for i in range(1,len(B)):
    B[i] += B[i-1]

A = A[A<=K]
B = B[B<=K]
array = np.where(A <= K)[0]
ia = -1 if len(array) == 0 else max(array)
sa = 0 if ia < 0 else A[ia]
array = np.where(B <= (K-sa))[0]
ib = -1 if len(array) == 0 else max(array)
cm = ia + ib + 2
while (ia >= 0):
    ia -= 1
    sa = 0 if ia < 0 else A[ia]
    array = np.where(B <= (K-sa))[0]
    ib = -1 if len(array) == 0 else max(array)
    cm = max(cm, ia + ib + 2)

print(cm)

Submission status: Time Limit Exceeded

Solution code:
import sys

rm = lambda: map(int, sys.stdin.buffer.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')

n, m, k = rm()
a = rl()
b = rl()

cnt = 0
lena = len(a)
lenb = len(b)
dp = [[inf]*(lenb + 1) for _ in range(lena + 1)]
dp[0][0] = 0
dp[0][1] = b[0]
dp[1][0] = a[0]

for i in range(lena):
    for j in range(lenb):
        dp[i][j+1] = dp[i][j] + b[j]
        dp[i+1][j] = dp[i][j] + a[i]
        if dp[i][j+1] <= k or dp[i+1][j] <= k:
            cnt = max(i+j+1, cnt)
        else:
            break
else:
    if dp[lena][lenb-1] + b[lenb-1] <= k:
        cnt += 1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
a,b,c = map(int,input().split())
d = list(map(int,input().split()))
e = list(map(int,input().split()))
f = c
count = 0

while c > 0:
  if(len(d) > 0):
    if((c-d[0]) >= 0):
      c = c - d[0]
      del d[0]
      count += 1
  if(len(e) > 0):
    if((c-e[0]) >= 0):
      c = c - e[0]
      del e[0]
      count += 1
  if(c == f):
    break
    
print(count)

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 k<0:
        return -1
    if i==n and j==m:
        return 0
    if i==n:
        if b[j]<=k:
            return 1+count(i,n,j+1,m,a,b,k-b[j])
        return 0
    if j==m:
        if a[i]<=k:
            return 1+count(i+1,n,j,m,a,b,k-a[i])
        return 0
    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:
# -*- coding: utf-8 -*-

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

A = input().split()
for i in range(len(A)):
	A[i] = int(A[i])

B = input().split()
for i in range(len(B)):
	B[i] = int(B[i])


n_array = [0]
for N_sub in range(N):
	n = N_sub + 1
	rem = K - sum(A[:n])
	if rem >= 0:
		n_array.append(n)

	for M_sub in range(M):
		m = M_sub + 1
		rem = K - sum(A[:n]) - sum(B[:m])
		if rem >= 0:
			n_array.append(m + n)
# print(n_array)
print(max(n_array))

Submission status: Time Limit Exceeded

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

    a_time = [0]
    need_time = 0
    for i in range(n):
        need_time+=a[i]
        a_time.append(need_time)
    
    b_time = [0]
    need_time = 0
    for i in range(m):
        need_time+=b[i]
        b_time.append(need_time)

    ans = 0
    for x in range(n+1):
        for y in range(m+1):
            if k < a_time[x] + b_time[y]:
                break
            else:
                if ans < x + y:
                    ans = x + y
    print(ans)

if __name__=="__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
A_len, B_len, K = map(int, input().split(' '))
#print("Aは{0}冊. Bは{1}冊. 上限{2}分".format(A_len, B_len, K))

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

# A,Bの累積和の配列を出して、A_acc,B_accの要素番地が最大となるような組み合わせを出す

A_acc = []
for idx_A in range(A_len): 
  if(idx_A == 0):
    A_acc.append(A[0])
  else:
    A_acc.append(A_acc[idx_A - 1] + A[idx_A])
A_acc.insert(0, 0)
    
B_acc = []
for idx_B in range(B_len):
  if(idx_B == 0):
    B_acc.append(B[0])
  else:
    B_acc.append(B_acc[idx_B - 1] + B[idx_B])
B_acc.insert(0, 0)

#print(A_acc, B_acc)

max_num = 0
left_time = K
for a_num in range(A_len + 1):
  b_num = 0
  left_time = K - A_acc[a_num]
  #print("Aから{0}冊読んだ場合はそもそも残り{1}分".format(a_num, left_time))
  if(left_time < 0):
    break
  
  while(True):
    #print("Bから{0}冊読んでる場合,残り時間{1}".format(b_num, left_time - B_acc[b_num]))
    
    if(b_num + 1 > B_len):
      break
    
    if(left_time - B_acc[b_num + 1] < 0):
      break
    
    b_num += 1
    
  #print("結果,A棚から{0}冊、B棚から{1}冊で合計{2}冊で経過時間{3}"
        #.format(a_num, b_num, a_num + b_num, A_acc[a_num] + B_acc[b_num]))
  max_num = max(max_num, (a_num + b_num))
  #print("現在max{0}".format(max_num))
  
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()))
a = 0
b = 0
ans = 0
for i in range(len(A)):
  if sum(A[0:i+1]) <= k:
    a += 1
  else:
    break

for i in range(len(B)):
  if sum(B[0:i+1]) <= k:
    b += 1
  else:
    break
#print(a)
#print(b)

su = sum(A[0:a]) + sum(B[0:b])
if su <= k:
  print(a+b)
else:
  for i in range(min(a,b)):
    ans += 1
    if A[a-1] >= B[b-1]:
      if su - A[a-1] <= k:
        break
      else:
        a -=1
    if A[a-1] < B[b-1]:
      if su - B[b-1] <= k:
        break
      else:
        b -=1   
  print(a+b-ans)