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

A_ruiseki = [0]
a_ruiseki = 0
B_ruiseki = [0]
b_ruiseki = 0
for a in A:
    a_ruiseki += a
    A_ruiseki.append(a_ruiseki)
for b in B:
    b_ruiseki += b
    B_ruiseki.append(b_ruiseki)

count_max = 0
for i, a in enumerate(A_ruiseki):
    for k, b in enumerate(B_ruiseki):
        if a+b <= K and i + k > count_max:
            count_max = i+k
            
print(count_max)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
def resolve():
    N, M, K = list(map(int, input().split()))
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    read = deque()
    w = 0
    while w <= K:
        if len(A) == 0 and len(B) == 0:
            break

        if len(A) == 0:
            if w + B[0] <= K:
                read.append(B[0])
                w += B[0]
            B.pop(0)
            continue

        if len(B) == 0:
            if w + A[0] <= K:
                read.append(A[0])
                w += A[0]
            A.pop(0)
            continue

        if A[0] < B[0]:
            if w + A[0] <= K:
                read.append(A[0])
                w += A[0]
            A.pop(0)
        else:
            if w + B[0] <= K:
                read.append(B[0])
                w += B[0]
            B.pop(0)

    print(len(read))


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()))
sa=[]
ka=0
sb=[]
kb=0
for i in range(n):
  if ka+a[i]<=k:
    ka+=a[i]
    sa.append(a[i])
  else:
    break
for i in range(m):
  if kb+b[i]<=k:
    kb+=b[i]
    sb.append(b[i])
  else:
    break
ans=max(len(sa),len(sb))
tb=[]
for i in range(len(sa)):
  if sa[i]>k:
    break
  while kb+sa[i]>k:
    kb-=sb[-1]
    sb.pop(-1)
  sb=[sa[i]]+sb
  tb.append(sa[i])
  kb+=sa[i]
  if ans<(len(sb)+len(tb)):
    ans=len(sb)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import accumulate
from sys import exit
N, M, K = map(int, input().split())
A = [0] + list(accumulate(list(map(int, input().split()))))
B = [0] + list(accumulate(list(map(int, input().split()))))
n = -1
m = -1
for a in A:
  n += 1
  if a >= K:
    break
for b in B:
  m += 1
  if b >= K:
    break
ans = 0
for i in range(n + 1):
  for j in range(m, -1, -1):
    if A[i] + B[j] <= K:
      if ans <= i + j:
        ans = max(ans, i + j)
        break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

def main():
    n, m, k = [int(x) for x in stdin.readline().rstrip().split()]

    pileda = 0
    a = [0]
    for x in stdin.readline().rstrip().split():
        x = int(x)
        pileda += x
        a.append(pileda)
    
    piledb = 0
    b = [0]
    for x in stdin.readline().rstrip().split():
        x = int(x)
        piledb += x
        b.append(piledb)

    ans = 0
    for ai, time_a in enumerate(a):
        if time_a > k:
            break
        for bi, time_b in enumerate(b):
            if time_a + time_b > k:
                break
            if time_a + time_b <= k and ai + bi > ans:
                ans = ai + bi
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from functools import reduce

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

cumA = reduce(lambda x, y: x + [y + x[-1]], As, [0])
cumB = reduce(lambda x, y: x + [y + x[-1]], Bs, [0])
cumA.append(np.inf)
cumB.append(np.inf)
cumA = np.array(cumA)
cumB = np.array(cumB)

max_count = 0
a_thresh = np.where(cumA > K)[0][0]
a_count = a_thresh
b_count = 0
b_thresh = np.where(cumB > K)[0][0]
while a_count >= 0 and b_count <= b_thresh:
    while b_count <= b_thresh and cumA[a_count] + cumB[b_count] <= K:
        b_count += 1
    b_count = max(0, b_count - 1)
    if cumA[a_count] + cumB[b_count] <= K:
        max_count = max(max_count, a_count + b_count)
    a_count -= 1

