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

n = N
while True:
    if sum(A[:n]) <= K:
        break
    n -= 1

m = M
max_num = 0
for n in range(n + 1):
    #print(n, m)
    if sum(A[:n]) > K:
        break
    while True:
        #print(n, m)
        if sum(A[:n]) + sum(B[:m]) <= K:
            max_num = max(max_num, n+m)
            break
        m -= 1
# print(cost)
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()))
aa=[0]
bb=[0]
a_sum=0
b_sum=0

for i in range(n):
    if (a_sum+a[i])<=k:
        a_sum+=a[i]
        aa.append(a_sum)
    else:
        break
for i in range(m):
    if (b_sum+b[i])<=k:
        b_sum+=b[i]
        bb.append(b_sum)
    else:
        break

#print(aa)
#print(bb)
cnt=0
for i in range(len(aa)):
    for j in range(len(bb)):
        if (aa[i]+bb[j])<=k:
            cnt=max(cnt,i+j)
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from queue import deque

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

A = input().split()
B = input().split()

que = deque([(0, K, 0, 0)])

max_count = 0

visited = set((0, 0))

while que:
    count, k, a_index, b_index = que.popleft()
    if (a_index, b_index) in visited:
        continue
    else:
        visited.add((a_index, b_index))
    max_count = max(max_count, count)
    if a_index >= len(A) and b_index >= len(B):
        break
    if a_index < len(A) and 0 <= k-int(A[a_index]):
        que.appendleft((count+1, k-int(A[a_index]), a_index+1, b_index))
    if b_index < len(B) and 0 <= k-int(B[b_index]):
        que.appendleft((count+1, k-int(B[b_index]), a_index, b_index+1))

print(max_count)

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_cnt in range(n+1):
    if a[a_cnt] > k:
        continue

    b_cnt = 0
    while b_cnt < m and a[a_cnt] + b[b_cnt + 1] <= k:
        b_cnt += 1
    ans = max(ans, a_cnt+b_cnt)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(2147483647)

def sum_A(i, A):
  if i == 0:
    return 0
  return sum_A(i-1, A) + A[i-1]

def sum_B(j, B):
  if j == 0:
    return 0
  return sum_B(j-1, B) + B[j-1]  
  
def sum(i, j, A, B):##A[0]~A[i-1](i=0のときは読まない) B[0]~B[j-1](j=0のときは読まない)の合計値を計算する関数
   return sum_A(i, A) + sum_B(j, B)
  
n, m, k = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
i = 0
count = 0
while i < n+1:
  j = 0
  while j < m+1:
    if sum(i, j, A, B) <= k and count < i + j:
      count = i + j 
    j +=1
  i +=1
  
print(count)

Submission status: Time Limit Exceeded

Solution code:
n, m, k = map(int,input().split())
lA = list(map(int, input().split()))
lB = list(map(int, input().split()))
ans = 0
for i in range(len(lA)+1):
    total_time = sum(lA[:i])
    if total_time <= k:
        books = len(lA[:i])
        ans = max(ans, books)
        for j in lB:
            total_time += j
            if total_time > k:
                break
            else:
                books += 1
                ans = max(ans, books)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
#import string
#from collections import defaultdict, deque, Counter
#import bisect
#import heapq
#import math
#from itertools import accumulate
#from itertools import permutations as perm
#from itertools import combinations as comb
#from itertools import combinations_with_replacement as combr
#from fractions import gcd
#import numpy as np

stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)
MIN = -10 ** 9
MOD = 10 ** 9 + 7
INF = float("inf")
IINF = 10 ** 18

def solve():
    #n = int(stdin.readline().rstrip())
    n,m,k = map(int, stdin.readline().rstrip().split())
    A = list(map(int, stdin.readline().rstrip().split()))
    B = list(map(int, stdin.readline().rstrip().split()))
    #numbers = [[int(c) for c in l.strip().split()] for l in sys.stdin]
    #word = [stdin.readline().rstrip() for _ in range(n)]
    #number = [[int(c) for c in stdin.readline().rstrip()] for _ in range(n)]
    #zeros = [[0] * w for i in range(h)]
    Ar = [0]
    for i in range(n):
        Ar.append(Ar[i] + A[i])
    Br = [0]
    for i in range(m):
        Br.append(Br[i] + B[i])
    ans = 0
    end = m
    for i in range(n+1):
        flag = 0
        time_a = Ar[i]
        for j in range(m):
            while end >= 0:
                if Br[end] + time_a > k:
                    end -= 1
                else:
                    ans = max(ans, i+end)
                    flag = 1
                    break
            if flag == 1:
                flag = 0
                break

    print(ans)


