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:
import math
import sys
from collections import deque

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


count = 0
time = 0
#もし先にどっちかの玉が尽きた時の問題。
for i in range(10**6):
    #本が尽きていない。
    if len(A) != 0 and len(B) != 0:

        if A[0] <= B[0]:
            time = time + A[0]
            if time <= K:
                count = count + 1
                A.popleft()
            else:
                print(count)
                sys.exit()

        elif A[0] > B[0]:
            time = time + B[0]
            if time <= K:
                count = count + 1
                B.popleft()
            else:
                print(count)
                sys.exit()
    else:
        #どっかの本が尽きている。
        if len(A) == 0 and len(B) != 0:
            time = time + B[0]
            if time <= K:
                count = count + 1
                B.popleft()
            else:
                print(count)
                sys.exit()
        elif len(A) != 0 and len(B) == 0:
            time = time +A[0]
            if time <= K:
                count = count + 1
                A.popleft()
            else:
                print(sys.exit())
        elif len(A) == 0 and len(B) == 0:
            print(count)
            sys.exit()
               


print(count)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

n, m, k = map(int, input().split())
Alist = deque()
Blist = deque()
Alist.extend(list(map(int, input().split())))
Blist.extend(list(map(int, input().split())))

sum = 0
book = 0
while (True):
    if (len(Alist) == 0):
        while (True):
            if (len(Blist) == 0):
                print(book)
                sys.exit()
            if (k - Blist[0] >= 0):
                k -= Blist[0]
                Blist.popleft()
                book += 1
            else:
                print(book)
                sys.exit()
    elif(len(Blist) == 0):
        while (True):
            if (len(Alist) == 0):
                print(book)
                sys.exit()
            if (k - Alist[0] >= 0):
                k -= Alist[0]
                Alist.popleft()
                book += 1
            else:
                print(book)
                sys.exit()

    if (Alist[0] < Blist[0]):
        if (k - Alist[0] >= 0):
            k -= Alist[0]
            Alist.popleft()
            book += 1
            # print(k)
        else:
            break
    else:
        if (k - Blist[0] >= 0):
            k -= Blist[0]
            Blist.popleft()
            book += 1
            # print(k)
        else:
            break

print(book)

Submission status: Wrong Answer

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



Kc=K
bookc=0

Ai=0
Bi=0

while Kc>0:
    if Ai<N:
        if Bi<M:
            if A[Ai]<=B[Bi] and A[Ai]<Kc:
                bookc+=1
                Kc-=A[Ai]
                Ai+=1

            elif B[Bi]<Kc:
                bookc+=1
                Kc-=B[Bi]
                Bi+=1
            else:
                break
        elif A[Ai]<Kc:
            bookc+=1
            Kc-=A[Ai]
            Ai+=1
        else:
            break
    elif Bi<M:
        if B[Bi]<Kc:
            bookc+=1
            Kc-=B[Bi]
            Bi+=1
        else:
            break

print(bookc)

Submission status: Wrong Answer

Solution code:
def the_number(A, B):
    ia = 0
    ib = 0
    t= 0
    f = 0
    length = len(A) + len(B)

    for i in range(length):

        if len(A) and len(B) > 0:
            if A[0] > B[0]:
                t = t + B[0]
                f = f + 1
                B.remove(B[0])
                if t > K:
                    f = f -1
                    break
            else:
                t = t + A[0]
                f = f + 1
                A.remove(A[0])
                if t > K:
                    f = f -1
                    break

        elif len(A) == 0:
                t = t + B[0]
                f = f + 1
                B.remove(B[0])   
                if t > K:
                    f = f -1
                    break
        elif len(B) == 0:
                t = t + A[0]
                f = f + 1
                A.remove(A[0])
                if t > K:
                    f = f -1
                    break

    return f

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

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
for i,j in zip(range(1,len(lk)+1), res):
    rmax = max(rmax, i+j)