print(max_count)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

from bisect import bisect_left


def leq_search(xs, x, s, t):
    # x以下で最大のindex
    if t-s <= 5:
        res = 0
        for i in range(s, t):
            if xs[i] <= x:
                res = i
        return res

    return max(leq_search(xs, x, s, (s+t)//2), leq_search(xs, x, (s+t)//2, t))



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

    S = [None] * (N+1)
    S[0] = 0
    for i in range(1, N+1):
        S[i] = S[i-1] + A[i-1]

    T = [None] * (M+1)
    T[0] = 0
    for i in range(1, M+1):
        T[i] = T[i-1] + B[i-1]

    n_read = 0
    for i in range(N+1):
        # print(i)
        if S[i] > K:
            break
        n = leq_search(T, K-S[i], 0, M+1)
        # print(n, T, K-S[i])
        if n_read < n + i:
            n_read = n + i
    
    return n_read


print(main())

Submission status: Time Limit Exceeded

Solution code:
def _integral(A):
    result = []
    sum_value = 0
    result.append(sum_value)
    for i in range(len(A)):
        sum_value += A[i]
        result.append(sum_value)
    return result


def _main(N, M, K, A, B):
    integral_a = _integral(A)
    integral_b = _integral(B)
    top_count = 0
    for a_index in range(len(A) + 1):
        a_value = integral_a[a_index]
        for b_index in range(len(B) + 1):
            if a_value + integral_b[b_index] <= K:
                if top_count < a_index + b_index:
                    top_count = a_index + b_index
    print(top_count)
    #
    #
    # sum_k = 0.0
    # a_index = 0
    # a_len = len(A)
    # b_index = 0
    # b_len = len(B)
    # while sum_k < K:
    #     current_a = A[a_index] if a_index < a_len else None
    #     current_b = B[b_index] if b_index < b_len else None
    #     if current_a is None and current_b is None:
    #         break
    #
    #     if current_a is not None and current_b is None:
    #         if K < sum_k + current_a:
    #             break
    #         sum_k += current_a
    #         a_index += 1
    #     elif current_a is None and current_b is not None:
    #         if K < sum_k + current_b:
    #             break
    #         sum_k += current_b
    #         b_index += 1
    #     elif current_a < current_b:
    #         if K < sum_k + current_a:
    #             break
    #         sum_k += current_a
    #         a_index += 1
    #     else:
    #         if K < sum_k + current_b:
    #             break
    #         sum_k += current_b
    #         b_index += 1
    #
    # print(a_index + b_index)


if __name__ == "__main__":
    Na, Ma, Ka = map(int, input().split())
    Aa = list(map(float, input().split()))
    Ba = list(map(float, input().split()))
    _main(Na, Ma, Ka, Aa, Ba)

Submission status: Time Limit Exceeded

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

a_lst = sorted(a_lst)
b_lst = sorted(b_lst)
count = []
for i in range(len(a_lst)+len(b_lst)):
    if sum(count) >= k:
        break
    if a_lst != [] and b_lst != []:
        if a_lst[0] <= b_lst[0]:
            count.append(a_lst.pop(0))
        else:
            count.append(b_lst.pop(0))
    elif a_lst == []:
        count.append(b_lst.pop(0))
    elif b_lst == []:
        count.append(a_lst.pop(0))
if sum(count) > k:
    print(len(count)-1)
else:
    print(len(count))

Submission status: Time Limit Exceeded

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


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


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

Submission status: Time Limit Exceeded

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

# print(n, m, k)
# print(a)
# print(b)

ans = set()
ans.add(0)
time_sum_a = 0
for i_n in range(0, n+1):
    if time_sum_a > k:
        break

    if i_n != 0:
        time_sum_a += a[i_n-1]
    time_sum_b = 0
    for i_m in range(0, m+1):
        if time_sum_a + time_sum_b > k:
            break
        if i_m != 0:
            time_sum_b += b[i_m-1]

        time_sum = time_sum_a + time_sum_b
        # print(i_n, a[:i_n], i_m, b[:i_m], time_sum, k)
        if time_sum <= k:
            # print(i_n, a[:i_n], i_m, sum(b[:i_m]), k)
            ans.add(i_n + i_m)

# print(ans)

print(list(ans)[-1])

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]

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:
def search (k,c,a,b):
    if k > 0:
        na = a[1:len(a)]
        nb = b[1:len(b)]
        if len(a) > 0 and len(b) > 0:
            return max( search(k - a[0] ,c+1,na,b) , search(k- b[0] , c+1, a,nb) )
        elif len(a) <= 0:
            return search(k- b[0] , c+1, a,nb)
        else:
            return search(k - a[0] ,c+1,na,b)
    elif k == 0:
        return c
    else:
        return c-1





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


print(search(k,0,a,b))

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()))
C=A+B
C.sort()
if sum(C)<=K :
    print(len(C))
    sys.exit()
