Problem:
 Score : 400 points    Problem Statement N people are arranged in a row from left to right.  You are given a string S of length N consisting of 0 and 1 , and a positive integer K .  The i -th person from the left is standing on feet if the i -th character of S is 0 , and standing on hands if that character is 1 .  You will give the following direction at most K times (possibly zero):  Direction : Choose integers l and r satisfying 1 \leq l \leq r \leq N , and flip the l -th, (l+1) -th, ... , and r -th persons. That is, for each i = l, l+1, ..., r , the i -th person from the left now stands on hands if he/she was standing on feet, and stands on feet if he/she was standing on hands.  Find the maximum possible number of consecutive people standing on hands after at most K directions.      Constraints  N is an integer satisfying 1 \leq N \leq 10^5 .  K is an integer satisfying 1 \leq K \leq 10^5 .  The length of the string S is N .  Each character of the string S is 0 or 1 .         Input Input is given from Standard Input in the following format:  N  K  S       Output Print the maximum possible number of consecutive people standing on hands after at most K directions.        Sample Input 1 5 1
00010      Sample Output 1 4  We can have four consecutive people standing on hands, which is the maximum result, by giving the following direction:   Give the direction with l = 1, r = 3 , which flips the first, second and third persons from the left.        Sample Input 2 14 2
11101010110011      Sample Output 2 8       Sample Input 3 1 1
1      Sample Output 3 1  No directions are necessary.

Solution (Python):
n, k = map(int, input().split())
s = list(input())
s = list((map(int, s)))
tipS = []
cnt = 1
ans = 0
zeroInd = []
for i in range(n-1):
    if s[i] == s[i+1]:
        cnt += 1
    else:
        tipS.append([s[i], cnt])
        cnt = 1
tipS.append([s[-1], cnt])
total_sum = [0] * len(tipS)
total_sum[0] += tipS[0][1]
for i in range(len(tipS)):
    if tipS[i][0] == 0:
        zeroInd.append(i)
    if i != 0:
        total_sum[i] += tipS[i][1] + total_sum[i-1]
if len(zeroInd) != 0:
    start = zeroInd[0]
tmp_cnt = 0
tmp = 0
tmp2 = 0
before = 0

for i in range(len(tipS)):

    if tipS[i][0] == 0:
        tmp_cnt += 1


        if tmp_cnt > k:
            ans = max(tmp, ans)
            
            tmp -= (total_sum[start] - before)
            before = total_sum[start]
            
            tmp += tipS[i][1]
            tmp_cnt -= 1
            tmp2 += 1
            start = zeroInd[tmp2]

        else:
            tmp += tipS[i][1]


    else:
        tmp += tipS[i][1]
    
    

ans = max(ans, tmp)

print(ans)

Solution (Python):
class BufInput:
  def __init__(self):
    self.save = False
    self.buf = None
  def read(self, save=False):
    if self.save:
      ret = self.buf
    else:
      try:
        ret = input()
      except EOFError as e:
      	ret = None
    self.save = save
    self.buf = ret
    return ret
    
  
bi = BufInput()
while bi.read(True):
  n, k = map(int, bi.read().split())
  s = bi.read().rstrip()

  def findEx(s, f, pos, notfound):
    pos = s.find(f, pos)
    if pos == -1:
      pos = notfound
    return pos

  ans = 0
  left = 0
  right = 0
  while right < n:
      if k > 0:
          right = findEx(s, '01', right, n-1) + 1
          k = k - 1
          right = findEx(s, '10', right, n-1) + 1
          ans = max(ans, right - left)
      else:
          left = findEx(s, '01', left, n-1) + 1
          k = k + 1

  print(ans)

Solution (Python):
n, k = map(int, input().split())
s = input().rstrip()

def findEx(s, f, pos, notfound):
  pos = s.find(f, pos)
  if pos == -1:
    pos = notfound
  return pos
    
ans = 0
left = 0
right = 0
while right < n:
    if k > 0:
        right = findEx(s, '01', right, n-1) + 1
        k = k - 1
        right = findEx(s, '10', right, n-1) + 1
        ans = max(ans, right - left)
    else:
        left = findEx(s, '01', left, n-1) + 1
        k = k + 1

print(ans)

Solution (Python):
from itertools import accumulate
N,K = map(int,input().split())
S = input()

memo = [0]
run = 0
turn = '1'

