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):
'''
Sを連続する値の数のリストに変換
(ただし、左右端は必ず1が0個以上存在するとする)
S='0111010011' -> A=[0,1,3,1,1,2,2]

K=1なら、
sum(A[0:3]), sum(A[2:5]), sum(A[4:7])のmaxが答え
'''

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


A = []
cur = S[0]
c = 0
for s in S[1:] + '*':
    c += 1
    if cur != s:
        cur = s
        A.append(c)
        c = 0


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


AA = [None] * len(A)
AA[0] = A[0]
for i in range(1, len(A)):
    AA[i] = AA[i-1] + A[i]



if len(A) <= 2 * K + 1:
    print(sum(A))
else:
    m = 0
    i = 0
    while i + 2*K <= len(A):
        
        p = AA[i+2*K] - (AA[i-1] if i > 0 else 0)
        m = max(m, p)
        i += 2
    print(m)

Solution (Python):

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

S = input()
A = []
if S[0] == '0':
  A.append(0)
  
i = 0

while i < len(S):
  j = i
  while j < len(S) and S[i] == S[j]:
    j += 1
  A.append(j - i)
  i = j

ans = 0
tot = 0
r = 0
for l in range(len(A)):
  while r < len(A) and r - l < 2 * K + 1:
    tot += A[r]
    r += 1
  if l % 2 == 0:
    ans = max(ans, tot)
  if r == l:
    r += 1
  else:
    tot -= A[l]
print(ans)

Solution (Python):
from itertools import groupby
from collections import deque

N, K = [int(x) for x in input().split()]
blocks = [[tp, list(people)] for tp, people in groupby(input())]

ans = 0
cur = 0
stand = 0
target = deque()

for tp, people in blocks:
    if tp == '0':
        stand += 1
        if stand > K:
            removed = target.popleft()
            cur -= len(removed[1])
            if removed[0] == '1' and len(target) > 0:
                removed = target.popleft()
                cur -= len(removed[1])

    target.append([tp, people])
    cur += len(people)
    ans = max(ans, cur)

print(ans)

Solution (Python):
from itertools import groupby
from collections import deque

N, K = [int(x) for x in input().split()]
blocks = [[tp, list(people)] for tp, people in groupby(input())]

ans = 0
cur = 0
stand = 0
target = deque()

for tp, people in blocks:
    if tp == '0':
        stand += 1
        if stand > K:
            removed = target.popleft()
            cur -= len(removed[1])
            if removed[0] == '1' and len(target) > 0:
                removed = target.popleft()
                cur -= len(removed[1])

    target.append([tp, people])
    cur += len(people)
    ans = max(ans, cur)

print(ans)

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

l = 0
count = 0

ans = 0
for r in range(N):
    if S[r] == '0':
        if r == 0 or S[r - 1] == '1':
            count += 1
    if count > K:
        while S[l] == '1':
            l += 1
        while S[l] == '0':
            l += 1
        count -= 1
    ans = max(ans, r - l + 1)

print(ans)

Solution (Python):


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


lst = [0]
for i in range(0, N-1):
    if S[i] != S[i+1]:
        lst += [i+1]

ans = 0
roop = len(lst)
lst = lst + [N]*(N+10**5)


for i in range(roop):
    if S[lst[i]] == '0':
        
        ans = max(ans, lst[i+2*K]- lst[i])
    if S[lst[i]] == '1':
        
        ans = max(ans, lst[i+2*K +1]- lst[i])
print (ans)

Solution (Python):
import sys
import itertools
n, k = map(int, sys.stdin.readline().split())
s = [int(string) for string in input()]

tmp = []
flag = s[0]
cnt = 1

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

ruisekiwa = list(itertools.accumulate(tmp))
ans = 0
ruisekiwa.insert(0, 0)
for left in range(0, len(ruisekiwa), 2):
    right = left + 2*k+1
    if len(ruisekiwa) <= right:
        right = len(ruisekiwa)-1
    ans = max(ans, ruisekiwa[right] - ruisekiwa[left])

