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

def get_input():
    N,K = [int(_) for _ in input().split()]
    S = [int(_) for _  in list(input())]
    return N,K,S

def solve(N,K,S):
    length = len(S)
    array = count_array(S)
    start_char = S[0]
    ans = find_max(array,K,start_char)
    return ans

def find_max(array,K,start_char):
    if start_char ==0 and len(array) <= 2 * K:
        return sum(array)
    elif start_char == 1 and len(array) <=2 * K+1 :
        return sum(array)
    else:
        if start_char == 0:
            start,end = 1,2*K+2
        else:
            start,end = 0,2*K+1
        idx = 0
                    
        current_sum = sum(array[start:end])
        ret = [current_sum]
        if start_char == 0:
            ret.append(sum(array[0:2*K]))
        ret.append(sum(array[-2*K:]))
        while(1):
            start+=2
            end+=2
            if end > len(array):
                break
            current_sum = current_sum- sum(array[start-2:start]) + sum(array[end-2:end])
            ret.append(current_sum)
    return max(ret)
            

def count_array(S):
    array = []
    cur_char = S[0]
    cur_val = 1
    for char in S[1:]:
        if cur_char != char:
            array.append(cur_val)
            cur_char = char
            cur_val = 1
        else:
            cur_val+=1
    array.append(cur_val)

    return array





if __name__ == "__main__":
    N,K,S = get_input()
    ans = solve(N,K,S)
    print(ans)

Solution (Python):
import sys

N, K = map(int, input().strip().split(' '))
S = input().strip()
prev = ''
l = 0
lst = []
for s in S:
    if s == prev:
        l += 1
    else:
        lst += [l]
        l = 1
    prev = s
lst += [l]
lst = lst[1:]


flip = S[0] == '1'
res = sum(lst[0:2*K+flip])
cur = res

flip ^ - 1
for i in range(1, len(lst)):
    cur -= lst[i-1]
    if not flip:
        cur += sum(lst[i+2*K-1:i+2*K+1])
        res = max(cur, res)
        
    
    
    flip ^= 1



print(res)

Solution (Python):
from heapq import heappush,heappop

def int_raw():
    return int(input())

def ss_raw():
    return input().split()

def ints_raw():
    return list(map(int, ss_raw()))


N, K = ints_raw()
S = input()
cts = []
ctree = {}



def main():
    ans = 0
    kukan = 1
    start = 0
    if S[0]=="0":
        cts.append(["1",0,0])
    for i in range(1,len(S)):
        if S[i-1]!=S[i]:
            cts.append([S[i-1],kukan,start])
            start = i
            kukan=0
        kukan+=1
    cts.append([S[-1],kukan,start])
    if S[-1]=="0":
        cts.append(["1",0, N-1])
    if K*2+1 >= len(cts):
        return N
    buf = sum([cts[i][1] for i in range(K*2+1)])
    ans = buf
    for i in range(K*2+1,len(cts)):
        n_buf = buf-cts[i-(K*2+1)][1]+cts[i][1]
        if cts[i][0]=="1":
            ans =max(ans,n_buf)
        buf = n_buf
    return ans

print(main())

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

cnt_list = []
i = 0
if s[0] == "0":
    key = "0"
else:
    key = "1"
last = 0
while i < N:
    j = i
    tmp = 1
    for j in range(i + 1, N):
        if s[j] == key:
            tmp += 1
            if j == N - 1:
                last = 1
        else:
            break

    cnt_list.append([int(key), tmp])

    if key == "0":
        key = "1"
    else:
        key = "0"
    i = max(j, i + 1)
    if last:
        break

cnt = K
i = 0
tmp = 0
while cnt > 0 and i < len(cnt_list):
    tmp += cnt_list[i][1]
    if cnt_list[i][0] == 0:
        cnt -= 1
    i += 1

if i < len(cnt_list):
    tmp += cnt_list[i][1]
    i += 1

anslist = []
anslist.append(tmp)

start = 0
while i < len(cnt_list) and start < len(cnt_list):
    if cnt_list[start][0] == 1:
        tmp -= cnt_list[start][1]
        if start + 1 < len(cnt_list):
            tmp -= cnt_list[start + 1][1]
        start += 2
    else:
        tmp -= cnt_list[start][1]
        start += 1

    if cnt_list[i][0] == 0:
        tmp += cnt_list[i][1]
        if i + 1 < len(cnt_list):
            tmp += cnt_list[i + 1][1]
        i += 2
    else:
        tmp += cnt_list[i][1]
        i += 1
    anslist.append(tmp)

