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 = input()
cnt01 = []
for i,s in enumerate(S):
    if i == 0:
        if s == "0":
            cnt01.append([1,0])
        cnt01.append([s,1])
    else:
        if cnt01[-1][0] == s:
            cnt01[-1][1] += 1
        else:
            cnt01.append([s,1])
if len(cnt01) <= 2*K+1:
    ans = sum([cnt[1] for cnt in cnt01])
    print(ans)
    exit()
else:
    ans = [sum([cnt[1] for cnt in cnt01[0:2*K+1]])]
    pos = 2
    while True:
        temp = ans[-1] - cnt01[pos-2][1] - cnt01[pos-1][1]
        if len(cnt01)-1 >= (pos + 2*K -1):
            temp += cnt01[pos+2*K-1][1]
        else:
            break
        if len(cnt01)-1 >= (pos + 2*K):
            temp += cnt01[pos+2*K][1]
        else:
            ans.append(temp)
            break
        ans.append(temp)
        pos += 2
print(max(ans))

Solution (Python):
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()

from collections import defaultdict, deque, Counter
from sys import exit
import heapq
import math
import copy
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)

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

flag = S[0]
sta = 1
lista = []





for i in range(1, N):
    if flag != S[i]:
        
        lista.append([flag, sta, i])
        flag = S[i]
        sta = i + 1
lista.append([flag, sta, N])
ans = 0
np = len(lista)
for i in range(np):
    if lista[i][0] == '0':
        index = min(i + 2 * K - 1, np - 1)
        opt = lista[index][2] - lista[i][1] + 1
        ans = max(ans, opt)
    else:
        index = min(i + 2 * K, np - 1)
        opt = lista[index][2] - lista[i][1] + 1
        ans = max(ans, opt)
print(ans)

Solution (Python):
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()

from collections import defaultdict, deque, Counter
from sys import exit
import heapq
import math
import copy
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)

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

flag = S[0]
sta = 1
lista = []
for i in range(1, N):
    if flag != S[i]:
        lista.append([flag, sta, i])
        flag = S[i]
        sta = i + 1
lista.append([flag, sta, N])
ans = 0
np = len(lista)
for i in range(np):
    if lista[i][0] == '0':
        index = min(i + 2 * K - 1, np - 1)
        opt = lista[index][2] - lista[i][1] + 1
        ans = max(ans, opt)
    else:
        index = min(i + 2 * K, np - 1)
        opt = lista[index][2] - lista[i][1] + 1
        ans = max(ans, opt)
print(ans)

Solution (Python):

import sys
try:
    from typing import List
except ImportError:
    pass


def solve(N: int, K: int, S: str):
    d = []  
    i = 0
    while True:
        r = 0
        while i < N and S[i] == "1":
            r += 1
            i += 1
        d.append(r)
        if i >= N:
            break
        s = 0
        while i < N and S[i] == "0":
            s += 1
            i += 1
        d.append(s)
    c = 2 * K + 1
    if c > len(d):
        print(sum(d))
        return
    dl = 0
    acc = sum(d[:c])
    ans = acc
    while dl + c + 2 <= len(d):
        acc += d[dl + c] + d[dl + c + 1] - d[dl] - d[dl + 1]
        ans = max(ans, acc)
        dl += 2
    print(ans)


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):
import sys

input = sys.stdin.readline

def main():
    N, K = map(int, input().split())
    S = list(input()[:-1])
    ans = 0; rtmp = 0
    cnt = K + (S[0] == '1')
    for l in range(N):
        if cnt > 0:
            for r in range(rtmp, N):
                if r == N-1:
                    break
                if S[r] > S[r+1]:
                    cnt -= 1
                if cnt <= 0:
                    break
        rtmp = r+1
        ans = max(rtmp - l, ans)
        if l == N-1:
            break
        if S[l] < S[l+1]:
            cnt += 1

    print(ans)
    
if __name__ == "__main__":
    main()

Solution (Python):


import sys










from itertools import groupby                











