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:
def main():
    import sys
    sys.setrecursionlimit(10 ** 7)

    INF = 10 ** 9 + 10

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

    memo = [[-1] * (len(B) + 1) for _ in range(len(A) + 1)]

    def rec(t, ai, bi):
        if t > K: return -INF
        if ~memo[ai][bi]: return memo[ai][bi]

        ac, bc = INF, INF
        if ai < len(A): ac = A[ai]
        if bi < len(B): bc = B[bi]

        ret = max(
            0,
            rec(t + ac, ai + 1, bi) + 1,
            rec(t + bc, ai, bi + 1) + 1
        )

        memo[ai][bi] = ret
        return ret

    print(rec(0, 0, 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(len(A)+1):
    for j in range(len(B)+1):
        if sum(A[:i])+sum(B[:j])<=k:
            ans=max(ans,i+j)
print(ans)

Submission status: Time Limit Exceeded

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

time = [[0] * (M+1) for i in range(N+1)]

for i in range(N+1):
    for j in range(M+1):
        if i == 0 and j == 0:
            time[i][j] = 0
            read = 0
        if i != 0 and j == 0:
            time[i][j] = time[i-1][j] + A[i]
        else:
            time[i][j] = time[i][j-1] + B[j]
        if time[i][j] > K:
            break
        if read < i+j:
            read = i+j

print(read)

Submission status: Time Limit Exceeded

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

books_total = 0
possible_combo_c = set()
possible_combo_c.add((0, 0, 0))
while True:
    possible_combo_n = set()
    for combo in possible_combo_c:
        if(combo[0] < n):
            combo_n_a = (combo[0]+1, combo[1], combo[2]+a[combo[0]])
            if combo_n_a[2] <= k:
                possible_combo_n.add(combo_n_a)
        if(combo[1] < m):
            combo_n_b = (combo[0], combo[1]+1, combo[2]+b[combo[1]])
            if combo_n_b[2] <= k:
                possible_combo_n.add(combo_n_b)
    if len(possible_combo_n) > 0:
        books_total += 1
        possible_combo_c = possible_combo_n
    else:
        break

ans = books_total
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()]
AA = [0]
BB = [0]
r = 0
for i in A:
    r += i
    AA.append(r)
r = 0
for i in B:
    r += i
    BB.append(r)
ans = 0
for i,v in enumerate(AA):
    for j,w in enumerate(BB):
        x = i+j
        y = v+w
        if y > K:
            break
        if x > ans:
            ans = x
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_max in range(max([k for k, v in enumerate(sums_A) if v <= K])+1):
    b_max = max([k for k, v in enumerate(sums_B) if v <= K-sums_A[a_max]])
    if ans < a_max+b_max:
        ans = a_max+b_max
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()))

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

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

ans = 0
for i in range(N+1):
    tmp_K = K-aa[i]
    if tmp_K == 0:
        ans = max(ans, i)
    elif tmp_K < 0:
        continue

    if tmp_K >= bb[-1]:
        ans = max(ans, i+M)
        continue
    mn = 0
    mx = M
    while True:
        if mn == mx:
            if tmp_K >= bb[mn]:
                ans = max(ans, i+mn)
            break

        mid = (mn+mx//2)
        if bb[mid] <= tmp_K:
            if mn == mid:
                mn = mid+1
            else:
                mn = mid
        else:
            if mx == mid:
                mx = mid-1
            else:
                mx = mid
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.sort()
b.sort()
count = 0

while (a != []) or (b != []):
    if not a:
        if (k - b[0]) < 0: break
        k -= b[0]
        del b[0]
    elif not b:
        if (k - a[0]) < 0: break
        k -= a[0]
        del a[0]
    elif a[0] <= b[0]:
        if (k - a[0]) < 0: break
        k -= a[0]
        del a[0]
    else:
        if (k - b[0]) < 0: break
        k -= b[0]
        del b[0]
    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()))

max_A_num = 0
for A_num in range(N+1):
    if sum(A[:A_num]) <= K:
        max_A_num = A_num
    else:
        break