print(max(anslist))

Solution (Python):
"""
python3 c.py < input.txt
"""
import math
import itertools as it
import heapq

N,K=map(int, input().split())
S=input()
T=[S[0]]
U=[1]
for c in S[1:]:
    if c==T[-1]:
        U[-1]=U[-1]+1
    else:
        T.append(c)
        U.append(1) 
M=len(U)
V=[U[0]]
for i in range(1,M):
    V.append(V[-1]+U[i])

X=[]
for l in range(M):
    if T[l]=="0":
        ll=max(0, l-1)
        r=max(l, min(M-1, l+2*(K-1)+1))
        X.append((ll, r))
    else:
        X.append((0,0))
V=[0]+V





print(max([V[r+1]-V[l] for (l,r) in X]))

Solution (Python):


import sys
import copy





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

cnt = []
kind = []

i, j = 0, 0
while i < N:
    while j < N and S[i] == S[j]:
        j += 1
    cnt.append(j - i)
    kind.append(int(S[i]) - int('0'))
    i = j

ans = 0
sum = 0
zero = 0
L = len(cnt)
right = 0
for left in range(len(cnt)):
    if left > right:
        right = left
    while right < len(cnt) and zero <= K:
        if kind[right] == 0 and zero == K:
            break
        if kind[right] == 0:
            zero += 1
        sum += cnt[right]
        right += 1

    ans = max(ans, sum)
    sum -= cnt[left]
    if kind[left] == 0:
        zero -= 1

print(ans)

Solution (Python):


import sys
import copy





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

cnt = []
kind = []

i, j = 0, 0
while i < N:
    while j < N and S[i] == S[j]:
        j += 1
    cnt.append(j - i)
    kind.append(int(S[i]) - int('0'))
    i = j

ans = 0
sum = 0
zero = 0
L = len(cnt)
right = 0
for left in range(L):
    if left > right:
        right = left
    while right < L and zero <= K:
        if kind[right] == 0 and zero == K:
            break
        if kind[right] == 0:
            zero += 1
        sum += cnt[right]
        right += 1

    ans = max(ans, sum)
    sum -= cnt[left]
    if kind[left] == 0:
        zero -= 1

print(ans)

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

state = 2
counts = []
k_count = 0 

end_lens = []
save_flag = False
end_i = -1
for s_in, s in enumerate(S):
    if s == "0":
        if state == 1: 
            
            
            
            if k_count >= K: 
                end_i += 1
                
                
                len_ = s_in - counts[end_i][0]
                end_lens.append(len_)
                save_flag = False

            state = 0

        elif state == 2: 
            new_count = [s_in, 0, -1]
            counts.append(new_count)
            state = 0
            pass
        else: 
            pass

    if s == "1":
        if state == 0: 
            k_count += 1            
            
            state = 1

            new_count = [s_in, 0, -1]
            counts.append(new_count)

        elif state == 2: 
            new_count = [s_in, 0, -1]
            counts.append(new_count)
            state = 1
        else: 
            pass


if True:
    s_in += 1
    end_i += 1
    
    len_ = s_in - counts[end_i][0]
    end_lens.append(len_)
    save_flag = False

if len(end_lens) == 0:
    out = len(S)
else:
    out = max(end_lens)

print(out)

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

prev = S[0]
if prev == "0":
  fst_is_0 = True
else:
  fst_is_0 = False

cnt = 0
cnt_0 = 0

cnt_lst = []
for idx, s in enumerate(S):
  if s == prev:
    cnt += 1
  else:
    cnt_lst.append(cnt)
    cnt = 1
    if s == "0":
      cnt_0 += 1
    prev = s
else:
  cnt_lst.append(cnt)



if cnt_0 <= K:
  print(len(S)-2)
else:
  prev = "1"
  cnt_lst = [0]
  for idx,s in enumerate(S):
    if prev != s:
      cnt_lst.append(idx)
    prev=s
  cnt_lst.append(len(S))
  s_ =[]
  for idx in range( int( (len(cnt_lst)+1)/2) -K):
    
    if idx==0 or idx==int( (len(cnt_lst)+1)/2) -K-1:
      s_.append(cnt_lst[2*idx+2*K+1] -cnt_lst[idx*2]-1)
    else:
      s_.append(cnt_lst[2*idx+2*K+1] -cnt_lst[idx*2])
  print(max(s_))

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

