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 = [int(x) for x in input().split(' ')]
A = [int(x) for x in input().split(' ')]
B = [int(x) for x in input().split(' ')]

# N, M, K = [int(x) for x in [2e3, 2e3, 1e9]]
# A = [int(x) for x in range(N)]
# B = [int(x) for x in range(M)]

stack = [[0, 0, 0, 0]]
ans = 0
while len(stack):
  s = stack[-1]
  stack.pop()

  count = s[3]
  a = count 
  b = count
  i = s[0]
  j = s[1]
  time = s[2]

  if i > N - 1 and j > M - 1:
    ans = max(ans, count)
    continue
  if time > K:
    ans = max(ans, count - 1)
    continue

  if i < N:
    stack.append([i + 1, j, time + A[i], count + 1])
  if j < M:
    stack.append([i, j + 1, time + B[j], count + 1])

print(ans)

Submission status: Time Limit Exceeded

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

def main():
    n,m,k = map(int,input().split())
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))
    a_cum = numpy.cumsum(a).tolist()
    b_cum = numpy.cumsum(b).tolist()
    a_cum.insert(0,0)
    b_cum.insert(0,0)
    ans = 0
    for i,c in enumerate(b_cum):
        for j,d in enumerate(a_cum):
            if c + d <= k and ans <= i + j:
                ans = i + j
            elif c + d > k:
                break
    print(ans)

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_list = []
ans_list.append(0)

sum_a = 0
for i in range(len(a)):
  sum_a += a[i]
  sum_b = 0
  if sum_a > k:
    break
  if sum_a == k:
    temp = i+1
    ans_list.append(temp)
    break
  for j in range(len(b)):
    sum_b += b[j]
    if max(ans_list) > i+1+j:
      pass
    else:
      if (sum_a + sum_b) > k:
        temp = i+1+j
        ans_list.append(temp)
        break
      if (sum_a + sum_b) == k:
        temp = i+1+j+1
        ans_list.append(temp)
        break
      
sum_b = 0
for i in range(len(b)):
  sum_b += b[i]
  sum_a = 0
  if sum_b > k:
    break
  if sum_b == k:
    temp = i+1
    ans_list.append(temp)
    break
  for j in range(len(a)):
    sum_a += a[j]    
    if max(ans_list) > i+1+j:
      pass
    else:
      if (sum_b + sum_a) > k:
        temp = i+1+j
        ans_list.append(temp)
        break
      if (sum_b + sum_a) == k:
        temp = i+1+j+1
        ans_list.append(temp)
        break
      
print(max(ans_list))

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]
if N[int(info[0]/2)] > info[2]:
  t = int(info[0]/2)
  
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:
#!/usr/bin/env python3

def solve(K,a,b):
    cum_a = [sum(a[:i]) for i in range(len(a)+1)]
    cum_b = [sum(b[:i]) for i in range(len(b)+1)]
    ans = 0
    for i in range(len(cum_a)):
        if cum_a[i] > K:
            break
        for j in range(len(cum_b)):
            time = int(cum_a[i]) + int(cum_b[j])
            if time <= K:
                ans = max(ans,i+j)
            else:
                break
    return ans

def main():
    N,M,K = map(int,input().split())
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))
    print(solve(K,a,b))
    return

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import itertools
import sys
input = sys.stdin.readline

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

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)


if sum(a) + sum(b) <= K:
    print(N + M)
else:
    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:
# from collections import deque
import numpy as np
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)[::-1]:
        if i + m <= cnt:
            break
        else:
            for j in range(m)[::-1]:
                #print(i,j,X[i],Y[j])
                if X[i] + Y[j] <= k:
                    cnt = max(cnt, i + j)
                    break
    print(cnt)



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

sums_B = ["" for _ in range(len(B)+1)]

for b in range(len(B)+1):
    sums_B[b] = sum(B[:b])
    
for a in range(len(A)+1):
  sums_A = sum(A[:a])
  if K - sums_A >= 0:
    b_max = max([k for k, v in enumerate(sums_B) if v <= K-sums_A])
    if ans < a+b_max:
        ans = a+b_max
  else:
    break
print(ans)

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

ans = 1
c = [0]
d = [0]
for i in range(1,n+1):
    c.append(c[i-1] + a[i-1])
    if(c[i] > k):
        c.pop()
        break
