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:
import sys
N,M,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
left_time = K
time_left_tmp = K
num = 0
while num <= N-1 :
    if time_left_tmp >= A[num]:
        time_left_tmp -= A[num]
        num += 1
    else:
        break
if num == 0:
    print(0)
    sys.exit()
max_num = 0
for i in range(num,-1,-1):
    A_time = sum(A[:i])
    B_time = K - A_time
    B_num = 0
    while B_num <= M -1:
        if B_time >= B[B_num]:
            B_time -= B[B_num]
            B_num += 1
        else:
            break
    if max_num < i + B_num:
        max_num = i + B_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()))

Asum=[0]
Bsum=[0]

for i in range(N):
    Asum.append(Asum[-1]+A[i])
    if Asum[i]>K:
        break
        
for i in range(M):
    Bsum.append(Bsum[-1]+B[i])
    if Bsum[i]>K:
        break
    
m=0
for i in range(len(Asum)):
    for j in range(len(Bsum)):
        if Asum[i]+Bsum[j]<=K and m<i+j:
            m=i+j
print(m)

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

Asum=[0]
Bsum=[0]

for i in range(N):
    Asum.append(Asum[-1]+A[i])
for i in range(M):
    Bsum.append(Bsum[-1]+B[i])
    
m=0
for i in range(len(Asum)):
    for j in range(len(Bsum)):
        if Asum[i]>K:
            print(m)
            sys.exit()
        elif Asum[i]+Bsum[j]<=K and m<i+j:
            m=i+j
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()))

l = 0
r = n + m + 1
while r - l > 1:
  p = (l + r) // 2
  best = k + 1
  for i in range(p + 1):
    if min(i, n) + min(p - i, m) < p:
      continue
    best = min(best, sum(A[:i]) + sum(B[:p - i]))
  if best <= k:
    l = p
  else:
    r = p
print(l)

Submission status: Time Limit Exceeded

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

c=[0]
for i in range(n):
  if c[i]+a[i]<=k:
    c.append(c[i]+a[i])
  else:
    break

d=[0]
for i in range(m):
  if d[i]+b[i]<=k:
    d.append(d[i]+b[i])
  else:
    break

e=0
f=len(d)-1
for i in range(len(c)):
  for j in range(f,-1,-1):
    if c[i]+d[j]<=k and i+j>e:
      e=i+j
      f=j
      break
print(e)

Submission status: Time Limit Exceeded

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

c=[0]
for i in range(n):
  if c[i]+a[i]<=k:
    c.append(c[i]+a[i])
  else:
    break

d=[0]
for i in range(m):
  if d[i]+b[i]<=k:
    d.append(d[i]+b[i])
  else:
    break

e=0
for i in range(len(c)):
  for j in range(len(d)-1,-1,-1):
    if c[i]+d[j]<=k and i+j>e:
      e=i+j
      break
print(e)

Submission status: Time Limit Exceeded

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

c=[0]
for i in range(n):
  if c[i]+a[i]<=k:
    c.append(c[i]+a[i])
  else:
    break

d=[0]
for i in range(m):
  if d[i]+b[i]<=k:
    d.append(d[i]+b[i])
  else:
    break

e=0
for i in range(len(c)):
  for j in range(len(d)):
    if c[i]+d[j]<=k and i+j>e:
      e=i+j
print(e)

Submission status: Time Limit Exceeded

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

c=[0]
for i in range(n):
  c.append(c[i]+a[i])

d=[0]
for i in range(m):
  d.append(d[i]+b[i])

e=0
for i in range(n+1):
  for j in range(m+1):
    if c[i]+d[j]<=k and i+j>e:
      e=i+j
print(e)

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.append(10**9)
b.append(10**9)
i = 0

while (k >= min(a))|(k >= min(b)):
    if min(a) <= min(b):
        k -= min(a)
        a.remove(min(a))
        i += 1

    else:
        k -= min(b)
        b.remove(min(b))
        i += 1

print(i)

Submission status: Time Limit Exceeded

Solution code:
n,m,k = map(int,input().split())
alist = list(map(int,input().split()))
blist = list(map(int,input().split()))
ans = 0
asum = k
for i in range(n+1):
    if asum < 0:
        break
    else:
        bsum = asum
        for j in range(m):
            if bsum < 0:
                if i+j-1 > ans:
                    ans = i+j-1
                break
            bsum -= blist[j]
        if bsum >= 0 and i + j+1 > ans:
            ans = i+j+1
    if i < n:
        asum -= alist[i]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,m,k = map(int,input().split())
