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):
import sys
input = sys.stdin.readline

def main():
    N, K = [int(x) for x in input().split()]
    S = input().strip()

    count, right, ans = 0, 1, 0
    if S[0] == '0':
        count += 1
    for i, v in enumerate(S):
        if i != 0 and S[i - 1] == '0' and S[i] == '1':
            count -= 1
        while count <= K and right < len(S):
            ans = max(ans, right - i)
            if S[right - 1] == '1' and S[right] == '0':
                count += 1
            right += 1
        if count <= K:
            ans = max(ans, right - i)

    print(ans)







if __name__ == '__main__':
    main()

Solution (Python):
from itertools import accumulate
 
 
def main():
    n, k = map(int, input().split())
    s = input()
 
    
    
    
    s = s.replace('01', '0,1').replace('10', '1,0')
    len_list = [len(string) for string in s.split(',')]
    if s[0] == '0':
        len_list = [0] + len_list
    if s[-1] == '0':
        len_list = len_list + [0]
 
    if 2 * k + 1 >= len(len_list):
        print(n)
        return
 
    sum_list = [0] + list(accumulate(len_list))
    max_len = 0
    for i in range(0, len(len_list) - (2 * k + 1) + 1, 2):
        handstand_len = sum_list[i + (2 * k + 1)] - sum_list[i]
        if max_len < handstand_len:
            max_len = handstand_len
    print(max_len)
 
 
if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = sys.stdin.readline

def main():
    N, K = [int(x) for x in input().split()]
    S = input().strip()
    ans = []
    prev = '0'
    if S[0] == '1':
        ans.append(0)
        prev = '1'

    count = 0
    for s in S:
        if s == prev:
            count += 1
        else:
            prev = s
            ans.append(count)
            count = 1
    ans.append(count)

    ans2 = 0
    tmp = 0
    count = 0
    r = 0
    l = 0
    if ans[0] == 0:
        l = 2
    for i, v in enumerate(ans):
        r = i
        if v == 0:
            continue
        tmp += v
        if count == K:
            break
        if not i & 1:
            count += 1

    ans2 = max(tmp, ans2)

    while r <= len(ans) - 2:
        if l == 0:
            tmp -= ans[0]
        else:
            tmp -= ans[l]
            tmp -= ans[l - 1]

        l += 2
        r += 2
        if not len(ans) - 1 < r:
            tmp += ans[r]
        tmp += ans[r - 1]

        ans2 = max(tmp, ans2)

    print(ans2)

if __name__ == '__main__':
    main()

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

ans=0
history=[0]*(2*K+1)
sum_recent=0
idx=0
c=1
for i in range(1,N+1):
    
    if i<N and S[i]==S[i-1]:
        c+=1
        continue
    sum_recent=sum_recent+c-history[idx]
    history[idx]=c
    idx=(idx+1)%(2*K+1)
    c=1
    if S[i-1]=='1':
        ans=max(ans,sum_recent)
    elif i==N:
        sum_recent-=history[idx]
        ans=max(ans,sum_recent)

print(ans)

Solution (Python):
from itertools import accumulate

def run_length(data, init):
    tmp = init
    cnt = 0
    length = []
    for c in data:
        if tmp != c:
            length.append(cnt)
            cnt = 0
        tmp = c
        cnt += 1
    length.append(cnt)
    return length

n,k = map(int, input().split())
s = input()
length = run_length(s, '')
if s[0] == '0':
    acc = [0] + list(accumulate(length))
else :
    acc = list(accumulate(length))
acc.append(acc[-1])

ans = acc[0]
if k * 2 + 1 > len(acc)-2:
    k = (len(acc)-2) // 2

for i in range(0, n, 2):
    
    if i + k * 2 + 1 >= len(acc):
        break
    if ans < acc[i + k * 2 + 1] - acc[i]:
        ans = acc[i + k * 2 + 1] - acc[i]
print(ans)

Solution (Python):


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

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

ans=0
if S[0]==0:
    l=[0]+l

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

