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())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

Sa = [0]*N
Sb = [0]*M
Sa[0] = A[0]
Sb[0] = B[0]
a = 0
b = 0
ans = 0
if Sa[0] > K:
    a = 0
else:
    for i in range(1, N):
        Sa[i] = Sa[i-1] + A[i]
        if Sa[i] > K:
            a = i
            break
    else:
        a = N

if Sb[0] > K:
    b = 0
else:
    for i in range(1, M):
        Sb[i] = Sb[i-1] + B[i]
        if Sb[i] > K:
            b = i
            break
    else:
        b = M

for i in range(a):
    temp = K - Sa[i]
    if temp < Sb[0]:
        ans = max(ans, i+1)
        continue
    elif temp >= Sb[b-1]:
        ans = max(ans, i+1+b)
        continue
    ok = 0
    ng = b
    t = (ok+ng)//2
    while ok+1 < ng:
        t = (ok+ng)//2
        if temp >= Sb[t]:
            ok = t
        else:
            ng = t
    ans = max(ans, i+1+ok+1)

print(ans)

Submission status: Wrong Answer

Solution code:
import bisect
import numpy

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

cost_a = numpy.insert(numpy.cumsum(a), 0, 0)
cost_b = numpy.insert(numpy.cumsum(b), 0, 0)

left_max = bisect.bisect_left(cost_a, k)
right_max = bisect.bisect_left(cost_b, k)

ans = 0
j = right_max - 1
for i in range(left_max):
    for j in range(j , 0, -1):
        if cost_a[i] + cost_b[j] <= k:
            ans  = i + j if ans < i + j else ans
            break
        else:
            pass
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()))

sumlistA = [0]
sumlistB = [0]
tmpA = 0
tmpB = 0
for i in range(N):
    tmpA += A[i]
    sumlistA.append(tmpA)
for i in range(M):
    tmpB += B[i]
    sumlistB.append(tmpB)

i = 0
while sumlistA[i] <= K: 
        if i == N:
            a = N
            break
        a = i
        i += 1
tmp = 0
#Aは0からa冊
#a冊の時
k = K-sumlistA[a]
j = 0
while sumlistB[j] <= k:
    if j == M:
        b = M
        break
    b = j
    j += 1
#b冊読める。B[b-1]まで読んだ
r = k-sumlistB[b] #端数
tmp = a+b
for i in range(a):
    #a-i-1冊目までの時
    r += A[a-i-1]
    j = b
    while r >= 0:
        if j == M:
            break
        r -= B[j]
        j += 1
    #やりすぎ戻す
    r += B[j-1]
    j -= 1
    if j > b+1:
        tmp = a+j

print(tmp)

Submission status: Wrong Answer

Solution code:
import sys

rm = lambda: map(int, sys.stdin.buffer.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))
inf = float('inf')

n, m, k = rm()
a = rl() + [0]
b = rl() + [0]

cnt = 0
lena = len(a)
lenb = len(b)
dp = [[inf]*(lenb + 10) for _ in range(lena + 10)]
dp[0][0] = 0
dp[0][1] = b[0]
dp[1][0] = a[0]

for i in range(lena):
    for j in range(lenb):
        dp[i][j+1] = dp[i][j] + b[j]
        dp[i+1][j] = dp[i][j] + a[i]
        if dp[i][j+1] <= k or dp[i+1][j] <= k:
            cnt = max(i+j, cnt)

print(cnt)

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

