Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().split()) # 横に2個
S = [int(i) for i in list(input())]

N0 = []
N1 = []
n0 = 0
n1 = 0
for s in S:
    if s == 0:
        if n1 > 0:
            N1.append(n1)
            n1 = 0
        n0 += 1
    if s == 1:
        if n0 > 0:
            N0.append(n0)
            n0 = 0
        n1 += 1
if n0 > 0:
    N0.append(n0)
if n1 > 0:
    N1.append(n1)

M0 = [0]
m0 = 0
for n0 in N0:
    m0 += n0
    M0.append(m0)

M1 = [0]
m1 = 0
for n1 in N1:
    m1 += n1
    M1.append(m1)
#print(M0)
#print(M1)

c = 0 if S[0] == 0 else 1

if len(N1) == 1 and len(N0) == 0:
    ans = N1[0]
elif len(N1) == 0 and len(N0) == 1:
    ans = N0[0]
else:
    ans = 0
    for i in range(N):
        if i + K >= len(M0):
            break
        if c == 0:
            if i == 0:
                l = M0[K] + M1[i+K]
            elif i == len(M1)-1:
                l = M0[i+K]-M0[i] + M1[i+K-1]-M1[i-1]
            else:
                l = M0[i+K]-M0[i] + M1[i+K]-M1[i-1]
        else:
            if i == len(M1)-2:
                l = M0[i+K]-M0[i] + M1[i+K]-M1[i]
            else:
                l = M0[i+K]-M0[i] + M1[i+K+1]-M1[i]
        #print(i, l)
        ans = max(ans, l)
print(ans)

Fixed submission:
N, K = map(int, input().split())
S = list(input())

l = 0
r = 0
change = 0 if S[0] == '1' else 1
ans = 0
while l < N and r < N:
    #print(l, r, change)
    ans = max(ans, r-l+1)
    if r == N-1:
        break
    if S[r] == '0':
        r += 1
    else:
        if S[r+1] == '0':
            if change < K:
                change += 1
                r += 1
            else:
                if S[l] == '0' and S[l+1] == '1':
                    r += 1
                l += 1
        else:
            r += 1
print(ans)

Buggy python submission (verdict: Runtime Error):
N, K = map(int, input().split()) # 横に2個
S = [int(i) for i in list(input())]

N0 = []
N1 = []
n0 = 0
n1 = 0
for s in S:
    if s == 0:
        if n1 > 0:
            N1.append(n1)
            n1 = 0
        n0 += 1
    if s == 1:
        if n0 > 0:
            N0.append(n0)
            n0 = 0
        n1 += 1
if n0 > 0:
    N0.append(n0)
if n1 > 0:
    N1.append(n1)

M0 = [0]
m0 = 0
for n0 in N0:
    m0 += n0
    M0.append(m0)

M1 = [0]
m1 = 0
for n1 in N1:
    m1 += n1
    M1.append(m1)
#print(M0)
#print(M1)

#d = 0 if len(M0)==len(M1) and c == 0


if len(N1) == 1 and len(N0) == 0:
    ans = N1[0]
elif len(N1) == 0 and len(N0) == 1:
    ans = N0[0]
else:
    ans = 0
    for i in range(N):
        if i+K+1 >= len(M1):
            a = M1[-1]-M1[-1-K]
            if i == 0:
                ans = a
        else:
            a = M1[i+K+1]-M1[i]
        if i + K >= len(M0):
            break
        l = M0[i+K]-M0[i] + a
        #print(l)
        ans = max(ans, l)
print(ans)

Fixed submission:
N, K = map(int, input().split())
S = list(input())

l = 0
r = 0
change = 0 if S[0] == '1' else 1
ans = 0
while l < N and r < N:
    #print(l, r, change)
    ans = max(ans, r-l+1)
    if r == N-1:
        break
    if S[r] == '0':
        r += 1
    else:
        if S[r+1] == '0':
            if change < K:
                change += 1
                r += 1
            else:
                if S[l] == '0' and S[l+1] == '1':
                    r += 1
                l += 1
        else:
            r += 1
print(ans)