tgt=sum(l[:2*K+1])
ans=max(ans,tgt)


for i in range(0,len(l)-(2*K+1))[::2]:
    tgt-=l[i]
    tgt-=l[i+1]
    tgt+=l[2*K+1+i]
    tgt+=l[2*K+2+i]
    ans=max(ans,tgt)
    

print(ans)

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

l = []
l.append(0)
ans = 0
before = ""
count = 0
if sl[0] == "0":
   l.append(0)
for idx,s in enumerate(sl):
   if s != before:
      before = s
      if idx > 0:
         l.append(idx)
         if len(l) == 2*k + 2:
            ans = max(l[-1] - l[0], ans)
            l.pop(0)
            l.pop(0)
l.append(n)
ans = max(l[-1] - l[0], ans)
print(ans)

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

A=[]
cnt=1

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

for i in range(n):
  if i==n-1:
    A.append(cnt)
    break
  if s[i]==s[i+1]:
    cnt+=1
  else:
    A.append(cnt)
    cnt=1
if s[-1]=="0":
  A.append(0)

nn=len(A)

if 2*k+1>=nn:
  print(sum(A))
  exit(0)

B=[0]*nn
B[0]=A[0]
for i in range(nn-1):
  B[i+1]=B[i]+A[i+1]
ans=sum(A[:2*k+1])

for i in range(1,nn-2*k-1,2):
  ans=max(ans,B[i+2*k+1]-B[i])
print(ans)

Solution (Python):
from itertools import groupby

N,K = map(int,input().split())
S = input()
cntS = []
sum_num = 0
for key, elems in groupby(S):
  num = len(list(elems))
  sum_num += num
  cntS.append([key,num,sum_num])
len_cntS = len(cntS)  

cnt0 = []
for i in range(len_cntS):
  key, num, sum_num = cntS[i]
  if key == '0':
    cnt0.append([i,num,sum_num])
  i += 1
  
if len(cnt0) <=K:
  print(N)
else:
  len_cnt0 = len(cnt0)
  ans = -float('inf')
  for i in range(-1,len_cnt0-K):
    if i == -1:
      tmp_ans = cnt0[i+K][2]
      if cnt0[i+K][0]+1 < len_cntS:
        tmp_ans += cntS[cnt0[i+K][0]+1][1]
    else:
      tmp_ans = cnt0[i+K][2]-cnt0[i][2]
      if cnt0[i+K][0]+1 < len_cntS:
        tmp_ans += cntS[cnt0[i+K][0]+1][1]
    ans = max(ans,tmp_ans)
    
  print(ans)

Solution (Python):
from itertools import groupby

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

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


nums = []
if S[0] == "0":
    nums.append(0)
left = 0
while left < N:
    right = left
    while right < N and S[right] == S[left]:
        right += 1
    nums.append(right - left)
    left = right
if S[-1] == "0":
    nums.append(0)

from itertools import accumulate

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

answer = 0
for left in range(0,len(nums),2):
    right = min(len(nums)-1, left + 2*K+1)
    answer = max(answer, nums[right] - nums[left])

print(answer)

Solution (Python):
from itertools import groupby, accumulate
from operator import sub
N, K = map(int, input().split())
S = input()
A = list(map(lambda p: (p[0], len(list(p[1]))), groupby(S, lambda s: s=="1")))
M = len([() for t,_ in A if not t])
B = [0] + list(accumulate(l for _,l in A))
R = B[K * 2 + int(A[0][0])::2] + ([] if A[-1][0] else [B[-1]])
L = ([] if A[0][0] else [B[0]]) + B[int(not A[0][0])::2]
print(max(map(sub, R, L)) if M > K else len(S))

Solution (Python):
N, K = map(int, input().split())
S = input()
zeros = []
ones = []
i = 0
if S[0] == '0':
    ones.append(0)
while i < N:
    count = 1
    j = i + 1
    while j < N:
        if S[i] == S[j]:
            count += 1
            j += 1
        else:
            break
    if S[i] == '0':
        zeros.append(count)
    else:
        ones.append(count)
    i = j