def main():
    mod = 1000000007                
    inf = float('inf')              
    
    sys.setrecursionlimit(10**6)    
    def input(): return sys.stdin.readline().rstrip()
    def ii():    return int(input())
    def mi():    return map(int, input().split())
    def mi_0():  return map(lambda x: int(x)-1, input().split())
    def lmi():   return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():    return list(input())
    
    
    n, k = mi()
    s = input()

    memo = [[], []]
    if s[0] == '0':
        memo[1].append(0)
    for key, g in groupby(s):
        memo[int(key)].append(len(list(g)))
    if s[-1] == '0':
        memo[1].append(0)

    accum_zero_memo = [0]
    for elm in memo[0]:
        accum_zero_memo.append(accum_zero_memo[-1] + elm)
    accum_one_memo = [0]
    for elm in memo[1]:
        accum_one_memo.append(accum_one_memo[-1] + elm)
    
    
    

    if k >= len(memo[0]):
        
        print(len(s))
    else:
        ans = 0
        for i in range(len(memo[0]) - k + 1):
            
            summation = (accum_zero_memo[i+k] - accum_zero_memo[i]) + (accum_one_memo[i+k+1] - accum_one_memo[i])
            ans = max(ans, summation)
        print(ans)


if __name__ == "__main__":
    main()

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

if "0" not in s:
  print(n)
  exit()
if s[0]=="0":
    d.append(0)
    f=False

seq=1
for i in range(1,n):
    if s[i]=="0" and not f:
        seq+=1
    elif s[i]=="0" and f:
        f=False
        d.append(seq)
        seq=1
    elif s[i]=="1" and f:
        seq+=1
    else:
        f=True
        d.append(seq)
        seq=1
    if i==n-1:
      d.append(seq)
if s[-1]=="0":
    d.append(0)
l=len(d)
res=[0]*(l+1)
res[0]=0
for i in range(1,l+1):
    res[i]=res[i-1]+d[i-1]
ans=1
for i in range(1,l+1):
    if i%2==1:
        continue
    if 2*k+i-1>l:
      a=res[l]-res[i-2]
    else:
      a=res[2*k+i-1]-res[i-2]
    if a>ans:
        ans=a
print(ans)

Solution (Python):

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

n,k=MI()
s=SI()
ze=[]
on=[]

if s[0]=="0":
    state="ze"
    sq=1
    on.append(0)
else:
    state="on"
    sq=1
    
for i in range(1,n):
    u=s[i]
    if state=="ze":
        if u=="0":
            sq+=1
        else:
            ze.append(sq)
            sq=1
            state="on"
            
        
    elif state=="on":
        if u=="1":
            sq+=1
        else:
            on.append(sq)
            sq=1
            state="ze"
if state=="on":
    on.append(sq)
elif state=="ze":
    ze.append(sq)
if len(ze)<k:
    print(n)
    sys.exit()
if s[-1]=="0":
    on.append(0)


u=len(on)

smz=[0 for i in range(u-k)]
smo=[0 for i in range(u-k)]
for i in range(k):
    smz[0]+=ze[i]
for i in range(k+1):
    smo[0]+=on[i]


for i in range(u-k-1):
    smz[i+1]=smz[i]-ze[i]+ze[i+k]
for i in range(u-k-1):
    smo[i+1]=smo[i]-on[i]+on[i+k+1]


mx=0
for i in range(u-k):
    val=smo[i]+smz[i]
    if val>mx:
        mx=val
print(mx)

Solution (Python):
n,k=map(int,input().split())
s=input()
from itertools import groupby
a=[len(list(j)) for i,j in groupby(s)]
if s[0]=='0':
  a=[0]+a
if s[n-1]=='0':
  a=a+[0]
w=2*k+1
if len(a)<=w:
  print(n)
  exit()
m=sum(a[:w])
x=m
for i in range(2,len(a)-2*k,2):
  m=m-a[i-1]-a[i-2]+a[i+w-1]+a[i+w-2]
  x=max(x,m)
print(x)

Solution (Python):
n,k=map(int,input().split())
s=input()
s+="6"
c=0
l=[]
zcnt=0
for i in range(n):
  if s[i]!=s[i+1]:
    if s[i]=="0":zcnt+=1
    l.append((s[i],i+1-c))
    c=i+1
L=len(l)
sl=[0]*(L+1)
for i in range(L):
    sl[i+1]=sl[i]+l[i][1]