ai =0
bi=0
st =0
c = 0
while True:
  if ai == N:
    if bi == M:
      break
    else:
      st += b[bi]
      if st > K:
        break
      bi += 1
      c +=1
  elif bi == M:
    if ai == N:
      break
    else:
      st += a[ai]
      if st > K:
        break
      ai += 1
      c += 1
  elif a[ai] > b[bi]:
    st += b[bi]
    if st > K:
      break
    bi +=1
    c +=1
  elif a[ai] < b[bi]:
    st += a[ai]
    if st > K:
      break
    ai +=1
    c +=1
  elif a[ai] == b[bi]:
    tmpst = st
    tmpc =0
    tmpai = ai
    tmpbi = bi
    while True:
      tmpst +=a[tmpai]
      if tmpai < N-1:
        tmpai+=1
      if tmpbi < M-1:
        tmpbi+=1
      if tmpst > K:
        st += a[ai]
        if st > K:
          print(c)
          exit()
        ai +=1
        c +=1
        #print(c+tmpc,'ja')
        #exit()
      if a[tmpai] > b[tmpbi]:
          st += b[bi]
          if st > K:
              print(c)
              exit()
          bi += 1
          c += 1
          break
      elif a[tmpai] < b[tmpbi]:
          st += a[ai]
          if st > K:
              print(c)
              exit()
          ai += 1
          c += 1
          break
      if tmpai == N-1 and tmpbi == M-1:
        while True:
          if ai == N:
              print(c)
              exit()
          st += a[ai]
          if st > K:
            print(c)
            exit()
          ai += 1
          c += 1
          st += b[bi]
          if st > K:
            print(c)
            exit()
          bi += 1
          c += 1

     # print(tmpst)
      tmpc += 1
     # print(tmpai, tmpbi)
print(c)

Submission status: Wrong Answer

Solution code:
k = [int(x) for x in input().split()][2]
ab = [int(x) for x in input().split()] + [int(x) for x in input().split()]
O = 0
ab.sort()
for i in range(len(ab)):
    k -= ab[i]
    if k < 0:
        break
    O +=1
print(O)

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 += [10**11]
B += [10**11]

ans = 0
T = 0
a, b = 0, 0
while True:
    if A[a] <= B[b]:
        T += A[a]
        a += 1
    else:
        T += B[b]
        b += 1
    if T > K or ans == (N+M):
        break
    ans += 1
    
print(ans)

Submission status: Wrong Answer

Solution code:
n,m,k=map(int,input().strip().split())
a=list(map(int,input().strip().split()))
b=list(map(int,input().strip().split()))
total=0
i=0
j=0
ans=0
while(i<n and j<m):
    if(a[i]==b[j] and (a[i]+b[j]+total)<=k):
        total+=(a[i]*2)
        ans+=2
        i+=1
        j+=1
    elif(a[i]==b[j] and (a[i]+total)<=k):
        total+=a[i]
        ans+=1
        p=i+1
        q=j+1
        flag=0
        while(p<n and q<m):
            if(a[p]<b[q]):
                i+=1
                flag=1
                break
            elif(b[q]<a[p]):
                j+=1
                flag=1
                break
            p+=1
            q+=1
        if(flag==0):
            i+=1
    elif(a[i]<b[j] and (a[i]+total)<=k):
        total+=a[i]
        ans+=1
        i+=1
    elif(b[j]<a[i] and (b[j]+total)<=k):
        total+=b[j]
        ans+=1
        j+=1
    else:
        break
while(i<n):
    if((a[i]+total)>k):
        break
    total+=a[i]
    ans+=1
    i+=1
while(j<m):
    if((b[j]+total)>k):
        break
    total+=b[j]
    ans+=1
    j+=1
print(ans%1000000007)

Submission status: Wrong Answer

Solution code:
import bisect
N,M,K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
a=[0]*N
b=[0]*M
a[0]=A[0]
b[0]=B[0]
for i in range(N-1):
    a[i+1]=a[i]+A[i]
for i in range(M-1):
    b[i+1]=b[i]+B[i]
diff=2*10**9
count=0
for i in range(N):
    j=bisect.bisect_left(b,K-a[i])-1
    if j+1<M and 0<=K-(a[i]+b[j+1])<=diff:
        diff=K-(a[i]+b[j+1])
        count=max(count,(i+1)+(j+2))
    elif 0<=K-(a[i]+b[j])<=diff:
        diff=K-(a[i]+b[j])
        count=max(count,(i+1)+(j+1))
