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: Wrong Answer

Solution code:
n,m,k = map(int, input().split())
ln_a = list( map(int, input().split()))
ln_b = list( map(int, input().split()))
total = 10**9
output = 0
for i in range(1,len(ln_a) + len(ln_b) +1):
    for j in range(i +1):
        total = min(total, sum(ln_a[:j]) +  sum(ln_a[:i-j] ))
    if total <= k:
        output = i
    total = 10**9
print(output)

Submission status: Wrong Answer

Solution code:
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
count_a = 0
count_b = 0
total = 0
minutes = 0
while count_a < n and count_b < m and minutes < k:
  if a[count_a] < b[count_b] and minutes + a[count_a] <= k:
    total += 1
    minutes += a[count_a]
    count_a += 1
  elif a[count_a] > b[count_b] and minutes + a[count_a] <= k:
    total += 1
    minutes += b[count_b]
    count_b += 1
while count_a < n and  minutes + a[count_a] <= k:
  total += 1
  minutes += a[count_a]
  count_a += 1
while count_b < m and  minutes + b[count_b] <= k:
  total += 1
  minutes += b[count_b]
  count_b += 1
print(total)

Submission status: Wrong Answer

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

def csum(l,ll):
    s = 0
    res = [0] * ll
    for i in range(ll):
        s += l[i]
        res[i] = s
    return res

csln = csum(ln,n)
cslm = csum(lm,m)

import numpy as np

lk = [k-x for x in csln if k-x >=0]
res = np.searchsorted(np.array(cslm), np.array(lk), side='right')
rmax = 0
if ln[0]>k and lm[0]>k:
    pass
else:
    for i,j in zip(range(1,len(lk)+1), res):
        rmax = max(rmax, i+j)
print(rmax)

Submission status: Wrong Answer

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

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


time = 0
i=0
k=0

if sum(A)+sum(B)<=K:
    print(M+N)
    exit()


while True:
    if i>=N and not k>=M:
        time += B[k]
        if time >K:
            print(i+k)
            exit()
        k+=1
    elif k>=M and not i>=N:
        time += A[i]
        if time >K:
            print(i+k)
            exit()
        i += 1
    
    else :
        if A[i]>=B[k]:
            time += B[k]
            if time >K:
                print(i+k)
                exit()
            k+=1

        else:
            time += A[i]
            if time >K:
                print(i+k)
                exit()
            i += 1

Submission status: Wrong Answer

Solution code:
a = input().split(" ")
N = int(a[0])
M = int(a[1])
K = int(a[2])
Ai = input().split(" ")
Bi = input().split(" ")
Ai = [int(s) for s in Ai]
Bi = [int(s) for s in Bi]
time_ = int(K)
time0 = 0
acount = 0
bcount = 0

for i in range(10000000000000000):
    try:
        acount += 1
        time0 += Ai[i]
        if time0 > time_:
            time0 -= Ai[i]
            break
    except IndexError:
        break
acount -= 1
atime = time0
time0 = 0
for i in range(10000000000000000):
    try:
        bcount += 1
        time0 += Bi[i]
        if time0 > time_:
            time0 -= Bi[i]
            break

    except IndexError:
        break
bcount -= 1
btime = time0
allc = bcount
if bcount > acount:
    hic = bcount
else:
    hic = acount
# AiからBiに移す
ci = 0
for i in range(acount):

    try:
        while time0 + Ai[i + ci] <= time_:
            time0 += Ai[i + ci]
            allc += 1
            ci += 1
            if hic < allc:
                hic = allc
    except IndexError:
        pass
    try:
        time0 -= Bi[bcount - 1]
        bcount -= 1
        allc -= 1
    except IndexError:
        break
    try:
        while time0 + Ai[i + ci] <= time_:
            time0 += Ai[i + ci]
            allc += 1
            ci += 1
            if hic < allc:
                hic = allc
    except IndexError:
        break
print(hic)

Submission status: Wrong Answer

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

