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:
from sys import stdin
import sys

l = stdin.readline().rstrip().split(' ')
a = stdin.readline().rstrip().split(' ')
b = stdin.readline().rstrip().split(' ')
k = int(l[2])
n = int(l[0])
m = int(l[1])
ia = 0
ib = 0
b_sum = 0
count = 0
a_sum = 0
res = []

for ib in range(m):
    b_sum += int(b[ib])
    if b_sum <= k:
        count += 1
    else:
        break
res.append(count)
count = 0
for ia in range(n):
    a_sum = a_sum + int(a[ia])
    if k < a_sum:
        #print("ok")
        res.append(count)
        continue
    else:
        count = ia + 1
    b_sum = a_sum
    for ib in range(m):
        b_sum += int(b[ib])
        if b_sum <= k:
            count += 1
        else:
            break
    res.append(count)
print(max(res))

Submission status: Time Limit Exceeded

Solution code:
import sys
import itertools

N, M, K = map(int, input().split())
A = list(map(int, sys.stdin.readline().rsplit()))
B = list(map(int, sys.stdin.readline().rsplit()))

sA = [0] + list(itertools.accumulate(A))
sB = [0] + list(itertools.accumulate(B))

res = 0
for i in range(N + 1):
    for j in range(M + 1):
        if sA[i] + sB[j] <= K:
            res = max(res, i + j)
        else:
            break

print(res)

Submission status: Time Limit Exceeded

Solution code:
import sys, os, math, bisect, itertools, collections, heapq, queue
# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from decimal import Decimal
from collections import defaultdict, deque

# import fractions

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)
# lcm = lambda x, y: (x * y) // fractions.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N, M, K = il()
    A = il()
    SA = [0] * (N + 1)
    for n in range(N):
        SA[n + 1] = A[n] + SA[n]
    B = il()
    SB = [0] * (M + 1)
    for m in range(M):
        SB[m + 1] = B[m] + SB[m]
        
    ret = 0
    for n in range(N+1):
        for m in range(M+1):
            if SA[n] + SB[m] <= K:
                ret = max(ret, n + m)
    print(ret)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(2000000)


def search(ai, bi, ti, ansi):
    global ans
    if ai < n and ti + a[ai] <= k:
        search(ai+1, bi, ti+a[ai], ansi+1)

    if bi < m and ti + b[bi] <= k:
        search(ai, bi+1, ti+b[bi], ansi+1)

    ans = max(ans, ansi)


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

ans = 0
search(0, 0, 0, 0)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys


sys.setrecursionlimit(10**9)


def readBook(i, j, time, cnt):
    if time < 0:
        return cnt - 1
    if i == N and j == M:
        return cnt
    elif i == N:
        res = readBook(i, j + 1, time - B[j], cnt + 1)
    elif j == M:
        res = readBook(i + 1, j, time - A[i], cnt + 1)
    else:
        res = max(readBook(i + 1, j, time - A[i], cnt + 1), readBook(i, j + 1, time - B[j], cnt + 1))
    return res


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

print(readBook(0, 0, K, 0))

Submission status: Time Limit Exceeded

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

import sys #追加
sys.setrecursionlimit(500*500) #追加

ans = [0]

def dfs(now_sum,i,j,cnt):
    # print(now_sum)
    # if ans[0] <= cnt:
    #     return
    if now_sum > K:
        # print("a")
        cnt -= 1
        ans[0] = max(ans[0],cnt)
        return
    
    # if i == N or j== M:
        # print("b")
        # print(now_sum,i,j)
    # ans[0] = max(ans[0],cnt)
        # return

    if i < N:
        dfs(now_sum + A[i],i+1,j,cnt+1)
    if j < M:
        dfs(now_sum + B[j],i,j+1,cnt+1)
    ans[0] = max(ans[0],cnt)
    


dfs(0,0,0,0)

print(ans[0])

Submission status: Time Limit Exceeded

Solution code:
N, M, K=map(int,input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A.insert(0, 0)
B.insert(0, 0)
A_sum = [sum(A[0:e+1]) for e in range(len(A)) ]
B_sum = [sum(B[0:e+1]) for e in range(len(B)) ]
num=0
for i in range(len(A)):
    for l in range(len(B)):
        if num<i+l:
            Time=A_sum[i]+B_sum[l]
            if Time<=K:
                num=i+l
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()))
for i in range(m):
    a.append(0)