for s in S:

    if s == turn:
        run += 1
    else:
        turn = s
        memo.append(run)
        run = 1

memo.append(run)
if turn == '0':
    memo.append(0)


if len(memo) > K*2+1:
    memo = list(accumulate(memo))
    res = max(x-y for x,y in zip(memo[K*2+1::2], memo[:-(K*2+1):2]))
else:
    res = N
print(res)

Solution (Python):
N, K = [int(x) for x in input().split()]
S = input()

L = [0]
R = []
for i in range(0, len(S) - 1):
    if S[i:i + 2] == "01":
        L.append(i + 1)
j = 0
while j < len(S) and S[j] == '1':
    j += 1
for i in range(j, len(S) - 1):
    if S[i:i + 2] == "10":
        R.append(i)
R.append(len(S) - 1)

ans = 0
z = min(len(L) - K, len(R) - K)
if z <= 0:
    ans = len(S)

for i in range(z + 1):
    Ri = max(0, i + K - 1)
    d = R[Ri] - L[i] + 1
    
    ans = max(ans, d)




print(ans)

Solution (Python):
n, k = map(int, input().split())
s = input().rstrip()
ans = 1
i = 0
left = 0
right = 1
while i < n:
    if k > 0:
        pos = s.find('01', i)
        if pos == -1:
            i = n
            right = n - 1
        else:
            pos = s.find('10', pos + 1)
            if pos == -1:
                i = n
                right = n - 1
            else:
                right = pos
                i = pos + 1
        k = k - 1
        ans = max(ans, right - left + 1)
    else:
        pos = s.find('01', left)
        left = pos + 1
        k = k + 1

print(ans)

Solution (Python):
n, k = map(int, input().split())
s = input().rstrip()
ans = 1
i = 0
left = 0
right = 1
while i < n:
    if k > 0:
        pos = s.find('01', i)
        if pos == -1:
            i = n
            right = n - 1
        else:
            pos = s.find('10', pos + 1)
            if pos == -1:
                i = n
                right = n - 1
            else:
                right = pos
                i = pos + 1
        k = k - 1
        ans = max(ans, right - left + 1)
    else:
        pos = s.find('01', left)
        left = pos + 1
        k = k + 1

print(ans)

Solution (Python):
n, k = map(int, input().split())
k += 1
s = "01" + input().rstrip()
n += 2
ans = 0
i = 0
left = 2
right = 1
while i < n:
    if k > 0:
        pos = s.find('01', i)
        if pos == -1:
            i = n
            right = n - 1
        else:
            pos = s.find('10', pos + 1)
            if pos == -1:
                i = n
                right = n - 1
            else:
                right = pos
                i = pos + 1
        k = k - 1
        ans = max(ans, right - left + 1)
    else:
        pos = s.find('01', left)
        left = pos + 1
        k = k + 1

print(ans)

Solution (Python):
N, K = map(int, input().split())
S = input()
starts = []
idx = 0
while idx < len(S):
    starts.append(idx)
    while idx < len(S) and S[starts[-1]] == S[idx]:
        idx += 1
starts.append(len(S))
ans = 0
for i, start in enumerate(starts[:-1]):
    if S[start] == '0':
        stop = starts[min(len(starts) - 1, i + 2 * K)]
    else:
        stop = starts[min(len(starts) - 1, i + 2 * K + 1)]
    ans = max(ans, stop - start)
print(ans)

Solution (Python):


N, K = map(int, input().split())
S = '0' + input() + '0'

Sl = [0] + [i for i in range(N + 1) if S[i:(i + 2)] == "01"] + [N]
Sr = [0] + [i for i in range(N + 1) if S[i:(i + 2)] == "10"] + [N]

if len(Sl) - K >= 1:
    print(max([Sr[i + K] - Sl[i] for i in range(len(Sl) - K)]))
else:
    print(N)

Solution (Python):
def solve(N, K, S):
    max_len = 0
    change_sum = 0
    start = 0
    end = 0

    while 1:
        if end == 0:
            if S[end] == '0':
                change_sum = change_sum + 1

        
        if change_sum <= K:
            if max_len < end - start + 1:
                max_len = end - start + 1
            end = end + 1
            
            if end >= len(S):
                break
            if S[end - 1] == '1' and S[end] == '0':
                change_sum = change_sum + 1
        else:
            start = start + 1
            if start >= len(S):
                break
            if S[start - 1] == '0' and S[start] == '1':
                change_sum = change_sum - 1

        

    return max_len