print(count)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

a = deque(list(map(int,input().split())))
b = deque(list(map(int,input().split())))
cnt = 0
total = 0

if a[0]==True and b[0]==True:
    while total+a[0] < k or total+b[0] < k:
        if len(a) > 0 and len(b) > 0:
            picked = min(a[0],b[0])
            if picked == a[0]:
                total += a.popleft()
                if len(a)==0 or len(b)==0:
                    break
                cnt += 1
            else:
                total += b.popleft()
                cnt += 1
elif a[0] and b[0]==False:
    while total + a[0] < k:
        total +=a.popleft()
        if len(a) == 0:
            break
        cnt += 1
else:
    while total + b[0] < k:
        total += b.popleft()
        if len(b) == 0:
            break
        cnt += 1
    
print(cnt)

Submission status: Wrong Answer

Solution code:
#import 
#=input()
#=int(input())
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
#=[list(map(int,input().split())) for _ in range()]

cnta = 0
suma = 0
i = 0
summa = 0
for p in range(n):
    summa += a[p]
if summa <= k:
    suma = summa
    cnta = n
else:
    while suma <= k:
        suma += a[i]
        cnta += 1
        if i == n-1:
            break
        else:
            i += 1
    cnta -= 1
    suma -= a[i]
sumb = 0
summb = 0
cntb = 0
j = 0
for u in range(m):
    summb += b[u]
if summb <= (k-suma):
    sumb = summb
    cntb = m
else:
    while (k - suma) >= sumb:
        sumb += b[j]
        cntb += 1
        if j == m-1:
            break
        else:
            j += 1
    sumb -= b[j-1]
    cntb -= 1
cnt = cnta + cntb
r = cnta
for q in range(r):
    dela = a[cnta-q-1]
    suma -= dela
    cnta -= 1
    nokori = k - suma - sumb
    w = 0
    if cntb == m:
        w = 1
    else:
        while nokori >= 0:
            sumb += b[cntb+w]
            nokori -= b[cntb+w]
            w += 1
            cntb += 1
            cntb -= 1
            sumb -= b[cntb+w-1]
    if cnt < cnta + cntb:
        cnt = cnta + cntb
print(cnt)

Submission status: Wrong Answer

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()))
C = N + M
cnt = 0
res = 0
A = deque(A)
B = deque(B)
for c in range(C):
    
    if A:
        a = A.popleft()
    else:
        a = 10**10
    if B:
        b = B.popleft()
    else:
        b = 10**10

    if a >= b:
        cnt += b
        A.appendleft(a)
    elif a < b:
        cnt += a
        B.appendleft(b)
    
    if cnt < K:
        res += 1
    else:
        break
print(res)

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

from itertools import accumulate
aa=list(accumulate(a))
bb=list(accumulate(b))

from bisect import bisect_left,bisect

kmax=0
for i in range(n):
    if k<aa[i]:
        break
    kk=k-aa[i]
#    k2=min(bisect(bb,kk),m-1)
    k2=bisect(bb,kk)
    if k2<m-1 and bb[k2+1]==kk:
        k2+=1
#    print(i,k2)
    kmax=max(kmax,k2+i+1)
    kmax=min(kmax,m+n)

print(kmax)

Submission status: Wrong Answer

Solution code:
N, M, K=(int(x) for x in input().split())
A_list = list(map(int, input().split()))
B_list = list(map(int, input().split()))

A_i = 0
B_i = 0

A_i_sub = 0
B_i_sub = 0

next_A = A_list[A_i]
next_B = B_list[B_i]

count_read = 0

while(K >= next_A or K >= next_B):
    if(next_A <= next_B and A_i < N):
        K = K - next_A
        count_read += 1
        if(A_i < N-1):
            A_i += 1
            next_A = A_list[A_i]
        elif(A_i == N-1):
            next_A = 100000000000
    elif(next_B < next_A and B_i < N):
        count_read += 1
        if(B_i < M-1):
            B_i += 1
            next_B = B_list[B_i]
        elif(B_i == M-1):
            next_B = 100000000000