if S[-1] == '0':
    ones.append(0)

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

sum_zero = 0
sum_one = 0
for i in range(K):
    sum_zero += zeros[i]
    sum_one += ones[i]
sum_one += ones[K]

ans = sum_zero + sum_one
for i in range(len(zeros) - K):
    sum_zero = sum_zero - zeros[i] + zeros[i+K]
    sum_one = sum_one - ones[i] + ones[i+K+1]
    ans = max(ans, sum_zero + sum_one)
print(ans)

Solution (Python):
def main():
    from itertools import groupby
    from collections import deque
    N, K = (int(i) for i in input().split())
    S = input()
    ans = 0
    cur_0 = 0
    cur_sum = 0
    que = deque()
    for k, g in groupby(S):
        len_g = len(list(g))
        
        if k == "0":
            cur_0 += 1
            cur_sum += len_g
            que.append((0, len_g))
        else:
            cur_sum += len_g
            que.append((1, len_g))

        if K < cur_0:
            cur_0 -= 1
            key, val = que.popleft()
            while key != 0:
                cur_sum -= val
                key, val = que.popleft()
            cur_sum -= val
        ans = max(ans, cur_sum)
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):

import numpy as np
import sys
sys.setrecursionlimit(100000)

directions = np.array([[1, 0], [0, 1], [-1, 0], [0, -1]])


def acinput():
    return list(map(int, input().split(" ")))


sys.setrecursionlimit(100000)


directions = [[1, 0], [0, 1], [-1, 0], [0, -1]]
directions = list(map(np.array, directions))


def acinput():
    return list(map(int, input().split(" ")))


N, K = acinput()
s = list(map(int, list(input())))

nums = [1]
for i in range(1, N):
    if s[i] == s[i-1]:
        nums[-1] += 1
    else:
        nums.append(1)

if s[0] != 1:
    nums.insert(0, 0)
nums_cs = np.cumsum(nums)

res = nums_cs[min(len(nums_cs)-1,2*K)]





for i in range(1,len(nums_cs)-2*K+1):
    if i % 2 == 0:
        
        res = max(res, nums_cs[min(i+2*K,len(nums_cs)-1)]-nums_cs[i-1])


print(res)

Solution (Python):
N,K=[int(s) for s in input().split()]
S=input()
ls=[]
if S[0]=='0':
  ls.append([0,0])
else:
  start=0
i=1
while True:
  if i>=N:
    if S[N-1]=='0':
      ls.append([N,N])
    else:
      ls.append([start,N])
    break
  if S[i-1]=='0' and S[i]=='1':
    start=i
  if S[i-1]=='1' and S[i]=='0':
    goal=i
    ls.append([start,goal])
  i+=1
if K>=len(ls)-1:
  print(N)
else:
  ls2=[ls[i+K][1]-ls[i][0] for i in range(len(ls)-K)]
  print(max(ls2))

Solution (Python):
import bisect
import cmath
import heapq
import itertools
import math
import operator
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter, mul, add, xor

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7


N, K = list(map(int, sys.stdin.buffer.readline().split()))
S = sys.stdin.buffer.readline().decode().rstrip()

if '1' not in S or '0' not in S:
    print(N)
    exit()

ans = 0
l = 0
r = S.index('0')
k = 0
while r < N:
    while r < N and k < K:
        while r < N and S[r] == '0':
            r += 1
        while r < N and S[r] == '1':
            r += 1
        k += 1
    ans = max(ans, r - l)
    k -= 1
    while l < N and S[l] == '1':
        l += 1
    while l < N and S[l] == '0':
        l += 1
print(ans)

Solution (Python):
def solve():
    N, K = map(int, input().split())
    S = input()
    
    cur = S[0]
    start = [0]
    end = []
    for i, char in enumerate(S):
        if cur == '0' and char == '1':
            start.append(i)
        elif cur == '1' and char == '0':
            end.append(i)
        cur = char
    end.append(len(S))
    
    ans = 0
    for s, e in zip(start, end[K+int(S[0])-1:]):
        ans = max(ans, e - s)
    if ans == 0:
        ans = len(S)
    print(ans)