for n in range(N+M):
  if len(A)==0:
    ans+=1
    time+=B[0]
    B.pop(0)
  elif len(B)==0:
    ans+=1
    time+=A[0]
    A.pop(0)
  else:
    if A[0]<B[0]:
      ans+=1
      time+=A[0]
      A.pop(0)
    elif B[0]<A[0]:
      ans+=1
      time+=B[0]
      B.pop(0)
    else:
      if sum(A)<sum(B):
        ans+=1
        time+=A[0]
        A.pop(0)
      else:
        ans+=1
        time+=A[0]
        B.pop(0)
        
  if K<time:
    print(ans-1)
    exit()
  
print(ans)

Submission status: Wrong Answer

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

AsFirst= As+Bs
BsFirst= Bs+As

#----------
def ShouldReadAs4(As,Bs):
    if As==Bs:
        return True
 
    for i in range(1,len(AsFirst)+1):
        if As[:i]==Bs[:i]:
            continue
        else:
            if AsFirst[i-1]<BsFirst[i-1]:
                return True
            elif AsFirst[i-1]>BsFirst[i-1]:
                return False
#----------
count=0
 
while (K>0):
    if ShouldReadAs4(AsFirst,BsFirst):
        K-=AsFirst.pop(0)
    else:
        K-=BsFirst.pop(0)
    count+=1
    
if len(AsFirst)>len(Bs) or len(BsFirst)>len(As):
    print(count-1)
else:
    print(count)

Submission status: Wrong Answer

Solution code:
import random
N, M, K=[int(i) for i in input().split(" ")]
a=[int(i) for i in input().split(" ")]
b=[int(i) for i in input().split(" ")]

while True:
    if (N>10**8) | (M>10**8) |(K>10**8):
        if random.random()>0.75:
            print(0)
            break
    time=0
    a_idx=0
    b_idx=0
    count=0
    while time<K:
        count+=1
        try:
            if a_idx+1==N:
                time+=b[b_idx]
                b_idx+=1
                if time>K:
                    count-=1
                continue
            elif b_idx+1==M:
                time+=a[b_idx]
                a_idx+=1
                if time>K:
                    count-=1
                continue    

            if a[a_idx]<b[b_idx]:
                time+=a[a_idx]
                a_idx+=1

            elif a[a_idx]>b[b_idx]:
                time+=b[b_idx]
                b_idx+=1
            else:
                try:
                    a_com=a_idx
                    b_com=b_idx
                    while True:
                        if a[a_com]>b[b_com]:
                            time+=a[a_idx]
                            a_idx+=1
                            break
                        elif a[a_com]<b[b_com]:
                            time+=b[b_idx]
                            b_idx+=1
                            break
                        a_com+=1
                        b_com+=1
                except:
                    time+=a[a_idx]
                    a_idx+=1
            if time>K:
                count-=1            
        except:
            break
    print(count)
    break

Submission status: Wrong Answer

Solution code:
N_M_K_str = input().split()
A_str = input().split()
B_str = input().split()

N_M_K = [int(s) for s in N_M_K_str]
A = [int(s) for s in A_str]
B = [int(s) for s in B_str]

limit = N_M_K[2]
time = 0
count = 0


for i in range(N_M_K[0] + N_M_K[1]):
    if A != [] and B != []:
        if A[0] + time > limit and B[0] + time > limit:
            break
        elif A[0] >= B[0]:
            time += B[0]
            count += 1
            del B[0]
        elif A[0] < B[0]:
            time += A[0]
            count += 1
            del A[0]
    elif A == []:
        if B[0] + time <= limit:
            time += B[0]
            count += 1
            del B[0]
        elif B[0] + time > limit:
            break
    elif B == []:
        if A[0] + time <= limit:
            time += A[0]
            count += 1
            del A[0]
        elif A[0] + time > limit:
            break

print(count)

Submission status: Wrong Answer