series = []
prev = "
for i, c in enumerate(s):
    if prev != c:
        series.append(i) 
        prev = c

ans = 0
if s[0] == "1":
    for i in range(0, len(series) -1 -2*k, 2):
        ans = max(ans, series[i + 2*k + 1] - series[i])
    ans = max(ans, series[min(k*2+1, len(series)-1)])
else:
    for i in range(1, len(series) -1 -2*k, 2):
        ans = max(ans, series[i + 2*k + 1] - series[i])
    ans = max(ans, series[min(k*2, len(series)-1)])
if s[-2] == "0":
    ans = max(ans, series[-1] - series[max(0, len(series)-1-2*k)])
print(ans)

Solution (Python):
from sys import exit
from queue import Queue

q = Queue()

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

S = input()

ans_list = []

delta = 0

Flag = False

buffer = 0

for i in range(len(S)):
    if S[i] == '0' and not Flag:
        Flag = True
        buffer = i
        if K == 0:
            ans_list.append(i - delta)
    if S[i] == '1' and Flag:
        if K > 0:
            K -= 1
            q.put((buffer, i - 1))
        else:
            delta = q.get()[1] + 1
            q.put((buffer, i - 1))
        Flag = False
if K > 0:
    print(len(S))
    exit()

if S[len(S) - 1] == '0':
    delta = q.get()[1] + 1
    ans_list.append(len(S) - delta)

if S[len(S) - 1] == '1':
    ans_list.append(len(S) - delta)

print(max(ans_list))

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


memo = []
cnt = 0
for i in range(len(S)):
    if S[i] == "1":
        if cnt > 0:
            memo.append([i-cnt,cnt])
        cnt = 0
    else:
        cnt += 1
if cnt > 0:
    memo.append([i+1-cnt,cnt])


import sys
if K >= len(memo):
    print(len(S))
    sys.exit()

max = 0
for i in range(len(memo) - K + 1):
    if i == 0:
        max = memo[K][0]
    elif i == (len(memo) - K):
        if max < len(S) - (memo[i-1][0] + memo[i-1][1]):
            max = len(S) - (memo[i-1][0] + memo[i-1][1])
    else:
        if max < memo[i + K][0] - (memo[i-1][0] + memo[i-1][1]):
            max = memo[i + K][0] - (memo[i-1][0] + memo[i-1][1])
print(max)

Solution (Python):
N,K = map(int,input().split())
s = input()
A = [0,0]
zerodig = 0
for i in range(N-1):
    if s[i] != s[i+1]:
        A.append(i+1)
        if (s[i] == "0" and s[i+1] == "1"):
            zerodig += 1
if s[N-1] == "0":
    zerodig += 1

A.append(len(s))
A.append(len(s))

if s[0] == "0":
    check = 0
else:
    check = 1

width = 2 * K + 1
score = 0

if zerodig <= K:
    print(len(s))
else:
    for i in range(len(A)-width):
        if score <= A[i+width] - A[i] and (i+check) % 2 == 0:
            score = A[i+width] - A[i]
    print(score)

Solution (Python):


import sys

n, k = [int(x) for x in input().split()]
ss = [int(x) for x in input()]

zero_array = []
one_array = []
curr_num = ss[0]
flag = 0

if curr_num == 0:
    zero_array.append(0)
else:
    one_array.append(0)
    flag = 1

for i in ss:
    if curr_num == i:
        if i == 0:
            zero_array[-1] += 1
        else:
            one_array[-1] += 1
    else:
        curr_num = i
        if i == 0:
            zero_array.append(1)
        else:
            one_array.append(1)





if k >= len(zero_array):
    print(sum(zero_array) + sum(one_array))
    sys.exit()