if __name__ == '__main__':
    solve()

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





LIST=[[[0] for j in range (2)]for i in range(N)]

t=S[0]
c=0
k=0
for i in range(N):
    if(t!=S[i]):
        LIST[k][0]=c
        LIST[k][1]=t
        t=S[i]
        k+=1
        c=0
    c+=1

LIST[k][0]=c
LIST[k][1]=t
LIST=LIST[0:k+1]
MAX=0

s=0
l=0
r=0

if(LIST[r][1]=="1"):
    s+=LIST[r][0]
    r+=1
x=0
while(K>0 and len(LIST)>r):
    s+=LIST[r][0]
    K-=1
    if(len(LIST)-1==r):
        x=1
        break
    r+=1
    s+=LIST[r][0]
    if(len(LIST)-1==r):
        x=1
        break
    r+=1

MAX=max(s,MAX)

f=0
if(x==0):
    while(r<len(LIST)):
        if(l< len(LIST)-1):
            s-=LIST[l][0]
            l+=1
            f=1
        if(l<len(LIST)-1 and LIST[l][1]=="0"):
            s-=LIST[l][0]
            l+=1

        if(f==0):
            break
        

        s+=LIST[r][0]
        MAX=max(s,MAX)
        r+=1
        if(len(LIST)==r):
            break
        
        s+=LIST[r][0]
        MAX=max(s,MAX)
        r+=1
        if(len(LIST)==r):
            break

print(MAX)

Solution (Python):
n,K = map(int, input().split(' '))
s = input()
ls0 = [0]
ls1 = [0]
if s[0] == "0":
    ls1[0] = -1
for i in range(n):
    if s[i] == "0":
        ls0[-1] += 1
        if ls1[-1] != 0:
            ls1.append(0)
    if s[i] == "1":
        ls1[-1] += 1
        if ls0[-1] != 0:
            ls0.append(0)
    
if s[0] == "0":
    ls1[0] = 0
if ls0[-1] == 0:
    del ls0[-1]

ans = []
ansmax = 0
if K>=len(ls0):
    ansmax = len(s)
else:
    ans.append(sum(ls0[0:K])+sum(ls1[0:K+1]))
    for i in range(1,len(ls0)-K+1):
        ans.append(ans[-1]+ls0[i+K-1]+ls1[i+K]-ls0[i-1]-ls1[i-1])
    else:
        ansmax = max(ans)
print(ansmax)

Solution (Python):
from itertools import accumulate

def length(S):
    cnt = 1
    A = []
    for i in range(len(S)-1):
        if S[i] == S[i+1]:
            cnt += 1
        else:
            A.append(cnt)
            cnt = 1
    else:
        A.append(cnt)

    return A

def main():
    N, K = map(int, input().split())
    S = input()
    lens = length(S)
    A = [0] + list(accumulate(lens))
    ans = 0
    if S[0] == '0':
        A = [0] + A
        for i in range(1,len(A)+1,2):
            right = i + 2 * K
            if right >= len(A):
                right = len(A) - 1
            tmp = A[right] - A[i-1]
            ans = max(ans, tmp)
    else:
        for i in range(1,len(A)+1,2):
            right = i + 2 * K
            if right >= len(A):
                right = len(A) - 1
            tmp = A[right] - A[i-1]
            ans = max(ans, tmp)

    print(ans)



if __name__ == "__main__":
    main()

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

run=[]
import itertools
for key,g in itertools.groupby(S):
  run.append(len(list(g)))
  
import numpy as np
run=np.cumsum(run)

ans=0
if S[0]=="0":
  left=0
  while left<len(run):
    if left==0:
      zone=min(2*K-1,len(run)-1)
      val=run[zone]
      left+=1
    else:
      zone=min(left+2*K,len(run)-1)
      val=run[zone]-run[left-1]
      left+=2
    ans=max(ans,val)