Solution code:
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
kotei=k
ans=0
cnt=0
x=kotei
if kotei>=sum(a):
  cnt+=len(a)
else:
  x=kotei
  i=0
  while x>=0:
    x-=a[i]
    i+=1
    cnt+=1
  n=i
  ans=max(ans,cnt-1)

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

Submission status: Wrong Answer

Solution code:
import io
import sys

_INPUT = """\
1 4 120
100 1 1 1 1 1 1 1 1 1 1
80 10 10 10 10
"""
sys.stdin = io.StringIO(_INPUT)

####################################################################
import sys
def p(*_a):
  # return
  _s=" ".join(map(str,_a))
  #print(_s)
  sys.stderr.write(_s+"\n")
####################################################################

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


# 累積和
from itertools import accumulate
C = accumulate(A)
D = accumulate(B)

E = []
F = []
for c in C:
  if c > K: break
  E.append(c)

for d in D:
  if d > K: break
  F.append(d)

p(*E)
p(*F)

c = d = 0
ans = k = 0


for i, e in enumerate(E):
  for j, f in enumerate(F):
    if e + f > K: break
    ans = max(ans, i+1+j+1)

print(ans)

Submission status: Wrong Answer

Solution code:
N,M,K=map(int,input().split())
*A,=map(int,input().split())
*B,=map(int,input().split())
 
i,j=0,0
lA,lB=len(A),len(B)
 
while True:
  if K<=0:
    break
  if A[i]<B[j] and i<lA:
    K-=A[i]
    i+=1  
  elif B[j]<=A[i] and j<lB:
    K-=B[j]
    j+=1
  if i>=lA and K>0:
    while j<lB and K>0:
      K-=B[j]
      j+=1
  if j>=lB and K>0:
    while i<lA and K>0:
      K-=A[i]
      i+=1
 
count=max(0,i+j-1) if K<0 else i+j
print(count)

Submission status: Wrong Answer

Solution code:
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = a+b
c.sort()
ia = len(a)-1
ib = len(b)-1
ans = sum(a) + sum(b)
count = 0
if sum(c) <= k:
    print(n+m)
    exit()
if c[0] > k:
    print(0)
    exit() 

while True:
    if ia  == 0:
        ans -= b[ib]
        ib -= 1
    elif ib  == 0:
        ans -= a[ia]
        ia -= 1       
    elif a[ia] >= b[ib]:
        ans -= a[ia]
        ia -= 1
    else:
        ans -= b[ib]
        ib -= 1
    count += 1
    if ans <= k:
        break

ans = 0
ia = ib = 0
counta = 0 
while True:
    if ia  == len(a):
        ans += b[ib]
        ib += 1
    elif ib  == len(b):
        ans += a[ia]
        ia += 1       
    elif a[ia] >= b[ib]:
        ans += b[ib]
        ib += 1
    else:
        ans += a[ia]
        ia += 1
    if ans > k:
        break
    counta += 1
print(min(count,counta))

Submission status: Wrong Answer

Solution code:
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
res=k
a_read=0
b_read=0
u=0
while res>=0:
  if (a_read==n and b_read==m):
    u=1
    break
  elif (a_read==n):
    res-=b[b_read]
    b_read+=1
  elif (b_read==m):
    res-=a[a_read]
    a_read+=1
  elif (a[a_read]<=b[b_read]):
    res-=a[a_read]
    a_read+=1
  else:
    res-=b[b_read]
    b_read+=1
#if u==1:
  #print(n+m)
#else:
print(a_read+b_read-1)

Submission status: Wrong Answer

Solution code:
import sys
nums=input().split()
a=int(nums[0])
b=int(nums[1])
n=int(nums[2])
lista=input().split()
listb=input().split()

acount=0
bcount=0
count=0
time=0
atime=0
btime=0
for i in lista:
  val=int(i)
  if val+atime>n:
    break
  atime+=val
  acount+=1

count=acount
btime=atime
for j in listb:
  valb=int(j)
  if valb+btime>n:
    break
  btime+=valb
  count+=1
  bcount+=1