for i in range(len(C)) :
    if sum(C[:i])>K :
        print(i-1)
        sys.exit()

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()))
AB=sorted(A+B)
NM=N+M
num=0
ans=0
while num<K:
    if NM==0:
        break
    ab = AB[0]
    if num + ab <= K:
        num += ab
        ans += 1
        del AB[0]
        NM-=1
    else:
        break
print(ans)

Submission status: Time Limit Exceeded

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

n_time = list(map(int, input().split()))
m_time = list(map(int, input().split()))

time = n_time + m_time

result = 0
count = 0

for i in range(len(time)):
    if(result > max_time):
        if(count < 1):
            break
        else:
            count -= 1
            break
    else:
        result += min(time)
        time.remove(min(time))
        count += 1

print(count)

Submission status: Time Limit Exceeded

Solution code:
from itertools import accumulate
from sys import exit
N, M, K = map(int, input().split())
A = [0] + list(accumulate(list(map(int, input().split()))))
B = [0] + list(accumulate(list(map(int, input().split()))))
n = -1
m = -1
for a in A:
  n += 1
  if a >= K:
    break
for b in B:
  m += 1
  if b >= K:
    break
ans = 0
for i in range(n + 1):
  for j in range(m, -1, -1):
    if A[i] + B[j] <= K:
      if ans <= i + j:
        ans = max(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()))

ruiA, ruiB = [0], [0]

for i in range(N):
    ruiA.append(ruiA[i] + A[i])

for j in range(M):
    ruiB.append(ruiB[j] + B[j])

tmp = 0
for i in range(N + 1):
    for j in range(M + 1):
        if ruiA[i] + ruiB[j] <= K:
            tmp = max(tmp, i + j)

print(tmp)

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

dp = {}
dp_a = {}
dp_b = {}

s = 0
for i, a in enumerate(A):
    s += a
    if s > K:
        break
    dp_a[s] = i+1

s = 0
newb = {}
for i, b in enumerate(B):
    s += b
    if s > K:
        break
    dp_b[s] = i+1
    for k, v in dp_a.items():
        if k+s > K:
            break
        if k+s in newb:
            newb[k+s] = min(newb[k+s], v + i+1)
        else:
            newb[k+s] = v + i+1

newa = {}
for i, a in enumerate(A):
    s += a
    if s > K:
        break
    for k, v in dp_b.items():
        if k+s > K:
            break
        if k+s in newa:
            newa[k+s] = min(newa[k+s], v + i+1)
        else:
            newa[k+s] = v + i+1

ans = 0
for k in range(K, -1, -1):
    if k in newb:
        ans = max(ans, newb[k])
    if k in newa:
        ans = max(ans, newa[k])
    if k in dp_b:
        ans = max(ans, dp_b[k])
    if k in dp_a:
        ans = max(ans, dp_a[k])

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



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

maxval=len(Asum)-1

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

Submission status: Time Limit Exceeded

