Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
s = input() # str

zero_lens = []
one_lens = []
flag_zero_before = 'None'
zeros = 0
ones = 0

for i in range(n):
    if s[i] == '0':
        if flag_zero_before == False:
            one_lens.append(ones)
            ones = 0
        zeros += 1
        flag_zero_before = True
    if s[i] == '1':
        if flag_zero_before and zeros != 0:
            zero_lens.append(zeros)
            zeros = 0
        ones += 1
        flag_zero_before = False

if s[-1] == '0':
    zero_lens.append(zeros)
if s[-1] == '1':
    one_lens.append(ones)

k = min(k, len(zero_lens))

counters = []
if s[0] == '1':
    for i in range(len(zero_lens) - k + 1):
        if i + k == len(one_lens):
            counter = sum(zero_lens[i: i+k]) + sum(one_lens[i: i+k])
        else:
            counter = sum(zero_lens[i: i+k]) + sum(one_lens[i: i+k+1])
        counters.append(counter)
elif s[0] == '0':
    for i in range(len(zero_lens) - k + 1):
        if i == 0:
            counter = sum(zero_lens[i: i+k]) + sum(one_lens[i: i+k])
        elif i + k > len(one_lens):
            counter = sum(zero_lens[i: i+k]) + sum(one_lens[i-1: i+k-1])
        else:
            counter = sum(zero_lens[i: i+k]) + sum(one_lens[i-1: i+k])
        counters.append(counter)

print(max(counters))


Fixed submission:
n, k = map(int, input().split())
s = input()

counts = []
tmp = []
for i in range(len(s)):
    if tmp == []:
        tmp = [s[i], 1]
    else:
        if tmp[0] == s[i]:
            tmp[1] += 1
        else:
            counts.append(tmp)
            tmp = [s[i], 1]
if tmp:
    counts.append(tmp)

zero_ints = 0
for char, count in counts:
    if char == '0':
        zero_ints += 1

if counts[0][0] == '0':
    left = 0
else:
    left = 1
right = left - 2

used = 0
l = 0
for char, count in counts:
    if char == '0':
        if used < k:
            used += 1
            right += 2
        else:
            break
    l += count

ans = l
for i in range(zero_ints):
    l = l - counts[left][1]
    if left - 1 >= 0:
        l-= counts[left - 1][1]
    if right + 2 < len(counts):
        l += counts[right + 2][1]
    if right + 3 < len(counts):
        l += counts[right + 3][1]
    ans = max(ans, l)
    left += 2
    right += 2
    # print(left, right, l)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
s = input() # str

zero_lens = []
one_lens = []
flag_zero_before = 'None'
zeros = 0
ones = 0

for i in range(n):
    if s[i] == '0':
        if flag_zero_before == False:
            one_lens.append(ones)
            ones = 0
        zeros += 1
        flag_zero_before = True
    if s[i] == '1':
        if flag_zero_before and zeros != 0:
            zero_lens.append(zeros)
            zeros = 0
        ones += 1
        flag_zero_before = False

if s[-1] == '0':
    zero_lens.append(zeros)
if s[-1] == '1':
    one_lens.append(ones)

k = min(k, len(zero_lens))

counters = []
if s[0] == '1':
    for i in range(len(zero_lens) - k + 1):
        if i + k == len(one_lens):
            counter = sum(zero_lens[i: i+k]) + sum(one_lens[i: i+k])
        else:
            counter = sum(zero_lens[i: i+k]) + sum(one_lens[i: i+k+1])
        counters.append(counter)
elif s[0] == '0':
    for i in range(len(zero_lens) - k + 1):
        if i == 0:
            counter = sum(zero_lens[i: i+k]) + sum(one_lens[i: i+k])
        elif i + k > len(one_lens):
            counter = sum(zero_lens[i: i+k]) + sum(one_lens[i-1: i+k-1])
        else:
            counter = sum(zero_lens[i: i+k]) + sum(one_lens[i-1: i+k])
        counters.append(counter)

print(max(counters))


Fixed submission:
n, k = map(int, input().split())
s = input()

counts = []
tmp = []
for i in range(len(s)):
    if tmp == []:
        tmp = [s[i], 1]
    else:
        if tmp[0] == s[i]:
            tmp[1] += 1
        else:
            counts.append(tmp)
            tmp = [s[i], 1]
if tmp:
    counts.append(tmp)

zero_ints = 0
for char, count in counts:
    if char == '0':
        zero_ints += 1

if counts[0][0] == '0':
    left = 0
else:
    left = 1
right = left - 2

used = 0
l = 0
for char, count in counts:
    if char == '0':
        if used < k:
            used += 1
            right += 2
        else:
            break
    l += count

ans = l
for i in range(zero_ints):
    l = l - counts[left][1]
    if left - 1 >= 0:
        l-= counts[left - 1][1]
    if right + 2 < len(counts):
        l += counts[right + 2][1]
    if right + 3 < len(counts):
        l += counts[right + 3][1]
    ans = max(ans, l)
    left += 2
    right += 2
    # print(left, right, l)
print(ans)

Buggy python submission (verdict: Runtime Error):
n, k = map(int, input().split())
s = input() # str

zero_positions = []
flag = False
zeros = []
for i in range(n):
    if s[i] == '0':
        zeros.append(i)
        flag = True
    if s[i] == '1' and flag:
        flag = False
        zero_positions.append(zeros)
        zeros = []
if zeros != []:
    zero_positions.append(zeros)

def get_one_positions(s):
    one_positions_len = []
    flag = False
    ones_len = 0
    for i in range(n):
        if s[i] == '1':
            ones_len += 1
            flag = True
        if s[i] == '0' and flag:
            flag = False
            one_positions_len.append(ones_len)
            ones_len = 0
    if ones_len != []:
        one_positions_len.append(ones_len)
    return(max(one_positions_len))

one_positions_lens = []
for i in range(len(zero_positions) - k + 1):
    choice = []
    for j in range(k):
        choice.extend(zero_positions[i + j])

    # reverse
    s_new = []
    for l in range(n):
        if l in choice:
            s_new.append('1')
        elif s[l] == '1':
            s_new.append('1')
        elif s[l] == '0':
            s_new.append('0')


    # count
    one_positions_len = get_one_positions(s_new)
    one_positions_lens.append(one_positions_len)

print(max(one_positions_lens))


Fixed submission:
n, k = map(int, input().split())
s = input()

counts = []
tmp = []
for i in range(len(s)):
    if tmp == []:
        tmp = [s[i], 1]
    else:
        if tmp[0] == s[i]:
            tmp[1] += 1
        else:
            counts.append(tmp)
            tmp = [s[i], 1]
if tmp:
    counts.append(tmp)

zero_ints = 0
for char, count in counts:
    if char == '0':
        zero_ints += 1

if counts[0][0] == '0':
    left = 0
else:
    left = 1
right = left - 2

used = 0
l = 0
for char, count in counts:
    if char == '0':
        if used < k:
            used += 1
            right += 2
        else:
            break
    l += count

ans = l
for i in range(zero_ints):
    l = l - counts[left][1]
    if left - 1 >= 0:
        l-= counts[left - 1][1]
    if right + 2 < len(counts):
        l += counts[right + 2][1]
    if right + 3 < len(counts):
        l += counts[right + 3][1]
    ans = max(ans, l)
    left += 2
    right += 2
    # print(left, right, l)
print(ans)