else:
    t = 0
    tmp = sum(zero_array[t: t+k]) + sum(one_array[t: t+k+flag])
    max_count = tmp
    
    
    while t + k + flag <= len(one_array) and t + k < len(zero_array):
        
        tmp += zero_array[t+k] - zero_array[t]
        if t+k+flag < len(one_array):
            tmp += one_array[t+k+flag]
            if not (t == 0 and flag == 0):
                if t == 0:
                    pass
                    
                tmp -= one_array[t+flag-1]
        elif not (t == 0 and flag == 0):
            tmp -= one_array[t+flag-1]
        
        if tmp > max_count:
            max_count = tmp
            
        t += 1

    print(max_count)

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

cnt = 0
for i in range(n):
    if s[i] == "1" and cnt == 0:
        ans_0.append(i)
        cnt += 1
    elif s[i] == "0":
        cnt = 0
cnt = 0
for i in range(n):
    if s[n-1 - i] == "1" and cnt == 0:
        ans_1.append(n-1-i)
        cnt += 1
    elif s[n-1 - i] == "0":
        cnt = 0
ans_1.sort()
ans_list = []
if len(ans_0) == 0:
    print(n)
elif len(ans_0) == 1:
    if k == 1:
        print(max(n-ans_0[0], ans_1[0]+1))
    else:
        print(n)
elif len(ans_0) < k:
    print(n)
else:
    if 0 not in ans_0:
        ans_0.append(0)
        ans_1.append(0)
        ans_0.sort()
        ans_1.sort()
    if n-1 not in ans_1:
        ans_0.append(n-1)
        ans_1.append(n-1)
    for i in range(len(ans_0) - k):
        ans_list.append(ans_1[i+k] - ans_0[i])
    print(max(ans_list) + 1)

Solution (Python):
import numpy as np

N, K = map(int, input().split(" "))
S = np.array([int(i) for i in input()])
pos1 = np.linspace(0,N-1,N)[S == 1]
lst = []
if len(pos1) == 0:
    print(N)
else:    
    st =[pos1[0]]
    if len(pos1) != 1:
        for i in pos1[1:]:
            if st[-1] + 1 == i:
                st.append(i)
            else:
                lst.append(st)
                st = [i]
            if i == pos1[-1]:
                lst.append(st)
    else:
        lst.append(st)
    lst = [[0]] + lst + [[N-1]]
    time = len(lst) - K
    if time <= 0:
        print(N)
    else:
        print(int(max([max(lst[i+K]) - min(lst[i])+1 for i in range(time)])))

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

def get_sequence(arr):
    if len(arr) == 0:
        return []
    result = []
    n = 1
    prev = arr[0]
    for c in arr[1:]:
        if c == prev:
            n += 1
        else:
            result.append([prev,n])
            prev = c
            n = 1
    result.append([prev,n])
    return result

seq = get_sequence(S)
lengths = [ x[1] for x in seq ]
if seq[0][0] == '0':
    lengths.insert(0,0)
if seq[-1][0] == '0':
    lengths.append(0)

L = (len(lengths)-1)//2
if L <= K:
    print(N)
else:
    ans = sum(lengths[:2*K+1])
    now = ans
    for i in range(L-K):
        now += sum(lengths[2*(K+i)+1:2*(K+i)+3])-sum(lengths[2*i:2*(i+1)])
        ans = max(ans,now)
    print(ans)

Solution (Python):




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

bgnl.append(0)
endr.append(0)

seqflg = False


for i in range(n):
    if s[i] == "0":
        if seqflg == True and i > 0:
            endr.append(i-1)
            seqflg = False
    elif s[i] == "1" and seqflg == False:
        bgnl.append(i)
        seqflg = True

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

bgnl.append(n-1)
endr.append(n-1)

max = 0
if len(bgnl) >= k:
    for j in range(0, len(bgnl)-k):
        pcnt = endr[j+k] - bgnl[j] + 1



        if max < pcnt:
            max = pcnt
    ans = max

else:
    ans = n
    




print(ans)

Solution (Python):
import re

def norm(s):
    s1 = list(map(len, re.split('0+', s)))
    s0 = list(map(len, re.split('1+', s)))
    if s0[0] == 0:
        s0.pop(0)
    if s0[-1] == 0:
        s0.pop(-1)
    return [s0,s1]

def solve(n, k, s):
    s0, s1 = norm(s)

    if len(s1) == 1:
        return(s1[0])

    if len(s0) <= k:
        return n
    
    s0_sum = sum(s0[0:k])
    s1_sum = sum(s1[0:k+1])

    v = s0_sum + s1_sum
    mv = v
    
    for last_idx in range(0, len(s0) - k):
        
        v = (v - s0[last_idx] + s0[last_idx + k] -
             s1[last_idx] + s1[last_idx + k + 1])
        if mv < v:
            mv = v

    return mv


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