elif S[0]=="1":
  left=0
  while left<len(run):
    if left==0:
      zone=min(2*K,len(run)-1)
      val=run[zone]
      left+=2
    else:
      zone=min(left+2*K,len(run)-1)
      val=run[zone]-run[left-1]
      left+=2
    ans=max(ans,val)
print(ans)

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

zero = []
one = []
if S[0] == '0':
    one.append(0)
from itertools import groupby
for k, g in groupby(S):
    if k == '0':
        zero.append(len(list(g)))
    else:
        one.append(len(list(g)))
if S[-1] == '0':
    one.append(0)

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

zerob = [0]
oneb = [0]
for i in range(len(zero)):
    zerob.append(zerob[i] + zero[i])
for i in range(len(one)):
    oneb.append(oneb[i] + one[i])

ans = 0
for i in range(K, len(zerob)):
    z = zerob[i] - zerob[i-K]
    o = oneb[i+1] - oneb[i-K]
    ans = max(ans, z+o)

print(ans)

Solution (Python):
n,k=map(int,input().split())
s=list(str(input()))
r=[]
l=[]
c=0
d=1
for i in range(n):
	if s[i]=='1' and d==1:
		r.append(i)
		c=1
		d=0
	elif s[i]=='0' and c==1:
		l.append(i-1)
		c=0
		d=1
if c>0:
	l.append(i)
if len(r)<=k+1:
	r=[0]*(k+1-len(r))+r+[n-1]*(k+1-len(r))
	l=[0]*(k+1-len(l))+l+[n-1]*(k+1-len(l))
r=[0]+r+[n-1]
l=[0]+l+[n-1]
ans=0
for i in range(len(r)-k):
	ans=max(ans,l[i+k]-r[i]+1)
print(ans)

Solution (Python):
n,K = map(int, input().split(' '))
s = input()
ls0 = [0]
ls1 = [0]
if s[0] == "0":
    ls1[0] = -1
for i in range(n):
    if s[i] == "0":
        ls0[-1] += 1
        if ls1[-1] != 0:
            ls1.append(0)
    if s[i] == "1":
        ls1[-1] += 1
        if ls0[-1] != 0:
            ls0.append(0)
if s[0] == "0":
    ls1[0] = 0
if ls0[-1] == 0:
    del ls0[-1]
ans = []
ansmax = 0
if K>=len(ls0):
    ansmax = len(s)
else:
    ans.append(sum(ls0[0:K])+sum(ls1[0:K+1]))
    for i in range(1,len(ls0)-K+1):
        ans.append(ans[-1]+ls0[i+K-1]+ls1[i+K]-ls0[i-1]-ls1[i-1])
    else:
        ans.sort()
        ansmax = ans[-1]
print(ansmax)

Solution (Python):
n,k=map(int,input().split())
s=input()
num=[0]
c=1
for i in range(1,n):

    if s[i]==s[i-1]:
        c+=1
    else:
        num.append(c)
        c=1
num.append(c)
num.append(0)


ans=0

if s[0]=="0":
    if len(num)-2*k+1<=0:
        ans=sum(num)
    else:
        asum=sum(num[0:2*k+1])
        ans=asum
        
        for i in range(2,len(num)-2*k,2):
            
            asum=asum+num[i+2*k]+num[i+2*k-1]-num[i-1]-num[i-2]
            ans=max(ans,asum)
if s[0]=="1":
    if len(num)-2*k+2<=0:
        ans=sum(num)
    else:
        asum=sum(num[1:2*k+2])
        ans=asum
        for i in range(3,len(num)-2*k,2):
            
            asum=asum+num[i+2*k]+num[i+2*k-1]-num[i-1]-num[i-2]
            ans=max(ans,asum)
print(ans)

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

T = []
for i in range(N):
  if i == 0:
    bit = S[i]
    cnt = 1
  else:
    if S[i] != S[i-1]:
      T.append([int(bit), cnt])
      bit = S[i]
      cnt = 1
    else:
      cnt += 1