print(int(rmax))

Submission status: Wrong Answer

Solution code:
import itertools
N, M, K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
Ac = list(itertools.accumulate(A))
Bc = list(itertools.accumulate(B))
amax, bmax = 0, 0
for i in range(len(A)):
    if Ac[i] <= K:
        amax = i + 1
    else: break
if amax == 0:
    At = 0
else:
    At = Ac[amax - 1]
for i in range(len(B)):
    if Bc[i] + At <= K:
        bmax = i + 1
    else: break
ans = amax + bmax
if amax > 0:
    for i in range(1, amax):
        anow = amax - i
        At = Ac[anow - 1]
        for j in range(bmax-1, len(B)):
            if Bc[j] + At <= K:
                bmax = j + 1
            else: break
        ans = max(ans,anow+bmax)
        
for i in range(len(B)):
    if Bc[i] <= K:
        bmax = i + 1
    else: break
if bmax == 0:
    Bt = 0
else:
    Bt = Bc[bmax - 1]
for i in range(len(A)):
    if Ac[i] + Bt <= K:
        amax = i + 1
    else: break
if bmax > 0:
    for i in range(1, bmax):
        bnow = bmax - i
        Bt = Bc[bnow - 1]
        for j in range(amax-1, len(A)):
            if Ac[j] + Bt <= K:
                amax = j + 1
            else: break
        ans = max(ans,bnow+amax)
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

cnt = 0
cnt_max = 0

t = 0

#aの本を全部読む場合を考える
#bを1冊ずつ増やしたときに最大になるかどうかを判定する

c = []

for i in a:
  t += i
  if t <= k:
    c.append(i)
    cnt += 1
  else:
    t -= i
    break

#print(c)
cnt_max = cnt
tmp_cnt = cnt

t_max = t

for i in range(len(c) + 1):
  d = []
  
  if i == 0:
    pass
  else:
    t_max -= c[i-1]
    tmp_cnt -= 1
    
  #print(i, t_max, tmp_cnt)
  
  t = t_max
  cnt = tmp_cnt
  
  for j in b:
    t += j
    #print('j:', b[j], t)
    if t <= k:
      cnt += 1
      d.append(j)
      t_max = t
      if cnt > cnt_max:
        cnt_max = cnt
        #print('cnt', cnt, 'max', cnt_max)
    else:
      break
      
  for i in range(len(d)):
    b.popleft()
      
print(cnt_max)

Submission status: Wrong Answer

Solution code:
n,m,k = map(int,input().split())
a = input().split(" ")
lista = [int(n) for n in a]
b = input().split(" ")
listb = [int(n) for n in b]
time = 0
count = 0
if sum(lista)+sum(listb)<=time:
  print(m+n)
  exit()
while k>=time:
  if (len(lista)>0 and len(listb)>0 and lista[0]>=listb[0]) or (len(listb)>0 and len(lista)==0):
    time+=listb.pop(0)
    count+=1  
  elif (len(lista)>0 and len(listb)>0 and listb[0]>=lista[0]) or (len(lista)>0 and len(listb)==0):
    time+=lista.pop(0)
    count+=1
  else:
    count+=1
    break