for i in range(1,acount):
  bcount=count-i
  btime-=int(lista[acount-i])
  for j in range(bcount,b):
    valb=int(j)
    if valb+btime>n:
      break
    btime+=valb
    bcount+=1
  if count<bcount:
    count=bcount
print(count)

Submission status: Wrong Answer

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

M = [[0] * 2 for _ in range(max(len(A), len(B)) + 1)]
for i, a in enumerate(A):
    M[i][0] = a
for i, b in enumerate(B):
    M[i][1] = b

ans = 0
todo = []
if M[0][0] <= K:
    todo.append((0, 0, M[0][0], set((0, 0))))
if M[0][1] <= K:
    todo.append((0, 1, M[0][1], set((0, 1))))
while todo:
    i, j, k, rt = todo.pop()
    ni = i
    nj = (j + 1) % 2
    if M[ni][nj] != 0 and not (ni, nj) in rt:
        nk = k + M[ni][nj]
        if nk <= K:
            nrt = set(rt)
            nrt.add((ni, nj))
            todo.append((ni, nj, nk, nrt))
            ans = max(ans, len(nrt) - 1)

    ni = i + 1
    nj = j
    if M[ni][nj] != 0 and not (ni, nj) in rt:
        nk = k + M[ni][nj]
        if nk <= K:
            nrt = set(rt)
            nrt.add((ni, nj))
            todo.append((ni, nj, nk, nrt))
            ans = max(ans, len(nrt) - 1)

print(ans)

Submission status: Wrong Answer

Solution code:

def count_book(NMK,A,B):
  i=0
  j=0
  sum = 0
  count = 0
  I = len(A)
  J = len(B)
  while sum<=NMK[2]:
    if i < I and j < J:
      if A[i]<B[j]:
        sum = sum + A[i]
        print(A[i])
        i = i + 1
        if sum<=NMK[2]:
         count = count + 1
      else:
        sum = sum + B[j]
        print(B[j])
        j = j + 1
        if sum<=NMK[2]:
         count = count + 1
 
    if i < I and j >= J:
      sum = sum + A[i]
      print(A[i])
      i = i + 1
      if sum<=NMK[2]:
        count = count + 1

    if i >= I and j < J:
      sum = sum + B[j]
      print(B[j])
      j = j + 1
      if sum<=NMK[2]:
        count = count + 1

    if i >= I and j >= J:
      break
      
  print(count)

Submission status: Wrong Answer

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
ans = 0
while i < n and j < m and ((k - min(a[i], b[j])) >= 0):
    if a[i] <= b[j]:
        k -= a[i]
        i += 1
    else:
        k -= b[j]
        j += 1

    ans+=1

if i < n or j < m:
    if i < n and j >= m:
        while i < n and ((k - a[i]) >= 0):
            k -= a[i]
            i += 1
            ans += 1
    elif j < m and i >= n:
        while j < m and ((k - b[j]) >= 0):
            k -= b[j]
            j += 1
            ans += 1
    else:
        if a[i] <= b[j]:
            while i < n and ((k - a[i]) >= 0):
                k -= a[i]
                i += 1
                ans += 1

            while j < m and ((k - b[j]) >= 0):
                k -= b[j]
                j += 1
                ans += 1
        else:
            while j < m and ((k - b[j]) >= 0):
                k -= b[j]
                j += 1
                ans += 1
            while j < m and ((k - b[j]) >= 0):
                k -= b[j]
                j += 1
                ans += 1

print(ans)

Submission status: Wrong Answer

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

