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 = [int(s) for s in sys.stdin.readline().split()]
n = 0
A = [0]
for s in sys.stdin.readline().split():
    n = n + int(s)
    A.append(n)

n = 0
B = [0]
for s in sys.stdin.readline().split():
    n = n + int(s)
    B.append(n)

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

print(ans)

Submission status: Time Limit Exceeded

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

sumA = [0]
sumB = [0]
tmp = 0
answer = 0
st = 0

for i in range(N):
    tmp = tmp + A[i]
    sumA.append(tmp)

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

for i in range(N, -1, -1):
    booktmp = 0

    for j in range(st, M + 1):

        if sumA[i] + sumB[j] <= K:
            booktmp = i + j
        else:
            st = j
            break
    answer = max(answer, booktmp)


print(answer)

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline

N, M, K = [int(s) for s in input().split()]
n = 0
A = [0]
for s in input().split():
    n = n + int(s)
    A.append(n)

n = 0
B = [0]
for s in input().split():
    n = n + int(s)
    B.append(n)

ans = 0
for i, a_time in enumerate(A):
    if a_time <= K:
        for j, b_time in enumerate(B):
            read_time = a_time + b_time
            if read_time > K:
                if ans <= i + j - 1:
                    ans = i + j - 1
                break
    else:
        break

if ans == 0:
    if A[1] < K:
        ans = N + M
    elif B[1] < K:
        ans = N + M

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline

N, M, K = [int(s) for s in input().split()]
n = 0
A = [0]
for s in input().split():
    n = n + int(s)
    A.append(n)

n = 0
B = [0]
for s in input().split():
    n = n + int(s)
    B.append(n)

ans = 0
for i, a_time in enumerate(A):
    if a_time <= K:
        for j, b_time in enumerate(B):
            read_time = a_time + b_time
            if read_time <= K:
                if ans <= i + j:
                    ans = i + j
            else:
                break
    else:
        break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline

N, M, K = [int(s) for s in input().split()]
n = 0
A = [0]
for s in input().split():
    n = n + int(s)
    A.append(n)

n = 0
B = [0]
for s in input().split():
    n = n + int(s)
    B.append(n)

ans = 0
for i, a_time in enumerate(A):
    if a_time <= K:
        for j, b_time in enumerate(B):
            read_time = a_time + b_time
            if read_time <= K:
                if ans <= i + j:
                    ans = i + j
            else:
                break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline

N, M, K = [int(s) for s in input().split()]
n = 0
A = [0]
for s in input().split():
    n = n + int(s)
    A.append(n)

n = 0
B = [0]
for s in input().split():
    n = n + int(s)
    B.append(n)

ans = 0
for i, a_time in enumerate(A):
    for j, b_time in enumerate(B):
        read_time = a_time + b_time
        if read_time <= K:
            if ans <= i + j:
                ans = i + j
        else:
            break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline

N, M, K = [int(s) for s in input().split()]
A = [int(s) for s in input().split()]
B = [int(s) for s in input().split()]

A_time = [0] + [sum(A[0:i+1]) for i in range(len(A))]
B_time = [0] + [sum(B[0:i+1]) for i in range(len(B))]

ans = 0
for i, a_time in enumerate(A_time):
    for j, b_time in enumerate(B_time):
        read_time = a_time + b_time
        if read_time <= K:
            if ans <= i + j:
                ans = i + j
        else:
            break

print(ans)

Submission status: Time Limit Exceeded

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

result = 0
while k > 0:
  if a_books and b_books:
    if a_books[0] <= b_books[0]:
      time = a_books.pop(0)
    else:
      time = b_books.pop(0)
  elif a_books and not b_books:
    time = a_books.pop(0)
  elif not a_books and b_books:
    time = b_books.pop(0)
  else:
    break
  
  if time <= k:
    result += 1
    k -= time

print(result)

Submission status: Time Limit Exceeded

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

A = [0]
B = [0]

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

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()))
 
totalA = [0]
totalB = [0]
sumA = 0
for i in range(N):
    sumA += A[i]
    totalA.append(sumA)