Solution code:
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
sa=[]
ka=0
sb=[]
kb=0
for i in range(n):
  if ka+a[i]<=k:
    ka+=a[i]
    sa.append(a[i])
  else:
    break
for i in range(m):
  if kb+b[i]<=k:
    kb+=b[i]
    sb.append(b[i])
  else:
    break
ans=max(len(sa),len(sb))
for i in range(len(sa)):
  if sa[i]>k:
    break
  while kb+sa[i]>k:
    kb-=sb[-1]
    sb.pop(-1)
  sb=[sa[i]]+sb
  kb+=sa[i]
  if ans<len(sb):
    ans=len(sb)
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()))

tmp = []
s = 0
for i in a:
    if s + i <= k:
        tmp.append(i)
        s += i
a = tmp
tmp = []
s = 0
for i in b:
    if s + i <= k:
        tmp.append(i)
        s += i
b = tmp
line = b[::-1] + a

ans = 0
left = 0
for left in range(len(line)):
    sum_ = 0
    right = left
    while right < len(line) and sum_ + line[right] <= k:
        sum_ += line[right]
        right += 1
    l = right -left
    if l > ans:
        ans = l
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,M,V = list(map(int,input().split()))
A=list(map(int,input().split()))
B=list(map(int,input().split()))
S=list(map(int,A+B))
ans=0
time=0
if sum(S)<=V:
  print(N+M)
  exit()
K=sorted(S)
for i in range(N+M):
  if K[0]+time<=V:
    time+=K[0]
    K.pop(0)
    ans+=1
  else:
    print(ans)
    exit()

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

    read = deque()

    while sum(read) <= K:
        if len(A) == 0 and len(B) == 0:
            break

        if len(A) == 0:
            if sum(read) + B[0] <= K:
                read.append(B[0])
            B.pop(0)
            continue

        if len(B) == 0:
            if sum(read) + A[0] <= K:
                read.append(A[0])
            A.pop(0)
            continue

        if A[0] < B[0]:
            if sum(read) + A[0] <= K:
                read.append(A[0])
            A.pop(0)
        else:
            if sum(read) + B[0] <= K:
                read.append(B[0])

            B.pop(0)

    print(len(read))


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

cumA, cumB = [0], [0]
sa, sb = 0, 0

for a in A:
    sa += a
    cumA.append(sa)
for b in B:
    sb += b
    cumB.append(sb)

ans = 0

for i in range(N+1):
    a = cumA[i]
    if a > K:
        break
    for j in range(M, -1, -1):
        if a + cumB[j] <= K:
            break
    
    ans = max(i + j, ans)    

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


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

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

total = 0
for i in range(len(A_cs)):
    for j in range(len(B_cs)):
        if A_cs[i]+B_cs[j]<=K:
            if total < i+ j:
                total = i+ j

print(total)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

def main():
    n, m, k = [int(x) for x in stdin.readline().rstrip().split()]

    pileda = 0
    a = [0]
    for x in stdin.readline().rstrip().split():
        x = int(x)
        pileda += x
        a.append(pileda)
    
    piledb = 0
    b = [0]
    for x in stdin.readline().rstrip().split():
        x = int(x)
        piledb += x
        b.append(piledb)

    ans = 0
    for ai, time_a in enumerate(a):
        if time_a > k:
            break
        for bi, time_b in enumerate(b):
            if time_b > k:
                break
            if time_a + time_b <= k and ai + bi > ans:
                ans = ai + bi
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

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

time = 0
ans = 0
a = 0

for i in range(n+1):
    a += a_li[i]
    b = 0
    for j in range(m+1):
        b += b_li[j]
        time = a + b
        if time <= k:
            ans = max(ans, i+j)
        else:
            break
            
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#import collections
# c = collections.Counter()
#import itertools as it
#import math
#import numpy as np
import sys 
#sys.exit()
 
#s = input()
#t = input()
 
#k = int(input())
#a, b  = map(int, input().split())
'''
#  = list(map(int, input().split()))
'''
'''
n = input()
a = [int(input()) for i in range(n)]
'''
#a = [list(map(int, input().split())) for _ in range(n)]
n,m,k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