T.append([int(bit), cnt])
N = len(T)


L,R = 0,0
rest = 1 if T[0][0] == 0 else 0
suml = T[0][1]
answer = 0

while 1:
  
  if rest <= K:
    answer = max(answer, suml)
  if rest <= K:
    if R == N-1:
      break
    R += 1
    if T[R][0] == 0:
      rest += 1
    suml += T[R][1]
  else:
    L += 1
    if T[L-1][0] == 0:
      rest -= 1
    suml -= T[L-1][1]
  if rest <= K:
    answer = max(answer, suml)
print(answer)

Solution (Python):


import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N, K = MAP()
S = input()



A = []

if S[0] == '0':
    A.append(0)
cnt = 1
cur = S[0]
for i, s in enumerate(S[1:], 1):
    if s != cur:
        A.append(cnt)
        cnt = 1
        cur = s
    else:
        cnt += 1
A.append(cnt)


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


if S[-1] == '0':
    A.append(0)

M = len(A)
ans = sm = sum(A[:K*2+1])

for i in range(K*2+2, M, 2):
    
    sm += A[i-1] + A[i]
    
    if i-K*2-2 >= 0:
        sm -= A[i-K*2-2]
    if i-K*2-1 >= 0:
        sm -= A[i-K*2-1]
    ans = max(ans, sm)
print(ans)

Solution (Python):


import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N, K = MAP()
S = input()


A = []

if S[0] == '0':
    A.append(0)
cur = S[0]
cnt = 1
for i, s in enumerate(S[1:], 1):
    if s != cur:
        A.append(cnt)
        cnt = 1
        cur = s
    else:
        cnt += 1
A.append(cnt)


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


if S[-1] == '0':
    A.append(0)

M = len(A)
ans = sm = sum(A[:K*2+1])
for i in range(K*2+1, M):
    sm += A[i]
    
    if i % 2 == 1:
        if i-K*2 >= 0:
            sm -= A[i-K*2]
        if i-K*2-1 >= 0:
            sm -= A[i-K*2-1]
    ans = max(ans, sm)
print(ans)

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

memo = []
for i, s in enumerate(S):
    if i == 0:
        now = s
        cnt = 1
        continue
    if s == now:
        cnt += 1
    else:
        memo.append(cnt)
        now = s
        cnt = 1
memo.append(cnt)

if S[-1] == "0":
    memo += [0]
if S[0] == "0":
    memo = [0]+memo

if len(S) <= 2 * k + 1:
    ans = sum(memo)
else:
    k = 2*k+1
    stand = sum(memo[0:k])
    cnt = (len(memo) - k) // 2

    ans = stand
    for i in range(cnt):
        stand -= memo[i * 2]
        stand -= memo[i * 2 + 1]
        stand += memo[k + i * 2]
        stand += memo[k + i * 2 + 1]
        ans = max(ans, stand)

print(ans)

Solution (Python):
import sys
input =  sys.stdin.readline

n, k = map(int, input().split())
s = input()+'-1'

cnt = 0
cnt_list = []
for i in range(n):
    if s[i] == s[i+1]:
        cnt += 1
    else:
        cnt_list.append(cnt+1)
        cnt = 0




l = 0
r = 2*k
ma = 0
su = 0
if s[0] == '0':
    r = 2*k
else:
    r = 2*k+1
su = sum(cnt_list[l:r])
while l < r:
    if ma < su:
        ma = su
    if l == 0 and r == 2*k:
        l += 1
        su -= cnt_list[l-1]
    else:
        l += 2
        su -= sum(cnt_list[l-2:l])
    r += 2
    su += sum(cnt_list[r-2:r])
    if r >= len(cnt_list):
        r = len(cnt_list)
    
print(ma)

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

for i in range(N):
    if S[i] == 1:
        if tmp != 0:
            s.append((tmp, i-1))
        tmp = 0
    else:
        tmp += 1
        if i == N-1:
            s.append((tmp, i))
