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):
from operator import itemgetter

def main():
    N,K = map(int,input().split())
    S = [int(s) for s in input()] + [1]
    used = [0]*(N + 3)
    right = -1
    ans = 0
    for left in range(N):
        if S[left] + S[left - 1] == 0:
            continue
        while right < N and used[right] - used[left-1] <= K:
            right += 1
            if S[right]:
                used[right] = used[right - 1]
            else:
                if S[right-1]:
                    used[right] = used[right - 1] + 1
                else:
                    used[right] = used[right - 1]
        ans = max(ans,right - left)
    print(ans)
    return 

if __name__ == "__main__":
    main()

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

S += '2'
l = []
cnt = 1
for i in range(N):
  if S[i] == S[i+1]:
  	cnt += 1
  else:
    l.append(cnt)
    cnt = 1

if S[0] == '0':
  cur = sum(l[:2*K])
  ans = cur
  for j in range(1, len(l), 2):
    cur += -sum(l[max(0, j-2): j]) + sum(l[j+2*K-1:j+2*K+1])
    ans = max(ans, cur)
else:
  cur = sum(l[:2*K+1])
  ans = cur
  for j in range(2, len(l), 2):
    cur += -sum(l[max(0, j-2): j]) + sum(l[j+2*K-1:j+2*K+1])
    ans = max(ans, cur)
print(ans)

Solution (Python):
def solve(s, k):
    c = []
    if s[0] == '0':
        c.append(0)

    target = ''

    for si in s:
        if si != target:
            target = si
            c.append(1)
        else:
            c[-1] += 1

    if s[-1] == '0':
        c.append(0)

    c_len = len(c)

    if 2*k+1 >= c_len:
        return sum(c)
    else:
        current = sum(c[0:2*k+1])
        ans = current

        for i in range(2, c_len-2*k, 2):
            current -= (c[i-2] + c[i-1])
            current += (c[i+2*k-1]+c[i+2*k])
            ans = current if current > ans else ans
    return ans


if __name__ == '__main__':
    _, k = map(int, input().split())
    s = input()
    print(solve(s, k))

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

num_cnt=[]
if s[0]=='1':
  num_cnt.append(0)

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

leng=len(num_cnt)
if leng/2>k:
  ans=sum(num_cnt[:2*k])
  tot=ans
  
  if leng%2==0:
    limit=leng//2-k
  else:
    num_cnt.append(0)
    limit=leng//2-k+1
  
  
  
  tmp2=num_cnt[2*k]+num_cnt[2*k+1]-num_cnt[0]
  tot+=tmp2
  if tot>ans:
    ans=tot
  
  for i in range(1,limit):
    
    tmp=num_cnt[2*(i+k)]+num_cnt[2*(i+k)+1]-num_cnt[2*i]-num_cnt[2*i-1]
    
    tot+=tmp
    if tot>ans:
      ans=tot
else:
  ans=n
print(ans)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
n,k = map(int,readline().split())
s = readline().rstrip().decode('utf-8')

if s[0] == 0: 
    r = 0
else: 
    r = 1

if r==0:
    lst1 = [0] 
else:
    lst1 = []
res = 0
cache = str(r)
for i in s:
    if i != cache:
        lst1.append(res)
        cache = "1" if cache=="0" else "0"
        res = 1
    else:
        res += 1

lst1.append(res)
lst1.append(0)


if len(lst1) <= 2*k:
    print(n)
    exit()


for i in range(len(lst1)-1):
    lst1[i+1] += lst1[i]

def cumsumdif(a,b,lst): 
    if a == 0:
        return lst[b]
    else:
        return lst[b] - lst[a-1]
i = 0
ans = 0
while True:
    if i+(k*2) >= len(lst1):
        break
    ans = max(ans,cumsumdif(i,i+2*k,lst1))
    i += 2

print(ans)

Solution (Python):
import sys


def main():
    N,K = map(int,input().split())
    S = str(input())
    if S[0] == "1":
        z,o = [0],[]
    else:
        z,o = [0],[0]
        
    for i in range(N-1):
        if S[i+1] != S[i]:
            if S[i] == "1":
                o.append(i+1)
            else:
                z.append(i+1)
                
    o.append(N)            
    if S[-1] == "0":
        z.append(N)

    l = len(o)
    if l == 1:
        print(N)
    else:
        if K+1 > l:
            print(N)
        else:
            ans = 0
            for i in range(l-K):
                u = o[i+K]-z[i]
                ans = max(ans,u)
            print(ans)
        