#-------------------------------#
#len(t)==len(s)+1
#print(t[0:-1])
'''
count=0
for i in range (len(s)):
    if s[i]!=t[i]:
        count=count+1
'''
c=a+b
d=sorted(c)
count=0
for i in range (len(d)):
    if sum(d[0:i+1])<=k:
        count=count+1
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()))

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

Submission status: Time Limit Exceeded

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

ans = 0

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


for a in range(len(A)+1):
    sums_A[a] = sum(A[:a])

for b in range(len(B)+1):
    sums_B[b] = sum(B[:b])


for a in range(len(A)+1):
    K_remain = K - sums_A[a]
    if K_remain >= 0:
        for b in range(len(B)+1):
            if K_remain >= sums_B[b]:
                if ans < a+b:
                    ans = a+b
            else:
                break
    else:
        break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
import sys
import re
from collections import deque, defaultdict
from itertools import combinations, combinations_with_replacement
import bisect
from math import factorial, sqrt, log, ceil, floor
sys.setrecursionlimit(1000000)


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

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

    max_num = 0
    for i in range(len(As)+1):
        k = sum(As[:i])
        if k > K:
            break
        num = i
        k += sum(Bs)
        b_num = len(Bs)
        for j in range(b_num+1):
            if k <= K:
                num += len(Bs)
                max_num = max([max_num, num])
                break
            else:
                if Bs:
                    b = Bs.pop(-1)
                    k -= b

    print(max_num)



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

mmax = 0
pre = False
num = 0
for i in range(N+1):
  ans = 0
  if i == 0:
    ans = 0
  else:
    ans = sum(A[:i])
  if ans > K:
    break
  for j in range(M+1):
    temp = ans
    if j == 0:
      ans += 0
    else:
      ans += sum(B[:j])
    if ans > K:
      break
    else:
      mmax = max(mmax, i+j)
    ans = temp

print(mmax)

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

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

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


n = 0
time = 0

for i, a in enumerate(at):
    if a > K:
        break
    for j, b in enumerate(bt):
        if a+b > K:
            break
        if a+b <= K and a+b >= time:
            time = a+b
            n = i + j

print(n)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
from bisect import bisect

N, M, K = map(int, input().split())
A = list(map(int, input().split()))
R_A = deque()
R_A.append(0)
i = 0
while i < N:
  R_A.append(R_A[-1]+A[i])
  i += 1 
B = list(map(int, input().split()))
R_B = deque()
R_B.append(0)
i = 0
while i < M:
  R_B.append(R_B[-1]+B[i])
  i += 1 
R_B = list(R_B)
ans = 0
i = 0
while i <= N:
  m = K - R_A[i]
  if m < 0:
    break
  idx = bisect(R_B, m)
  ans = max(ans, i+idx-1)
  R_B = R_B[:idx+1]
  i += 1
print(ans)

Submission status: Time Limit Exceeded

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

from itertools import accumulate
from collections import deque
A=deque(list(accumulate(A)))
B=deque(list(accumulate(B)))
A.appendleft(0)
B.appendleft(0)
ans=0
for i in range(N+1):
    if A[i]>K:
        break
    else:
        ans=max(ans,i)
        for j in range(M+1):
            if A[i]+B[j]>K:
                break
            else:
                ans=max(ans,i+j)
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()))



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

maxval=len(Asum)-1

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

Submission status: Time Limit Exceeded

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

if min(A[0],B[0])>K:
  print(0)
  exit()

c=[0 for _ in range(N)] + [1 for _ in range(M)]
C=list(permutations(c,len(c)))

for i in range(len(C)):
  countA=0
  countB=0
  ans=0
  answer=0
  for j in range(len(c)):
    if C[i][j]==0:
      ans+=A[countA]
      countA+=1
    else:
      ans+=B[countB]
      countB+=1
    if ans>=K and countA+countB>answer:
      answer=countA+countB
      break
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()))
SUM_A=[0 for _ in range(N+1)]
for i in range(N):
    SUM_A[i+1] = A[i] + SUM_A[i]

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

