Problem:


Score : 300 points

Problem Statement

We have two desks: A and B. Desk A has a vertical stack of N books on it, and Desk B similarly has M books on it.

It takes us A_i minutes to read the i-th book from the top on Desk A (1 \leq i \leq N), and B_i minutes to read the i-th book from the top on Desk B (1 \leq i \leq M).

Consider the following action:

Choose a desk with a book remaining, read the topmost book on that desk, and remove it from the desk.

How many books can we read at most by repeating this action so that it takes us at most K minutes in total? We ignore the time it takes to do anything other than reading.

Constraints

1 \leq N, M \leq 200000

1 \leq K \leq 10^9

1 \leq A_i, B_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M K
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_M

Output

Print an integer representing the maximum number of books that can be read.

Sample Input 1

3 4 240
60 90 120
80 150 80 150

Sample Output 1

3

In this case, it takes us 60, 90, 120 minutes to read the 1-st, 2-nd, 3-rd books from the top on Desk A, and 80, 150, 80, 150 minutes to read the 1-st, 2-nd, 3-rd, 4-th books from the top on Desk B, respectively.

We can read three books in 230 minutes, as shown below, and this is the maximum number of books we can read within 240 minutes.

Read the topmost book on Desk A in 60 minutes, and remove that book from the desk.

Read the topmost book on Desk B in 80 minutes, and remove that book from the desk.

Read the topmost book on Desk A in 90 minutes, and remove that book from the desk.

Sample Input 2

3 4 730
60 90 120
80 150 80 150

Sample Output 2

7

Sample Input 3

5 4 1
1000000000 1000000000 1000000000 1000000000 1000000000
1000000000 1000000000 1000000000 1000000000

Sample Output 3

0

Watch out for integer overflows.



Sample input:
3 4 240
60 90 120
80 150 80 150


Sample output:
3

Submission status: Time Limit Exceeded

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

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

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

        if len_A==0 and weightA + weightB + b > K:
            break

        if weightA + weightB > K:
            break

        if weightA + weightB + b <= K:
            weightB += b
            current_count += 1

        elif terminated==False:
            while weightA + weightB + b > K:
                if len_A>0:
                    weightA -= A[len_A-1]
                    len_A -= 1
                    current_count -= 1
                else:
                    terminated=True

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


    print(max_count)
    return


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

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

        if len_A==0 and weightA + weightB + b > K:
            break

        if weightA + weightB + b <= K:
            weightB += b
            current_count += 1

        elif terminated==False:
            while weightA + weightB + b > K:
                if len_A>0:
                    weightA -= A[len_A-1]
                    len_A -= 1
                    current_count -= 1
                else:
                    terminated=True

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

    print(max_count)
    return


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

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
from sys import exit

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

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

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

max_books = 0
for a_count, a in enumerate(a_book):
    for b_count, b in enumerate(b_book):
        if a + b <= K:
            max_books = max(max_books, a_count+b_count)