input_line = input().rstrip().split(' ')
N = int(input_line[0])
K = int(input_line[1])
S = input()

print(solve(N, K, S))

Solution (Python):
N, K = map(int, input().split())
S = input()



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



cumulative_sum = []
M = len(nums)
sum = 0
for i in range(M):
    sum += nums[i]
    cumulative_sum.append(sum)



ans = 0
start = 0
final = 2 * K
answers = []





if final <= M-1:
    while(final <= M-1):
        if start == 0:
            answers.append(cumulative_sum[final])
        else:
            ans = cumulative_sum[final] - cumulative_sum[start-1]
            answers.append(ans)

        
        start += 2
        final += 2
    print(max(answers))
else:
    print(cumulative_sum[-1])

Solution (Python):
N, K = map(int, input().split())
S = input()



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



cumulative_sum = []
M = len(nums)
sum = 0
for i in range(M):
    sum += nums[i]
    cumulative_sum.append(sum)



ans = 0
start = 0
final = 2 * K
answers = []


if M < 2 * K + 1:
    print(len(S))
    exit()
if final <= M-1:
    while(final <= M-1):
        if start == 0:
            answers.append(cumulative_sum[final])
        else:
            ans = cumulative_sum[final] - cumulative_sum[start-1]
            answers.append(ans)

        
        start += 2
        final += 2
    print(max(answers))
else:
    print(cumulative_sum[0])

Solution (Python):
N,K = map(int,input().split())

S=input()

left = 0
right = 0

max_len = N

count0 =0
henka = 0
first = int(S[0])
for i in range(1,N):
    if S[i-1]!=S[i]:
        henka +=1
    if S[i]=='0'and henka == (2*K+first):
        right = i-1
        max_len = i
        break

    



right_count = 3
left_count = 3

while(1):
    
    if right_count <= 2: break
    if max_len == N:break
    
    right_count = 0
    while(1):
        if right+1 >= N: break
        if S[right+1] !=  S[right]:
            right_count += 1
        if right_count >2 : break
        right += 1
    left_count = 0
    
     
    if S[left] =='0':
        left_count = 1
    else:
        left_count = 0
    while(1):
        if right_count == 0:break
        if S[left+1] !=  S[left]:
            left_count += 1
        left += 1
        if left_count >1 : break
        
    

    
    max_len = max(right-left+1,max_len)

print(max_len)

Solution (Python):
n, k = map(int, input().split())
s = input()

from itertools import groupby, accumulate

def gen(s):
  if s[:1] == '0':
  	yield 0
  for ke, g in groupby(s):
    yield len(list(g))
  if s[-1:] == '0':
    yield 0


mc = 0
acc = list(accumulate(gen(s)))
r = 2 * k + 1
if len(acc) < r:
  mc = n
else:
  for i in range(0, len(acc)-r+1, 2):
    if i == 0:
      mc = max(mc, acc[i+r-1])
    else:
      mc = max(mc, acc[i+r-1] - acc[i-1])
print(mc)

Solution (Python):
n, k = map(int, input().split())
s = input()

c = [0]
import itertools
for _, g in itertools.groupby(s):
  c.append(c[-1] + len(list(g)))
  
c = c[1:]
if s[0] == "1":
  c = [0] + c
else:
  c = [0,0] + c
if s[-1] == "0":
  c = c + [c[-1]]

ans = 0
for i in range(0,len(c), 2):
  end = min(i+2*k+1, len(c)-1)
  ans = max(ans, c[end]-c[i])

print(ans)

Solution (Python):
import re

input_str=input()
N=int(input_str.split()[0])
K=int(input_str.split()[1])
str_teams = str(input())
sub_str_teams = str_teams

def next_check_val(check_val):
  if check_val == '1':
    return '0'
  else:
    return '1'

cur_check_val = str_teams[0]
teams_group = []
teams_state = ''


for i in range(N):
  person = str_teams[i]
  if str_teams[i] == cur_check_val:
    teams_state = teams_state + str_teams[i]
  else:
    teams_group.append(teams_state)
    teams_state = str_teams[i]
    cur_check_val = next_check_val(cur_check_val)

teams_group.append(teams_state)




count_0 = 0
for i in range(len(teams_group)):
  if teams_group[i].startswith('0'):
    count_0 += 1
if count_0 <= K:
  print(N)
  exit()

sum_handstand = [] 
handstands = 0     