if len(s) <= K:
    print(N)
    exit(0)
ans = 0
for i in range(len(s)-K+1):
    tmp = 0
    if i+K == len(s):
        tmp = N-1
    else:
        tmp = s[i+K][1]-s[i+K][0]

    if i != 0:
        tmp -= s[i-1][1]
    else:
        tmp += 1
    ans = max(ans, tmp)

print(ans)

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

def renzoku(s):
    n=len(s)
    lis=[]
    c=1
    if len(s)==1:
        return [1]
    for i in range(n-1):
        if s[i+1]==s[i]:
            c+=1
        else:
            lis.append(c)
            c=1
        if i==n-2:
            lis.append(c)
    return lis


r=renzoku(s)
i=0
if s[0]=="0":
    r=[0]+r
if s[-1]=="0":
    r=r+[0]

cand = sum(r[:min(2*k+1,len(r))])
ans=cand
while 2*k+3 + 2*i<=len(r):
    cand += r[2*k+1+2*i]+r[2*k+2+2*i]-r[2*i]-r[2*i+1]
    if cand>ans:
        ans=cand
    i+=1
print(ans)

Solution (Python):
def p_d():
    n, k = map(int, input().split())
    *s, = map(int, input())
    ans = 0
    l, r = 0, 0
    count = 0
    while r < n:
        while r < n and count < k:
            if s[r] == 0:
                count += 1
                while r < n and s[r] == 0:
                    r += 1
            else:
                while r < n and s[r] == 1:
                    r += 1
        while r < n and s[r] == 1:
            r += 1
        ans = max(ans, r - l)
        while l < r and s[l] == 1:
            l += 1
        while l < r and s[l] == 0:
            l += 1
        count -= 1

    print(ans)


if __name__ == '__main__':
    p_d()

Solution (Python):






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





last = S[0]
if last == '0':
    A = [0]
else:
    A = []
renzoku = 1
for i in range(1, N):
    if S[i] == last:
        renzoku += 1
    else:
        A.append(renzoku)
        renzoku = 1
        last = S[i]
A.append(renzoku)
if S[-1] == '0':
    A.append(0)





A





l = 0
r = l + 2 * M + 1
if r > len(A):
    t = sum(A)
else:
    t = sum(A[l:r])
ret = t
l = 2
while True:
    r = l + 2 * M + 1
    if r > len(A):
        break
    t = t + A[r - 2] + A[r - 1] - A[l - 2] - A[l - 1]
    ret = max(t, ret)
    l += 2
print(ret)

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

    t = []
    cont = 0
    p = '1'
    for c in s:
        if c == p:
            cont += 1
        else:
            t.append(cont)  
            p = c
            cont = 1
    t.append(cont)  
    t.extend([0] * 2)
    if len(t) % 2 == 1:
        t.append(0)
    

    st = t[::2]  
    acc = [0]  

    p = 0
    j = 0
    while j < len(t) - 1:
        p += sum(t[j:j + 2])
        j += 2
        acc.append(p)

    ret = 0
    for j, (x, acc_) in enumerate(zip(st, acc)):
        ret = max(ret, (x + acc_) - acc[max(0, j - k)])
    return ret


if __name__ == '__main__':
    print(solve())

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007



n, k = LI()
s = S()
X = []
if s[0] == '0':
    X += [0]

cnt = 1
for i in range(n - 1):
    if s[i] != s[i + 1]:
        X += [cnt]
        cnt = 1
    else:
        cnt += 1
X += [cnt]
if s[n - 1] == '0':
    X += [0]

if len(X) <= 2 * k + 1:
    print(n)
else:
    ans = 0
    acc = list(accumulate([0] + X))
    for j in range(2 * k + 1, len(acc), 2):
        ans = max(acc[j] - acc[j - (2 * k + 1)], ans)
    print(ans)

Solution (Python):
N, K = map(int, input().split())
S = input()
S += "
S_list = []
if S[0] == "0":
    S_list.append(0)