#print(a)
for i in range(n):
    b.append(0)
#print(b)
c=0
d=0
while c<=k:
    for i in range(len(b)):
        #print(i)
        if a[i]<=b[i] and a[i]<k and a[i] !=0:
            c=c+(a[i])
            d=d+1
            a.remove(a[i])
            a.insert(0,0)
            b.insert(0,0)
            b.remove(b[len(b)-1])
        elif a[i]>b[i] and b[i]<k and b[i] !=0:
            c=c+(b[i])
            d=d+1
            b.remove(b[i])
            b.insert(0,0)
            a.insert(0,0)
            a.remove(a[len(a)-1])
if d!=0:
    d=d-1
print(d)

Submission status: Time Limit Exceeded

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

def main():
    n,m,k = map(int,input().split())
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))
    a_cum = numpy.cumsum(a).tolist()
    b_cum = numpy.cumsum(b).tolist()
    a_cum.insert(0,0)
    b_cum.insert(0,0)
    ans_i = 0
    for i,c in enumerate(a_cum):
        for j,d in enumerate(b_cum):
            if c + d <= k and ans_i <= c + d:
                ans = i + j
                ans_i = c + d
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:


def main3():
    n, m, k = map(int, input().split())
    aa = list(map(int, input().split()))
    bb = list(map(int, input().split()))
    time_sum = 0
    counter = 0
    ans = []

    if bb[0] >= aa[0]:
        if k < aa[0]:
            return counter
    if bb[0] <= aa[0]:
        if k < bb[0]:
            return counter

    if (sum(aa)+sum(bb)) <= k:
        return n+m

    for i in range(n+1):
        counter = i
        time_sum = sum(aa[:i])
        if time_sum > k:
            break
        elif time_sum == k:
            ans.append(counter)
        else:
            for j in range(m):
                time_sum += bb[j]
                if time_sum > k and j != 0:
                    ans.append(counter)
                    break
                if time_sum > k and j == 0:
                    break
                elif time_sum == k:
                    counter += 1
                    ans.append(counter)
                    break
                else:
                    counter += 1

    ans.sort(reverse=True)
    return ans[0]


if __name__ == '__main__':
    ans = main3()
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math

def solve(n,m,k,a_lst,b_lst):
    #机ごと、読書時間の累積をリストにする。ただし、リスト化はk時間内で読める範囲とする
    aa_lst = make_sum_list(a_lst,k)
    bb_lst = make_sum_list(b_lst,k)

    #A,Bいずれかが一冊も読めないケースを考慮
    if len(aa_lst) == 0:
        if len(bb_lst) == 0: return 0
        else: return len(bb_lst)
    elif len(bb_lst) == 0: return len(aa_lst)
    
    #全て読めるケースを考慮
    if aa_lst[-1] + bb_lst[-1] <= k: return len(aa_lst) + len(bb_lst) 
    
    #A,Bいずれかのみ読むケースを考慮
    max_read = max(len(aa_lst),len(bb_lst))

    #リスト範囲でA+Bの最大値を探索する
    for i in range(len(aa_lst)):
        time = aa_lst[i]
        read = i + 1
        for j in range(len(bb_lst)):
            if time + bb_lst[j] > k:
                read += j
                break
		
        max_read = max(read,max_read)

    return max_read

def make_sum_list(lst,max):
    sum_lst = []
    sum = 0
    for n in lst:
        sum += n
        if sum > max:
            break
        sum_lst.append(sum)
    return sum_lst

n,m,k = map(int, input().split())
a_lst = list(map(int, input().split()))
b_lst = list(map(int, input().split()))
ans = solve(n,m,k,a_lst,b_lst)
print(ans)

Submission status: Time Limit Exceeded

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

max_num = 0

for a in range(N+1):
    for b in range(M+1):
        if sum(A[:a])+sum(B[:b]) <= K:
            if max_num < a+b:
                max_num = a+b
        else:
            break

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

flag=True
ans=0



while True:
    for i in range(ans+1):
        if ans-i<=M and i <=N: 
            if sum(A[s] for s in range(i))+sum(B[t] for t in range(ans-i))<=K:
                flag=False
                break
    if flag:
        break
    if not(flag):
        ans+=1
        flag=True
    

    
print(ans-1)

Submission status: Time Limit Exceeded

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

dp=[[0 for j in range(K+1)] for i in range(max(N,M)+1)]
if(N<M):
    ma=M
    mi=N
    turn='M'
