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,V = list(map(int,input().split()))
A=list(map(int,input().split()))
B=list(map(int,input().split()))
S=list(map(int,A+B))
ans=0
time=0
if sum(S)<=V:
  print(N+M)
  exit()
K=sorted(S)
for i in range(N+M):
  if K[0]+time>V:
    print(ans)
    exit()
  else:
    time+=K[0]
    K.pop(0)
    ans+=1

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()))
result=0
for i in range(len(a)+1):
    for j in range(len(b)+1):
        if sum(a[:i]) + sum(b[:j]) <= k:
            if result < i+j:
                result=i+j
print(result)

Submission status: Time Limit Exceeded

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

while k >= 0:
    if not a:
        if (k - b[0]) < 0: break
        k -= b[0]
        del b[0]
    elif not b:
        if (k - a[0]) < 0: break
        k -= a[0]
        del a[0]
    elif a[0] <= b[0]:
        if (k - a[0]) < 0: break
        k -= a[0]
        del a[0]
    else:
        if (k - b[0]) < 0: break
        k -= b[0]
        del b[0]
    count += 1
    if not a and not b: 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()))

at = [0]
for i in range(len(A)):
    at.append(A[i])
    if i > 0:
        at[-1] += at[i]

bt = [0]
for i in range(len(B)):
    bt.append(B[i])
    if i > 0:
        bt[-1] += bt[i]


n = 0
time = 0

for i, a in enumerate(at):
    if a > K:
        break
    for j, b in enumerate(bt):
        if b > K:
            break
        if a+b <= K and a+b >= time:
            time = a+b
            n = i + j

print(n)

Submission status: Time Limit Exceeded

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

def main():
    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 = [0]*(n+1)
    a_sum[0] = a[0]
    for i in range(1, n+1):
        a_sum[i] = a_sum[i-1] + a[i]

    b_sum = [0]*(m+1)
    b_sum[0] = b[0]
    for i in range(1, m+1):
        b_sum[i] = b_sum[i-1] + b[i]

    ans = 0
    for i_a in range(n+1):
        for j_b in range(m+1):
            if a_sum[i_a]+b_sum[j_b] <= k:
                ans = max(ans, i_a + j_b)
            else:
                break

    print(ans)
    return


if __name__ == "__main__":
    main()
    sys.exit(0)

Submission status: Time Limit Exceeded

Solution code:
N,M,V = list(map(int,input().split()))
A=list(map(int,input().split()))
B=list(map(int,input().split()))
S=list(map(int,A+B))
ans=0
time=0
K=sorted(S)
for i in range(N+M):
  if K[0]+time>V:
    print(ans)
    exit()
  elif i==N+M-1:
    print(N+M)
  else:
    time+=K[0]
    K.pop(0)
    ans+=1

Submission status: Time Limit Exceeded

Solution code:
def solve():
  N, M, K = map(int,input().split())
  AA = list(map(int,input().split()))
  BB = list(map(int,input().split()))
  A_max = 0
  AAs = [0]*N
  AAs[0] = AA[0]
  if K>=AAs[0]:
    A_max = 1
  for i in range(1,N):
    AAs[i] = AAs[i-1] + AA[i]
    if K>=AAs[i]:
      A_max+=1
  BBs = [0]*M
  BBs[0] = BB[0]
  for i in range(1,M):
    BBs[i] = BBs[i-1] + BB[i]
  Book_max = 0
  for i in range(A_max+1):
    b_max = i
    if i == 0:
      B_time = K
    else:
      B_time = K - AAs[i-1]
    for j in range(M):
      if B_time>=BBs[j]:
        b_max +=1
    if Book_max<b_max:
      Book_max = b_max
  print(Book_max) 
solve()

Submission status: Time Limit Exceeded

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

A=[int(i) for i in input().split()]

B=[int(i) for i in input().split()]

count=0
time=0
while(1):
    if ((time>k)|((A==[])&(B==[]))|time>=1000000000):
        break
    if A[0]>B[0]:
        count+=1
        time+=A[0]
        A=A[1:]
    elif A[0]<B[0]:
        count+=1
        time+=B[0]
        B=B[1:]
    if A==[]:
        A.append(0)
    if B==[]:
        B.append(0)
print(count)

Submission status: Time Limit Exceeded

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

time_A = list(map(int, input().split()))
time_B = list(map(int, input().split()))
max_books = 0
change_point = 0