print(count-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
sum=0
while sum<=k and (A!=[] or B!=[]):
    if A==[]:
        sum+=B.pop(0)
        ans+=1
    elif B==[]:
        sum+=A.pop(0)
        ans+=1
    else:
        a=A[0]
        b=B[0]
        if a<b:
            sum += A.pop(0)
            ans += 1
        elif a>b:
            sum += B.pop(0)
            ans += 1
        else:
            sum+=A.pop(0)
            ans+=1
            if sum<k:
                sum += B.pop(0)
                ans += 1
if sum==k:
    print(ans)
else:
    print(ans-1)

Submission status: Wrong Answer

Solution code:
n,m,k=3,4,730
a=[60,90,120]
b=[80,150,80,150]

ans=0
count=0
i,j=0,0
if a[i]>k:
  print(0)
  exit()
elif b[j]>k:
  print(0)
  exit()
elif sum(a)+sum(b)<=k:
  print(n+m)
  exit()
else:
  while ans<=k:
    if i > n-1:
      num=b[j]
    elif j > m-1:
      num=a[i]
    else:
      num=min(a[i],b[j])
      if num==a[i]:
        i+=1
        count+=1
        ans+=num
      else:
        j+=1
        count+=1
        ans+=num

if num==k:
  print(count)
else:
  print(count-1)

Submission status: Wrong Answer

Solution code:
def main():
    n, m, k = map(int,input().split())
    from collections import deque
    from heapq import heappush, heappop
    a = list(map(int,input().split()))
    b = list(map(int,input().split()))
    if sum(a)+sum(b) <= k:
        print(m+n)
        return 0
    ad = deque(a)
    bd = deque(b)
    cnt = 0
    inf = 10**18
    p = inf
    q = inf
    for _ in range(m+n):
        if p == inf and ad:
            p = ad.popleft()
        if q == inf and bd:
            q = bd.popleft()
        if p < q:
            n -= 1
            k -= p
            p = inf
        else:
            m -= 1
            k -= q
            q = inf
        if k >= 0:
            cnt += 1
        if k <= 0:
            break
    print(cnt)              


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
import math
import heapq
def get_array(): return list(map(int, sys.stdin.readline().strip().split()))
def get_ints(): return map(int, sys.stdin.readline().strip().split())
def input(): return sys.stdin.readline().strip()
n,m,k=get_ints()
a=get_array()
b=get_array()
ab=sum(a)+sum(b)
sumi=0
ans=0
flag=0
while(1):
    i=0
    if a==[]:
        flag=1
        break
    elif b==[]:
        flag=2
        break
    if a[i]<=b[i]:
        sumi=sumi+a[0]
        a.pop(0)
        if sumi<=k and sumi<=ab:
            ans=ans+1
        else:
            break
    elif b[i]<a[i]:
        sumi=sumi+b[0]
        b.pop(0)
        if sumi<=k and sumi<=ab:
            ans=ans+1
        else:
            break
if flag==1:
    for z in range(len(b)):
        sumi=sumi+b[z]
        if sumi<=k and sumi<=ab:
            ans=ans+1
        else:
            break
elif flag==2:
    for j in range(len(a)):
        sumi=sumi+a[j]
        if sumi<=k and sumi<=ab:
            ans=ans+1
        else:
            break
print(ans)

Submission status: Wrong Answer

Solution code:
i = list(map(int, input().split()))
n=i[0]
m=i[1]
k=i[2]
A = list(map(int, input().split()))
B = list(map(int, input().split()))
a=b=0 #それぞれの読んだ数
cal=0 #読んだ時間
while True:
    if a!=n and b!=m:
        if A[a]<B[b]:
            cal+=A[a]
            if cal>k:
                break
            a+=1
        elif B[b]<A[a]:
            cal+=B[b]
            if cal>k:
                break
            b+=1
        elif B[b]==A[a]:
          cal+=A[a]
          if cal>k:
              break
          a+=1
    elif a==n and b==m:
        break
    elif a==n:
        cal+=B[b]
        if cal>k:
            break
        b+=1
    elif b==m:
        cal+=A[a]
        if cal>k:
            break
        a+=1
print(a+b)

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 for _ in range(n)]
sb = [0 for _ in range(m)]
x = 0
for i in range(n):
  x += a[i]
  sa[i] = x
y = 0
for j in range(m):
  y += b[j]
  sb[j] = y
ans = 0
si = -1
for i in range(n):
  if sa[n-1-i] <= k:
    si = n-1-i
    break
if si == -1:
  ej = -1
  for j in range(m):
    if sb[j] >k:
      ej = j
      break
  if ej == -1:
    print(m)
    exit()
  print(ej)
  exit()