else:
    ma=N
    mi=M
    turn='N'

if(turn=='M'):
    for i in range(ma):
        for j in range(K+1):
            if(mi<=i):
                if(B[i]<j):
                    dp[i+1][j]=max(dp[i][j-B[i]]+1,dp[i][j])
                else:
                    dp[i+1][j]=dp[i][j]
            else:

                if(A[i]<j and B[i]<j):

                    dp[i+1][j]=max(dp[i][j-A[i]-B[i]]+2,dp[i][j])

                elif(A[i]<j):
                    dp[i+1][j]=max(dp[i][j-A[i]]+1,dp[i][j])
                elif(B[i]<j):
                    dp[i+1][j]=max(dp[i][j-B[i]]+1,dp[i][j])
                else:
                    dp[i+1][j]=dp[i][j]
else:
    for i in range(ma):
        for j in range(K+1):
            if(mi<=i):
                if(A[i]<j):
                    dp[i+1][j]=max(dp[i][j-A[i]]+1,dp[i][j])
                else:
                    dp[i+1][j]=dp[i][j]
            else:
                if(A[i]<j and B[i]<j):
                    dp[i+1][j]=max(dp[i][j-A[i]-B[i]]+2,dp[i][j])

                elif(A[i]<j):
                    dp[i+1][j]=max(dp[i][j-A[i]]+1,dp[i][j])
                elif(B[i]<j):
                    dp[i+1][j]=max(dp[i][j-B[i]]+1,dp[i][j])
                else:
                    dp[i+1][j]=dp[i][j]

print(dp[ma][K])

Submission status: Time Limit Exceeded

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

na = 0
nb = 0
t = 0
b = 0
hq = []
heapq.heappush(hq,(t, na, nb))

while hq:
  t, na, nb = heapq.heappop(hq)
  if (na < N) and (t + A[na] <= K):
    heapq.heappush(hq,(t + A[na], na + 1, nb))
    b = max(b,na + nb + 1)
  if (nb < M) and (t + B[nb] <= K):
    heapq.heappush(hq,(t + B[nb], na, nb + 1))
    b = max(b,na + nb + 1)
#
print(b)

Submission status: Time Limit Exceeded

Solution code:
#!python3

import sys
iim = lambda: map(int, sys.stdin.readline().rstrip().split())
from bisect import bisect


def resolve():
    it = map(int, sys.stdin.read().split())
    N, M, K = next(it), next(it), next(it)

    A = [0] * (N+1)
    B = [0] * (M+1)
    ans = 0
    for i in range(1, N+1):
        A[i] = A[i-1] + next(it)
    for i in range(1, M+1):
        B[i] = B[i-1] + next(it)

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

    print(ans)


if __name__ == "__main__":
    resolve()

Submission status: Time Limit Exceeded

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

# readline = sys.stdin.readline
# generator = (readline().strip() for _ in range(N))

# N, M = map(int, input().split())
# As = list(map(int, input().split()))
# queries = (input() for _ in range(N))


def dfs(n, m, total, count, N, M, K, As, Bs):
    if total > K:
        return count - 1

    if n == N and m == M:
        return count

    if n < N:
        c1 = dfs(n + 1, m, total + As[n], count + 1, N, M, K, As, Bs)
    else:
        c1 = count

    if m < M:
        c2 = dfs(n, m + 1, total + Bs[m], count + 1, N, M, K, As, Bs)
    else:
        c2 = count

    return max(c1, c2)


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

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

    return dfs(0, 0, 0, 0, N, M, K, As, Bs)


def main():
    print(solve())


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


import bisect

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

ans = 0
for i in range(n + 1):
    t = sum(A[:i])
    if t <= k:
        bnum = i
    else:
        bnum = 0
    if t <= k:
        idx = bisect.bisect_right(cum, k - t)
        bnum += idx - 1
    ans = max(ans, bnum)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

a_sum = np.cumsum(np.array(a))
b_sum = np.cumsum(np.array(b))

for i in range(n+1):
    for j in range(m+1):
        if a_sum[i] + b_sum[j] <= k:
            if num < i + j:
                num = i+j    
            continue
        else:
            break