if k>=zcnt:print(n);exit()
ans=0
if s[0]=="0":
    for i in range(zcnt-k+1):
        ans=max(ans,sl[min(L,2*k+i*2)]-sl[max(0,i*2-1)])
    print(ans)
else:
    for i in range(zcnt-k+1):
        ans=max(ans,sl[min(L,2*k+i*2+1)]-sl[i*2])
    print(ans)

Solution (Python):
from collections import deque
import numpy as np

N,K = (int(x) for x in input().split())
S = input()
count = []
d = deque(list(S))
now = d.popleft()
cum = 1
for i in range(len(S)-1):
    popped = d.popleft()
    if popped == now:
        cum += 1
    else:
        count.append(cum)
        now = popped
        cum = 1
count.append(cum)
cs = np.array(count).cumsum()
block = len(count)
if S[0] == '0':
    zeros = int(np.ceil(block/2))
    if K >= zeros:
        print(len(S))
    else:
        m = cs[K*2-1]
        if S[-1] == '1':
            for i in range(1,zeros-K+1):
                m = max(m,cs[2*i+2*K-1]-cs[2*(i-1)])
        else:
            for i in range(1,zeros-K):
                m = max(m,cs[2*i+2*K-1]-cs[2*(i-1)])
            m = max(m,cs[-1]-cs[-2*K-1])
        print(m)
else:
    zeros = int(np.floor(block/2))
    if K >= zeros:
        print(len(S))
    else:
        m = cs[2*K]
        if S[-1] == '1':
            for i in range(1,zeros-K+1):
                m = max(m,cs[2*i+2*K]-cs[2*i-1])
        else:
            for i in range(1,zeros-K):
                m = max(m,cs[2*i+2*K]-cs[2*i-1])
            m = max(m,cs[-1]-cs[-2*K-1])
        print(m)

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    n,k=map(int,input().split())
    S=input()
    lis=[0]
    for i in range(1,n):
        if S[i-1]!=S[i]:
            lis.append(i)
    lis.append(n)
    len_lis=len(lis)
    ans=[]
    for i in range(len_lis-1):
        if S[lis[i]]=='0':
            ans.append(lis[min(i+2*k,len_lis-1)]-lis[i])
        else:
            ans.append(lis[min(i+2*k+1,len_lis-1)]-lis[i])
    print(max(ans))

if __name__=='__main__':
    main()

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    n,k=map(int,input().split())
    S=input()
    lis=[0]
    for i in range(1,n):
        if S[i-1]!=S[i]:
            lis.append(i)
    lis.append(n)
    len_lis=len(lis)
    ans=[]
    for i in range(len_lis-1):
        if S[lis[i]]=='0':
            ans.append(lis[min(i+2*k,len_lis-1)]-lis[i])
        else:
            ans.append(lis[min(i+2*k+1,len_lis-1)]-lis[i])
    print(max(ans))

if __name__=='__main__':
    main()

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

zero_list = []
for i in range(N):
    if S[i] == '0':
        if i > 0 and S[i - 1] == '0':
            zero_list[-1][0] += 1
            zero_list[-1][2] += 1
        else:
            zero_list.append([1, i, i])
            
if len(zero_list) <= K:
    ans = N
else:
    zero_c_max = zero_list[K][1]
    l = 0
    r = zero_list[K][1] - 1
    for j in range(1, len(zero_list) - K + 1):
        l = zero_list[j - 1][2] + 1
        if j + K < len(zero_list): 
            r = zero_list[j + K][1] - 1
        else:
            r = N - 1
        if (r - l) + 1 > zero_c_max:
            zero_c_max = (r - l) + 1
    
    ans = zero_c_max
    
print(ans)

Solution (Python):
n,k=map(int,input().split())
s=input()
s+="2"
lst=[]
ruiseki=[0]
i=0;cnt=1;ans=0
while True:
    if s[i]==s[i+1]:
        i+=1
        cnt+=1
    else:
        lst.append(cnt)
        ruiseki.append(ruiseki[-1]+cnt)
        i+=1
        cnt=1 
    if i==n:
        break
if s[0]=="1":
    flag=True
    if 2*k>=len(lst)-1:
        print(ruiseki[-1])
        exit()
