Problem p02623 (python)

Fixed submission:
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
cum_a = [0] * (n + 1)
for i, v in enumerate(a):
    cum_a[i + 1] = cum_a[i] + a[i]
cum_b = [0] * (m + 1)
for i, v in enumerate(b):
    cum_b[i + 1] = cum_b[i] + b[i]

a_idx = n
b_idx = 0
res = 0
for i in range(n + 1):
    current_a_idx = a_idx - i
    sum_a = cum_a[current_a_idx]
    if sum_a > k:
        continue
    sum_b = cum_b[b_idx]
    while sum_a + sum_b <= k and b_idx < m:
        b_idx += 1
        sum_b = cum_b[b_idx]
        if sum_a + sum_b > k:
            b_idx -= 1
            break
    res = max(b_idx + current_a_idx, res)
a_idx = 0
b_idx = m
for i in range(m + 1):
    current_b_idx = b_idx - i
    sum_b = cum_b[current_b_idx]
    if sum_b > k:
        continue
    sum_a = cum_a[a_idx]
    while sum_a + sum_b <= k and a_idx < n:
        a_idx += 1
        sum_a = cum_a[a_idx]
        if sum_a + sum_b > k:
            a_idx -= 1
            break
    res = max(current_b_idx + a_idx, res)

print(res)

Buggy submission (verdict: Runtime Error):
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
cum_a = [0]*(n+1)
for i, v in enumerate(a):
    cum_a[i+1] = cum_a[i]+a[i]
cum_b = [0]*(m+1)
for i, v in enumerate(b):
    cum_b[i+1] = cum_b[i]+b[i]
cum_b = [i for i in cum_b if i <= k]
cum_a = [i for i in cum_a if i <= k]
n = len(cum_a)-1
m = len(cum_b)-1
a_idx = n
b_idx = 0
res = 0
for i in range(n+1):
    current_a_idx = a_idx-i
    sum_a = cum_a[current_a_idx]
    if sum_a > k:
        continue
    sum_b = cum_b[b_idx]
    while sum_a+sum_b <= k and b_idx < m:
        b_idx += 1
        sum_b = cum_b[b_idx]
        if sum_a + sum_b > k:
            b_idx -= 1
            break
    res = max(b_idx+current_a_idx, res)
a_idx = 0
b_idx = m
for i in range(n+1):
    current_b_idx = b_idx-i
    sum_b = cum_b[current_b_idx]
    if sum_b > k:
        continue
    sum_a = cum_a[a_idx]
    while sum_a+sum_b <= k and a_idx < n:
        a_idx += 1
        sum_a = cum_a[a_idx]
        if sum_a + sum_b > k:
            a_idx -= 1
            break
    res = max(current_b_idx+a_idx, res)


print(res)

Buggy submission (verdict: Runtime Error):
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
cum_a = [0]*(n+1)
for i, v in enumerate(a):
    cum_a[i+1] = cum_a[i]+a[i]
cum_b = [0]*(m+1)
for i, v in enumerate(b):
    cum_b[i+1] = cum_b[i]+b[i]


a_idx = n
b_idx = 0
res = 0
for i in range(n+1):
    current_a_idx = a_idx-i
    sum_a = cum_a[current_a_idx]
    if sum_a > k:
        continue
    sum_b = cum_b[b_idx]
    while sum_a+sum_b <= k and b_idx < m:
        b_idx += 1
        sum_b = cum_b[b_idx]
        if sum_a + sum_b > k:
            b_idx -= 1
            break
    res = max(b_idx+current_a_idx, res)
a_idx = 0
b_idx = m
for i in range(n+1):
    current_b_idx = b_idx-i
    sum_b = cum_b[current_b_idx]
    if sum_b > k:
        continue
    sum_a = cum_a[a_idx]
    while sum_a+sum_b <= k and a_idx < n:
        a_idx += 1
        sum_a = cum_a[a_idx]
        if sum_a + sum_b > k:
            a_idx -= 1
            break
    res = max(current_b_idx+a_idx, res)


print(res)