print(count_read)

Submission status: Wrong Answer

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

from collections import deque

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

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

an = an
bn = bn

vMax = 10**9+10

import itertools

accum_a = [0]
accum_b = [0]



candidate = []

for a_i in range(0,len(an)):


    sumCandidate = accum_a[-1] + an[a_i]
    if sumCandidate > k:
        break
    accum_a.append(sumCandidate)

for b_i in range(0,len(bn)):
    i_ = accum_b[-1] + bn[b_i]
    if i_ > k:
        break
    accum_b.append(i_)


def Find(a, b, k1):
    if a[-1] + b[-1] <= k1:
        return len(a) - 1 + len(b) - 1

    if len(a) > len(b):
        rest = k - a[-1]
        for i in range(len(b)):
            if rest <= b[-i]:
                return len(a) - 1 + len(b) - 1 - i

    if len(b) > len(a):
        rest = k - b[-1]
        for i in range(len(a)):
            if rest <= a[-i]:
                return len(b) - 1 + len(a) - 1 - i

    return Find(a[0:len(a)-1], b[0:len(b)-1], k - min(a[-1], b[-1])) + 1


print(Find(accum_a, accum_b, k))

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()))
result = []
if sum(a)+sum(b) <= k:
  print(len(a)+len(b))
  exit()
while sum(result) < k:
  if not a:
    result.append(b.pop(0))
  if not b:
    result.append(a.pop(0))
  elif a and b:
    if a[0]==min(a[0],b[0]):
      result.append(a.pop(0))
    else:
      result.append(b.pop(0))
print(len(result)-1)

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

#n,m,k = random.randint(1,1000),random.randint(1,1000),random.randint(1,10**8)
##a = [random.randint(1,10**5) for i in range(n)]
#b = [random.randint(1,10**5) for i in range(m)]

def solv():
	global a,b,n,m,k
	akari = [0]*(n+1)
	bkari = [0]*(m+1)
	for i in range(n):
		akari[i+1] = akari[i] + a[i]

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

	def bin(ls,s,e,sikii):
		if e - s <= 1:
			return s
		mid = (s+e)//2
		if sikii < ls[mid]:return bin(ls,s,mid,sikii)
		else:return bin(ls,mid,e,sikii)

	ans = 0
	for i in range(n+1):
		time = akari[i]
		if k - time <= 0:break
		ref = bin(bkari,0,m+1,k-time)
		ans = max(ans,ref+i)
	return ans

def karisolv():
	global a,b,n,m,k
	akari = [0]*(n+1)
	bkari = [0]*(m+1)
	for i in range(n):
		akari[i+1] = akari[i] + a[i]

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

	ans = 0
	for i in range(n+1):
		time = akari[i]
		ref = 0
		if k - time <= 0:break
		cost = 0
		for j in range(m+1):
			if k - time - bkari[j] >= 0:
				ans = max(ans,j+i)
		
	return ans

def makedata():
	global a,b,n,m,k
	n,m,k = random.randint(1,10000),random.randint(1,10000),random.randint(1,1000000)
	a = [random.randint(1,10**5) for i in range(n)]
	b = [random.randint(1,10**5) for i in range(m)]
import sys
def main():
	for i in range(1000000):
		makedata()
		kari = karisolv()
		kotae = solv()
		if kari != kotae:
			print(n,m,k,a,b)
			sys.exit()

print(solv())

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()))
t = 0
c = 0
if sum(a) + sum(b) <= k:
    print(n+m)
    quit()
for i in range(min(n, m)):
    if t + a[i]> k:
        break
    t += a[i]
    c += 1
    if t + b[i]> k:
        break
    t += b[i]
    c += 1
if t < k:
    if max(n, m) == n:
        d = a
        e = b
    else:
        d = b
        e = a
    for i in range (len(e), len(d)-1):
        if t + d[i] > k:
            break
        t += d[i]
        c += 1
    print(c)