for i in range(1,m+1):
    d.append(d[i-1] + b[i-1])
    if(d[i] > k):
        d.pop()
        break


leng = len(c)
lend = len(d)
ans = leng - 1
for i in range(leng):
    p = c[leng-i-1]
    t = 0
    while(t < lend and p+d[t] <= k):
        t += 1
    ans = max(ans,leng-i-2+t)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

Acum = np.cumsum(np.array(A))
Bcum = np.cumsum(np.array(B))

maxnum = 0
for n in range(N,-1,-1):
    if n>0:
        tA = Acum[n-1]
    else:
        tA = 0
    if tA>K:
        continue
    
    for m in range(M,-1,-1):
        if m>0:
            tB = Bcum[m-1]
        else:
            tB = 0
        
        if tA+tB<=K:
            num = n+m
            if num>maxnum:
                maxnum = num
            break

print(maxnum)

Submission status: Time Limit Exceeded

Solution code:
import sys

sys.setrecursionlimit(10 ** 9)
N,M,K=map(int,input().split())
As=list(map(int,input().split()))
Bs=list(map(int,input().split()))

def f(nb,s,ia,ib):
    #print(nb,s,ia,ib)
    if s > K:
        return nb-1
    if ia==N and ib==M:
        return nb
    
    nb_max=0
    for nxt in ['a','b']:
        if nxt == 'a':
            if ia==N:
                continue
            nb_max=f(nb+1,s+As[ia],ia+1,ib)
        if nxt == 'b':
            if ib==M:
                continue
            nb_tmp=f(nb+1,s+Bs[ib],ia,ib+1)
            if nb_max < nb_tmp:
                nb_max = nb_tmp
    return nb_max



ans = f(0,0,0,0)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from queue import Queue

N, M, K = list(map(int, input().split()))
A = list(map(int, [0] + input().split()))
B = list(map(int, [0] + input().split()))
cost = 0
ind_a = 0
ind_b = 0
cumsum_a = np.cumsum(A)
cumsum_b = np.cumsum(B)
max_book = 1

queue = Queue()
queue.put([0, 0])
num_book = 0
while not queue.empty():
    a, b = queue.get()
    if a+1 <= N and b <= M:
        if cumsum_a[a + 1] + cumsum_b[b] <= K:
            queue.put([a + 1, b])
            num_book = max(num_book, a + b + 1)
    if a <= N and b+1 <= M:
        if cumsum_a[a] + cumsum_b[b + 1] <= K:
            queue.put([a, b + 1])
            num_book = max(num_book, a + b + 1)

print(num_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()))
Aa = []
Ba = []
for i in range(N):
    Aa.append(sum(A[:(i+1)]))
for j in range(M):
    Ba.append(sum(B[:(j+1)]))
Aa = [0] + Aa + [10**9 for _ in range(400000)]
Ba = [0] + Ba + [10**9 for _ in range(400000)]
for l in range(1,400001):
    f = True
    for k in range(0,l+1):
        p = Aa[k]+Ba[l-k]
        if p <= K:
            f = False
            break
    if f:
        print(l-1)
        exit()

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

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

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

for i in range(len(A)):
    if sum(A[0:i + 1]) <= K:
        n_a += 1
        t_a += A[i]
        continue
    else:
        break
# print(n_a)
# print(t_a)

for i in range(len(B)):
    if sum(B[0:i + 1]) <= K:
        n_b += 1
        t_b += B[i]
        continue
    else:
        break
# print(n_b)
# print(t_b)

if n_a >= n_b:
    n_b = 0
    t_b = 0
    for i in range(len(B)):
        if sum(B[0:i + 1]) <= K - t_a:
            n_b += 1
            t_b += B[i]
            continue
        else:
            break
    # print(n_b)
    # print(t_b)

else:
    n_a = 0
    t_a = 0
    for i in range(len(A)):
        if sum(A[0:i + 1]) <= K - t_b:
            n_a += 1
            t_a += A[i]
            continue
        else:
            break
    # print(n_a)
    # print(t_a)

print(n_a + n_b)

Submission status: Time Limit Exceeded

Solution code:
import math