print(max_books)

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]

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

    print(ans)


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

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):
    if ans < i :
        if asum[i] <= k:
            ans=i
    else:
        for j in range(ans-i,m+1,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
  #print(a, b, k)
  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(a[1:], b, k-a[0]) + 1
    dict[sstr] = ak
  if (len(b)>0 and b[0]<=k):
    bk = dp(a, b[1:], k-b[0]) + 1
    dict[sstr] = bk
  return max([ak, bk])

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

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[58]:


import numpy as np


# In[93]:


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


# In[96]:


A_sum = [0]
for n in range(N):
    A_sum.append(A_sum[-1]+A[n])
B_sum = [0]
for m in range(M):
    B_sum.append(B_sum[-1]+B[m])
B_sum = np.array(B_sum)


# In[97]:


mat = np.array([A_sum] * (M+1))
for i in range(M+1):
    mat[i] += B_sum[i]
print(max(np.sum(np.array(np.where(mat <= K)).T, axis=1)))


# In[ ]:

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_index = 0
b_index = 0
book = 0
time = 0
for i in a:
    time += i
    if time > k:
        break
    else:
        a_index += 1
        book += 1
ans = 0
for i in range(0, a_index+1):
    book = a_index
    time = sum(a[:i]) + sum(b[0:a_index-i])
    for j in range(a_index-i, m):
        time += b[j]
        if time > k:
            break
        else:
            book += 1
    ans = max(ans, book)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
na,nb,k = map(int, input().split())
ta = list(map(int, input().split()))
tb = list(map(int, input().split()))


def get_bcnt( usek,tb=tb ):
    tb_sum = [ sum(tb[:i]) + tt for i, tt in enumerate(tb)]
    tb_sum_tf = [ i <= usek for i in tb_sum]
    
    if False in tb_sum_tf:
        return tb_sum_tf.index(False)
    else:
        return len(tb)
    
 
readbook = [ i+1 + get_bcnt( k - sum( ta[:i] ) )  for i in range(na) if sum( ta[:i+1] ) <= k ]
readbook.append( get_bcnt(k) )
print( max(readbook) )

Submission status: Time Limit Exceeded

Solution code:
'''
.append(要素) リストに要素を追加
.extend(リスト) リストを連結する
.insert(場所,要素) 指定した場所に要素追加
del 配列名[場所]
-1で末尾で末尾から一つ前

sort()
sorted(reverse=true)

配列の初期化
配列名=[a]*n+[b]*m

Decimal()
'''

from sys import stdin
from itertools import accumulate, dropwhile, takewhile, groupby
import bisect
import copy
import math
from functools import reduce
from operator import mul
from collections import Counter
import itertools
import fractions
import sys


def cumulative_sum(ls):
    res = []
    tmp = 0
    for el in ls:
        tmp += el
        res.append(tmp)
    return res


d1 = stdin.readline().rstrip().split()
N = int(d1[0])
M = int(d1[1])
K = int(d1[2])
A = [int(s) for s in stdin.readline().rstrip().split()]
B = [int(s) for s in stdin.readline().rstrip().split()]


sumsA = cumulative_sum(A)
sumsB = cumulative_sum(B)
countA = 0
countB = 0
res = 0
count = 0

sumsA.insert(0, 0)
sumsB.insert(0, 0)
for elA in sumsA:
    countB = 0
    for elB in sumsB:
        if elA+elB <= K:
            if countA+countB >= res:
                res = countA+countB
        countB += 1
    countA += 1
print(res)

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    '''
    code here
    '''
    import collections
    N, M, K = [int(item) for item in input().split()]
    As = [int(item) for item in input().split()]
    Bs = [int(item) for item in input().split()]

    res = 0
    time = 0

    memo_A = [0]
    for i in range(N):
        memo_A.append(As[i]+memo_A[-1])

    memo_B = [0]
    for i in range(M):
        memo_B.append(Bs[i]+memo_B[-1])

    temp_max = 0
    for i, av in enumerate(memo_A):
        for j, bv in enumerate(memo_B):
            if av + bv <= K:
                temp_max = max(temp_max, i + j)
            else:
                break
    print(temp_max)

if __name__ == "__main__":
    resolve()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[58]:


import numpy as np


# In[93]:


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


# In[94]:


A_sum = [sum(A[:i]) for i in range(N+1)]
B_sum = np.array([sum(B[:i]) for i in range(M+1)])
mat = np.array([A_sum] * (M+1))
for i in range(M+1):
    mat[i] += B_sum[i]
print(max(np.sum(np.array(np.where(mat <= K)).T, axis=1)))


# In[ ]:

Submission status: Time Limit Exceeded

Solution code:
#関数リスト
import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def ruiseki(mydata):
    res = [0]*(len(mydata)+1)
    for i in range(len(mydata)):
        res[i+1] = res[i] + mydata[i]
    return res

n, k , m = MI()
a = ruiseki(LI())
b = ruiseki(LI())
a = a
b = b

result = 0
for i, ii in enumerate(a):
    if ii > m:
        break
    for j, jj in enumerate(b):
        if ii + jj > m:
            break
        if result <= i+j:
            result = i+j

print(result)

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)