else:
    flag=False
    if 2*k>=len(lst):
        print(ruiseki[-1])
        exit()

for i in range(1,len(lst)):
    if flag:
        ans=max(ans,ruiseki[min(i+2*k,len(lst))]-ruiseki[i-1])
        flag=False
    else:
        ans=max(ans,ruiseki[min(i+2*k-1,len(lst))]-ruiseki[i-1])
        flag=True
print(ans)

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

ns = []
now_num = s[0]
cnt = 0
for i in range(n):
    if now_num == s[i]:
        cnt += 1
    else:
        ns.append((now_num, cnt))
        now_num = s[i]
        cnt = 1
ns.append((now_num, cnt))
len_ns = len(ns)
pre = [0] * (len_ns + 1)
for i in range(len_ns):
    pre[i + 1] = pre[i] + ns[i][1]

ans = 1
for i in range(len_ns - 2*k + 1):
    temp = pre[i + 2*k] - pre[i]
    if i > 0 and ns[i - 1][0] == '1':temp += ns[i - 1][1]
    if i + 2*k < len_ns and ns[i + 2*k][0] == '1':temp += ns[i + 2*k][1]
    if temp > ans:ans = temp
print(ans if len_ns - 2*k + 1 > 0 else len(s))

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
from itertools import permutations, accumulate, combinations, combinations_with_replacement, groupby
from math import sqrt, ceil, floor, factorial
from bisect import bisect_left, bisect_right, insort_left, insort_right
from copy import deepcopy
from operator import itemgetter
from functools import reduce
from fractions import gcd
import sys
def input(): return sys.stdin.readline().rstrip()
def I(): return int(input())
def Is(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def TI(): return tuple(map(int, input().split()))
def IR(n): return [I() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def TIR(n): return [TI() for _ in range(n)]
def S(): return input()
def Ss(): return input().split()
def LS(): return list(input())
def SR(n): return [S() for _ in range(n)]
def SsR(n): return [Ss() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
sys.setrecursionlimit(10**6)
MOD = 10**9+7
INF = float('inf')


n, k = Is()
s = S()
A = []  
if s[0] == "0":
    A.append(0)
for key, value in groupby(s):
    A.append(len(list(value)))
if s[-1] == "0":
    A.append(0)

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

print(ans)

Solution (Python):
n, k = map(int, input().split())
s = input()
hand_list = []
for i in range(n):
    if i == 0:
        hand_list.append(i)
    else:
        if s[i] != s[i - 1]:
            hand_list.append(i)
hand_list.append(n)

ans_list = []
if len(hand_list) > 2 * k:
    for i in range(len(hand_list) - 2 * k):
        if s[hand_list[i]] == '1':
            ans = hand_list[min(i + 2 * k + 1, len(hand_list)-1)] - hand_list[i]
        else:
            ans = hand_list[i + 2 * k] - hand_list[i]
        ans_list.append(ans)
else:
    ans_list.append(n)
print(max(ans_list))

Solution (Python):
from collections import deque

N, K = map(int, input().split(' '))
S = input()
arr = []
idx = 0

while idx < N:
    start = idx
    while idx + 1 < N and S[idx] == S[idx + 1]:
        idx += 1
    if S[start] == '0':
        arr.append(-(idx - start + 1))
    else:
        arr.append(idx - start + 1)
    idx += 1

if arr[0] < 0:
    arr = [0] + arr
if arr[-1] > 0:
    arr.append(0)

l, r = 0, 0     
ans, tmp = max(arr), 0
while l < len(arr) and r < len(arr):
    tmp += abs(arr[r])
    r += 1

    if r - l > 2 * K + 1:
        tmp -= arr[l] - arr[l + 1]
        l += 2

    if r - l <= 2 * K + 1:
        ans = max(ans, tmp)
print(ans)

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

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

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

Solution (Python):
def main():
    N, K = list(map(int, input().split(' ')))
    S = input()
    
    zero_one_indices = [0]
    one_zero_indices = [0] if S[0] == '0' else []
    before_c = None
    for i, c in enumerate(S):
        if (before_c, c) == ('0', '1'):
            zero_one_indices.append(i)
        elif (before_c, c) == ('1', '0'):
            one_zero_indices.append(i)
        before_c = c
    max_length = 0
    for k in range(len(zero_one_indices)):
        if k + K < len(one_zero_indices):
            
            length = one_zero_indices[k + K] - zero_one_indices[k]
        else:
            length = N - zero_one_indices[k]
        max_length = max([length, max_length])
    print(max_length)


if __name__ == '__main__':
    main()

Solution (Python):
N,K = map(int,input().split())
S = input()
row = []
count = 1
flag = S[0]
for i in range(1,N):
    if S[i] != flag:
        row.append([count,flag])
        count = 1
        flag = S[i]
    else:
        count += 1
row.append([count,flag])
inst = 0
ans,temp = 0,0
left,right = 0,0
for i in range(len(row)):
    if row[i][1] == "1":
        temp += row[i][0]
    else:
        inst += 1
        if inst <= K:
            temp += row[i][0]
        else:
            temp -= row[left][0]
            if row[left][1] == "1":
                temp -=row[left+1][0]
                left += 1
            temp += row[i][0]
            left += 1
    ans = max(ans,temp)
print(ans)

Solution (Python):
def ranlength(A):
    num = list(A)
    count = 1
    result = ''
    table = []
    for i in range(len(num)):
        try:
            if num[i] == num[i+1]:
               count = count + 1
            else:
                result += str(num[i]) + str(count)
                table.append(count)
                count = 1
        except:
            result += str(num[i]) + str(count)
            table.append(count)
    return(table)
    
N,K=map(int,input().split())
S=input()

RL=ranlength(S)
if S[0]=='0':
    RL=[0]+RL
if S[-1]=='0':
    RL.append(0)


cnt=RL[0]
ans=RL[0]
for i in range(len(RL)):
    if i%2==0 and i>=2:
        cnt+=RL[i]
        cnt+=RL[i-1]
        if i>=K*2+2:
            cnt-=RL[i-(K*2+1)]
            cnt-=RL[i-(K*2+2)]
        ans=max(ans,cnt)
        
print(ans)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n, k = map(int, input().split())
    S = input()

    
    nums = []
    if S[0] == "0":
        nums.append(0)
    i = 0
    while i < n:
        j = i
        while j < n and S[j] == S[i]:
            j += 1
        nums.append(j - i)
        i = j
    nums.append(0) if S[-1] == "0" else None

    
    sums = [0] * (len(nums) + 1)
    for i in range(len(nums)):
        sums[i + 1] = sums[i] + nums[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)


if __name__ == '__main__':
    resolve()

Solution (Python):
n,k=map(int,input().split())
S=input()+"*"
A=[0,0] if S[0]=="0" else [0]
num,cnt=S[0],0
for i in range(n+1):
    if num==S[i]:cnt +=1
    else:
        A.append(cnt)
        num=S[i]
        cnt=1

ans=0
for i in range(1,len(A)):
    A[i]=A[i]+A[i-1]
for i in range(0,len(A),2):
    ans=max(ans,A[min(i+2*k+1,len(A)-1)]-A[i])
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
from itertools import accumulate
def input(): return sys.stdin.readline().strip()


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

    """
    chokudaiさんのコードも尺取りで写経してみる！
    """

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

    """
    この時点でA = [(1の個数), (0の個数), (1の個数), (0の個数),...,(1の個数)]
    のように並んでいるので、Sの先頭が0か1かの場合分けが不要になるし、
    以降の尺取りは(1の個数)のところから常にカウントを始めて良い。
    """

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

    i = 0
    l = len(A)
    while i < l:
        nextLeft = i
        nextRight = min(i + add, l)
        while nextLeft > left:
            tmp -= A[left]
            left += 1
        while nextRight > right:
            tmp += A[right]
            right += 1
        ans = max(ans, tmp)
        i += 2
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import sys
from itertools import accumulate
def input(): return sys.stdin.readline().strip()


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

    """
    反転させる区間は独立であるとしてよい。実際反転させる２区間が左図のように
    オーバーラップしていたとすると、これは右図のように反転させるのと同値だからである：
               _______________                             _______  
        ______|_______        |     --->    ______        |       |
    ___|______|_______|_______|_____    ___|______|_______|_______|_____ 
    
    そうすると安直には0がなるべく長く連続している区間に反転をかけたくなるが、
    長い順に反転をかけた場合1が連続する保証がないのでダメ。

    そこで1の連続する箇所を固定したうえで、そこから0の連続する区間に順番に反転をかけることを考える。
    当然愚直にやるとTLEなので、累積和で実装する
    """

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

    B = list(accumulate(A))
    B.append(B[-1])
    
    l = len(A)
    if S[0] == '0':
        ans = B[min(K * 2 - 1, l - 1)]
        for i in range(1, l - K * 2 + 1):
            if i % 2 == 0:
                j = i + K * 2 - 1
                val = B[j] - B[i - 1]
            else:
                j = i + K * 2
                val = B[j] - B[i - 1]
            
            ans = max(ans, val)
    else:
        ans = B[min(K * 2, l - 1)]
        for i in range(1, l - K * 2 + 1):
            if i % 2 == 1:
                j = i + K * 2 - 1
                val = B[j] - B[i - 1]
            else:
                j = i + K * 2
                val = B[j] - B[i - 1]
            
            ans = max(ans, val)
    print(ans)
       


if __name__ == "__main__":
    main()

Solution (Python):
import itertools
N, K = map(int, input().split())
S = input()
flg = int(S[0])
gr = itertools.groupby(S)
ans, tmp = 0, 0
check_0, check_1 = [], []
for key, group in gr:
    target = len(list(group))
    tmp += target
    if key == '1':
        check_1.append(target)
    else:
        check_0.append(target)
    if key == '0':
        if len(check_0) > K:
                tmp -= check_0[len(check_0)-(K+1)]
        if len(check_1) > K:
                tmp -= check_1[len(check_1)-(K+1)]
    ans = max(ans,tmp)
print(ans)

Solution (Python):
n,k=map(int,input().split())
s=input()
s01=[[1,s[0]]]
for i in range(1,n):
  if s[i]==s[i-1]:
    s01[-1][0]+=1
  else:
    s01.append([1,s[i]])
a=len([i0 for i0,i1 in s01 if i1=='0'])
if a<=k:
  print(n)
  exit()

si=0
sums=[0]
for s01i in s01:
  si+=s01i[0]
  sums.append(si)
ans=0


l=0 if s[0]=='0' else 1
for i in range(l,len(s01),2):
  l=max(0,i-1)
  r=min(len(sums)-1,2*k+i)
  
  ans=max(ans,sums[r]-sums[l])
print(ans)

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

if(S[0] == "1"):
    s = []
else:
    s = [0]
cnt = 1
tmp = S[0]
for i in range(1, len(S)):
    if(tmp == S[i]):
        cnt += 1
    else:
        s.append(cnt)
        tmp = S[i]
        cnt = 1
s.append(cnt)
tmp1 = 0
if(len(s)//2 <= K):
    ans = sum(s)        
else:
    ans = sum(s[:(2*K+1)])
    tmp = ans
    cnt = 0
    for i in range(2*K+1, len(s)-1, 2):
        if(ans < tmp-s[cnt]-s[cnt+1]+s[i]+s[i+1]):
            ans = tmp-s[cnt]-s[cnt+1]+s[i]+s[i+1]
            tmp = tmp-s[cnt]-s[cnt+1]+s[i]+s[i+1]
        else:
            tmp = tmp-s[cnt]-s[cnt+1]+s[i]+s[i+1]
        cnt += 2
    ss = s[::-1]
    if(S[-1] == "0"):
        tmp1 = sum(ss[:(2*K)])
    else:
        tmp1 = sum(ss[:(2*K+1)])
print(max(ans, tmp1))

Solution (Python):
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for _ in range(j)] for _ in range(k)]


from itertools import accumulate 

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

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

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

len_list.append(cnt)

l_list = len(len_list)
wa = [0] + list(accumulate(len_list))
ans = 0
i = 1
l_wa = len(wa)
while True:
    if i+2*K >= l_wa:
        ans = max(ans, wa[l_wa-1] - wa[i-1]) 
        break
    
        
    ans = max(ans, wa[i+2*K] - wa[i-1]) 
    i += 2
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for _ in range(j)] for _ in range(k)]




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