shelfA = list(map(int,input().split()))
shelfB = list(map(int,input().split()))
prodA = [0]*(n+1)
prodB = [0]*(m+1)
for i in range(1,n+1):
    prodA[i] = prodA[i-1] + shelfA[i-1]
for i in range(1,m+1):
    prodB[i] = prodB[i-1] + shelfB[i-1]
if(n>=m):
    ans = [-1]*(n+1)
    for i in range(n+1):
        for j in range(m+1):
            if(prodA[i]+prodB[j] <= k):
                if(ans[i] < i+j):
                    ans[i] = i+j
            else:
                break
            
else:
    ans = [-1]*(m+1)
    for i in range(m+1):
        for j in range(n+1):
            if(prodB[i]+prodA[j] <= k):
                if(ans[i] < i+j):
                    ans[i] = i+j
            else:
                break

print(max(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 = []
Bsum = []
a = 0
b = 0
for i in range(N):
    a += A[i]
    Asum.append(a)
for i in range(M):
    b += B[i]
    Bsum.append(b)
Asum.append(0)
Bsum.append(0)
res = 0
for i in range(-1,N):
    if Asum[i] > K:
        break
    for j in range(-1,M):
        if Asum[i] + Bsum[j] > K:
           break
        else:
           res = max(res,(i+j+2))
print(res)

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 = []
Bsum = []
a = 0
b = 0
for i in range(N):
    a += A[i]
    Asum.append(a)
for i in range(M):
    b += B[i]
    Bsum.append(b)
Asum.append(0)
Bsum.append(0)
res = 0
for i in range(-1,N):
    for j in range(-1,M):
        if Asum[i] + Bsum[j] <= K:
           res = max(res,(i+j+2))
        else:
           break
print(res)

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

for i in range(1, N+1):
    A[i] += A[i-1]
for i in range(1, M+1):
    B[i] += B[i-1]

ans = 0

for i in range(N+1):
    if A[i] > K: break
    else:
        for j in range(M+1):
            if A[i] + B[j] > K: break
            elif ans < i + j:
                ans = i + j

print(ans)

Submission status: Time Limit Exceeded

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

cnt = -1

aa = a.copy()
bb = b.copy()

aa = [sum(a[:s+1]) for s in range(n)]
bb = [sum(b[:s+1]) for s in range(m)]
      
ans = 0

for i in range(n):
    bb_sharp = k - aa[i]
    if bb_sharp < 0:
        break
    else:
        bb_below =[bb[s] for s in range(m) if bb[s] <= bb_sharp]
        temp = i+1 + len(bb_below)
        if temp > ans:
           ans = temp
        
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()))

max_cnt = 0
for a in range(len(A)+1):
    if sum(A[:a]) > K:
        break
    for b in range(len(B)+1):
        time = sum(A[:a]) + sum(B[:b])
        if time <= K:
            cnt = a + b
            if cnt > max_cnt:
                max_cnt = cnt
        else:
            break
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()))

max_cnt = 0
for a in range(len(A)+1):
    for b in range(len(B)+1):
        time = sum(A[:a]) + sum(B[:b])
        if time <= K:
            cnt = a + b
            if cnt > max_cnt:
                max_cnt = cnt
print(max_cnt)

Submission status: Time Limit Exceeded

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


AX=[0]*(N+1)
na=N+1
for i in range(1,N+1):
    AX[i]=AX[i-1]+A[i-1]
    if K<AX[i]:
        na=i
        break

BX=[0]*(M+1)
nb=M+1
for i in range(1,M+1):
    BX[i]=BX[i-1]+B[i-1]
    if K<BX[i]:
        nb=i
        break

ans=0
for i in range(na):
 for j in range(nb):
   if K < AX[i]+BX[j]:break
   ans=max(ans,i+j)
            
print(ans)

Submission status: Time Limit Exceeded

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


AX=[0]*(N+1)
na=N+1
for i in range(1,N+1):
    AX[i]=AX[i-1]+A[i-1]
    if K<AX[i]:na=min(na,i)

