Problem p03074 (python)

Fixed submission:
n, k = list(map(int, input().split()))
s = input()
now = 1
cnt = 0
Nums = []
for i in range(len(s)):
    if s[i] == str(now):
        cnt += 1
    else:
        Nums.append(cnt)
        now = 1 - now
        cnt = 1
if cnt != 0:
    Nums.append(cnt)
if len(Nums) % 2 == 0:
    Nums.append(0)

ans = 0
Add = 2 * k + 1

left = 0
right = 0
tmp = 0

for i in range(len(Nums))[::2]:
    Nextleft = i
    Nextright = min(i + Add, len(Nums))

    while Nextleft > left:
        tmp -= Nums[left]
        left += 1

    while Nextright > right:
        tmp += Nums[right]
        right += 1

    ans = max(tmp, ans)
print(ans)

Buggy submission (verdict: Runtime Error):
import sys
from collections import deque
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces

n, k = na()
s = input()
s = s + "2"
li_0 = []
li_1 = []
ct = 1
for i in range(n):
    if s[i] != s[i+1]:
        if s[i] == '0':
            li_0.append(ct)
            ct = 1
        else:
            li_1.append(ct)
            ct = 1
    else:
        ct += 1
if len(li_0) <= k:
    print(len(s)-1)
else:
    li_0 = deque(li_0)
    li_1 = deque(li_1)
    if s[0] == "0":
        li_1.appendleft(0)
    li_1.append(0)
    Sum_0 = sum(li_0[i] for i in range(k))
    Sum_1 = sum(li_1[i] for i in range(k+1))
    Max = Sum_0 + Sum_1
    for i in range(len(li_1)-k-1):
        Sum_0 = Sum_0 - li_0[i] + li_0[i+k]
        Sum_1 = Sum_1 - li_1[i] + li_1[i+k+1]
        Max = max(Max, Sum_0 + Sum_1)
    print(Max)

Buggy submission (verdict: Runtime Error):
n, k = list(map(int, input().split()))
s = input()
now = 1
cnt = 0
Nums = []
for i in range(len(s)):
    if s[i] == str(now):
        cnt += 1
    else:
        Nums.append(cnt)
        now = 1 - now
        cnt = 1
if cnt != 0:
    Nums.append(cnt)
if len(Nums) % 2 == 0:
    Nums.append(0)

ans = 0
Add = 2 * k + 1

sum = [0] * (len(Nums) + 1)

for i in range(len(sum)):
    sum[i + 1] = sum[i] + Nums[i]

for i in range(len(Nums))[::2]:
    left = i
    right = min(i + Add, len(Nums))
    tmp = sum[right] - sum[left]
    ans = max(tmp, ans)
    
print(ans)

Buggy submission (verdict: Runtime Error):
n, k = list(map(int, input().split()))
s = input()
now = 1
cnt = 0
Nums = []
for i in range(len(s)):
    if s[i] == str(now):
        cnt += 1
    else:
        Nums.append(cnt)
        now = 1 - now
        cnt = 1
if cnt != 0:
    Nums.append(cnt)
if len(Nums) % 2 == 0:
    Nums.append(0)

ans = 0
Add = 2 * k + 1

sum = [0] * (len(Nums) + 1)

for i in range(len(sum)):
    sum[i + 1] = sum[i] + Nums[i]

for i in range(len(Nums))[::2]:
    left = i
    right = min(i + Add, len(Nums))
    tmp = sum[left] - sum[right]
    ans = max(tmp, ans)
    
print(ans)

Buggy submission (verdict: Runtime Error):
N, K = list(map(int, input().split()))
S = input()
ct = 1
li0 = []
li1 = []
cap = S[0]
su = 0
if N != 1:
    for i in range(N - 1):
        if S[i + 1] == S[i]:
            ct += 1
        else:
            if S[i] == '0':
                li0.append(ct)
            else:
                li1.append(ct)
            ct = 1
    if S[i+1] == '0':
        li0.append(ct)
    else:
        li1.append(ct)
    for i in range(len(li0) - K + 1):
        num = sum(li0[i:i + K])
        if su < num:
            su = num
            idx = i
    print(max((su + sum(li1[idx:idx + K+1])), max(li1)))
else:
    print(1)

Buggy submission (verdict: Runtime Error):
N, K = list(map(int, input().split()))
S = input()
ct = 1
li0 = []
li1 = []
cap = S[0]
su = 0
if N != 1:
    for i in range(N - 1):
        if S[i + 1] == S[i]:
            ct += 1
        else:
            if S[i] == '0':
                li0.append(ct)
            else:
                li1.append(ct)
            ct = 1
    if S[i+1] == '0':
        li0.append(ct)
    else:
        li1.append(ct)
    for i in range(len(li0) - K + 1):
        num = sum(li0[i:i + K])
        if su < num:
            su = num
            idx = i
    print(max((su + sum(li1[idx:idx + K+1])), max(li1)))
else:
    print(1)