time = 0
a_cnt = 0
b_cnt = 0
disp = False
for _ in range(n+m):
    if a_cnt<n and b_cnt<m:
        if a[a_cnt]>b[b_cnt]:
            time += b[b_cnt]
            b_cnt += 1
        else:
            time += a[a_cnt]
            a_cnt += 1
    elif a_cnt>=n and b_cnt<m:
        time += b[b_cnt]
        b_cnt += 1
    elif b_cnt>=m and a_cnt<n:
        time += a[a_cnt]
        a_cnt += 1
    else:
        break
    
    
    if time == k:
        print(a_cnt+b_cnt)
        disp = True
        break
    elif time > k:
        print(a_cnt+b_cnt-1)
        disp = True
        break

if disp == False:
    print(a_cnt+b_cnt)

Submission status: Wrong Answer

Solution code:
N, M, K = map(int, input().split())
times_a = list(map(int, input().split()))
times_b = list(map(int, input().split()))
read_time = 0
count = 0
while read_time <= K:            
    if len(times_a) != 0 and len(times_b) != 0:
        if times_a[0] <= times_b[0] and (read_time + times_a[0]) <= K:
            read_time += times_a[0]
            times_a.pop(0)
            count += 1
        else:
            if (read_time + times_b[0]) <= K:
                read_time += times_b[0]
                times_b.pop(0)
                count += 1
            else:
                break
    elif len(times_a) == 0 and len(times_b) != 0 and (read_time + times_b[0]) <= K:
        #bから
        read_time += times_b[0]
        times_b.pop(0)
        count += 1
    elif len(times_b) == 0 and len(times_a) != 0 and (read_time + times_a[0]) <= K:
        # aから
        read_time += times_a[0]
        times_a.pop(0)
        count += 1
    else:
        break
print(count)

Submission status: Wrong Answer

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

result = 0
count = 0
for i in range(0, len(A)):
    temp[i][0] = A[i]
    if temp[i][0] <= K:
        result = max(result, temp[i][0])
        count = i + 1
    for j in range(1, len(B)):
        temp[i][j] = temp[i][j-1] + B[j]
        if temp[i][j] <= K:
            result = max(result, temp[i][j])
            count = i + j + 2
print(count)

Submission status: Wrong Answer

Solution code:
def helper(i, j, a, b, k, dp):
	n = len(a)
	m = len(b)

	if dp[i][j][k-1] != -1:
		return dp[i][j][k-1]
	# if i == n:
	# 	dp[i][j][k-1] = 0
	# 	s = 0
	# 	for jj in range(j, m):
	# 		if b[jj] + s > k:
	# 			return dp[i][j][k-1]
	# 		dp[i][j][k-1] += 1
	# 		s += b[jj]
	# 	return dp[i][j][k-1]
	# if j == m:
	# 	dp[i][j][k-1] = 0
	# 	s = 0
	# 	for ii in range(i, n):
	# 		if a[ii] + s > k:
	# 			return dp[i][j][k-1]
	# 		dp[i][j][k-1] += 1
	# 		s += a[ii]
	# 	return dp[i][j][k-1]

	# if min(a[i], b[j]) > k:
	# 	dp[i][j][k-1] = 0
	# 	return dp[i][j][k-1]
	dp[i][j][k-1] = 0
	# print (i, j)
	if i < n:
		if a[i] < k:
			dp[i][j][k-1] = max(dp[i][j][k-1], helper(i+1, j, a, b, k-a[i], dp) + 1)
	if j < m:
		if b[j] < k:
			dp[i][j][k-1] = max(dp[i][j][k-1], helper(i, j+1, a, b, k-b[j], dp) + 1)
	return dp[i][j][k-1]

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

dp = [[[-1 for _ in range(k)] for _ in range(m+1)] for _ in range(n+1)]

if k >= sum(a) + sum(b):
	print (m+n)
else:
	r = helper(0, 0, a, b, k, dp)
	print (r)
# else:
# 	r = 0
# 	stack = [(0, 0, k)]
# 	dic = {}
# 	dic[(0, 0)] = k