Solution (Python):
import numpy as np


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


arr = np.array([0] + s_arr.tolist() + [0])
diff = np.diff(arr)
heads = np.where(diff == 1)[0]
tails = np.where(diff == -1)[0]
assert heads.size == tails.size

if s_arr[0] == 0:
    heads = np.array([0] + heads.tolist())
    tails = np.array([0] + tails.tolist())

if s_arr[-1] == 0:
    heads = np.array(heads.tolist() + [len(s_arr)])
    tails = np.array(tails.tolist() + [len(s_arr)])

ans = 0
for i, head in enumerate(heads):
    idx = min(i + K, len(tails) - 1)
    tail = tails[idx]
    ans = max(ans, tail - head)

print(ans)

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

b = '1'

l = []
c = 0
for s in S:
	if b == s:
		c = c + 1
	else:
		l.append(c)
		c = 1
		b = '1' if b == '0' else '0'

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

n = len(l)//2
if n <= K:
	print(N)
	exit()

size = len(l)
cand = []
k = 2*K+1
cd = sum(l[0:k])
cand.append(cd)
for i in range(0,size-k,2):
	cd = cd - l[i]-l[i+1] + l[i+k] + l[i+k+1]
	cand.append(cd)
print(max(cand))

Solution (Python):
N, K = map(int, input().split())
S = input()
T = []
if S[0] == '0': T.append(0)
cnt = 1
for i in range(1, N):
    if S[i - 1] == S[i]:
        cnt += 1
    else:
        T.append(cnt)
        cnt = 1
if cnt > 0: T.append(cnt)
if S[-1] == '0': T.append(0)
cs = [0] * (len(T) + 1)
n = 0
for i in range(1, len(T) + 1):
    n += T[i - 1]
    cs[i] = n

n = min(K * 2 + 1, len(T))
ans = 0
for i in range(1, len(T) - n + 2, 2):
    t = cs[i + n - 1] - cs[i - 1]
    if ans < t: ans = t
print(ans)

Solution (Python):
import math

N,K=map(int,input().split())
S=input()
L=[]
seq=0
prev='2'
for i in S:
  if i == prev:
    seq += 1
  else:
    L.append(seq)
    prev = i
    seq = 1
L.append(seq)
L.append(0)

L_cum = [0]*(len(L)+1)
for i in range(len(L)):
  L_cum[i]=L_cum[i-1]+L[i]

if S[0] == '0':
  num_0 = math.ceil((len(L)-2)/2)
  if num_0<=K:
    ans=N
  else:
    rev=[0]*(num_0-K+1)
    for i in range(num_0-K+1):
      rev[i]=L_cum[2*(K+i)]-L_cum[i*2-1]
    ans=max(rev)
else:
  num_0 = math.floor((len(L)-2)/2)
  if num_0<=K:
    ans=N
  else:
    rev=[0]*(num_0-K+1)
    for i in range(num_0-K+1):
      rev[i]=L_cum[2*(K+i)+1]-L_cum[2*i]
    ans=max(rev)
print(ans)

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

Solution (Python):
N, K = map(int, input().split())
S = input()
split_list = [0]
section_list = [0]
for i in range(1, N):
    if S[i-1] != S[i]:
        section_list.append(i - split_list[-1])
        split_list.append(i)
split_list.append(N)
section_list.append(split_list[-1] - split_list[-2])
del split_list[0]
del section_list[0]
answer = 0
if len(section_list) <= 2 * K:
    answer= N
else:
    total = sum(section_list[:2 * K + 1])
    if S[0] == '1':
        answer = total
    for i in range(len(section_list) - 2 * K - 1):
        total = total - section_list[i] + section_list[i+2*K+1]
        if (S[0] == '0') & (i % 2 == 0):
            if total >= answer:
                answer = total
        if (S[0] == '1') & (i % 2 == 1):
            if total >= answer:
                answer = total
    
    total = sum(section_list[:2 * K])
    if total > answer:
        answer = total
    
    total = sum(section_list[- 2 * K:])
    if total > answer:
        answer = total