consumptions_A = []
for i in range(len(A) + 1):
    consumptions_A.append(sum(A[:i]))

consumptions_B = []
for i in range(len(B) + 1):
    consumptions_B.append(sum(B[:i]))

answer = 0
for i in range(len(consumptions_A)):
    if (consumptions_A[i] > K): break

    for j in range(len(consumptions_B)):
        consumption = consumptions_A[i] + consumptions_B[j]
        if (consumption > K): break
    
        num_books = i + j
        answer = max(answer, num_books)

print(answer)

Submission status: Time Limit Exceeded

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

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

A = deque(map(int, input().split()))
B = deque(map(int, input().split()))

dp = [[0] * (M+2) for _ in range(N+2)]

dp[0][0] = 0

max_c = 0
for i in range(N):
  for j in range(M):
    dp[i][j+1] = dp[i][j] + B[j]
    dp[i+1][j] = dp[i][j] + A[i]
    dp[i+1][j+1] = dp[i][j] + A[i] + B[j]
    if dp[i+1][j] <= K or dp[i][j+1] <= K:
      max_c = i+j+1
    if dp[i+1][j+1] <= K:
      max_c = i+j+2

print(max_c)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
alen,blen,k = map(int,input().split())
alist = list(map(int,input().split()))
blist = list(map(int,input().split()))
max = 0

for a in range(alen+1):
    acount = alen-a
    bcount = 0
    asum = sum(alist[:acount])
    while asum + sum(blist[:bcount+1]) < k and bcount <= blen:
        bcount += 1
    if acount + bcount > max and asum + sum(blist[:bcount])<=k:
        max = acount + bcount

print(max)

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np
import math  # gcdもあるよ
from collections import deque
from collections import defaultdict
from copy import deepcopy
from itertools import accumulate #リストを与えると累積和を返す
def input(): return sys.stdin.readline().rstrip()
from functools import lru_cache #メモ化

def main():
    n, m, k = list(map(int,sys.stdin.readline().split()))
    a = list(map(int,sys.stdin.readline().split()))
    b = list(map(int,sys.stdin.readline().split()))
    ans_list = []

    # a_p : aから読む冊数
    a_p = n
    for i in range(n+1):
        if sum(a[:i]) > k:
            a_p = i - 1
            break
    #print(a_p)

    b_p = 0
    for i in range(a_p):
        for j in range(b_p, m + 1):
            if sum(a[:a_p - i]) + sum(b[:j]) > k:
                b_p = j - 1
                break
            b_p = j
        ans_list.append(a_p + b_p)
    if ans_list == []:
        print("0")
        return 0
    print(max(ans_list))
    return 0

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
for i in range(1, n+m+1):
  for j in range(i+1):
    l = []
    l.extend(a[:min(i-j, n)])
    l.extend(b[:min(j, m)])
    if sum(l) <= k:
      ans = max(ans, len(l))
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()))

# print(N,M,K)
# print(A)
# print(B)
sum_on = 0
count = 0
i = 0
if K < A[0]:
    pass
else:
    while True:
        if sum_on < K:
            if len(A) == 0:
                sum_on += B.pop(i)
                count += 1
                continue
            else:
                sum_on += A.pop(i)
                count += 1
                continue
                if A[i] > B[i]:
                    sum_on += B.pop(i)
                    count += 1
                    continue
                else:
                    sum_on += A.pop(i)
                count += 1
                continue
        else:
            break
        break   