sumB = 0
for i in range(M):
    sumB += B[i]
    totalB.append(sumB)

iA = N
iB = 0
countMax = 0
while iA >= 0:
    if totalA[iA] + totalB[iB] > K:
        iA -= 1
    else:
        while totalA[iA] + totalB[iB] <= K:
          countMax = max(countMax,iA+iB)
          if iB == M:
            break
          iB += 1
    if iA == N and iB == M:
      break
          
print(countMax)

Submission status: Time Limit Exceeded

Solution code:


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

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

As_sum = [0]
Bs_sum = [0]

answer = 0

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

answer = 0

for i in range(len(As_sum)):
    j = M
    if As_sum[i] > K:
        continue
    while As_sum[i] + Bs_sum[j] > K and j>=0:
        j -= 1
    answer = max(answer, i+j)

print(answer)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

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

As_selected = []
Bs_selected = []

answer = 0

for i in range(N+1):
    if(As[0:i].sum() <= K):
         As_selected.append(As[0:i].sum())
    else:
        break
for i in range(M+1):
    if(Bs[0:i].sum() <= K):
         Bs_selected.append(Bs[0:i].sum())
    else:
        break

answer = 0

for i in np.arange(len(As_selected))+1:
    for j in np.arange(len(Bs_selected))+1:
        if(As_selected[-i] + Bs_selected[-j] <= K):
            answer = max(len(As_selected)+len(Bs_selected) -i - j, answer)
            break

print(answer)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

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

As_selected = []
Bs_selected = []

answer = 0

for i in range(N+1):
    if(As[0:i].sum() <= K):
         As_selected.append(As[0:i].sum())
    else:
        break
for i in range(M+1):
    if(Bs[0:i].sum() <= K):
         Bs_selected.append(As[0:i].sum())
    else:
        break

answer = 0

for i in range(len(As_selected)):
    for j in range(len(Bs_selected)):
        if(As_selected[i] + Bs_selected[j] <= K):
            answer = max(i + j , answer)
        else:
            break

print(answer)

Submission status: Time Limit Exceeded

Solution code:
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()]

cnt_max=0

pat="A"*n+"B"*m
import itertools as it
for buf in set(it.permutations(pat)):
#  print(buf)
  kb=k
  ai=0
  bi=0
  cnt=0
  for c in buf:
    if c=="A":
      if kb>=a[ai]:
        kb-=a[ai]
 #       print(a[ai])
        cnt+=1
        ai+=1
      else:
        break
    else:
      if kb>=b[bi]:
        kb-=b[bi]
        cnt+=1
        bi+=1
      else:
        break
#  print(cnt)
  if cnt_max<cnt:
    cnt_max=cnt

print(cnt_max)

Submission status: Time Limit Exceeded

Solution code:
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()]

cnt_list=[]

pat="A"*n+"B"*m
import itertools as it

for buf in set(it.permutations(pat)):
 # print(buf)
  kb=k
  ai=0
  bi=0
  cnt=0
  for c in buf:
    if c=="A":
      if kb>=a[ai]:
        kb-=a[ai]
 #       print(a[ai])
        cnt+=1
        ai+=1
      else:
        break
    else:
      if kb>=b[bi]:
        kb-=b[bi]
        cnt+=1
        bi+=1
      else:
        break
#  print(cnt)
  cnt_list.append(cnt)

print(max(cnt_list))

Submission status: Time Limit Exceeded

Solution code:
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()]

cnt_list=[]

pat="A"*n+"B"*m
import itertools as it

for buf in it.permutations(pat):
 # print(buf)
  kb=k
  ai=0
  bi=0
  cnt=0
  for c in buf:
    if c=="A":
      if kb>=a[ai]:
        kb-=a[ai]
 #       print(a[ai])
        cnt+=1
        ai+=1
      else:
        break
    else:
      if kb>=b[bi]:
        kb-=b[bi]
        cnt+=1
        bi+=1
      else:
        break