BX=[0]*(M+1)
nb=M+1
for i in range(1,M+1):
    BX[i]=BX[i-1]+B[i-1]
    if K<BX[i]:nb=min(nb,i)

ans=0
for i in range(na):
 for j in range(nb):
   if K < AX[i]+BX[j]:break
   ans=max(ans,i+j)
            
print(ans)

Submission status: Time Limit Exceeded

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

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

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

import bisect
ni=bisect.bisect_right(AX,K)
mi=bisect.bisect_right(BX,K)
    
ans=0
for i in range(ni):
 for j in range(mi):
   if K < AX[i]+BX[j]:break
   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()))

for i in range(1, N+1):
    A[i] += A[i-1]
for i in range(1, M+1):
    B[i] += B[i-1]

ans = 0

for a_index in range(N+1):
    left = K - A[a_index]
    if left < 0: continue

    b_index = M
    while left - B[b_index] < 0:
        b_index -= 1

    ans = max(ans, a_index + b_index)

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

for i in range(1, N+1):
    A[i] += A[i-1]
for i in range(1, M+1):
    B[i] += B[i-1]

ans = 0

for a_index in range(N+1):
    left = K - A[a_index]
    if left < 0: break

    b_index = M
    while left - B[b_index] < 0:
        b_index -= 1

    ans = max(ans, a_index + b_index)

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

for i in range(1, N+1):
    A[i] += A[i-1]
for i in range(1, M+1):
    B[i] += B[i-1]

ans = 0

for a_index in range(N+1):
    left = K - A[a_index]
    if left < 0: break
    score = a_index

    for b_index in reversed(range(M+1)):
        if B[b_index] <= left:
            score += b_index
            break

    ans = max(ans, score)

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

Submission status: Time Limit Exceeded

Solution code:
import math
import sys
import os
from operator import mul
# import numpy as np

sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(_S())
def LS(): return list(_S().split())
def LI(): return list(map(int,LS()))

if os.getenv("LOCAL"):
    inputFile = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt'
    sys.stdin = open(inputFile, "r")
INF = float("inf")


N,M,K = LI()
A = LI()
B = LI()
ans = 0

# A = np.array(A)
# A_cum = np.zeros(N+1,dtype='int')
# A_cum[1:] = A.cumsum()
A_cum = [0]*(N+1)
for i in range(N):
    A_cum[i+1] = A_cum[i]+A[i]

# B = np.array(B)
# B_cum = np.zeros(M+1,dtype='int')
# B_cum[1:] = B.cumsum()

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

# print(A_cum)
# print(B_cum)

for i in range(N+1):
    for j in range(M+1):
        tmp = A_cum[i]+B_cum[j]
        if tmp <= K:
            ans = max(ans,i+j)
        else:
            break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math
import sys
import os
from operator import mul
import numpy as np

sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(_S())
def LS(): return list(_S().split())
def LI(): return list(map(int,LS()))

if os.getenv("LOCAL"):
    inputFile = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt'
    sys.stdin = open(inputFile, "r")
INF = float("inf")


N,M,K = LI()
A = LI()
B = LI()
ans = 0

A = np.array(A)
A_cum = np.zeros(N+1,dtype='int')
A_cum[1:] = A.cumsum()

B = np.array(B)
B_cum = np.zeros(M+1,dtype='int')
B_cum[1:] = B.cumsum()

# print(A_cum)
# print(B_cum)

for i in range(N+1):
    for j in range(M+1):
        tmp = A_cum[i]+B_cum[j]
        if tmp <= K:
            ans = max(ans,i+j)
        else:
            break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

a_tmp = [0] + list(itertools.accumulate(a))
b_tmp = [0] + list(itertools.accumulate(b))
ans = 0

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

Submission status: Time Limit Exceeded

Solution code:
import itertools
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

a_tmp = [0] + list(itertools.accumulate(a))
b_tmp = [0] + list(itertools.accumulate(b))
ans = 0

for i in range(n+1):
    for j in range(m+1):
        if a_tmp[i]+b_tmp[j] <= k:
            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()))
t=0
c=0
if K<A[0] and K<B[0]:
  print(c)
  exit()
