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


def solve(N: int, K: int, S: str):
    ranlength = []
    lastBit = ""
    lastLength = 0
    for c in S:
        if lastBit == "":
            lastBit = c
            lastLength += 1
            continue
        if lastBit == c:
            lastLength += 1
            continue
        else:
            ranlength.append((lastLength, int(lastBit)))
            lastLength = 1
            lastBit = c
            continue
    ranlength.append((lastLength, int(lastBit)))
    count0 = 0
    length0 = 0
    end = 0
    for i in range(len(ranlength)):
        if count0 == K and ranlength[i][1] == 0:
            break
        if ranlength[i][1] == 0:
            count0 += 1
        length0 += ranlength[i][0]
        end = i
    
    nextLength = length0
    lastMax = length0
    begin = 0
    
    while end + 1 < len(ranlength):
        nextLength += ranlength[end + 1][0]
        end += 1
        for i in range(end + 1, len(ranlength)):
            if ranlength[i][1] == 0:
                break
            else:
                nextLength += ranlength[i][0]
            end = i
        for i in range(begin, len(ranlength)):
            begin = i + 1
            nextLength -= ranlength[i][0]
            if ranlength[i][1] == 0:
                break
        lastMax = max(lastMax, nextLength)
        
    
    print(lastMax)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    K = int(next(tokens))  
    S = next(tokens)  
    
    solve(N, K, S)


if __name__ == '__main__':
    main()

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

arr = []
cnt = 0
comp = S[0]
for s in S:
    if s==comp:
        cnt += 1
    else:
        arr.append(cnt)
        cnt = 1
        comp = s
arr.append(cnt)

if len(arr)<3:
    print(sum(arr))
else:
    if S[0]=='1':
        L = (len(arr)+1)//2 - K +1
        l = 0
        tmp = sum(arr[2*l:2*(l+K)+1])
        ans = tmp
        for l in range(1, L):
            tmp = tmp - (arr[2*(l-1)]+arr[2*l-1]) + sum(arr[2*(l-1+K)+1 : min(2*(l+K)+1, len(arr))])
            ans = max(tmp, ans)
    else:
        L = (len(arr)+1)//2 - K + 1
        l = 0
        tmp = sum(arr[2*l:2*(l+K)])
        ans = tmp
        for l in range(1, L):
            tmp = tmp - sum(arr[max(0,2*l-3) : 2*l-1]) + sum(arr[2*(l+K-1): min(2*(l+K), len(arr))])
            ans = max(tmp, ans)
            
    print(ans)

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


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

    
    
    tank = []
    cnt = 1
    for i in range(1,n):
        if s[i] != s[i-1]:
            tank.append(cnt)
            cnt = 0
        cnt += 1
        if i == n-1:
            tank.append(cnt)
    
    p = len(tank)

    
    if s[0] == '0' and (p+1)//2 <= k:
        print(n)
        exit()
    if p//2 <= k and s[0] == '1':
        print(n)
        exit()

    
    ans = 0
    
    res = 0
    if s[0] == '0':
        for j in range(2*k):
            res += tank[j]
        ans = res
        for j in range(2*k,p):
            if j == 2*k:
                res -= tank[0]
            elif j%2 == 0:
                res= res - tank[j-2*k] -tank[j-2*k-1]
            res += tank[j]
            ans = max(ans,res)

    else:
        for j in range(2*k+1):
            res += tank[j]
        ans = res
        for j in range(2*k+1,p):
            if j % 2 == 1:
                res = res- tank[j-2*k-1]-tank[j-2*k]
            res += tank[j]
            ans = max(ans,res)

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)


def read_int():
    return int(input())


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, K, S):
    s1 = list(map(lambda x: len(x), filter(lambda x: x != '', S.split('0'))))
    s0 = list(map(lambda x: len(x), filter(lambda x: x != '', S.split('1'))))
    if len(s0) <= K:
        return N
    if S[0] == '0':
        s1.insert(0, 0)
    if S[-1] == '0':
        s1.append(0)
    s = []
    i = 0
    for i in range(len(s0)):
        s.append(s1[i])
        s.append(s0[i])
    s.extend(s1[i+1:])

    sa = [0]
    for v in s:
        sa.append(sa[-1]+v)
        
    ans = 0
    for i in range(0, len(sa), 2):
        if i+2*K+1 < len(sa):
            ans = max(ans, sa[i+2*K+1]-sa[i])
        elif i+2*K < len(sa):
            ans = max(ans, sa[i+2*K]-sa[i])
        else:
            break
      

    return ans