print(answer)

Solution (Python):
from itertools import groupby


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



val_list = []
id_list = []
count = 0
for k, g in groupby(S):
    id_list.append(count)
    val_list.append(k)
    lg = len(list(g))
    count += lg
id_list.append(count)

num_sec = len(id_list)
if num_sec == 1:
    print(N)
else:
    m = 0
    end = 0
    for i in range(num_sec):
        if end == num_sec - 1:
            break
        if val_list[i] == '1':
            
            end = min(i+2*K+1, num_sec - 1)
            v = id_list[end] - id_list[i]
            
            m = max(m, v)
        elif i == 0:
            
            end = min(i+2*K, num_sec - 1)
            v = id_list[end] - id_list[i]
            
            m = max(m, v)
    print(m)

Solution (Python):
n, k = [int(_) for _ in input().split(' ')]
s = '0' + input() + '0'


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

state = ''
for i in range(n + 1):
    if s[i] == '0' and s[i + 1] == '1':
        state = i + 1
    elif s[i] == '1' and s[i + 1] == '0':
        a.append((state, i))

if s[n] == '0':
    a.append((n + 1, n))

l = len(a)
if k <= l - 1:
    print(max([a[i + k][1] - a[i][0] + 1 for i in range(l - k)]))
else:
    print(n)

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

a = []
b = []

index = 0
length = 0
judge = "0"
for i in range(len(s)):
	if s[i] == judge:
		length += 1
	if s[i] != judge:
		if judge == "0":
			b.append(length)
		if judge == "1":
			a.append(length)
		length = 1
		judge = str((int(judge)+1)%2)
if judge == "0":
	b.append(length)
	a.append(0)
if judge == "1":
	a.append(length)


M = len(a)
if K >= M:
	print(len(s))
	exit()

ans = 0
ans_new = 0
for i in range(K):
	ans += a[i]+b[i]
ans_max = ans
for i in range(M-K):
	if i == 0:
		ans_new = ans-b[0]+b[K]+a[K]
	if i != 0:
		ans_new = ans-a[i-1]-b[i]+b[K+i]+a[K+i]
	ans_max = max(ans_max,ans_new)
	ans = ans_new
print(ans_max)

Solution (Python):

n, k = map(int, input().split())
S = str(input())

flag = '0'
ans = []
for i, s in enumerate(S):
    if s == '1':
        if flag == '0':
            tmp = [0]*2
            tmp[0] = i+1
            flag = '1s'
        if flag == '1s' and i == n-1:
            tmp[1] = i+1
            ans.append(tmp)
    else:
        if flag == '1s':
            tmp[1] = i
            ans.append(tmp)
            flag = '0'
if S[0] == '0':
    ans.insert(0, [1, 1])
if S[n-1] == '0':
    ans.append([n, n])

anser = []
if k >= len(ans):
    print(n)
else:
    for i in range(len(ans)):
        if i+k > len(ans)-1:
            break
        else:
            anser.append(ans[i+k][1]-ans[i][0]+1)
    print(max(anser))

Solution (Python):
def solve():
  n,k = (int(i) for i in input().split())
  s = input()
  one = []
  zero = []  
  for i in range(n):
    if i == 0:
      if s[i] == "0":
        one.append(0)
        zero.append(1)
      else:
        one.append(1)
    
    else:     
     if s[i] == s[i-1]:
      if s[i] == "0":
        zero[-1]+= 1
      else:
        one[-1] += 1
     else:
      if s[i] == "0":
        zero.append(1)
      else:
        one.append(1)
  ans = 0 
  if len(zero) <= k:
    ans = n

  else:
   one = one + [0]*5
   zero = zero + [0]*5
   tmp = 0
   for i in range(k+1):
    tmp += one[i]
   for i in range(k):
    tmp += zero[i]
   ans = max(tmp,ans)
   for i in range(len(zero)-k-1):
        
       tmp -=  one[i]
       tmp +=  one[i+k+1]
       tmp -=  zero[i]
       tmp +=  zero[i+k]
       ans = max(ans,tmp)

  
 
  print(ans)
    
solve()

Solution (Python):
n, k = map(int, input().split())
S = list(map(int, input()))
T = []    
if S[0] == 1:
    T.append(1)
else:
    T.append(0)
    T.append(-1)