for i in range(1, K+1):  # 変化点は、aがもう一冊読める　or bが読める本一冊へる
    if i <= change_point-1:
        continue
    if max_books == len(time_A) + len(time_B):
        break
    num_books = 0
    change_point_a = 10000000000
    change_point_b = 10000000000
    allot_to_A = i
    allot_to_B = K-i

    for j in range(len(time_A)):
        if time_A[j] <= allot_to_A:
            allot_to_A -= time_A[j]
            num_books += 1
        else:
            change_point_a = time_A[j] - allot_to_A  # Aで読める本が+1される時間
            break
    for k in range(len(time_B)):
        if time_B[k] <= allot_to_B:
            allot_to_B -= time_B[k]
            num_books += 1
        else:
            change_point_b = allot_to_B + time_B[k-1]  # Bで読める本が-1される時間
            break
    change_point = min(change_point_a, change_point_b) + i  # 次の変化点
    max_books = max(max_books, num_books)

print(max_books)

Submission status: Time Limit Exceeded

Solution code:
[N,M,K]=list(map(int, input().split()))
alist=list(map(int, input().split()))
blist=list(map(int, input().split()))
alist.sort()
blist.sort()
time=0
ans=-1
while time<=K:
    ans+=1
    if alist==[] and blist==[]:
        break
    elif alist==[]:
        time+=blist[0]
        blist.remove(blist[0])
    elif blist==[]:
        time+=alist[0]
        alist.remove(alist[0])
    elif alist[0]>blist[0]:
        time+=blist[0]
        blist.remove(blist[0])
    else:
        time+=alist[0]
        alist.remove(alist[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()))

books = sorted(A + B)
time = 0
count = 0
while books:
    time += books.pop(0)

    if time > K:
        break
    else:
        count += 1

print(count)

Submission status: Time Limit Exceeded

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

if min(A[0],B[0])>K:
  print(0)
  exit()

c=[0 for _ in range(N)] + [1 for _ in range(M)]
C=list(permutations(c,len(c)))

for i in range(len(C)):
  countA=0
  countB=0
  ans=0
  answer=[]
  for j in range(len(c)):
    if C[i][j]==0:
      ans+=A[countA]
      countA+=1
    else:
      ans+=B[countB]
      countB+=1
    if ans>=K:
      answer.append(countA+countB)
      break
print(max(answer))

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

sb = [0] * (m + 1)
for j in range(m):
  sb[j + 1] = sb[j] + b[j]

sa = 0
anses = []
for i in range(n + 1):
  for j in range(m + 1):
    # print(sa,sb[j])
    if sa + sb[j] <= k:
      anses.append(i + j)
  if i == n:
    break
  sa += a[i]

print(max(anses))

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()))
C=A+B
C.sort()
for i in range(len(C)) :
    if sum(C[:i])>K :
        print(i-1)
        sys.exit()
print(len(C))

Submission status: Time Limit Exceeded

Solution code:
# 解法 全探索をする，上から読んでる冊数をi,jとしてKを超えない範囲で最大のものを週つ力すれば良い
N,M,K = map(int,input().split())

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

sum_time=0
max_count = 0
count = 0
# i,jは実際に読んでいる数とする
for i in range(N+1):
    count = 0
    if i ==0:
        sum_time=0
    else:
        sum_time=sum(A[:i])
        count += i

    for j in range(M+1):
        if j == 0:
            sum_time += 0
        else:
            sum_time += sum(B[:j])
            count += j
        if sum_time > K:
            break
        max_count = max(max_count,count)
        sum_time -= sum(B[:j])
        count -= j

print(max_count)

Submission status: Time Limit Exceeded

Solution code:
N,M,K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
A=sorted(A)
B=sorted(B)
num=0
ans=0
while num<K:
    if N==0 and M==0:
        break
    if N==0:
        b = B[0]
        if num+b<=K:
            num+=b
            ans+=1
            del B[0]
            continue
        else:
            break
    if M==0:
        a = A[0]
        if num + a <= K:
            num += a
            ans += 1
            del A[0]
            continue
        else:
            break
    if A[0]>=B[0]:
        b=B[0]
        if num+b<=K:
            num+=b
            ans+=1
            del B[0]
            M-=1
        else:
            break
    else:
        a=A[0]
        if num+a<=K:
            num+=a
            ans+=1
            del A[0]
            N-=1
        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()))