#  print(cnt)
  cnt_list.append(cnt)

print(max(cnt_list))

Submission status: Time Limit Exceeded

Solution code:
number=list(map(int,input().split()))
N,M,K=number[0],number[1],number[2]
A=list(map(int,input().split()))
B=list(map(int,input().split()))
i=len(A)
C=A
answer=0
l=M
if sum(A)>K:
    while sum(C)>=K:
        i-=1
        C=A[:i]
for j in range(len(C),0,-1):
    C=C[:j]
    while sum(B[:l])>K-sum(C):
        l-=1
        
    key=l+j
    answer=max(answer,key)
print(answer)

Submission status: Time Limit Exceeded

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

A = [0]
B = [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 n in range(N + 1):
  for m in range(M + 1):
    time = A[n] + B[m]
    if time <= K:
      ans = max(ans, n + m)
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()))
ALLB = sorted(A+B)

sumt = 0
cnt = 0
summax = sum(ALLB)
if summax <= K:
    print(N+M)
    exit(0)
    
for i in range(N+M):
    sumt += ALLB.pop(0)
    if sumt > K:
        print(cnt)
        exit(0)
        
    cnt += 1

Submission status: Time Limit Exceeded

Solution code:

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

sumt = 0
cnt = 0
for i in range(N+M):
    sumt += ALLB.pop(0)
    if sumt > K:
        print(cnt)
        exit(0)
    cnt += 1

print(N+M)

Submission status: Time Limit Exceeded

Solution code:
def get_sum_arr(arr):
    sum_arr = [0]
    N = len(arr)
    for i in range(1,N+1):
        sum_arr.append(sum_arr[i-1] + arr[i-1])
    return sum_arr
def get_max_book_num(A,B,K):
    A_sum_arr,B_sum_arr = get_sum_arr(A),get_sum_arr(B)
    count,i,j = 0,0,0
    for i in range(0,len(A_sum_arr)):
        if K < A_sum_arr[i]:
            break
        else:
            j = 0
            while B_sum_arr[j] <= (K - A_sum_arr[i]):
                j += 1
                if j >= len(B_sum_arr):
                    break
            j -= 1
        count = max(count,(i+j))
    return count

if __name__ == "__main__":
    N,M,K = list(map(int,input().split()))
    A = list(map(int,input().split()))
    B = list(map(int,input().split()))
    print(get_max_book_num(A,B,K))

Submission status: Time Limit Exceeded

Solution code:
def limit_b_arr_idx(B,K):
    j=0
    for j in range(0,len(B)):
            if K < B[j]:
                j -= 1
                break
            else:
                K -= B[j]
    return j
    
if __name__ == "__main__":
    N,M,K = list(map(int,input().split()))
    A = list(map(int,input().split()))
    B = list(map(int,input().split()))
    count,i_a,i_b = 0,0,0
    # ADD A item
    for i_a in range(len(A)):
        if K < A[i_a]:
            i_a -= 1
            break
        else:
            K -= A[i_a]
    # ADD B item
    
    i_b = limit_b_arr_idx(B,K)
    count = (i_a+1) + (i_b+1) # 読んだ本のindexの次のindexが格納されている。
    # 1冊も読めない⇒0
    # idx = i_a,i_bまで読んだ。
    # forの繰り返しをすべて終えた時 +1されるのか?
    for i in range(i_a,-1,-1): # A のアイテムを1つずつ減らしていく。
        K += A[i] # i番目の本を読まなかったとする。
        i_b = limit_b_arr_idx(B,K)
        count = max(count,(i+i_b+1))
    print(count)

Submission status: Time Limit Exceeded

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

sa = {0: 0}
for i, val in enumerate(A):
  sa[i+1] = sa[i] + val

sb = {0: 0}
for i, val in enumerate(B):
  sb[i+1] = sb[i] + val

max_num = 0

for i in range(N+1):
  if(sa[i]>K):
    break
  for j in range(M+1):
    if(sb[M-j]<=K-sa[i]):
      break
  max_num = max(max_num,i+M-j)

      
      