else:
  ej = 0
  while True:
    while sa[si]+sb[ej] <= k:
      if ans < si + ej + 2:
        ans = si + ej + 2
      ej += 1
      if ej == m:
        break
    if si == 0 or ej == m:
      break
    else:
      si -= 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()))
time = 0
count = 0
i = 0
j = 0
A = a[0]
B = b[0]
while True:
    if A<=B:
        time += A
        i += 1
        if i != n:
            A = a[i]
        else:
            A = 1000000000000
    else:
        time += B
        j += 1
        if j != m:
            B = b[j]
        else:
            B = 1000000000000
    if time > k:
        break
    count += 1
    if i == n and j == m:
        break

print(count)

Submission status: Wrong Answer

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

res = 0
m = 0
df = [[0 for _ in range(M+1)] for _ in range(N+1)]
for i in range(1, N + 1):
    df[i][0] = df[i-1][0] + A[i]
    if df[i][0] > K:
        break
    res = i
    m = i
for i in range(1, m+1):
    for j in range(1, M+1):
        df[i][j] = df[i][j-1] + B[j]
        if df[i][j] > K:
            break
        if res < i + j:
            res = i + j
print(res)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
import itertools as it
def main():
    n,m,k=map(int,input().split())
    a = [0] + list(it.accumulate(map(int, input().split())))
    b = [0] + list(it.accumulate(map(int, input().split())))
    AA = calc(k,n,m,a,b)
    BB = calc(k,m,n,b,a)
    print(max(AA,BB))

def calc(k,n,m,a,b):
    for A in range(n,-1,-1):
        aa = a[A]
        Kaa = k-aa
        for B in range(m,-1,-1):
            if b[B] <= Kaa:
                return A+B

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
NMK = list(map(int, input().split()))

Asum = []
Bsum = []

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

sum = 0
for i in range(NMK[0]):
    sum += A[i]
    Asum.append(sum)

sum = 0
for i in range(NMK[1]):
    sum += B[i]
    Bsum.append(sum)

cutA = 0
max = NMK[2]
indexA = 0
while(True):
    cutA = Asum[indexA]
    if (max < cutA):
        break
    indexA += 1
    if (indexA == NMK[0]):
        break
if (indexA == 0):
    maxA = 0
else:
    maxA = Asum[indexA - 1]

cutB = 0
indexB = 0
while(True):
    cutB = Bsum[indexB]
    if (max < cutB):
        break
    indexB += 1
    if (indexB == NMK[1]):
        break
if (indexB == 0):
    maxB = 0
else:
    maxB = Bsum[indexB - 1]


def returnAnswer(max, min, Marray, marray):
    answer = 0
    remain = 0
    maxAnswer = 0
    max = max if max < 10 else 10
    for i in range(max):
        answer = max - i
        index = 0
        remain = NMK[2] - Marray[max - i - 1]
        while (True):
            if (remain >= marray[index]):
                answer += 1
                index += 1
                if (index == min):
                    break
            else:
                break

        if (maxAnswer < answer):
            maxAnswer = answer
    return maxAnswer

if (indexA > indexB):
    if (indexA == 0):
        print(0)
        exit()
    print(returnAnswer(indexA, indexB, Asum, Bsum))
else:
    if (indexB == 0):
        print(0)
        exit()
    print(returnAnswer(indexB, indexA, Bsum, Asum))

Submission status: Wrong Answer

Solution code:
N,M,K=list(map(int,input().split()))
a_list = list(map(int,input().split()))
b_list = list(map(int,input().split()))
totalbook = 0
totaltime = 0
for a in a_list:
  if totaltime+a <= K:
    totaltime+=a
    totalbook+=1
  else:
    break
ans = totalbook
nowaind = totalbook-1 if totalbook > 0 else 0
nowbind = 0
for b in b_list:
  if totaltime+b<=K:
    totaltime+=b
    totalbook+=1
    nowbind+=1
  else:
    break
