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:
if __name__ == '__main__':
    N, M, K = map(int, input().split()) 
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))
    
    acc_A = []
    acc_B = []
    acc_A.append(0)
    acc_B.append(0)
    
    sum_a = 0
    sum_b = 0

    for a in A:
        sum_a += a
        acc_A.append(sum_a)

    for b in B:
        sum_b += b
        acc_B.append(sum_b)

    large_ar = []
    small_ar = []

    if N > M:
        large_ar = acc_A
        small_ar = acc_B
    else:
        large_ar = acc_B
        small_ar = acc_A
    
    ans = 0
    for i in range(len(small_ar)):
        for j in range(ans - i, len(large_ar)):
            if large_ar[j] + small_ar[i] <= K:
                ans = max(ans, i+j)
            else: break
    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, 0]
a =0
ta = 0
b = 0
tb = 0
m = 0

for i in range(M):
    if tb + B[i] > K:
        break
    b = i+1
    tb += B[i]

for i in range(N):
    if ta + A[i] > K:
        break
    a = i+1
    ta += A[i]
    while True:
        if ta + tb > K:
            if b == 0:break
            b -= 1
            tb -= B[b]
        else:
            if a + b > m:
                m = a + b
                ans = [a, b]
            break
print(ans[0]+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()))
a=[0]
b=[0]
for i in range(N):
  a.append(a[-1]+A[i])
for j in range(M):
  b.append(b[-1]+B[j])  
for i in range(len(A)+1):
  for j in range(len(B)+1):
    score=a[i]+b[j]
    if score <= K:
      point=[]
      point.append(i+j)
print(max(point))

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とbをそれぞれk冊まで読んだ時のかかる時間のリストを作成
a=[0]
b=[0]
for i in range(N):
  a.append(a[-1]+A[i])
for j in range(M):
  b.append(b[-1]+B[j])
  
for i in range(len(A)+1):
  for j in range(len(B)+1):
    score=a[i]+b[j]
    if score <= K:
      point=[]
      point.append(i+j)
print(max(point))

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

Asum=[0]
Bsum=[0]

for i in range(N):
    Asum.append(Asum[-1]+A[i])
        
for i in range(M):
    Bsum.append(Bsum[-1]+B[i])
    
m=0
j=M

for i in range(len(Asum)):
    if Asum[i]>K:
        break
    elif Asum[i]+Bsum[j]<=K and m<i+j:
        m=i+j
    else:
        j-=1
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()))

As = [0] * (n + 1)
Bs = [0] * (m + 1)
for i in range(n):
  As[i + 1] = A[i] + As[i - 1]
for i in range(m):
  Bs[i + 1] = B[i] + Bs[i - 1]

l = 0
r = n + m + 1
while r - l > 1:
  p = (l + r) // 2
  best = k + 1
  for i in range(p + 1):
    if min(i, n) + min(p - i, m) < p:
      continue
    best = min(best, As[i] + Bs[p - i])
  if best <= k:
    l = p
  else:
    r = p
print(l)

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 = 0
r = n + m + 1
while r - l > 1:
  p = (l + r) // 2
  best = k + 1
  for i in range(min(n, p + 1)):
    if min(i, n) + min(p - i, m) < p:
      continue
    best = min(best, sum(A[:i]) + sum(B[:p - i]))
  if best <= k:
    l = p
  else:
    r = p
print(l)

Submission status: Wrong Answer

Solution code:
N,M,K = map(int, input().split())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
b_total = 0
for i,b in enumerate(B):
    if b_total + b > K:
        i = i-1
        break
    b_total += b
ans,pre_ans = i+1, i+1

for j,a in enumerate(A):
    b_total += a
    for k,b in enumerate(B[:i+1][::-1]):
        b_total -= b
        if b_total <= K:
            i -= k+1
            ans = max(pre_ans-k+j, ans)
            pre_ans = pre_ans-k+j
            break
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 = 0
r = n + m + 1
while r - l > 1:
  p = (l + r) // 2
  total = min(sum(A[:i]) + sum(B[:p - i]) for i in range(p))
  if total <= k:
    l = p
  else:
    r = p