def solve(n,m,k,a_lst,b_lst):
    #机ごと、読書時間の累積をリストにする。ただし、リスト化はk時間内で読める範囲とする
    aa_lst = make_sum_list(a_lst,k)
    bb_lst = make_sum_list(b_lst,k)
    len_aa = len(aa_lst)
    len_bb = len(bb_lst)

    #A,Bいずれかが一冊も読めないケースを考慮
    if len_aa == 0:
        if len_bb == 0: return 0
        else: return len_bb
    elif len_bb == 0: return len_aa
    
    #全て読めるケースを考慮
    if aa_lst[-1] + bb_lst[-1] <= k: return len_aa + len_bb 
    
    #A,Bいずれかのみ読むケースを考慮
    max_read = max(len_aa,len_bb)

    #リスト範囲でA+Bの最大値を探索する
    for i in reversed(range(len_aa)):
        time = aa_lst[i]
        read = i + 1
        if read + len_bb + 1 <= max_read:
            break

        for j in reversed(range(len_bb)):
            if read + j + 1 <= max_read:
                break
            if time + bb_lst[j] <= k:
                read += j + 1
                break
		
        max_read = max(read,max_read)

    return max_read

def make_sum_list(lst,max):
    sum_lst = []
    sum = 0
    for n in lst:
        sum += n
        if sum > max:
            break
        sum_lst.append(sum)
    return sum_lst

n,m,k = map(int, input().split())
a_lst = list(map(int, input().split()))
b_lst = list(map(int, input().split()))
ans = solve(n,m,k,a_lst,b_lst)
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()))
ai=0
bi=0
if sum(a)+sum(b)<k:print(n+m);exit()
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:
n,m,c=map(int,input().split())
numa=numb=0
time=0
bmax=0
a=list(map(int,input().split()))#0~n-1のn冊
b=list(map(int,input().split()))#0~m-iのm冊
sa=[0]*(n+1)
sb=[0]*(m+1)
sa[1]=a[0]
sb[1]=b[0]
for i in range(2,n+1):
    sa[i]+=sa[i-1]+a[i-1]
for j in range(2,m+1):
    sb[j]+=sb[j-1]+b[j-1]
for k in range(n+1):
    for l in range(m+1):
        if sa[k]+sb[l]<=c:
            bmax=max(k+l,bmax)
        else:
            break
    if sa[k]+sb[0]>c:break
print(bmax)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
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)
A_sum = [sum(A[0:e+1]) for e in range(len(A)) ]
B_sum = [sum(B[0:e+1]) for e in range(len(B)) ]
num=0
for i in range(len(A_sum)):
    for l in range(len(B_sum)):
        if num<i+l:
            Time=A_sum[i]+B_sum[l]
            if Time<=K:
                num=i+l
print(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()))

Used = [0]*M

total_max = 0
A_count = 0
B_count = 0

counter = 0

for i in range (0, N):
	if A[i] <= K and counter ==0:
		A_count+=1
		K-=A[i]
	else:
		counter = 1
		break

counter = 0

for i in range (0, M):
	if B[i] <= K and counter ==0:
		B_count+=1
		K-=B[i]
	else:
		counter = 1
		break

totalmax = A_count+B_count


for i in range (0, A_count):
	counter = 0
	D = K + sum(A[A_count-1-i:A_count])
	for j in range (0, M):
		zencount = A_count-i-1
		if sum(B[0:j]) <= D:
			zencount+=(j+1)
		if zencount >= totalmax:
			totalmax = zencount

print(totalmax)

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
check = 0
check_A = [0]
check_B = [0]

for n in range(N):
    check_A.append(check_A[n] + A[n])

for m in range(M):
    check_B.append(check_B[m] + B[m])

for n in range(N+1):
    if check_A[n] > K:
        break
    for m in range(M+1):
        if check_B[m] > K:
            break
        calc = check_A[n] + check_B[m]
        if calc > K:
            break
        if check <= calc:
            check = calc
            ans = n + m

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys

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

sys.setrecursionlimit(1000000)

s = set()
maxn = 0
def dfs(a, b, t):
    global maxn
    global s

    if (a, b) in s:
        return
    s.add((a, b))
    maxn = max(maxn, a + b)
    if a < n and t + aa[a] <= k:
        dfs(a + 1, b, t + aa[a])
    if b < m and t + bb[b] <= k:
        dfs(a, b + 1, t + bb[b])
