Buggy python submission (verdict: Runtime Error):
    n,k=map(int, input().split())
    s=input()
    
    ruiwa = [0]

    for i in range(1,len(s)):
        if s[i] != s[i-1]:
            ruiwa.append(i)
    ruiwa.append(n)

    if s[0] == "1":
        ans = ruiwa[1]
        if ((len(ruiwa) - 1) // 2) <= k:
            ans = n
        else:
            for i in range(((len(ruiwa)-1)//2)-k+1):
                ans = max(ans,ruiwa[min(2*k+1+2*i,len(ruiwa)-1)] - ruiwa[2*i])
    else:
        ans = 0
        if len(ruiwa) - 1 - (len(ruiwa) - 1) // 2 <= k:
            ans = n
        else:
            for i in range(len(ruiwa) - 1 - (len(ruiwa)-1)//2 - k + 1):
                ans = max(ans, ruiwa[min(2*k+2*i, len(ruiwa)-1)] - ruiwa[max(0,2*i-1)])

    print(ans)


Fixed submission:
n,k = [int(i) for i in input().split()]
s = input()

if s[0] == "1":
    retu = "whiteblack"
else:
    retu = "blackwhite"


count = 0
pre_person = s[0]
groups = []
for i in range(n):
    person = s[i]

    if person != pre_person:
        groups.append(count)
        count = 0
        pre_person = person
    count += 1
groups.append(count)


maxscore1 = 0
if retu == "whiteblack":
    score1 = 0
    blackgroups = len(groups)//2
    if k < blackgroups:
        start_index = 1
        score1 = groups[start_index-1]
        for j in range(k):
            score1 += groups[start_index+2*j]
            if start_index + 2*j+1 < len(groups):
                score1 += groups[start_index+2*j+1]
        maxscore1 = max(score1, maxscore1)
        for i in range(blackgroups - k):
            score1 -= groups[start_index + 2*i - 1]
            score1 -= groups[start_index + 2*i]
            score1 += groups[start_index + 2*k +2*i]
            if start_index+2*k+1+2*i < len(groups):
                score1 += groups[start_index + 2 * k +1 + 2 * i]
            maxscore1 = max(score1, maxscore1)
    else:
        maxscore1 = n

else:
    score1 = 0
    blackgroups = len(groups) - len(groups)//2
    if k < blackgroups:
        start_index = 0
        score1 = 0
        for j in range(k):
            score1 += groups[start_index+2*j]
            if start_index + 2*j+1 < len(groups):
                score1 += groups[start_index+2*j+1]

        maxscore1 = max(score1, maxscore1)
        for i in range(blackgroups - k):
            if i >=1:
                score1 -= groups[start_index + 2*i - 1]
            score1 -= groups[start_index + 2*i]
            score1 += groups[start_index + 2*k +2*i]
            if start_index+2*k+1+2*i < len(groups):
                score1 += groups[start_index + 2 * k + 1 + 2 * i]

            maxscore1 = max(score1, maxscore1)
    else:
        maxscore1 = n

print(maxscore1)

Buggy python submission (verdict: Time Limit Exceeded):
n,k = [int(i) for i in input().split()]
s = input()

if s[0] == "1":
    retu = "whiteblack"
else:
    retu = "blackwhite"


count = 0
pre_person = s[0]
groups = []
for i in range(n):
    person = s[i]

    if person != pre_person:
        groups.append(count)
        count = 0
        pre_person = person
    count += 1
groups.append(count)


maxscore1 = 0
if retu == "whiteblack":
    score1 = 0
    blackgroups = len(groups)//2
    if k < blackgroups:
        for i in range(blackgroups-k+1):
            start_index = 2*i + 1
            score1 = groups[start_index-1]
            for j in range(k):
                score1 += groups[start_index+2*j]
                if start_index + 2*j+1 < len(groups):
                    score1 += groups[start_index+2*j+1]
            maxscore1 = max(score1, maxscore1)
    else:
        maxscore1 = n

else:
    score1 = 0
    blackgroups = len(groups) - len(groups)//2
    if k < blackgroups:
        for i in range(blackgroups-k+1):
            start_index = 2*i
            if start_index-1 >= 0:
                score1 = groups[start_index-1]
            else:
                score1 = 0
            for j in range(k):
                score1 += groups[start_index+2*j]
                if start_index + 2*j+1 < len(groups):
                    score1 += groups[start_index+2*j+1]
            maxscore1 = max(score1,maxscore1)
    else:
        maxscore1 = n

print(maxscore1)

Fixed submission:
n,k = [int(i) for i in input().split()]
s = input()

if s[0] == "1":
    retu = "whiteblack"
else:
    retu = "blackwhite"


count = 0
pre_person = s[0]
groups = []
for i in range(n):
    person = s[i]

    if person != pre_person:
        groups.append(count)
        count = 0
        pre_person = person
    count += 1
groups.append(count)


maxscore1 = 0
if retu == "whiteblack":
    score1 = 0
    blackgroups = len(groups)//2
    if k < blackgroups:
        start_index = 1
        score1 = groups[start_index-1]
        for j in range(k):
            score1 += groups[start_index+2*j]
            if start_index + 2*j+1 < len(groups):
                score1 += groups[start_index+2*j+1]
        maxscore1 = max(score1, maxscore1)
        for i in range(blackgroups - k):
            score1 -= groups[start_index + 2*i - 1]
            score1 -= groups[start_index + 2*i]
            score1 += groups[start_index + 2*k +2*i]
            if start_index+2*k+1+2*i < len(groups):
                score1 += groups[start_index + 2 * k +1 + 2 * i]
            maxscore1 = max(score1, maxscore1)
    else:
        maxscore1 = n

else:
    score1 = 0
    blackgroups = len(groups) - len(groups)//2
    if k < blackgroups:
        start_index = 0
        score1 = 0
        for j in range(k):
            score1 += groups[start_index+2*j]
            if start_index + 2*j+1 < len(groups):
                score1 += groups[start_index+2*j+1]

        maxscore1 = max(score1, maxscore1)
        for i in range(blackgroups - k):
            if i >=1:
                score1 -= groups[start_index + 2*i - 1]
            score1 -= groups[start_index + 2*i]
            score1 += groups[start_index + 2*k +2*i]
            if start_index+2*k+1+2*i < len(groups):
                score1 += groups[start_index + 2 * k + 1 + 2 * i]

            maxscore1 = max(score1, maxscore1)
    else:
        maxscore1 = n

print(maxscore1)