at = [0]
for i in range(len(A)):
    at.append(A[i])
    if i > 0:
        at[-1] += at[i]

bt = [0]
for i in range(len(B)):
    bt.append(B[i])
    if i > 0:
        bt[-1] += bt[i]


n = 0
time = 0

for i, a in enumerate(at):
    for j, b in enumerate(bt):
        if a+b <= K and a+b >= time:
            time = a+b
            n = i + j

print(n)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n, m, k = [int(k) for k in input().split()]
a = np.array([int(k) for k in input().split()], dtype=int)
b = np.array([int(k) for k in input().split()], dtype=int)
r = 0
c = 0
l = np.concatenate([a, b])
while True:
    i = np.argmin(l)
    r += l[i]
    l = np.delete(l, i)
    if r <= k:
        c += 1
    else:
        break
    if len(l) == 0:
        break
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()))
readable = 0
for rx in range(1,n+m+1):
  for ra in range(0,rx+1):
    rb = rx - ra
    rt = 0
    if ra <= len(a) and rb <= len(b):
      if ra>0 :
        for i in range(0,ra):
          rt += a[i]
      if rb>0 :
        for i in range(0,rb):
          rt += b[i]
      if rt <= k: readable = rx
print(readable)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys
from itertools import accumulate


def debug(*x):
    print(*x)


def solve():
    "void()"
    pass


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

    AS = list(map(int, input().split()))
    BS = list(map(int, input().split()))

    sA = [0] + list(accumulate(AS))
    sB = [0] + list(accumulate(BS))
    # debug("s:sA,sB", sA, sB)
    last_can_read = 0
    for ij in range(1, N + M + 2):
        # debug(": ij", ij)
        for i in range(0, ij + 1):
            j = ij - i
            # debug(": i,j", i, j)
            if j > M:
                # debug("cont: ", )
                continue
            if i > N:
                # debug("break: ", )
                break
            cost = sA[i] + sB[j]
            # debug(": cost", cost)
            if cost <= K:
                # can read
                # debug("can read: ", i, j, cost)
                last_can_read = ij
                break
        else:
            # no break = can't read
            break

    print(last_can_read)


def _test():
    """
    >>> 
    >>> main()
    """
    import doctest
    as_input('''
    3 4 240
    60 90 120
    80 150 80 150
    ''')
    main()
    doctest.testmod()


def as_input(s):
    "use in test, use given string as input file"
    import io
    global read, input
    f = io.StringIO(s.strip())
    input = f.readline
    read = f.read


USE_NUMBA = False
if (USE_NUMBA and sys.argv[-1] == 'ONLINE_JUDGE') or sys.argv[-1] == '-c':
    print("compiling")
    from numba.pycc import CC
    cc = CC('my_module')
    cc.export('solve', solve.__doc__.strip().split()[0])(solve)
    cc.compile()
    exit()
else:
    input = sys.stdin.buffer.readline
    read = sys.stdin.buffer.read

    if (USE_NUMBA and sys.argv[-1] != '-p') or sys.argv[-1] == "--numba":
        # -p: pure python mode
        # if not -p, import compiled module
        from my_module import solve  # pylint: disable=all
    elif sys.argv[-1] == "-t":
        _test()
        sys.exit()
    elif sys.argv[-1] != '-p' and len(sys.argv) == 2:
        # input given as file
        input_as_file = open(sys.argv[1])
        input = input_as_file.buffer.readline
        read = input_as_file.buffer.read

    main()

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

    max_num = 0
    for i in range(len(As)+1):
        k = sum(As[:i])
        if k > K:
            break
        num = i
        for j in range(len(Bs)):
            if k + Bs[j] <= K:
                k += Bs[j]
                num += 1
            else:
                break
        max_num = max([max_num, num])

    print(max_num)



if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

    C = np.sort(A + B)

    if sum(C) <= K:
        print(int(N+M))
        return

    for i in range(len(C)):
        if sum(C[:i]) <= K and K < sum(C[:i+1]):
             print(int(i))
             return


solve()

Submission status: Time Limit Exceeded

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

# print(n, m, k)
# print(a)
# print(b)

ans = set()
ans.add(0)
for i_n in range(0, n+1):
    if sum(a[:i_n]) > k:
        break
    for i_m in range(0, m+1):
        if sum(b[:i_m]) > k:
            break

        time_sum = sum(a[:i_n]) + sum(b[:i_m])
        if time_sum <= k:
            # print(i_n, a[:i_n], i_m, sum(b[:i_m]), k)
            ans.add(i_n + i_m)