print(num)

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()]
import sys
sys.setrecursionlimit(10**6)
maxc = 0
a_i=0
b_i=0
def dfs(a_i,b_i,t,cnt):
    global maxc
    
    if(a_i==n and b_i==m):
        maxc = m+n
        return
    if(k < t):
        maxc = max(maxc,cnt-1)
        return 
    if(a_i < n):
        dfs(a_i+1,b_i,A[a_i]+t,cnt+1)

    if(b_i < m):
        dfs(a_i,b_i+1,B[b_i]+t,cnt+1)



        
dfs(0,0,0,0)
print(maxc)

Submission status: Time Limit Exceeded

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

rest = K
ans = 0

def dfs(cost, aindex, bindex, score):
    global ans

    if K == cost:
        # print("1:", score, cost)
        # return score
        ans = max(score, ans)
        return

    elif K < cost:
        # print("2:", score, cost)
        # return score - 1
        ans = max(score - 1, ans)
        return 
    
    if aindex == N and bindex == M:
        # print("3:", score, cost)
        # return score
        ans = max(score, ans)

    elif aindex >= N:
        dfs(cost + B[bindex], aindex, bindex+1, score + 1)
    
    elif bindex >= M:
        dfs(cost + A[aindex], aindex+1, bindex, score + 1)

    else:
        dfs(cost + A[aindex], aindex+1, bindex, score + 1)
        dfs(cost + B[bindex], aindex, bindex + 1, score + 1)

dfs(A[0], 1, 0, 1)
dfs(B[0], 0, 1, 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()))
cnt=0
cntmax=0

for i in range(N+1):
  sumA=sum(A[0:i])
  if sumA>K:
    break
  for j in range(M+1):
    sumB=sum(B[0:j])
    if sumA+sumB<=K:
      cnt=i+j
      if cntmax<=cnt:
        cntmax=cnt
        #print(i,j,sumA,sumB,cntmax)
    else:
      break
print(cntmax)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

Acum = np.cumsum(np.array(A))
Bcum = np.cumsum(np.array(B))

rmax = 0
for n in range(N+1):
    if n==0:
        Know = K
    else:
        Know = K-Acum[n-1]
    if Know<0:
        break

    tmp = (Bcum>Know).nonzero()[0]
    if len(tmp)>0:
        m = tmp[0]
    else:
        m = M
    
    if n+m>rmax:
        rmax=n+m

print(rmax)

Submission status: Time Limit Exceeded

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

ar_time = 0
a_count = 0
for i in a:
  ar_time += i
  if k < ar_time:
    ar_time -= i
    break
  else:
    a_count +=1
br_time = 0
b_count = 0
for i in b:
  br_time += i
  if k < br_time:
    br_time -= i
    break
  else:
    b_count +=1
max_count = a_count
for i in range(a_count+1):
  count = i
  r_time = sum(a[0:i])
  for j in range(b_count):
    r_time += b[j]
    if k < r_time:
      break
    else:
      count += 1
  if max_count < count:
    max_count = count
print(max_count)

Submission status: Time Limit Exceeded

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

import itertools
l = [0]*n + [1]*m
pattern = [0]
for v in set(itertools.permutations(l,n+m)):
  tmp_k = k
  tmp_a = ais[:]
  tmp_b = bis[:]
  total = 0
  for i in v:
    if i == 0:
      time = tmp_a.pop(0)
    else:
      time = tmp_b.pop(0)
    tmp_k-=time
    if tmp_k < 0:
      break
    total+=1
  pattern.append(total)
  if total == n+m:
    break
  
pattern.sort(reverse=True)
print(pattern[0])

Submission status: Time Limit Exceeded

Solution code:
from sys import *
ip = stdin.readline
N,M,K = map(int,ip().split())
A = list(map(int,ip().split()))
B = list(map(int,ip().split()))
li = []
ans,tmp = 0,0
while 1:
    if A and B:
        if A[0] < B[0]:
            li.append(A.pop(0))
        else:
            li.append(B.pop(0))
    elif A:
        while A:
            li.append(A.pop(0))
    elif B:
        while B:
            li.append(B.pop(0))
 
    else:
        break
for i in li:
    if i+tmp <= K:
        tmp += i
        ans += 1
print(ans)

Submission status: Time Limit Exceeded

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

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

max = 0
temp = []

def dfs(i,j, time, count):
    global max
    global temp
    
    if [i,j] in temp:
        return 
    if time > k:
        return 
    else:
        if count >= max:
            max = count
    if i >= n and j >= m:
        return 
    elif i >= n:
        temp.append([i, j])
        dfs(i, j+1, time+b[j], count+1)
    elif j >= m:
        temp.append([i,j])
        dfs(i+1, j, time+a[i], count+1)
    else:
        temp.append([i,j])
        dfs(i, j+1, time+b[j], count+1)
        dfs(i+1, j, time+a[i], count+1)

