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:
#!/usr/bin/env python
# coding: utf-8

# In[44]:


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


# In[46]:


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:
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
max_count = a_count
for i in range(a_count+1):
  count = i
  r_time = sum(a[0:i])
  for j in b:
    r_time += 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:
from itertools import accumulate


def main():
    N, M, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    accumulateA = list(accumulate(A))
    accumulateB = list(accumulate(B))
    accumulateA.insert(0, 0)
    accumulateB.insert(0, 0)
    count = 0
    for i in range(N+1):
        for j in range(M+1):
            if accumulateA[i] + accumulateB[j] <= K:
                count = max(count, i + j)
    print(count)


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

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

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
    for j in range(l):
        if ((i>>j)&1):
            bag[l-1-j]='B'
        if bag.count('A') == n:
            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:
from numba import jit
import sys

@jit(cache=True)
def aaa(al,bl):
    p=0
    for i in range(len(al)):
        for l in range(len(bl)):
            if al[-i-1]+bl[-l-1]<=k:
                q=len(al)+len(bl)-i-l-2
                if p<q:
                    p=q
                break
        if al[-i-1]+bl[-1]<k:
            break
    return p

x=input()
x0=x.split()
n=int(x0[0])
m=int(x0[1])
k=int(x0[2])
a=input()
b=input()
a0=[int(i) for i in a.split()]
b0=[int(i) for i in b.split()]
a=0
b=0
al=list([0])
bl=list([0])
for i in range(n):
    a=a+a0[i]
    if a>k:
        break
    al.append(a)
for i in range(m):
    b=b+b0[i]
    if b>k:
        break
    bl.append(b)

z=aaa(al,bl)
print(z)

Submission status: Time Limit Exceeded

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

n,m,k = map(int,input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a.append(1)
b.append(1)
ans = 0

def dfs(time,count,i,j):
    global ans,n,m,k,a,b
    if i <= n-1 and time + a[i] <= k:
            dfs(time + a[i],count + 1,i+1,j)
    if j <= m-1 and time + b[j] <= k:
           dfs(time + b[j],count + 1,i,j+1)  
    if (i == n or time + a[i] > k) and (j == m or time + b[j] > k):
        ans = max(ans, count)

shoki_time = 0
shoki_count = 0
shoki_i = 0
shoki_j = 0
dfs(shoki_time,shoki_count,shoki_i,shoki_j)
print(ans)

Submission status: Time Limit Exceeded

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

newlista=[sum(A[i] for i in range(j)) for j in range(len(A)+1)]
newlistb=[sum(B[i] for i in range(j)) for j in range(len(B)+1)]

comb=[[newlista[i]+newlistb[j] for i in range(N+1)]for j in range(M+1)]

flag=True
ans=0



while True:
    for i in range(ans+1):
        if ans-i<=M and i <=N: 
            if comb[ans-i][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:
import sys
sys.setrecursionlimit(10**1000000000)

# 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 solve():
    N, M, K = map(int, input().split())

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

    def dfs(n, m, total, count):
        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)
        else:
            c1 = count

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

        return max(c1, c2)

    return dfs(0, 0, 0, 0)


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()))
A.insert(0, 0)
B.insert(0, 0)
num=0
for i in range(len(A)):
    for l in range(len(B)):
        if num<i+l:
            Time=sum(A[0:i+1])+sum(B[0:l+1])
            if Time<=K:
                num=i+l
print(num)

Submission status: Time Limit Exceeded

Solution code:
# coding=UTF-8
from collections import deque
from collections import Counter
from operator import itemgetter
from bisect import bisect_left, bisect
from decimal import *
import itertools
import sys
import math
import numpy as np
import time
sys.setrecursionlimit(10**6)



def main():
    n,m,k=map(int,input().split())
    a=[int(x) for x in input().split()]
    b=[int(x) for x in input().split()]
    ans=[0]
    for i in range(len(a)+1):
        cnt = 0
        time = 0
        if i == 0:
            a_sum=0
        else:
            a_sum = sum(a[:i])
        cnt += i
        time += a_sum
        if time > k:
            break
        for b_v in b:
            if time + b_v > k:
                break
            else:
                cnt+=1
                time += b_v
        ans.append(cnt)
    print(max(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()))

a = 0
b = 0

ans = 0

dp_a = {}
dp_b = {}

s = 0
for i, a in enumerate(A):
    s += a
    if s > K:
        break
    dp_a[s] = i+1

s = 0
for i, b in enumerate(B):
    s += b
    if s > K:
        break
    dp_b[s] = i+1