max_book_count = 0
for A_num_2 in range(A_num,-1,-1):
    max_B_num = 0
    for B_num in range(max_B_num, M+1):
        if sum(A[:A_num_2]) + sum(B[:B_num]) <= K:
            max_B_num = B_num
            if max_book_count < A_num_2 + B_num:
                max_book_count = A_num_2 + B_num
        else:
            break
print(max_book_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()))
NM = N + M
result = 0
for i in range(2 ** NM):
    t = 0
    kk = 0
    aa = 0
    bb = 0
    for j in range(NM):
        if ((i >> j) & 1 and aa < len(A)) or bb >= len(B):
            kk += A[aa]
            aa += 1
            if (kk > K):
                break
        else:
            kk += B[bb]
            bb += 1
            if (kk > K):
                break
        t += 1
    if result < t:
        result = t
print(result)

Submission status: Time Limit Exceeded

Solution code:
def SS(A, K):
    sA = 0
    AA = []
    for a in A:
        sA += a
        if sA > K:
            break
        AA.append(sA)

    return AA


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

    AA = SS(A, K)
    BB = SS(B, K)
    AA = AA[::-1]

    M = max(len(AA), len(BB))

    left = 0
    right = len(BB) - 1

    while left < len(AA):
        if AA[left] + BB[right] <= K:
            m = len(AA) - left + right + 1
            if m > M:
                M = m
            left += 1
            right = len(BB) - 1
        else:
            right -= 1
            if right < 0:
                left += 1
                right = len(BB) - 1

    print(M)


main()

Submission status: Time Limit Exceeded

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


def DFS(point,left,right,cnt):
    global ans
    if point<=k:
        if cnt>ans:
            ans=cnt

    if left<n and point<=k:DFS(point+A[left],left+1,right,cnt+1)
    if right<m and point<=k:DFS(point+B[right],left,right+1,cnt+1)


n,m,k=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
ans=0
DFS(0,0,0,0)
print(ans)

Submission status: Time Limit Exceeded

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

A=[]
B=[]

for a in A_in:
    A.append(int(a))
    
for b in B_in:
    B.append(int(b))

time = 0
cnt = 0

Max = len(A) + len(B)

while cnt < Max:
    
    if len(A) > 0 and len(B) > 0:
        if A[0] > B[0]:
            time += B[0]
            B.pop(0)
        else:
            time += A[0]
            A.pop(0)
            
    elif len(A) == 0 and len(B) > 0:
        time += B[0]
        B.pop(0)
            
    elif len(B) == 0 and len(A) > 0:
        time += A[0]
        A.pop(0)

    if time <= K:
        cnt += 1
                
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N , M , K = list(map(int,input().split()))
A = list(map(int,input().split()))
B = list(map(int,input().split()))
 
T_A = [0]
T_B = [0]
for i in range(max(M,N)):
  if i == 0:
    T_A.append(A[0])
    T_B.append(B[0])
  else:
    try:
      T_A.append(T_A[i] + A[i])
    except:
      pass
    try:
      T_B.append(T_B[i] + B[i])
    except:
      pass
 
book = 0
for i in range(len(T_A)):
  for j in range(len(T_B)):
    if K < T_A[i] + T_B[j]:
      break
    else:
      if i + j  > book:
        book = i + j
      else:
        pass
    
print(book)

Submission status: Time Limit Exceeded

Solution code:


n, m, k = list(map(int, input().split()))
a = [0]+list(map(int, input().split()))
b = [0]+list(map(int, input().split()))
d = [[0]*(m+1) for _ in range(n+1)]
res = 0
for i in range(n+1):
    for j in range(m+1):
        t = 0
        if i > 0 and j > 0:
            t = d[0][j] + d[i][0]
        elif i > 0:
            t = d[i-1][j] + a[i]
        elif j > 0:
            t = d[i][j-1] + b[j]
        d[i][j] = t
        if t <= k and i + j > res:
            res = i + j
print(res)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(2000000)

def input_int():
    return map(int, input().split())

def one_int():
    return int(input())

def one_str():
    return input()

def many_int():
    return list(map(int, input().split()))

N, M, K = input_int()
A = many_int()
B = many_int()