print(l)

Submission status: Wrong Answer

Solution code:
N,M,K = map(int, input().split())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]
A_memo = [sum(A[:i]) for i in range(1,N+1)]
B_memo = [sum(B[:i]) for i in range(1,M+1)]
ans = 0

for i, a in enumerate(A_memo):
    if a > K:
        break
    for j, b in enumerate(B_memo):
        if a+b > K:
            break
        ans = max(i+j+2, ans)
print(ans)

Submission status: Wrong Answer

Solution code:
ri = lambda S: [int(v) for v in S.split()]
def rii(): return ri(input())

N, M, K = rii()
A = rii()
B = rii()

i = j = 0
book = 0
while i < N or j < M:
    if i == N and j == M:
        break
    Ai = A[i] if i < N else 0
    Bj = B[j] if j < M else 0
    Ai = Ai if Ai <= K else 0
    Bj = Bj if Bj <= K else 0

    if Ai == 0 and Bj == 0: break

    book += 1

    if Ai > 0 and Bj > 0:
        if Ai < Bj:
            K -= Ai
            i += 1
        else:
            K -= Bj
            j += 1
    else:
        if Ai == 0:
            K -= Bj
            j += 1
        else:
            K -= Ai
            i += 1


print(book)

Submission status: Wrong Answer

Solution code:
def getval():
    n,m,k = map(int,input().split())
    A = list(map(int,input().split()))
    B = list(map(int,input().split()))
    a = [A[0]]
    b = [B[0]]
    for i in range(0,n-1):
        a.append(a[i]+A[i+1])
    for i in range(0,m-1):
        b.append(b[i]+B[i+1])
    return n,m,k,a,b

def main(n,m,k,a,b):
    idxa = 0
    ans = 0
    t = 0
    while a[idxa]<=k and idxa<n:
        idxa += 1
        if idxa==n:
            break
    ans = idxa
    idxa -= 1
    t = a[idxa]
    for i in range(m):
        t = a[idxa] + b[i]
        if t<=k:
            ans = max(ans,idxa+i+2)
        else:
            flag = False
            while t>k:
                idxa -= 1
                if idxa<0:
                    flag = True
                    break
                t = a[idxa]+b[i]
            if flag:
                break
            ans = max(ans,idxa+i+2)

    print(ans)

if __name__=="__main__":
    n,m,k,a,b = getval()
    main(n,m,k,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()))
cumsum_a = [0] * N
cumsum_b = [0] * M
cumsum_a[0] = A[0]
cumsum_b[0] = B[0]
for i in range(1, N):
    cumsum_a[i] = cumsum_a[i - 1] + A[i]

for i in range(1, M):
    cumsum_b[i] = cumsum_b[i - 1] + B[i]

ans = 0
for i in range(N):
    if cumsum_a[i] >= K:
        break

    remain_time = K - cumsum_a[i]
    left = 0
    right = M - 1
    while right - left > 1:
        mid = (left + right) // 2
        if cumsum_b[mid] > remain_time:
            right = mid - 1
        else:
            left = mid
    if cumsum_b[right] <= remain_time:
        ans = max(ans, i + 1 + right + 1)
    elif cumsum_b[left] <= remain_time:
        ans = max(ans, i + 1 + left + 1)
    else:
        ans = max(ans, i + 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()))
cumsum_a = [0] * N
cumsum_b = [0] * M
cumsum_a[0] = A[0]
cumsum_b[0] = B[0]
for i in range(1, N):
    cumsum_a[i] = cumsum_a[i - 1] + A[i]

for i in range(1, M):
    cumsum_b[i] = cumsum_b[i - 1] + B[i]