print(max_num)

Submission status: Time Limit Exceeded

Solution code:
INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))
def do(): 
    n,m,k=INTM()
    A=LIST()
    B=LIST()
    As=[0]*(n+1)
    Bs=[0]*(m+1)
    ans=0
    for i in range(n):
        As[i+1]=As[i]+A[i]
    A=As
    for i in range(m):
        Bs[i+1]=Bs[i]+B[i]
    B=Bs
    #print(A,B)

    if A[-1]+B[-1]<=k:
        ans=n+m

    for i in range(n+1):

        if A[i]>k:
            #print(i)
            temp=i-1
            ans=max(ans,temp)
            break
        for i2 in range(m+1):
            #print(i,i2)
            #print(A[i]+B[i2])
            if A[i]+B[i2]>k:
                temp=i+i2-1
                #print(ans,temp)
                ans=max(ans,temp)
                break
        

    
    print(ans)

if __name__ == '__main__':
    do()

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

for i in range(n-1):
    a[i+1] = a[i] + a[i+1]
a.insert(0, 0)
for i in range(m-1):
    b[i+1] = b[i] + b[i+1]
b.insert(0, 0)

x = 0
jmax = 0
for i in range(n+1):
    for j in range(jmax, m+1):
        if b[m-j] > k-a[i]:
            pass
        else:
            jmax = m-j
            if jmax + i > x:
                x = jmax + i
print(x)

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

for i in range(n-1):
    a[i+1] = a[i] + a[i+1]
a.insert(0, 0)
for i in range(m-1):
    b[i+1] = b[i] + b[i+1]
b.insert(0, 0)

x = 0
for i in range(n+1):
    for j in range(m+1):
        if b[m-j] > k-a[i]:
            pass
        else:
            jmax = m-j
            if jmax + i > x:
                x = jmax + i
print(x)

Submission status: Time Limit Exceeded

Solution code:
import sys


def input(): return sys.stdin.readline().strip()


def main():
    N, M, K = map(int, input().split())
    A = [int(i) for i in input().split()]
    B = [int(i) for i in input().split()]

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

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

    ans, j = 0, M
    for i in range(len(a)):
        if a[i] > K:
            break
        while a[i] + b[j] > K:
            j -= 1
        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()))
ta=sum(a)
a.append(0)
tb=0
ans=0

for i in range(n+1):
    ta -= a[n-i]
    if ta>k:
        continue
    for j in range(m):
        tb += b[j]
        if tb + ta >k:
            break
        if j==m-1:
            j +=1

    ans=max(ans,n-i+j)
    tb=0
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()))

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

s2 = [0]
for i in range(m):
    s2.append(s2[-1]+b[i])
ans = 0
for i in range(n+1):
    p = k-s1[i]
    j = m
    if p<0:
        break
    while p-s2[j]<0 and j>0:
            j -= 1 
    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()))
ta=0
tb=0
ans=0
for i in range(n+1):
    if i>0:
        ta += a[i-1]

    if ta>k:
        break
    for j in range(m):
        tb=sum(b[:j+1])
        ans=max(ans,i+j)
        if tb + ta >k:
            break
        ans=max(ans,i+j+1)
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]"):
    a = 0
    a_n = [a]
    for a_i in A:
        a += a_i
        a_n.append(a)

    b = 0
    b_n = [b]
    for b_i in B:
        b += b_i
        b_n.append(b)
    b_n = b_n[::-1]

    max_ = 0
    max_j = M
    for i, a in enumerate(a_n):
        for j, b in enumerate(b_n[-(max_j+1):]):
            if b <= (K-a):
                num = i+max_j-j
                if max_ < num:
                    max_ = num
                max_j -= j
                break

    return max_


# 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]"
    print(solve(N, M, K, A, B))

