Buggy python submission (verdict: Runtime Error):
n, k = map(int,input().split())
p = list(input())
a = []
ape = 1
for i in range(1,n):
     if p[i-1] != p[i]:
          a.append(ape)
          ape = 1
     else:
          ape += 1
a.append(ape)

if (p[0] == '1') or (len(a) == 1):
     kaisi = 0
else:
     kaisi = 1

ans = 0
anslist = []

for i in range(kaisi,min(2*k+1+kaisi,len(a))):
     ans += a[i]
anslist.append(ans)

for i in range(kaisi+2,len(a),2):
     if i+2*k <= len(a)-1:
          ans = ans-a[i-2]-a[i-1]+a[i+2*k]+a[i+2*k-1]
          anslist.append(ans)
     else:
          break
ans = 0
if (kaisi != 0) and (k != 0):
     for i in range(0,2*k):
          ans += a[i]
     anslist.append(ans)

print(max(anslist))
     

Fixed submission:
#参考　https://atcoder.jp/contests/abc124/submissions/4948803
from itertools import accumulate

n, k = map(int,input().split())
p = list(input())
a = []
pre = '1'
cnt = 0
for i in range(n):
     if pre != p[i]:
          a.append(cnt)
          cnt = 1
          pre = p[i]
     else:
          cnt += 1
a.append(cnt)
if pre == '0':
     a.append(0)

w = 2*k+1

if w >= len(a):
     print(n)
else:
     acc = list(accumulate(a))
     ans = 0
     acc = [0] + acc
     for i in range(0,len(a)-w+1,2):
          ans = max(ans,acc[i+w]-acc[i])
     print(ans)

Buggy python submission (verdict: Runtime Error):
n, k = map(int,input().split())
p = list(input())
a = []
ape = 1
for i in range(1,n):
     if p[i-1] != p[i]:
          a.append(ape)
          ape = 1
     else:
          ape += 1
a.append(ape)

if (p[0] == '1') or (len(a) == 1):
     kaisi = 0
else:
     kaisi = 1

ans = 0
anslist = []

for i in range(kaisi,min(2*k+1+kaisi,len(a))):
     ans += a[i]
anslist.append(ans)

for i in range(kaisi+2,len(a),2):
     if i+2*k <= len(a)-1:
          ans = ans-a[i-2]-a[i-1]+a[i+2*k]+a[i+2*k-1]
          anslist.append(ans)
     else:
          break
ans = 0
if kaisi != 0 and k != 0:
     for i in range(0,2*k):
          ans += a[i]
     anslist.append(ans)

print(max(anslist))

Fixed submission:
#参考　https://atcoder.jp/contests/abc124/submissions/4948803
from itertools import accumulate

n, k = map(int,input().split())
p = list(input())
a = []
pre = '1'
cnt = 0
for i in range(n):
     if pre != p[i]:
          a.append(cnt)
          cnt = 1
          pre = p[i]
     else:
          cnt += 1
a.append(cnt)
if pre == '0':
     a.append(0)

w = 2*k+1

if w >= len(a):
     print(n)
else:
     acc = list(accumulate(a))
     ans = 0
     acc = [0] + acc
     for i in range(0,len(a)-w+1,2):
          ans = max(ans,acc[i+w]-acc[i])
     print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int,input().split())
p = list(input())
a = []
ape = 1
for i in range(1,n):
     if p[i-1] != p[i]:
          a.append(ape)
          ape = 1
     else:
          ape += 1
a.append(ape)

def count(a,k,i,start):#start = p[0],a[i]からスタート
     ans = 0
     if start == '1':
          if i%2 == 0:
               for j in range(2*k+1):
                    if i+j>=len(a):
                         break
                    ans += a[i+j]
          else:
               for j in range(2*k):
                    if i+j>=len(a):
                         break
                    ans += a[i+j]
     else:
          if i%2 == 1:
               for j in range(2*k+1):
                    if i+j>=len(a):
                         break
                    ans += a[i+j]
          else:
               for j in range(2*k):
                    if i+j>=len(a):
                         break
                    ans += a[i+j]
     return ans

ans = count(a,k,0,p[0])
if (p[0] == '1') or (len(a) == 1):
     kaisi = 0
else:
     kaisi = 1