def main():
    N, K = read_int_n()
    S = read_str()
    rS = ''.join(['0' if c == '1' else '1' for c in S])
    print(max(slv(N, K, S), slv(N, K-1, rS)))

    
    
    
    
    

if __name__ == '__main__':
    main()

Solution (Python):
N, K = map(int, input().split())
S = input()
 
l = [[], []]
ans = []
k = [int(s) for s in S]
if k[0] == 0:
    l[1].append(0)
v0 = 1 if k[0] == 0 else 0
v1 = 1 if k[0] == 1 else 0
for i in range(1, N):
    if (k[i]==0) & (k[i-1]==0):
        v0 += 1
    elif (k[i]==1) & (k[i-1]==0):
        l[0].append(v0)
        v0 = 0
        v1 += 1
    elif (k[i]==0) & (k[i-1]==1):
        l[1].append(v1)
        v0 += 1
        v1 = 0 
    else:
        v1 += 1
        
for i, v in enumerate([v0, v1]):
    if v > 0:
        l[i].append(v)
        
if k[0] == 0:
    l[1].append(0)
    
l[0] += [0]*(K+1)
l[1] += [0]*(K+1)
a = sum(l[0][:K]) + sum(l[1][:K+1])
ans.append(a)
for i in range(1, len(l[0])-K-1):
    a -= l[0][i-1] + l[1][i-1]
    a += l[0][i+K-1] + l[1][i+K]
    ans.append(a)
print(max(ans))

Solution (Python):
from itertools import groupby
N, K = map(int,input().split())
S = '1' + input() + '1'
acc = [0]
for i, (k, v) in enumerate(groupby(S)):
    acc.append(acc[i] + len(list(v)))
sz = len(acc)
l = [acc[min(sz - 1, i + 2 * K + 1)] - acc[i] - (i == 0) - (i + 2 * K + 1 >= sz - 1) for i in range(sz)]
print(max(l[::2]))

Solution (Python):
from sys import stdin

N, K = [int(x) for x in stdin.readline().rstrip().split()]

S = [int(x) for x in stdin.readline().rstrip()] + [-1]


straight_stand_cnt = 0

if S[0] == 0:
    straight_stand_cnt = 1

l = 0
r = 0
ans = -999

while r < N:
    
    
    if straight_stand_cnt <= K:
        if S[r] == 1 and S[r+1] == 0:
            straight_stand_cnt += 1

        ans = max(ans, r-l+1)
        r += 1
    else:
        if S[l] == 0 and S[l+1] == 1:
            straight_stand_cnt -= 1
        l += 1      


print(ans)

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

nums = []
count = 0
prev = '1'
for c in list(s):
    if prev != c:
        nums.append(count)
        count = 1
    else:
        count += 1
    prev = c
nums.append(count)
if prev == '0':
    nums.append(0)

cml = [0]
for i in range(len(nums)):
    cml.append(cml[-1] + nums[i])

ans = 0
lencml = len(cml)
for l in range(0,lencml,2):
    r = l + (2 * k + 1)
    if r >= lencml:
        r = lencml - 1
    tmp = cml[r] - cml[l]
    if ans < tmp:
        ans = tmp

print(ans)

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

S=S+'E'
ans=-1

if(S[0]=='0'):
    stand=1
else:
    stand=0

L=0
R=0

while(R<N):
    if(stand<=K):
        if(S[R]=='1' and S[R+1]=='0'):
            stand+=1
        
        ans=max(ans,R-L+1)
        R+=1
    elif(L==R):
        L+=1
        R+=1
    else:
        if(S[L]=='0' and S[L+1]=='1'):
            stand-=1
        L+=1
        
print(ans)

Solution (Python):






n, k = [int(s) for s in input().split()]
S = input()