else:
    print(c)

Submission status: Wrong Answer

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

time = 0
count = 0

while(True):
  if (len(Alist) != 0) and (len(Blist) != 0):
    if Blist[0] <= Alist[0]:
      time += Blist.pop(0)
      if time > K:
        break
      count += 1  
    else:
      time += Alist.pop(0)
      if time > K:
        break
      count += 1
  
  elif (len(Alist) == 0) and (len(Blist) != 0):
    if time+Blist[0] > K:
      break
    else:
      time += Blist.pop(0)
      count += 1
  
  elif (len(Alist) != 0) and (len(Blist) == 0):
    if time+Alist[0] >= K:
      break
    else:
      time += Alist.pop(0)
      count += 1
  
  else:
    break
    
print(count)

Submission status: Wrong Answer

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

def one_int():
    return int(input())

def one_str():
    return input()

def many_int():
    return list(map(int, input().split()))

N, M, K = input_int()
A = many_int()
B = many_int()


import itertools as it
import bisect

A_it = list(it.accumulate(A))
B_it = list(it.accumulate(B))

A_index = bisect.bisect_left(A_it, K)
B_index = bisect.bisect_left(B_it, K)

A_it = A_it[:A_index]
B_it = B_it[:B_index]

maxs = 0
count=0
for i in A_it[::-1]:
    if K-i < 0:
        continue
    now = K-i
    B_index = bisect.bisect_right(B_it, now)
    
    maxs = max(maxs, B_index + A_index - count)
    count+=1

if len(A_it)==0:
    print(B_index)
elif len(B_it)==0:
    print(A_index)
else:
    print(maxs)

Submission status: Wrong Answer

Solution code:
n,m,k=map(int,input().split())
x=[int(x) for x in input().split()]
y=[int(x) for x in input().split()]
c=0
cost=0
f,t=0,0
for i in range(n):
    if i<n and cost<k and cost+x[i]<=k and f==0:
        cost+=x[i]
        c+=1
    else:f=1
    if i<m and  cost<k and cost+y[i]<=k and t==0:
        cost+=y[i]
        c+=1
    else:t=1
i+=1
if m>n and t==0:
    while(i<(m)):
        if cost+y[i]<=k and cost<k:
            cost+=y[i]
            c+=1
        i+=1
print(c)

Submission status: Wrong Answer

Solution code:
import sys
n,m,k=map(int,input().split())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
prog=0
a_idx=0
b_idx=0

while a_idx<n:
    if a[a_idx]+prog>k:
        break
    prog+=a[a_idx]
    a_idx+=1

if a_idx==n:
    while b_idx<m:
        if b[b_idx]+prog>k:
            break
        prog+=b[b_idx]
        b_idx+=1

mx=a_idx+b_idx

if mx==n+m:
    print(mx)
    sys.exit()

for i in range(a_idx):
    while b_idx<m and b[b_idx]+prog<=k:
        prog+=b[b_idx]
        b_idx+=1
    if a_idx+b_idx>mx:
        mx=a_idx+b_idx
    if b_idx==m:
        break
    a_idx-=1
    prog-=a[a_idx]

print(mx)

Submission status: Wrong Answer

Solution code:
from collections import deque
import bisect
N,M,K = list(map(int, input().split()))
As = list(map(int, input().split()))
Bs = list(map(int, input().split()))

if(sum(As) + sum(Bs) <= K):
    print(N + M)
    exit()


Asruisekiwa = []
Bsruisekiwa = []

Asruisekiwa.append(As[0])
Bsruisekiwa.append(Bs[0])

for i, j in enumerate(As[1:]):
    Asruisekiwa.append(Asruisekiwa[-1]+j)

for i, j in enumerate(Bs[1:]):
    Bsruisekiwa.append(Bsruisekiwa[-1] + j)

#print(Asruisekiwa)
#print(Bsruisekiwa)