cnt = 0
for i in range(N+1):
    for j in range(M+1):
        if SUM_A[i]+SUM_B[j] <= K:
            if i+j > cnt:
                cnt = i+j
        else:
            break
print(cnt)

Submission status: Time Limit Exceeded

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

flag = False
a_total = 0
for i in range(N):
    a_total += A[i]
    if a_total > K:
        flag = True
        break

if flag:
    a_max = i
    ans = a_max
else:
    a_max = N
    ans = N

for i in range(a_max+1)[::-1]:
    total = sum(A[:i])
    #print('b total: ', total)
    flag = False
    for j in range(M):
        total += B[j]
        #print('a total: ', total)
        if total > K:
            flag = True
            break
    if flag:
        ans = max(ans, i + j)
    else:
        ans = max(ans, i + j + 1)
    #print('ans: ', ans)
    #print()

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy

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

A = numpy.cumsum(numpy.array(A))
A = list(A)
A.insert(0, 0)
B = numpy.cumsum(numpy.array(B))
B = list(B)
B.insert(0, 0)
ans = 0

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


ll = [(0, 0, 0)]
ans = 0

while len(ll) > 0:
    c, an, bn = ll.pop(0)
    if an < n:
        if c+a[an] <= k:
            ll.append((c+a[an], an+1, bn))
        elif an + bn > ans:
            ans = an+bn
    else:
        if an + bn > ans:
            ans = an+bn

    if bn < m:
        if c+b[bn] <= k:
            ll.append((c+b[bn], an, bn+1))
        elif an + bn > ans:
            ans = an+bn
    else:
        if an + bn > ans:
            ans = an+bn

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,m,k=map(int,input().split())
l1=list(map(int,input().split()))
l2=list(map(int,input().split()))
l1.append(100000000000)
l2.append(100000000000)
val=0
i=0
j=0
temp=0
while(val<k):
    if(l1[0]<l2[0]):
        val+=l1[0]
        i+=1
        l1.remove(l1[0])
        temp+=1
    elif(l1[0]>l2[0]):
        val+=l2[0]
        j+=1
        l2.remove(l2[0])
        temp+=1
    elif(l1[0]>k)and(l2[0]>k):
        temp+=1
        break
if(val==k):
    print(temp)
else:
    print(temp-1)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
from functools import reduce

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

cumA = reduce(lambda x, y: x + [y + x[-1]], As, [0])
cumB = reduce(lambda x, y: x + [y + x[-1]], Bs, [0])

max_count = 0
a_count = len(As)
b_count = 0
while a_count >= 0 and b_count < len(Bs):
    while b_count <= len(Bs) and cumA[a_count] + cumB[b_count] <= K:
        b_count += 1
    b_count = max(0, b_count - 1)
    if cumA[a_count] + cumB[b_count] <= K:
        max_count = max(max_count, a_count + b_count)
    a_count -= 1

print(max_count)

Submission status: Time Limit Exceeded

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

A.extend(B)
AB = sorted(A)

cnt = []
ans = 0
for ab in AB:
    cnt.append(ab)
    if sum(cnt) <= K:
        ans += 1
    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()))
atime = {0:0}
btime = {0:0}
for i in range(1,n+1):
  if atime[i-1]+a[i-1]<=k:
    atime[i] = atime[i-1]+a[i-1]
  else:
    n = i-1
    break
for i in range(1,m+1):
  if btime[i-1]+b[i-1]<=k:
    btime[i] = btime[i-1]+b[i-1]
  else:
    m = i-1
    break
book = 0
for i in range(n+1):
  if atime[i]>k:
    break
  else:
    for j in range(m+1):
      if atime[i]+btime[j]>k:
        break
      else:
        book = max(book,i+j)
print(book)