Buggy python submission (verdict: Runtime Error):
def A(x):
  return x + x**2 + x**3


def B(s, t):
  ans = 0
  for i in range(len(s)):
    if s[i] != t[i]:
      ans += 1
  return ans


def C(n, m, k, a, b):
  ans = 0
  while k > 0:
    if a and b:
      if a[0] < b[0]:
        k -= a[0]
        a.pop()
      else:
        k -= b[0]
        b.pop()
    elif a:
      k -= a[0]
      a.pop()
    elif b:
      k -= b[0]
      b.pop()
    else:
      break
    if k >= 0:
      ans += 1
  return ans


def C2(n, m, k, a, b):

  def _dfs(pa, pb, acc_b, acc_t, k):
    go_a = _dfs(pa + 1, pb, acc_b + 1, acc_t + a[pa + 1], k)
    go_b = _dfs(pa, pb + 1, acc_b, acc_t, k)
    return go_a if go_a > go_b else go_b

  return _dfs(pa=0, pb=-1, acc_b=0, acc_t=0, k=k)


def C3(n, m, k, a, b):

  kl = [k]
  def _get_pa_init():
    pa = 0
    while pa < len(a):
      if kl[0] > a[pa]:
        kl[0] -= a[pa]
        pa += 1
      else:
        break
    return pa - 1

  pa = _get_pa_init()
  pb = -1

  k = kl[0]
  max_read = pa + pb + 2
  while True:
    while pb + 1 < len(b) and k >= b[pb + 1]:
      k -= b[pb + 1]
      pb += 1
    if pa <= -1 and k < b[pb + 1]:
      break
    # print(k, pa, pb, pa + pb + 2)
    if pa + pb + 2 > max_read:
      max_read = pa + pb + 2
    if pa > -1:
      pa -= 1
      k += a[pa + 1]
    # print(k, pa, pb, pa + pb + 2)
    if pa + pb + 2 > max_read:
      max_read = pa + pb + 2
    if pb >= len(b) - 1:
      break
  return max_read


def main():
  n, m, k = map(int, input().split())
  a = list(map(int, input().split()))
  b = list(map(int, input().split()))
  print(C3(n, m, k, a, b))


if __name__ == '__main__':
  main()


Fixed submission:
def A(x):
  return x + x**2 + x**3


def B(s, t):
  ans = 0
  for i in range(len(s)):
    if s[i] != t[i]:
      ans += 1
  return ans


def C(n, m, k, a, b):

  def _get_acc(x):
    acc = [0]
    for idx in range(len(x)):
      acc.append(x[idx] + acc[idx])
    return acc

  acc_a = _get_acc(a)
  acc_b = _get_acc(b)

  pa = 0
  pb = 0

  while pa <= n and acc_a[pa] <= k:
    pa += 1
  pa -= 1

  max_num = 0
  while True:
    if pa < 0 or pb > m:
      break
    if acc_a[pa] + acc_b[pb] <= k:
      if max_num < pa + pb:
        max_num = pa + pb
      pb += 1
    else:
      pa -= 1

  return max_num


def main():
  n, m, k = map(int, input().split())
  a = list(map(int, input().split()))
  b = list(map(int, input().split()))
  print(C(n, m, k, a, b))


if __name__ == '__main__':
  main()

Buggy python submission (verdict: Runtime Error):
def A(x):
  return x + x**2 + x**3


def B(s, t):
  ans = 0
  for i in range(len(s)):
    if s[i] != t[i]:
      ans += 1
  return ans


def C(n, m, k, a, b):
  pa = 0
  pb = 0
  ans = 0
  while k > 0:
    if pa < len(a) and a[pa] < b[pb]:
      k -= a[pa]
      pa += 1
    elif pb < len(b):
      k -= b[pb]
      pb += 1
    else:
      break
    if k >= 0:
      ans += 1
  return ans


def main():
  n, m, k = map(int, input().split())
  a = list(map(int, input().split()))
  b = list(map(int, input().split()))
  print(C(n, m, k, a, b))


if __name__ == '__main__':
  main()


Fixed submission:
def A(x):
  return x + x**2 + x**3


def B(s, t):
  ans = 0
  for i in range(len(s)):
    if s[i] != t[i]:
      ans += 1
  return ans


def C(n, m, k, a, b):

  def _get_acc(x):
    acc = [0]
    for idx in range(len(x)):
      acc.append(x[idx] + acc[idx])
    return acc

  acc_a = _get_acc(a)
  acc_b = _get_acc(b)

  pa = 0
  pb = 0

  while pa <= n and acc_a[pa] <= k:
    pa += 1
  pa -= 1

  max_num = 0
  while True:
    if pa < 0 or pb > m:
      break
    if acc_a[pa] + acc_b[pb] <= k:
      if max_num < pa + pb:
        max_num = pa + pb
      pb += 1
    else:
      pa -= 1

  return max_num


def main():
  n, m, k = map(int, input().split())
  a = list(map(int, input().split()))
  b = list(map(int, input().split()))
  print(C(n, m, k, a, b))


if __name__ == '__main__':
  main()