ans = 0
#print(Asruisekiwa)
#print(Bsruisekiwa)

if(Asruisekiwa[0] >= Bsruisekiwa[0]):
    tmp = Asruisekiwa
    Asruisekiwa = Bsruisekiwa
    Bsruisekiwa = tmp


Bsruisekiwa.insert(0,0)

#print(Asruisekiwa)
#print(Bsruisekiwa)


for index, i in enumerate(Asruisekiwa):

    if(i > K):
        break

    tmp = i
    maxcheck = K - i


    hoge = bisect.bisect_left(Bsruisekiwa, maxcheck)

    ans = max(ans, index + hoge)

#        print(ans, tmp, hoge, maxcheck)

print(ans)

Submission status: Wrong Answer

Solution code:
n, m, k = map((lambda x: int(x)), input().split())
a_list = list(map((lambda x: int(x)), input().split()))
b_list = list(map((lambda x: int(x)), input().split()))
read_cnt = 0
a_cnt = 0
b_cnt = 0
k = 0
a_list.append(200001)
b_list.append(200001)
val = 'aaaaa'
ans = 'bbbbb'
while k > 0:
    if a_list[a_cnt] > b_list[b_cnt]:        
#         pdb.set_trace()
        k -= b_list[b_cnt]
        b_cnt += 1
        read_cnt += 1
    elif a_list[a_cnt] == b_list[b_cnt]:
        if a_list[a_cnt] == 200001:
            break
        else:
            val, ans = compare_list(a_list, b_list, a_cnt, b_cnt)
            if ans == 'a':
                k -= a_list[a_cnt]
                a_cnt += 1
                read_cnt += 1                
            else:
                k -= b_list[b_cnt]
                b_cnt += 1
                read_cnt += 1
    else:
        k -= a_list[a_cnt]
        a_cnt += 1
        read_cnt += 1

print(read_cnt)        
        
def compare_list(a, b, a_cnt, b_cnt):
    dummy_a_cnt = a_cnt
    dummy_b_cnt = b_cnt
    while a_list[dummy_a_cnt] == b_list[dummy_b_cnt]:
        dummy_a_cnt += 1
        dummy_b_cnt += 1
    if a_list[dummy_a_cnt] > b_list[dummy_b_cnt]:
        answer = 'b'
        return b_list[b_cnt], answer
    else:
        answer = 'a'
        return a_list[a_cnt], answer

Submission status: Wrong Answer

Solution code:
A=[]
B=[]
books = 0
time = 0
n, m, k = map(int,input().split())
i = 0
j = 0
A = list(map(int,input().strip().split()))[:n] 
B = list(map(int,input().strip().split()))[:m] 
while not(time > k) :
    if ((i<n and j<m and A[i]<B[j]) or (i<n and not(j<m))):
        time = time+A[i]
        i=i+1
        books=books+1
    elif ((i<n and j<m and (not(A[i]<B[j]))) or (j<m and not(i<n))):
        time = time+B[j]
        j=j+1
        books=books+1
print(books)

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

a=[]
while True:
  if len(A)==0 or len(B)==0:
    break
  if A[0]<B[0]:
    a.append(A[0])
    A.pop(0)
  elif A[0]>B[0]:
    a.append(B[0])
    B.pop(0)
  elif A[0]==B[0]:
    a.append(A[0])
    a.append(B[0])
    A.pop(0)
    B.pop(0)
if len(A)!=0:
  a.extend(A)
elif len(B)!=0:
  a.extend(B)

books=0
count =0
for i in range(N+M):
  books += a[i]
  if books<=K:
    count+=1
  else:
    print(count)
    break

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()))
count = 0
ai = 0
bi = 0
while True:
  if (n != ai and m != bi and a[ai] <= b[bi]) or (n != ai and m == bi):
    k -= a[ai]
    ai += 1
  elif (n != ai and m != bi and a[ai] > b[bi]) or (n == ai and m != bi):
    k -= b[bi]
    bi += 1
  else:
    break
  if k < 0:
    break
  count += 1