ans = 0
for i in range(N):
    if cumsum_a[i] >= K:
        break

    remain_time = K - cumsum_a[i]
    left = 0
    right = M - 1
    while right - left > 1:
        mid = (left + right) // 2
        if cumsum_b[mid] > remain_time:
            right = mid - 1
        else:
            left = mid
    if cumsum_b[right] <= remain_time:
        ans = max(ans, i + 1 + right + 1)
    else:
        ans = max(ans, i + 1 + left + 1)

print(ans)

Submission status: Wrong Answer

Solution code:
import math , sys, bisect

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

Asum=[]
Bsum=[]
if A[0] > K and B[0] > K:
    print(0)
    sys.exit()
flag=True
for i in range(N):
    if i ==0:
        tot = A[i]
    else:
        tot += A[i]
        if tot > K and flag:
            ans=i-1
            flag=False
    Asum.append(tot)
if B[0]>K:
    print(ans)
    sys.exit()
flag=True
ans=0
for i in range(M):
    if i ==0:
        tot = B[i]
    else:
        tot += B[i]
        if tot > K and flag:
            ans=i-1
            flag=False
    Bsum.append(tot)

if A[0]>K:
    print(ans)
    sys.exit()

for i in range(N):
    tot = Asum[i]
    if tot >K:
        print(ans)
        sys.exit()
    num = bisect.bisect(Bsum,K-tot)
    if i + num >ans:
        ans=i+1+num
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

N, M, K = map(int, input().split())
A = deque(map(int, input().split()))
B = deque(map(int, input().split()))
S = 0
cnt = 0
for _ in range(N + M):
  if len(B) == 0:
    S += A.popleft()
    if S <= K:
      cnt += 1
    else:
      break
  elif len(A) == 0:
    S += B.popleft()
    if S <= K:
      cnt += 1
    else:
      break
  elif A[0] <= B[0]:
    S += A.popleft()
    if S <= K:
      cnt += 1
    else:
      break
  else:
    S += B.popleft()
    if S <= K:
      cnt += 1
    else:
      break
print(cnt)

Submission status: Wrong Answer

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

point_a = 0
point_b = 0

while(buf < k):

    if point_a < n and point_b < m:
        buf += min(a[point_a],b[point_b])
        ans += 1
        if a[point_a] < b[point_b]:
            if point_a < n:
                point_a += 1
        else:
            if point_b < m:
                point_b += 1

    elif point_a < n and point_b >= m:
        buf += a[point_a]
        point_a += 1
        ans += 1

    elif point_a >= n and point_b < m:
        buf += b[point_b]
        point_b += 1
        ans += 1
    else:
        break

    """print("point_a : %d , point_b : %d" % (point_a,point_b))
    print('buf : ',buf)"""

if buf > k:
    print(ans - 1)
else:
    print(ans)

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

readA = 0 #Aから読まれた数
readB = 0 #Bから読まれた数
Atime = 0 #Aにかかる時間
Atime = 0 #Bにかかる時間

ACC = list(itertools.accumulate(A))
BCC = list(itertools.accumulate(B))

for i in range(N):
    if K - ACC[i] >= 0:
        readA += 1
        Atime = ACC[i]

Btime = K - Atime #Bから読める時間

for j in range(M): 
    if Btime - BCC[j] >= 0:
            readB += 1

print(readA + readB)

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()))
maximum = 0
sumA = 0
sumB = 0
for i in range(N):
  if sumA + A[i] <=K:
    sumA += A[i]
    maximum += 1
  else:
    break
Aindex = maximum
Bindex = 0
while Bindex < M:
  if sumA + sumB + B[Bindex] <= K:
    sumB += B[Bindex]
    Bindex += 1
    maximum +=1
  else:
    break
while Bindex < M and Aindex >=0:
  Aindex -= 1
  while Bindex < M:
    if sumA + sumB + B[Bindex] <= K:
      sumB += B[Bindex]
      Bindex += 1
      maximum = max(maximum,Aindex+Bindex)
    else:
      break
print(maximum)

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 = [0] + a
b = [0] + b