if __name__ == '__main__':
    solve()

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

time = float(0)
max_b = 0

for i in range(n+1):
    for j in range(m+1):
        r_time = sum(a[:i]) + sum(b[:j])
        if not r_time > k and max_b < i+j:
            max_b = i+j

print(max_b)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
def readinput():
    n,m,k=list(map(int,input().split()))
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    return n,m,k,a,b

def main(n,m,k,a,b):
    qa=deque(a)
    qb=deque(b)
    count=0
    res=k
    while(len(qa)>0 and len(qb)>0):
        if qa[0] < qb[0]:
            res-=qa[0]
            qa.popleft()
        else:
            res-=qb[0]
            qb.popleft()
        if res<0:
            return count
        count+=1
    else:
        if len(qa)==0:
            while(len(qb)>0):
                res-=qb[0]
                qb.popleft()
                if res<0:
                    return count
                count+=1
            return count
        else:
            while(len(qa)>0):
                res-=qa[0]
                qa.popleft()
                if res<0:
                    return count
                count+=1
            return count

import sys
INFTY=sys.maxsize
def find(t,s):
    '''
    単調増加配列sからs[i]がtを超えない最大のiを返す
    '''
    ss=s[:]
    ss.append(INFTY)
    left=0
    right=len(ss)
    while(left<right):
        middle=(left+right)//2
        #print(left,middle,right,t,ss)
        if ss[middle]==t:
            #print(middle, ss)
            return middle
        if t<ss[middle]:
            right=middle
        else:
            left=middle+1
    else:
        return left-1

def main2(n,m,k,a,b):
    sa=[0]
    for i in range(1,n+1):
        sa.append(sa[i-1]+a[i-1])
    sb=[0]
    for i in range(1,m+1):
        sb.append(sb[i-1]+b[i-1])
    maxab=0
    for na in range(0,n+1):
        res=k-sa[na]
        if res>=0:
            nb=find(res,sb)
            #print(na,nb,sa,sb)
            maxab=max(maxab,na+nb)
    return maxab


def readinput2():
    m=2*10**5
    n=2*10**5
    a=[10**9]*n
    b=[10**9]*m
    k=1
    return n,m,k,a,b

if __name__=='__main__':
    n,m,k,a,b=readinput()
    ans=main2(n,m,k,a,b)
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
n,m,k=map(int,input().split())
a=deque(map(int,input().split()))
b=deque(map(int,input().split()))
b_sum=deque()
b_sum.append(b[0])
b_j=b[0]
for j in range(1,m):
    b_j=b_j+b[j]
    b_sum.append(b_j)
b_sum.append(10**10)
b_sum.appendleft(0)
b_sum.appendleft(-1)
ans=0
a.append(0)
a_sum=0
for l in range(n+1):
    timecount=k-a_sum
    a_sum+=a[l]
    if timecount>=0:
        ok=0
        ng=m+2
        while ng-ok>1:
            midlle=(ng+ok)//2
            if b_sum[midlle]<=timecount:
                ok=midlle
            else:
                ng=midlle
        ans=max(l+ng-2,ans)
    else:
        break
print(ans)

Submission status: Time Limit Exceeded

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

l_ans=[]
for i in range(m+1):
  l_ans.append([0]*(n+1))
ct=0

for i in range(m+1):
  we=i+n
  for j in range(n+1):
    if i==0 and j==0:
      l_ans[i][j]=0
    elif i==0:
      l_ans[i][j]=l_ans[i][j-1]+l[j-1]
    elif j==0:
      l_ans[i][j]=l_ans[i-1][j]+ll[i-1]
    else:
      l_ans[i][j]=l_ans[i][j-1]+l[j-1]
    
    if l_ans[i][0]>k:
      we=0
    elif l_ans[i][j]>k:
      we=i+j-1
      break
    elif l_ans[i][j]==k:
      we=i+j
      break
  
  #print(ct,we)

  if we>ct:
    ct=we