dfs(0,0,0,0)
print(max)

Submission status: Time Limit Exceeded

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


ans = 0
sum_a = 0
for i in range(len(a)):
  sum_a += a[i]
  sum_b = 0
  if sum_a > k:
    break
  if sum_a == k:
    temp = i+1
    if temp > ans:
      ans = temp
    break
  for j in range(len(b)):
    sum_b += b[j]
    if (sum_a + sum_b) > k:
      temp = i+1+j
      if temp > ans:
        ans = temp
      break
    if (sum_a + sum_b) == k:
      temp = i+1+j+1
      if temp > ans:
        ans = temp
      break
      
sum_b = 0
for i in range(len(b)):
  sum_b += b[i]
  sum_a = 0
  if sum_b > k:
    break
  if sum_b == k:
    temp = i+1
    if temp > ans:
      ans = temp
    break
  for j in range(len(a)):
    sum_a += a[j]
    if (sum_b + sum_a) > k:
      temp = i+1+j
      if temp > ans:
        ans = temp
      break
    if (sum_b + sum_a) == k:
      temp = i+1+j+1
      if temp > ans:
        ans = temp
      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()))

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

if csA[-1] + csB[-1] <= K:
    print(N+M)

else:
    
    ans = 0
               
    for i in range(N+1):
        wa = csA[i]
        numA = i
        if wa <= K:
            for j in range(M+1):
                numB = j
                wa += csB[j]
                if wa > K:
                    if numA + numB - 1 > ans:
                        ans = numA + numB - 1
                    break
                else:
                    wa = csA[i]
        else:
            break

    print(ans)

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()))
lastime = 0
ans_list = []
a.insert(0,0)
for i in range(n+1):
  time = lastime + a[i]
  lastime = lastime + a[i]
  book = i
  if time <= k:
    ans_list.append(book)
  else:
    break
  for j in range(m):
    time = time + b[j]
    book = book + 1
    if time <= k:
      ans_list.append(book)
    else:
      break
  time = lastime
if len(ans_list) == 0:
  ans = n + m
else:
  ans = max(ans_list)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
import sys
import re
"""
from collections import deque, defaultdict
from itertools import combinations, combinations_with_replacement
import bisect
from math import factorial, sqrt, log, ceil, floor
sys.setrecursionlimit(1000000)
"""

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

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

    B_sums = []
    B = 0
    for b in Bs:
        B += b
        B_sums.append(B)

    max_num = 0
    A = None
    B_pos = len(B_sums) - 1
    for i in range(len(As)+1):
        if i == 0:
            A = 0
        else:
            A += As.pop(0)
        k = A
        if k > K:
            break
        num = i

        remain = K - k
        
        while B_pos >= 0:
            if remain >= B_sums[B_pos]:
                num += B_pos + 1
                break
            B_pos -= 1

        max_num = max([max_num, num])

    print(max_num)


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

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[47]:


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


# In[48]:


ans = 0
for a in range(N+1):
    a_sum = sum(A[:a])
    if a_sum > K:
        break
    for b in range(M+1):
        b_sum = sum(B[:b])
        if a_sum + b_sum > K:
            break
        ans = max(ans, a+b)
print(ans)


# In[ ]:

Submission status: Time Limit Exceeded

Solution code:
nmk = input('')
n = int(nmk.split(' ')[0])
m = int(nmk.split(' ')[1])
k = int(nmk.split(' ')[2])

alist = list(map(int,input().split()))
blist = list(map(int,input().split()))

l = (n+m)
t =k

max_read=0

for i in range(2**l):
    bag = ['A']*l
    bcount=0
    for j in range(l):
        if ((i>>j)&1):
            bag[l-1-j]='B'
            bcount+=1
            if bcount == m:
                break
    if bag.count('A')==n:
        time=0
        read=0
        a=0
        b=0
        for item in bag:
            t=0
            if item=='A':
                t = alist[a]
                a+=1
            else:
                t = blist[b]
                b+=1
            time+=t
            if k>=time:
                read+=1
            else:
                break
        if max_read < read:
            max_read = read
#        print("{} {}/{} {}".format(i,time,k,bag))

print(max_read)

Submission status: Time Limit Exceeded