def dfs(A_index, B_index, K, books):
    
    return_books = books
    return_booksA = books

    if N > A_index and K>=A[A_index]:
        return_books = dfs(A_index+1, B_index, K-A[A_index], books+1)
    if M > B_index and K>=B[B_index]:
        return_booksA = dfs(A_index, B_index+1, K-B[B_index], books+1)

    return max(return_books, return_booksA)

print(dfs(0, 0, K, 0))

Submission status: Time Limit Exceeded

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

    INF = 10 ** 9 + 10

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

    def rec(t, ai, bi):
        if t > K: return -INF

        ac, bc = INF, INF
        if ai < len(A): ac = A[ai]
        if bi < len(B): bc = B[bi]

        return max(
            0,
            rec(t + ac, ai + 1, bi) + 1,
            rec(t + bc, ai, bi + 1) + 1
        )

    print(rec(0, 0, 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()))
m = 0

for i in range(len(A) + 1):
    a = sum(A[:i])
    for j in range(len(B) + 1):
        k = a + sum(B[:j])
        if k <= K:
            if i + j > m:
                m = i + j
        else:
            break

print(m)

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
rest = k
sm = 0
end = -1
for j, b in enumerate(B):
    sm += b
    if sm < k:
        end = j
    else:
        sm -= b
        break
ans = end+1
for i, a in enumerate(A):
    sm += a
    if sm <= k:
        ans = max(end+1+i+1, ans)
    else:
        for j, b in enumerate(B[:end+1][::-1]):
            sm -= b
            if sm <= k:
                end -= j+1
                ans = max(end+1+i+1, ans)
                break
print(ans)

Submission status: Time Limit Exceeded

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


def DFS(point,left,right,cnt):
    global ans
    if point<=k:
        if cnt>ans:
            ans=cnt

    if left<n:DFS(point+A[left],left+1,right,cnt+1)
    if right<m:DFS(point+B[right],left,right+1,cnt+1)


n,m,k=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
ans=0
DFS(0,0,0,0)
print(ans)

Submission status: Time Limit Exceeded

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

time = [[0] * (M+1) for i in range(N+1)]

for i in range(N+1):
    for j in range(M+1):
        if i == 0 and j == 0:
            time[i][j] = 0
            read = 0
        if i != 0 and j == 0:
            time[i][j] = time[i-1][j] + A[i]
        else:
            time[i][j] = time[i][j-1] + B[j]
        if time[i][j] <= K and read < i+j:
            read = i+j

print(read)

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 na in range(n+1):
    for nb in range(m+1):
        time = sum(a[:na]) + sum(b[:nb])
        if time > k:
            break
        ans = max(ans, na+nb)
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

a.insert(0, 0)
b.insert(0, 0)

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

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

print(mmax)

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]
    # ai = 0
    for x in stdin.readline().rstrip().split():
        # ai += 1
        x = int(x)
        pileda += x
        a.append(pileda)
        if pileda > k:
            break
    
    piledb = 0
    b = [0]
    for x in stdin.readline().rstrip().split():
        x = int(x)
        piledb += x
        b.append(piledb)
        if piledb > k:
            break

    ans = 0
    for ai, time_a in enumerate(a):
        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:
# coding: utf-8
# Your code here!
import numpy as np

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


sum_A = np.cumsum(A)
sum_B = np.cumsum(B)
list_N = [0 for i in range(len(A)+1)]

for i in range(len(A)+1):
    if i == 0:
        num_B = len(sum_B[sum_B<=K])
        list_N[i] = i + num_B
    else:
        if K>=sum_A[i-1]:
            num_B = len(sum_B[sum_B<=(K-sum_A[i-1])])
            list_N[i] = i + num_B
        else:
            list_N[i] = 0
    
print(np.max(list_N))

Submission status: Time Limit Exceeded

Solution code:
import sys
x=input()
x0=x.split()
n=int(x0[0])
m=int(x0[1])
k=int(x0[2])
a=input()
b=input()
a0=[int(i) for i in a.split()]
b0=[int(i) for i in b.split()]
a=0
b=0
al=list([0])
bl=list([0])
for i in range(n):
    a=a+a0[i]
    if a>k:
        break
    al.append(a)
for i in range(m):
    b=b+b0[i]
    if b>k:
        break
    bl.append(b)