ans = max(ans,totalbook)
while nowaind >= 0:
  totalbook-=1
  totaltime-=a_list[nowaind]
  nowaind-=1

  for b in range(nowbind,len(b_list)):
    while nowaind >= 0 and totaltime+b_list[b]>K:
      totalbook-=1
      totaltime-=a_list[nowaind]
      nowaind-=1
    
    if totaltime+b_list[b]<=K:
      totaltime+=b_list[b]
      totalbook+=1
      nowbind+=1
    else:
      break
  
ans = max(ans,totalbook)
print(ans)

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()))
time = 0
counter = 0

if k >= sum(a) + sum(b):
    print(len(a) + len(b))
    sys.exit()
if k < a[0] and k < b[0]:
    print(0)
    sys.exit()

for i in range(len(a) + len(b)):
    if len(a) == 0:
        while time + b[0] < k:
            time += b[0]
            counter += 1
            del b[0]
            if len(b) == 0:
                break
        break    
    elif len(b) == 0:
        while time + a[0] < k:
            time += a[0]
            counter += 1
            del a[0]
            if len(a) == 0:
                break
        break
    elif time + a[0] > k and time + b[0] > k:
        break

    elif a[0] <= b[0]:
        time += a[0]
        counter += 1
        del a[0]
    elif b[0] < a[0]:
        time += b[0]
        counter += 1
        del b[0]
print(counter)

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
cnt = 0
ai = 0
bi = 0
bcnt = 0
while time<k:
  if ai<len(a) and bi<len(b):
    if a[ai]<=b[bi]:
      time += a[ai]
      ai += 1
      cnt += 1
    else:
      time += b[bi]
      bi += 1
      cnt += 1
  elif ai<len(a) and bi>=len(b):
    time += a[ai]
    ai += 1
    cnt += 1
  elif ai>=len(a) and bi< len(b):
    time += b[bi]
    bi += 1
    cnt += 1
  else:
    break
aib = 0
bib = 0
timeb = 0   
while timeb<k:
  if aib<len(a) and bib<len(b):
    if a[aib]<b[bib]:
      timeb += a[aib]
      aib += 1
      bcnt += 1
    else:
      timeb += b[bib]
      bib += 1
      bcnt += 1
  elif aib<len(a) and bib>=len(b):
    timeb += a[aib]
    aib += 1
    bcnt += 1
  elif aib>=len(a) and bib< len(b):
    timeb += b[bib]
    bib += 1
    bcnt += 1
  else:
    break

if time>k:
  cnt -= 1
if timeb>k:
  bcnt -= 1

print(max(cnt,bcnt))

Submission status: Wrong Answer

Solution code:
# 初期入力
from collections import deque
import sys
input = sys.stdin.readline
N,M,K = (int(i) for i in input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
dq_a =deque(A)
dq_b =deque(B)

#上から読む＝配列の前から読む
read_time =0
ans =0
a=dq_a.popleft()
b=dq_b.popleft()
for i in range(N+M):
    if a <=b :
        if read_time +a <=K:
            read_time +=a
            ans +=1
            if len(dq_a) >0:
                a =dq_a.popleft()
            else:
                a=10**9 +1
       
    if a >=b:
        if read_time +b <=K:
            read_time +=b
            ans +=1
            if len(dq_b) >0:
                b =dq_b.popleft()
            else:
                b=10**9 +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()))