formertmp = 0
maxj = 0
ans = 0
for i in range(m+1):
    formertmp += b[i]
    # print(formertmp)
    if formertmp > k:
        maxj = i
        break
    else:
        maxj = m+1
# print(maxj)

for l in range(n+1):
    latertmp = 0
    # print("l =" ,l)
    latertmp = sum(a[0:l+1])
    # print(latertmp)
    if latertmp >= k:
        break
    else:
        for j in range(maxj):
            latertmp += b[j]
            # print(latertmp)
            if latertmp <= k:
                ans = max(ans, l + j)
            else:
                latertmp = 0

print(ans)

Submission status: Wrong Answer

Solution code:
N,M,K=map(int,input().split()) 
nlist = list(map(int, input().split()))
mlist = list(map(int, input().split()))
nlist.append(10000000000)
mlist.append(10000000000)
n = 0
m = 0
count = 0
kei = 0
while K >= nlist[n] or K>= mlist[m]:
    if nlist[n] <= mlist[m]:
        kei = kei+ nlist[n]
        n = n+1
        count = count+1
        if kei > K:
            count = count-1
    elif nlist[n] > mlist[m]:
        kei = kei+ mlist[m]
        m = m+1
        count = count+1
        if kei > K:
            count = count-1
print(count)

Submission status: Wrong Answer

Solution code:
N,M,K=map(int,input().split()) 
nlist = list(map(int, input().split()))
mlist = list(map(int, input().split()))
nlist.append(10000000000)
mlist.append(10000000000)
n = 0
m = 0
count = 0
while K >= nlist[n] or K>= mlist[m]:
    if nlist[n] <= mlist[m]:
        if K - nlist[n] >= 0:
            K = K - nlist[n]
            n = n+1
            count = count+1
    elif nlist[n] > mlist[m]:
        if K - mlist[m] >= 0:
            K = K - mlist[m]
            m = m+1
            count = count+1
print(count)

Submission status: Wrong Answer

Solution code:
N,M,K=map(int,input().split()) 
nlist = list(map(int, input().split()))
mlist = list(map(int, input().split()))
nlist.append(10000000000)
mlist.append(10000000000)
n = 0
m = 0
count = 0
while K >= nlist[n] or K>= mlist[m]:
    if nlist[n] <= mlist[m]:
        K = K - nlist[n]
        n = n+1
        count = count+1
    elif nlist[n] > mlist[m]:
        K = K - mlist[m]
        m = m+1
        count = count+1
print(count)

Submission status: Wrong Answer

Solution code:
N,M,K=map(int,input().split()) 
nlist = list(map(int, input().split()))
mlist = list(map(int, input().split()))
nlist.append(10000000000)
mlist.append(10000000000)
n = 0
m = 0
count = 0
while K >= 0:
    if nlist[n] <= mlist[m]:
        K = K - nlist[n]
        n = n+1
        count = count+1
    elif nlist[n] > mlist[m]:
        K = K - mlist[m]
        m = m+1
        count = count+1
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()))
booka = 0
bookb = 0
booknum = 0
time = 0
ba=0
bb=0
while True:
    if booka >= N:
        while time<=K and bookb<M:
            bb += 1
            time += B[bookb]
            bookb += 1
        break
    if bookb>=M:
        while time<=K and booka<N:
            ba += 1
            time += A[booka]
            booka += 1
        break
    if A[booka]<B[bookb]:
        time += A[booka]
        booka += 1
    else:
        time += B[bookb]
        bookb += 1
    if time >K:
        break
    booknum += 1
if time > K and (bb!=0 or ba!=0):
    booknum-=1
booknum += (ba+bb)
print(booknum)

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()))
booka = 0
bookb = 0
booknum = 0
time = 0
ba=0
bb=0
while True:
    if booka >= N:
        while time<=K and bookb<M:
            bb += 1
            time += B[bookb]
            bookb += 1
        break
    if bookb>=M:
        while time<=K and booka<N:
            ba += 1
            time += A[booka]
            booka += 1
        break
    if A[booka]<B[bookb]:
        time += A[booka]
        booka += 1
    else:
        time += B[bookb]
        bookb += 1
    if time >K:
        break
    booknum += 1