# print(ans)

print(list(ans)[-1])

Submission status: Time Limit Exceeded

Solution code:

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

sum_time = 0
count = 0

while sum_time<K:
    if A[0] <= B[0]:
        sum_time+=A[0]
        A.pop(0)
        if len(A)==0:
            A.append(10**9)
    else:
        sum_time+=B[0]
        B.pop(0)
        if len(B)==0:
            B.append(10**9)
    count+=1
if sum_time>K:
    count-=1
print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(500000)

from sys import stdin
input = stdin.readline
def Tsundoku(i,j,k):
    if (i,j) in dp:
        return dp[(i,j)]
    ans1,ans2 = 0,0
    if i<n and k-a[i]>=0:
        ans1 = 1+Tsundoku(i+1,j,k-a[i])
    if j<m and k-b[j]>=0:
        ans2 = 1+Tsundoku(i,j+1,k-b[j])
    dp[(i,j)] = max(ans1,ans2)
    return dp[(i,j)]
        
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
dp = dict()
ans = Tsundoku(0,0,k)
print(ans)
#print(dp)

Submission status: Time Limit Exceeded

Solution code:
import sys

def resolve():
    lines = sys.stdin.readlines()

    n, m, k = tuple(map(int, lines[0].strip().split(' ')))
    a = list(map(int, lines[1].strip().split(' ')))
    b = list(map(int, lines[2].strip().split(' ')))
    c_max = 0
    c = 0
    s = 0
    for q in b:
        s += q
        if s > k:
            break
        c += 1
    c_max = max(c, c_max)
    s = 0
    c = 0
    for p in a:
        s += p
        c += 1
        if s > k:
            break
        sb = s
        cb = c
        for q in b:
            sb += q
            if sb > k:
                break
            cb += 1
        c_max = max(cb, c_max)


    print(c_max)

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()))
SUM_A=[0]
for a in A:
    SUM_A.append(a+SUM_A[-1])
SUM_B=[0]
for b in B:
    SUM_B.append(b+SUM_B[-1])

cnt = 0
for i in range(len(A)+1):
    for j in range(len(B)+1):
        if SUM_A[i]+SUM_B[j] <= K:
            if i+j > cnt:
                cnt = i+j
print(cnt)

Submission status: Time Limit Exceeded

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

ans=0
anum=0
bnum=0
tmp=0
P=0
for a in range(n+1):
    bunm=0
    if(a>0):
        anum+=A[a-1]
    if(anum>k):
        break
    for b in range(m+1):
        if(b>0):
            bnum+=B[b-1]
        if(anum+bnum<=k):      
          #  print(a,b,ans,P,anum,bnum)
            tmp=max(anum+bnum, ans)
            if(ans<=tmp):
                P=max(a+b, P)
            ans=tmp
        if(bnum+anum>k):
            break
    bnum=0

print(P)

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.sort()
b.sort()
sum=0
book_count=0
for i in range(3000000):
  if a==[] and b==[]:
    print(book_count)
    exit()
  elif a==[] and b!=[]:
    if sum+b[0]<=k:
      sum+=b[0]
      book_count+=1
      b.pop(0)
    else:
      print(book_count)
      exit()
      
  elif a!=[] and b==[]:
    if sum+a[0]<=k:
      sum+=a[0]
      book_count+=1
      a.pop(0)
    else:
      print(book_count)
      exit()
  else:    
    if sum+min(a[0],b[0])<=k:
      sum+=min(a[0],b[0])
      book_count+=1
      if min(a[0],b[0])==a[0]:
        a.pop(0)
      else:
        b.pop(0)
    
    else:
      print(book_count)
      exit()
print(book_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()))
atime = {0:0}
btime = {0:0}
for i in range(1,n+1):
  atime[i] = atime[i-1]+a[i-1]
for i in range(1,m+1):
  btime[i] = btime[i-1]+b[i-1]
book = 0
for i in range(n+1):
  if atime[i]>k:
    break
  else:
    for j in range(m+1):
      if atime[i]+btime[j]>k:
        break
      else:
        book = max(book,i+j)
print(book)

Submission status: Time Limit Exceeded

Solution code:
#!python3

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