for i in range(1, n):
    if S[i] == S[i-1] and S[i] == 1:
        T[-1] += 1
    elif S[i] == S[i-1]:
        T[-1] -= 1
    elif S[i] == 1:
        T.append(1)
    else:
        T.append(-1)
if T[-1] < 0:
    T.append(0)
m = len(T)

ans = []
ans.append(T[0])
for i in range(0, k):
    if 2*i+2 < m:
        ans[0] = ans[0] - T[2*i+1] + T[2*i+2]
    else:
        break

i = 0
j = k+1
while True:
    if 2*j >= m:
        break
    re = ans[-1] - T[2*i] + T[2*i+1] - T[2*j-1] + T[2*j]
    i += 1
    j += 1
    ans.append(re)
    
    
print(max(ans))

Solution (Python):
N,K = map(int,input().split())
S = input()
L = []
cur = S[0]
cnt = 0
if S[len(S)-1] == '1':
  S += '0'
else:
  S += '1'
for i in range(len(S)):
  if S[i] == cur:
    cnt += 1
  else:
    L.append(cnt)
    cnt = 1
    cur = S[i]
ruiseki = [L[0]]
for i in range(len(L)-1):
  ruiseki.append(ruiseki[i]+L[i+1])
ruiseki.insert(0,0)
if len(ruiseki) == 2:
  print(len(S)-1)
else:
  if S[0] == '1' and S[len(S)-2] == '1':
    if (len(L)-1)/2 > K:
      ans = 0
      for i in range(len(ruiseki)-2*K-1):
        if i%2 == 0:
          if ans <= ruiseki[i+2*K+1]-ruiseki[i]:
            ans = ruiseki[i+2*K+1]-ruiseki[i]
      print(ans)
    else:
      print(len(S)-1)
  elif S[0] == '1' and S[len(S)-2] == '0':
    if len(L)/2 > K:
      ans = 0
      for i in range(len(ruiseki)-2*K-2):
        if i%2 == 0:
          if ans <= ruiseki[i+2*K+1]-ruiseki[i]:
            ans = ruiseki[i+2*K+1]-ruiseki[i]
      print(max(ans,ruiseki[len(ruiseki)-1]-ruiseki[len(ruiseki)-1-2*K]))
    else:
      print(len(S)-1)
  elif S[0] == '0' and S[len(S)-2] == '1':
    if len(L)/2 > K:
      ans = 0
      for i in range(1,len(ruiseki)-2*K):
        if i%2 != 0:
          if ans <= ruiseki[i+2*K+1]-ruiseki[i]:
            ans = ruiseki[i+2*K+1]-ruiseki[i]
      print(max(ans,ruiseki[2*K]-ruiseki[0]))
    else:
      print(len(S)-1)
  else:
    ans = 0
    if (len(L)+1)/2 > K:
      for i in range(len(ruiseki)-2*K-3):
        if i%2 != 0:
          if ans <= ruiseki[i+2*K+1]-ruiseki[i]:
            ans = ruiseki[i+2*K+1]-ruiseki[i]
      print(max(ans,ruiseki[2*K]-ruiseki[0],ruiseki[len(ruiseki)-1]-ruiseki[len(ruiseki)-1-2*K]))
    else:
      print(len(S)-1)

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

cnt = 0
c0 = '1'
for c in s:
    if c == c0:
        cnt += 1
    else:
        x.append(cnt)
        c0 = c
        cnt = 1
else:
    if cnt != 0:
        x.append(cnt)

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

if len(x) <= k*2+1:
    print(sum(x))
else:
    y = sum(x[0:k*2+1])
    ans = y
    for i in range(2,len(x)-k*2,2):
        y = y - sum(x[i-2:i]) + sum(x[i+2*k-1:i+2*k+1])
        if ans < y:
            ans = y
    print(ans)

Solution (Python):
N, K = map(int, input().split())
S = list(input())
current = S[0]
L0 = []
L1 = []
Lall = []
stack = 0
for i in range(N):
    if current == S[i]:
        stack += 1
    else:
        if current == '0':
            L0.append(stack)
        else:
            L1.append(stack)
        Lall.append(stack)
        stack = 1
        current = S[i]
if current == '0':
    L0.append(stack)
else:
    L1.append(stack)
Lall.append(stack)
len0 = len(L0)
len1 = len(L1)
if len0 == 0:
    print(L1[0])