read_time=0
Anum=0
Bnum=0
total=0
while read_time<k:
    if Anum!=len(A) and Bnum!= len(B):
        if (A[Anum]+read_time > k) and(B[Bnum]+read_time > k):
            break
        if A[Anum]<=B[Bnum]:
            read_time+=A[Anum]
            Anum+=1
            total+=1
        else:
            read_time+=B[Bnum]
            Bnum+=1
            total+=1
    elif Anum==len(A) and Bnum!= len(B):
        if (B[Bnum]+read_time > k):
            break
        else:
            read_time+=B[Bnum]
            Bnum+=1
            total+=1
    elif Anum!=len(A) and Bnum== len(B):
        if (A[Anum]+read_time > k):
            break
        else :
            read_time+=A[Anum]
            Anum+=1
            total+=1
    else:
        break

    
print(total)

Submission status: Wrong Answer

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

b1=[int(x) for x in input().split()]
b2=[int(x) for x in input().split()]

b=b1+b2
b.sort()
if sum(b1)+sum(b2)==k:
    print(n+m)
else:
    sum=0
    for i in range(len(b)):
        sum=sum+b[i]
        if sum > k:
            break
    print(i)

Submission status: Wrong Answer

Solution code:
n, m, k = (int(x) for x in input().split())

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

K = k
ans = 0

i = 0
while K-a[i]>=0:
    K -= a[i]
    i += 1
    if i==n:
        break
ans = i
    
# K = k
for j in range(i-1, -1, -1):
    l = 0
    while K-b[l]>=0:
        K -= b[l]
        l += 1
        if l==m:
            break
    ans = max(ans, j+l+1)
    K += a[j]


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

a=N
b=M
count =0

while K != "sf":
    
    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>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
        
    else:
        break
    #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(map(int,input().split()))
B = list(map(int,input().split()))

A.append(10**10)
B.append(10**10)

c=0

for i in range(10**10):
    a = A[0]
    b = B[0]
    if a < b:
        K = K-a
        if K<0:
            break
        A = A[1:] 
        c = c+1
    else:
        K = K-b
        if K<0:
            break
        B = B[1:]
        c = c+1
        
print(c)

Submission status: Wrong Answer

Solution code:
def Tsundoku():

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

    value_A = list(map(int,input().split()))
    value_B = list(map(int,input().split()))

    total_time = 0
    total_num = 0
    
    numA = 0
    numB = 0
    
    if value_A[numA] < Num[2] or value_B[numB] < Num[2]:
    
        while total_time < Num[2]:
    
            if len(value_A) == numA and len(value_B) == numB:
                break
            
            elif len(value_B) != numB and len(value_A) != numA and value_A[numA] > value_B[numB]:
                total_time = total_time + value_B[numB]
                numB = numB + 1
                total_num = total_num + 1
                
            elif len(value_A) != numA and len(value_B) != numB and value_A[numA] <= value_B[numB]:
                total_time = total_time + value_A[numA]
                numA = numA + 1
                total_num = total_num + 1            
            
        
            elif len(value_A) == numA and total_time < total_time + value_B[numB]:
                total_time = total_time + value_B[numB]
                numB = numB + 1
                total_num = total_num + 1
                
            elif len(value_B) != numB and total_time < total_time + value_A[numA]:
                total_time = total_time + value_A[numA]
                numA = numA + 1
                total_num = total_num + 1

        print(total_num)

    else:
        print(total_num)


if __name__ == '__main__':
    Tsundoku()

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
an,bn=0,0
ans=0
while temp<=k:
    if an==n and bn==m: break
    if an>=n:
        temp+=b[bn]
        if temp>k: break
        bn+=1
    elif bn>=m:
        temp+=a[an]
        if temp>k: break
        an+=1
    else:
        if a[an]<b[bn]:
            temp+=a[an]
            if temp>k: break
            an+=1
        elif a[an]>b[bn]:
            temp+=b[bn]
            if temp>k: break
            bn+=1
        else:
            cnt=0
            while True:
                if an==n:
                    bn-=cnt
                    break
                if bn==m:
                    an-=cnt
                    break
                if a[an]<b[bn]:
                    temp+=a[an]
                    an+=1
                    bn-=cnt
                    ans+=1
                    break
                elif a[an]>b[bn]:
                    temp+=b[bn]
                    bn+=1
                    ans+=1
                    an-=cnt
                    break
                temp+=a[an]
                if temp>k:
                    break
                an+=1
                bn+=1
                ans+=1
                cnt+=1
            if temp>k:
                break
    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()))