print(count)

Submission status: Wrong Answer

Solution code:
import math
n,m,k = map(int,input().split())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
a.append(10**10)
b.append(10**10)

ans=0
time=0
for i in range(n+m):
    ans += 1    
    if a[0]<=b[0]:
      time+=a.pop(0)
    else:
      time+=b.pop(0)
      
    if time>k:
      break
    
         
print(ans-1)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

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

A = defaultdict(lambda:10**9)
B = defaultdict(lambda:10**9)

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

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

A.append(10**9+1)
B.append(10**9+1)

re = i = 0


while(m > 0 and i < a+b):
    s = min(A[0],B[0])
    if(A[0] < B[0]):
        del A[0]
    else:
        del B[0]
    m -= s
    if(m >= 0):
        re += 1
        i+=1
print(re)

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:
        time0 += Ai[i]
        if time0 > time_:
            time0 -= Ai[i]
            break
        acount += 1
    except IndexError:
        break
atime = time0
time0 = 0
for i in range(10000000000000000):
    try:
        time0 += Bi[i]
        if time0 > time_:
            time0 -= Bi[i]
            break
        bcount += 1
    except IndexError:
        break
btime = time0
allc = bcount
if bcount > acount:
    hic = bcount
else:
    hic = acount
# AiからBiに移す
cip = 0
for i in range(acount):
    ci = i + cip
    try:
        while time0 + Ai[ci] <= time_:
            time0 += Ai[ci]
            allc += 1
            ci += 1
            cip += 1
            if hic < allc:
                hic = allc
    except IndexError:
        pass
    try:
        time0 -= Bi[bcount - 1]
        bcount -= 1
        allc -= 1
    except IndexError:
        pass
    try:
        while time0 + Ai[ci] <= time_:
            time0 += Ai[ci]
            allc += 1
            ci += 1
            cip += 1
            if hic < allc:
                hic = allc
    except IndexError:
        break

print(hic)

Submission status: Wrong Answer

Solution code:
#n = int(input())
n,m,k = map(int,input().split())
a_L = list(map(int,input().split()))
b_L = list(map(int,input().split()))

a_L = list(reversed(a_L))
b_L = list(reversed(b_L))


dp = [float("inf")] * (n+m+1)
dp[0] = 0
if min(a_L[-1],b_L[-1]) > k:
    print(0)
    exit()

ans = 0
for i in range(1,m+n+1):
    try:
        if a_L[-1] >= b_L[-1]:
            dp[i] = dp[i-1] + b_L[-1] 
            b_L.pop()
        else:
            dp[i] = dp[i-1] + a_L[-1]
            a_L.pop()

    except:
        try:
            dp[i] = dp[i-1] + a_L[-1]
            a_L.pop()
        except:
            dp[i] = dp[i-1] + b_L[-1]
            b_L.pop()

ans = 0

for i in dp:

    if i > k:
        print(ans-1)
        exit()
    ans += 1

print(ans-1)

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

ia = 0
ib = 0
book = 0
minute = 0

while minute < k:
    if a[0] > b[0]:
        minute += b[0]
        book += 1
        if minute > k:
            book -= 1
        ib += 1
        if ib == m:
            ib = m - 1
            b[ib] = float('inf')
    else:
        minute += a[0]
        book += 1
        if minute > k:
            book -= 1
        ia += 1
        if ia == n:
            ia = n - 1
            a[ia] = float('inf')

print(book)

Submission status: Wrong Answer