def resolve():
    it = map(int, sys.stdin.read().split())
    N, M, K = next(it), next(it), next(it)
    A = [next(it) for i in range(N)]
    B = [next(it) for i in range(M)]

    dq = []
    heappush(dq, (0, 0, 0, 0))
    ans = 0
    seen = set()
    while dq:
        k, n, i, j = heappop(dq)

        if (i, j) in seen:
            continue
        if k > K:
            break
        seen.add((i, j))
        ans = max(ans, n)

        if i < N:
            heappush(dq, (k+A[i], n+1, i+1, j))
        if j < M:
            heappush(dq, (k+B[j], n+1, i, j+1))

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

cnt = 0
for i in range(len(A)+1):
    for j in range(len(B)+1):
        if sum(A[:i])+sum(B[:j]) <= K:
            if i+j > cnt:
                cnt = i+j
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
t = 0
c = 0
while(t<k or a!=[] or b!=[]):
  if a[0] < b[0]:
    t += a.pop(0)
    c+=1
  else:
    t += b.pop(0)
    c+=1
  if a==[]:a.append(10**9+1)
  if b==[]:b.append(10**9+1)
  if a[0]==10**8 and b[0]==10**8:
    c+=1
    break
print(c-1)

Submission status: Time Limit Exceeded

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

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

Acum = [0]*n
Bcum = [0]*m
Acum[0] = A[0]
Bcum[0] = B[0]

for i in range(1,n):
  Acum[i] = Acum[i-1] + A[i]  
for i in range(1,m):
  Bcum[i] = Bcum[i-1] + B[i]

mx = 0
for i in range(n+1):
  cnt = 0
  if i==0:
    rest = k
  else:
    rest = k - Acum[i-1]
    cnt = i
    if rest<0:continue
  
  for j in range(m):
    if rest<Bcum[j]:
      cnt += j
      break
  if rest>=Bcum[-1]:
    cnt += m
  if cnt>mx:
    mx = cnt
    
print(mx)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

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

q = deque()
q.append((0,0,0))#(time,a,b)
r = 0

while len(q) > 0:
  el = q.popleft()
  time = el[0]
  a = el[1]
  b = el[2]
  
  r = max(r,a+b)
  
  if a<N:
    if Ali[a] <= K-time:
      q.append((time+Ali[a],a+1,b))
    
  if b<M:
    if Bli[b] <= K-time:
      q.append((time+Bli[b],a,b+1))
    
print(r)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

for i in range(n):
    a[i+1] += a[i]

for i in range(m):
    b[i+1] += b[i]

ans = 0

for i in range(n+1):
    if k < a[i]:
        continue
    l = 0
    r = m + 1
    while r - l > 1:
        mid = (l + r) // 2
        if b[mid] + a[i] <= k:
            l = mid
        else:
            r = mid

    ans = max(ans, i + l)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
#!python3

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

def resolve():
    it = map(int, sys.stdin.read().split())
    N, M, K = next(it), next(it), next(it)
    A = [next(it) for i in range(N)]
    B = [next(it) for i in range(M)]

    dq = []
    heappush(dq, (0, 0, 0, 0))
    ans = 0
    while dq:
        k, n, i, j = heappop(dq)

        if k > K:
            break
        ans = max(ans, n)

        if i < N:
            heappush(dq, (k+A[i], n+1, i+1, j))
        if j < M:
            heappush(dq, (k+B[j], n+1, i, j+1))

    print(ans)


if __name__ == "__main__":
    resolve()

Submission status: Time Limit Exceeded

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

#a = int(input())
#b = list(map(int, input().split()))
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
#s = input()
#s,t = input().split()
#a = [int(input()) for _ in range(n)]
#
#readline = sys.stdin.readline
#n,m = [int(i) for i in readline().split()]
#ab = [[int(i) for i in readline().split()] for _ in range(n)]

a.sort()
b.sort()

ans = 0
time = 0

while time <= k:
    if a and b:
        tmp_a = time+a[0]
        tmp_b = time+b[0]
        if tmp_a <= k and tmp_b <= k and a[0] <= b[0]:
            time += a[0]
            a.pop(0)
            ans += 1
        elif tmp_a <= k and tmp_b <= k and b[0] <= a[0]:
            time += b[0]
            b.pop(0)
            ans += 1
        elif tmp_a <= k:
            time += a[0]
            a.pop(0)
            ans += 1
        elif tmp_b <= k:
            time += b[0]
            b.pop(0)
            ans += 1
        else:
            break
    elif a:
        tmp_a = time+a[0]
        if tmp_a <= k:
            time += a[0]
            a.pop(0)
            ans += 1
        else:
            break
    elif b:
        tmp_b = time+b[0]
        if tmp_b <= k:
            time += b[0]
            b.pop(0)
            ans += 1
        else:
            break
    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()))
