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

aa = [0]
bb = [0]

for i in range(n):
    aa.append(a[i] + aa[i])

for i in range(m):
    bb.append(b[i] + bb[i])

ans = 0
flag = 0
for i in range(n+1):
    if k - aa[i] < 0:
        flag = 1
        break
    for j in range(m+1):
        if k - aa[i] - bb[j] < 0:
            flag =1
            break
    if ans < i + j - 1:
        ans = i + j - 1


if flag == 0:
    print(i+j)
    exit()
print(ans)

Submission status: Time Limit Exceeded

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

nss = [0]
for i, v in enumerate(ns): nss.append(nss[i] + ns[i])
mss = [0]
for i, v in enumerate(ms): mss.append(mss[i] + ms[i])

mx = 0
for i in range(n + 1):
    if nss[i] > k:
        break

    j = -1
    for v in mss:
        if nss[i] + v > k:
            break
        j += 1
    mx = max(mx, i + j)

print(mx)

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 i in range(N + 1):
        for j in range(M + 1):
            if sA[i] + sB[j] <= K:
                if last_can_read < i + j:
                    last_can_read = i + j
            else:
                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:
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

sumA, sumB = [0], [0]
sa, sb = 0, 0
for a in A:
    sa += a
    sumA.append(sa)
for b in B:
    sb += b
    sumB.append(sb)

n_max = 0
for na in range(N+1):
    for nb in range(M+1):
        k = sumA[na] + sumB[nb]
        if k > K:
            break
        n = na + nb
        n_max = max(n_max, n)
print(n_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()))
A.insert(0, 0)
B.insert(0, 0)
a_sum = 0

max_cnt = 0
for i in range(n + 1):
    a_sum += A[i]
    if a_sum > k:
        break

    if max_cnt < i:
        max_cnt = i

    b_sum = 0
    for j in range(m + 1):
        b_sum += B[j]
        if a_sum + b_sum > k:
            break

        if max_cnt < i + j:
            max_cnt = i + j

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

a = [0]
for i in range(N):
    a.append(a[i]+A[i])
b = [0]
for i in range(M):
    b.append(b[i]+B[i])

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: break
        ans = max(ans, i+j)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    '''
    code here
    '''

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

    res = 0
    for i, av in enumerate(memo_A):
        if av > K:
            break
        else:
            j = M
            while av + memo_B[j] > K:
                j -= 1
            
            res = max(res,j+i)

    print(res)

if __name__ == "__main__":
    resolve()

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

a = [0]
b = [0]
for i in range(len(A)):
    a.append(a[i] + A[i])

for i in range(len(B)):
    b.append(b[i] + B[i])


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

ans = set()
ans.add(0)

for i_n in range(0, n+1):
    if a[i_n] > k:
        break

    start_idx = max(list(ans)[-1] - i_n, 0)
    # print('start_idx', start_idx)
    for i_m in range(start_idx, m+1):
        if a[i_n] + b[i_m] > k:
            break

        time_sum = a[i_n] + b[i_m]
        # 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:
n, m, k = list(map(int, input().split()))
ns = list(map(int, input().split()))
ms = list(map(int, input().split()))

nss = [sum(ns[:i]) for i in range(n+1)]
mss = [sum(ms[:i]) for i in range(m+1)]

mx = 0
for i in range(n + 1):
    if nss[i] > k:
        break

    j = -1
    for v in mss:
        if nss[i] + v > k:
            break
        j += 1
    mx = max(mx, i + j)

print(mx)

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

L = [float("inf") for _ in range(N+M+1)]
L[0] = 0

a = 0
b = 0
ans = 0
for k in range(len(A)):
    a += A[k]
    A[k] = a
for k in range(len(B)):
    b += B[k]
    B[k] = b

AA = [0] + A

BB = [0] + B

for k in range(N+1):
    for j in range(M+1):
        if  K >= AA[k] + BB[j]:
            ans = max(k+j, ans)
            continue
        else:
            break
print(ans)

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
    for b in B:
        if 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

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

            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:

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):
  j = m
  if AS[i] > k:
    break
 
  while AS[i] + BS[j] > k:
    j -= 1
  ans = max(ans, i+j)
        
print(ans)

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:
        N = index - 1
        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:
        M = index - 1
        break

search = deque()
check = {}


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

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

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

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


search_count((0, 0))

print(max(check.values()))

Submission status: Time Limit Exceeded

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

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

aa = [0]
bb = [0]

for i in range(n):
    aa.append(a[i] + aa[i])

for i in range(m):
    bb.append(b[i] + bb[i])