if __name__ == "__main__":
    main()

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

Nums = []

tmp = "1"
cnt = 0
for s in S:
  if s == tmp:
    cnt += 1
  else:
    Nums.append(cnt)
    tmp = s
    cnt = 1
else:
  Nums.append(cnt)
  if tmp == "0":
    Nums.append(0)

T = list(accumulate(Nums))
T.insert(0,0)

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

print(ans)

Solution (Python):
n,k=map(int,input().split())
s=input()
l=[]
cnt=1
for i in range(n-1):
  if s[i]!=s[i+1]:
    l.append(i+1)
l=[0]+l+[n]
ans=0
for i in range(len(l)-1):
  if s[l[i]]=='0':
    ans=max(ans,l[min(i+2*k,len(l)-1)]-l[i])
  else:
    ans=max(ans,l[min(i+2*k+1,len(l)-1)]-l[i])
print(ans)

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


cnt_list = []
flag = True if S[0] == '1' else False
cnt = 1
flag_flag = True
for i in range(1, N):
    if S[i] == '1':
        if i == N - 1:
            flag_flag = True
        if flag:
            cnt += 1
        else:
            flag = True
            cnt_list.append([cnt, -1])
            cnt = 1
    if S[i] == '0':
        if i == N - 1:
            flag_flag = False
        if flag:
            flag = False
            cnt_list.append([-1, cnt])
            cnt = 1
        else:
            cnt += 1
if not flag_flag:
    cnt_list.append([cnt, -1])
else:
    cnt_list.append([-1, cnt])

n = len(cnt_list)
if cnt_list[0][0] == -1:    
    if K >= n//2:
        print(N)
        exit()
    k = K
else:                       
    if K >= math.ceil(n/2):
        print(N)
        exit()
    k = K



cumsum = [0] * (n+1)
for i in range(n):
    cumsum[i+1] = cumsum[i] + max(cnt_list[i])

ans = 0
if cnt_list[0][0] == -1:        
    if cnt_list[-1][0] == -1:   
        for i in range(1, n - 2 * k + 1, 2):
            ans = max(ans, cumsum[2 * k + i] - cumsum[i-1])
    else:                        
        for i in range(1, n - 2*k + 1, 2):
            ans = max(ans, cumsum[2 * k + i] - cumsum[i-1])
        ans = max(ans, cumsum[n] - cumsum[n - 2*k])
    
else:                           
    if cnt_list[-1][0] == -1:   
        ans = cumsum[2*k] - cumsum[0]
        for i in range(2, n - 2 * k + 1, 2):
            ans = max(ans, cumsum[2 * k + i] - cumsum[i-1])
    else:                       
        ans = cumsum[2 * k] - cumsum[0]
        for i in range(2, n - 2 * k + 1, 2):
            ans = max(ans, cumsum[ 2* k + i] - cumsum[i-1])
        ans = max(ans, cumsum[n] - cumsum[n - 2*k])

print(ans)

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

first = now = S[0]

count = [] 
c = 0
for s in S:
  if s == now:
    c += 1
  else:
    count.append(c)
    c = 1
    now = s

count.append(c)