print(ans)

Solution (Python):
N,K=map(int,input().split())
S=input()+"2"
_list=[]
if S[0]=="1":
    _list=[]
else:
    _list=[0]
cnt=1
for i in range(N):
    if S[i]==S[i+1]:
        cnt+=1
    else:
        _list.append(cnt)
        cnt=1
if S[-2]=="0":
    _list.append(0)
import itertools
__list=[0]+list(itertools.accumulate(_list))
C=len(__list)
K=min(C,K)
if (C+1)//2<=K:
    print(__list[-1])
else:
    print(max([__list[2*i+2*K+1]-__list[2*i] for i in range((C+1)//2-K)]))

Solution (Python):


'''
allinputs = iter(input().splitlines())
input = lambda : next(allinputs)


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

L = [0]
R = [0]

for i in range(N):
	if S[i] == 0:
		if i == N - 1:
			L.append(i + 1)
		elif S[i + 1] == 1:
			L.append(i + 1)
	if S[N - i -1] == 0:
		if i == N - 1:
			R.append(i + 1)
		elif S[N - i -2] == 1:
			R.append(i + 1)

ans = 0
M = len(L) - K

if M <= 0:
	ans = N
	
for i in range(M):
	tmp = N - L[i] - R[M - i - 1]
	if tmp > ans:
		ans = tmp

print(ans)

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

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())


n,k = li()
s = ns()

cur = "1"
cum = [0]

for i, si in enumerate(s):
    if cur != si:
        cur = si
        cum.append(i)

cum.append(n)

if cur == "0":
    cum.append(n)


ans = 0
if k >= len(cum)//2:
    ans = n
else:
    for i in range(0, len(cum) - 2*k - 1, 2):
        ans = max(ans, cum[i + 2*k + 1] - cum[i])

print(ans)

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

l = 0
count = 0

ans = 0
for r in range(N):
    if S[r] == '0':
        if r == 0 or S[r - 1] == '1':
            count += 1
    if count > K:
        while S[l] == '1':
            l += 1
        while S[l] == '0':
            l += 1
        count -= 1
    ans = max(ans, r - l + 1)

print(ans)

Solution (Python):
import numpy as np

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








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


if len(next_lst) <= 2*K+1:
  ans = sum(next_lst)
  print(ans)
  exit()


ans = 0
lst = np.cumsum(np.array([0]+next_lst))
for i in range((len(next_lst)-2*K+1)//2):
  var = lst[2*i+2*K+1] - lst[2*i]
  ans = max(ans, var)

print(ans)

Solution (Python):
def intinput(): return map(int,input().split())

n,k=intinput()
s=input()
x=1
cnt=0
lis=[]
for i in range(n):
    if s[i]==str(x):
        cnt+=1
    else:
        lis.append(cnt)
        cnt=1
        x ^= 1
lis.append(cnt)
if len(lis)%2==0:
    lis.append(0)
x=[0]
for i in range(len(lis)):
    x.append(x[i]+lis[i])
l = 2*k+1
ans=0
for i in range(0,len(x),2):
    left=i
    right=min(len(x)-1,l+i)
    tmp = x[right]-x[left]
    ans = max(ans,tmp)
print(ans)

Solution (Python):
from itertools import accumulate

import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

N, K = map(int, input().split())
S = input()[:-1]

L = [0]
L_index = [0]
prev = ""
for c in S:
    if c == prev:
        L[-1] += 1
    else:
        prev = c
        L.append(1)
        L_index.append(c)

L_acc = list(accumulate(L))

ans = 0
len_L = len(L)
for s in range(1, len_L):
    if L_index[s] == "0":
        right_end = min(s+K*2-1, len_L-1)
    else:
        right_end = min(s+K*2, len_L-1)
    ans = max(ans, L_acc[right_end]-L_acc[s-1])
print(ans)

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

t = []
l = r = 0

for i in range(1, n + 1):
    if s[i - 1] == '0' and s[i] == '1':
        l = i
    if s[i] == '1' and s[i + 1] == '0':
        r = i
        t.append((l, r))

if len(t) > 0:
    res = 0

    if k < len(t):
        for i in range(len(t) - k):
            tmp = t[i + k][1] - t[i][0]
            res = max(res, tmp)
        if t[0][0] != 1:
            tmp = t[k - 1][1] - 1
            res = max(res, tmp)
        if t[-1][1] != n:
            tmp = n - t[-k][0]
            res = max(res, tmp)
    else:
        for i in range(len(t)):
            res = t[-1][1] - t[0][0]
        if t[0][0] != 1:
            tmp = t[min(k, len(t))-1][1] - 1
            res = max(res, tmp)
        if t[-1][1] != n:
            tmp = n - t[-min(k,len(t))][0]
            res = max(res, tmp)

    print(res + 1)
else:
    print(n)

Solution (Python):
n, k = map(int, input().split())
s = input()
x = [0]
for i in range(n - 1):
    if s[i:i + 2] in ['01', '10']:
        x.append(i + 1)
y = []
for i in range(len(x)):
    idx = i + 2 * k
    if s[x[i]] == '0':
        if i > 0:
            continue
    else:
        idx += 1

    if idx < len(x):
        y.append(x[idx] - x[i])
    else:
        y.append(n - x[i])
        break
print(max(y))

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



prevcCt = 0 
nextCnt = 0 



val = 0
vals = []
for i in range(len(s)):
	if s[i] == "0":
		val -= 1
	else:
		val += 1

	
	
	if i == len(s) - 1 or s[i] != s[i+1]:
		vals.append(val)
		val = 0




if vals[0] < 0:
	vals.insert(0,0)

vals.append(0)
vals.append(0)



vals = list(map(abs, vals))




max = 0

loopCnt = len(vals) - k
if  loopCnt < 1:
	loopCnt = 1



sumVal = sum(vals[0:2*k + 1])
max = sumVal




for i in range(loopCnt):
	
	
	if i%2 == 0 and i>0:
		
		outVal = sum(vals[i-2:i])
		
		inVal = sum(vals[i + 2*k -1:i + 2*k + 1])
	
		sumVal = sumVal - outVal + inVal

		
		

		
		
		
		if sumVal > max:
			max = sumVal

print(max)

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

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

sums = [0] * (len(nums) + 1)
for i in range(len(nums)):
    sums[i+1] = sums[i] + nums[i]

Add = 2 * K + 1
res = 0
for left in range(0, len(nums), 2):
    right = left + Add
    if right >= len(sums):
        right = len(sums) - 1
    res = max(res, sums[right] - sums[left])

print(res)

Solution (Python):

















import math
import copy
from collections import defaultdict
from collections import Counter
from collections import deque

from itertools import product

from itertools import permutations

from itertools import combinations
from bisect import bisect_left, bisect_right


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]

def i_inpl(): return int(input())
def s_inpl(): return map(int,input().split())
def l_inpl(): return list(map(int, input().split()))
INF = float("inf")






N, K = l_inpl()
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)

ruiseki = [0]

for i in range(1, len(nums)+1):
    ruiseki.append(ruiseki[i-1] + nums[i-1])

ans = 0

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

print(ans)

Solution (Python):
n, k = [int(x) for x in input().split()]
stands = [int(x) for x in input()]

wl = wr = 0
a = 1 - stands[0]
maxhand = 1

while wr < n-1:
    
    if stands[wr + 1] == 1:
        wr += 1
    else: 
        if stands[wr] == 0:
            wr += 1
        else: 
            if (a+1 <= k): 
                a += 1
                wr += 1
            else: 
                
                if (wr - wl + 1) > maxhand:
                    maxhand = wr - wl + 1
                while stands[wl] == 1:
                    wl += 1
                while stands[wl] == 0:
                    wl += 1
                a -= 1

if (wr - wl + 1) > maxhand:
    maxhand = wr - wl + 1

print(maxhand)

Solution (Python):
import sys
n, k = map(int, input().split())
s = input()
sList = [] 
switch = int(s[0])
num = 1
if int(s[0]) == 0:
  sList.append(0)
if n > 1:
  for i in range(1, n):
    if int(s[i]) == switch:
      num += 1
    else:
      sList.append(num)
      num = 1
      switch = int(s[i])
sList.append(num)
if len(sList) // 2 < k: 
  print(n)
  sys.exit()
elif len(sList) % 2 == 0: 
  sList.append(0)
  
s = sum(sList[0:2*k+1])
sMax = s

for i in range(1, (len(sList)-2*k) // 2 + 1): 
  s += - sList[2*i-2] - sList[2*i-1] + sList[2*i+2*k] + sList[2*i+2*k-1]
  if sMax < s:
    sMax = s
    
print(sMax)

Solution (Python):
import sys
n, k = map(int, input().split())
s = input()
sList = [] 
switch = int(s[0])
num = 1
if int(s[0]) == 0:
  sList.append(0)
if n > 1:
  for i in range(1, n):
    if int(s[i]) == switch:
      num += 1
    else:
      sList.append(num)
      num = 1
      switch = int(s[i])
sList.append(num)
if len(sList) // 2 < k: 
  print(n)
  sys.exit()
elif len(sList) % 2 == 0: 
  sList.append(0)
  
s = sum(sList[0:2*k+1])
sMax = s

for i in range(1, (len(sList)-2*k) // 2 + 1): 
  s += - sList[2*i-2] - sList[2*i-1] + sList[2*i+2*k] + sList[2*i+2*k-1]
  if sMax < s:
    sMax = s
    
print(sMax)

Solution (Python):
n,k=[int(i) for i in input().split()]
s=input()
 
ope=s[0]
cnt=1
l=[];
if s[0]=='0':
	l.append(0)
for i in range(1,len(s)):
	if s[i]==ope:
		cnt+=1
	else:
		l.append(cnt)
		ope=s[i]
		cnt=1
l.append(cnt)
 
if s[len(s)-1]=='0':
	l.append(0)
	
start=0
end=2*k
 
max_sum=sum(l[start:end+1])
cur_sum=max_sum
start+=2
end+=2
while end<len(l):
    cur_sum=cur_sum-l[start-2]-l[start-1]+l[end-1]+l[end]
    max_sum=max([max_sum,cur_sum])
    start+=2
    end+=2
print(max_sum)

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

q = []
tmp = 0

if len(s) == 1:
    if k >= 1 or s[0] == '1':
        print(1)
    else:
        print(0)
else:
    if s[0] == '0':
        q.append(0)
    for i in range(len(s)):
        if i == 0:
            tmp += 1
        elif i != len(s)-1:
            if s[i] == s[i-1]:
                tmp += 1
            else:
                q.append(tmp)
                tmp = 1
        else:
            if s[i] == s[i-1]:
                tmp += 1
                q.append(tmp)
            else:
                q.append(tmp)
                q.append(1)
    if s[len(s)-1] == '0':
        q.append(0)

    tmp = 0
    max_k = min(math.ceil(len(q)/2), k)
    """
    if len(q) == 1:
        if s[0] == '0' and k == 0:
            ans.append(0)
        else:
            ans.append(q[0])

    
    elif s[0] == '0' and len(q) % 2 == 1:
        for i in range(1, len(q) - 2 * max_k, 2):
            ans.append(sum(q[i:i+2*max_k+1]))
        ans.append(sum(q[:+2*max_k]))
        ans.append(sum(q[-2*max_k+1:]))
    
    elif s[0] == '0' :
        for i in range(1, len(q) - 2 * max_k, 2):
            ans.append(sum(q[i:i+2*max_k+1]))
        ans.append(sum(q[:2*max_k]))
    
    elif len(q) % 2 == 0:
        for i in range(1, len(q) - 2 * max_k, 2):
            ans.append(sum(q[i:i+2*max_k+1]))
        ans.append(sum(q[-2*max_k+1:]))
    else:
        if max_k == math.ceil(len(q)/2):
            max_k -= 1
        for i in range(0, len(q) - 2 * max_k, 2):
            ans.append(sum(q[i:i+2*max_k+1]))

    """
    
    left = 0
    right = 0
    tmp = 0
    ans = 0
    for i in range(0, len(q), 2):
        next_left = i
        next_right = min(i + 2 * k+1, len(q))

        while (next_left > left):
            tmp -= q[left]
            left += 1

        while (next_right > right):
            tmp += q[right]
            right += 1

        ans = max(ans, tmp)
    print(ans)

Solution (Python):
N,K =map(int,input().split())
S =input()
lis = [[],[]]
if S[0]=="0":
    lis[1].append(0)
j = S[0]
su = 0
for i in S:
    if i ==j:
        su += 1
    else:
        lis[int(j)].append(su)
        j = i
        su = 1
lis[int(j)].append(su)
if j=="0":
    lis[1].append(0)
if len(lis[0])<=K:
    print(N)
else:
    ans = 0

    ans = sum(lis[0][0:K]) + sum(lis[1][0:K+1])
    foo = ans
    for i in range(len(lis[0])-K):
        foo += lis[0][K+i] -lis[0][i] +lis[1][K+1+i] -lis[1][i]
        if ans < foo:
            ans = foo
    print(ans)

Solution (Python):
N,K =map(int,input().split())
S =input()
lis = [[],[]]
if S[0]=="0":
    lis[1].append(0)
j = S[0]
su = 0
for i in S:
    if i ==j:
        su += 1
    else:
        lis[int(j)].append(su)
        j = i
        su = 1
lis[int(j)].append(su)
if j=="0":
    lis[1].append(0)
if len(lis[0])<=K:
    print(N)
else:
    ans = 0

    ans = sum(lis[0][0:K]) + sum(lis[1][0:K+1])
    foo = ans
    for i in range(len(lis[0])-K):
        foo += lis[0][K+i] -lis[0][i] +lis[1][K+1+i] -lis[1][i]
        if ans < foo:
            ans = foo
    print(ans)

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

ans = 0
l = 0
r = 0
count = 1 if s[0]=='0' else 0

while r < n:
    if count <= k:
        ans = max(ans, r-l+1)
        if s[r]=='1' and s[r+1]=='0':
            count += 1
        r += 1
    else:
        if s[l]=='0' and s[l+1]=='1':
            count -= 1
        l += 1

print(ans)

Solution (Python):
N,K = map(int,input().split())
S = list(input())
cnt = 1
k = {}
if S[0] == "0":
    k[cnt] = [0]
    cnt += 1


flg = False
for i in range(N):
    if S[i] == '1':
        if flg == False :
            k[cnt] = []
        k[cnt].append(i)
        flg = True
    else:
        if flg == True:
            flg = False
            cnt += 1

if S[-1] == '0':
    k[cnt] = [N-1]

if '0' not in S:
    print(len(S))
    exit()
if N == 1:
    print(1)
    exit()
if cnt - K + 1 < 0:
    print(len(S))
    exit()

ans = 0
for i in range(1,cnt - K+1):
    ans = max(ans , k[i+K][-1]-k[i][0] + 1)
print(ans)

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

ans = 0
r = 0
l = 0
count = 1 if s[0]=='0' else 0

while r < n:
    if count <= k:
        ans = max(ans, r-l+1)
        if s[r]=='1' and s[r+1]=='0':
            count += 1
        r += 1
    else:
        if s[l]=='0' and s[l+1]=='1':
            count -= 1
        l += 1

print(ans)

Solution (Python):
import numpy as np




class Interval():
    def __init__(self, li):
        self.li = li
        self.n = len(li)
        self.sum_li = [li[0]]
        for i in range(1, self.n):
            self.sum_li.append(self.sum_li[i-1] + li[i])

    def __call__(self, a, b=None): 
        if b is None:
            return self.sum(0, a)

        res = self.sum_li[min(self.n-1, b-1)]
        if a > 0:
            res -= self.sum_li[min(self.n-1, a-1)]
        return res


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

s = input()
ar = np.array(list(map(int, s)))

idx = -1
b = []

for i in np.where(ar[:-1] - ar[1:] != 0)[0]:
    b.append(i-idx)
    idx = i

b.append(N-1-idx)

b_int = Interval(b)

if s[0] == '1':
    print(max(
        b_int(i, i+2*K+1) for i in range(0, b_int.n) if i%2 == 0
    ))    
else:
    res = b_int(0, 2*K)
    if b_int.n > 1:
        res = max(res,max(
            b_int(i, i+2*K+1) for i in range(1, b_int.n) if i%2 == 1
        ))
    print(res)

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

tank = []
counter = 1
for i in range(1, N):
    if S[i] != S[i-1]:
        tank.append(counter)
        counter = 0
    counter += 1
    if i == N-1:
        tank.append(counter)

if S[0] == '0' and (len(tank)+1)//2 <= K:
    print(N)
    exit()
if len(tank)//2 <= K and S[0] == '1':
    print(N)
    exit()

ans = 0
res = 0
if S[0] == '0':
    for i in range(2*K):
        res += tank[i]
    ans = res
    for i in range(2*K, len(tank)):
        if i == 2*K:
            res -= tank[0]
        elif i%2 == 0:
            res = res - tank[i-2*K] - tank[i-2*K-1]
        res += tank[i]
        ans = max(ans, res)
else:
    for i in range(2*K+1):
        res += tank[i]
    ans = res
    for i in range(2*K+1, len(tank)):
        if i % 2 == 1:
            res = res - tank [i-2*K-1] - tank[i-2*K]
        res += tank[i]
        ans = max(ans, res)

print(ans)

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


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


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

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

Solution (Python):
import sys
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def II(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
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 SI(): return input()
YN = lambda b: print('YES') if b else print('NO')
yn = lambda b: print('Yes') if b else print('No')

def main():
    N, K = LI()
    S = SI().strip()

    searching = 1
    count = [0]
    head = 0
    for i in range(N):
        if int(S[i]) == searching:
            count[head] += 1
        else:
            searching = (searching+1)%2
            head += 1
            count.append(1)
    if len(count)//2 <= K:
        print(N)
        sys.exit()

    count.append(0)

    current_sum = sum(count[0:2*K+1])
    max_length = current_sum
    try:
        for k in range(1, N):
            if 2*k+2*K > len(count): break
            current_sum = current_sum - count[2*k-2] - count[2*k-1] + count[2*k+2*K-1] + count[2*k+2*K]
            max_length = max(current_sum, max_length)
    except:
        pass
    print(max_length)

main()

Solution (Python):
def sub():
    N, K = [int(e) for e in input().split(" ")]
    ss = [int(e) for e in input()]
    
    
    

    dd = [ss[i+1] - ss[i] for i in range(N-1)]
    idd = [0] + [i+1 for i, d in enumerate(dd) if d != 0]
    
    maxScore = 0
    for i in range(len(idd)):
        if ss[idd[i]] == 0:
            ni = i + 2*K
            if ni < len(idd):
                score = idd[ni] - idd[i]
            else:
                score = N - idd[i]
            if score > maxScore:
                maxScore = score
        else:
            ni = i + 2*K + 1
            if ni < len(idd):
                score = idd[ni] - idd[i]
            else:
                score = N - idd[i]
            if score > maxScore:
                maxScore = score
        
    print(maxScore)

sub()

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

left = 0
count,ans = 0,0

for right in range(n):
    if s[right] =="0":
        if right==0 or s[right-1]=="1":
            count += 1
    if count > k:
        while s[left] == "1":
            left += 1
        while s[left] == "0":
            left += 1
        count -= 1
    ans = max(ans,right-left+1)
print(ans)

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

a = []
x = 0 
y = 0 
if s[0] == 0:
  a.append(0)
for i in range(n):
  if s[i] == 1:
    x += 1
  else:
    y += 1
  if x > 0 and y > 0:
    if s[i] == 0:
      a.append(x)
      x = 0
    else:
      a.append(y)
      y = 0
if x > y:
  a.append(x)
else:
  a.append(y)
  a.append(0)


m = len(a)
left=[0]*(m+1)
right=[0]*(m+1)

for i in range(m):
  left[i+1] = left[i] + a[i]
for i in range(m):
  right[m-i-1] = right[m-i] + a[m-i-1]

ans = 0
if m%2 == 0:
  r = int(m/2) + 1
else:
  r = int((m+1)/2) 

for i in range(r):
  if 2*i+2*k+1 <= m:
    x = left[2*i+2*k+1]-left[2*i]
  else:
    x = left[-1] - left[2*i]
  if ans < x:
    ans = x
print(ans)

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



change_point = [0]
for i in range(N-1):
    if S[i+1] != S[i]:
        change_point.append(i+1)


ans_candidates = []
l = len(change_point)
for i in range(l):
    if i+2*K < l and S[change_point[i]] == '0':
        ans_candidates.append(change_point[i+2*K]-change_point[i])
    elif i+2*K+1 < l and S[change_point[i]] == '1':
        ans_candidates.append(change_point[i+2*K+1]-change_point[i])
    else:
        ans_candidates.append(N-change_point[i])

print( max(ans_candidates) )

Solution (Python):
def main():
    n, k = [int(i) for i in input().split()]
    s = [int(i) for i in list(input())]
    table = []
    now = 1
    cnt = 0
    for i in range(n):
        if s[i] == now:
            cnt += 1
        else:
            table.append(cnt)
            cnt = 1
            now ^= 1 
    if cnt != 0:
        table.append(cnt)
    if len(table)%2 == 0:
        
        table.append(0)
    ans = 0
    l = 0
    d = min(len(table), 2*k+1)
    r = l + d
    tmp = sum(table[l:r])
    while r <= len(table):
        ans = max(ans, tmp)
        if l < len(table):
            tmp -= table[l]
        l += 1
        if l < len(table):
            tmp -= table[l]
        l += 1
        r += 1
        if r <= len(table):
            tmp += table[r-1]
        r += 1
        if r <= len(table):
            tmp += table[r-1]
    print(ans)


if __name__ == '__main__':
    main()

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

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

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

P = [0] * (len(L) + 1)
for i in range(len(L)):
    P[i + 1] = P[i] + L[i][0]
    
ma = 0
t = K * 2 + 1
for i in range(len(P) - 1):
    if L[i][1] == '0':
        index = min(i + t - 1, len(P) - 1)
        ma = max(ma, P[index] - P[i])
    else:
        index = min(i + t, len(P) - 1)
        ma = max(ma, P[index] - P[i])
    
print(ma)

Solution (Python):
[n, k] = [int(i) for i in input().split()]
s = input()

l = []
cnt = 1
if s[0] == '0':
    l.append(0)
for i in range(len(s) - 1):
    if (s[i] == s[i + 1]):
        cnt += 1
    else:
        l.append(cnt)
        cnt = 1
l.append(cnt)
if s[-1] == '0':
    l.append(0)
m = (len(l)) // 2
sum1 = []
temp = sum(l[0:min(2 * m + 1, 2 * k + 1)])
sum1.append(temp)
if (m > k):
    for i in range(1, m - k + 1):
        temp += l[2 * (k + i) - 1] + l[2 * (k + i)] - l[2 * i - 1] - l[2 * i - 2] 
        sum1.append(temp)
print(max(sum1))

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

if len(S) == 1:
    print(1)
elif len(S) == 2:
    print(2)
elif S[0] == '0':
    LA = [0]
    LB = [0]
    for i in range(0,N-1):
        if (S[i] != S[i+1]) & (S[i] == '0'):
            LA.append(i+1)
    for i in range(1,N-1):
        if (S[i] != S[i+1]) & (S[i] == '1'):
            LB.append(i)
    LB.append(N-1)
    ANS = 0
    if K >= len(LB)-1:
        print(N)
    else:
        for i in range(len(LB)-K):
            J = LB[i+K]-LA[i]+1
            if J > ANS:
                ANS = J
        print(ANS)
else:
    LA = [0]
    LB = []
    for i in range(0,N-1):
        if (S[i] != S[i+1]) & (S[i] == '0'):
            LA.append(i+1)
        elif (S[i] != S[i+1]) & (S[i] == '1'):
            LB.append(i)
    LB.append(N-1)
    ANS = 0
    if K >= len(LB)-1:
        print(N)
    else:
        for i in range(len(LB)-K):
            J = LB[i+K]-LA[i]+1
            if J > ANS:
                ANS = J
        print(ANS)

Solution (Python):
N, K = map(int,input().split())
S = input()
L = []
t = 1
for k in range(1,N):
    if S[k-1] == S[k]:
        t += 1
    else:
        L.append(t)
        t = 1
L.append(t)
l = len(L)
A = [0 for k in range(l+2)]

for k in range(l):
    A[k+1] = A[k] + L[k]
A[l+1] = A[l]
if S[0] == "0":
    ans = A[min(l,2*K)]
    if l%2 == 1:
        for k in range(l-2*K+1):
            if k%2 == 1:
                ans = max(ans,A[k+2*K+1]-A[k])
    else:
        for k in range(l-2*K):
            if k%2 == 1:
                ans = max(ans,A[k+2*K+1]-A[k])
else:
    ans = A[min(l,2*K+1)]
    if l%2 == 1:
        for k in range(l-2*K):
            if k%2 == 0:
                ans = max(ans,A[k+2*K+1]-A[k])
    else:
        for k in range(l-2*K+1):
            if k%2 == 0:
                ans = max(ans,A[k+2*K+1]-A[k])

print(ans)

Solution (Python):
import copy
import sys
N, K = (int(x) for x in input().split())
string = input()
ls = list(map(int, string))

if string == "1" or string == "0":
    print(1)
    sys.exit()


def cal_list(pb):
    comp = []
    k = 0
    m = 0
    if pb[1][0] == "0":
        pb[0].insert(0, 0)
        pb[1].insert(0, "1")
    if pb[1][len(pb[1][:])-1] == "0":
        pb[0].append(0)
        pb[1].append("1")
    comp.append(pb[0][0])
    for i in range(1, len(pb[0][:])):
        comp.append(comp[i-1]+pb[0][i])
    ans = 0
    j = 0
    for h in list(reversed(range(K+1))):
        while 2*h+1+2*j <= len(comp):
            if j == 0:
                ans = comp[2*h]
            else:
                ans = max(ans, comp[2*h+2*j]-comp[j*2-1])
            j += 1
    print(ans)


def split_01(lis):
    pb = [[] for i in range(2)]
    i = 0
    while i < len(lis):
        count_0, count_1 = 0, 0
        if lis[i] == 0:
            while lis[i] == 0:
                count_0 += 1
                i += 1
                if i >= len(lis):
                    break
            pb[0].append(count_0)
            pb[1].append("0")
        if i >= len(lis):
            break
        if lis[i] == 1:
            while lis[i] == 1:
                count_1 += 1
                i += 1
                if i >= len(lis):
                    break
            pb[0].append(count_1)
            pb[1].append("1")
        if i >= len(lis):
            break
    return pb


pr = split_01(copy.copy(ls))
cal_list(copy.copy(pr))

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

    
    
    results = [0]
    prev = 1
    for i in range(N):
        s = int(S[i])
        if prev == s:
            results[-1] += 1
        else:
            results.append(1)
        prev = s
    if prev == 0: 
        results.append(0)
    if K >= (len(results) // 2):
        return len(S)

    lh = 0
    ans = results[0]
    maximum = -1
    for rh in range(2, len(results), 2):
        diff = results[rh] + results[rh-1]
        if (rh - lh) > 2*K:
            lh += 2
            diff -= (results[lh-1] + results[lh-2])
        ans = ans + diff
        maximum = max(maximum, ans)
    return maximum

print(m())