Buggy python submission (verdict: Runtime Error):

N, K = map(int, input().split())
S = input()
S = S + "8"

a = []
f = S[0]
l = 0
for i in range(1, N+1):
    #print(S[i])
    if f != S[i]:
        #a.append([l, i-1, int(f)])
        #a.append([i-l, int(f)])
        a.append(i-l)
        l = i
        f = S[i]
        
#print(a)

if S[0] == '0':
    a = [0] + a

#print(a[0:0+2*K+1])
SUM = sum(a[0:0+2*K+1])

MAX = max(0, SUM)
for i in range(1, len(a)//2+len(a)%2):
    if i % 2 == 0:
        #print(a[i:i+2*K+1])
        #print(a[i-2], a[i-1])
        #print(a[i+2*K-1], a[i+2*K])
        #print(sum(a[i:i+2*K+1]))
        #MAX = max(MAX, sum(a[i:i+2*K+1]))
        SUM = SUM-(a[i-2]+a[i-1])+(a[i+2*K-1]+a[i+2*K])
        MAX = max(MAX, SUM)

print(MAX)

Fixed submission:

N, K = map(int, input().split())
S = input()
S = S + "8"

a = []
f = S[0]
l = 0
for i in range(1, N+1):
    if f != S[i]:
        a.append(i-l)
        l = i
        f = S[i]
        

if S[0] == '0':
    a = [0] + a

SUM = sum(a[0:0+2*K+1])

MAX = max(0, SUM)
for i in range(2, len(a)):
    if i % 2 == 0:
        try:
            SUM = SUM - (a[i-2]+a[i-1]) + (a[i+2*K-1]+a[i+2*K])
        except:
            try:
                SUM = SUM - (a[i-2]+a[i-1]) + (a[i+2*K-1])
            except:
                break
        
        MAX = max(MAX, SUM)


print(MAX)

Buggy python submission (verdict: Time Limit Exceeded):

N, K = map(int, input().split())
S = input()
S = S + "8"

a = []
f = S[0]
l = 0
for i in range(1, N+1):
    #print(S[i])
    if f != S[i]:
        #a.append([l, i-1, int(f)])
        #a.append([i-l, int(f)])
        a.append(i-l)
        l = i
        f = S[i]
        
#print(a)

if S[0] == '0':
    a = [0] + a

MAX = 0
for i in range(len(a)):
    if i % 2 == 0:
        #print(a[i:i+2*K+1])
        #print(sum(a[i:i+2*K+1]))
        MAX = max(MAX, sum(a[i:i+2*K+1]))
        if len(a) < i+1+2*K+1:
            break

print(MAX)

Fixed submission:

N, K = map(int, input().split())
S = input()
S = S + "8"

a = []
f = S[0]
l = 0
for i in range(1, N+1):
    if f != S[i]:
        a.append(i-l)
        l = i
        f = S[i]
        

if S[0] == '0':
    a = [0] + a

SUM = sum(a[0:0+2*K+1])

MAX = max(0, SUM)
for i in range(2, len(a)):
    if i % 2 == 0:
        try:
            SUM = SUM - (a[i-2]+a[i-1]) + (a[i+2*K-1]+a[i+2*K])
        except:
            try:
                SUM = SUM - (a[i-2]+a[i-1]) + (a[i+2*K-1])
            except:
                break
        
        MAX = max(MAX, SUM)


print(MAX)

Buggy python submission (verdict: Time Limit Exceeded):

N, K = map(int, input().split())
S = input()
S = S + "8"

a = []
f = S[0]
l = 0
for i in range(1, N+1):
    #print(S[i])
    if f != S[i]:
        #a.append([l, i-1, int(f)])
        #a.append([i-l, int(f)])
        a.append(i-l)
        l = i
        f = S[i]
        
#print(a)

if S[0] == '0':
    a = [0] + a

MAX = 0
for i in range(len(a)):
    if i % 2 == 0:
        #print(a[i:i+2*K+1])
        #print(sum(a[i:i+2*K+1]))
        MAX = max(MAX, sum(a[i:i+2*K+1]))

print(MAX)

Fixed submission:

N, K = map(int, input().split())
S = input()
S = S + "8"

a = []
f = S[0]
l = 0
for i in range(1, N+1):
    if f != S[i]:
        a.append(i-l)
        l = i
        f = S[i]
        

if S[0] == '0':
    a = [0] + a

SUM = sum(a[0:0+2*K+1])

MAX = max(0, SUM)
for i in range(2, len(a)):
    if i % 2 == 0:
        try:
            SUM = SUM - (a[i-2]+a[i-1]) + (a[i+2*K-1]+a[i+2*K])
        except:
            try:
                SUM = SUM - (a[i-2]+a[i-1]) + (a[i+2*K-1])
            except:
                break
        
        MAX = max(MAX, SUM)


print(MAX)