Solution code:
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
aa = a.copy()
bb = b.copy()
sa = sum(a)
sb = sum(b)
c = [10**10 for i in range(n+m+1)]
c[n+m] = sa + sb
x = sa + sb
c[0] = 0
for i in range(1,n+m):
  if len(a) == 0:
    x -= b[-1]
    b.pop(-1)
    c[n+m-i] = x
    continue
  if len(b) == 0:
    x -= a[-1]
    a.pop(-1)
    c[n+m-i] = x
    continue
  if a[-1] > b[-1]:
    x -= a[-1]
    a.pop(-1)
  else:
    x -= b[-1]
    b.pop(-1)
  c[n+m-i] = x

s = 0
d = [0 for i in range(n+m+1)]
for i in range(n+m+1):
  if i == 0:
    continue
  if len(aa) == 0:
    s += bb[0]
    bb.pop(0)
    d[i] = s
    continue
  if len(bb) == 0:
    x += aa[0]
    aa.pop(0)
    d[i] = s
    continue
  if aa[0] > bb[0]:
    s += bb[0]
    bb.pop(0)
  else:
    s += aa[0]
    aa.pop(0)
  d[i] = s
  e = min(c[i],d[i])
  if e > k:
    print (i-1)
    exit()
if e <= k:
  print (n+m)
else:
  print (0)

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()]
import sys
sys.setrecursionlimit(10**6)
maxc = 0
a_i=0
b_i=0
def dfs(a_i,b_i,t,cnt):
    global maxc
    
    if(a_i==n and b_i==m):
        maxc = m+n
        return
    
    if(a_i < n and (A[a_i]+t)<=k):
        dfs(a_i+1,b_i,A[a_i]+t,cnt+1)
    else:
        maxc = max(cnt,maxc)
    if(b_i < m and (B[b_i]+t)<=k):
        dfs(a_i,b_i+1,B[b_i]+t,cnt+1)
    else:
        maxc = max(cnt,maxc)


        
dfs(0,0,0,0)
print(maxc)

Submission status: Time Limit Exceeded

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

import itertools
l = [0]*n + [1]*m
pattern = [0]
for v in set(itertools.permutations(l,n+m)):
  tmp_k = k
  tmp_a = ais[:]
  tmp_b = bis[:]
  total = 0
  for i in v:
    if i == 0:
      time = tmp_a.pop(0)
    else:
      time = tmp_b.pop(0)
    tmp_k-=time
    if tmp_k < 0:
      break
    total+=1
  pattern.append(total)
  
pattern.sort(reverse=True)
print(pattern[0])

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N, M, K =map(int, input().split())
A = list(map(int, input().split()))
A.insert(0,0)
B = list(map(int, input().split()))  
B.insert(0,0)
len_A = len(A)
len_B = len(B)
dp = [[10**10]*len_B for _ in range(len_A)]
ans = 0
for i in range(len_A):
    for j in range(len_B):
        if i == 0 and j == 0:
            dp[i][j] = 0
        elif i == 0:
            dp[i][j] = dp[i][j-1] + B[j]
            if dp[i][j] <= K and ans < i+j :
                ans = i+j
            if dp[i][j] >= K:
                break
        else:
            dp[i][j] = dp[i-1][j] + A[i]
            if dp[i][j] <= K and ans < i+j :
                ans = i+j
            if dp[i][j] >= K:
                break

print(ans)

Submission status: Time Limit Exceeded

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

a_ruiseki=[0]
b_ruiseki=[0]
ans=0

for vamvaawmvaow in a_ls:
	a_ruiseki+=[a_ruiseki[-1]+vamvaawmvaow]

for vamvaawmvaow in b_ls:
	b_ruiseki+=[b_ruiseki[-1]+vamvaawmvaow]


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


print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools
import bisect

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

aa = [0] + list(itertools.accumulate(A))
bb = [0] + list(itertools.accumulate(B))

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

    if i + M <= ans:
        continue

    if ans != 0 and bb[(ans-i+1)] >= tmp_K:
        continue

    if tmp_K >= bb[-1]:
        ans = max(ans, i+M)
        continue

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

        mid = (mn+mx//2)
        if bb[mid] <= tmp_K:
            if mn == mid:
                mn = mid+1
            else:
                mn = mid

        else:
            if mx == mid:
                mx = mid-1
            else:
                mx = mid
            if i+mx <= ans:
                break
print(ans)