len_list = [0]*(2*(10**5)+100)
ind = 0
if S[0] == '0':
    len_list[0] = 0
    ind += 1

cnt = 1
for i in range(1, N):
    if S[i] != S[i-1]:
        len_list[ind] = cnt
        ind += 1
        cnt = 1
    else:
        cnt += 1
len_list[ind] = cnt


i = 0
wa = 0
sum_l = sum(len_list[:2*K+1])
while True:
    if len_list[i+2*K+1] != 0:
        wa = max(wa, sum_l)
    else:
        wa = max(wa, sum_l)
        break
    sum_l -= (len_list[i] + len_list[i+1])
    sum_l += (len_list[i+2*K+1] + len_list[i+2*K+2])
    i += 2
print(wa)

Solution (Python):
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for _ in range(j)] for _ in range(k)]




N, K = mi()
S = input()
inds = [[0, 0]]

flag = 0
s = 0
for i in range(N):
    if S[i] == '0':
        if not flag:
            flag = 1
            inds.append([s, i-1])
    else:
        if flag:
            s = i
        flag = 0
if flag:
    inds.append([N-1, N-1])
if not flag:
    inds.append([s, N-1])

l_inds = len(inds)
wa = 0
if K >= l_inds-1:
    print(N)
    exit()
for i in range(l_inds-K):
    wa = max(inds[i+K][1] - inds[i][0] + 1, wa)