#print(l_ans)
print(ct)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
n,m,k=map(int,input().split())
a=deque(map(int,input().split()))
b=deque(map(int,input().split()))
a_sum=deque()
b_sum=deque()
a_sum.append(a[0])
b_sum.append(b[0])
a_i=a[0]
b_j=b[0]
for i in range(1,n):
    a_i=a_i+a[i]
    a_sum.append(a_i)
for j in range(1,m):
    b_j=b_j+b[j]
    b_sum.append(b_j)
b_sum.append(10**10)
b_sum.appendleft(0)
b_sum.appendleft(-1)
a_sum.appendleft(0)
ans=0
for l in range(n+1):
    timecount=k-a_sum[l]
    if timecount>=0:
        ok=0
        ng=m+2
        while ng-ok>1:
            midlle=(ng+ok)//2
            if b_sum[midlle]<=timecount:
                ok=midlle
            else:
                ng=midlle
        ans=max(l+ng-2,ans)
    else:
        break
print(ans)

Submission status: Time Limit Exceeded

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

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

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

ans = 0

for i in range(n+1):
    cnt = 0
    x = k
    x -= sum(a[:i])
    
    if x<0:
        continue
    
    while x>0 and cnt<m:
        x -= b[cnt]
        cnt += 1
        
    if x<0:
        cnt -= 1
        
    ans = max(ans,cnt+i)
    
print(ans)

Submission status: Time Limit Exceeded

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

a=[sum(A[:n]) for n in range(N+1)]
b=[sum(B[:m]) for m in range(M+1)]
num=0
bmax=M+1
for n in range(N+1):
    if a[n]>K:
        break
    for m in reversed(range(bmax)):
        if b[m]<=K-a[n]:
            num=max(num,n+m)
            bmax=m+1
            break
print(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()))
time = []
#print(time.pop(1))
count = int()

while K - sum(time) >= 0:
    if sum(A) == 0:
        if sum(B) == 0:
            break
        if K - sum(time) >= min(B):
            #print('B:', B)
            time.append(min(B))
            B.pop(0)
            count += 1
            #print('K - sum(time)b:', K - sum(time))
            #print('最終結果：A', A, '最終結果：B', B, '残時間：', K - sum(time))
            continue


    elif sum(B) == 0:
        if sum(A) == 0:
            break
        if K - sum(time) >= min(A):
            #print('A:', A)
            time.append(min(A))
            A.pop(0)
            count += 1
            #print('K - sum(time)a:', K - sum(time))
            #print('最終結果：A', A, '最終結果：B', B, '残時間：', K - sum(time))
            continue

    else:
        if min(A) >= min(B):
            if K - sum(time) >= min(B):
                # print('B:', B)
                time.append(min(B))
                B.pop(0)
                count += 1
                # print('K - sum(time)b:', K - sum(time))
                #print('最終結果：A', A, '最終結果：B', B, '残時間：', K - sum(time))
                continue
            else:
                break
        else:
            if K - sum(time) >= min(A):
                # print('A:', A)
                time.append(min(A))
                A.pop(0)
                count += 1
                # print('K - sum(time)a:', K - sum(time))
                #print('最終結果：A', A, '最終結果：B', B, '残時間：', K - sum(time))
                continue
            else:
                break

print(count)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
n, m, k = map(int, stdin.readline().split())
astack = list(map(int, stdin.readline().split()))
bstack = list(map(int, stdin.readline().split()))

# max books from A upto which total time <= k.
tot_time = 0
maxbooks = 0
prefix_tot_time_a = [0]
for i in astack:
    if tot_time + i <= k:
        tot_time += i
        maxbooks += 1
        prefix_tot_time_a.append(tot_time)
    else:
        break

# remove one book at a time from maxbooks of A. And checking how many more books of B we can take.
ans = maxbooks

prefix_tot_time_b = [bstack[0]]
for i in bstack[1:]:
    prefix_tot_time_b.append(prefix_tot_time_b[-1] + i)

# maxbooks-1 is also the index position of the last book taken from A.
for ind1 in range(maxbooks, -1, -1):
    currmax = ind1
    tot_time = prefix_tot_time_a[ind1]
    for ind2 in range(m):
        if tot_time + prefix_tot_time_b[ind2] <= k:
            currmax += 1
        else:
            break
    ans = max(ans, currmax)
print(ans)
            


























########## MY ATTEMPT IN CONTEST; COULD ONLY GET 10 AC AND 10 WA ################
# Tried greedy approach in various ways which were wrong.


