Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
S = input()

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

if S.count("1") == N or S.count("0") == N:
  print(N)
else:
  ans = 0
  for i in range(len(l)):
    cnt = 0
    if S[0] == "0" and i == 0:
      for j in range(K * 2):
        cnt += l[j]
        if j == len(l) - 1:
          break
    elif S[0] == "0" and i % 2 == 1 or S[0] == "1" and i % 2 == 0:
      for j in range(i, i + K * 2 + 1):
        cnt += l[j]
        if j == len(l) - 1:
          break  
    ans = max(ans, cnt)
  print(ans)

Fixed submission:
N, K = map(int, input().split())
S = input()

nums = []
now = 1
cnt = 0
for i in range(N):
  if S[i] == str(now):
    cnt += 1
  else:
    nums.append(cnt)
    now = 1 - now
    cnt = 1
if cnt != 0:
  nums.append(cnt)

if len(nums) % 2 == 0:
  nums.append(0)

add = 2 * K + 1

sums = [0 for _ in range(len(nums) + 1)]
for i in range(len(nums)):
  sums[i + 1] = sums[i] + nums[i]

ans = 0
for i in range(len(nums)):
  if i % 2 == 1:
    continue
  
  left = i
  right = min(i + add, len(nums))
  tmp = sums[right] - sums[left]

  ans = max(tmp, ans)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
S = input()

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

if S.count("1") == N or S.count("0") == N:
  print(N)
else:
  ans = 0
  for i in range(len(l)):
    cnt = 0
    if S[0] == "0":
      if i % 2 == 0:
        if i != 0:
          continue
        for j in range(K * 2):
          cnt += l[j]
          if j == len(l) - 1:
            break
      else:
        for j in range(i, i + K * 2 + 1):
          cnt += l[j]
          if j == len(l) - 1:
            break  
    else:
      if i % 2 == 0:
        for j in range(i, i + K * 2 + 1):
          cnt += l[j]
          if j == len(l) - 1:
            break
      else:
        continue

    ans = max(ans, cnt)
  print(ans)

Fixed submission:
N, K = map(int, input().split())
S = input()

nums = []
now = 1
cnt = 0
for i in range(N):
  if S[i] == str(now):
    cnt += 1
  else:
    nums.append(cnt)
    now = 1 - now
    cnt = 1
if cnt != 0:
  nums.append(cnt)

if len(nums) % 2 == 0:
  nums.append(0)

add = 2 * K + 1

sums = [0 for _ in range(len(nums) + 1)]
for i in range(len(nums)):
  sums[i + 1] = sums[i] + nums[i]

ans = 0
for i in range(len(nums)):
  if i % 2 == 1:
    continue
  
  left = i
  right = min(i + add, len(nums))
  tmp = sums[right] - sums[left]

  ans = max(tmp, ans)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, K = map(int, input().split())
S = input()

if S.count("1") == N or S.count("0") == N:
  print(N)
else:
  ans = 0
  for i in range(N):
    cnt = 0
    change = 0
    if i != 0 and S[i] == "0":
      continue
    for j in range(i, N):
      if j != 0 and S[j] == "0" and S[j - 1] == "1":
        change += 1
      if S[i] == "0" and change < K or S[i] == "1" and change <= K:
        cnt += 1
      else:
        break
    ans = max(ans, cnt)
  print(ans)

Fixed submission:
N, K = map(int, input().split())
S = input()

nums = []
now = 1
cnt = 0
for i in range(N):
  if S[i] == str(now):
    cnt += 1
  else:
    nums.append(cnt)
    now = 1 - now
    cnt = 1
if cnt != 0:
  nums.append(cnt)

if len(nums) % 2 == 0:
  nums.append(0)

add = 2 * K + 1

sums = [0 for _ in range(len(nums) + 1)]
for i in range(len(nums)):
  sums[i + 1] = sums[i] + nums[i]

ans = 0
for i in range(len(nums)):
  if i % 2 == 1:
    continue
  
  left = i
  right = min(i + add, len(nums))
  tmp = sums[right] - sums[left]

  ans = max(tmp, ans)

print(ans)