p=0
for i in range(len(al)):
    for l in range(len(bl)):
        if al[-i-1]+bl[-l-1]<=k:
            q=len(al)+len(bl)-i-l-2
            if p<q:
                p=q
print(p)

Submission status: Time Limit Exceeded

Solution code:
import sys
from functools import lru_cache

sys.setrecursionlimit(10 ** 7)


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


@lru_cache(None)
def dp(i, j, cur):
    if cur > K:
        return 0
    elif i >= N:
        if j < M and cur + B[j] <= K:
            return dp(i, j + 1, cur + B[j]) + 1
        else:
            return 0
    elif j >= M:
        if i < N and cur + A[i] <= K:
            return dp(i + 1, j, cur + A[i]) + 1
        else:
            return 0
    else:
        res = 0
        if cur + A[i] <= K:
            res = max(1 + dp(i + 1, j, cur + A[i]), res)
        if cur + B[j] <= K:
            res = max(1 + dp(i, j + 1, cur + B[j]), res)
        return res


print(dp(0, 0, 0))

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, -1):
    a = cumA[i]
    if a > K:
        continue
    for j in range(M, -1, -1):
        if i + j == ans:
            break
        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_max=N
B_max=M
sum_A=0
sum_B=0
ans=0
for i in range(N):
    sum_A+=A[i]
    if sum_A>K:
        A_max=i
for i in range(M):
    sum_B+=B[i]
    if sum_B>K:
        B_max=i
for j in range(A_max+1):
    for k in range(B_max+1):
        tmp=sum(A[:j])+sum(B[:k])
        #print(tmp,j,k)
        if tmp<=K:
            tmp2=j+k
            if tmp2>ans:
                ans=tmp2
print(ans)

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)[::-1]:
        a_value = integral_a[a_index]
        if K < a_value:
            continue
        for b_index in range(len(B) + 1)[::-1]:
            if a_value + integral_b[b_index] <= K:
                if top_count < a_index + b_index:
                    top_count = a_index + b_index
                break
    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 = 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]
    if SUM_A[i+1] > K:
        N=i
        break

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


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

Submission status: Time Limit Exceeded

Solution code:
import sys #追加
sys.setrecursionlimit(500*500)

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

time = 0
cnt = 0
ans = 0

def dfs(time, cnt):
    global ans
    # print(time)
    if time > K:
        # print(cnt)
        ans = max(ans, cnt-1)
        return
    if A:
        dfs(time+A.pop(0), cnt+1)
    if B:
        dfs(time+B.pop(0), cnt+1)
    if len(A)==0==len(B):
        return cnt

if sum(A)+sum(B)<=K:
    print(N+M)
else:
    dfs(0, 0)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()

def main():
    n, m, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    ans = 0
    a_time = [0]
    b_time = [0]
    for i in range(1, len(a) + 1):
        a_time.append(a_time[i - 1] + a[i - 1])
    for i in range(1, len(b) + 1):
        b_time.append(b_time[i - 1] + b[i - 1])

    for i in range(len(a_time)):
        if a_time[i] > k:
            break
        for j in range(len(b_time)):
            if a_time[i] + b_time[j] > k:
                break
            else:
                ans = max(ans, i + j)
    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()))

counter = 0
ans = 0

for i in range(n+1):
    if sum(A[:i])>k:
        break
    for j in range(m+1):
        if sum(A[:i])+sum(B[:j])<=k:
            ans = max(ans, i+j)
        else:
            break
            
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/python3
# import bisect
# from collections import Counter, deque, OrderedDict, defaultdict
# from copy import copy, deepcopy # pythonのみ．copyは1次元，deepcopyは多次元．
# from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
# from functools import reduce
# from heapq import heapify, heappop, heappush
# from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
# import math
# import numpy as np  # Pythonのみ！
# from operator import xor
# import re
# from scipy.sparse.csgraph import connected_components  # Pythonのみ！
# ↑cf.  https://note.nkmk.me/python-scipy-connected-components/
# from scipy.sparse import csr_matrix
# import statistics # Pythonのみ
# import string
import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