# from sys import stdin
# from collections import deque as dq
# from itertools import zip_longest
# n, m, k = map(int, input().split())
# a = dq(map(int, stdin.readline().split()))
# b = dq(map(int, stdin.readline().split()))

# tot = 0
# count = 0

# i = j = 0
# while i < n and j < m:
#     # pick one line
#     if tot + a[i] + b[j] > k:
#         count1 = 0
#         ssf = tot
#         for ind1 in range(i, n):
#             if a[ind1] + ssf > k:
#                 break
#             else:
#                 ssf += a[ind1]
#                 count1 += 1

#         count2 = 0
#         ssf = tot
#         for ind2 in range(j, m):
#             if b[ind2] + ssf > k:
#                 break
#             else:
#                 ssf += b[ind2]
#                 count2 += 1

#         if count1 > count2:
#             print(count + count1)
#         else:
#             print(count + count2) 
#         exit()

#     # pick from any line 
#     else:
#         if tot + min(a[i], b[j]) > k:
#             break
#         elif a[i] < b[j]:
#             tot += a[i]
#             count += 1
#             i += 1
#         elif b[j] < a[i]:
#             tot += b[j]
#             count += 1
#             j += 1
#         else:
#             count1 = 0
#             for ind1 in range(i, n):
#                 if a[ind1] <= a[i]:
#                     count1 += 1
#                 else:
#                     break
#             count2 = 0
#             for ind2 in range(j, m):
#                 if a[ind2] <= b[j]:
#                     count2 += 1
#                 else:
#                     break
            
#             if count1>count2:
#                 tot += a[i]
#                 i += 1
#             elif count2<count1:
#                 tot += b[j]
#                 j += 1
#             else:
#                 if sum(a[i: min(i + count1 + 1, n)]) < sum(b[j: min(j + count2 + 1, m)]):
#                     tot += a[i]
#                     i += 1
#                 else:
#                     tot += b[j]
#                     j += 1
#             count += 1


# if i == n and j == m:
#     print(count)
# elif i < n:
#     for ind in range(i, n):
#         if tot + a[ind] <= k:
#             count += 1
#             tot += a[ind]
#     print(count)
# elif j < m:
#     for ind in range(j, m):
#         if tot + b[ind] <=k:
#             count += 1
#             tot += b[ind]
#     print(count)




# while tot < k:
#     atop = a[0] if a else 10**10
#     btop = b[0] if b else 10**10

#     # both empty
#     if atop == 10**10 and btop == 10**10:
#         break
    
#     # if adding the minimum crosses k.
#     if tot + min(atop, btop) > k:
#         break
    
#     if tot + atop + b
#     if atop < btop:
#         tot += a.popleft()
#         count += 1
#     elif btop < atop:
#         tot += b.popleft()
#         count += 1
#     else:
#         rema = True
#         for i, j in zip(a, b):
#             if i < j:
#                 rema = True
#                 break
#             elif j < i:
#                 rema = False
#                 break
#             else:
#                 pass
#         if rema:
#             tot += a.popleft()
#             count += 1
#         else:
#             tot += b.popleft()
#             count += 1

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

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

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

    ans = 0
    for ai, at in enumerate(a):
        if at > k:
            break

        bi = m
        while at + b[bi] > k:
            bi -= 1
        
        ans = max(ans, ai+bi)
    
    print(ans)
            

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys

input_data = []
input_pointer = 0

output_data = []

def input_scan():
    global input_data
    input_data = sys.stdin.readlines()
    input_data = [x[:-1] for x in input_data]

def next_line():
    global input_pointer
    global input_data
    if input_pointer < len(input_data):
        input_pointer += 1
        return input_data[input_pointer - 1]
    else:
        return ""

def append_output(string):
    global output_data
    output_data.append(string)

def print_output():
    global output_data
    output_data.append("")
    sys.stdout.write("\n".join(output_data))

# problem A
# a = int(input())
# append_output(str(a + a * a + a * a * a))
# print_output()

# problem B
# input_scan()
# s = next_line()
# t = next_line()
# print(sum([1 for x, y in zip(list(s), list(t)) if x != y]))

input_scan()
[n, m, k] = [int(x) for x in next_line().split(" ")]
stack_a = [int(x) for x in next_line().split(" ")]
stack_b = [int(x) for x in next_line().split(" ")]