dfs(0, 0, 0)
print(maxn)

Submission status: Time Limit Exceeded

Solution code:
# from collections import deque
import numpy as np
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)[::-1]:
            #print(i,j,X[i],Y[j])
            if X[i] + Y[j] <= k:
                cnt = max(cnt, i + j)
                break
    print(cnt)



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()))
AS=[0]
BS=[0]
r=[]
for i in A:
  if i+AS[-1]>k:
    break
  AS.append(i+AS[-1])
for i in B:
  if i+BS[-1]>k:
    break
  BS.append(i+BS[-1])
for i in range(len(AS)):
  for j in range(len(BS)):
    if AS[i]+BS[j]<=k:
      r.append(i+j)
print(sorted(r)[-1])

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 and (t[0]+1, t[1]) not in dic:
      d.append((t[0]+1, t[1]))

    if t[1] < m and (t[0], t[1]+1) not in dic:
      d.append((t[0], t[1]+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()))

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


total = []
a = 0
while True:
    if a > N or SA[a] > K:
        break
    else:
        b = 0
        while True:
            if b > M or SA[a] + SB[b] > K:
                total.append(a + b - 1)
                break
            else:
                b += 1
        a += 1
print(max(total))

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()
def calc(k, ab):
    return k - ab
for i in range(len(ab)):
    k = calc(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 = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
i,j= 0,0
cnt = 0
arr = [0 for i in range(n+1)]
brr = [0 for i in range(m+1)]
ans = 0
for i in range(1,n+1):
    arr[i] = arr[i-1] + a[i-1]
for i in range(1,m+1):
    brr[i] = brr[i-1] + b[i-1]
for i in range(n+1):
    if arr[i]>k:
        break
    for j in range(m+1):
        if arr[i]+brr[j]>k:
            break
        ans = max(ans, i+j)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import copy

n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
time = 0
count = 0
a_a = copy.copy(a)
a_time = 0
a_count = 0
b_b = copy.copy(b)
b_time = 0
b_count = 0

while k >= a_time:
    a_time += a_a[0]
    if k >= a_time:
        a_count += 1
    #print(a_a[0])
    a_a.pop(0)
    if len(a_a) == 0:
        break

while k >= b_time:
    b_time += b_b[0]
    if k >= b_time:
        b_count += 1
    #print(b_b[0])
    b_b.pop(0)
    if len(b_b) == 0:
        break

if k < a[0] and k < b[0]:
    pass
else:
    while k >= time:
        if a[0] < b[0]:
            time += a[0]
            if k >= time:
                count += 1
            #print(a[0])
            a.pop(0)
            if len(a) == 0:
                a.append(1000000000)
        else:
            time += b[0]
            if k >= time:
                count += 1
            #print(b[0])
            b.pop(0)
            if len(b) == 0:
                b.append(1000000000)

if a_count > count and count >= b_count:
    print(a_count)
elif b_count > count and count >= a_count:
    print(b_count)
else:
    print(count)

Submission status: Time Limit Exceeded

Solution code:
n,m,c=map(int,input().split())
numa=numb=0
time=0
bmax=0
a=list(map(int,input().split()))#0~n-1のn冊
b=list(map(int,input().split()))#0~m-iのm冊
sa=[0]*(n+1)
sb=[0]*(m+1)
sa[1]=a[0]
sb[1]=b[0]
for i in range(2,n+1):
    sa[i]+=sa[i-1]+a[i-1]
for j in range(2,m+1):
    sb[j]+=sb[j-1]+b[j-1]
for k in range(n+1):
    for l in range(m+1):
        if sa[k]+sb[l]<=c:
            bmax=max(k+l,bmax)
        else:
            break
print(bmax)

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
  adds = 0
  for v in r:
    adds += v
    if cntA + adds > 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:
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
            else:
                break
        if i+1>mx:
            mx=i+1
    else:
        break
for q,w in enumerate(d):
    if w<=k:
        if q+1>mx:
            mx=1+q
    else:
        break
print(mx)

Submission status: Time Limit Exceeded

Solution code:
import sys

sys.setrecursionlimit(10**6)
N, M, K = map(int, input().split())
A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))

A_sum = [0]
B_sum = [0]

for a in A_list:
    A_sum.append(A_sum[-1] + a)