def resolve():
    
    n,m,k=map(int,input().split())
    A=[0]+list(map(int,input().split()))
    B=[0]+list(map(int,input().split()))

    from itertools import accumulate
    
    
    AA = [0] + A
    BB=[0]+B
    # 累積和を格納したリスト．A[l:r]の総和はB[r] - B[l]
    AA = list(accumulate(A))
    BB=list(accumulate(B))

    ans=0
    for i in range(n+1):
        for j in range(m+1):
            if AA[i]+BB[j]<=k:
                ans=max(ans,i+j)
            else: break
    print(ans)

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

print(sorted(i + j for i in range(N + 1) for j in range(M + 1) if sum(A[idx] for idx in range(i)) + sum(B[idx] for idx in range(j)) <= K)[-1])

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

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


large = 10**10
time = 0
ans = 0
while len(A) != 0 or len(B) != 0:
    a = large
    b = large

    if len(A)!=0:
        a = A[0]
    if len(B)!=0:
        b = B[0]

    if a < b:
        if time+a > K:
            break
        time += a
        A = A[1:]
        ans += 1
    elif a > b:
        if time+b > K:
            break
        time += b
        B = B[1:]
        ans += 1


print(ans)

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]
            if ans < i+j:
                ans = i+j
    print(ans)


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

Submission status: Time Limit Exceeded

Solution code:
# 解法 全探索をする，上から読んでる冊数をi,jとしてKを超えない範囲で最大のものを週つ力すれば良い
# 全探索だったらだめだったN^2になるから
# まずAを最大限選ぶ，次にその時選べる最大のBを選ぶ
N,M,K = map(int,input().split())

A = list(map(int,input().split()))
B = list(map(int,input().split()))
# 累積和
sumb = 0
Bs = [0]*M
for i,b in enumerate(B):
    sumb += B[i]
    Bs[i] = sumb
sum_time=0
max_count = 0
count = 0
# i,jは実際に読んでいる数とする
import bisect
for i in range(N+1):
    count = 0
    if i ==0:
        sum_time=0
    else:
        sum_time=sum(A[:i])
        count += i
    if sum_time>K:
        continue
    ind = bisect.bisect_right(Bs,K-sum_time)
    count += ind
    max_count = max(max_count,count)

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

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

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

ans = 0
for i in range(N+1):
    tmp_K = K-aa[i]
    if tmp_K == 0:
        ans = max(ans, i)
    elif tmp_K < 0:
        continue
    mn = 0
    mx = M
    while True:
        if mn == mx:
            if tmp_K >= bb[mn]:
                ans = max(ans, i+mn)
            break

        mid = (mn+mx//2)
        if bb[mid] <= tmp_K:
            if mn == mid:
                mn = mid+1
            else:
                mn = mid
        else:
            if mx == mid:
                mx = mid-1
            else:
                mx = mid
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()))

maxrnum = 0
sum1 = 0
for n in range(N+1):
    if n>0:
        sum1 += A[n-1]
    if sum1>K:
        break

    sum2 = 0
    for m in range(M+1):
        if m>0:
            sum2 += B[m-1]
        
        rtime = sum1 + sum2
        if rtime<=K:
            rnum = n+m
            if rnum>maxrnum:
                maxrnum = rnum
        else:
            break

print(maxrnum)

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 sum(A) <= K:
    a_max = N
    ans = N

else:
    a_total = 0
    for i in range(N):
        a_total += A[i]
        if a_total > K:
            break
    a_max = i
    ans = a_max


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

print(ans)

Submission status: Time Limit Exceeded

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

time = 0
cnt = 0
while True:
  if As[-1] < Bs[0]:
    if time + As[0] > K:
      break
    time+=As[0]
    del As[0]
    if As == []:
      As.append(10**10)
  else:
    if time + Bs[0] > K:
      break
    time+=Bs[0]
    del Bs[0]
    if Bs == []:
      Bs.append(10**10)
  cnt+=1
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()))

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 or asum+bsum>k:
        break
      bsum+=b[j]
    if i==n or asum>k:
      break
    asum+=a[i]
  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
    A = None
    for i in range(len(As)+1):
        if i == 0:
            A = 0
        else:
            A += As[i-1]
        k = A
        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()