while 1:
  if t>=K:
    print(c)
    break
  else:
    if len(A)==0 and len(B)==0:
      print(c)
      break
    elif len(A)==0:
      t=t+B[0]
      B.remove(B[0])
      c=c+1
    elif len(B)==0:
      t=t+A[0]
      A.remove(A[0])
      c=c+1
    else:
      if A[0]>=B[0]:
        t=t+A[0]
        A.remove(A[0])
        c=c+1
      else:
        t=t+B[0]
        B.remove(B[0])
        c=c+1

Submission status: Time Limit Exceeded

Solution code:
from itertools import accumulate
N, M, K = map(int,input().split())
A_list = [0] + list(map(int,input().split()))
B_list = [0] + list(map(int,input().split()))
A_sums_list = list(accumulate(A_list))
B_sums_list = list(accumulate(B_list))

for n in range(N + 1):
    if A_sums_list[n] > K:
        break
    A_max = n
for m in range(M + 1):
    if B_sums_list[m] > K:
        break
    B_max = m
ans = max(A_max, B_max)

for i in reversed(range(A_max + 1)):
    for j in reversed(range(B_max + 1)):
        if i + j > ans:
            if A_sums_list[i] + B_sums_list[j] <= K:
                ans = i + j
                break
        else:
            break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import accumulate
N, M, K = map(int,input().split())
A_list = [0] + list(map(int,input().split()))
B_list = [0] + list(map(int,input().split()))
A_sums_list = list(accumulate(A_list))
B_sums_list = list(accumulate(B_list))

for n in range(N + 1):
    if A_sums_list[n] > K:
        break
    A_max = n
for m in range(M + 1):
    if B_sums_list[m] > K:
        break
    B_max = m
ans = max(A_max, B_max)

for i in range(A_max + 1):
    for j in reversed(range(B_max + 1)):
        if i + j > ans:
            if A_sums_list[i] + B_sums_list[j] <= K:
                ans = i + j
        else:
            break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import accumulate
N, M, K = map(int,input().split())
A_list = [0] + list(map(int,input().split()))
B_list = [0] + list(map(int,input().split()))
A_sums_list = list(accumulate(A_list))
B_sums_list = list(accumulate(B_list))

for n in range(N + 1):
    if A_sums_list[n] > K:
        break
    A_max = n
for m in range(M + 1):
    if B_sums_list[m] > K:
        break
    B_max = m
ans = max(A_max, B_max)
for i in reversed(range(A_max + 1)):
    for j in reversed(range(B_max + 1)):
        if i + j > ans:
            if A_sums_list[i] + B_sums_list[j] <= K:
                ans = i + j
        else:
            break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import accumulate
N, M, K = map(int,input().split())
A_list = [0] + list(map(int,input().split()))
B_list = [0] + list(map(int,input().split()))
A_sums_list = list(accumulate(A_list))
B_sums_list = list(accumulate(B_list))

for n in range(N + 1):
    if A_sums_list[n] > K:
        break
    A_max = n
for m in range(M + 1):
    if B_sums_list[m] > K:
        break
    B_max = m
ans = max(A_max, B_max)
for i in reversed(range(A_max + 1)):
    for j in reversed(range(0, B_max + 1)):
        if i + j > ans:
            if A_sums_list[i] + B_sums_list[j] <= K:
                ans = i + j
        else:
            continue
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import accumulate
N, M, K = map(int,input().split())
A_list = [0] + list(map(int,input().split()))
B_list = [0] + list(map(int,input().split()))
A_sums_list = list(accumulate(A_list))
B_sums_list = list(accumulate(B_list))
for n in range(N + 1):
    if A_sums_list[n] > K:
        break
        A_max = n
for m in range(M + 1):
    if B_sums_list[m] > K:
        break
        B_max = m
ans = 0
for i in range(n + 1):
    for j in range(m + 1):
        if A_sums_list[i] + B_sums_list[j] <= 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 = [0] + list(map(int,input().split()))
b = [0] + list(map(int,input().split()))
for i in range(1,n+1):
  a[i] += a[i-1]
for i in range(1,m+1):
  b[i] += b[i-1] 
ans,ai,bi = 0,0,0
for i in range(n+1):
  if a[i]<=k:
    ai = i
for j in range(m+1):
  if a[ai]+b[j]<=k:
    bi = j
ans = ai+bi
for i in range(ai):
  for j in range(bi,m+1):
    if a[ai-i-1]+b[j]<=k:
      ans = max(ans,ai-i-1+j)
      bi = j
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