for b in B_list:
    B_sum.append(B_sum[-1] + b)

count = 0

dict = {}


def solve(i, j):
    global count
    if (i, j) in dict:
        return
    else:
        dict[(i, j)] = 1
    if A_sum[i] + B_sum[j] > K:
        return
    if count < i + j:
        count = i + j
    if i == len(A_sum) - 1 and j == len(B_sum) - 1:
        return
    elif i == len(A_sum) - 1:
        solve(i, j + 1)
    elif j == len(B_sum) - 1:
        solve(i + 1, j)
    else:
        solve(i + 1, j)
        solve(i, j + 1)


solve(0, 0)
print(count)

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):
    a = A[:i]
    if sum(a) > K:
        continue
    for j in range(M+1):
        b = B[:j]
        if sum(b) > K:
            continue
        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 ** 8 )

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

a.append( 10 ** 9 + 1 )
b.append( 10 ** 9 + 1 )

def dp( i, j, t ):
    next_i = min( i+1, n )
    next_j = min( j+1, m )
    if i == n and j == m:
        return 0
    elif a[ i ] + t > k and b[ j ] + t > k:
        return 0
    elif a[ i ] + t > k:
        return dp( i, next_j, t+b[j] ) + 1
    elif b[ j ] + t > k:
        return dp( next_i, j, t+a[i] ) + 1
    else:
        return max( dp( next_i, j, t+a[i] ), dp( i, next_j, t+b[j] ) ) + 1

read = dp( 0, 0, 0 )
    
print( read )

Submission status: Time Limit Exceeded

Solution code:
import sys
import bisect
import itertools
import collections
import fractions
import heapq
import math
from operator import mul
from functools import reduce
from functools import lru_cache


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    N, M, K = map(int, readline().split())
    A = list(map(int, readline().split()))
    B = list(map(int, readline().split()))


    nowtime = 0

    Asum = [0]
    Bsum = [0]
    tmp = 0
    for a in A:
        tmp += a
        Asum.append(tmp)
    tmp = 0
    for b in B:
        tmp += b
        Bsum.append(tmp)

    maxbooks = 0
    before = M
    for acount in range(N+1):
        for bcount in range(0, before+1):
            if K < Asum[acount] + Bsum[bcount]:
                bcount -= 1
                break
        before = bcount
        if bcount < 0:
            break
        maxbooks = max(maxbooks, acount+bcount)

    print(maxbooks)



if __name__ == '__main__':
    solve()

Submission status: Time Limit Exceeded

Solution code:
def totalChecke(A,B,N,M,K):
  max = 0
  for i in range(N+1):
    total,time = [0,0]
    for j in range(i):
      time += A[j]
      if time > K:
        time -= A[j]
        break
      else:
        total += 1
    if time > K:
      if total > max:
        max = total
      continue
    for k in range(M):
      time += B[k]
      if time > K:
        break
      else:
        total += 1
    if total > max:
      max = total
  return max

N,M,K = list(map(int,input().split(" ")))
A = list(map(int,input().split(" ")))
B = list(map(int,input().split(" ")))
if N >= M:
  print(totalChecke(A,B,N,M,K))
else:
  print(totalChecke(B, A, M, N, K))

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()))
kotei=k
ans=0
cnt=0

if kotei>=sum(a):
  cnt+=len(a)
  ans=max(ans,cnt)
else:
  x=kotei
  i=0
  while x>=0:
    x-=a[i]
    i+=1
    cnt+=1
  ans=max(ans,cnt-1)

for j in range(m):
  kotei-=b[j]
  if kotei<0:
    break
  cnt=j+1
  if kotei>=sum(a):
    cnt+=len(a)
    ans=max(ans,cnt)
    continue
  else:
    x=kotei
    i=0
    while x>=0:
      x-=a[i]
      i+=1
      cnt+=1
    ans=max(ans,cnt-1)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(2147483647)

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

def calc(k,i,j):
    c1 = 0
    c2 = 0
    global N,M,K,A,B
    if N-1>=i and A[i]+k <= K:
        c1 = 1+calc(A[i]+k,i+1,j)
    if M-1>=j and B[j]+k <= K:
        c2 = 1+calc(B[j]+k,i,j+1)
    return max(c1,c2)
count = calc(0,0,0)
print(count)