l = len(count)
if first == '0':
  up_group = -(-l//2) 
else:
  up_group = l//2

if up_group <= K: 
  print(N)
  quit()


if first == '0':
  ans = sum(count[:2*K])
  s = ans
  idx1 = 0
  idx2 = 2*K
  while idx2 < l:
    if idx1 == 0:
      s = s - count[0] + sum(count[idx2:idx2+2])
      idx1 += 1
    else:    
      s = s - sum(count[idx1:idx1+2]) + sum(count[idx2:idx2+2])
      idx1 += 2
    
    idx2 += 2
    ans = max(ans, s)
  
  print(ans)
else:
  ans = sum(count[:2*K+1])
  s = ans
  idx1 = 1
  idx2 = 2*K+1
  while idx2 < l:
    if idx1 == 1:
      s = s - sum(count[:2]) + sum(count[idx2:idx2+2])
      idx1 += 1
    else:
      s = s - sum(count[idx1:idx1+2]) + sum(count[idx2:idx2+2])
      idx1 += 2
    
    idx2 += 2
    ans = max(ans, s)
  
  print(ans)

Solution (Python):
import sys
from collections import deque
from itertools import groupby
def input(): return sys.stdin.readline().rstrip()

def main():
    _, K = map(int, input().split())
    S = input()

    ans = 0
    l = 0
    que = deque()
    que_sum = 0
    for k, v in groupby(S):
        if k == '0':
            l0 = len(tuple(v))
            que_sum += l0
            que.append(l + l0)
            if len(que) > K:
                que_sum -= que.popleft()
        else:
            l = len(tuple(v))
            que_sum += l
        if que_sum > ans:
            ans = que_sum
    print(ans)

if __name__ == '__main__':
    main()

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

xs = []
cur, length = S[0], 1
for i in range(1, N):
    if S[i] != cur:
        xs.append((cur, length))
        cur = S[i]
        length = 1
    else:
        length += 1
xs.append((cur, length))

if sum(1 for v, l in xs if v == 0) <= K:
    print(N)
else:
    length = 0
    changed_count = 0
    for i, (v, l) in enumerate(xs):
        if v == 0:
            changed_count += 1
        length += l
        if changed_count == K:
            left = 0 if xs[0][0] == 0 else 1
            if i + 1 < len(xs):
                length += xs[i + 1][1]
            break
    max_length = length
    for right in range(i + 2, len(xs), 2):
        length -= xs[left][1]
        if 0 <= left - 1:
            length -= xs[left - 1][1]
        length += xs[right][1]
        if right + 1 < len(xs):
            length += xs[right + 1][1]
        max_length = max(max_length, length)
        left += 2
    print(max_length)

Solution (Python):
import sys, bisect, math, itertools, string, queue, copy


from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush

sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input()) 
def inpm(): return map(int,input().split()) 
def inpl(): return list(map(int, input().split())) 
def inpls(): return list(input().split())  
def inplm(n): return list(int(input()) for _ in range(n)) 
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)] 
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)]) 
def graph():
    n=inp()
    g=[[] for _ in range(n)]
    for i in range(n):
        a = inp()
        a -= 1
        g[i].append(a)
        g[a].append(i)
    return n,g

def main():
    n,k = inpm()
    s = list(input())
    t = [0]
    for i in range(n-1):
        if s[i] != s[i+1]:
            t.append(i+1)
    a = len(t)
    if len(t)<2*k-1:
        print(n)
        return
    for i in range(2*a+4):
        t.append(n)
    ans = 0
    if s[0]=='1':
        for i in range(a):
            if 2*k+2*i+1 >= len(t):
                break
            ans = max(ans,t[2*k+2*i+1]-t[2*i])
    else:
        ans = t[2*k]
        for i in range(a):
            if 2*k+2*i+2 >= len(t):
                break
            ans = max(ans,t[2*k+2*i+2]-t[2*i+1])
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
N,K = map(int,input().split())
s=input()
if '0' not in s:
    print(len(s))
else:
    s2 = [0]
    count = 0
    for i in range(len(s)-1):
        count += 1
        if s[i] != s[i+1]:
            s2.append(count)
    s2.append(count+1)
    if s[0] == '0':
        s2.insert(0,0)
    ans = 0
    for i in range(0,len(s2)-1,2):
        ans = max(ans,s2[min(len(s2)-1,i+2*K+1)] - s2[i])
    print(ans)

Solution (Python):
import sys

n, k = map(int, sys.stdin.readline().split())
s = sys.stdin.readline().rstrip()
s += '0'

def main():
    l = [0]
    r = [0]
    flag = False
    for i in range(n+1):
        if s[i] == '1':
            if not flag:
                l.append(i)
            flag = True
        else:
            if flag:
                r.append(i-1)
            flag = False

    l.append(n-1)
    r.append(n-1)
    m = len(l)

    if k >= m - 1:
        return n
    res = 0
    for i in range(m-k):
        res = max(res, r[i+k]-l[i]+1)
    return res
    
if __name__ == '__main__':
    ans = main()
    print(ans)

Solution (Python):
n,k = map(int,input().split())
s = list(input())
s_0_1 = [1]*(k+int(s[0]))
s_1_0 = []
for i in range(n-1):
    if s[i] == "0" and s[i+1] == "1":
        s_0_1.append(i+2)
    elif s[i] == "1" and s[i+1] == "0":
        s_1_0.append(i+1)
s_1_0.extend([n]*(k+int(s[-1])))
ans = 0
for i in range(len(s_0_1)):
    ans = max(ans, s_1_0[i]-s_0_1[i]+1)