asum = list(itertools.accumulate(a))
bsum = list(itertools.accumulate(b))

ans = 0
for i in range(n+1):
    for j in range(m,max(-1,ans-i),-1):
        if asum[i]+bsum[j]<=k:
            ans = max(ans,i+j)
            break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

def main():
    import bisect
    def partition(A, p, r):
        x = A[r]
        i = p - 1
        for j in range(p, r):
            if A[j] <= x:
                i += 1
                a = A[i]
                b = A[j]
                A[i] = b
                A[j] = a
        c = A[i + 1]
        A[i + 1] = x
        A[r] = c
        return i + 1
    N, M, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    time = 0
    cnt = 0
    C = []
    SA = [0]
    SB = [0]
    for a in A:
        SA.append(SA[-1] + a)
    for b in B:
        SB.append(SB[-1] + b)
    for x in range(N + 1):
        L = K - SA[x]
        if L < 0:
            break
        SB.append(L)
        y_max = partition(SB, 0, len(SB) - 1) - 1
        SB.remove(L)
        C.append(y_max + x)
    print(max(C))


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()))
 
cost = {}#[[float('inf')]*(m+1) for i in range(n+1)]
#Aからi冊、Bからｊ冊読むときにかかる時間がcost[i][j]
              
cost[(0,0)] = 0
max_count = 0
 
i = 0
while i<=n:
  j = 0
  while j<m:
    cost[(i,j+1)] = cost[(i,j)] + b[j]
    j += 1
    if cost[(i,j)]<=k:
      max_count = max(max_count,i+j)
    else:
      break
  if i == n:
    break
  cost[(i+1,0)] = cost[(i,0)] + a[i]
  i += 1
  if cost[(i,0)]<=k:
    max_count = max(max_count,i)
  else:
    break
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()))

cost = {}#[[float('inf')]*(m+1) for i in range(n+1)]
#Aからi冊、Bからｊ冊読むときにかかる時間がcost[i][j]
              
cost[(0,0)] = 0
max_count = 0

i = 0
while i<=n:
  j = 0
  while j<m:
    cost[(i,j+1)] = cost[(i,j)] + b[j]
    j += 1
    if cost[(i,j)]<=k:
      max_count = max(max_count,i+j)
    else:
      break
  if i == n:
    break
  cost[(i+1,0)] = cost[(i,0)] + a[i]
  i += 1
  if cost[(i,0)]<=k:
    max_count = max(max_count,i)
  else:
    break
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()))

cost = [[float('inf')]*(m+1) for i in range(n+1)]
#Aからi冊、Bからｊ冊読むときにかかる時間がcost[i][j]
              
cost[0][0] = 0
max_count = 0

i = 0
while i<=n:
  j = 0
  while j<m:
    cost[i][j+1] = cost[i][j] + b[j]
    j += 1
    if cost[i][j]<=k:
      max_count = max(max_count,i+j)
    else:
      break
  if i == n:
    break
  cost[i+1][0] = cost[i][0] + a[i]
  i += 1
  if cost[i][0]<=k:
    max_count = max(max_count,i)
  else:
    break
print(max_count)

Submission status: Time Limit Exceeded

Solution code:
#C
from sys import stdin

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

    A_accum=A.copy()
    B_accum=B.copy()

    for i in range(1,len(A_accum)):
        A_accum[i]+=A_accum[i-1]
    A_accum.insert(0,0)
    for i in range(1,len(B_accum)):
        B_accum[i]+=B_accum[i-1]       
    B_accum.insert(0,0) 

    Comb=[]
    for i in A_accum:
        for j in B_accum:
            Comb.append([i,j])

    Total=[]
    for i in Comb:
        Total.append(sum(i))
    
    limit=[]
    for i in Total:
        if i <= K:
            limit.append(1)
        else:
            limit.append(0)

    accept=[]
    for i in range(len(limit)):
        if limit[i] ==1:
            accept.append(Comb[i])

    accept_book=[]
    for i in accept:
        accept_book.append([A_accum.index(i[0]),B_accum.index(i[1])])

    accept_num=[]
    for i in accept_book:
        accept_num.append(sum(i))
    
    print(max(accept_num))
main()

Submission status: Time Limit Exceeded

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

A_accum=A.copy()
B_accum=B.copy()