Solution code:
def main():
    J = [int(_) for _ in input().split()]
    N = J[0]
    M = J[1]
    K = J[2]
    A = [int(_) for _ in input().split()]
    B = [int(_) for _ in input().split()]

    if A[0] > K and B[0] > K:
        print(0)
        return

    Asum = sum(A)
    Bsum = sum(B)

    if Asum + Bsum <= K:
        print(N+M)
        return


    Anum = 0
    Bnum = 0
    Asum = 0
    Bsum = 0
    MaxNum = 0
    if N>M:
        MaxNum = N
    else:
        MaxNum = M

    for i in range(0, MaxNum):
        if i < N and Asum + A[i] < K:
            Asum = Asum + A[i]
            Anum = i + 1
        if i < M and Bsum + B[i] < K:
            Bsum = Bsum + B[i]
            Bnum = i + 1

    maxval = 0
    if Bnum > Anum:
        Asum = 0
        Anum = 0
        maxval = Bnum
        for i in reversed(range(0, Bnum)):
            tmpNum = i + 1
            tmpSum = Bsum
            for j in range(0, N):
                if tmpSum + A[j] <= K:
                    tmpNum = tmpNum + 1
                    tmpSum = tmpSum + A[j]
            if maxval < tmpNum:
                maxval = tmpNum
            Bsum = Bsum - B[i]
    else:
        Bsum = 0
        Bnum = 0
        maxval = Anum
        for i in reversed(range(0, Anum)):
            tmpNum = i + 1
            tmpSum = Asum
            for j in range(0, M):
                if tmpSum + B[j] <= K:
                    tmpNum = tmpNum + 1
                    tmpSum = tmpSum + B[j]
            if maxval < tmpNum:
                maxval = tmpNum
            Asum = Asum - A[i]

    print(maxval)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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()))
if sum(A) + sum(B) <= K:
    print(N + M)
else:
    A = deque(A)
    B = deque(B)
    time = 0
    ans = 0
    while time <= K:
        if len(A) == 0:
            time += B[0]
            ans += 1
            B.popleft()
        elif len(B) == 0:
            time += A[0]
            ans += 1
            A.popleft()
        else:
            if A[0] <= B[0]:
                time += A[0]
                ans += 1
                A.popleft()
            else:
                time += B[0]
                ans += 1
                B.popleft()
    print(ans-1)

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
cost = 0
b_i = 0
if not (a[0] >= k and b[0] >= k):
  while 1:
    if ans >= n:
      if ans-n>=m:
        break
      _next = b[b_i]
      b_i += 1
    else:
      _next = a[ans]
    if (cost + _next) <= k:
      ans += 1
      cost += _next
    else:
      b_i -= 1
      break

  max_ans = ans
  if b_i == 0 or b_i == -1:
    for i in range(ans-1,-1,-1): # 2から
      ans -= 1
      cost -= a[i]
      while 1:
        if b_i >= m:
          break
        _next = b[b_i]
        if cost + _next > k:
          break
        cost += _next
        ans += 1
        b_i  += 1
      max_ans = max(max_ans, ans)
  else:
    b_i += 1
    for i in range(n-1,-1,-1):
      ans -= 1
      cost -= a[i]
      while cost < k and b_i < m:
        ans += 1
        cost += b[b_i]
        b_i += 1
      max_ans = max(max_ans, ans)

  if a[0] <= k:
    print(max_ans)
  else:
    ans = 0
    cost = 0
    _next = 0
    while 1:
      if ans >= m:
        break
      _next = b[ans]
      if cost + _next > k:
        break
      cost += _next
      ans += 1
    print(ans)
else:
  print(0)

Submission status: Wrong Answer

Solution code:
info = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]

N = list()
M = list()
N.append(0)
for i in range(info[0]):
  N.append(N[i]+A[i])
M.append(0)
for i in range(info[1]):
  M.append(M[i]+B[i])

t = info[0]
if N[int(info[0]/2)]+M[-1] > info[2]:
  t = int(info[0]/2)
  
ans = 0
for i in range(t, -1, -1):
  if N[i] > info[2] :
    continue
  if i+info[1] <= ans:
    break
  for j in range(info[1], -1, -1):
    if i+j <= ans:
      break
    if N[i]+M[j] <= info[2]:
      ans = i+j
      break
    
print(ans)