print(ans)

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

c0 = S.count('0')
if c0 == 0 or c0 == N:
  ans = N
else:
  A = [[0, 0, 0]]
  c = 0
  now = '1' if S[0]=='0' else '0'
  for s in S:
    if s == '0':
      if now == '1':
        A[-1][2] = c
        A.append([c, 1, 0])
        c = 1
        now = '0'
      else:
        c += 1
    else:
      if now == '1':
        c += 1
      else:
        A[-1][1] = c
        c = 1
        now = '1'
  
  A = A[1:]  
  if s == '0':
    if S[-2] == '0':
      A[-1][1] = c
  else:
    if S[-2] == '0':
      A[-1][2] = 1
    else:
      A[-1][2] = c
  m = len(A)
  if m <= K:
    ans = N
  else:
    ans = 0
    for i, a in enumerate(A):
      ans += a[0]+a[1]
      if i == K-1:
        ans += a[2]
        break

    l = ans
    for i in range(1, m-K+1):
      a1 = A[i-1]
      a2 = A[i+K-1]
      l = l-(a1[0]+a1[1])+(a2[1]+a2[2])
      ans = max(ans, l)


print(ans)

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

cnt = 0

if s[0] == '0': ss.append(0)
i = 0
while i < n:
    j = i
    while j < n and s[i] == s[j]: j += 1
    ss.append(j-i)
    i = j
if s[-1] == '0': ss.append(0)

right = 2*k+1
left = 0
val = sum(ss[:right])
ans = val


while right < len(ss):
    for i in [1, 2]:
        
        val += ss[right]
        val -= ss[left]
        left += 1
        right += 1
    
    ans = max(ans, val)
    
print(ans)

Solution (Python):
def f(nums, l):
  out=sum(nums[:l])
  tmp=sum(nums[:l])
  n=len(nums)
  i=2
  while i+l-1<n:
    tmp=tmp-nums[i-2]-nums[i-1]+nums[i+l-2]+nums[i+l-1]
    out=max(out, tmp)
    i+=2
  return out

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

i=0
j=1
nums=[]
while i<N:
  while j<N and S[i]==S[j]:
    j+=1
  nums.append(j-i)
  i=j
  j=i+1
  
if S[0]=='0':
  nums=[0]+nums
  
if S[-1]=='0':
  nums=nums+[0]
  
ans=f(nums, 2*K+1)
print(ans)

Solution (Python):


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

s=1
temp=0
for i in range(N):
    if s!= int(S[i]):
        A.append(temp)
        temp=1
        s=(s+1)%2
    else:
        temp+=1
        
A.append(temp)
if s==0:
    A.append(0)
        
NA=len(A)
if 2*K+1>=NA:
    ans=N
else:
    temp=sum(A[:2*K+1])
    ans=temp
    for i in range(0,NA-2*K-2,2):
        temp=temp-A[i]-A[i+1]+A[i+2*K+1]+A[i+2*K+2]
        ans=max(ans,temp)
        
print(ans)

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

stand = [0]
rev = [0]
if s[0] == '0':	
	rev.append(0)
cnt = 1
for i in range(1,n):
	if s[i] == s[i-1]:
		cnt += 1
	else:
		if s[i-1] == '0':
			stand.append(cnt)	
		else:
			rev.append(cnt)
		cnt = 1
if s[n-1] == '0':
	stand.append(cnt)
	rev.append(0)
else:
	rev.append(cnt)

for i in range(1,len(rev)):
	rev[i] += rev[i-1]
	
for i in range(1,len(stand)):
	stand[i] += stand[i-1]

ans = 0

k = min(k,len(stand)-1)
for i in range(len(rev) - k - 1):
	sms = rev[i+k+1] - rev[i] + stand[i+k] - stand[i]
	ans = max(ans,sms)

print(ans)

Solution (Python):
import copy
N, K = map(int, input().split())
S = input()
if S.count('0') == 0:
    print(S.count('1'))
    exit()

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

olst = []
zlst = []
tmp = S[0]
tcnt = 1
for i in range(1, N):
    if tmp != S[i]:
        if tmp == '1':
            olst.append(tcnt)
        else:
            zlst.append(tcnt)
        tmp = S[i]
        tcnt = 1
    else:
        tcnt += 1

if tmp == '1':
    olst.append(tcnt)