for i in range(kaisi,len(a),2):
     ans = max(count(a,k,i,p[0]), ans)
print(ans)

Fixed submission:
#参考　https://atcoder.jp/contests/abc124/submissions/4948803
from itertools import accumulate

n, k = map(int,input().split())
p = list(input())
a = []
pre = '1'
cnt = 0
for i in range(n):
     if pre != p[i]:
          a.append(cnt)
          cnt = 1
          pre = p[i]
     else:
          cnt += 1
a.append(cnt)
if pre == '0':
     a.append(0)

w = 2*k+1

if w >= len(a):
     print(n)
else:
     acc = list(accumulate(a))
     ans = 0
     acc = [0] + acc
     for i in range(0,len(a)-w+1,2):
          ans = max(ans,acc[i+w]-acc[i])
     print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int,input().split())
p = list(input())
a = []
ape = 1
for i in range(1,n):
     if p[i-1] != p[i]:
          a.append(ape)
          ape = 1
     else:
          ape += 1
a.append(ape)

def count(a,k,i,start):#start = p[0],a[i]からスタート
     ans = 0
     if start == '1':
          if i%2 == 0:
               for j in range(2*k+1):
                    if i+j>=len(a):
                         break
                    ans += a[i+j]
          else:
               for j in range(2*k):
                    if i+j>=len(a):
                         break
                    ans += a[i+j]
     else:
          if i%2 == 1:
               for j in range(2*k+1):
                    if i+j>=len(a):
                         break
                    ans += a[i+j]
          else:
               for j in range(2*k):
                    if i+j>=len(a):
                         break
                    ans += a[i+j]
     return ans

ans = count(a,k,0,p[0])
if (p[0] == '1') or (len(a) == 1):
     kaisi = 0
else:
     kaisi = 1
for i in range(kaisi,len(a),2):
     ans = max(count(a,k,i,p[0]), ans)
print(ans)

Fixed submission:
#参考　https://atcoder.jp/contests/abc124/submissions/4948803
from itertools import accumulate

n, k = map(int,input().split())
p = list(input())
a = []
pre = '1'
cnt = 0
for i in range(n):
     if pre != p[i]:
          a.append(cnt)
          cnt = 1
          pre = p[i]
     else:
          cnt += 1
a.append(cnt)
if pre == '0':
     a.append(0)

w = 2*k+1

if w >= len(a):
     print(n)
else:
     acc = list(accumulate(a))
     ans = 0
     acc = [0] + acc
     for i in range(0,len(a)-w+1,2):
          ans = max(ans,acc[i+w]-acc[i])
     print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int,input().split())
p = list(input())
a = []
ape = 1
for i in range(1,n):
     if p[i-1] != p[i]:
          a.append(ape)
          ape = 1
     else:
          ape += 1
a.append(ape)

def count(a,k,i,start):#start = p[0],a[i]からスタート
     ans = 0
     if start == '1':
          if i%2 == 0:
               for j in range(2*k+1):
                    if i+j>=len(a):
                         break
                    ans += a[i+j]
          else:
               for j in range(2*k):
                    if i+j>=len(a):
                         break
                    ans += a[i+j]
     else:
          if i%2 == 1:
               for j in range(2*k+1):
                    if i+j>=len(a):
                         break
                    ans += a[i+j]
          else:
               for j in range(2*k):
                    if i+j>=len(a):
                         break
                    ans += a[i+j]
     return ans

ans = 0
for i in range(len(a)):
     ans = max(count(a,k,i,p[0]), ans)
print(ans)

Fixed submission:
#参考　https://atcoder.jp/contests/abc124/submissions/4948803
from itertools import accumulate

n, k = map(int,input().split())
p = list(input())
a = []
pre = '1'
cnt = 0
for i in range(n):
     if pre != p[i]:
          a.append(cnt)
          cnt = 1
          pre = p[i]
     else:
          cnt += 1
a.append(cnt)
if pre == '0':
     a.append(0)

w = 2*k+1

if w >= len(a):
     print(n)
else:
     acc = list(accumulate(a))
     ans = 0
     acc = [0] + acc
     for i in range(0,len(a)-w+1,2):
          ans = max(ans,acc[i+w]-acc[i])
     print(ans)