print(wa)

Solution (Python):
n,k = map(int,input().split())
s = input()
l = x = ans = 0
for r in range(n):
    if s[r] == '0' :
        if r == 0 or s[r-1] == '1':
            x += 1
    if x > k:
        while s[l] == '1':
            l += 1
        while s[l] == '0':
            l += 1
        x -= 1
    ans = max(ans,r-l+1)
print(ans)

Solution (Python):
n, k = map(int, input().split())
s = input()
s1 = []
count = 0
if s[0] == "0":
    s1.append(0)
for i in range(n-1):
    if s[i+1] == s[i]:
        count += 1
    elif s[i+1] != s[i]:
        count += 1
        s1.append(count)
        count = 0
s1.append(count+1)
if s[-1] == "0":
    s1.append(0)




s2 = [0]*(len(s1)+1)
s2[1] = s1[0]
for i in range(1, len(s1)):
    s2[i+1] = s1[i] + s2[i]


ans = 0
if len(s2) > 2*k+1:
    for l in range(len(s2)-(2*k+1))[::2]:
        res = s2[2*k+1+l] - s2[l]
        ans = max(ans, res)
else:
    ans = s2[-1]
print(ans)

Solution (Python):
n, k = map(int, input().split())
a = input()
l = []
if a[0] == '0':
    l.append(0)
