Buggy python submission (verdict: Runtime Error):
def sub():
    N, K = [int(e) for e in input().split(" ")]
    ss = [int(e) for e in input()]
    if len(set(ss)) == 1:
        print(len(ss))
        return

    dd = [ss[i+1] - ss[i] for i in range(N-1)]
    idd = [0] + [i+1 for i, d in enumerate(dd) if d != 0]
    
    maxScore = 0
    for i in idd:
        if ss[i] == 0:
            ni = i + 2*K
            if ni < len(idd):
                score = idd[ni] - idd[i]
            else:
                score = N - idd[i]
            if score > maxScore:
                maxScore = score
        else:
            ni = i + 2*K + 1
            if ni < len(idd):
                score = idd[ni] - idd[i]
            else:
                score = N - idd[i]
            if score > maxScore:
                maxScore = score
    print(maxScore)

sub()


Fixed submission:
def sub():
    N, K = [int(e) for e in input().split(" ")]
    ss = [int(e) for e in input()]
    # if len(set(ss)) == 1:
    #     print(len(ss))
    #     return

    dd = [ss[i+1] - ss[i] for i in range(N-1)]
    idd = [0] + [i+1 for i, d in enumerate(dd) if d != 0]
    # print(idd)
    maxScore = 0
    for i in range(len(idd)):
        if ss[idd[i]] == 0:
            ni = i + 2*K
            if ni < len(idd):
                score = idd[ni] - idd[i]
            else:
                score = N - idd[i]
            if score > maxScore:
                maxScore = score
        else:
            ni = i + 2*K + 1
            if ni < len(idd):
                score = idd[ni] - idd[i]
            else:
                score = N - idd[i]
            if score > maxScore:
                maxScore = score
        
    print(maxScore)

sub()

Buggy python submission (verdict: Time Limit Exceeded):
def sub():
    N, K = [int(e) for e in input().split(" ")]
    ss = [int(e) for e in input()]
    if len(set(ss)) == 1:
        print(len(ss))
        return

    dd = [ss[i+1] - ss[i] for i in range(N-1)]
    memIdx = 0
    buf = []
    memNbOne = 0
    memChange = None
    for i, d in enumerate(dd):
        if d > 0:
            e = [(memIdx, i+1), 0]
            buf.append(e)
            memIdx = i+1
            memChange = d
        elif d < 0:
            e = [(memIdx, i+1), 1]
            buf.append(e)
            memIdx = i+1
            memChange = d
    if memChange > 0:
        e = [(memIdx, N), 1]
        buf.append(e)
    else:
        e = [(memIdx, N), 0]
        buf.append(e)
    # print(buf)
    maxScore = 0
    for i in range(len(buf)):
        score = 0
        count = 0
        lastNext = None
        for j in range(i, len(buf)):
            lastNext = j + 1
            b, flag = buf[j]
            score = score + b[1]-b[0]
            if flag == 0:
                count = count + 1
            if count >= K:
                break
        if i-1 >= 0:
            b, flag = buf[i-1]
            if flag == 1:
                score = score + b[1] - b[0]
        if lastNext is not None:
            if lastNext < len(buf):
                b, flag = buf[lastNext]
                if flag == 1:
                    score = score + b[1] - b[0]

        if score > maxScore:
            maxScore = score
        # print(i, score)
    print(maxScore)
sub()


Fixed submission:
def sub():
    N, K = [int(e) for e in input().split(" ")]
    ss = [int(e) for e in input()]
    # if len(set(ss)) == 1:
    #     print(len(ss))
    #     return

    dd = [ss[i+1] - ss[i] for i in range(N-1)]
    idd = [0] + [i+1 for i, d in enumerate(dd) if d != 0]
    # print(idd)
    maxScore = 0
    for i in range(len(idd)):
        if ss[idd[i]] == 0:
            ni = i + 2*K
            if ni < len(idd):
                score = idd[ni] - idd[i]
            else:
                score = N - idd[i]
            if score > maxScore:
                maxScore = score
        else:
            ni = i + 2*K + 1
            if ni < len(idd):
                score = idd[ni] - idd[i]
            else:
                score = N - idd[i]
            if score > maxScore:
                maxScore = score
        
    print(maxScore)

sub()