print(count)

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):
    if ans < i :
        if asum[i] <= k:
            ans=i
    else:
        for j in range(ans-i,m+1,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
input = sys.stdin.readline

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

a_wa = [0]
max_a_idx = None
for idx, a in enumerate(A):
    a_wa.append(a_wa[-1] + a)

    if a_wa[-1] > K:
        break

max_a_idx = len(a_wa)
b_wa = [0]
max_b_idx = None
for idx, b in enumerate(B):
    b_wa.append(b_wa[-1] + b)

    if b_wa[-1] > K:
        break

max_b_idx = len(b_wa)

if max_a_idx + max_b_idx == 2:
    print(0)
    exit()
elif max_a_idx == 1:
    print(max_b_idx)
    exit()
elif max_b_idx == 1:
    print(max_a_idx + 1)
    exit()

out_num = 0
for a_idx in range(max_a_idx):
    for b_idx in range(max_b_idx):
        if a_wa[a_idx] + b_wa[b_idx] <= K:
            out_num = max(out_num, a_idx + b_idx)

print(out_num)

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np
import math  # gcdもあるよ
from collections import deque
from collections import defaultdict
from copy import deepcopy
from itertools import accumulate #リストを与えると累積和を返す
def input(): return sys.stdin.readline().rstrip()
from functools import lru_cache #メモ化

def main():
    n, m, k = list(map(int,sys.stdin.readline().split()))
    a = list(map(int,sys.stdin.readline().split()))
    b = list(map(int,sys.stdin.readline().split()))
    ans_list = []

    # a_p : aから読む冊数
    a_p = n
    for i in range(n+1):
        if sum(a[:i]) > k:
            a_p = i - 1
            break
    #print(a_p)

    b_p = 0
    for i in range(a_p):
        for j in range(m + 1):
            if sum(a[:a_p - i]) + sum(b[:j]) > k:
                b_p = j - 1
                break
            b_p = j
        ans_list.append(a_p + b_p)
    if ans_list == []:
        print("0")
        return 0
    print(max(ans_list))
    return 0

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
N, M, K = [int(x) for x in input().split()]
tempA = [int(x) for x in input().split()]
tempB = [int(x) for x in input().split()]
A = [0]
B = [0]
s = 0
t = 0
for a in tempA:
    s += a
    A.append(s)
for b in tempB:
    t += b
    B.append(t)
lst = []
ans = 0
for i in range(N+1):
    for j in range(M+1):
        if K >= A[i] + B[j]:
            ans = max(ans, i+j)
        else:
            break
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()))
from itertools import accumulate
AS = [0] + list(accumulate(A))
BS = [0] + list(accumulate(B))

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

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
from itertools import accumulate
import numba
import numpy as np


def debug(*x):
    print(*x)


@numba.njit
def solve(N, M, K, sA, sB):
    "void()"

    last_can_read = 0
    for ij in range(1, N + M + 2):
        # debug(": ij", ij)
        for i in range(0, ij + 1):
            j = ij - i
            # debug(": i,j", i, j)
            if j > M:
                # debug("cont: ", )
                continue
            if i > N:
                # debug("break: ", )
                break
            cost = sA[i] + sB[j]
            # debug(": cost", cost)
            if cost <= K:
                # can read
                # debug("can read: ", i, j, cost)
                last_can_read = ij
                break
        else:
            # no break = can't read
            break

    print(last_can_read)


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

    AS = list(map(int, input().split()))
    BS = list(map(int, input().split()))

    sA = np.array([0] + list(accumulate(AS)))
    sB = np.array([0] + list(accumulate(BS)))
    # debug("s:sA,sB", sA, sB)
    solve(N, M, K, sA, sB)


def _test():
    """
    >>> 
    >>> main()
    """
    import doctest
    as_input('''
    3 4 240
    60 90 120
    80 150 80 150
    ''')
    main()
    doctest.testmod()


def as_input(s):
    "use in test, use given string as input file"
    import io
    global read, input
    f = io.StringIO(s.strip())
    input = f.readline
    read = f.read