else:
    zlst.append(tcnt)

index = 0
flst, slst = [], []
if S[0] == '1':
    index = 1
    flst = copy.deepcopy(olst)
    slst = copy.deepcopy(zlst)
else:
    flst = copy.deepcopy(zlst)
    slst = copy.deepcopy(olst)
total = [0] * (len(flst) + len(slst) + 1)
for i in range(1, len(flst) + len(slst) + 1):
    if i % 2 != 0:
        total[i] += flst[i // 2] + total[i - 1]
    else:
        total[i] += slst[i // 2 - 1] + total[i - 1]
ans = 0
tmp1 = (2 * K) - 1
tmp2 = 2 * K
for i in range(1, len(total) - K):
    tmp = 0
    if index == 0:
        if i % 2 != 0:
            if i + tmp1 < len(total):
                tmp += total[i + tmp1] - total[i - 1]
            else:
                tmp += total[-1] - total[i - 1]
        else:
            if i + tmp2 < len(total):
                tmp += total[i + tmp2] - total[i - 1]
            else:
                tmp += total[-1] - total[i - 1]
    else:
        if i % 2 != 0:
            if i + tmp2 < len(total):
                tmp += total[i + tmp2] - total[i - 1]
            else:
                tmp += total[-1] - total[i - 1]
        else:
            if i + tmp1 < len(total):
                tmp += total[i + tmp1] - total[i - 1]
            else:
                tmp += total[-1] - total[i - 1]
    ans = max(ans, tmp)
print(ans)

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

s.append("")

current = 0
l = []
for i in range(n+1):
    if s[i] != s[current]:
        l.append(i-current)
        current = i

if s[0] == "0":
    l.insert(0, 0)
if s[-2] == "0":
    l.append(0)

cusum = [0]*(len(l)+1)
for i in range(len(l)):
    cusum[i+1] = cusum[i]+l[i]

ans = 0
for i in range(0, len(cusum), 2):
    j = i+k*2+1
    if j >= len(cusum): j = len(cusum) - 1
    ans = max(ans, cusum[j]-cusum[i])

print(ans)

Solution (Python):
N,K=map(int,input().split())
S=input()
if N==1:
    print(1)
    exit()


lst=[]
if S[0]!="1":
    lst.append([0,0])
flag=0
for i in range(N):
    if flag==0 and S[i]=="1":
        flag=1
        st=i
    elif flag and S[i]=="0":
        lst.append([st,i-1])
        flag=0
if S[-1]!="1":
    lst.append([N-1,N-1])

if flag:
    lst.append([st,N-1])

if K>=len(lst):
    print(N)
    exit()
else:
    value=0
    for i in range(len(lst)-K):
        a=lst[i+K][1]-lst[i][0] +1
        if a>value:
            value=a
print(value)

Solution (Python):
n,k=map(int,input().split())
l=[0]
s=input()
cnt=0
for c in s:
  if (len(l)%2) ^ (c=='0'):
    cnt+=1
  else:
    l.append(cnt)
    cnt+=1
l.append(cnt)
if len(l)%2:
  l.append(cnt)

k = min(k,(len(l)-1)//2)

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

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

if len(set(S)) == 1:
  print(len(S))
  exit(0)
  

nums = []
if S[0] != '1':
  nums.append(0)

current = S[0]
count = 1
for i, s in enumerate(list(S)):
  if i == len(S)-1:
    nums.append(count)
    break
  if S[i+1] != s:
    nums.append(count)
    count = 1
    current = s
  else:
    count += 1

if S[len(S)-1] != '1':
  nums.append(0)

sums = [0]
for index, i in enumerate(nums):
  sums.append(sums[index] + i)
  
res = 0
for left in range(0, len(sums), 2):
  right = left + K*2 + 1
  if right > len(sums):
    right = len(sums) - 1
  res = max(res, sums[right] - sums[left])
  
print(res)

Solution (Python):
N, K = map(int, input().split())
S = input()
T = []
count = 1
for n in range(N - 1):
    s0, s1 = S[n], S[n + 1]
    if s0 == s1:
        count += 1
    else:
        T.append(count)
        count = 1
T.append(count)
if S[0] == '0':
    T = [0] + T
if S[-1] == '0':
    T = T + [0]
L = len(T)
if 2 * K + 1 >= L:
    print(N)
    exit()

now = sum(T[0: 2 * K + 1])
ans = now
for l in range(2, L - (2 * K + 1) + 1, 2):
    now -= T[l - 2]
    now -= T[l - 1]
    now += T[l + 2 * K - 1]
    now += T[l + 2 * K]
    if now > ans:
        ans = now
print(ans)

Solution (Python):
from itertools import groupby
from numpy import cumsum


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

if len(set(S)) == 1:
    print(N)
    exit()


G = []
for k, v in groupby(S):
    G.append(len(list(v)))


if S[0] == '0':
    G.insert(0, 0)
if S[-1] == '0':
    G.append(0)

cum = [0]
cum.extend(list(cumsum(G)))
cum.append(0)

ans = 0
gc = len(G)
w = min(gc, 3+(K-1)*2)
for i in range(0, gc-w+2, 2):
    ans = max(ans, cum[i+w]-cum[i])
print(ans)

Solution (Python):
from itertools import groupby
from numpy import cumsum


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

if len(set(S)) == 1:
    print(N)
    exit()

G = []
for k, v in groupby(S):
    G.append(len(list(v)))
if S[0] == '0':
    G.insert(0, 0)
if S[-1] == '0':
    G.append(0)

cum = [0]
cum.extend(list(cumsum(G)))
cum.append(0)

ans = 0
gc = len(G)
w = min(gc, 3+(K-1)*2)
for i in range(0, gc-w+2, 2):
    ans = max(ans, cum[i+w]-cum[i])
print(ans)

Solution (Python):
from itertools import groupby
from numpy import cumsum


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

if len(set(S)) == 1:
    print(N)
    exit()

G = []
if S[0] == '0':
    G.append(0)
for k, v in groupby(S):
    G.append(len(list(v)))
G.append(0)

cum = [0]
cum.extend(list(cumsum(G)))
cum.append(0)

ans = 0
gc = len(G)
w = min(gc, 3+(K-1)*2)
for i in range(0, gc-w+2, 2):
    ans = max(ans, cum[i+w]-cum[i])
print(ans)

Solution (Python):
from sys import exit
def groupby(a):
    a2=[[a[0],1]]
    for i in range(1,len(a)):
        if a2[-1][0]==a[i]:
            a2[-1][1]+=1
        else:
            a2.append([a[i],1])
    return a2

n,k=map(int,input().split())
s=list(input())
x=groupby(s)
l=len(x)
y=[x[i][1] for i in range(l)]

if x[0][0]=="0":
    su=sum(y[:2*k])
    ans=su
    y.pop(0)
    l-=1
else:
    ans=0
su=sum(y[:2*k+1])
ans=max(ans,su)
l_sub=(l-(2*k+1))//2
if l_sub<=0:
    print(ans)
    exit()
for i in range(l_sub):
    su-=y[2*i]
    su-=y[2*i+1]
    su+=y[2*k+1+(2*i)]
    su+=y[2*k+1+(2*i+1)]
    ans=max(su,ans)

if 2*k+1+2*l_sub<l:
    su-=y[2*l_sub]
    su-=y[2*l_sub+1]
    su+=y[2*k+1+2*l_sub]
    ans=max(su,ans)
print(ans)

Solution (Python):
n, k = map(int, input().split())
s = input()
start = [0] * k
c = '0'
ret = 0
for i in range(len(s)):
    if c == '0' and s[i] == '1':
        start.append(i)
    elif c == '1' and s[i] == '0':
        ret = max(ret, i - start.pop(0))
    c = s[i]
ret = max(ret, n - start.pop(0))
print(ret)

Solution (Python):
n, k = map(int, input().split())
s = input()
start = [0] * (k + 1)
c = '1'
ret = 0
for i in range(len(s)):
    if c == '0' and s[i] == '1':
        start.append(i)
    elif c == '1' and s[i] == '0':
        ret = max(ret, i - start.pop(0))
    c = s[i]
ret = max(ret, n - start.pop(0))
print(ret)

Solution (Python):
n, k = map(int, input().split())
s = input()
l = [0] * (k * 2 + 1)
c = '1'
p = 0
ret = 0
for i in range(len(s)):
    if s[i] != c:
        ret = max(ret, i - l[p])
        l.append(i)
        if c == '1':
            l.pop(0)
            l.pop(0)
        c = s[i]
ret = max(ret, n - l[p])
print(ret)

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

segs = []
i = 0
while i < n:
    if s[i] == '0':
        j = i
        while j + 1 < n and s[j + 1] == '0':
            j += 1
        segs.append((i, j))
        i = j
    i += 1
segs = segs or [(0, n - 1)]

ret = 0
for right in range(len(segs)):
    left = max(0, right - nFlips + 1)
    size = segs[right][1] - segs[left][0] + 1
    size += segs[left][0] - segs[left - 1][1] - 1 if left - 1 >= 0 else segs[left][0]
    size += segs[right + 1][0] - segs[right][1] - 1 if right + 1 < len(segs) else n - segs[right][1] - 1
    ret = max(ret, size)
print(ret)

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

segs = []
i = 0
while i < n:
    if s[i] == '0':
        j = i
        while j + 1 < n and s[j + 1] == '0':
            j += 1
        segs.append((i, j))
        i = j
    i += 1
segs = segs or [(0, n - 1)]

ret = 0
left = 0
for right in range(len(segs)):
    while right - left + 1 > nFlips:
        left += 1
    size = segs[right][1] - segs[left][0] + 1
    size += segs[left][0] - segs[left - 1][1] - 1 if left - 1 >= 0 else segs[left][0]
    size += segs[right + 1][0] - segs[right][1] - 1 if right + 1 < len(segs) else n - segs[right][1] - 1
    ret = max(ret, size)
print(ret)

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

segs = []
i = 0
while i < n:
    if s[i] == '0':
        j = i
        while j + 1 < n and s[j + 1] == '0':
            j += 1
        segs.append((i, j))
        i = j + 1
    else:
        i += 1
segs = segs or [(0, n - 1)]

ret = 0
left = 0
for right in range(len(segs)):
    while right - left + 1 > nFlips:
        left += 1
    size = segs[right][1] - segs[left][0] + 1
    size += segs[left][0] - segs[left - 1][1] - 1 if left - 1 >= 0 else segs[left][0]
    size += segs[right + 1][0] - segs[right][1] - 1 if right + 1 < len(segs) else n - segs[right][1] - 1
    
    ret = max(ret, size)
print(ret)

Solution (Python):
n, k = [int(i) for i in input().split()]
s = input() + 'E'
top = int(s[0])
now = s[0]
idx = 0
ranges = []
for i, v in enumerate(s):
	if now != v:
		ranges.append(i - idx)
		idx = i
		now = v
zero = (k * 2) * [0]
ranges = zero + ranges + zero
targetIdx = 1 if top % 2 == 0 else 0 
ret = 0
tmp = 0
for i, v in enumerate(ranges):
	tmp += v
	if i > (k * 2 + 1):
		tmp -= ranges[i - (k * 2 + 1)]
	if i % 2 == targetIdx:
		ret = max(tmp, ret)
print(ret)

Solution (Python):
n, k = map(int, input().split())
s = input()
l = [0] * (k * 2 + 1)
c = '1'
p = 0
ret = 0
for i in range(len(s)):
    if s[i] != c:
        ret = max(ret, i - l[p])
        l.append(i)
        if c == '1':
            p += 2
        c = s[i]
ret = max(ret, n - l[p])
print(ret)

Solution (Python):
N, K = [int(s) for s in input().split(' ')]
S = input()
ss = []
ssc = []
subs = S[0]
for s in S[1:]:
    if subs[-1] != s:
        ss.append(subs)
        ssc.append(len(subs))
        subs = s
        continue
    subs += s
ss.append(subs)
ssc.append(len(subs))
suma = [0]
for sc in ssc:
    suma.append(suma[-1] + sc)


max_c = 0
for i in range(len(ss)):
    k = (K * 2 + 1) if ss[i][0] == '1' else (K * 2)
    l = i
    r = min(i + k, len(ss))
    c = suma[r] - suma[l]
    max_c = max(max_c, c)

print(max_c)

Solution (Python):
from bisect import bisect_right


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

psum = [0] * 2
ncnt = [0] * 2

i = 1
state = int(S[0])
if state == 0:
    ncnt[i] += 1
for s in S:
    if int(s) != state:
        psum.append(psum[i])
        ncnt.append(ncnt[i])
        i += 1
        state ^= 1
        if s == '0':
            ncnt[i] += 1

    psum[i] += 1

ans = 0
for i in range(len(psum)):
    j = bisect_right(ncnt, K + ncnt[i])
    ans = max(ans, psum[j - 1] - psum[i])

print(ans)

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

A=[]
B=[]
for i in range(N-1):
  if S[i]=='0'and S[i+1]=='1':
    B.append(i)
if S[-1]=='0':
  B.append(N-1)
if S[0]=='0':
  A.append(0)
for i in range(N-1):
  if S[i]=='1'and S[i+1]=='0':
    A.append(i+1)

if len(A)<=K:
  print(N)
else:
  y=A[K]
  z=N-B[-1*(K+1)]-1
  l=0
  ans=0
  for i in range(len(A)-K-1):
    l=A[i+K+1]-B[i]-1
    ans=max(l,ans)
  print(max(ans,y,z))

Solution (Python):
N,K=map(int,input().split())
S=list(input())
j=0
ans=0
cnt=0
for i in range(N):
  if S[i]=="0":
    if i==0 or S[i-1]=="1":
      cnt+=1
  if cnt>K:
    while S[j]=="1":
      j+=1
    while S[j]=="0":
      j+=1
    cnt-=1
  ans=max(ans,i-j+1)

print(ans)

Solution (Python):
n, m = map(int, input().split())
s = input()
a = []
for i in range(n):
    if i == 0 or s[i] != s[i - 1]:
        a.append(1)
    else:
        a[len(a) - 1] += 1
for i in range(1, len(a)):
    a[i] += a[i - 1]
cur = int(s[0])
ans = 0
for i in range(len(a)):
    tmp = a[i]
    if cur:
        if i > 2 * m:
            tmp -= a[i - 2 * m - 1]
    else:
        if i > 2 * m - 1:
            tmp -= a[i - 2 * m]
    ans = max(ans, tmp)
    cur = 1 - cur
print(ans)

Solution (Python):
from _collections import deque


def parser():
    while 1:
        data = list(input().split(' '))
        for number in data:
            if len(number) > 0:
                yield (number)


input_parser = parser()


def gw():
    global input_parser
    return next(input_parser)


def gi():
    data = gw()
    return int(data)


MOD = int(1e9 + 7)

import numpy
from collections import deque
from math import sqrt
from math import floor


"""
need to consider the case that ticket is not enough to lower everything
"""

N = gi()
K = gi()
S = gw()

seg_ps = []

seg_ps.append(0)

cl = 1
cur = S[0]

for i in range(1, len(S)):
    if (S[i] == S[i - 1]):
        cl += 1
    else:
        tail = seg_ps[-1]
        seg_ps.append(tail + cl)
        cur = S[i]
        cl = 1

seg_ps.append(seg_ps[-1] + cl)

ans = 0

start1 = S[0] == '1'

for i in range(1, len(seg_ps)):
    is1 = 0
    if start1:
        is1 = i % 2
    else:
        is1 = (i + 1) % 2
    if is1:
        si = max(0, i - 2 * K - 1)
    else:
        si = max(0, i - 2 * K)
    ans = max(ans, seg_ps[i] - seg_ps[si])

print(ans)

Solution (Python):
n,k=map(int,input().split())
s=list(input())
j=0
ans=0
cnt=0
for i in range(n):
    if s[i]=="0":
        if i==0 or s[i-1]=="1":
            cnt+=1
    if cnt>k:
        while s[j]=="1":
            j+=1
        while s[j]=="0":
            j+=1
        cnt-=1
    ans=max(ans,i-j+1)

print(ans)

Solution (Python):
import sys
from heapq import heappush,heappop
sys.setrecursionlimit(10**9)
INF = 10**18


def input():
    return sys.stdin.readline().rstrip()


def main():
    N,K=map(int,input().split())
    S=list(map(int,list(input())))
    f=S[0]
    left1=0
    for i,x in enumerate(S):
        if x==0:
            break
        else:
            left1+=1
    l=[left1]
    tmp=0
    pre=1
    for i,x in enumerate(S[left1:]):
        if pre!=x:
                l.append(tmp)
                tmp=0
        tmp+=1
        pre=x
    l.append(tmp)
    A=[0]*(len(l)+1)
    for i in range(len(l)):
        A[i+1]=A[i]+l[i]
    ans=0
    for i in range(len(l)+1):
        if i%2:
            ans=max(ans,A[i]-A[max(0,i-K*2)])
        else:
            ans=max(ans,A[i]-A[max(0,i-K*2-1)])
    print(ans)
    

if __name__ == '__main__':
    main()