node = [len(s) for s in S.split('0')]
node = [s for i, s in enumerate(node) if not (s==0 and i!=0 and i!=len(node)-1)]
arch = [len(s) for s in S.split('1')]
arch = list(filter(int, arch))
from itertools import chain
flatten = chain.from_iterable
xs = list(flatten(zip(node, arch))) + node[-1:]
l = len(xs)
sumlist = [None]*(l+2)
sumlist[-1] = 0
for i in range(l):
    sumlist[i] = sumlist[i-1] + xs[i]
def get_sum(f, n):
    f = min(f, l)
    t = f+n
    t = min(t, l)
    return sumlist[t-1] - sumlist[f-1]
def get_ans():
    for i in range(l//2+1):
        yield get_sum(i*2, k*2+1)
ans = max(get_ans())







print(ans)

Solution (Python):
from bisect import bisect_right
N, K = map(int, input().split())
s = input()

arr = [0] * N
cnt = 0
Flag = False
for i, char in enumerate(s):
    if not Flag and char == '0':
        Flag = True
        cnt += 1
    if char == '1':
        Flag = False
    arr[i] = cnt
ans = 0
for i, val in enumerate(s):
    if val == '0':
        ans = max(ans, bisect_right(arr,arr[i]+K-1)-i)
    else:
        ans = max(ans, bisect_right(arr,arr[i]+K)-i)
print(ans)

Solution (Python):
import math
N, K = list(map(int, input().split()))
S = str(input())
list = []
cnt = 1
list.append(1)
for i in range(len(S)):
  if(i + 1) >= len(S): 
    break
  if (S[i+1] != S[i]):
    list.append(i + 2)


ans = 0
tmp = 0

if (S[0] == '1'):
  for i in range(len(list) // 2):
    if ((2 * i) + (2 * K) + 1) >= len(list):
      tmp = len(S) - list[(2 * i)] + 1
      if ans < tmp:
        ans = tmp
      break
    tmp = list[(2 * i) + (2 * K) + 1] - list[(2 * i)]
    if ans < tmp:
      ans = tmp
  
  if(len(list) // 2) == 0: ans = len(S)

if (S[0] == '0'):
  for i in range(math.ceil(len(list) / 2)):
    
    if ((2 * i) - 1) >= 0:
      if ((2 * i) + (2 * K) + 1) > len(list):
        tmp = len(S) - list[(2 * i) - 1] + 1
        if ans < tmp:
          ans = tmp
        break
      tmp = list[(2 * K) + (2 * i)] - list[(2 * i) - 1]
      if ans < tmp:
        ans = tmp
    
    else:
      if ((2 * i) + (2 * K) + 1) > len(list):
        tmp = len(S)
        if ans < tmp:
          ans = tmp
        break
      tmp = list[(2 * K)] - 1
      if ans < tmp:
        ans = tmp
  
  if(len(list) // 2) == 0: ans = len(S)
print(ans)

Solution (Python):
n, k = map(int,input().split())
s = input() + '1'
L = []

i = 0
d = []
while i <= n:
    if s[i] == '0':
        d.append(i)

    else:
        if d != []:
            L.append(d)
            d = []
    i += 1

ans = 0
nl = len(L)

if nl <= k:
    print(n)
else:
    for i in range(nl - k + 1):

        if i == 0:
            sta = 0
        else:
            sta = L[i - 1][-1] + 1

        if i == nl - k:
            end = n - 1
        else:
            end = L[i + k][0] - 1

        ans = max(ans, end - sta + 1)

    print(ans)

Solution (Python):


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







start = 0
l = []
for i in range(1,N):
    if S[i-1] == S[i]:
        continue
    else:
        v = True if S[i-1] == "1" else False
        l.append((start, i, v))
        start = i
v = True if S[-1] == "1" else False
l.append((start, N, v))

mx = 0
L = len(l)
for i, val in enumerate(l):
    start, end, v = val
    if v is True:   
        
        idx = min(i+2*K, L-1)
    else:   
        
        idx = min(i+2*K-1, L-1)
    ln = l[idx][1] - start
    if ln > mx:
        mx = ln

print(mx)

Solution (Python):


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

num_list = []
now = 1
count = 0

for char in S:
    if char == (0 + now):
        count += 1
    else:
        num_list.append(count)
        now = 1 - now
        count = 1
if count > 0:
    num_list.append(count)

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

ans = 0
add = 2 * K + 1

left = 0
right = 0
tmp = 0 
for i in range(0, len(num_list), 2):
    next_left = i
    next_right = min(i + add, len(num_list))

    while left < next_left:
        tmp -= num_list[left]
        left += 1
    while right < next_right:
        tmp += num_list[right]
        right += 1
    ans = max(ans, tmp)
print(ans)

Solution (Python):


N, K = [int(_) for _ in input().split()]
S = str(input())

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
































        



add = 2 * K + 1
sums = [0 for _ in range(len(Nums) + 1)]
for i in range(len(Nums)):
    sums[i+1] = sums[i] + Nums[i]


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

Solution (Python):
N, K = map(int, input().split())
S = input()
Nums = []
now = 1 
cnt = 0 
for i in range(N):
    if S[i] == str(now):
        cnt += 1
    else:
        Nums.append(cnt)
        now = 1 - now   
        cnt = 1
if cnt != 0:
    Nums.append(cnt)


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

Add = 2 * K + 1



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

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

print(ans)

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

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

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

ans = 0
for i in range(len(nums))[::2]:
    left = i
    right = min(i+k*2+1, len(nums))
    tmp = summ[right] - summ[left]

    ans = max(tmp, ans)


print(ans)

Solution (Python):


N, K = [int(_) for _ in input().split()]
S = str(input())

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
















add = 2 * K + 1
ans = 0
left = 0
right = 0
tmp = 0
for i in range(0, len(Nums), 2):
    nextLeft = i 
    nextRight = min(i + add, len(Nums))
    while nextLeft > left:
        tmp -= Nums[left]
        left += 1
    while nextRight > right:
        tmp += Nums[right]
        right += 1
    
    ans = max(tmp, ans)
        
print(ans)

Solution (Python):
N, K = map(int, input().split())
S = input()
Nums = []
now = 1 
cnt = 0 
for i in range(N):
    if S[i] == str(now):
        cnt += 1
    else:
        Nums.append(cnt)
        now = 1 - now   
        cnt = 1
if cnt != 0:
    Nums.append(cnt)


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

Add = 2 * K + 1
ans = 0

left = 0
right = 0
tmp = 0 

for i in range(0, len(Nums), 2):
    
    Nextleft = i
    Nextright = min(i + Add, len(Nums))
    
    while Nextleft > left:
        tmp -= Nums[left]
        left += 1
    
    while Nextright > right:
        tmp += Nums[right]
        right += 1
    ans = max(tmp, ans)

print(ans)

Solution (Python):

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

num_list = []
now = 1
count = 0

for char in S:
    if char == (0 + now):
        count += 1
    else:
        num_list.append(count)
        now = 1 - now
        count = 1
if count > 0:
    num_list.append(count)

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


cumulative = [0] * (len(num_list) + 1)
for i in range(len(num_list)):
    cumulative[i + 1] = cumulative[i] + num_list[i]

ans = 0
add = 2 * K + 1

for i in range(len(cumulative)):
    if i % 2 == 0:
        add_i = min(i + add, len(cumulative) - 1)
        ans = max(ans, cumulative[add_i] - cumulative[i])
print(ans)

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

DP = [0]
ans = 0
l = 0

for index,x in enumerate(S):
    if(index==0):
        if(x=='0'):
            DP.append(0)
            DP.append(1)
        else:
            DP.append(1)
    else:
        if(S[index-1]!=x):
            DP.append(DP[-1]+1)
        else:
            DP[-1]+=1
    if(index==N-1):
        if(x=='1'):
            DP.append(DP[-1])
        DP.append(DP[-1])

l=0
i = min(int((len(DP))/2)-1,K)
while(1+2*i+l<len(DP)):
    ans = max(DP[2*i+1+l]-DP[l],ans)
    l+=2

if(not '0' in S):
    ans = len(S)
elif(not '1' in S):
    ans = len(S)

print(ans)

Solution (Python):
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
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 LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def _I(): return int(sys.stdin.readline())
def _F(): return float(sys.stdin.readline())
def pf(s): return print(s, flush=True)

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

positions = []
idx = 0
keep = None
while idx < N:
    if S[idx] == '0':
        if keep == None:
            keep = idx
    else:
        
        if keep != None:
            positions.append([keep, idx])
            keep = None
        else:
            
            keep = None
    idx += 1
else:
    if keep != None:
        positions.append([keep, keep+1])

if len(positions) == 0:
    print(N)
    exit()
elif len(positions) <= K:
    print(N)
    exit()

ans = 0

for idx in range(len(positions)):
    if idx + K - 1 > len(positions) - 1:
        break
    if idx == 0:
        left = 0
    else:
        left = positions[idx-1][1]

    if idx + K - 1 == len(positions)-1:
        end = N
    else:
        end = positions[idx+K-1+1][0]
    
    ans = max(ans, end - left)
print(ans)

Solution (Python):
n,k=map(int,input().split())
s=list(input())
s=list(map(int,s))
stack=[]
tmp=0
now=1
for i in range(n):
  if s[i]==now:
    tmp+=1
  else:
    stack.append(tmp)
    tmp=1
    now=1-now
stack.append(tmp)
if len(stack)%2==0:
  stack.append(0)
Acc=[0]*(len(stack)+1)
for i in range(len(stack)):
  Acc[i+1]=Acc[i]+stack[i]
ans=0
for i in range(0,len(stack),2):
  left = i
  right = min(i+k*2+1,len(stack))
  ans=max(ans,Acc[right]-Acc[left])
print(ans)

Solution (Python):
from itertools import accumulate

N, K = map(int, input().split())
S = input() + "2"
T = []
i = 0
for j in range(1, len(S)):
    if S[i] != S[j]:
        T.append(j - i)
        i = j
U = [0] + list(accumulate(T))
ans = 0
for i in range(len(T)):
    if (i + int(S[0])) % 2 == 0:
        j = i + 2 * (K - 1) + 1
    else:
        j = i + 2 * K
    j = min(j, len(T) - 1)
    ans = max(ans, U[j + 1] - U[i])
print(ans)

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

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

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

print(ans)

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

if S.count("0") == 0:
	print(N)
	exit(0)


curLen=0

rangeVals=[]
stackLens=[]
prev = S[0]
for n in range(N):
	if prev != S[n]:
		
		stackLens.append(curLen)
		rangeVals.append(prev)
		
	
	
	curLen+=1
	prev = S[n]

stackLens.append(curLen)
rangeVals.append(prev)

stackLens.insert(0,0)
rangeVals.insert(0,rangeVals[0])






curSumLenMax=0
for r in range(1,len(stackLens)):
	rangeLen=0
	if rangeVals[r] == "0":
		high = 2*K+r-1
		if len(stackLens)-1 < high:
			high = len(stackLens)-1
		rangeLen=stackLens[high]-stackLens[r-1]
	else:
		high = 2*K+1+r-1
		if len(stackLens)-1 < high:
			high = len(stackLens)-1
		
		rangeLen=stackLens[high]-stackLens[r-1]
	curSumLenMax = max( curSumLenMax, rangeLen )


print(curSumLenMax)

Solution (Python):
def main():
    n, k = map(int, input().split())
    s = input()
    
    arr = []
    if s[0] == '0':
        arr.append(0)
    cnt = 1
    i = 1
    while i != n:
        if s[i] != s[i - 1]:
            arr.append(cnt)
            cnt = 1
        else:
            cnt += 1
        i += 1
    arr.append(cnt)
    if s[-1] == '0':
        arr.append(0)
    
    ans = 0
    curr = arr[0]
    j = 1
    if len(arr) - 1 <= 2 * k:
        print(n)
        return 0
    for i in range(1, len(arr), 2):
        curr += arr[i] + arr[i + 1]
        if i - j == 2 * k:
            curr -= arr[j] + arr[j - 1]
            j += 2
        ans = max(ans, curr)
    print(ans)
    return 0
main()

Solution (Python):
def main():
    ans = 0
    n, k = map(int, input().split())
    s = input()
    arr = []
    if s[0] == '0':
        arr.append(0)
    cnt = 1
    i = 1
    while i != n:
        if s[i] != s[i - 1]:
            arr.append(cnt)
            cnt = 1
        else:
            cnt += 1
        i += 1
    arr.append(cnt)
    if s[-1] == '0':
        arr.append(0)
    ans = 0
    curr = arr[0]
    j = 1
    if len(arr) - 1 <= 2 * k:
        print(n)
        return 0
    for i in range(1, len(arr), 2):
        curr += arr[i] + arr[i + 1]
        if i - j == 2 * k:
            curr -= arr[j] + arr[j - 1]
            j += 2
        ans = max(ans, curr)
    print(ans)
    return 0
main()

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

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

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

add=2*K+1
ans=0
for i in range(0,n+1,2):
	left=i
	right=min(i+add,n)
	ans=max(ans,R[right]-R[left])
print(ans)

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

cnt = 0
now = 1
Nums = []

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

if cnt != 0:
    Nums.append(cnt)
if len(Nums) % 2 == 0:
    Nums.append(0)

add = 2*K+1
ans = 0
left = right = 0
tmp = 0

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

print(ans)

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


S = S + 'E'
ans = -1


if (S[0] == '0'):
    stand = 1
else:
    stand = 0

L = 0
R = 0


while(R < N):
    if (stand <= K):
        
        if (S[R] == '1' and S[R+1] == '0'):
            stand+=1
        ans = max(ans, R - L + 1)
        R += 1
    elif L == R:
        L += 1
        R += 1
    else:
        if (S[L] == '0' and S[L+1] == '1'):
            stand -= 1
        L += 1

print(ans)

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


S = S + 'E'
ans = -1


if (S[0] == '0'):
    stand = 1
else:
    stand = 0

L = 0
R = 0


while(R < N):
    if (stand <= K):
        
        if (S[R] == '1' and S[R+1] == '0'):
            stand+=1
        ans = max(ans, R - L + 1)
        R += 1
    elif L == R:
        L += 1
        R += 1
    else:
        if (S[L] == '0' and S[L+1] == '1'):
            stand -= 1
        L += 1

print(ans)

Solution (Python):
from itertools import accumulate

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

S = input()
n = list(map(int, str(S)))
count = 1
mas = []
for i in range(N-1):
    if n[i] == n[i + 1]:
        count += 1
        if i == N-2:
            mas.append(count)
    else:
        mas.append(count)
        count = 1
        if i == N-2:
            mas.append(1)
        

mas2 = []
if n[0] == 0:
    mas.insert(0, 0)
    
if n[N-1] == 0:
    mas.append(0)
    
l=len(mas)
mas2=[0] + list(accumulate(mas))


    
if K >= (l-1)/2:
    print(N)
else:
    mas3 = []
    k = int(((l-1)/2)-K+1)
    for i in range(k):
        mas3.append(mas2[K*2 + 2*i+1]-mas2[0 + 2*i])
    print(max(mas3))

Solution (Python):
from itertools import accumulate
N, K = map(int, input().split())
S = "1" + input() + "1"
arr = []
cnt = 0
flg = 1
for s in S:
    if s == "1" and 1 - flg:
        flg = 1
        arr.append(cnt)
        cnt = 1
    elif s == "0" and flg:
        flg = 0
        arr.append(cnt)
        cnt = 1
    else:
        cnt += 1
arr.append(cnt)
csum = [0] + list(accumulate(arr))
ans = 0
tmp = 0
for i in range(0, len(csum), 2):
    l = i
    r = min(i + 2*K + 1, len(csum) - 1)
    tmp = csum[r] - csum[l] - (r == len(csum) - 1) - (l == 0) - (l == 1)
    ans = max(ans, tmp)
if len(csum) == 2:
    print(csum[1] - 2)
else:
    print(ans)

Solution (Python):
N, K = map(int, input().split())
S = "1" + input() + "1"
arr = []
cnt = 0
flg = 1
for s in S:
    if s == "1" and 1 - flg:
        flg = 1
        arr.append(cnt)
        cnt = 1
    elif s == "0" and flg:
        flg = 0
        arr.append(cnt)
        cnt = 1
    else:
        cnt += 1
arr.append(cnt)
csum = [0] * (len(arr)+1)
for i in range(len(arr)):
    csum[i + 1] = csum[i] + arr[i]
ans = 0
tmp = 0
for i in range(0, len(csum), 2):
    l = i
    r = min(i + 2*K + 1, len(csum) - 1)
    tmp = csum[r] - csum[l] - (r == len(csum) - 1) - (l == 0) - (l == 1)
    ans = max(ans, tmp)
if len(csum) == 2:
    print(csum[1] - 2)
else:
    print(ans)

Solution (Python):
N, K = map(int, input().split())
S = "1" + input() + "1"
arr = []
cnt = 0
flg = 1
for s in S:
    if s == "1" and 1 - flg:
        flg = 1
        arr.append(cnt)
        cnt = 1
    elif s == "0" and flg:
        flg = 0
        arr.append(cnt)
        cnt = 1
    else:
        cnt += 1
arr.append(cnt)
csum = [0] * (len(arr)+1)
for i in range(len(arr)):
    csum[i + 1] = csum[i] + arr[i]
ans = 0
tmp = 0
for i in range(0, len(csum), 2):
    l = i
    r = min(i + 2*K + 1, len(csum) - 1)
    tmp = csum[r] - csum[l] - (r == len(csum) - 1) - (l == 0) - (l == 1)
    ans = max(ans, tmp)
if len(csum) == 2:
    print(csum[1] - 2)
else:
    print(ans)

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


ans=1
l=0
r=0
edge=[]



if S[l]=='0':
    count=1
else:
    count=0    


while(r < N-1):
    r+=1
    
    if ( S[r-1]=='1') & (S[r]=='0'):
        count+=1

    
    while(count>K):
        l+=1
        
        if (S[l-1]=='0') & (S[l]=='1'):
            count-=1
        
        if l==r:
            r+=1
        

    
    ans=max(ans,r-l+1)
    

print(ans)

Solution (Python):
N, K = map(int, input().split())
S = input()
s = [int(S[i]) for i in range(0,N)]


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


left = 0
right = 0
NN = len(ll)
max = 0
ln = 0

if (right < NN) :   
    if ll[right] > 0 :
        ln += ll[right]
        right += 1
for i in range(0,K) :
    if (right < NN) :    
        if ll[right] < 0 :
            ln -= ll[right]
            right += 1
    if (right < NN) :    
        if ll[right] > 0 :
            ln += ll[right]
            right += 1
    else :               
        break
    
max = ln
while (right < NN) :
    if (right < NN) :    
        if ll[right] < 0 :
            ln -= ll[right]
            right += 1
    if (right < NN) :    
        if ll[right] > 0 :
            ln += ll[right]
            right += 1
    else :                
        right = right  
    
    if (left < NN) :    
        if ll[left] > 0 :
            ln -= ll[left]
            left += 1
    if (left < NN) :    
        if ll[left] < 0 :
            ln += ll[left]
            left += 1
    if max < ln :
        max = ln

print (max)

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

num101 = []
if("0" == S[1]):
    num101 = [0]
for i in range(1, N + 1):
    if(S[i - 1] != S[i]):
        st = i
    if(S[i] != S[i + 1]):
        num101.append(i - st + 1)
if("0" == S[-2]):
    num101.append(0)


sum101 = [0]
for i in range(len(num101)):
    sum101.append(sum101[-1] + num101[i])


maxsum = 0
for i in range(0, len(sum101), 2):
    left = i
    right = min(i + 2 * K + 1, len(sum101) - 1)
    sumK = sum101[right] - sum101[left]
    maxsum = max(maxsum, sumK)
    if(right >= len(sum101) - 1):
        break

print(maxsum)

Solution (Python):

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


run_length_S = []
count = 1
for i in range(len(S)):
    try:
        if S[i] == S[i+1]:
            count += 1
        else:
            run_length_S.append([S[i], count])
            count = 1
    except:
        run_length_S.append([S[i], count])


right = 0
total = 0
max_total = 0

for left in range(len(run_length_S)):
    
    while right < len(run_length_S) and K >= 0:
        
        if run_length_S[right][0] == "1":
            total += int(run_length_S[right][1])
            right += 1
        
        elif K == 0 and run_length_S[right][0] == "0":
            break
        else:
            total += int(run_length_S[right][1])
            right += 1
            K -= 1
    
    
    max_total = max(total, max_total)
    
    
    if right == left:
        right += 1
    else:
        total -= int(run_length_S[left][1])
    
    
    if run_length_S[left][0] == "0":
        K += 1

print(max_total)

Solution (Python):
t,s=open(0)
n,k=map(int,t.split())
l=2*k+1
x=[0]+[i for i in range(n)if s[i]!=s[i-1]]+[n]*l
print(max(x[i+l]-x[i]for i in range(s>'1',len(x)-l,2)))