Buggy python submission (verdict: Time Limit Exceeded):
nums = list(map(int, input().split()))
N, K = nums[0], nums[1]
S = input()

first_one = S[0] == '1'
cnts = []
cnt = 1
for i, e in enumerate(S):
    if i == 0:
        pass
    else:
        if e != S[i-1]:
            cnts.append(cnt)
            cnt = 1
        else:
            cnt += 1
cnts.append(cnt)


zero_obj = []
for i, c in enumerate(cnts):
    if first_one:
        if i % 2 == 1:
            left = 0
            if i - 1 >= 0:
                left = cnts[i-1]
            right = 0
            if i + 1 < len(cnts):
                right = cnts[i+1]
            zero_obj.append((left, c, right))
    else:
        if i % 2 == 0:
            left = 0
            if i - 1 >= 0:
                left = cnts[i-1]
            right = 0
            if i + 1 < len(cnts):
                right = cnts[i+1]
            zero_obj.append((left, c, right))

if len(zero_obj) <= K:
    print(len(S))
else:
    m = -1
    for i in range(len(zero_obj) - K + 1):
        tmp = zero_obj[i][0]
        for j in range(K):
            tmp += zero_obj[i+j][1] + zero_obj[i+j][2]
        if tmp > m:
            m = tmp
    print(m)


Fixed submission:
nums = list(map(int, input().split()))
N, K = nums[0], nums[1]
S = input()

first_one = S[0] == '1'
cnts = []
cnt = 1
for i, e in enumerate(S):
    if i == 0:
        pass
    else:
        if e != S[i-1]:
            cnts.append(cnt)
            cnt = 1
        else:
            cnt += 1
cnts.append(cnt)


zero_obj = []
for i, c in enumerate(cnts):
    if first_one:
        if i % 2 == 1:
            left = 0
            if i - 1 >= 0:
                left = cnts[i-1]
            right = 0
            if i + 1 < len(cnts):
                right = cnts[i+1]
            zero_obj.append((left, c, right))
    else:
        if i % 2 == 0:
            left = 0
            if i - 1 >= 0:
                left = cnts[i-1]
            right = 0
            if i + 1 < len(cnts):
                right = cnts[i+1]
            zero_obj.append((left, c, right))

if len(zero_obj) <= K:
    print(len(S))
else:
    m = -1
    tmp = zero_obj[0][0]
    for i in range(K):
        tmp += zero_obj[i][1] + zero_obj[i][2]
    m = tmp
    for i in range(len(zero_obj) - K):
        tmp -= zero_obj[i][0] + zero_obj[i][1]
        tmp += zero_obj[i+K][1] + zero_obj[i+K][2]
        if tmp > m:
            m = tmp
    print(m)

Buggy python submission (verdict: Time Limit Exceeded):
nums = list(map(int, input().split()))
N, K = nums[0], nums[1]
S = input()

first_one = S[0] == '1'
cnts = []
cnt = 1
for i, e in enumerate(S):
    if i == 0:
        pass
    else:
        if e != S[i-1]:
            cnts.append(cnt)
            cnt = 1
        else:
            cnt += 1
cnts.append(cnt)


zero_obj = []
for i, c in enumerate(cnts):
    if first_one:
        if i % 2 == 1:
            left = 0
            if i - 1 >= 0:
                left = cnts[i-1]
            right = 0
            if i + 1 < len(cnts):
                right = cnts[i+1]
            zero_obj.append((left, c, right))
    else:
        if i % 2 == 0:
            left = 0
            if i - 1 >= 0:
                left = cnts[i-1]
            right = 0
            if i + 1 < len(cnts):
                right = cnts[i+1]
            zero_obj.append((left, c, right))

if len(zero_obj) <= K:
    print(len(S))
else:
    m = -1
    for i in range(len(zero_obj) - K + 1):
        tmp = zero_obj[i][0]
        for j in range(K):
            tmp += zero_obj[i+j][1] + zero_obj[i+j][2]
        if tmp > m:
            m = tmp
    print(m)


Fixed submission:
nums = list(map(int, input().split()))
N, K = nums[0], nums[1]
S = input()

first_one = S[0] == '1'
cnts = []
cnt = 1
for i, e in enumerate(S):
    if i == 0:
        pass
    else:
        if e != S[i-1]:
            cnts.append(cnt)
            cnt = 1
        else:
            cnt += 1
cnts.append(cnt)


zero_obj = []
for i, c in enumerate(cnts):
    if first_one:
        if i % 2 == 1:
            left = 0
            if i - 1 >= 0:
                left = cnts[i-1]
            right = 0
            if i + 1 < len(cnts):
                right = cnts[i+1]
            zero_obj.append((left, c, right))
    else:
        if i % 2 == 0:
            left = 0
            if i - 1 >= 0:
                left = cnts[i-1]
            right = 0
            if i + 1 < len(cnts):
                right = cnts[i+1]
            zero_obj.append((left, c, right))

if len(zero_obj) <= K:
    print(len(S))
else:
    m = -1
    tmp = zero_obj[0][0]
    for i in range(K):
        tmp += zero_obj[i][1] + zero_obj[i][2]
    m = tmp
    for i in range(len(zero_obj) - K):
        tmp -= zero_obj[i][0] + zero_obj[i][1]
        tmp += zero_obj[i+K][1] + zero_obj[i+K][2]
        if tmp > m:
            m = tmp
    print(m)