ans = 0
aa = 0
bb = 0
p = k

for i in range(n):
    if p - a[i] <0:
        break
    else:
        p -= a[i]
        aa += 1
ans = aa
#print(ans)

for i in range(m):
    if p - b[i] >= 0:
        p -= b[i]
        bb += 1
        if ans < aa + bb:
            ans = aa + bb
    else:
        while aa > 0:
            aa -= 1
            #print(aa)
            #print(aa)
            p += a[aa]
            if p - b[i] >= 0:
                p -= b[i]
                bb += 1
        if ans < aa + bb:
            ans = aa + bb
        #if aa <= 0:
        #    break
print(ans)

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, M , W = MAP()
A = LIST()
B = LIST()

sa = sum(A)
sb = sum(B)
A.reverse()
C = A + B
# print(C)
ans = 0
left_wa = 0
right_wa = 0


ruiseki = C.copy()
ruiseki = [0] + ruiseki
for i in range(len(ruiseki)-1):
    ruiseki[i+1] = ruiseki[i] + ruiseki[i+1]
# print(ruiseki)
left = 0
# for right in range(1, len(C)+1):
#     ruiseki[left:right]
idx = 0
wa = 0
ans = 0
# print(len(ruiseki))
for right in range(len(A), len(ruiseki)):
    # idx += 1
    # print("right:", right)
    wa = ruiseki[right] - ruiseki[left]
    # if
    while wa > W:
        left += 1
        wa = ruiseki[right] - ruiseki[left]
    if left < len(A):
        tmp = right - left
        # print(tmp)
        ans = max(tmp, ans)
    else:
        break
    # print("left, right, :", left, right)

print(ans)        






exit()
for right in range(1, N+1):
    # sum(A[left:right])
    right_wa += C[right-1]
    wa = right_wa - left_wa
    if wa <= K:
        while wa <= K:
            if left < N:
                left_wa += C[left]
                wa = right_wa - left_wa
            left += 1
    ans += left
    



exit()
A = deque(A)
B = deque(B)

# C = A + B
cnt = 0
while W >= 0 and cnt != N + M:
    if len(A) != 0 and len(B) != 0:
        a = A[0]
        b = B[0]
        if a <= b:
            W -= a
            A.popleft()
        else:
            W -= b
            B.popleft()
    elif len(A) != 0:
        # a = INF
        a = A.popleft()
        W -= a
    elif len(B) != 0:
        # a = INF
        b = B.popleft()
        W -= b
    # print(W)
    if W >= 0:
        cnt += 1


print(cnt)



# 3 4 240
# 60 90 120
# 80 150 80 150
# 出力例 1

Submission status: Wrong Answer

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
# from fractions import gcd
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from functools import reduce
from bisect import bisect_left, insort_left
from heapq import heapify, heappush, heappop

INPUT = lambda: sys.stdin.readline().rstrip()
INT = lambda: int(INPUT())
MAP = lambda: map(int, INPUT().split())
S_MAP = lambda: map(str, INPUT().split())
LIST = lambda: list(map(int, INPUT().split()))
S_LIST = lambda: list(map(str, INPUT().split()))
ZIP = lambda: zip(*(MAP() for _ in range(n)))

sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    N, M, K = MAP()
    A = LIST()
    B = LIST()

    qa = deque(A)
    qb = deque(B)
    qa.append(INF)
    qb.append(INF)

    times = 0
    cnt = 0
    while 1:
        if qa[0] < qb[0]:
            times += qa.popleft()
        else:
            times += qb.popleft()

        if times > K: break
        cnt += 1

    print(cnt)


if __name__ == '__main__':
    main()