ans = 0
flag = 0
for i in range(n+1):
    if k - aa[i] < 0:
        flag = 1
        if ans < i-1:
            ans = i-1   
        break
    for j in range(m+1):
        if k - aa[i] - bb[j] < 0:
            flag = 1
            if ans < i + j - 1:
                ans = i + j - 1
            break

if flag == 0:
    print(n+m)
    exit()
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 = [0] * (n + 1)
for i in range(1, n + 1):
    A[i] = A[i - 1] + a[i - 1]

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

ans = 0
for i in range(n + 1):
    for j in range(m + 1):
        t = A[i] + B[j]
        if t > 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()))

A_sum = [0]
for i in range(N):
    A_sum.append(A[i]+A_sum[i])

B_sum = [0]
for i in range(M):
    B_sum.append(B[i]+B_sum[i])

max_count = 0
for i in range(N+1):
    j = M
    if A_sum[i] > K:
        break

    while B_sum[j] + A_sum[i] > K:
        j -= 1
    max_count = max(max_count, i + j)
print(max_count)

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(N+1):
    a = AS[i]
    if a > K:
        continue
    if max - i > 0:
        j = max - i
    else:
        j = 0
    while j < M + 1:
        b = BS[j]
        if a + b <= K and max < i + j:
            max = i + j
        j += 1

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

aa = [0]*(n+1)
bb = [0]*(m+1)

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

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

ans = 0
flag = 0
for i in range(n+1):
    if k - aa[i] < 0:
        flag = 1
        if ans < i-1:
            ans = i-1   
        break
    for j in range(m+1):
        if k - aa[i] - bb[j] < 0:
            flag = 1
            if ans < i + j - 1:
                ans = i + j - 1
            break

if flag == 0:
    print(n+m)
    exit()
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, m, k = map(int, input().split())
a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))
A, B = [0], [0]
a_sum, b_sum = 0, 0

for i in range(len(a_list)):
    a_sum += a_list[i]
    A.append(a_sum)
for i in range(len(b_list)):
    b_sum += b_list[i]
    B.append(b_sum)

max_cnt = 0
for i in range(n + 1):
    if A[i] > k:
        break

    if max_cnt < i:
        max_cnt = i

    for j in range(m + 1):
        if A[i] + B[j] > k:
            break

        if max_cnt < i + j:
            max_cnt = i + j

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

A_sum = [0]
a_sum = 0
for a in A:
    a_sum += a
    A_sum.append(a_sum)

B_sum = [0]
b_sum = 0
for b in B:
    b_sum += b
    B_sum.append(b_sum)

max_count = 0
for i in range(N+1):
    j = M
    if A_sum[i] > K:
        break

    while B_sum[j] + A_sum[i] > K:
        j -= 1
    max_count = max(max_count, i + j)
print(max_count)

Submission status: Time Limit Exceeded

Solution code:
import random
n, m, k = map(int, input().split())
# n, m, k = random.randint(1, 10**5), random.randint(1, 10**5), random.randint(10**8, 10**9)
a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))
# a_list = [random.randint(1, 10**8) for i in range(n)]
# b_list = [random.randint(1, 10**7) for i in range(m)]
# print(n,m,k)
# print(a_list[0], b_list[0])
a_sum = [0]
b_sum = [0]
for i in range(1, max(n+1, m+1)):
    if i < n+1:
        a_sum.append(a_sum[i-1] + a_list[i-1])
    if i < m+1:
        b_sum.append(b_sum[i-1] + b_list[i-1])

ans = 0
for i in range(n+1):
    if a_sum[i] > k:
        break
    else:
        j = 0
        while j <= m and b_sum[j] <= k - a_sum[i]:
            j += 1
        j -= 1
    ans = max(ans, i+j)
        
print(ans)
# print(n+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()))
countA=2
countB=2
shoyou=K
tot=0
sumA=[0,A[0]]
sumB=[0,B[0]]
mmm=True

for i in range(len(A)-1):
    sumA.append(A[countA-1]+sumA[countA-1])
    countA+=1
for i in range(len(B)-1):
    sumB.append(B[countB-1]+sumB[countB-1])
    countB+=1

tot=0
for i in range(len(sumA)+1):
    for j in range(len(sumB)):
        if sumA[len(sumA)-i-1]+sumB[len(sumB)-j-1]<=K and tot<len(sumA)-i-1+len(sumB)-j-1:
            tot=len(sumA)-i-1+len(sumB)-j-1
            break
print(tot)

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    '''
    code here
    '''

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

    res = 0
    for i, av in enumerate(memo_A):
        if av > K:
            break
        else:
            j = M
            for bv in memo_B[::-1]:
                if bv + av  <= K:
                    res = max(res, i+j)

                j -= 1
            
    print(res)