new = {}
for b, s in dp_b.items():
    flag = False
    for k, v in sorted(dp_a.items(), reverse=True, key=lambda x : x[1]):
        if k+b > K:
            continue
        if flag:
            break
        if k+b in new:
            new[k+b] = min(new[k+b], v + s)
        else:
            new[k+b] = v +s
        flag = True

ans = 0

for k in range(K, -1, -1):
    if k in new:
        ans = max(ans, new[k])
        break
    if k in dp_b:
        ans = max(ans, dp_b[k])
        break
    if k in dp_a:
        ans = max(ans, dp_a[k])
        break

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

csA = np.cumsum(A)
csB = np.cumsum(B)

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:
from collections import deque
from array import array
N, M, K = map(int, input().split())

A = array('i', (int(s) for s in input().split()))
B = array('i', (int(s) for s in input().split()))

init = (K, 0, 0)
res = 0
q = deque()
q.append(init)
while q:
    deadline, ai, bi = q.popleft()
    res = max(res, ai+bi)
    if  ai < N and deadline - A[ai] >= 0:
        q.append((deadline - A[ai], ai+1, bi))
    if bi < M and deadline - B[bi] >= 0:
        q.append((deadline - B[bi], ai, bi+1))
    
print(res)

Submission status: Time Limit Exceeded

Solution code:
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
ans=0
for i in range(n):
  if sum(a[:i+1])>k:
    break
  elif sum(a[:i+1])+sum(b)<=k:
      ans=max(ans,i+m+1)
  else:  
    for j in range(m):
      if sum(a[:i+1])+sum(b[:j+1])<=k:
        continue
      else:
        ans=max(ans,i+j+1)
        break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, m, k = map(int, input().split())
a = list(map(int, input().split())) # list()でリスト化、それを変数に代入。
b = list(map(int, input().split())) # list()でリスト化、それを変数に代入。
ans = 0
kazu = 0
c_a = 0
c_b = 0
time = 0
'''
a.append(10**9 + 1)
b.append(10**9 + 1)


while True:
    if time + min(a[c_a],b[c_b])>k:
        break
    if a[c_a]>b[c_b]:
        time += b[c_b]
        c_b += 1
    elif a[c_a]<b[c_b]:
        time += a[c_a]
        c_a += 1

    ans += 1
'''
a = [0]+a
b = [0]+b

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

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

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

ans = 0

b_last = len(B) + 1


for i in range(len(A) + 1):
    a_sum = sum(A[:i])
    if a_sum > K:
        break
    while True:
        b_sum = sum(B[:b_last])
        if b_sum + a_sum < K:
            tmp = i + b_last
            if ans < tmp:
                ans = tmp
            break
        b_last -= 1

print(ans)

Submission status: Time Limit Exceeded

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

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

ans = 0


def shouka(s: str, pa, pb, total):
    global ans
    if s == "a":
        total += a[pa]
        if total > k:
            ans = max(ans, pa + pb)
            return
        pa += 1
    else:
        total += b[pb]
        if total > k:
            ans = max(ans, pa + pb)
            return
        pb += 1
    if pa < n:
        shouka("a", pa, pb, total)
    if pb < m:
        shouka("b", pa, pb, total)

    ans = max(ans, pa+pb)


shouka("a", 0, 0, 0)
shouka("b", 0, 0, 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()))

ma=0
for x in range(len(a)+1):
    sa=sum(a[:x])
    for y in range(len(b)+1):
        if sa+sum(b[:y])>k:break
        ma=max(ma,x+y)
print(ma)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
# Your code here!
from itertools import accumulate
import numpy as np

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

sum_A = list(accumulate(A))
sum_B = list(accumulate(B))

#sum_A = np.cumsum(A)
#sum_B = np.cumsum(B)

list_N = [0 for i in range(len(A)+1)]

pre_n = len(B)

for i in range(len(A)+1):
    if i == 0:
        buf = np.array(sum_B[:pre_n])
        num_B = len(buf[buf<=K])
        pre_n = num_B
        list_N[i] = i + num_B
    else:
        if K>=sum_A[i-1]:
            buf = np.array(sum_B[:pre_n])
            num_B = len(buf[buf<=(K-sum_A[i-1])])
            pre_n = num_B
            list_N[i] = i + num_B
        else:
            list_N[i] = 0
    
print(np.max(list_N))

Submission status: Time Limit Exceeded

Solution code:
def LI():
  return list(map(int,input().split()))

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