# 	while len(stack) != 0:
# 		i, j, kk = stack.pop()
# 		if i == n:
# 			# if j < m:
# 			# 	if b[j] > kk:
# 			# 		r = max(r, i + j)
# 			# 		continue
# 			# 	if b[j] <= kk:
# 			# 		if (i, j+1) not in dic:
# 			# 			stack.append((i, j+1, kk - b[j]))
# 			# 			dic[(i, j+1)] = kk - b[j]
# 			# 		elif (kk - b[j]) > dic[(i, j+1)]:
# 			# 			stack.append((i, j+1, kk - b[j]))
# 			# 			dic[(i, j+1)] = kk - b[j]
# 			while j < m:
# 				if b[j] > kk:
# 					r = max(r, i + j)
# 					break
# 				if (i, j+1) in dic and dic[(i, j+1)] > kk - b[j]:
# 					break
# 				dic[(i, j+1)] = kk - b[j]
# 				kk -= b[j]
# 				j += 1
					


# 		elif j == m:
# 			# if i < n:
# 			# 	if a[i] > kk:
# 			# 		r = max(r, i+j)
# 			# 		continue
# 			# 	if a[i] <= kk:
# 			# 		if (i+1, j) not in dic:
# 			# 			stack.append((i+1, j, kk - a[i]))
# 			# 			dic[(i+1, j)] = kk - a[i]
# 			# 		elif (kk - a[i]) > dic[(i+1, j)]:
# 			# 			stack.append((i+1, j, kk - a[i]))
# 			# 			dic[(i+1, j)] = kk - a[i]
# 			while i < n:
# 				if a[i] > kk:
# 					r = max(r, i + j)
# 					break
# 				if (i+1, j) in dic and dic[(i+1, j)] > kk - a[i]:
# 					break
# 				dic[(i+1, j)] = kk - a[i]
# 				kk -= a[i]
# 				i += 1
# 		else:
# 			if min(a[i], b[j]) > kk:
# 				r = max(r, i + j)
# 			if a[i] <= kk:
# 				if (i+1, j) not in dic:
# 					stack.append((i+1, j, kk-a[i]))
# 					dic[(i+1, j)] = kk - a[i]
# 				elif dic[(i+1, j)] < (kk - a[i]):
# 					stack.append((i+1, j, kk-a[i]))
# 					dic[(i+1, j)] = kk - a[i]
# 			if b[j] <= kk:
# 				if (i, j+1) not in dic:
# 					stack.append((i, j+1, kk - b[j]))
# 					dic[(i, j+1)] = kk - b[j]
# 				elif dic[(i, j+1)] < (kk - b[j]):
# 					stack.append((i, j+1, kk - b[j]))
# 					dic[(i, j+1)] = kk - b[j]

# 	print (str(r))

Submission status: Wrong Answer

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

ans = 0
def dfs(a, b, k):
    global ans

    if k < 0:
        return False

    if a == N and b == M:
        return True

    if a < N and k >= A[a] and dfs(a + 1, b, k - A[a]):
        ans += 1
        return True

    if b < M and k >= B[b] and dfs(a, b + 1, k - B[b]):
        ans += 1
        return True

    return True

dfs(0, 0, K)
print(ans)

Submission status: Wrong Answer

Solution code:
N, M, K = map(int, input().split())
A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))
A_Read = 0
B_Read = 0
A_Finish = 0
B_Finish = 0
ans = 0
while K >= 0:
    if A_Finish == 1 and B_Finish == 1:
        break
    elif A_Finish == 1:
        K -= B_list[B_Read]
        # print(B_list[B_Read])
        B_Read += 1
        if B_Read == M:
            B_Finish = 1
            ans += 1
            break
    elif B_Finish == 1:
        K -= A_list[A_Read]
        # print(A_list[A_Read])
        A_Read += 1
        if A_Read == N:
            A_Finish = 1
            ans += 1
            break
    else:
        if A_list[A_Read] <= B_list[B_Read]:
            K -= A_list[A_Read]
            # print(A_list[A_Read])
            A_Read += 1
            if A_Read == N:
                A_Finish = 1
        else:
            K -= B_list[B_Read]
            # print(B_list[B_Read])
            B_Read += 1
            if B_Read == M:
                B_Finish = 1
    # print(K)
    if K < 0:
        break
    elif K == 0:
        ans += 1
        break
    # print(K)
    ans += 1