total_books = 0

remaining_time = k
for i in range(len(stack_b)):
    if remaining_time - stack_b[i] >= 0:
        total_books += 1
        remaining_time -= stack_b[i]
    else:
        break

remaining_time = k
total_books_with_stack_a = 0
for i in range(len(stack_a)):
    if remaining_time < stack_a[i]:
        break

    remaining_time -= stack_a[i]
    remaining_time_for_b = remaining_time
    total_books_with_stack_a += 1
    total_books_this_time = total_books_with_stack_a
    for j in range(len(stack_b)):
        if remaining_time_for_b - stack_b[j] >= 0:
            remaining_time_for_b -= stack_b[j]
            total_books_this_time += 1
        else:
            break

    total_books = max(total_books, total_books_this_time)

append_output(str(total_books))
print_output()

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])
    
c=0

for i in range(n+1):
    for j in range(m+1):
        if a[i]+b[m-j]>k:
            continue
        else:
            c=max(i+m-j,c)
            continue
else:
    print(c)

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

c=0
for i in range(n+1):
    for j in range(m+1):
        if a[i]+b[j]>k:
            break
        else:
            c=max(c,i+j)
else:
    print(c)

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

A = [0]+A
B = [0]+B
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
best = M
for i in range(N+1):
    if A[i] > K:
        break
    for j in range(best, -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 = [0]+list(map(int,input().split()))
b = [0]+list(map(int,input().split()))
s = [0]*(n+1)
t = [0]*(m+1)
x = 0
for i in range(n+1):
    s[i] += x + a[i]
    x = s[i]
x = 0
for i in range(m+1):
    t[i] += x + b[i]
    x = t[i]
#print(s,t)
x = 1
while(1):
#for i in range(30):
    f = 0
    #print(x)
    for j in range(x-min(x,m),min(x,n)+1):
        #print(j,x-j)
        if(s[j]+t[x-j] <= k):
            #print(j,x-j)
                f = 1
                break
    if(f == 0):
        break
    x += 1
print(x-1)

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()))
s = [0]*(n+1)
t = [0]*(m+1)
p = m-1
q = n-1
x = 0
for i in range(n+1):
    s[i] += x + a[i]
    x = s[i]
x = 0
for i in range(m+1):
    t[i] += x + b[i]
    x = t[i]
#print(s,t)
x = 1
while(1):
#for i in range(30):
    f = 0
    #print(x)
    for j in range(x+1):
        if(m>=x-j):
            if(n >= j):
                if(s[j]+t[x-j] <= k):
                    #print(j,x-j)
                    f = 1
                    break
            else:
                break
    if(f == 0):
        break
    x += 1
print(x-1)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

AA = np.insert(A,0,0)
BB = np.insert(B,0,0)
AAA = np.cumsum(AA)
BBB = np.cumsum(BB)
max_mins = 0
max_books = 0
for a in enumerate(AAA):
    if a[1] > K:
        break
    for b in enumerate(BBB):
        if a[1] + b[1] > K:
            break
        if a[0] + b[0] > max_books:
            max_books = a[0] + b[0]
print(max_books)

Submission status: Time Limit Exceeded

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

N, M, K = map(int, readline().split())
A = list(map(int, readline().split()))
B = list(map(int, readline().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])

max = 0
for i in range(N+1):
  t = a[i]
  if t > K:
    break
  for j in range(M+1):
    if t + b[j] > K:
      break
    if i + j > max:
      max = i + j
print(max)

Submission status: Time Limit Exceeded

Solution code:
n,m,k = map(int,input().split())
ls_a = list(map(int,input().split()))
ls_b = list(map(int,input().split()))
 
n_max = n
for i in range(n):
  if sum(ls_a[:i+1]) > k:
    n_max = i
    break
    
readable_max = 0
pre_max = 0
for i in range(n_max, -1, -1):
  m_time = k - sum(ls_a[:i])
  
  m_max = m
  for j in range(pre_max, m+1):
    
    if sum(ls_b[:j+1]) > m_time:
      m_max = j
      pre_max = j
      break
    elif j == m:
  	  break
  else:
    continue
  break
    
  if i + m_max > readable_max:
    readable_max = i + m_max
    
if i + m_max > readable_max:
    readable_max = i + m_max
    
print(readable_max)

Submission status: Time Limit Exceeded

