Problem: 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.

Submission attempts (in order):
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): 
      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)

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
while cost <= k:
  ans += 1
  if ans >= n:
    cost += b[n-ans]
    b_i += 1
  else:
    cost += a[ans]

max_ans = ans
if b_i == 0:
  for i in range(ans-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)
else:
  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 not a[0] >= k and b[0] >= k:
  print(max_ans)
else:
  print(0)

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

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





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

  max_ans = ans
  if b_i == 0:
    for i in range(ans-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)
  else:
    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)

  print(max_ans)
else:
  print(0)

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

a, b=[0], [0]
for i in range(N):
  a.append(a[i]+A[i])
for i in range(M):
    b.append(b[i]+B[i])
    
ans, j=0, M
for i in range(N+1):
  if a[i]>K:
    break
  while b[j]>K-a[i]:
    j-=1
  ans=max(ans, i+j)
  
print(ans)

Verdicts: Wrong Answer

Runtime Error

Wrong Answer

Accepted