Buggy python submission (verdict: Time Limit Exceeded):
def load_input():
    n, k = list(map(int, input().split()))
    s = list(input())
    return n, k, s


def main():
    n, k, s = load_input()

    starts = [0]
    ends = []
    for i in range(n):
        if 0 < i and s[i - 1] == '1' and s[i] == '0':
            ends.append(i)

        if 0 < i and s[i - 1] == '0' and s[i] == '1':
            starts.append(i)
    ends.append(n)

    ml = 0
    for start in starts:
        order = k
        if start == 0 and s[0] == '0':
            order -= 1
        for end in ends:
            if start < end:
                if order == 0:
                    if ml < end - start:
                        ml = end - start
                    break
                order -= 1
        if ml < end - start:
            ml = end - start
    print(ml)


if __name__ == '__main__':
    main()


Fixed submission:
def binarySearch(alist, item):
    first = 0
    last = len(alist) - 1

    while first <= last:
        midpoint = (first + last)//2
        if item < alist[midpoint] and alist[midpoint - 1] < item or midpoint == 0 and item < alist[midpoint]:
            return midpoint
        else:
            if item < alist[midpoint]:
                last = midpoint - 1
            else:
                first = midpoint + 1
    return -1


def load_input():
    n, k = list(map(int, input().split()))
    s = list(input())
    return n, k, s


def main():
    n, k, s = load_input()

    starts = [0]
    ends = []
    for i in range(n):
        if 0 < i and s[i - 1] == '1' and s[i] == '0':
            ends.append(i)

        if 0 < i and s[i - 1] == '0' and s[i] == '1':
            starts.append(i)
    ends.append(n)

    ml = 0
    for start in starts:
        order = k
        if start == 0 and s[0] == '0':
            order -= 1
        i = binarySearch(ends, start)
        if i + order < len(ends):
            end = ends[i + order]
        else:
            end = ends[-1]
        if ml < end - start:
            ml = end - start
    print(ml)


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def load_input():
    n, k = list(map(int, input().split()))
    s = list(input())
    return n, k, s


def main():
    n, k, s = load_input()

    max_len = 0
    init_pos = 0
    while True:
        next_init = False
        order = k
        current = 1
        prev = s[init_pos]
        if s[init_pos] == '0':
            order -= 1
        for i in range(init_pos + 1, len(s)):
            if prev != s[i]:
                if s[i] == '0':
                    if 0 < order:
                        order -= 1
                    else:
                        if max_len < current:
                            max_len = current
                        break
                else:
                    if next_init is False:
                        next_init = True
                        init_pos = i
            prev = s[i]
            current += 1
        if max_len < current:
            max_len = current
        if next_init is False:
            break
    print(max_len)


if __name__ == '__main__':
    main()


Fixed submission:
def binarySearch(alist, item):
    first = 0
    last = len(alist) - 1

    while first <= last:
        midpoint = (first + last)//2
        if item < alist[midpoint] and alist[midpoint - 1] < item or midpoint == 0 and item < alist[midpoint]:
            return midpoint
        else:
            if item < alist[midpoint]:
                last = midpoint - 1
            else:
                first = midpoint + 1
    return -1


def load_input():
    n, k = list(map(int, input().split()))
    s = list(input())
    return n, k, s


def main():
    n, k, s = load_input()

    starts = [0]
    ends = []
    for i in range(n):
        if 0 < i and s[i - 1] == '1' and s[i] == '0':
            ends.append(i)

        if 0 < i and s[i - 1] == '0' and s[i] == '1':
            starts.append(i)
    ends.append(n)

    ml = 0
    for start in starts:
        order = k
        if start == 0 and s[0] == '0':
            order -= 1
        i = binarySearch(ends, start)
        if i + order < len(ends):
            end = ends[i + order]
        else:
            end = ends[-1]
        if ml < end - start:
            ml = end - start
    print(ml)


if __name__ == '__main__':
    main()