Solution code:
n,m,k = map(int,input().split())
ls_a = list(map(int,input().split()))
ls_b = list(map(int,input().split()))
 
n_max = n
for i in range(n):
  if sum(ls_a[:i+1]) > k:
    n_max = i
    break
    
readable_max = 0
pre_max = 0
for i in range(n_max, -1, -1):
  m_time = k - sum(ls_a[:i])
  
  m_max = m
  for j in range(pre_max, m+1):
    if sum(ls_b[:j+1]) > m_time:
      m_max = j
      pre_max = j
      break
  if i + m_max > readable_max:
    readable_max = i + m_max
    
print(readable_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()))
#n,m=200000,200000
#k=1000000000
#a=[1]*10000000
#b=[1]*10000000

stack=[[0,0,0]]
mx=0

while stack:
  x,y,k1=stack.pop()
  if x>n or y>m:
    break
  if x<n and k1+a[x] <=k:
    stack.append([x+1,y,k1+a[x]])
    if x+1+y> mx:
      mx=x+1+y
  if y<m and k1+b[y] <=k:
    stack.append([x,y+1,k1+b[y]])
    if x+1+y>mx:
      mx=x+1+y
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()))
#n,m=200000,200000
#k=1000000000
#a=[1]*10000000
#b=[1]*10000000

stack=[[0,0,0]]
mx=0

while stack:
  x,y,k1=stack.pop()
  
  if x<n and k1+a[x] <=k:
    stack.append([x+1,y,k1+a[x]])
    if x+1+y> mx:
      mx=x+1+y
  if y<m and k1+b[y] <=k:
    stack.append([x,y+1,k1+b[y]])
    if x+1+y>mx:
      mx=x+1+y
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()))

n = N
while True:
    if sum(A[:n]) <= K:
        break
    n -= 1

m = M
cost = []
for n in range(n + 1):
    #print(n, m)
    if sum(A[:n]) > K:
        break
    while True:
        #print(n, m)
        if sum(A[:n]) + sum(B[:m]) <= K:
            cost.append(n + m)
            break
        m -= 1
# print(cost)
print(max(cost))

Submission status: Time Limit Exceeded

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

m = M
cost = []
for n in range(N + 1):
    #print(n, m)
    if sum(A[:n]) > K:
        break
    while True:
        #print(n, m)
        if sum(A[:n]) + sum(B[:m]) <= K:
            cost.append(n + m)
            break
        m -= 1
# print(cost)
print(max(cost))

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()))
stack=[[0,0]]
vi=[[0]*(m+1) for _ in range(n+1)]
vi[0][0]=0
mx=0

while stack:
  x,y=stack.pop()
  
  if x<n and vi[x][y]+a[x] <=k:
    vi[x+1][y]=vi[x][y]+a[x]
    stack.append([x+1,y])
    if x+1+y> mx:
      mx=x+1+y
  if y<m and vi[x][y]+b[y] <=k:
    vi[x][y+1]=vi[x][y]+b[y]
    stack.append([x,y+1])
    if x+1+y>mx:
      mx=x+1+y
print(mx)

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

print(max)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque


def _tsundoku(n, m, k, a, b):
    # print(f'{a=}')
    # print(f'{b=}')
    a_len = len(a)
    b_len = len(b)

    ans = [0, 0, 0]

    L = []
    d = deque()
    d.append((0, 0, 0))
    while d:
        # print(d, L)
        a_readed, b_readed, t = d.popleft()
        if not (a_len > a_readed and t + a[a_readed] <= k) and not (b_len > b_readed and t + b[b_readed] <= k):
            # print(a_readed, b_readed, t)
            L.append(a_readed + b_readed)
        else:
            if a_len > a_readed and t + a[a_readed] <= k:
                d.append((a_readed + 1, b_readed, t + a[a_readed]))
            if b_len > b_readed and t + b[b_readed] <= k:
                d.append((a_readed, b_readed + 1, t + b[b_readed]))
    ans = max(L)
    return ans

def tsundoku(f):
    readline = f.readline
    n, m, k = map(int, readline().split())
    a = list(map(int, readline().split()))
    b = list(map(int, readline().split()))

    return _tsundoku(n, m, k, a, b)

def main():
    ans = tsundoku(sys.stdin.buffer)
    print(ans)

if __name__ == '__main__':
    main()