for i in range(1,len(A_accum)):
    A_accum[i]+=A_accum[i-1]
A_accum.insert(0,0)
for i in range(1,len(B_accum)):
    B_accum[i]+=B_accum[i-1]       
B_accum.insert(0,0) 

Comb=[]
for i in A_accum:
    for j in B_accum:
        Comb.append([i,j])

Total=[]
for i in Comb:
    Total.append(sum(i))
    
limit=[]
for i in Total:
    if i <= K:
        limit.append(1)
    else:
        limit.append(0)

accept=[]
for i in range(len(limit)):
    if limit[i] ==1:
        accept.append(Comb[i])

accept_book=[]
for i in accept:
    accept_book.append([A_accum.index(i[0]),B_accum.index(i[1])])

accept_num=[]
for i in accept_book:
    accept_num.append(sum(i))
    
print(max(accept_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())) 


sum_a = [0]
for i in range(n):
    sum_a.append(sum_a[i]+a[i])

sum_b = [0]
for i in range(m):
    sum_b.append(sum_b[i]+b[i])

max = 0 
for i in range(len(sum_a)):
    if sum_a[i] > k:
        max = i-1 if i-1 > max else max
        break
    else:
        for j in range(m+1):
            sum_ab = sum_a[i] + sum_b[j]
            if sum_ab > k:
                max = i+j-1 if i+j-1 > max else max
                break
            else:
                max = i+j if i+j > max else max

print(max)

Submission status: Time Limit Exceeded

Solution code:
n, m, k = map(int, input().split())
a = list(map(int, input().split())) 
a.insert(0,0)
b = list(map(int, input().split())) 
b.insert(0,0)


max = 0
for i in range(n+1):
    sum_a = sum(a[ii] for ii in range(i+1))
    if sum_a > k:
        max = i-1 if i-1 > max else max
        break
    else:
        for j in range(m+1):
            sum_ab = sum_a + sum(b[jj] for jj in range(j+1))
            if sum_ab > k:
                max = i+j-1 if i+j-1 > max else max
                break
            else:
                max = i+j if i+j > max else max

print(max)

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

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

for i in range(n+1):
    time = sum(a[:i+1])
    if time <= k:
        for j in range(m+1):
            time += b[j]
            if time > k:
                j = j-1
                break
    else:
        break
    list1.append(i+j)
print(max(list1))

Submission status: Time Limit Exceeded

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

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

ans = 0
count_a = 0
for i in a:
    count_b = 0
    for j in b:
        if (i + j) <= K and ans <= (count_a + count_b):
            ans = count_a + count_b
        count_b += 1
    count_a += 1

print(ans)

Submission status: Time Limit Exceeded

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

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

ans = 0
for i in a:
    for j in b:
        if (i + j) <= K and ans <= (a.index(i) + b.index(j)):
            ans = a.index(i) + b.index(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, b = [0], [0]
for i in range(N):
    a.append(A[i] + a[i])
for i in range(M):
    b.append(B[i] + b[i])
ans = 0
for s, i in enumerate(a):
    for t, j in enumerate(b):
        if (i + j) <= K and ans <= (s + t):
            ans = s + t
print(ans)

Submission status: Time Limit Exceeded

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

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

ans = 0
for s, i in enumerate(sumA):
    for t, j in enumerate(sumB):
        if (i + j) <= K and ans <= (s + t):
            ans = s + t

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_cumsum, B_cumsum = [0], [0]

for i in range(n):
    A_cumsum.append(A_cumsum[i] + A[i])
    
for j in range(m):
    B_cumsum.append(B_cumsum[j] + B[j])
    
ans = 0

for i in range(n + 1): # 0を最初に代入した分
    if A_cumsum[i] > k:
        break
    for j in range(m + 1):
        if A_cumsum[i] + B_cumsum[j] > k:
            break
        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_cumsum, B_cumsum = [0], [0]

for i in range(n):
    A_cumsum.append(A_cumsum[i] + A[i])
    
for j in range(m):
    B_cumsum.append(B_cumsum[j] + B[j])
    
ans = 0

for i, a in enumerate(A_cumsum):
    if a > k:
        break
    
    for j, b in enumerate(B_cumsum):
        if (a + b) > k:
            break
        elif (i + j) < ans:
            pass
        else:
            ans = (i + j)

print(ans)