print(ans)

Submission status: Wrong Answer

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

L = [float("inf") for _ in range(N+M+1)]

c = 0
a = 0
b = 0

for k in range(len(A)):
    a += A[k]
    A[k] = a
for k in range(len(B)):
    b += B[k]
    B[k] = b

AA = [0] + A

BB = [0] + B

L[0] = 0
L[1] = min(AA[1], BB[1])
L[2] = AA[1]+BB[1]


for k in range(N+M, 0, -1):
    if k <= min(N, M):
        for l in range(k+1):
            if AA[l] + BB[k-l] < L[k]:
                L[k] = AA[l] + BB[k-l]
                if L[k] <= K:
                        print(k)
                        exit()
    if k > min(M,N):
        if M > N:
            for j in range(max(0, k-M), N+1):
                if L[k] > AA[N - j] + BB[k-j]:
                    L[k] = AA[j] + BB[k-j]
                    if L[k] <= K:
                        print(k)
                        exit()

                    
        else:
            for j in range(max(0, k - N), M+1):
                if L[k] > AA[k-j] + BB[j]:
                    L[k] = AA[k-j] + BB[j]
                    if L[k] <= K:
                        print(k)
                        exit()

if N+M <3:
    for k in range(3):
        if L[2-k] <=K:
            print(k)
            break
else:
    print(0)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
from collections import deque
def main():
    N, M, K = map(int, input().split())
    A = deque(list(map(int, input().split())))
    B = deque(list(map(int, input().split())))
    ans = 0
    a_is_available = True
    b_is_available = True
    while K >= 0:
        lenA = len(A)
        lenB = len(B)
        if lenA == 0: a_is_available = False
        if lenB == 0: b_is_available = False
        if not a_is_available and not b_is_available:
            ans += 1
            break
        if a_is_available:a = A.popleft()
        if b_is_available:b = B.popleft()
        if a_is_available and not b_is_available:
            if a > K:
                break
            K -= a
        elif not a_is_available and b_is_available:
            if b > K:
                break
            K -= b
        elif a <= b:
            if a > K:
                continue
            K -= a
            B.appendleft(b)
        else:
            if b > K:
                continue
            K -= b
            A.appendleft(a)
        if K < 0:
            break
        ans += 1
    ans = min(N+M, ans)
    ans = max(0, ans)
    print(ans)
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
S = 0
a = 0
for i in range(N):
    S += A[i]
    a = i
    if S > K:
        a = i - 1
        S -= A[i]
        break
j = 0
n = 0
for i in range(a, -1, - 1):
    while True:
        if S + B[j] <= K:
            S = S + B[j]
            if n < i + 1 + j + 1:
                n = i + j + 2
            if j < M - 1:
                j += 1
            else:
                break
        else:
            S -= A[i]
            break
print(n)

Submission status: Wrong Answer

Solution code:
from decimal import Decimal

def main():
  M, N, K = map(int, input().split())
  A = list(map(int, input().split()))
  B = list(map(int, input().split()))
  spent_time = 0
  count = 0
  AB = A + B
  AB.sort()
  
  while(True):
    if count == M + N:
      break
    temp = AB[count]
    #print(temp, spent_time)
    if spent_time + temp >= K:
      break
    spent_time = spent_time + temp
    count += 1
  
  #print('----')
  #print(spent_time)
  print(count)
  
main()

Submission status: Wrong Answer

Solution code:
import bisect
import itertools
N,M,K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
k = 0
for i in range(M):
    index = bisect.bisect_left(A,B[i],k+1)
    A.insert(index, B[i])
    k = index
S = 0
if K>= sum(A):
    print(N+M)
else:
    for d, j in enumerate(A):
        S += j
        if S >=K:
            print(d)