atime = [0]*(n+1)
btime = [0]*(m+1)
for i in range(1,n+1):
  atime[i] = atime[i-1]+a[i-1]
for i in range(1,m+1):
  btime[i] = btime[i-1]+b[i-1]
book = 0
for i in range(n+1):
  if atime[i]>k:
    break
  else:
    for j in range(m+1):
      if atime[i]+btime[j]>k:
        break
      else:
        book = max(book,i+j)
print(book)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque
import bisect
n,m,k = map(int, input().split())
a = deque(map(int, input().split()))
b = deque(map(int, input().split()))
ans = 0
a.appendleft(0)
b.appendleft(0)

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

for i in range(n+1):
    now = k-a[i]
    if now<0:
        continue
    idx = bisect.bisect_right(b,now)
    ans = max(ans,idx-1+i)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, M, K = map(int, input().split())
As = [int(i) for i in input().split()]
Bs = [int(i) for i in input().split()]

time = 0
cnt = 0
while True:
  if As[-1] < Bs[0]:
    if time + As[0] > K:
      break
    time+=As[0]
    del As[0]
    if As == []:
      As.append(10**10)
  else:
    if time + Bs[0] > K:
      break
    time+=Bs[0]
    del Bs[0]
    if Bs == []:
      Bs.append(10**10)
  cnt+=1
print(cnt)

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,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)]
    ans = 0
    ab = A + B
    ab.sort()
    while k >= 0 and len(ab) > 0:
        if k >= ab[0]:
            k -= ab[0]
            ans += 1
            ab.pop(0)
        else:
            print(ans)
            exit()
    print(ans)
    exit()






if __name__ == '__main__':
    solve()

Submission status: Time Limit Exceeded

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

time_A = list(map(int, input().split()))
time_B = list(map(int, input().split()))
max_books = 0

for i in range(1, K+1):
    num_books = 0
    allot_to_A = i
    allot_to_B = K-i

    for j in range(len(time_A)):
        if time_A[j] <= allot_to_A:
            allot_to_A -= time_A[j]
            num_books += 1
        else:
            break
    for k in range(len(time_B)):
        if time_B[k] <= allot_to_B:
            allot_to_B -= time_B[k]
            num_books += 1
        else:
            break

    max_books = max(max_books, num_books)

print(max_books)

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 = 0
count = 0
if(min(min(A),min(B))>K):
    print(0)
    exit()
while time<=K:
    if(len(A)!=0 and len(B)!=0):
        if(A[0]<B[0]):
            time+= A[0]
            A.pop(0)
            count +=1
        elif(A[0]>B[0]):
            time += B[0]
            B.pop(0)
            count+=1
    elif((len(A)==0) and (len(B)==0)):
        break
    elif(len(A)==0):
        time += B[0]
        B.pop(0)
        count += 1
    elif(len(B)==0):
        time += A[0]
        A.pop(0)
        count += 1
if(time == K):
    print(count)
else:
    print(count-1)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
from collections import deque
import heapq
import itertools
from decimal import Decimal
import bisect
from operator import itemgetter
MAX_INT = int(10e18)
MIN_INT = -MAX_INT
mod = 1000000000+7
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N,M,K = IL()
a = IL()
b = IL()

ans = 0
tmp2 = 0
for j in range(M):
  tmp2 += b[j]
  if tmp2 > K:
    ans = j
    break
else:
  ans = M

tmp = 0
for i in range(N):
  cnt = i+1
  tmp += a[i]
  time = tmp
  if time > K:
    break
  else:
    nokori = K - time
    tmp2 = 0
    for j in range(M):
      tmp2 += b[j]
      if nokori < tmp2:
        cnt += j
        break
    else:
      cnt += M
  ans = max(ans, cnt)
print(ans)

Submission status: Time Limit Exceeded

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

count = 0
while True:
    if len(a) > 0 and a[0] <= k:
        k -= a[0]
        a.pop(0)
        count += 1
        continue
    if len(b) > 0 and b[0] <= k:
        k -= b[0]
        b.pop(0)
        count += 1
        continue
    break
print(count)