if __name__ == "__main__":
    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()))

L = [float("inf") for _ in range(N+M+1)]
L[0] = 0

a = 0
b = 0

for k in range(len(A)):
    a += A[k]
    A[k] = a
for k in range(len(B)):
    b += B[k]
    B[k] = b

AA = [0] + A

BB = [0] + B

for k in range(N+1):
    for j in range(M+1):
        if  L[k+j] > AA[k] + BB[j]:
            L[k+j] = AA[k] + BB[j]
for k in range(1, len(L)+1):
    if L[-k] <= K:
        print(len(L)-k)
        break

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
from collections import deque

import numpy as np

n, m, k = map(int, input().split())
a = [0] + list(map(int, input().split()))
b = [0] + list(map(int, input().split()))


def bfs():
    d = ((1, 0), (0, 1))
    maze = [[0] * (m + 1) for _ in range(n + 1)]
    q = deque([])
    q.append((0, 0))
    maze[0][0] = k
    while q:
        x, y = q.popleft()
        if maze[x][y] < 0:
            for i in range(x, n + 1):
                for j in range(y, m + 1):
                    maze[i][j] = -1
        for i in range(2):
            nx = x + d[i][0]
            ny = y + d[i][1]
            if 0 <= nx <= n and 0 <= ny <= m and maze[nx][ny] == 0:
                q.append((nx, ny))
                maze[nx][ny] = maze[x][y] - a[nx] * d[i][0] - b[ny] * d[i][1]
    o = np.array(maze)
    o = np.argwhere(o >= 0)
    return max(sum(i) for i in o)


print(bfs())

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

def find_nearest(array, value):
    array = np.asarray(array)
    idx = (np.abs(array - value)).argmin()
    return array[idx]

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

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

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

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

#print(A_acc, B_acc)

max_num = 0
left_time = K
for a_num in range(A_len + 1):
  b_num = 0
  left_time = K - A_acc[a_num]
  #print("Aから{0}冊読んだ場合はそもそも残り{1}分".format(a_num, left_time))
  if(left_time < 0):
    break
  
  #while(True):
  #  print("Bから{0}冊読んでる場合,残り時間{1}".format(b_num, left_time - B_acc[b_num]))
    
  #  if(b_num + 1 > B_len):
  #    break
    
  #  if(left_time - B_acc[b_num + 1] < 0):
  #    break
    
  #  b_num += 1
  nearest_b = find_nearest(B_acc, left_time)
  b_num = B_acc.index(nearest_b)
  if(left_time < nearest_b and b_num > 0):
    b_num -= 1
  #print("最も近い分数は{0}. 冊数は{1}".format(nearest_b, b_num))
    
  #print("結果,A棚から{0}冊、B棚から{1}冊で合計{2}冊で経過時間{3}"
  #      .format(a_num, b_num, a_num + b_num, A_acc[a_num] + B_acc[b_num]))
  max_num = max(max_num, (a_num + b_num))
  #print("現在max{0}".format(max_num))
  
print(max_num)

Submission status: Time Limit Exceeded

Solution code:
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A = [0] + A
B = [0] + B
BM = M
X = 0
SA = 0
for i in range(N + 1):
    SA += A[i]
    SB = 0
    for j in range(M + 1):
        SB += B[j]
        if SA + SB <= K:
            X = max(X, i + j)
            M = j
        else:
            break
print(X)

Submission status: Time Limit Exceeded

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

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


a = [0]
b = [0]

for i in range(N):
    a.append(a[-1] + A[i])


for i in range(M):
    b.append(b[-1] + B[i])

ret = 0
for i in range(N+1):
    for j in range(M+1):
        if a[i] + b[j] <= K:
            ret = max(i+j, ret)
        else:
            break

print(ret)

Submission status: Time Limit Exceeded

Solution code:
n, m, k = map(int, input().split())
a = list(map(int,input().split()))
b = list(map(int, input().split()))
 
ans_list = []
ans_list.append(0)

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

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]
    b_sum = [0]
    for i in range(n):
        a_sum.append(a_sum[i] + a[i])
    for i in range(m):
        b_sum.append(b_sum[i] + b[i])

    ans = 0
    for i in range(n + 1):
        if a_sum[i] > k:
            break
        for j in range(m + 1):
            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:
N,M,K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
countA=2
countB=2
shoyou=K
tot=0
sumA=[0,A[0]]
sumB=[0,B[0]]
mmm=True