if time != K and (bb!=0 or ba!=0):
    booknum-=1
booknum += (ba+bb)
print(booknum)

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()))
booka = 0
bookb = 0
booknum = 0
time = 0
ba=0
bb=0
while True:
    if booka >= N:
        while time<=K and bookb<M:
            bb += 1
            time += B[bookb]
            bookb += 1
        break
    if bookb>=M:
        while time<=K and booka<N:
            ba += 1
            time += A[booka]
            booka += 1
        break
    if A[booka]<B[bookb]:
        time += A[booka]
        booka += 1
    else:
        time += B[bookb]
        bookb += 1
    if time >K:
        break
    booknum += 1
if time != K:
    booknum-=1
booknum += (ba+bb)
print(booknum)

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()))
booka = 0
bookb = 0
booknum = 0
time = 0
ba=0
bb=0
while True:
    if booka >= N:
        while time<K and bookb<M:
            bb += 1
            time += B[bookb]
            bookb += 1
        break
    if bookb>=M:
        while time<K and booka<N:
            ba += 1
            time += A[booka]
            booka += 1
        break
    if A[booka]<B[bookb]:
        time += A[booka]
        booka += 1
    else:
        time += B[bookb]
        bookb += 1
    if time >K:
        break
    booknum += 1
if ba != 0:
    ba -= 1
if bb != 0:
    bb -= 1
booknum += (ba+bb)
print(booknum)

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()))
booka = 0
bookb = 0
booknum = 0
time = 0
while True:
    if booka >= N:
        while time<K and bookb<M:
            booknum += 1
            time += B[bookb]
            bookb += 1
        break
    if bookb>=M:
        while time<K and booka<N:
            booknum += 1
            time += A[booka]
            booka += 1
        break
    if A[booka]<B[bookb]:
        time += A[booka]
        booka += 1
    else:
        time += B[bookb]
        bookb += 1
    if time >K:
        break
    booknum += 1
print(booknum)

Submission status: Wrong Answer

Solution code:
N,M,K=map(int,input().split()) 
nlist = list(map(int, input().split()))
mlist = list(map(int, input().split()))
n = 0
m = 0
count = 0
while K >= 0:
    if n >= len(nlist) and m >= len(mlist):
        K = -1
        count = count+1
    elif n >= len(nlist):
        K = K - mlist[m]
        m = m+1
        count = count+1
    elif m >= len(mlist):
        K = K - nlist[n]
        n = n+1
        count = count+1
    else:
        if nlist[n] <= mlist[m]:
            K = K - nlist[n]
            n = n+1
            count = count+1
        elif nlist[n] > mlist[m]:
            K = K - mlist[m]
            m = m+1
            count = count+1
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()))
booka = 0
bookb = 0
booknum = 0
time = 0
while True:
    if A[booka]<B[bookb]:
        time += A[booka]
        booka += 1
    else:
        time += B[bookb]
        bookb += 1
    if time >K:
        break
    booknum += 1
    if booka >= N:
        while time<K:
            booknum += 1
            time += B[bookb]
            bookb += 1
        break
    if bookb>=M:
        while time<K:
            booknum += 1
            time += A[booka]
            booka += 1
        break
print(booknum)

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()))
booka = 0
bookb = 0
booknum = 0
time = 0
while True:
    if A[booka]<B[bookb]:
        time += A[booka]
        booka += 1
    else:
        time += B[bookb]
        bookb += 1
    if time >K:
        break
    booknum += 1
    if booka >= N:
        booknum -= 1
        while time<K:
            booknum += 1
            time += B[bookb]
            bookb += 1
        break
    if bookb>=M:
        booknum -= 1
        while time<K:
            booknum += 1
            time += A[booka]
            booka += 1
        break