cumA = [0]
for a in A:
  cumA.append(cumA[-1]+a)

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

res = 0
for i in range(N+1):
  cuma = cumA[i]
  if cuma > K: continue
  rem = K-cuma
  deskb = 0
  for j in range(M, -1, -1):
    if cumB[j] <= rem:
      deskb = j
      break

  res = max(res, i+deskb)

print(res)

Submission status: Time Limit Exceeded

Solution code:
import bisect
import sys
import math
from collections import deque
from heapq import heappush, heappop

def sRaw():
    return input().rstrip("\r")


def iRaw():
    return int(input())


def ssRaw():
    return input().split()


def isRaw():
    return list(map(int, ssRaw()))


INF = 1 << 29

#DIV = (10**9) + 7
DIV = 998244353

def mod_inv_prime(a, mod=DIV):
    return pow(a, mod-2, mod)


def mod_inv(a, b):
    r = a
    w = b
    u = 1
    v = 0
    while w != 0:
        t = r//w
        r -= t*w
        r, w = w, r
        u -= t*v
        u, v = v, u
    return (u % b+b) % b


def CONV_TBL(max, mod=DIV):
    fac, finv, inv = [0]*max, [0]*max, [0]*max
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    inv[1] = 1
    for i in range(2, max):
        fac[i] = fac[i-1]*i % mod
        inv[i] = mod - inv[mod % i] * (mod//i) % mod
        finv[i] = finv[i-1]*inv[i] % mod

    class CONV:
        def __init__(self):
            self.fac = fac
            self.finv = finv
            pass

        def ncr(self, n, k):
            if(n < k):
                return 0
            if(n < 0 or k < 0):
                return 0
            return fac[n]*(finv[k]*finv[n-k] % DIV) % DIV

    return CONV()


def cumsum(As):
    s = 0
    for a in As:
        s += a
        yield s


sys.setrecursionlimit(200005)


def dijkstra(G,start=0):
    heap = []
    cost = [INF]*len(G)
    heappush(heap,(0,start))
    while len(heap)!=0:
        c,n = heappop(heap)
        if cost[n] !=INF:
            continue
        cost[n]=c
        for e in G[n]:
            ec,v = e
            if cost[v]!=INF:
                continue
            heappush(heap,(ec+c,v))
    return cost


def main():
    N,M,K = isRaw()
    As = isRaw()
    Bs = isRaw()
    sAs = [0]
    for a in As:
        t = a + sAs[-1]
        if t>K:
            break
        sAs.append(t)

    sBs=[0]
    for b in Bs:
        t = b + sBs[-1]
        if t>K:
            break
        sBs.append(t)

    ai = len(sAs)-1
    bi = len(sBs)-1
    while sAs[ai]+sBs[bi] > K:
        if ai==0 and bi==0:
            break
        dA = 10**9+1
        if ai!=0:
            dA = sAs[ai]-sAs[ai-1]
        dB = 10**9+1
        if bi!=0:
            dB = sBs[bi]-sBs[bi-1]
        if dA >dB:
            ai=ai-1
            if bi+1!=len(sBs) and sAs[ai]+sBs[bi+1]<=sAs[ai+1]+sBs[bi]:
                bi+=1
        else:
            bi=bi-1
            if ai+1 != len(sAs) and sAs[ai+1]+sBs[bi] <= sAs[ai]+sBs[bi+1]:
                ai += 1
    print(ai+bi)


main()

Submission status: Time Limit Exceeded

Solution code:
# coding: UTF-8

# import numpy as np

N, M, K = list(map(int,input().split(" ")))
A = list(map(int,input().split(" ")))
B = list(map(int,input().split(" ")))
# N, M, K = np.fromstring(input(), sep=" ", dtype=np.int64)
# A = np.fromstring("0 " + input(), sep=" ", dtype=np.int64)
# B = np.fromstring("0 " + input(), sep=" ", dtype=np.int64)

aa = [0]
bb = [0]
for i in range(N):
    aa.append(aa[-1] + A[i])
for i in range(M):
    bb.append(bb[-1] + B[i])
# aa = np.add.accumulate(A)
# bb = np.add.accumulate(B)

# print(aa)
# print(bb)

base = 0
for i in range(N):
    if aa[i] <= K:
        base += 1
    else:
        break

while True:
    for i in range(max(N+1,M+1)):
        if not ((base - i) < N+1 and i < M+1):
            # print("continue", base-i, i)
            continue
        # print(base-i, i,(aa[base-i] + bb[i]))
        if (aa[base-i] + bb[i]) <= K:
            base += 1
            break
    else:
        break
print(base-1)
# cc = np.add.outer(aa, bb)
# print(cc)
# print(base)
#
# dd = cc <= K
#
# num = np.add.outer(np.arange(N+1), np.arange(M+1))
# # print(dd.shape)
# # print(num.shape)
# print(np.max(num[dd]))

Submission status: Time Limit Exceeded

Solution code:
import bisect

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

a_sum = []
for i in range(len(a)):
  a_sum.append(sum(a[:i+1]))
b_sum = []
for j in range(len(b)):
  b_sum.append(sum(b[:j+1]))

if a_sum[0] > k and b_sum[0] > 0:
  print(0)
  exit()

ans = 0
for i in range(len(a)):
  tmp = i + 1 + bisect.bisect(b_sum, k - a_sum[i])
  ans = max(ans, tmp)

print(ans)

Submission status: Time Limit Exceeded

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

n,m,k = map(int,input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a.append(1)
b.append(1)
ans = 0

def dfs(time,count,i,j):
    global ans,n,m,k,a,b
    if i <= n-1:
        if time + a[i] <= k:
            dfs(time + a[i],count + 1,i+1,j)
    if j <= m-1:
        if time + b[j] <= k:
           dfs(time + b[j],count + 1,i,j+1)  
    if (i == n or time + a[i] > k) and (j == m or time + b[j] > k):
        ans = max(ans, count)

shoki_time = 0
shoki_count = 0
shoki_i = 0
shoki_j = 0
dfs(shoki_time,shoki_count,shoki_i,shoki_j)
print(ans)

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

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

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

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

print(max(dfs(A[0], 1, 0, 1), dfs(B[0], 0, 1, 1)))

Submission status: Time Limit Exceeded

Solution code:
#import collections
import numpy

def get_index(l, n):
    tmp = [x for x in l if x <= n]
    return len(tmp)

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

Ac = numpy.cumsum(A)
Bc = numpy.cumsum(B)

ans = 0
for i in range(len(Ac)):
    n = K - Ac[i]
    if n < 0: break
    ans = i + 1
    ans += get_index(Bc, n)
ans = max(ans, get_index(Ac, K), get_index(Bc, K))
print(ans)


'''
#Aq = collections.deque(A)
#Bq = collections.deque(B)

ans = 0
sumt = 0
flag = True
while len(Aq) > 0 and len(Bq) > 0:
    if Aq[0] <= Bq[0]:
        if sumt + Aq[0] > K:
            flag = False
            break
        sumt += Aq.popleft()
        ans += 1
    else:
        if sumt + Bq[0] > K:
            flag = False
            break
        sumt += Bq.popleft()
        ans += 1
if flag:
    if len(Aq) > 0:
        while len(Aq) > 0:
            if sumt + Aq[0] > K:
                break
            sumt += Aq.popleft()
            ans += 1
    else:
        while len(Bq) > 0:
            if sumt + Bq[0] > K:
                break
            sumt += Bq.popleft()
            ans += 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()))
i = 0
j = 0
count = 0
for i in range(N+1):
	for j in range(M+1):
		if sum(A[:i]) + sum(B[:j]) <= K:
			tmp = i + j
			count = max(tmp, count)
		else:
			break
	if sum(A[:i]) > K:
		break
print(count)

Submission status: Time Limit Exceeded

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


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

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

if sum(a)+sum(b)<=k:
    print(len(a)+len(b))
    
else:
    times=0
    a_n=0
    b_n=0
    for j in range(len(a)):
        times+=a[j]
        a_n+=1
        if times>k:
            a_n-=1
            break
    ans=[]
    ans.append(a_n)
    for i in range(a_n+1):
        times=0
        b_n=0
        for j in range(len(b)):
            times=sum(a[:i])+sum(b[:j])
            if times>k:
                ans.append(i+j-1)
                break
        else:
            ans.append(i+j)
    print(max(ans))

Submission status: Time Limit Exceeded

Solution code:
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
ans=0
for i in range(n):
  if sum(a[:i+1])>k:
    break
  elif sum(a[:i+1])+sum(b)<=k:
      ans=max(ans,i+m+1)
  for j in range(m):
    if sum(a[:i+1])+sum(b[:j+1])<=k:
      continue
    else:
      ans=max(ans,i+j+1)
      break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    n,m,k=map(int,input().split())
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    A,B=[],[]
    A.append(0)
    B.append(0)
    x=0
    for i in range(n):
        x+=a[i]
        A.append(x)
    x=0
    for j in range(m):
        x+=b[j]
        B.append(x)
    ans=0
    for i in range(n+1):
        if A[i]>k:
            break
        else:
            for j in range(m+1):
                if A[i]+B[j]>k:
                    j-=1
                    break
            ans=max(ans,i+j)
    print(ans)

if __name__ == '__main__':
    resolve()

Submission status: Time Limit Exceeded

Solution code:
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
aa = a.copy()
bb = b.copy()
sa = sum(a)
sb = sum(b)
ans = 10**7
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)]
for i in range(n+m):
  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
d = [0] + d

for i in range(n+m+1):
  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:
#!/usr/bin/env python3
import sys


def solve(N: int, M: int, K: int, A: "List[int]", B: "List[int]"):
    weightA = 0
    tmp_A = N
    for idx, a in enumerate(A):
        if weightA + a > K:
            tmp_A = idx
            break
        else:
            weightA += a
    max_count = current_count  = tmp_A
    len_A = tmp_A

    weightB = 0
    terminated = False
    for b in B:
        if terminated == True and weightA + weightB > K:
            break

        if len_A==0 and weightA + weightB + b > K:
            break

        if weightA + weightB + b <= K:
            weightB += b
            current_count += 1

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

            if weightA + weightB + b <= K:
                weightB += b
                current_count += 1
        max_count = max(max_count, current_count)

    print(max_count)
    return


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    B = [int(next(tokens)) for _ in range(M)]  # type: "List[int]"
    solve(N, M, K, A, B)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(500000)

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

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


class X:
    def __init__(self, K, A, B, N, M):
        self.A = A
        self.B = B
        self.K = K
        self.N = N
        self.M = M
        self.ret = 0

    def helper(self, i, j, time, num_a, num_b):
        if time > self.K:
            return

        self.ret = max(self.ret, num_a + num_b)

        if num_a < self.N:
            self.helper(i + 1, j, time + self.A[i], num_a + 1, num_b)

        if num_b < self.M:
            self.helper(i, j + 1, time + self.B[j], num_a, num_b + 1)

        return

    def run(self):
        self.helper(0, 0, 0, 0, 0)
        return self.ret


c = X(K, A, B, N, M)
print(c.run())

Submission status: Time Limit Exceeded

Solution code:
def main():
  N, M, K = map(int, input().split())
  A = []
  A = list(map(int, input().split()))
  B = []
  B = list(map(int, input().split()))
  if sum(A) + sum(B) <= K:
    print(N+M)
    return

  min_A = []
  s = 0
  for a in A:
    s = s + a
    if s > K:
      break
    min_A.append(a)

  min_B = []
  s = 0
  for b in B:
    s = s + b
    if s > K:
      break
    min_B.append(b)


  if len(min_A) == len(min_B):
    if sum(min_A) < sum(min_B):
      long_arr = min_A
      short_arr = min_B
    else:
      long_arr = min_B
      short_arr = min_A

  elif len(min_A) > len(min_B):
    long_arr = min_A
    short_arr = min_B

  else:
    long_arr = min_B
    short_arr = min_A

  short_len = len(short_arr)
  long_len = len(long_arr)

  long_sum = sum(long_arr)

  for i in range(short_len):
    short_cand = short_arr[0:short_len-i]
    for j in range(0, len(short_cand)):
      long_cand = long_arr[long_len-j:]
      # print("i: {}, j: {}, short_cand: {}, long_cand: {}".format(i, j, short_cand, long_cand))
      if long_sum - sum(long_cand) + sum(short_cand) <= K:
        print(long_len - j + len(short_cand))
        return

  print(long_len)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:

def appendd(N, A, sumlis, lis, K):
    dp_max = max(lis)
    dp_index = lis.index(dp_max)
    for a in range(N):
        if A[a]+sumlis > K:
            if dp_max > A[a]:
                sumlis += A[a] - dp_max
                lis[dp_index] = A[a]
                dp_max = max(lis)
                dp_index = lis.index(dp_max)
        else :
            lis.append(A[a])
            sumlis += A[a]
            dp_max = max(lis)
            dp_index = lis.index(dp_max)
    return sumlis, lis


def func():
    N,M,K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    sumlis = 0
    lis =[0]
    
    sumlis, lis = appendd(N, A, sumlis,lis,K)
    _,     lis = appendd(M, B, sumlis,lis,K)
    print(len(lis)-1)
if __name__=='__main__':
    func()