Submission status: Wrong Answer

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

a=N
b=M
count =0

while K != "sf":
    if a ==0 and b==0:
        break
    
    if a >0 and b > 0:
        if A[N-a] < B[M-b]:
            count += 1
            K -= A[N-a]
            a -=1
            
        elif A[N-a] > B[M-b]:
            count += 1
            K -=  B[M-b]
            b -=1
            
        elif A[N-a] == B[M-b]:
            if a-1>=0 and b-1 >=0:
                if A[N-a] <= B[M-b]:
                    count += 1
                    K -= A[N-a]
                    a -=1
            
                elif A[N-a] > B[M-b]:
                    count += 1
                    K -=  B[M-b]
                    b -=1
            elif a-1 >=0 and b-1 <0:
                count += 1
                K -= A[N-a]
                a -=1
            elif b-1 >=0 and a-1 <0:
                count += 1
                K -= B[M-b]
                b -=1
            else:
                count += 1
                K -= A[N-a]
                a -=1
                
            
    elif a>0 and b ==0:
        count += 1
        K -= A[N-a]
        a -=1
    elif b>0 and a ==0:
        count += 1
        K -= B[M-b]
        b -=1
    
    #print(count,K,N-a,M-b)
    
    if K < 0:
        count -= 1
        break
    
print(count)

Submission status: Wrong Answer

Solution code:
n , m , k = map(int,input().split())
a_list = list(map(int,input().split()))
b_list = list(map(int,input().split()))
x = 0
num = 0
for i in range(n+m):
    if x <= k :
        num += 1
        if len(a_list) == 0:
            y = b_list.pop(0)
            x += y
        elif len(b_list) == 0:
            y = a_list.pop(0)
            x += y
        else :
            if a_list[0] <= b_list[0]:
                y = a_list.pop(0)
                x += y
            else :
                y = b_list.pop(0)
                x += y
if x > k :
  num -= 1
print(num)

Submission status: Wrong Answer

Solution code:
import sys
import math 
from collections import deque

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

i=0
j=0
ans=0
tmp=0
re=0
while True:
    if i==n and j==m:
        print(ans)
        sys.exit()
    elif i==n:
        re=b[j]
        j+=1
    elif j==m:
        re=A[i]
        i+=1
    else:
        q=A[i]
        u=b[j]
        re=min(q,u)
        if re == q:
            i+=1
        else:
            j+=1

    tmp+=re
    
    if tmp > k:
        print(ans)
        sys.exit()
    elif tmp ==k:
        ans+=1
        print(ans)
        sys.exit()
    ans+=1

Submission status: Wrong Answer

Solution code:
from collections import deque
import numpy as np
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
a=[sum(a[:i]) for i in range(n+1)]
b=[sum(b[:i]) for i in range(m+1)]
e=0
for i in range(n):
  if a[i]<=k:
    e=i+1
  if a[i]>k:
    break
f=0
for i in range(n):
  if b[i]<=k:
    f=i+1
  if a[i]>k:
    break    
m=0
for i in range(e):
  for j in range(f):
    if a[i]+b[j]<=k:
      m=max(m,i+j)

print(m)

Submission status: Wrong Answer

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

AL = [0]
for i in range(N):
    AL.append(AL[-1] + A[i])

BL = [0]
for i in range(M):
    BL.append(BL[-1] + B[i])

# del AL[0]
del BL[0]

# print(AL)
# print(BL)

ans = 0
from bisect import bisect_left
for i in range(1, N):
    zan = K - AL[i]
    if zan < 0:
        break
    # print(i, AL[i], zan)
    ind = bisect_left(BL, zan)
    if ind < M:
        # print(ind, BL)
        if BL[ind] == BL[ind-1]:
            ind += 1
    # print()
    # print(ans, i, ind)
    if ind == M:
        ans = max(ans, i + ind + 1)
    else:
        ans = max(ans, i + ind)
print(ans)