Buggy python submission (verdict: Runtime Error):
import sys
stdin = sys.stdin 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()
N,K=na();S=ns()
l=[]
c1=0
c0=0
x0=0
first=True
if(S[0]=="1"):
    c1=1
else:
    c0=1
for i in range(1,N):
    if(S[i]=="1"):
        if(S[i]!=S[i-1]):
            l.append(c0)
            c0=0
        c1+=1
    else:
        if(S[i]!=S[i-1]):
            x0+=1
            l.append(c1)
            c1=0
        c0+=1
if(c1):
    l.append(c1)
else:
    x0+=1
    l.append(c0)
#print(l)
#print(x0)
if(x0<=K):
    print(N)
    exit()
else:
    res=0
    if(int(S[0])>0):
        if(int(S[-1])>0):
            right = 0
            left = 2*K
            while(True):
                if(left>=len(l)-1):
                    print(res)
                    exit()
                res=max(sum(l[right:left+1]),res)
                #print(res,right,left)
                right+=2
                left+=2
        else:
            right = 0
            left = 2*K
            #print("llll")
            res=max(sum(l[len(l)-(1+(K-1)*2)-1:]),res)
            #print(len(l)-(1+(K-1)*2))
            while(True):
                if(left>=len(l)-1):
                    print(res)
                    exit()
                res=max(sum(l[right:left+1]),res)
                #print(res,right,left)
                right+=2
                left+=2
    else:
        if(int(S[-1])>0):
            res=max(res,sum(l[0:2*K]))
            right = 1
            left = 2*K+1
            while(True):
                if(left>=len(l)-1):
                    print(res)
                    exit()
                res=max(sum(l[right:left+1]),res)
                #print(res,right,left)
                right+=2
                left+=2
        else:
            right = 0
            left = 2*K
            #最後
            res=max(sum(l[len(l)-(1+(K-1)*2)-1:]),res)
            #最初
            res=max(res,sum(l[0:2*K]))
            right = 1
            left = 2*K+1
            while(True):
                if(left>=len(l)-1):
                    print(res)
                    exit()
                res=max(sum(l[right:left+1]),res)
                #print(res,right,left)
                right+=2
                left+=2


Fixed submission:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [tuple(map(int, l.split())) for l in sys.stdin]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)

def main():
    n,k = LI()
    s = [int(c) for c in S()]
    a = []
    if s[0] == 0:
        a.append(0)
    t = s[0]
    c = 1
    for b in s[1:]:
        if t == b:
            c += 1
        else:
            a.append(c)
            c = 1
            t = b
    a.append(c)
    l = len(a)
    if l // 2 <= k:
        return sum(a)

    r = t = sum(a[:k*2+1])
    if l % 2 == 0:
        a.append(0)
        l += 1
    g = k * 2 + 1
    for i in range(k*2+1,l-1,2):
        t -= a[i-g] + a[i-g+1]
        t += a[i] + a[i+1]
        if r < t:
            r = t

    return r


print(main())

Buggy python submission (verdict: Runtime Error):
import sys
stdin = sys.stdin 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()
N,K=na();S=ns()
l=[]
c1=0
c0=0
x0=0
first=True
if(S[0]=="1"):
    c1=1
else:
    c0=1
for i in range(1,N):
    if(S[i]=="1"):
        if(S[i]!=S[i-1]):
            l.append(c0)
            c0=0
        c1+=1
    else:
        if(S[i]!=S[i-1]):
            x0+=1
            l.append(c1)
            c1=0
        c0+=1
if(c1):
    l.append(c1)
else:
    x0+=1
    l.append(c0)
#print(l)
#print(x0)
if(x0<=K):
    print(N)
    exit()
else:
    res=0
    if(int(S[0])>0):
        if(int(S[-1])>0):
            right = 0
            left = 2*K
            while(True):
                if(left>=len(l)-1):
                    print(res)
                    exit()
                res=max(sum(l[right:left+1]),res)
                #print(res,right,left)
                right+=2
                left+=2
        else:
            right = 0
            left = 2*K
            #print("llll")
            res=max(sum(l[len(l)-(1+(K-1)*2)-1:]),res)
            #print(len(l)-(1+(K-1)*2))
            while(True):
                if(left>=len(l)-3):
                    print(res)
                    exit()
                res=max(sum(l[right:left+1]),res)
                #print(res,right,left)
                right+=2
                left+=2
    else:
        if(int(S[-1])>0):
            res=max(res,sum(l[0:2*K]))
            right = 1
            left = 2*K+1
            while(True):
                if(left>=len(l)-1):
                    print(res)
                    exit()
                res=max(sum(l[right:left+1]),res)
                #print(res,right,left)
                right+=2
                left+=2
        else:
            right = 0
            left = 2*K
            #最後
            res=max(sum(l[len(l)-(1+(K-1)*2)-1:]),res)
            #最初
            res=max(res,sum(l[0:2*K]))
            right = 1
            left = 2*K+1
            while(True):
                if(left>=len(l)-3):
                    print(res)
                    exit()
                res=max(sum(l[right:left+1]),res)
                #print(res,right,left)
                right+=2
                left+=2