USE_NUMBA = False
if (USE_NUMBA and sys.argv[-1] == 'ONLINE_JUDGE') or sys.argv[-1] == '-c':
    print("compiling")
    from numba.pycc import CC
    cc = CC('my_module')
    cc.export('solve', solve.__doc__.strip().split()[0])(solve)
    cc.compile()
    exit()
else:
    input = sys.stdin.buffer.readline
    read = sys.stdin.buffer.read

    if (USE_NUMBA and sys.argv[-1] != '-p') or sys.argv[-1] == "--numba":
        # -p: pure python mode
        # if not -p, import compiled module
        from my_module import solve  # pylint: disable=all
    elif sys.argv[-1] == "-t":
        _test()
        sys.exit()
    elif sys.argv[-1] != '-p' and len(sys.argv) == 2:
        # input given as file
        input_as_file = open(sys.argv[1])
        input = input_as_file.buffer.readline
        read = input_as_file.buffer.read

    main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

A_array = np.array(list(map(int, input().split())))

B_array = np.array(list(map(int, input().split())))

cumA_array = np.cumsum(A_array)
cumB_array = np.cumsum(B_array)


if np.sum(A_array) + np.sum(B_array) <= K:
    print(N + M)
    
elif A_array[0] > K and  B_array[0] > K:
    print(0)

elif A_array[0] > K and  B_array[0] <= K:    
    print(np.sum((cumB_array <= K)))
    
elif A_array[0] <= K and  B_array[0] > K:
    print(np.sum((cumA_array <= K)))        
    
else:
    result = []
    result.append(np.sum((cumA_array <= K)))
    result.append(np.sum((cumB_array <= K)))     
    
    for i in range(N):
        if cumA_array[i] > K:
            break
        for j in range(M):
            if cumA_array[i] + cumB_array[j] > K:
                break
                
        result.append(i + 1 + j)     
        
    print(max(result))

Submission status: Time Limit Exceeded

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

import sys
import numpy as np
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

N, M, K = map(int, readline().split())
A = list(map(int, readline().split()))[::-1]
B = list(map(int, readline().split()))[::-1]

cnt = 0
tmp = 0
res = [0]
for a in A:
    if sum(res) + a <= K:
        res.append(a)
        cnt += 1
    else:
        break
for b in B:
    if sum(res) + b <= K:
        res.append(b)
        cnt += 1
    else:
        break
ans = cnt

cnt = 0
tmp = 0

for b in B:
    if tmp + b <= K:
        tmp += b
        cnt += 1
    else:
        break
for a in A:
    if tmp + a <= K:
        tmp += a
        cnt += 1
    else:
        break
ans = max(ans, cnt)

tot = 0
cnt = 0

while True:
    if len(A) > 0:
        acc_a = A.pop()
    else:
        acc_a = sys.maxsize
    if len(B) > 0:
        acc_b = B.pop()
    else:
        acc_b = sys.maxsize
    if acc_a == acc_b == sys.maxsize:
        break
    acc = min(acc_a, acc_b)

    if tot + acc <= K:
        cnt += 1
    else:
        break

    tot += acc
    if acc_a <= acc_b:
        B.append(acc_b)
    else:
        A.append(acc_a)

ans = max(ans, cnt)

print(ans)

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 b_index >= len(B):
        break
    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:
INF = 10**9+7
N, M, K = [int(x) for x in input().split()]
tempA = [int(x) for x in input().split()]
tempB = [int(x) for x in input().split()]
A = [0]
B = [0]
s = 0
t = 0
for a in tempA:
    s += a
    A.append(s)
for b in tempB:
    t += b
    B.append(t)
lst = []
for i in range(N+1):
    for j in range(M+1):
        lst.append((i,j))
ans = 0
for i, j in lst:
    if K >= A[i] + B[j]:
        ans = max(ans, i+j)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(4100000)

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