sum = []
i = 0
while i < len(a):
    j = i
    while j < len(a) and a[j] == a[i]:
        j += 1
    l.append(j-i)
    i = j
if a[-1] == '0':
    l.append(0)
num = [0]*(len(l)+1)
for i in range(0, len(l)):
    num[i+1] = num[i]+l[i]
ans = -1
for i in range(0, len(num), 2):
    j = i + k*2+1
    if j >= len(num):
        j = len(num)-1
    ans = max(ans, num[j] - num[i])
print(ans)

Solution (Python):
import itertools
N,K=map(int,input().split())
S=input()
gr=itertools.groupby(S)
li=[] if S[0]=='1' else [0]
for key,group in gr:
    li.append(len(list(group)))
res=0
cum=[0]*(len(li)+1)
for i in range(len(li)):
    cum[i+1]=cum[i]+li[i]
for i in range(0,len(li),2):
    r=i+2*K+1
    if r>=len(cum):
        r=len(cum)-1
    res=max(res,cum[r]-cum[i])
print(res)

Solution (Python):
n,k=map(int,input().split())
s=input()
t=1
l=[0]
for i in s:
  i=int(i)
  if i==t: l[-1]+=1
  else: l+=[1]; t=i
if t<1: l+=[0]
s=[0]
for i in l: s+=[s[-1]+i]
a=0
for i in range(0,len(l),2):
  if i<2*k: a=max(a,s[i+1])
  else: a=max(a,s[i+1]-s[i-k*2])
print(a)