if ('0' in teams_group[0]):
  for k in range(K*2):
    handstands += len(teams_group[k])
else:
  for k in range(K*2+1):
    handstands += len(teams_group[k])

sum_handstand.append(handstands)


for i in range(1,len(teams_group)-(K*2)):
  
  if ('0' in teams_group[i]):
    handstands = sum_handstand[i-1]
  else:
    if i == 1:
      handstands = sum_handstand[i-1] - len(teams_group[i-1]) + (len(teams_group[i+2*K-1])+len(teams_group[i+2*K]))
    else:
      handstands = sum_handstand[i-1] - (len(teams_group[i-1])+len(teams_group[i-2])) + (len(teams_group[i+2*K-1])+len(teams_group[i+2*K]))
  sum_handstand.append(handstands)

handstands = 0

if ('0' in teams_group[-1]):
  for k in range(0,K*2):
    handstands += len(teams_group[len(teams_group)-2*K+k])
  sum_handstand.append(handstands)



print(max(sum_handstand))

Solution (Python):
N, K = map(int, input().split())
S = list(input())

nums = []
now = 1
count = 0
for i in range(len(S)):
    if S[i] == str(now):
        count += 1
    else:
        nums.append(count)
        now = 1 - now
        count = 1
if count != 0:
    nums.append(count)
if len(nums) % 2 == 0:
    nums.append(0)

add = 2 * K + 1

sum = [0] * (len(nums)+1)

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

print(ans)

Solution (Python):

_, k = map(int, input().split())
s = input()

a = []
cnt_one = 0
cnt_zero = 0

if s[0] == "0":
    a.append(0)

for i in s:
    if i == "1":
        cnt_one += + 1
        if cnt_zero != 0:
            a.append(cnt_zero)
            cnt_zero = 0
    else:
        cnt_zero += 1
        if cnt_one != 0:
            a.append(cnt_one)
            cnt_one = 0

a.append(max(cnt_zero, cnt_one))

if s[-1] == "0":
    a.append(0)

n = len(a)
right = 2 * k + 1
up = sum(a[:right])
max_up = up