for i in range(len(A)-1):
    sumA.append(A[countA-1]+sumA[countA-1])
    countA+=1
for i in range(len(B)-1):
    sumB.append(B[countB-1]+sumB[countB-1])
    countB+=1

tot=0
for i in range(len(sumA)):
    for j in range(len(sumB)):
        if sumA[i]+sumB[j]<=K and i+j>tot:
            tot=i+j
        elif sumA[i]+sumB[j]>K:
            break
print(tot)

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
j = M
for i in range(len(consumptions_A)):
    if (consumptions_A[i] > K): break

    remaining_time = K - consumptions_A[i]
    while consumptions_B[j] > remaining_time:
        j -= 1
    
    num_books = i + j
    answer = max(answer, num_books)

print(answer)

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    '''
    code here
    '''

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

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

if __name__ == "__main__":
    resolve()

Submission status: Time Limit Exceeded

Solution code:
N, M, K = map(int, input().split())
As = list(map(int, input().split()))
Bs = list(map(int, input().split()))
ans = 0
a, b = [0], [0]

for i in range(N):
  a.append(a[i] + As[i])
for i in range(M):
  b.append(b[i] + Bs[i])    

for i in range(N + 1):
  if (a[i] > K):
    break
  k_ = K - a[i]
  for j in range(M + 1):
    if (b[j] > k_):
      ans = max(ans, i + (j - 1))
      break
    elif (j == M):
      ans = max(ans, i + j)
  
print(ans)

Submission status: Time Limit Exceeded

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

total_A_minutes = sum(A_minutes)
total_B_minutes = sum(B_minutes)

# すべて読める場合
if total_A_minutes + total_B_minutes <= K:
    print(N + M)
    exit()

# 冊数
ans = 0
# Kを超える最小のi
limitover_A_index = 0
# Aの合計が超えた場合
if total_A_minutes > K:
    tmp_total_A_minutes = 0
    for i in range(N):
        tmp_total_A_minutes = tmp_total_A_minutes + A_minutes[i]
        if tmp_total_A_minutes > K:
            limitover_A_index = i
            # Kを超過したindex が Kを超過しない冊数となる
            ans = i
            break
# Aの合計が超えない場合
else:
    limitover_A_index = N
    ans = N

while limitover_A_index >= 0:
    tmp_total_minute = sum(A_minutes[:limitover_A_index])
    tmp_ans = limitover_A_index

    # Bを1冊ずつ確認
    for B_minute in B_minutes:
        # Bの冊数を足したことでK分を超えるなら
        if tmp_total_minute + B_minute > K:
            break

        tmp_total_minute = tmp_total_minute + B_minute
        tmp_ans = tmp_ans + 1

    # 最大冊数の記録を超えたら更新する
    if ans < tmp_ans:
        ans = tmp_ans

    limitover_A_index = limitover_A_index - 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
# import io
# import sys

# _INPUT = """\
# 1 4 120
# 100 1 1 1 1 1 1 1 1 1 1
# 80 10 10 10 10
# """
# sys.stdin = io.StringIO(_INPUT)

####################################################################
import sys
def p(*_a):
  return
  _s=" ".join(map(str,_a))
  #print(_s)
  sys.stderr.write(_s+"\n")
####################################################################

N,M,K = map(int, input().split())
A = [0] + [int(i) for i in input().split()]
B = [0] + [int(i) for i in input().split()]


# 累積和
from itertools import accumulate
C = list(accumulate(A))
D = list(accumulate(B))

p(*C)
p(*D)

ans = 0

for i, c in enumerate(C):
  for j, d in enumerate(D):
    p(i,j,c,d)
    if c + d > K: break
    ans = max(ans, i+j)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import random
n, m, k = map(int, input().split())
# n, m, k = random.randint(1, 10**5), random.randint(1, 10**5), random.randint(10**7, 10**8)
a_list = list(map(int, input().split()))
b_list = list(map(int, input().split()))
# a_list = [random.randint(1, 10**2) for i in range(n)]
# b_list = [random.randint(1, 10**2) for i in range(m)]
# print(n,m,k)
# print(a_list[0], b_list[0])
a_sum = [0]
b_sum = [0]
for i in range(1, max(n+1, m+1)):
    if i < n+1:
        a_sum.append(a_sum[i-1] + a_list[i-1])
    if i < m+1:
        b_sum.append(b_sum[i-1] + b_list[i-1])

ans = 0
for i in range(n+1):
    if a_sum[i] > k:
        break
    else:
        j = m
        while b_sum[j] > k - a_sum[i]:
            j -= 1
    ans = max(ans, i+j)
        
print(ans)
# print(n+m)