Problem p02623 (python)

Fixed submission:
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

cnt = 0
cost = 0
for i, x in enumerate(a):
    cost += x
    if cost > k:
        cnt = i
        cost -= x
        break
else:
    for j, y in enumerate(b):
        cost += y
        if cost > k:
            cnt = n + j
            cost -= y
            break
    else:
        cnt = n + m

if cnt <= n:
    i_max, j = cnt - 1, 0
else:
    i_max, j = n - 1, cnt - n

while j < m and cost + b[j] <= k:
    cost += b[j]
    j += 1
    cnt += 1

ans = cnt
for i in range(i_max, -1, -1):
    if j == m: break
    cost -= a[i]
    cnt -= 1
    while j < m and cost + b[j] <= k:
        cost += b[j]
        j += 1
        cnt += 1

    ans = max(cnt, ans)

print(ans)

Buggy submission (verdict: Runtime Error):
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

cnt = 0
cost = 0
for i, x in enumerate(a):
    cost += x
    if cost > k:
        cnt = i
        cost -= x
        break
else:
    for j, y in enumerate(b):
        cost += y
        if cost > k:
            cnt = n + j
            cost -= y
            break
    else:
        cnt = n + m

if cnt <= n:
    i_max, j = cnt - 1, 0
else:
    i_max, j = n - 1, cnt - n + 1

while cost + b[j] <= k and j < m - 1:
    j += 1
    cnt += 1
    cost += b[j]

ans = cnt
for i_max in range(i_max, -1, -1):
    cost -= a[i]
    cnt -= 1
    while cost + b[j] <= k and j < m - 1:
        j += 1
        cnt += 1
        cost += b[j]
    ans = max(cnt, ans)
    if j == m - 1: break
    i -= 1

print(ans)

Buggy submission (verdict: Runtime Error):
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

cnt = 0
cost = 0
for i, x in enumerate(a):
    cost += x
    if cost > k:
        cnt = i
        cost -= x
        break
else:
    for j, y in enumerate(b):
        cost += y
        if cost > k:
            cnt = n + j
            cost -= y
            break
    else:
        cnt = n + m

if cnt <= n:
    i, j = cnt, 0
else:
    i, j = n, cnt - n + 1

ans = cnt
initial = True
while i >= 0 and j < m:
    if i < n and not initial:
        cost -= a[i]
        cnt -= 1
    while cost + b[j] <= k and j < m:
        j += 1
        cnt += 1
        cost += b[j]
    ans = max(cnt, ans)
    i -= 1
    if initial: initial = False

print(ans)

Buggy submission (verdict: Runtime Error):
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

ans = 0
cnt = 0
cost = 0
for i, x in enumerate(a):
    cost += x
    if cost > k:
        cnt = i
        cost -= x
        break
else:
    for j, y in enumerate(b):
        cost += y
        if cost > k:
            cnt = n + j
            cost -= y
            break
    else:
        ans = n + m

if cnt <= n:
    i, j = cnt - 1, 0
else:
    i, j = n - 1, cnt - n + 1

while True:
    if cost == 0: break
    if i == -1 or j == m: break
    while cost + b[j] <= k and j < m:
        j += 1
        cnt += 1
        cost += b[j]
    cost += - a[i] + b[j]
    i, j = i - 1, j + 1
    if cost > k: break
if ans != 0:
    print(ans)
else:
    print(cnt)

Buggy submission (verdict: Runtime Error):
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

ans = 0
cnt = 0
cost = 0
for i, x in enumerate(a):
    cost += x
    if cost > k:
        cnt = i
        cost -= x
        break
else:
    for j, y in enumerate(b):
        cost += y
        if cost > k:
            cnt = n + j
            cost -= y
            break
    else:
        ans = n + m

if cnt <= n:
    i, j = cnt - 1, 0
else:
    i, j = n - 1, cnt - n + 1

while True:
    if cost == 0: break
    if i == n or j == -1: break
    while cost + b[j] <= k:
        j += 1
        cnt += 1
        cost += b[j]
    cost += - a[i] + b[j]
    i, j = i - 1, j + 1
    if cost > k: break
if ans != 0:
    print(ans)
else:
    print(cnt)

Buggy submission (verdict: Runtime Error):
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

ans = 0
cnt = 0
cost = 0
for i, x in enumerate(a):
    cost += x
    if cost > k:
        cnt = i
        cost -= x
        break
else:
    for j, y in enumerate(b):
        cost += y
        if cost > k:
            cnt = n + j
            cost -= y
            break
    else:
        ans = n + m

if cnt <= n:
    i, j = cnt - 1, 0
else:
    i, j = n - 1, cnt - n + 1

while True:
    if cost == 0: break
    cost += - a[i] + b[j]
    i, j = i - 1, j + 1
    while cost + b[j] <= k:
        j += 1
        cnt += 1
    if cost > k: break
if ans != 0:
    print(ans)
else:
    print(cnt)

Buggy submission (verdict: Runtime Error):
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

ans = 0
cnt = 0
cost = 0
for i, x in enumerate(a):
    cost += x
    if cost > k:
        cnt = i
        cost -= x
        break
else:
    for j, y in enumerate(b):
        cost += y
        if cost > k:
            cnt = n + j
            cost -= y
            break
    else:
        ans = n + m

if cnt <= n:
    i, j = cnt - 1, 0
else:
    i, j = n - 1, cnt - n + 1

while True:
    if cost == 0: break
    cost += - a[i] + b[j]
    while cost + b[j] <= k:
        j += 1
        cnt += 1
    if cost > k: break
if ans != 0:
    print(ans)
else:
    print(cnt)