for i in range((n - right) // 2):
    new_up = up + a[right + i * 2] + a[right + i * 2 + 1] - a[i * 2] - a[i * 2 + 1]
    max_up = max(max_up, new_up)
    up = new_up

print(max_up)

Solution (Python):
import math
 
N,K = (int(x) for x in input().split())
S = input()
 
list1 = []
now = 1
count = 0
 
for i in range(N):
    if now == int(S[i]):
        count += 1
    else:
        list1.append(count)
        now = 1-now
        count = 1
else:
    list1.append(count)

if len(list1) % 2 == 0:
    list1.append(0)
 
tmp=0
left=0
right=0
ans=0
for i in range(0,len(list1),2):
    nextleft = i
    while left<nextleft:
        tmp = tmp - list1[left]
        left = left + 1
 
    nextright = min(i+2*K+1,len(list1))
    while right<nextright:
        tmp = tmp + list1[right]
        right = right + 1

    ans=max(ans,tmp)

print(ans)

Solution (Python):
n, k = list(map(int, input().split()))
s = input()

l = [0]
ans = 0

for i in range(1, n):
  if s[i-1]=="1" and s[i]=="0":
    l.append(i)
  if s[i-1]=="0" and s[i]=="1":
    l.append(i)

for i, e in enumerate(l):
  if s[e]=="0":
    if i+2*k>=len(l):
      ans = max(ans, n-e)
    else:
      ans = max(ans, l[i+2*k]-e)
  else:
    if i+2*k+1>=len(l):
      ans = max(ans, n-e)
    else:
      ans = max(ans, l[i+2*k+1]-e)
print(ans)

Solution (Python):
def func():
    N,K = map(int,input().split())
    S = input()
    L = []
    L.append(0) if S[0] == '0' else 0
    num = '2'
    for i in range(N):
        if num != S[i]:
            L.append(i)
            num = S[i]
    L.append(N)
    L.append(N)
    ans = 0
    for i in range(0,len(L)-K*2-1,2):
        ans = max(ans,L[i+K*2+1]-L[i])
    return N if N < 3 or K >= len(L)//2 else ans
print(func())

Solution (Python):
def main():
    N, K = map(int, input().split())
    S = input()
    nums = []
    now = 1
    cnt = 0
    for s in S:
        if int(s) == now:
            cnt += 1
        else:
            nums.append(cnt)
            now ^= 1
            cnt = 1
    if cnt > 0:
        nums.append(cnt)
    if len(nums) % 2 == 0:
        nums.append(0)
    add = 2 * K + 1
    sum = [0 for _ in range(len(nums) + 1)]
    for i in range(len(nums)):
        sum[i + 1] = sum[i] + nums[i]
    ans = 0
    for cnt in range(0, len(nums), 2):
        left = cnt
        right = min(cnt + add, len(nums))
        tmp = sum[right] - sum[left]
        ans = max(tmp, ans)
    print(ans)


main()

Solution (Python):
def main():
    N, K = map(int, input().split())
    S = input()
    nums = []
    now = 1
    cnt = 0
    for s in S:
        if int(s) == now:
            cnt += 1
        else:
            nums.append(cnt)
            now ^= 1
            cnt = 1
    if cnt > 0:
        nums.append(cnt)
    if len(nums) % 2 == 0:
        nums.append(0)
    add = 2 * K + 1
    ans = 0
    left = 0
    right = 0
    tmp = 0
    for cnt in range(0, len(nums), 2):
        nleft = cnt
        nright = min(cnt + add, len(nums))
        while nleft > left:
            tmp -= nums[left]
            left += 1
        while nright > right:
            tmp += nums[right]
            right += 1
        ans = max(tmp, ans)
    print(ans)


main()

Solution (Python):
from itertools import accumulate

N, K = map(int, input().split())
s = input()
s = list(map(int, s))

v = []
now = 1  
cnt = 0  
for ss in s:
    if ss == now:
        cnt += 1
    else:
        v.append(cnt)
        now = 1 - now  
        cnt = 1  
if cnt > 0:
    v.append(cnt)






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



c_sum = [0] + list(accumulate(v))



lv = len(v)

ans = 0
for i in range(0, lv, 2):
    left = i
    right = min(2 * K + 1 + i, lv)
    ans = max(c_sum[right] - c_sum[left], ans)

print(ans)

Solution (Python):
N, K = map(int, input().split())
s = input()
s = list(map(int, s))

v = []
now = 1  
cnt = 0  
for ss in s:
    if ss == now:
        cnt += 1
    else:
        v.append(cnt)
        now = 1 - now  
        cnt = 1  
if cnt > 0:
    v.append(cnt)






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





lv = len(v)
left, right = 0, 0

ans = 0
cur_total = 0
for i in range(0, lv, 2):
    nextLeft = i
    nextRight = min(2 * K + 1 + i, lv)
    while left < nextLeft:
        cur_total -= v[left]
        left += 1
    while right < nextRight:
        cur_total += v[right]
        right += 1
    ans = max(cur_total, ans)

print(ans)

Solution (Python):
N, K = map(int, input().split())
s = input()
s = list(map(int, s))

v = []
now = 1  
cnt = 0  
for ss in s:
    if ss == now:
        cnt += 1
    else:
        v.append(cnt)
        now = 1 - now  
        cnt = 1  
if cnt > 0:
    v.append(cnt)






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



add = 2 * K + 1

c_sum = [0]
for vv in v:
    c_sum.append(c_sum[-1] + vv)



lv = len(v)
ans = 0
for i in range(0, lv, 2):
    ans = max(ans, c_sum[min(lv, 2 * K + 1 + i)] - c_sum[i])
print(ans)

Solution (Python):
N, K = map(int, input().split())
s = input()
S = [int(x) for x in list(str(s))]

l = []
tmp = 1
p = S[0]

for i in range(1, N):
    if p == S[i]:
        tmp += 1
    else:
        l.append(tmp)
        p = S[i]
        tmp = 1
l.append(tmp)

if len(l) == 1:
    print(N)
    exit()

i = 0
check = False

if S[0] == 0:
    r = 2 * K - 1
    check = True
else:
    r = 2 * K

ans = sum(l[i:r + 1])
tmp = ans

while r < len(l) - 2:
    tmp += l[r + 1] + l[r + 2]
    r += 2
    if check:
        tmp -= l[i]
        i += 1
        check = False
    else:
        tmp -= l[i] + l[i + 1]
        i += 2
    ans = max(tmp, ans)

if S[-1] == 0:
    tmp += l[r + 1]
    if check:
        tmp -= l[i]
    else:
        tmp -= l[i] + l[i + 1]

ans = max(ans, tmp)
print(ans)

Solution (Python):
N,K = list(map(int,input().split()))
A = input()
A = list(A)
A = list(map(int,A))
l = [0]
s = []

for i in range(N-1):
    if A[i] != A[i+1]:
        l.append(i+1)
        s.append(l[len(l)-1]-l[len(l)-2])
l.append(N)
s.append(l[len(l)-1]-l[len(l)-2])
a = 0
right = 0
left = 0
g = 0
su = 0
if A[0] == 0:
    for i in range(len(s)):
        su +=s[right]
        if right%2==0:
            g += 1
        right +=1
        if g<=K:
            a = max(a,su)
        else:
            while g>K:
              su -= s[left]
              if left%2==0:
                  g -=1
              left +=1
        a = max(a,su)
else:
    for i in range(len(s)):
        su +=s[right]
        if right%2==1:
            g += 1
        right +=1
        if g<=K:
            a = max(a,su)
        else:
            while g>K:
              su -= s[left]
              if left%2==1:
                  g -=1
              left +=1
        a = max(a,su)

print(a)

Solution (Python):
n, k = map(int, input().split())
s = str(input())

a = [0]

ls = s.replace('10', '1,0')
ls = ls.replace('01', '0,1')
ls = ls.split(',')
for i in ls:
    a.append(a[-1] + len(i))

ans = 0
res = 0
if s[0] == '1':
    for i in range(len(a)):
        if i % 2 == 0:
            ans = a[min(i + 2 * k + 1, len(a) - 1)] - a[i]
        else:
            ans = a[min(i + 2 * k, len(a) - 1)] - a[i]

        res = max(res, ans)

else:
    for i in range(len(a)):
        if i % 2 == 0:
            ans = a[min(i + 2 * k, len(a) - 1)] - a[i]
        else:
            ans = a[min(i + 2 * k + 1, len(a) - 1)] - a[i]

        res = max(res, ans)

print(res)

Solution (Python):
n,k = map(int, input().split())
s = input()
cnt = 0
init = (s[0]=='1')
h=[1]
h_cnt = 0

for i in range(1,n):
    if(s[i]!=s[i-1]):
        h_cnt+=1
        h.append(1)
    else:
        h[h_cnt]+=1

h_cnt+=1

zero_num = 0
ans = 0

if(init): 
    zero_num = h_cnt//2;
    if(zero_num<=k):
        ans = n
    else:
        sum_0 = sum(h[0:k*2+1])
        ans = sum_0
        h = h +[0,0,0]
        for j in range(0,zero_num-k): 
            sum_0 = sum_0 - h[j*2] - h[j*2+1] + h[(j+k)*2+1] + h[(j+k)*2+2]
            if(ans<=sum_0):
                ans = sum_0

else: 
    zero_num = h_cnt//2 +1;
    if(zero_num<=k):
        ans = n
    else:
        sum_0 = sum(h[0:k*2])
        ans = sum_0
        h = [0] + h +[0,0,0]
        for j in range(0,zero_num-k):
            sum_0 = sum_0 - h[j*2] - h[j*2+1] + h[(j+k)*2+1] + h[(j+k)*2+2]
            if(ans<=sum_0):
                ans = sum_0

print(ans)

Solution (Python):
n, k = map(int, input().split(" "))
s = input()
l = []
if s[0] == "0":
    l.append((0, 0))
i = 0
while i < n:
    if s[i] == "1":
        b = i
        while i < n and s[i] == "1":
            i += 1
        l.append((b, i))
    i += 1
if s[n-1] == "0":
    l.append((n, n))
if len(l)-1 <= k:
    print(n)
else:
    maximum = 0
    for i in range(len(l)-k):
        maximum = max(maximum, l[i+k][1]-l[i][0])
    print(maximum)

Solution (Python):
n, k = map(int, input().split(" "))
s = input()
l = []
if s[0] == "0":
    l.append((0, 0))
i = 0
while i < n:
    if s[i] == "1":
        b = i
        while i < n and s[i] == "1":
            i += 1
        l.append((b, i))
    i += 1
if s[n-1] == "0":
    l.append((n, n))
if len(l)-1 <= k:
    print(n)
else:
    maximum = 0
    for i in range(len(l)-k):
        maximum = max(maximum, l[i+k][1]-l[i][0])
    print(maximum)

Solution (Python):
N, K = map(int, input().split())
S = input()
l = []
if S[0] == '0':
  l.append(0)
p = S[0]
cnt = 0
for c in S:
  if c == p:
    cnt += 1
  else:
    l.append(cnt)
    cnt = 1
  p = c
l.append(cnt)
if S[-1] == '0':
  l.append(0)


top = 0
ll = 2*K+1
m = min(ll, len(l))
for i in range(m):
  top += l[i]

t = top
for i in range(2, len(l)-(ll)+1, 2):
  t = t - l[i-2] - l[i-1] + l[i+ll-2] + l[i+ll-1]
  
  if t > top:
    top = t
print(top)

Solution (Python):
n, k = map(int, input().split())
s = input()
n = len(s)

if n == 1:
    print(1)
    exit(0)

l = []
if s[0] == "0":
    l.append(0)

temp = 1
for i in range(1, n):
    if s[i] == s[i - 1]:
        temp += 1
        if i == n - 1:
            l.append(temp)
    else:
        l.append(temp)
        temp = 1
        if i == n - 1:
            l.append(temp)

if s[-1] == "0":
    l.append(0)

ans = 0

b = []
b.append(l[0])
for i in range(1, len(l)):
    b.append(b[-1] + l[i])


if 2 * k + 1 > len(l) - 1:
    print(b[len(l) - 1])
    exit(0)
else:
    ans = b[2 * k]


for i in range(1, len(l)):
    if i * 2 + 2 * k > len(l) - 1:
        break
    ans = max(ans, b[min(i * 2 + 2 * k, len(l) - 1)] - b[i * 2 - 1])
print(ans)

Solution (Python):
from itertools import groupby, accumulate


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


A = list(accumulate(len(list(seq)) for _, seq in groupby(S))) + [N]
m = (
    1 if S[0] == '0' else
    0
)

ans = max(
    A[min(2 * K - 1, len(A) - 1)],
    A[-1] - A[max(-(2 * K), -len(A))],
    max(
        (
            a - b
            for a, b in zip(A[(2 * K + m)::2], ([0] + A)[m::2])
        ),
        default=0
    )
)


print(ans)

Solution (Python):
from itertools import groupby, accumulate


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


A = list(accumulate(len(list(seq)) for _, seq in groupby(S))) + [N]
m = (
    1 if S[0] == '0' else
    0
)

ans = (
    max(
        A[min(2 * K - 1, len(A) - 1)],
        A[-1] - A[max(-(2 * K), -len(A))],
        max(
            (
                a - b
                for a, b in zip(A[(2 * K + m)::2], ([0] + A)[m::2])
            ),
            default=0
        )
    ) if m < len(A) else
    A[-1]
)


print(ans)

Solution (Python):
n, k = map(int,input().split())
s = list(input())

Nums = []
now = 1
cnt = 0

for i in range(0,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)
sum = [0]*(len(Nums)+1)

for i in range(len(Nums)):
    sum[i+1] = sum[i] + Nums[i]

add = 2 * k + 1
ans = 0

for i in range(0,len(sum),2):
    tmp = 0
    left = i
    right = min(i+add,len(Nums))
    tmp = sum[right] - sum[left]
    ans = max(ans,tmp)

print(ans)

Solution (Python):
n,k = map(int,input().split())
S = input()+'!'
A = [0]
tmp = 1
for i in range(1,n+1):
    if S[i] == S[i-1]:
        tmp += 1
    else:
        A.append(tmp)
        tmp = 1
for i in range(1,len(A)):
    A[i] += A[i-1]

res = 0
if S[0] == '0':
    for i in range(len(A)):
        if i%2 == 0:
            ans = A[min(i+2*k,len(A)-1)]-A[i]
        else:
            ans = A[min(i+2*k+1,len(A)-1)]-A[i]
        if res <= ans:
            res = ans
else:
    for i in range(len(A)):
        if i%2 == 0:
            ans = A[min(len(A)-1,i+2*k+1)]-A[i]
        else:
            ans = A[min(i+2*k,len(A)-1)]-A[i]
        if res <= ans:
            res = ans

print(res)

Solution (Python):
N, K = map(int, input().split(" "))
S = input()
ans = 0
count_0 = []
count_1 = []

tmp = 0
now_is_0 = True
if S[0] == "1":
    now_is_0 = False

for i, c in enumerate(S):
    if c == "0":
        if now_is_0:
            tmp += 1
        else:
            count_1.append(tmp)
            now_is_0 = True
            tmp = 1
    else:
        if now_is_0:
            count_0.append(tmp)
            now_is_0 = False
            tmp = 1
        else:
            tmp += 1
if now_is_0:
    count_0.append(tmp)
else:
    count_1.append(tmp)

if len(count_0) <= K:
    print(N)
    exit()

if S[0] == "0":
    count_1 = [0, ] + count_1
if S[N-1] == "0":
    count_1 = count_1 + [0, ]

tmp_ans = sum(count_0[:K] + count_1[:K+1])
max_ans = tmp_ans

i = 0
length = len(count_1)
while K+i+1 < length:
    tmp_ans -= count_1[i]
    tmp_ans -= count_0[i]

    tmp_ans += count_0[K+i]
    tmp_ans += count_1[K+1+i]
    max_ans = max(tmp_ans, max_ans)
    i += 1
print(max_ans)

Solution (Python):
N,K = map(int,input().split())
S = input()

if S[N-1] == '1':
    S += '0'
else:
    S += '1'

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

n = len(memo)
for i in range(2*K+1):
    memo.append(N+1)

m = 0
l = []
if S[0] == '1':
    for i in range(n):
        if i%2 == 0:
            l.append(memo[i+2*K+1]-memo[i])
        else:
            l.append(memo[i+2*K]-memo[i])



        
        
else:
    for i in range(n):
        if i%2 == 1:
            l.append(memo[i+2*K+1]-memo[i])
        else:
            l.append(memo[i+2*K]-memo[i])


        
        

print(max(l))

Solution (Python):
import numpy as np

N,K = map(int,input().split())
S = list(input())

State = [0] * N
counter0 = 0
counter1 = 0
flag = -1
jj = 0
for i in range(N):
    if S[i] == '0':
        if flag == 1:
            State[jj] = counter1
            jj +=1
            counter1 = 0
        counter0 += 1
        flag = 0
    if S[i] == '1':
        if flag == 0:
            State[jj] = counter0
            jj += 1
            counter0 = 0
        counter1 += 1
        flag = 1
    if i == N-1:
        State[jj] = max(counter0,counter1)
        
N2 = np.count_nonzero(State)
State = int(S[0] == '0') * [0] + State[:N2] + int(S[N-1] == '0') * [0]
N2 = len(State)
ans = 0
total = 0
K2 = 2*K+1

if K2 >= N2:
    K2 = N2

for i in range(K2):
    total += State[i]
        
ans = total
for i in range(0,N2-K2,2):
    total2 = total - State[i] - State[i+1] + State[i+K2] + State[i+1+K2]
    ans = max(ans,total2)
    total = total2
    
print(ans)

Solution (Python):
N, K = map(int, input().split())
S = input()
new_S = []
count_S = []

cur_c = S[0]
cur_count = 0

new_S.append(cur_c)
for c in S:
    if c == cur_c:
        cur_count += 1
    else:
        new_S.append(c)
        count_S.append(cur_count)
        cur_count = 1
        cur_c = c
count_S.append(cur_count)        

new_count = []
st_ix = 0
if S[0] == '0':
    cur_sum = sum(count_S[:2*K])
    st_ix = 1
    new_count.append(cur_sum)
else:
    cur_sum = sum(count_S[:2*K+1])
    new_count.append(cur_sum)

en_ix = st_ix + 2*K + 1
cur_sum = sum(count_S[st_ix:en_ix])
new_count.append(cur_sum)

while en_ix < len(count_S):
    
    st_ix = st_ix + 2
    en_ix = en_ix + 2
    if en_ix >= len(count_S):
        new_count.append(sum(count_S[st_ix:]))
    else:
        cur_sum -= (count_S[st_ix-2] + count_S[st_ix-1])
        cur_sum += (count_S[en_ix-2]
                        + count_S[en_ix-1])
    new_count.append(cur_sum)

print(max(new_count))

Solution (Python):
from itertools import accumulate

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

nums = []
tmp = "1" 
count = 0
for s in S:
    if tmp == s:
        count += 1
    else:
        nums.append(count)
        count = 1
        tmp = s
nums.append(count)
if tmp == "0":
    nums.append(0) 




acc = [0]+list(accumulate(nums))

n = len(nums)
w = 2*K + 1

if len(nums) <= w:
    print(N)
else:
    ans = 0
    for i in range(0,n-w+1,2): 
        ans = max(ans,acc[i+w]-acc[i])

    print(ans)