elif len1 == 0:
    if K != 0:
        print(L0[0])
    else:
        print(0)
elif len0 <= K:
    print(len(S))
else:
    L = [0]
    for i in range(len(Lall)):
        L.append(L[-1] + Lall[i])
    if len0 < len1:
        ans = 0
        for k in range(len0-K+1):
            ans = max(ans, L[2*k + 2*K + 1]-L[2*k])
    elif len0 > len1:
        ans = max(L[2*K]-L[0], L[-1]-L[-1-2*K])
        for k in range(len0-K-1):
            ans = max(ans, L[2*k + 2*K + 2]-L[2*k + 1])
    elif S[0] == '0':
        ans = L[2*K]-L[0]
        for k in range(len0 - K):
            ans = max(ans, L[2*k + 2*K + 2]-L[2*k + 1])
    else:
        ans = L[-1]-L[-1-2*K]
        for k in range(len0 - K):
            ans = max(ans, L[2 * k + 2 * K + 1] - L[2 * k])
    print(ans)

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

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

n0 = (len(seq) - 1) // 2
if n0 <= K:
    ans = N
else:
    total = [0]
    for s in seq:
        total.append(total[-1] + int(s))

    ans = 0
    for i in range(n0 - K + 1):
        ans = max(ans, total[(i + K) * 2 + 1] - total[i * 2])

print(ans)

Solution (Python):
from collections import deque
N,K=map(int,input().split())
S=input()
zeros=deque()
ichistart=0
zeros.append([-1,-1])
streak=0
hajime=0
for k,i in enumerate(S):
    if i=="0":
        if streak==0:
            hajime=k
        streak+=1
    elif streak!=0:
        owari=hajime+streak-1
        streak=0
        zeros.append([hajime,owari])
if streak!=0:
    zeros.append([hajime,N-1])
zeros.append([N,N])
curmax=0
done=0
z=len(zeros)
if z<=K+1:
    print(N)
    done=1
if not done:
    for n,[s,f] in enumerate(zeros):
        if n+K+1<z:
            curmax=max(curmax,zeros[n+K+1][0]-f-1)
    print(curmax)

Solution (Python):
N, K = map(int, input().split())
S = input()
split_list = [0]
section_list = [0]
for i in range(1, N):
    if S[i-1] != S[i]:
        section_list.append(i - split_list[-1])
        split_list.append(i)
split_list.append(N)
section_list.append(split_list[-1] - split_list[-2])
del split_list[0]
del section_list[0]
answer = 0
if len(section_list) <= 2 * K:
    answer= N
else:
    total = sum(section_list[:2 * K + 1])
    if S[0] == '0':
        for i in range(len(section_list) - 2 * K - 1):
            total = total - section_list[i] + section_list[i+2*K+1]
            if i % 2 == 0:
                if total >= answer:
                    answer = total
    if S[0] == '1':
        answer = total
        for i in range(len(section_list) - 2 * K - 1):
            total = total - section_list[i] + section_list[i+2*K+1]
            if i % 2 == 1:
                if total >= answer:
                    answer = total
    
    total = sum(section_list[:2 * K])
    if total > answer:
        answer = total
    
    total = sum(section_list[- 2 * K:])
    if total > answer:
        answer = total
print(answer)

Solution (Python):
import sys
input = sys.stdin.readline
N, K = map(int, input().split())
S = input()
c0, c1 = [], []
if S[0] == '1':
    c0.append(0)
i = 0
while i < N:
    p = S[i]
    c = 1
    i += 1
    while i < N and S[i] == p:
        i += 1
        c += 1
    if p == '0':
        c0.append(c)
    else:
        c1.append(c)
if S[N-1] == '0':
    c1.append(0)
l0 = len(c0)
if l0 < K:
    K = l0
ans = sum(c0[0:K]) + sum(c1[0:K])
for i in range(1, l0):
    c0[i] += c0[i-1]
    c1[i] += c1[i-1]
l = l0 - K + 1
if l0 == 2 and S[0] == '1':
    ans = N
else:
    for i in range(1, l):
        tmp = c0[i+K-1] - c0[i-1] + c1[i+K-1]
        if i - 2 >= 0:
            tmp -= c1[i-2]
        if ans < tmp:
            ans = tmp
print(ans)