count = 1
before_s = S[0]
for s in S[1:]:
    count += 1
    if before_s != s:
        S_list.append(count-1)
        count = 1
    before_s = s

if S[-2] == "0":
    S_list.append(0)



if len(S_list) < 2 * K:
    print(sum(S_list))
else:
    ans = 0
    ans_list = []
    for i in range(0, len(S_list) - (2 * K), 2):
        left = i
        right = i + 2 * K
        
        if i == 0:
            ans = sum(S_list[left:right+1])
            ans_list.append(ans)
        else:
            
            ans = ans - sum(S_list[left-2:left]) + sum(S_list[right-1:right+1])
            ans_list.append(ans)
    print(max(ans_list))

Solution (Python):
n, k = map(int, input().split())
s = list(map(int, list(str(input()))))
c = 0
ran = [0,0]
for i in range(n):
    if s[c] == s[i]:
        ran[-1] += 1
    else:
        ran.append(i+1)
        c = i
result = []
for i in range(1, len(ran)):
    if s[ran[i-1]] == 1:
        result.append(ran[min(i+2*k,len(ran)-1)]-ran[i-1])
    else:
        result.append(ran[min(i+2*k-1, len(ran)-1)]-ran[i-1])
print(max(result))

Solution (Python):

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

rec = []
for i in range(n):
  if i == 0:
    last = s[0]
    combo = 1
  elif s[i] == last:
    combo += 1
  else:
    last = s[i]
    rec.append(combo)
    combo = 1
rec.append(combo)
if s[0] == '0':
  rec = [0] + rec
if s[-1] == '0':
  rec = rec + [0]

rec = [0] * (2*n) + rec + [0] * (2*n)

for j in range(len(rec)):
  if j == 0:
    cum = [rec[0]]
  else:
    cum.append(cum[-1] + rec[j])


can = []
st = 1
while True:
  try:
    can.append(cum[st+2*k+1]-cum[st])
    st += 2
  except:
    break

print(max(can))

Solution (Python):
n,k = map(int, input().split())
s = input()
now = s[0]
start = []
end = []
if k != 0 or s[0] == "1":
  start.append(0)
for i,v in enumerate(s):
  if now == "0" and v == "1":
    start.append(i)
  elif now == "1" and v == "0":
    end.append(i)
  now = v
end.append(len(s))
ans = 0
for st,en in zip(start,end[k+int(s[0])-1:]):
  ans = max(ans,en-st)
if ans == 0:
  if not (all([i == "0" for i in s]) and k == 0):
    ans = len(s)    
print(ans)

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

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

if S[0] == "1" and S[-1] == "0":
	A = A + [0]
	N += 1
elif S[0] == "0" and S[-1] == "1":
	A = A[::-1] + [0]
	N += 1
elif S[0] == "0" and S[-1] == "0":
	   A = [0] + A + [0]
	   N += 2

ans = sum(A[:2*K+1])
tmp = ans
tK = 2*K
for i in range((N-1)//2-K):
	ind = 2*i
	tmp = tmp - sum(A[ind:ind+2]) + sum(A[ind+tK+1:ind+tK+3])
	ans = max(ans, tmp)

print(ans)

Solution (Python):
N, K = map(int, input().split())
s = input() + '
D = []
b = s[0]
cnt = 0
for c in s:
    if c == b:
        cnt += 1
    else:
        D.append((b, cnt))
        cnt = 1
        b = c

zero_start = False
if D[0][0] == '0':
    zero_start = True
    D.insert(0,(1, 1)) 

lend = len(D)
l = lend // 2 
ans = -1
if K >= l:
    ans = N
else:
    sm = sum([x[1] for x in D[:2 * K + 1]])
    ans = sm
    if zero_start :
        ans -= 1 

    for i in range(2 * K + 1, lend, 2): 
        sm += D[i][1]
        if i + 1 < lend:
            sm += D[i + 1][1]
        sm -= D[i - 2 * K][1]
        sm -= D[i - 2 * K - 1][1]

        ans = max(ans, sm)

print(ans)