if __name__ == '__main__':
    main()

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]"):
    a = 0
    a_n = [a]
    for a_i in A:
        a += a_i
        a_n.append(a)

    b = 0
    b_n = [b]
    for b_i in B:
        b += b_i
        b_n.append(b)
    b_n = b_n[::-1]

    maxs = []
    max_j = M
    for i, a in enumerate(a_n):
        for j, b in enumerate(b_n[-(max_j+1):]):
            if b <= (K-a):
                maxs.append(i+max_j-j)
                max_j -= j
                break

    return max(maxs)


# 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]"
    print(solve(N, M, K, A, B))

if __name__ == '__main__':
    main()

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]"):
    a = 0
    a_n = [a]
    for a_i in A:
        a += a_i
        a_n.append(a)

    b = 0
    b_n = [b]
    for b_i in B:
        b += b_i
        b_n.append(b)

    maxs = []
    max_j = M
    for i, a in enumerate(a_n):
        for j, b in enumerate(b_n[:max_j+1][::-1]):
            #print("j, b", j, b)
            if b <= (K-a):
                #print("a, b:", a, b)
                #print("i, j", i, j)
                maxs.append(i+max_j-j)
                max_j -= j
                #print("maxs:", maxs)
                break

    return max(maxs)


# 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]"
    print(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 = [0]+list(map(int, input().split()))
b = [0]+list(map(int, input().split()))

Goukei_a=[a[0]]
for i in range(1,n+1):
    Goukei_a.append(Goukei_a[i-1]+a[i])

Goukei_b=[b[0]]
for i in range(1,m+1):
    Goukei_b.append(Goukei_b[i-1]+b[i])

goukei=0
kaisuu=0
ans=0

for i in range(0,n+1):
    if Goukei_a[i]>k:
        break
    for j in range(0,m+1):
        if Goukei_b[j]>k:
            break
        goukei=Goukei_a[i]+Goukei_b[j]
        if goukei<=k:
            ans=max(ans,i+j)
        else:
            break
    
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()))

Goukei_a=[a[0]]
for i in range(1,n+1):
    Goukei_a.append(Goukei_a[i-1]+a[i])

Goukei_b=[b[0]]
for i in range(1,m+1):
    Goukei_b.append(Goukei_b[i-1]+b[i])

goukei=0
kaisuu=0
ans=0

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

Submission status: Time Limit Exceeded

Solution code:
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
a_wa,b_wa = [0],[0]
for i,num in enumerate(a):
    a_wa.append(num + a_wa[i])
for i,num in enumerate(b):
    b_wa.append(num + b_wa[i])
count = 0
for i,a_value in enumerate(a_wa):
    for j,b_value in enumerate(b_wa):
        if a_value + b_value <= k:
            count = max(count , i + j)
        else:
            break
print(count)

Submission status: Time Limit Exceeded

Solution code:
def main():
    n, m, k = map(int, input().split())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    ans = solve(n, m, k, a, b)
    print(ans)

def solve(n, m, k, a, b):
    aa, bb = [0], [0]
    for i in range(n):
        aa.append(aa[i] + a[i])
    for i in range(m):
        bb.append(bb[i] + b[i])

    ans = 0
    for i in range(n + 1):
        if aa[i] > k:
            break
        j = m
        while bb[j] > k - aa[i]:
            j -= 1
        ans = max(ans, i + j)
    return 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()))

s_a = [0]
s_b = [0]
for i in range(n):
    s_a.append(s_a[i] + a[i])
for i in range(m):
    s_b.append(s_b[i] + b[i])
    
ans = 0
ok = m
for i in range(n+1):
    for j in range(ok+1):
        if s_a[i] + s_b[j] <= k:
            ans = max(ans, i+j)
            tmp = j
    ok = tmp

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

Submission status: Time Limit Exceeded

Solution code:
nmk = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))

def read(an):
    sumb = 0
    for i in range(nmk[1]):
        if sum(a[0:an]) + sumb + b[i] <= nmk[2]:
            sumb += b[i]
        else:
            return an+i
            break
    return an+nmk[1]

ans=read(0)
for j in range(nmk[0]+1):
    if sum(a[0:j]) <= nmk[2]:
        if read(j) > ans:
            ans = read(j)
    else:
        break