print(booknum)

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
from itertools import accumulate
def main():
    n, m, k = map(int,input().split())
    A = [0] + list(map(int,input().split()))
    B = [0] + list(map(int,input().split()))
    A = list(accumulate(A))
    B = list(accumulate(B))
    ans = 0
    j = m
    for i in range(n + 1):
        if A[i] > k:
            break
        while A[i] + B[j] > k:
            j -= 1
        ans = max(ans, i + j)
        print(i, j, ans)
    print(ans)


if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    n, m, k = map(int,input().split())
    A = list(map(int,input().split()))
    B = list(map(int,input().split()))
    time = 0
    max_a = -1
    while max_a + 1 < n and time + A[max_a + 1] <= k:
        max_a += 1
        time += A[max_a]
    max_b = -1
    ans = max_a + 1
    for max_ax in range(max_a, -1, -1):
        while max_b + 1 < m and time + B[max_b + 1] <= k:
            max_b += 1
            time += B[max_b]
        ans = max(ans, max_ax + max_b + 2)
        #print(max_ax, max_b, time, ans+2)
        time -= A[max_ax]
    print(ans)


if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    n, m, k = map(int,input().split())
    A = list(map(int,input().split()))
    B = list(map(int,input().split()))
    time = 0
    max_a = -1
    ans = 0
    while max_a + 1 < n and time + A[max_a + 1] <= k:
        max_a += 1
        time += A[max_a]
    ans = max_a 
    max_b = -1
    for max_ax in range(max_a, -1, -1):
        while max_b + 1 < m and time + B[max_b + 1] <= k:
            max_b += 1
            time += B[max_b]
        ans = max(ans, max_ax + max_b)
        #print(max_ax, max_b, time, ans+2)
        time -= A[max_ax]
    if max_a == -1 or max_b == -1:
        print(ans+1)
    else:
        print(ans+2)


if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    n, m, k = map(int,input().split())
    A = list(map(int,input().split()))
    B = list(map(int,input().split()))
    time = 0
    max_a = -1
    ans = 0
    while max_a + 1 < n and time + A[max_a + 1] < k:
        max_a += 1
        time += A[max_a]
    ans = max_a 
    max_b = -1
    for max_ax in range(max_a, -1, -1):
        while max_b + 1 < m and time + B[max_b + 1] <= k:
            max_b += 1
            time += B[max_b]
        ans = max(ans, max_ax + max_b)
        time -= A[max_ax]
    if max_a == -1 or max_b == -1:
        print(ans+1)
    else:
        print(ans+2)


if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    n, m, k = map(int,input().split())
    A = list(map(int,input().split()))
    B = list(map(int,input().split()))
    time = 0
    max_a = -1
    ans = 0
    while max_a + 1 < n and time + A[max_a + 1] < k:
        max_a += 1
        time += A[max_a]
    ans = max_a + 1
    max_b = -1
    for max_ax in range(max_a, -1, -1):
        while max_b + 1 < m and time + B[max_b + 1] <= k:
            max_b += 1
            time += B[max_b]
        if max_b == -1:
            ans += 1
        else:
            ans = max(ans, max_ax + max_b + 2)
        time -= A[max_ax]
    print(ans)


if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
# input here
_INPUT = """\
3 4 730
60 90 120
80 150 80 150
"""
"""
K = int(input())
H, W, K = map(int, input().split())
a = list(map(int, input().split()))
xy = [list(map(int, input().split())) for i in range(N)]
p = tuple(map(int,input().split()))
"""
def main():
    N, M, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    time = 0
    s = 0
    while True:
        if  (len(A) == 0) & (len(B) == 0):
            break
        elif not A:
            time += B.pop(0)
        elif not B:
            time += A.pop(0)
        else :
            if A[0] < B[0]:
                time += A.pop(0)
            else :
                time += B.pop(0)

        if time > K:
            break

        s += 1
    
    print(s)
    


            
if __name__ == '__main__':
    import io
    import sys
    import math
    import itertools
    from collections import deque

    # sys.stdin = io.StringIO(_INPUT)
    main()