Fixed submission:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [tuple(map(int, l.split())) for l in sys.stdin]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)

def main():
    n,k = LI()
    s = [int(c) for c in S()]
    a = []
    if s[0] == 0:
        a.append(0)
    t = s[0]
    c = 1
    for b in s[1:]:
        if t == b:
            c += 1
        else:
            a.append(c)
            c = 1
            t = b
    a.append(c)
    l = len(a)
    if l // 2 <= k:
        return sum(a)

    r = t = sum(a[:k*2+1])
    if l % 2 == 0:
        a.append(0)
        l += 1
    g = k * 2 + 1
    for i in range(k*2+1,l-1,2):
        t -= a[i-g] + a[i-g+1]
        t += a[i] + a[i+1]
        if r < t:
            r = t

    return r


print(main())

Buggy python submission (verdict: Runtime Error):
import sys
stdin = sys.stdin 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()
N,K=na();S=ns()
l=[]
c1=0
c0=0
x0=0
first=True
if(S[0]=="1"):
    c1=1
else:
    c0=1
for i in range(1,N):
    if(S[i]=="1"):
        if(S[i]!=S[i-1]):
            l.append(c0)
            c0=0
        c1+=1
    else:
        if(S[i]!=S[i-1]):
            x0+=1
            l.append(c1)
            c1=0
        c0+=1
if(c1):
    l.append(c1)
else:
    x0+=1
    l.append(c0)
#print(l)
#print(x0)
if(x0<=K):
    print(N)
    exit()
else:
    res=0
    if(int(S[0])>0):
        if(int(S[-1])>0):
            right = 0
            left = 2*K
            while(True):
                if(left>=len(l)):
                    print(res)
                    exit()
                res=max(sum(l[right:left+1]),res)
                #print(res,right,left)
                right+=2
                left+=2
        else:
            right = 0
            left = 2*K
            #print("llll")
            res=max(sum(l[len(l)-(1+(K-1)*2)-1:]),res)
            #print(len(l)-(1+(K-1)*2))
            while(True):
                if(left>=len(l)-1):
                    print(res)
                    exit()
                res=max(sum(l[right:left+1]),res)
                #print(res,right,left)
                right+=2
                left+=2
    else:
        if(int(S[-1])>0):
            res=max(res,sum(l[0:2*K]))
            right = 1
            left = 2*K+1
            while(True):
                if(left>=len(l)):
                    print(res)
                    exit()
                res=max(sum(l[right:left+1]),res)
                #print(res,right,left)
                right+=2
                left+=2
        else:
            right = 0
            left = 2*K
            #最後
            res=max(sum(l[len(l)-(1+(K-1)*2)-1:]),res)
            #最初
            res=max(res,sum(l[0:2*K]))
            right = 1
            left = 2*K+1
            while(True):
                if(left>=len(l)-1):
                    print(res)
                    exit()
                res=max(sum(l[right:left+1]),res)
                #print(res,right,left)
                right+=2
                left+=2


Fixed submission:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [tuple(map(int, l.split())) for l in sys.stdin]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)

def main():
    n,k = LI()
    s = [int(c) for c in S()]
    a = []
    if s[0] == 0:
        a.append(0)
    t = s[0]
    c = 1
    for b in s[1:]:
        if t == b:
            c += 1
        else:
            a.append(c)
            c = 1
            t = b
    a.append(c)
    l = len(a)
    if l // 2 <= k:
        return sum(a)

    r = t = sum(a[:k*2+1])
    if l % 2 == 0:
        a.append(0)
        l += 1
    g = k * 2 + 1
    for i in range(k*2+1,l-1,2):
        t -= a[i-g] + a[i-g+1]
        t += a[i] + a[i+1]
        if r < t:
            r = t

    return r


print(main())