ans_cand = []

def dfs(a_i, b_i, ans, time):
    if time == K:
        ans_cand.append(ans)
        return 0

    if time > K:
        ans_cand.append(ans - 1)
        return 0

    if a_i < N:
        dfs(a_i + 1, b_i, ans + 1, time + A[a_i])
    if b_i < M:
        dfs(a_i, b_i + 1, ans + 1, time + B[b_i])
    return 0


dfs(0, 0, 0, 0)

print(max(ans_cand))

Submission status: Time Limit Exceeded

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

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

AS = list(it.accumulate(A))
BS = list(it.accumulate(B))
AS.insert(0,0)
BS.insert(0,0)
max = 0
for i in range(len(AS)):
    a = AS[i]
    if a > K:
        continue
    for j in range(len(BS)):
        b = BS[j]
        if a + b > K:
            break
        if max < i + j:
            max = i + j

print(max)

Submission status: Time Limit Exceeded

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

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([sum_A[:mark_A+1]]*(mark_B+1)) + np.array(sum_B[:mark_B+1]).reshape(-1, 1)
# 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:
n,m,k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
readable = 0
for rx in range(1,n+m+1):
  if readable == rx: break
  for ra in range(0,rx+1):
    rb = rx - ra
    rt = 0
    if ra <= len(a) and rb <= len(b):
      if ra>0 :
        for i in range(0,ra):
          rt += a[i]
      if rb>0 :
        for i in range(0,rb):
          rt += b[i]
      if rt <= k:
        readable = rx
        break
print(readable)

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

timeleft = K
counter = 0

flag = True
while flag:
    if len(A) == 0 and len(B) == 0:
        flag = False
    elif len(A) == 0:
        if B[0] <= timeleft:
            counter += 1
            timeleft -= B[0]
            B.pop(0)
        else:
            flag = False
    elif len(B) == 0:
        if A[0] <= timeleft:
            counter += 1
            timeleft -= A[0]
            A.pop(0)
        else:
            flag = False
    elif len(A) == 1 and len(B) == 1:
        if A[0] < B[0]:
            if A[0] <= timeleft:
                counter += 1
                timeleft -= A[0]
                A.pop(0)
            else:
                flag = False
        else:
            if B[0] <= timeleft:
                counter += 1
                timeleft -= B[0]
                B.pop(0)
            else:
                flag = False
    elif len(A) == 1:
        if A[0] < B[0]:
            if A[0] <= timeleft:
                counter += 1
                timeleft -= A[0]
                A.pop(0)
            else:
                flag = False
        else:
            if B[0] <= timeleft:
                counter += 1
                timeleft -= B[0]
                B.pop(0)
            else:
                flag = False
    elif len(B) == 1:
        if B[0] < A[0]:
            if B[0] <= timeleft:
                counter += 1
                timeleft -= B[0]
                B.pop(0)
            else:
                flag = False
        else:
            if A[0] <= timeleft:
                counter += 1
                timeleft -= A[0]
                A.pop(0)
            else:
                flag = False 
    else:
        if A[0] < B[0]:
            if B[0] <= timeleft:
                if A[0] + A[1] < B[0] + B[1]:
                    timeleft -= A[0]
                    counter += 1
                    A.pop(0)
                else:
                    timeleft -= B[0]
                    counter += 1
                    B.pop(0)
            elif A[0] <= timeleft:
                timeleft -= A[0]
                counter += 1
                A.pop(0)
            else:
                flag = False
        else:
            if A[0] <= timeleft:
                if B[0] + B[1] < A[0] + A[1]:
                    timeleft -= B[0]
                    counter += 1
                    B.pop(0)
                else:
                    timeleft -= A[0]
                    counter += 1
                    A.pop(0)
            elif B[0] <= timeleft:
                timeleft -= B[0]
                counter += 1
                B.pop(0)
            else:
                flag = False
print(counter)