print(ans)

Submission status: Time Limit Exceeded

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


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

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

maxman=0
for i in range(len(A)):
    for j in range(len(B)):
        if A[i]+B[j]<=k:
            maxman=max(maxman,i+j)
            
print(maxman)

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

Submission status: Time Limit Exceeded

Solution code:
N,M,K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
 
a,b = [0],[0]
n,m,ans = 0, 0, 0
 
for i in range(N):
  a.append(a[i]+A[i])
  if a[i] <= K:
    n = i
for i in range(M):
  b.append(b[i]+B[i])
  if b[i] <= K:
    m = i

n = max(n,m)

for i in range(n,N+M+2):
  tmp = 0
  for j in range(max(0,i-M),min(N+1,i+1)):
    if a[j]+b[i-j] <= K:
      tmp = 1
      ans = i
      break
  if tmp == 0:
    break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,M,K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
 
a,b = [0],[0]
k,ans = 0, 0
 
for i in range(N):
  a.append(a[i]+A[i])
  if a[i] <= K:
    k = i
for i in range(M):
  b.append(b[i]+B[i])

for i in range(k,N+M+2):
  tmp = 0
  for j in range(max(0,i-M),min(N+1,i+1)):
    if a[j]+b[i-j] <= K:
      tmp = 1
      ans = i
      break
  if tmp == 0:
    break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,M,K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
 
a,b = [0],[0]
ans = 0
 
for i in range(N):
  a.append(a[i]+A[i])
for i in range(M):
  b.append(b[i]+B[i])
  
for i in range(N+M+2):
  tmp = 0
  for j in range(max(0,i-M),min(N+1,i+1)):
    if a[j]+b[i-j] <= K:
      tmp = 1
      ans = i
      break
  if tmp == 0:
    break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,M,K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
 
a,b = [0],[0]
ans = 0
 
for i in range(N):
  a.append(a[i]+A[i])
for i in range(M):
  b.append(b[i]+B[i])
  
for i in range(N+M+2):
  tmp = 0
  for j in range(min(M+1,i+1)):
    if i-j > N:
      continue
    if a[i-j]+b[j] <= K:
      tmp = 1
      ans = i
      break
  if tmp == 0:
    break
 
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

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

sumA = sum(A)
countA = len(A)
ans = 0
for i in range(N,-1,-1):
    if i != N:
        sumA -= A[i]
        countA -= 1
    time = sumA
    
    if time > K:
        count = 0
    else:
        count = countA
    j=0
    while time+B[j] <=K:
        count += 1
        time += B[j]
        j += 1
        if j >= M:
            break
        #print(count,time,B[i])
    ans = max(ans,count)
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()))
ta=0
tb=0
ans=0
for i in range(n+1):
    ta=sum(a[:i])
    if ta>k:
        break
    for j in range(m):
        tb=sum(b[:j+1])
        ans=max(ans,i+j)
        if tb + ta >k:
            break
        ans=max(ans,i+j+1)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools
import numpy as np
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
aa = [sum(a[0:i]) for i in range(n+1)]
bb = [sum(b[0:i]) for i in range(m+1)]
ab = [list(range(n+1)), list(range(m+1))]
candidate = list(itertools.product(*ab))
candidate2 = []
for i in range(n+1):
  candidate2.append(candidate[i*(m+1):i*(m+1)+m+1])
answer = -1
for can in candidate2:
  for c in can:
    time = aa[c[0]] + bb[c[1]]
    if time <= k:
      answer = max(answer, c[0]+c[1])
    else:
      break
print(answer)

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()))
aa = [sum(a[0:i]) for i in range(n+1)]
bb = [sum(b[0:i]) for i in range(m+1)]
ab = [list(range(n+1)), list(range(m+1))]
candidate = list(itertools.product(*ab))
answer = -1
for can in candidate:
  time = aa[can[0]] + bb[can[1]]
  if time <= k:
    answer = max(answer, can[0]+can[1])
print(answer)