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 = str(input())
head = s[0]

counts = []
_hd = head
_cnt = 0
for _s in s:
  if _s == _hd:
    _cnt += 1
  else:
    counts.append(_cnt)
    _hd = _s
    _cnt = 1
else:
  counts.append(_cnt)

acc = [0]
for c in counts:
  acc.append(acc[-1] + c)
if s[-1] == '0':
  acc.append(acc[-1])

ans = 1
if len(counts) <= 2*k:
  ans = len(s)
else:
  if head == '1':
    for i in range(2*k+1, len(acc)+1, 2):
      p = acc[i] - acc[i - (2*k+1)]
      ans = max(ans, p)
  else:
    for i in range(2*k, len(acc)+1, 2):
      p = acc[i] - acc[max(0, i-(2*k+1))]
      ans = max(ans, p)

print(ans)

Solution (Python):
n,k = map(int,input().split())
s = input()
cnt = 1
arr0 = []
if s[0]=="1":
  arr = [0]
else:
  arr = [0,0]
for i in range(n-1):
  if s[i] == s[i+1]:
    cnt += 1
  else:
    arr0.append(cnt)
    cnt = 1
arr0.append(cnt)
arr += arr0
if s[n-1] == "0":
  arr.append(0)
lsum = [0]*(len(arr)//2)
lsum[0] = arr[0]+arr[1]
for i in range(1,len(arr)//2):
  lsum[i] = lsum[i-1]+arr[2*i]+arr[2*i+1]
arr = [0,0]+[0]*(s[0]=="0")+arr0+[0]*(s[n-1]=="1")
rsum = [0]*(len(arr)//2)
for i in range(1,len(arr)//2):
  rsum[i] = rsum[i-1]+arr[2*i]+arr[2*i+1]
ans = 0
if k >= len(lsum)-1:
  print(n)
  quit()
for i in range(k,len(lsum)):
  ans = max(ans,lsum[i]-rsum[i-k])
print(ans)

Solution (Python):
n,k = map(int,input().split())
s = input()
cnt = 1
arr0 = []
if s[0]=="1":
  arr = [0]
else:
  arr = [0,0]
for i in range(n-1):
  if s[i] == s[i+1]:
    cnt += 1
  else:
    arr0.append(cnt)
    cnt = 1
arr0.append(cnt)
arr += arr0
if s[n-1] == "0":
  arr.append(0)
lsum = [0]*(len(arr)//2)
lsum[0] = arr[0]+arr[1]
for i in range(1,len(arr)//2):
  lsum[i] = lsum[i-1]+arr[2*i]+arr[2*i+1]
arr = [0,0]+[0]*(s[0]=="0")+arr0+[0]*(s[n-1]=="1")
rsum = [0]*(len(arr)//2)
for i in range(1,len(arr)//2):
  rsum[i] = rsum[i-1]+arr[2*i]+arr[2*i+1]
ans = 0
if k >= len(lsum)-1:
  print(n)
  quit()
for i in range(k,len(lsum)):
  ans = max(ans,lsum[i]-rsum[i-k])
print(ans)

Solution (Python):
n,k = map(int,input().split())
s= input()
if n==1:
    print(1)
    exit()

ls = []
cnt=[0,0]
prev=int(s[0])
if prev==0:
    ls.append(0)
cnt[prev]+=1
for i in range(1,n):
    now= int(s[i])
    cnt[now] +=1
    if now!=prev:
        ls.append(cnt[prev])
        cnt[prev]=0
    prev = now
if cnt[now]!=0:
    ls.append(cnt[now])
if s[-1] =="0":
    ls.append((0))



l=len(ls)//2+1
p = 0
idx = 0
ans =ls[0]
temp=ls[0]
for i in range(1,l):
    temp+= ls[2*i]+ls[2*i-1]
    ans = max(temp,ans)
    p+=1
    if p>=k:
        temp -= (ls[2*idx]+ls[2*idx+1])
        idx+=1
print(ans)

Solution (Python):
n,k = map(int,input().split())
s = input()
x = len(s)
lst = [0]
ans = 0
if s[0] == "1" and s[x-1] == "0":
    s = s[::-1]
for i in range(1,x):
    if s[i-1] != s[i]:
        lst.append(i)
lst.append(x)
if s[0] == "0" and s[x-1] == "1":
    if 2*k >= len(lst):
        ans = x
    else:
        i = 1
        while i+2*k+1 <= len(lst)-1:
            ans = max(ans,lst[i+2*k+1]-lst[i])
            i += 2
        ans = max(ans,lst[2*k])
if s[0] == "0" and s[x-1] == "0":
    if 2*k >= len(lst):
        ans = x
    else:
        i = 1
        while i+2*k+1 <= len(lst)-1:
            ans = max(ans,lst[i+2*k+1]-lst[i])
            i += 2
        ans = max(ans,lst[2*k])
        ans = max(ans,x-lst[len(lst)-2*k-1])
if s[0] == "1" and s[x-1] == "1":
    if 2*k+1 >= len(lst):
        ans = x
    else:
        i = 0
        while i+2*k+1 <= len(lst)-1:
            ans = max(ans,lst[i+2*k+1]-lst[i])
            i += 2
print(ans)

Solution (Python):
n,k = map(int,input().split())
s = str(input())
s += "2"
if int(s[0]) == 0:
  a = [0]
  prev = "0"
  count = 1
else:
  a = []
  flag = False
  prev = "1"
  count = 1
ap = a.append
for i in range(1,n+1):
  if s[i] == prev:
    count += 1
  else:
    ap(count)
    count = 1
    prev = s[i]
if s[-2] == "0":
  ap(0)
if 2*k+1 >= len(a):
  print(sum(a))
  exit()
g = sum(a[0:2*k+1])
mx = g
i = 1
while 2*i+2*k < len(a):
  g -= (a[2*i-2]+a[2*i-1])
  g += (a[2*i+2*k-1]+a[2*i+2*k])
  if g > mx:
    mx = g
  i += 1
print(mx)

Solution (Python):

import sys
input = sys.stdin.readline
import numpy as np









N,K = map(int,input().split())
S = input().rstrip() + "x"


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


if S[0] != "1":
    A = [0] + A
if S[N-1] != "1":
    A = A + [0]

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

if 2*K+1 > len(A):
    print(A[-1])
else:
    si = 2*K
    ans = A[si]
    for i in range(2,len(A)-si,2):
        ans = max(ans, A[si+i]-A[i-1])
    print(ans)

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

A=[0] if S[0]=="1" else []
cnt,fig=0,S[0]
for i in range(n+1):
    if fig!=S[i]:
        A.append(cnt)
        fig=S[i]
        cnt=1
    else:cnt +=1

if S[-2]=="0":A.append(0)
num=2*(k+1)
B=[[0,0]] if S[0]=="0" else []
for i in range(1,len(A)):
    A[i]=A[i]+A[i-1]
for i in range(0,len(A),2):
    B.append([A[i],A[i+1]])

C=[]
left=0
for i in range(max(0,len(B)-k)):
    C.append(B[k+i][1]-B[left+i][0])
print(max(C) if len(B)-k>0 else B[-1][1])

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

from itertools import accumulate

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

S = input()

num = []

if S[0] == '0':
    num.append(0)

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

cnt = 1

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

if S[-2] == '0':
    num.append(0)

num = [0] + num

cum_sum = list(accumulate(num))


ans = 0
for i in range(0, len(cum_sum), 2):
    right = i + K * 2 + 1

    if right > len(cum_sum)-1:
        right = len(cum_sum) - 1

    ans = max(ans, cum_sum[right]-cum_sum[i])

print(ans)

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

d = deque([])
ans = 0
before = "1"
tmp = 0
p = 0
for s in sl:
    if before == s:
        p += 1
    else:
        d.append(p)
        tmp += p
        p = 1
        before = s
    if len(d) == 2 * k + 1:
        ans = max(tmp, ans)
        tmp -= d.popleft()
        tmp -= d.popleft()
else:
    d.append(p)
    tmp += p
    ans = max(tmp, ans)

print(ans)

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

t = []
c = '0'
v = 0
for i in range(n):
    if c != s[i]:
        t.append(v)
        v = 1
        c = s[i]
    else:
        v += 1
t.append(v)

r = 0
temp = 0
result = 0
for l in range(0, len(t), 2):
    while (r - l) // 2 + 1 <= k and r < len(t):
        temp += t[r]
        if r + 1 < len(t):
            temp += t[r + 1]
        r += 2
    result = max([temp, result])

    if r == l:
        r += 2
        if r - l < len(t):
            temp += t[r - 1] 
    else:
        temp -= t[l]
        if l - 1 > 0:
            temp -= t[l - 1]

print(result)

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

state = '1'
cnt = 0
start = 0
ret = 0

for i in range(len(s)):
    if state == '1' and s[i] == '0':
        cnt += 1
        if cnt <= k:
            length = i - start + 1
            ret = max(ret, length)
        else:
            has_zero = False
            while True:
                if has_zero and s[start] == '1':
                    break
                else:
                    if s[start] == '0':
                        has_zero = True
                    start += 1
            length = i - start + 1
            ret = max(ret, length)
        state = '0'
    elif state == '1' and s[i] == '1':
        length = i - start + 1
        ret = max(ret, length)
    elif state == '0' and s[i] == '0':
        length = i - start + 1
        ret = max(ret, length)
    else:
        state = '1'
        length = i - start + 1
        ret = max(ret, length)

print(ret)

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

now=S[0]
cnt=1
A=[] 
for i in range(n-1):
    if S[i+1]==now:
        cnt+=1
    else:
        A.append(cnt)
        cnt=1
        now=S[i+1]
if cnt>0:
    A.append(cnt)
l=len(A)
    
if S[0]=="0":
    if l<=2*k:
        ans=n
    elif 2*k+1<=l<=2*k+2:
        ans=max(sum(A[:2*k]),sum(A[1:]))
    else:
        ans=max(sum(A[:2*k]),sum(A[1:2*k+2]))
        cnt=sum(A[1:2*k+2])
        for i in range((l-2*k-2)//2):
            cnt-=A[2*i+1]+A[2*i+2]
            cnt+=A[2*k+2*i+2]+A[2*k+2*i+3]
            ans=max(cnt,ans)
        if l%2!=0:
            ans=max(sum(A[l-2*k:]),ans)
else:
    if l<=2*k+1:
        ans=n
    else:
        ans=sum(A[:2*k+1])
        cnt=ans
        for i in range((l-2*k-1)//2):
            cnt-=A[2*i]+A[2*i+1]
            cnt+=A[2*k+2*i+1]+A[2*k+2*i+2]
            ans=max(cnt,ans)
        if l%2==0:
            ans=max(sum(A[l-2*k:]),ans)
print(ans)

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

zero = []
tmp_zero_cnt = 0
tmp_zero_idx = -1
for i in range(N):
    if S[i] == "0":
        if tmp_zero_cnt == 0:
            tmp_zero_idx = i
        tmp_zero_cnt += 1
    else:
        if tmp_zero_cnt != 0:
            zero.append([tmp_zero_idx, tmp_zero_cnt])
            tmp_zero_cnt = 0
if tmp_zero_cnt != 0:
    zero.append([tmp_zero_idx, tmp_zero_cnt])

if len(zero) <= K:
    print(N)
else:
    zero = [[-1, 1]] + zero + [[N, 1]]
    M = len(zero)
    ans = 0
    for i in range(1, M - K):
        num = (zero[i + K][0] - 1) - (zero[i - 1][0] + zero[i - 1][1]) + 1
        if num > ans:
            ans = num
    print(ans)

Solution (Python):
N, K=map(int, input().split())
zeros=[0]
ones=[0]
S=list(input())
now=S[0]
one=0
zero=0
for i,s in enumerate(S):
  if s==now:
    if s=="0":
      zero+=1
    else:
      one+=1
  else:
    if s=="0":
      zero+=1
      ones.append(one)
      now=s
    else:
      one+=1
      zeros.append(zero)
      now=s
  if i==N-1:
    if s=="0":
      zeros.append(zero)
    else:
      ones.append(one)

ans=0
lone=len(ones)
lzero=len(zeros)
if lzero==0:
  print(N)
  exit()
if lzero<=K:
  print(N)
  exit()

if S[0]=="0":
  
  for i in range(lzero-K+1):
    idx=min(i+K, lzero-1)
    idx2=min(idx, lone-1)
    idx3=max(i-1, 0)
    ans=max(ans, zeros[idx]-zeros[i]+ones[idx2]-ones[idx3])
    
else:
  for i in range(lzero-K+1):
    idx=min(i+K, lzero-1)
    idx2=min(idx+1, lone-1)
    
    ans=max(ans, zeros[idx]-zeros[i]+ones[idx2]-ones[i])

print(ans)

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip()
from itertools import accumulate

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

S = input()


num = []

if S[0] == '0':
    num.append(0)

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

cnt = 1
for i in range(len(S)-1):
    if S[i] == S[i+1]:
        cnt += 1
    else:
        num.append(cnt)
        cnt = 1 
if S[-2] == '0':
    num.append(0)

num = [0] + num
cum_sum = list(accumulate(num))

ans = 0

for i in range(0, len(cum_sum), 2):
    right = i + K * 2 + 1
    if i + K * 2 >= len(cum_sum): 
        right = len(cum_sum) - 1

    ans = max(ans, cum_sum[right] - cum_sum[i])
print(ans)

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

data = [[],[]]
key = 1
cnt = 0
for i in range(n):
    if s[i] == key:
        cnt += 1
    
    else:
        data[key].append(cnt)
        key ^= 1
        cnt = 1
        
    if i == n-1:
        data[key].append(cnt)
    
if len(data[0]) == len(data[1]):
    data[1].append(0)

m = len(data[0])


if m <= k:
    print(n)
    exit()

tmp = data[1][0]
for i in range(k):
    tmp += data[0][i]
    tmp += data[1][i+1]

ans = tmp

for i in range(k,m):
    tmp -= data[0][i-k]
    tmp -= data[1][i-k]
    tmp += data[0][i]
    tmp += data[1][i+1]
    ans = max(ans,tmp)

print(ans)

Solution (Python):
n,k=map(int,input().split())
S=list(input())
A=[]
a=1
c=0
for i in range(n):
  if int(S[i])==a:
    c=c+1
  else:
    A.append(c)
    a=(a+1)%2
    c=1
A.append(c)
if S[-1]=="0":
  A.append(0)
aa=len(A)
if k>=((aa-1)//2):
  print(n)
else:
  ans=0
  a=0
  for i in range(2*k+1):
    a=a+A[i]
  if a>ans:
    ans=a
  for i in range((aa-1)//2-k):
    a=a-A[2*i]-A[2*i+1]+A[2*(i+k)+1]+A[2*(i+k)+2]
    if a>ans:
      ans=a
  print(ans)

Solution (Python):
n, k = map(int, input().split())
s = str(input())
v = [0]
for i in range(len(s)-1):
  if s[i] != s[i+1]:
    v.append(i+1)
ans = 0
arr = v + [n] *(2*k+1)
for i, j in enumerate(v):
  if s[j] == "0":
    ans = max(ans, arr[i+2*k]-arr[i])
  else:
    ans = max(ans, arr[i + 2*k +1] -arr[i])
print(ans)

Solution (Python):
from itertools import groupby
n, k = map(int, input().split())
S = input().strip()
L = [len(list(g)) for v, g in groupby(S)]
if S[0] == '0':
    L = [0] + L
if S[-1] == '0':
    L = L + [0]

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

Solution (Python):
n,k=map(int,input().split())
s=list(input())
idx=[]
idx.append(0)
for i in range(1,n):
  if s[i]!=s[i-1]:
    idx.append(i)
arr=idx+[n]*(2*k+1)
ans=-10**18
z=0
for m in idx:
    if s[m]=='0':
      ans=max(ans,arr[z+2*k]-arr[z])
    else:
      ans=max(ans,arr[z+2*k+1]-arr[z])
    z+=1
print(ans)

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    
    from itertools import combinations, permutations, accumulate, groupby
    
    from bisect import bisect_left,bisect_right
    import heapq
    from math import floor, ceil
    

    
    
    N,K = map(int, input().split())
    s = list(input().rstrip())
    l = []
    for k,v in groupby(s):
        l.append(len(list(v)))

    res = 0
    if s[0]=='1':
        start = 0
        stop = 2*K+1
        res = sum(l[:stop])
        ans = res
        while start<len(l):
            res += sum(l[stop:stop+2])-sum(l[start:start+2])
            ans = max(ans, res)
            start += 2
            stop += 2
    else:
        start = 1
        stop = 2*K+2
        res = sum(l[start:stop])
        ans = res
        while start<len(l):
            res += sum(l[stop:stop+2])-sum(l[start:start+2])
            ans = max(ans, res)
            start += 2
            stop += 2
        ans = max(ans, sum(l[:2*K]))
    print(ans)

if __name__ == '__main__':
    main()

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

now = "1"
p = 0
l = []

for i in range(n):
    if s[i] == now:
        p += 1
    else:
        l.append(p)
        if now == "1":
            now = "0"
        else:
            now = "1"
        p = 1

l.append(p)

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

m = len(l) // 2

ans = 1

if m <= k:
    ans = n
else:
    ans = sum(l[:2 * k + 1])
    nowans = ans
    for i in range(m - k):
        nowans += l[2*(k+i)+1] + l[2*(k+i+1)]
        nowans -= l[2*i] + l[2*i+1]
        ans = max(ans, nowans)

print(ans)

Solution (Python):
import sys
from bisect import *
from heapq import *
from collections import *
from itertools import *
from functools import *
from math import *
from fractions import *

sys.setrecursionlimit(100000000)
input = lambda: sys.stdin.readline().rstrip()

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

X = []
for c in S:
    if len(X) >= 1 and X[-1][0] == c:
        X[-1][1] += 1
    else:
        X.append([c, 1])

sm = [0] * (len(X) + 1)
for i in range(len(X)):
    sm[i + 1] = sm[i] + X[i][1]

ans = 0
for i in range(len(X)):
    if X[i][0] == 0:
        
        ans = max(ans, sm[min(i + (K - 1) * 2 + 2, len(X))] - sm[i])
        pass
    else:
        
        ans = max(ans, sm[min(i + K * 2 + 1, len(X))] - sm[i])
print(ans)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, K = MAP()
S = [int(s) for s in input()] + [1]

r = -1
ans = 0
cnt = 0
used = [0]*(N + 1)
for l in range(N):
    if S[l] + S[l-1] == 0:
        continue
    while r < N and used[r] - used[l-1] <= K:
        r += 1
        if S[r-1] == 1 and S[r] == 0:
            used[r] = used[r - 1] + 1
        else:
            used[r] = used[r - 1]
    ans = max(ans, r-l)

print(ans)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, K = MAP()
S = [int(s) for s in input()] + [1]

l = 0
r = -1
ans = 0
cnt = 0
tmp = ""
used = [0]*(N + 1)
for l in range(N):
    if S[l] + S[l-1] == 0:
        continue
    while r < N and used[r] - used[l-1] <= K:
        r += 1
        if S[r]:
            used[r] = used[r - 1]
        else:
            if S[r-1]:
                used[r] = used[r - 1] + 1
            else:
                used[r] = used[r - 1]
    ans = max(ans, r-l)

print(ans)

Solution (Python):
from sys import stdin
def main():
    
    readline=stdin.readline
    N,K=map(int,readline().split())
    S=readline().strip()

    li=[]
    i=0
    now=0
    if S[0]=="0":
        li.append(0)
    while i<len(S):
        if S[i]=="1":
            while True:
                if S[i]=="1":
                    now+=1
                    i+=1
                    if i==len(S):
                        break
                else:
                    break
        elif S[i]=="0":
            while True:
                if S[i]=="0":
                    now+=1
                    i+=1
                    if i==len(S):
                        break
                else:
                    break
        li.append(now)
        now=0
                
    
    for i in range(len(li)):
        if i==0:
            continue
        else:
            li[i]+=li[i-1]

    res=0
    if len(li)>=2*K+1:
        for left in range(0,len(li),2):
            if left==0:
                res=li[2*K]
            else:
                res=max(res,li[min(2*K+left,len(li)-1)]-li[left-1])
    else:
        res=li[-1]
    print(res)
    
if __name__=="__main__":
    main()

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



sum_s = []
cnt = 0
flag = int(s[0])
if flag == 0:
    sum_s.append((1, 0))
flag = int(s[0])
for i in range(n):
    if s[i] == '0':
        if flag == 0:
            cnt += 1
        else:
            sum_s.append((flag, cnt))
            flag, cnt = 0, 1
    else:
        if flag == 0:
            sum_s.append((flag, cnt))
            flag, cnt = 1, 1
        else:
            cnt += 1
sum_s.append((flag, cnt))



rs = [0] * (len(sum_s)+1)
for i in range(1, len(sum_s)+1):
    rs[i] = rs[i-1] + sum_s[i-1][1]

ans = 0


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

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

ans = 0

check =[[0]]
i = 0
while i < n:
    if l[i] == "1":
        a = i
        while i < n and l[i] == "1":
            i += 1
        check.append([a,i-1])
    i += 1
check.append([n-1])
if len(check)-k < 1:
    print(n)
    exit()
for i in range(len(check)-k):
       
    ans = max(ans,check[i+k][-1]-check[i][0]+1)
print(ans)

Solution (Python):
N, K = map(int, input().split())
S = input()
cnt = 1
counts = []

if S[0]=='0':
  counts.append(0)
if N == 1:
  counts.append(1)
  
for i in range(1,N):
  if S[i-1]!=S[i]:
    counts.append(cnt)
    cnt = 1
  else:
    cnt += 1
  
  if i == N-1:
    counts.append(cnt)


K = min(K,len(counts)//2)
for i in range(len(counts)//2-K+1):
  if i == 0:
    s = sum(counts[:2*K+1])
    m = s
  else:
    s += sum(counts[2*(i+K)-1:2*(i+K)+1])-sum(counts[2*i-2:2*i])
    m = max(m,s)
  
print(m)

Solution (Python):
import bisect




import itertools



import sys



ipti = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)

def main():
    n, k = list(map(int,ipti().split()))
    s = input()

    lg = [0] * (n+3)

    s_first = s[0]
    s_last =s[-1]

    idx = 0

    if s_first == "0":
        lg[0] = 0
        idx = 1

    s_groupby = itertools.groupby(s)

    for _, group in s_groupby:
        lg[idx] = len(list(group))
        idx += 1

    if s_last == "0":
        idx += 1

    rn = 2*k + 1

    if rn >= idx:
        print(n)
        sys.exit()

    temp = sum(lg[0:rn])
    ans = temp

    for i in range(2, idx-rn+1, 2):
        temp -= (lg[i-2] + lg[i-1])
        temp += (lg[i-2+rn] + lg[i-1+rn])
        ans = max(ans, temp)

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 7)

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

scomp = []

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


lens = len(scomp)
ans = 0

start = 0
if s[0] == '0':
    start = 1
    end = min(k*2,lens)
    tmp = sum(scomp[:end])
    ans = max(ans,tmp)

end = min(start + k*2+1,lens)
tmp = sum(scomp[start:end])
ans = max(ans,tmp)
for i in range(start,lens-k*2-2,2):
    tmp -= scomp[i]
    tmp -= scomp[i+1]
    tmp += scomp[i+k*2+1]
    tmp += scomp[i+k*2+2]
    ans = max(ans,tmp)

if s[-1] == '0':
    start = max(lens-k*2,0)
    tmp = sum(scomp[start:])
    ans = max(ans,tmp)

print(ans)

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


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))

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

print(ans)

Solution (Python):
n, k = map(int, input().split())
s = input()
r = [0, 1]
prev = s[0]

for i in range(1, len(s)):
    if prev == s[i]:
        r[-1] += 1
    else:
        r.append(r[-1] + 1)
    prev = s[i]

lr = len(r)
import math

if s[0] == '0' and math.ceil((lr-1) / 2) <= k:
    print(len(s))
    exit()
if s[0] == '1' and math.floor((lr-1) / 2) <= k:
    print(len(s))
    exit()

start = 0
if s[0] == '0':
    start = 1

ans = 0
for i in range(start, lr - (2 * k + 1), 2):
    ans = max(ans, r[i + (2 * k + 1)] - r[i])
    



if s[0] == '0':
    ans = max(ans, r[2 * k] - r[0])
    

if s[-1] == '0':
    ans = max(ans, r[-1] - r[lr - 2 * k - 1])
    

print(ans)

Solution (Python):
N,K = map(int,input().split())
S = input()
l = [1]
for i in range(N-1):
    if S[i] == S[i+1]:
        l[-1] += 1
    else:
        l.append(1)
if S[0] == "0":
    l = [0] + l
if S[-1] == "0":
    l += [0]
s = [0]
for i in range(len(l)):
    s.append(s[-1]+l[i])
if len(l) <= 2*K + 1:
    print(s[-1])
else:
    cnt = 0
    for i in range((len(l)-2*K-1)//2+1):
        cnt = max(cnt,s[2*K+2*i+1]-s[2*i])
    print(cnt)

Solution (Python):
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=1000000007
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()


def summarize_list(sl):

  a=sl[0]
  c=1
  res=[]

  for x in sl[1:]:
    if x==a:
      c+=1
    else:
      res.append(c)
      a=x
      c=1
  res.append(c)

  return res


def main():
  n,k=LI()
  s=list(S())

  l=summarize_list(s)
  

  
  if s[0]=='0':
    l=[0]+l

  
  if s[-1]=='0':
    l+=[0]

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

  l=[0]+l

  

  ans=0
  left=0
  while True:
    right=min(len(l)-1,left+2*k+1)
    if left>=len(l):
      return ans

    
    ans=max(ans,l[right]-l[left])

    left+=2


print(main())

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

if s[0]=='0':
    seq=[0]
else:
    seq=[]

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

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

bleft=0
bright=min(2*k,len(seq)-1)

cursum=sum(seq[bleft:bright+1])
ret=cursum

for i in range(2,len(seq),2):
    left=i
    right=min(i+2*k,len(seq)-1)

    if bleft!=left:
        cursum-=seq[bleft]+seq[bleft+1]
        bleft=left
    if bright!=right:
        cursum+=seq[right-1]+seq[right]
        bright=right
    
    ret=max(ret,cursum)
    
print(ret)

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

prev = S[0]
renzoku_start = [0]
for i in range(1, N):
  if prev == S[i]:
    continue
  else:
    prev = S[i]
    renzoku_start.append(i)
renzoku_start.append(N)

ans = []
l = len(renzoku_start)
for j in range(l):
  if S[renzoku_start[j]] == 0:
    if j + 2 * K >= l:
      ans.append(N - renzoku_start[j])
      break
    ans.append(renzoku_start[j + 2 * K] - renzoku_start[j])
  else:
    if j + 2 * K + 1 >= l:
      ans.append(N - renzoku_start[j])
      break
    ans.append(renzoku_start[j + 2 * K + 1] - renzoku_start[j])

if ans:
  print(max(ans))
else:
  print(N)

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

count = 1
Nums_count = []
now = s[0]
start = 0
for i in range(1,n):
    if s[i] == str(now):
        count += 1
    else:
        Nums_count.append([start, count])
        now = s[i]
        start = i
        count = 1
    if i == n-1:
        Nums_count.append([start, count])
if len(s) == 1:
    Nums_count.append([0,1])

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


ans = 0
m = len(Nums_count_rui)

if s[0] == '0' and m <= 2*k:
    ans = n
elif s[0] == '1' and m <= 2*k+1:
    ans = n

for i in range(1,m):
    if s[Nums_count_rui[i][0]] == '0':
        if i+2*k-1 >= m:
            tot = Nums_count_rui[-1][1] - Nums_count_rui[i-1][1]
        else:
            tot = Nums_count_rui[i+2*k-1][1] - Nums_count_rui[i-1][1]
    else:
        if i+2*k >= m:
            tot = Nums_count_rui[-1][1] - Nums_count_rui[i-1][1]
        else:
            tot = Nums_count_rui[i+2*k][1] - Nums_count_rui[i-1][1]
    ans = max(tot, ans)
print(ans)

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

change = [0]
for i, (e1, e2) in enumerate(zip(s, s[1:])):
	e1 = int(e1)
	e2 = int(e2)
	if e1 ^ e2:
		change.append(i + 1)

change.append(n)
end = len(change) - 1

ans = 0
for i, l in enumerate(change[:-1]):
	if s[l] == "1":
		ri = min(i + 2 * k + 1, end)
	else:
		ri = min(i + 2 * k, end)

	r = change[ri]

	ans = max(ans, r - l)

print(ans)

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

from itertools import groupby

N, K = [int(x) for x in input().split()]
S = list(input().rstrip())

S_groupby = groupby(S)

zero_idx = [] 
value_list = [] 
i = -1
for j in S_groupby:
    i += 1
    key, value = j[0], len(list(j[1]))
    value_list.append(value)
    if key == '0':
        zero_idx.append(i)

if len(zero_idx) <= K:
    print(len(S))
    sys.exit()

left = zero_idx[0]
right = zero_idx[K - 1]
if zero_idx[0] == 0:
    ans = [sum(value_list[left:right + 2])]
else:
    ans = [sum(value_list[left - 1:right + 2])]

for i in range(K, len(zero_idx)):
    new_right = zero_idx[i]
    new_left = zero_idx[i - K + 1]
    new_ans = ans[-1] + value_list[new_right] - value_list[left]
    if new_right + 1 < len(value_list):
        new_ans += value_list[new_right + 1]
    if left - 1 >= 0:
        new_ans -= value_list[left - 1]
    ans.append(new_ans)
    right, left = new_right, new_left

print(max(ans))

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

from itertools import groupby

N, K = [int(x) for x in input().split()]
S = list(input().rstrip())

S_groupby = groupby(S)

zero_idx = [] 
value_list = [] 
i = -1
for j in S_groupby:
    i += 1
    key, value = j[0], len(list(j[1]))
    value_list.append(value)
    if key == '0':
        zero_idx.append(i)

if len(zero_idx) <= K:
    print(len(S))
    sys.exit()

left = zero_idx[0]
right = zero_idx[K - 1]
if zero_idx[0] == 0:
    ans = [sum(value_list[left:right + 2])]
else:
    ans = [sum(value_list[left - 1:right + 2])]

for i in range(K, len(zero_idx)):
    new_right = zero_idx[i]
    new_left = zero_idx[i - K + 1]
    new_ans = ans[-1] + value_list[new_right] - value_list[left]
    if new_right + 1 < len(value_list):
        new_ans += value_list[new_right + 1]
    if left - 1 >= 0:
        new_ans -= value_list[left - 1]
    ans.append(new_ans)
    right, left = new_right, new_left

print(max(ans))

Solution (Python):
import sys
from itertools import accumulate
from bisect import bisect_left,bisect_right,insort


def main():
    n,k=map(int,input().split())
    s=list(input())
    flg,cnt,chk='1',0,[0]    
    for i in range(n):
        if s[i]==flg:
            chk[cnt]+=1
        else:
            flg=str((int(flg)+1)%2)
            cnt+=1
            chk.append(1)
    if s[-1]=='0':
        chk.append(0)
    lng=len(chk)
    if k*2>lng:
        print(n)
    else:
        ans=0
        acc=[0]+list(accumulate(chk))
        for i in range(0,len(chk)-2*k,2):
            ans=max(ans,acc[i+2*k+1]-acc[i])
        print(ans)

if __name__ == '__main__':
    main()