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 int_raw():
    return int(input())


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


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


N, K = ints_raw()
S = [int(s) for s in raw_input()]
current = S[0]
count = 0
stand_array = []
reverse_array = []
if current == 0:
    reverse_array.append(0)
for s in S:
    if s == current:
        count += 1
    else:
        if current == 1:
            reverse_array.append(count)
        else:
            stand_array.append(count)
        count = 1
        current = 1 - current
if current == 1:
    reverse_array.append(count)
else:
    stand_array.append(count)
if current == 0:
    reverse_array.append(0)

if len(stand_array) <= K:
    print(N)
else:
    max_score = 0
    score = 0
    for i in range(K):
        score += stand_array[i]
        score += reverse_array[i]
    score += reverse_array[K]
    max_score = score
    for i in range(len(stand_array) - K):
        score -= stand_array[i]
        score -= reverse_array[i]
        score += stand_array[i + K]
        score += reverse_array[i + K + 1]
        max_score = max(score, max_score)
    print(max_score)

Solution (Python):
n,k = map(int,input().split())
s = str(input())
num = 0
nu = 0
lis = []
li = []
if s[0] == "0":
    li.append(0)
for i in range(len(s)):
    if s[i] == "0":
        if nu != 0:
            num = 1
            li.append(nu)
            nu = 0
        else:
            num += 1
    else:
        if num != 0:
            nu = 1
            lis.append(num)
            num = 0
        else:
            nu += 1
lis.append(num)
li.append(nu)
if len(lis) == len(li):
    li.append(0)
cou = 0
nn = max(0,k-len(lis))
for i in range(nn):
    lis.append(0)
    li.append(0)
ans = sum(lis[:k])+sum(li[:k+1])
num = sum(lis[:k])+sum(li[:k+1])
for i in range(len(lis)-k):
    num -= li[i]+lis[i]
    num += li[i+k+1]+lis[i+k]
    ans = max(ans,num)
print(ans)

Solution (Python):
N,K=map(int,input().split())
S=input()
start=[0]
end=[]
bef=S[0]
if bef=='0':
    end.append(0)
for i in range(1,N):
    if S[i]!=bef:
        if bef=='0':
            start.append(i)
        else:
            end.append(i-1)
        bef=S[i]
if S[-1]=='1':
    end.append(N-1)
ans=0
for i in range(len(end)):
    if i-K<0:
        x=0
    else:
        x=start[i-K]
    ans=max(ans,end[i]-x+1)
if S[-1]=='0' and K!=0:
    if len(start)-K<0:
        x=0
    else:
        x=start[-1*K]
    ans=max(ans,N-x)
print(ans)

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

x = []
idx = 0
b = 1
while idx<n:
    st = idx
    while idx<n and int(s[idx])==b:
        idx += 1
    x.append(idx-st)
    st = idx
    b ^= 1

if b == 1:
    x.append(0)

ans = n
if len(x) > 2*k+1:
    ans = 0

    t = 0
    for i in range(2*k+1):
        t += x[i]
    ans = max(ans, t)

    for i in range(2*k+1, len(x), 2):
        t -= x[i - (2*k+1)]
        t -= x[i+1 - (2*k+1)]
        t += x[i]
        t += x[i+1]
        ans = max(ans, t)

print(ans)

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

start = s[0]
arr = []
pre = start
cnt = 0
for x in s:
    if x == pre:
        cnt+=1
        continue
    else:
        pre = x
        arr.append(cnt)
        cnt = 1

arr.append(cnt)

if len(arr) == 1:
    print(arr[0])
    exit()

m = 0
if start == "1":
    now = sum(arr[:2*k+1])
    m = now
    for i in range(1, (len(arr)//2)-k+1):
        now = now - arr[2*i-1] - arr[2*i-2] + sum(arr[2*i+2*k-1:2*i+2*k+1]) 
        m = max(now, m)

else:
    m = sum(arr[:2*k])
    now = sum(arr[1:2*k+2])
    m = max(now, m)
    for i in range(1, ((len(arr)+1)//2)-k):
        now = now - arr[2*i] - arr[2*i-1] + sum(arr[2*i+2*k:2*i+2*k+2])
        m = max(now, m)
    
print(m)

Solution (Python):
N,K = map(int,input().split())
S = input()
x = [0]
hoge = 1
for i in S:
    i = int(i)
    if i == hoge:
        x[-1] += 1
    else:
        hoge = (hoge+1)%2
        x.append(1)
x.append(0)
l = len(x)
hoge = 2*K+1
saka = [0]
if hoge > l:
    print(N)
else:
    for i in range(hoge):
        saka[0] += x[i]
    for i in range(int((l-hoge)/2)):
        saka.append(saka[i]-x[i*2]-x[i*2+1]+x[i*2+hoge]+x[i*2+1+hoge])
    print(max(saka))

Solution (Python):
N,K=map(int,input().split())
S=input()
L=[]
c=0
for i in range(N):
  if c==0 and S[i]=="1":
    c=1
    s=i
  elif c==1 and S[i]=="0":
    c=0
    t=i-1
    L.append([s,t])
if c==1:
  L.append([s,N-1])
if len(L)<=K-1:
  ans=N
elif len(L)==K:
  if L[0][0]!=0 and L[-1][1]!=N-1:
    ans=max(L[-1][1]+1,N-L[0][0])
  else:
    ans=N
else:
  ans=0
  for i in range(len(L)-K):
    ans=max(ans,L[i+K][1]-L[i][0]+1)
  if L[0][0]!=0:
    ans=max(ans,L[K-1][1]+1)
  if L[-1][1]!=N-1:
    ans=max(ans,N-L[-K][0])
print(ans)

Solution (Python):
N, K = map(int, input().split())
S = '1' + input() + '1'
s = 0
t = 0
k = 0
l = 0
while s < N:
  while s < N:
    s += 1
    if S[s] == '0' and S[s + 1] == '1':
      k += 1
    if k == K and S[s] == '1' and S[s + 1] == '0':
      break
  l = max(s - t, l)
  while t < s:
    t += 1
    if S[t] == '0' and S[t + 1] == '1':
      k -= 1
      break
print(l)

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


def main():
    n,k = map(int,input().split())
    s = list(input())
    tank = []
    judge = True
    if s[0] == '0':
        judge = False
    cnt = 0
    for i in range(n):
        if judge:
            if s[i] == '0':
                tank.append(cnt)
                judge = False
                cnt = 0
            cnt += 1
        else:
            if s[i] == '1':
                tank.append(cnt)
                judge = True
                cnt = 0
            cnt += 1
        if i == n-1:
            tank.append(cnt)
    
    p = len(tank)
    if s[0] == '0' and (p+1)//2 <= k:
        print(n)
        exit()
    if p//2 <= k and s[0] == '1':
        print(n)
        exit()

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

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

    print(ans)

if __name__ == '__main__':
    main()

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

ones = []
zeros = []
current = '1'
n = 0
for i in range(N):
    if S[i] == current:
        n += 1
    else:
        if current == '1':
            ones.append(n)
            current = '0'
        else:
            zeros.append(n)
            current = '1'
        n = 1
if current == '1':
    ones.append(n)
else:
    zeros.append(n)
    ones.append(0)

L = len(zeros)
if L <= K:
    print(N)
else:
    ans = sum(ones[:K+1]) + sum(zeros[:K])
    now = ans
    for i in range(L-K):
        now += (ones[i+K+1]+zeros[i+K]-ones[i]-zeros[i])
        ans = max(ans,now)
    print(ans)

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

runlengths = []  
if S[0] == "0":
    runlengths.append(0)

pre_s = S[0]
cnt = 1
for s in S[1:]:
    if pre_s != s:
        runlengths.append(cnt)
        pre_s = s
        cnt = 1
    else:
        cnt += 1

runlengths.append(cnt)
if S[-1] == "0":
    runlengths.append(0)

if len(runlengths) < K * 2 + 1:
    runlengths.extend([0]*((K*2+1)-len(runlengths)))




con = sum(runlengths[0:K*2+1])
max_con = con
for e_new in range(K*2+2, len(runlengths), 2):
    s_old = e_new - (K*2+2)
    con = con - runlengths[s_old] - runlengths[s_old+1] +\
        runlengths[e_new-1] + runlengths[e_new]
    max_con = max(max_con, con)

print(max_con)

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

L = [[0, 0]]
re = S[0]
for i in range(1, len(S)):
  if S[i] != re:
    re = S[i]
    L[-1][1] = i
    L.append([i, 0])
l = len(L)
L[-1][1] = N

if S[0] == "0":
  z = (l // 2) + 1
else:
  z = (l + 1) // 2
  
if z <= K:
  print(N)
  exit()

ma = 0
if S[0] == "0":
  ma = max(ma, L[K * 2 - 1][1])
  s = 1
else:
  s = 0
if S[-1] == "0":
  ma = max(ma, N - L[-K * 2][0])

t = K * 2
for i in range(s, l - t, 2):
  ma = max(ma, L[t + i][1] - L[i][0])
print(ma)

Solution (Python):


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

neg = [0]
negSum = 0
pos = [0]
posSum = 0
value = 1
for i in range(N):
    c =S[i]
    if c == "1":
        posSum += 1
        if value == 0:
            neg.append(negSum)
            value = 1
    else:
        negSum += 1
        if value == 1:
            pos.append(posSum)
            value = 0
if value == 1:
    pos.append(posSum)
else:
    neg.append(negSum)
    pos.append(posSum)


L = len(neg)

ans = pos[1]
for i in range(1,L):
    endI = min(L-1,i+K-1)
    
    candidate = neg[endI] - neg[i-1] + pos[endI+1] - pos[i-1]
    ans = max(ans,candidate)
print(ans)

Solution (Python):
n,k=map(int, input().split())
s=input()
b=[0]
for i in range(n-1):
    if s[i]!=s[i+1]:
        b.append(i+1)
b.append(len(s))
if s[0]=='0':
    b.insert(0,0)
if s[-1]=='0':
    b.append(len(s))
ans=0
stride=k*2+1
if len(b)-stride<1:
    stride=len(b)-1
for i in range(0,len(b)-stride,2):
    ans=max(ans,b[i+stride]-b[i])
print(ans)

Solution (Python):
n,k=map(int,input().split())
S=input()
One,Zero=[],[]
cnt1,cnt0=0,0
for s in S:
    if s=='1':
        cnt1+=1
        if cnt0:
            Zero.append(cnt0)
            cnt0=0
    else:
        cnt0+=1
        if cnt1:
            One.append(cnt1)
            cnt1=0
if cnt1:
    One.append(cnt1)
if cnt0:
    Zero.append(cnt0)
if S[0]=='0':
    One=[0]+One
if S[-1]=='0':
    One=One+[0]
l=len(Zero)
if k>=l:
    ans=n
else:
    ans=sum(One[:k+1]+Zero[:k])
    prev=ans
    for i in range(l-k):
        tmp=prev-One[i]+One[i+k+1]-Zero[i]+Zero[i+k]
        ans=max(ans,tmp)
        prev=tmp
print(ans)

Solution (Python):
import sys
from collections import deque
import copy
import math
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline



def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N, K] = [int(input_raw[0]), int(input_raw[1])]
    input_raw = read_func().strip().split()
    S = input_raw[0]
    count_list = []
    s_list = []
    j = 0
    count = 1
    for i in range(1, N):
        if S[i] == S[i - 1]:
            count += 1
        else:
            count_list.append(count)
            s_list.append(S[i - 1])
            count = 1
    count_list.append(count)
    s_list.append(S[N - 1])
    n = len(count_list)
    count = 1
    sum_list = [0 for i in range(n + 1)]
    for i in range(1, n + 1):
        sum_list[i] = sum_list[i - 1] + count_list[i - 1]

    max_s = 0
    for i in range(1, n + 1):
        if s_list[i - 1] == "0":
            k = 2 * K
        else:
            k = 2 * K + 1
        max_s = max(sum_list[i] - sum_list[max(i - k , 0)], max_s)
    print max_s








if __name__ == '__main__':
    main()

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

if S[0] =='0':
  pre = '0'
  memo = {'0': [1], '1':[0]}
else:
  pre = '1'
  memo = {'0': [], '1':[1]}

for s in S[1:]:
  if s==pre:
    memo[s][-1] +=1
  else:
    pre = s
    memo[s].append(1)

if S[-1] =='0':
  memo['1'].append(0)
    
if len(memo['0'])<=K:
  print(len(S))
  exit()

window_sum = sum(memo['0'][:K] + memo['1'][:K+1])
answer = window_sum
for i in range(0, len(memo['0'])-K):
  window_sum = window_sum -memo['0'][i] - memo['1'][i] + memo['0'][i+K] + memo['1'][i+K+1]
  answer = max(answer, window_sum)

print(answer)

Solution (Python):

def inpl(): return list(map(int, input().split()))
N, K = inpl()
S = input()

x = S[0]
A = []
B = []
tmp = 0
for s in S:
    if s == x:
        tmp += 1
    else:
        if x == "0":
            A.append(tmp)
        else:
            B.append(tmp)
        x = s
        tmp = 1

if tmp > 0:
    if s == "0":
        A.append(tmp)
    else:
        B.append(tmp)

ans = 0
if S[0] == "1":
    T = sum(A[:K]) + sum(B[:K+1])
    ans = max(ans, T)
    for i in range(len(A) - K):
        T += -A[i] + A[i+K] - B[i]
        if len(B) > i + K + 1:
            T += B[i+K+1]
        ans = max(ans, T)
    print(ans)
else:
    T = sum(A[:K]) + sum(B[:K])
    ans = max(ans, T)
    for i in range(len(A) - K):
        T += -A[i] + A[i+K] - (B[i-1] * (i!=0))
        if len(B) > i + K:
            T += B[i+K]
        ans = max(ans, T)
    print(ans)

Solution (Python):
import sys
import random
sys.setrecursionlimit(20000000)
input = sys.stdin.readline
n, k = map(int,input().split())
s = input().rstrip()
ans = []
if s[0] == "0":
    ans.append(0)
    mae = "0"
else:
    mae = "1"
b = 0
for i in range(n):
    if s[i] == mae:
        b += 1
    else:
        ans.append(b)
        if mae == "0":
            mae = "1"
        else:
            mae = "0"
        b = 1
else:
    if b:
        ans.append(b)
rui = [0]*(len(ans)+1)
for i in range(len(ans)):
    rui[i+1] = ans[i] + rui[i]
MAX = 0
if len(ans[1::2]) <= k:
    print(n)
    exit()
for i in range(len(ans)+1):
    try:
        if i %2 == 0:
            MAX = max(rui[i+2*k+1]-rui[i],MAX)
        else:
            pass
    except:
        if i %2 == 0:
            MAX = max(rui[len(ans)]-rui[i],MAX)    
print(MAX)

Solution (Python):
n,k=map(int, input().split())
s=input()
st=[0]
for i in range(1,n):
    if(s[i]!=s[i-1]):
        st.append(i)
st.append(n)
a=len(st)-1
if(s[0]=="0"):
    ans=st[min(2*k,a)]
    for i in range(1,a+1,2):
        ans=max(ans,st[min(2*k+1+i,a)]-st[i])
elif(s[0]=="1"):
    ans=0
    for i in range(0,a+1,2):
        ans=max(ans,st[min(2*k+1+i,a)]-st[i])
print(ans)

Solution (Python):

import itertools
import sys
import math
from functools import lru_cache





from queue import Queue
from operator import mul
from functools import reduce
from queue import Queue
from operator import mul
from functools import reduce
from functools import lru_cache

input = sys.stdin.readline


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

s = input() + '2'




ret = 0



def next0(l: int):
    if l >= n:
        return l
    l += 1
    while s[l] != '2' and s[l] != '0':
        l += 1
    return min(l, n)



def next1(l: int):
    if l >= n:
        return l
    l += 1
    while s[l] != '2' and s[l] != '1':
        l += 1
    return min(l, n)


start = 0
end = 0
ret = 0

end = next0(end)
for i in range(k):
    end = next1(end)
    end = next0(end)

ret = end - start


while end < n:
    start = next1(next0(start))
    end = next0(next1(end))
    ret = max(ret, end - start)
    
print(ret)

Solution (Python):
n, k = map(int, input().split())
s=list(map(int, list(input())))
tmp=s[0]
cnt=[]
c=0
for i in range(n):
    if tmp==s[i]:
        c+=1
    else:
        tmp=s[i]
        cnt.append( c)
        c=1
    if i==n-1:
        cnt.append( c)
ans=0
S=[0]*len(cnt)
S[0]=cnt[0]
for i in range(1, len(cnt)):
    S[i]=S[i-1]+cnt[i]
S=[0]+S

for i in range(len(cnt)):
    if i%2==s[0]:
        if i+k*2<=len(cnt):
            ans=max(ans, S[i+k*2]-S[i])
        else:
            ans=max(ans, S[len(cnt)]-S[i])
    else:
        if i+k*2+1<=len(cnt):
            ans=max(ans, S[i+k*2+1]-S[i])
        else:
            ans=max(ans,S[len(cnt)]-S[i])
print(ans)

Solution (Python):
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
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())

from itertools import accumulate
n,k = li()
s = ns()

prev = '0'

zeros = []
ones = []

cur = 0
for si in s:
    if si == prev:
        cur += 1
        
    else:
        if prev == '0':
            zeros.append(cur)
            prev = '1'
        else:
            ones.append(cur)
            prev = '0'
        cur = 1
        
if prev == '0':
    zeros.append(cur)
else:
    ones.append(cur)
    zeros.append(0)

if len(ones) == 0 or len(zeros) == 0:
    ans = n

else:
    ans = 0
    zero_cum = [0] + list(accumulate(zeros))
    one_cum = [0] + list(accumulate(ones))
    
    if len(zeros) <= k:
        ans = n
    
    else:
        for i in range(len(zeros)-k+1):
            tmp = (zero_cum[i+k] - zero_cum[i])\
                    + (one_cum[min(len(ones), i+k)] - one_cum[max(0, i-1)])
            ans = max(ans, tmp)
        
print(ans)

Solution (Python):
N, K = map(int, input().split())
S = input()
arr = []
prev = '1'
length = 0
for c in S:
  if prev != c:
    arr.append(length)
    length = 0
  length += 1
  prev = c
arr.append(length)
if S[-1] == '0':
  arr.append(0)

sarr = [0]
for a in arr:
  sarr.append(sarr[-1] + a)

L = K * 2 + 1
if L >= len(sarr):
  print(N)
  exit()
ans = 0
for i in range(0, len(sarr)-L, 2):
  t = sarr[i+L] - sarr[i]
  ans = ans if ans > t else t
print(ans)

Solution (Python):
(N,M) = (int(i)for i in input().split(" "))
S = input()
l = [[0,0]]
ch = '0'
count = 0
for s in S:
	if s==ch:
		count += 1
	else:
		if s == '0':
			l[-1].append(count)
		else:
			l.append([count])
		ch = s
		count += 1
if len(l[-1]) == 1:
	l[-1].append(count)
elif l[-1][1] != count:
	l.append([count, count])

left = 0
right = M
if len(l) <= M:
	right = len(l) - 1

max_ans = 0
while right < len(l):
	ans = l[right][1] - l[left][0]
	if ans > max_ans:
		max_ans = ans
	right += 1
	left += 1

print(max_ans)

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


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

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

ans = 0
if s[0]=="1":
  flag = True
else:
  flag = False

for i in range(1,len(cursum)):
        if flag:
            ans = max(ans,cursum[i] - cursum[max(i-2*k-1,0)])
            flag = False
        else:
            ans = max(ans,cursum[i] - cursum[max(i-2*k,0)])
            flag = True

if ans == 0:
    print(n)
else:
    print(ans)

Solution (Python):
from itertools import groupby

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


l = [len(list(g)) for k, g in groupby(S)]

if S[0] == "0":
    l = [0] + l
if S[-1] == "0":
    l += [0]



w = K*2+1

m = sum(l[:w])
max_num = m
for i in range(len(l)-w):
    
    m -= l[i]
    m += l[w+i]
    if i%2 == 1:
        
        max_num = max(max_num, m)

print(max_num)

Solution (Python):
n, k = [int(item) for item in input().split()]
s = input()
arr = []
c = 0
if s[0] == "0":
  cur = "0"
  arr.append(0)
else:
  cur = "1"

for i in s:
  if i == cur:
   c += 1
  else:
    cur = i
    arr.append(c)
    c = 1
arr.append(c)
if s[-1]== "0":
  arr.append(0)

nk = 2 * k + 1
cusum = sum(arr[:nk])
mnum = cusum
for i in range(nk, len(arr), 2):
  cusum += -(arr[i-nk]) + arr[i] -(arr[i-nk+1]) + arr[i+1]
  mnum = max(mnum, cusum)
print(mnum)

Solution (Python):




def main():
    N, K = list(map(int, input().split()))
    lc = list(map(int, list(input())))
    l, r = 0, 0
    k = 0
    bc = 1
    ret = 0
    for i, c in enumerate(lc):
        if c == 0 and bc == 1:
            if k == K:
                ret = max(ret, i-l)
                c2 = lc[l]
                while c2 == 1 and l < N-1:
                    l += 1
                    c2 = lc[l]
                while c2 == 0 and l < N-1:
                    l += 1
                    c2 = lc[l]
            else:
                k += 1
        bc = c
    ret = max(ret, N-l)
    print(ret)


if __name__ == '__main__':
    main()

Solution (Python):




def main():
  _, N = map(int, input().split())
  stats = list(map(int, input()))
  values = []
  prev = -1

  for s in stats:
    if s == prev:
      values[-1][1] += 1
    else:
      values.append([s, 1])
      prev = s

  values.append([1, 0])

  window = N * 2 + 1
  length = 0
  max_length = 0

  for i in range(len(values)):
    if i - window >= 0:
      length -= values[i - window][1]

    length += values[i][1]

    if values[i][0] == 1:
      max_length = max(length, max_length)

  print(max_length)


if __name__ == '__main__':
  main()

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

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

L = 0
r = 0

for _ in range(k):
    while r < len(s) and s[r] == '1':
        r += 1
    while r < len(s) and s[r] == '0':
        r += 1
while r < len(s) and s[r] == '1':
    r += 1

ans = r - L

while True:
    while L < len(s) and s[L] == '1':
        L += 1
    while L < len(s) and s[L] == '0':
        L += 1
    while r < len(s) and s[r] == '0':
        r += 1
    while r < len(s) and s[r] == '1':
        r += 1
    ans = max(ans, r - L)
    if r >= len(s):
        break

print(ans)

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

S = input()

xs = []

i = 0

while i < N :
    l = 0
    while i < N and S[i] == '1':
        i += 1
        l += 1
    xs.append(l)
    l = 0
    while i < N and S[i] == '0':
        i += 1
        l += 1
    xs.append(l)

xs += [0] * (K * 2)



result = 0

l = (K * 2) + 1
result = r = sum(xs[0:l])


i = 0
while i + l + 1 < len(xs):
    r = r - xs[i] - xs[i+1] + xs[i+l] + xs[i+l+1]
    result = max(result, r)
    
    i += 2

print(result)

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

ans = 0
k = 0
for i,(a,b) in enumerate(zip(S,S[1:])):
    if a=='0' and b=='1':
        k += 1
    elif k==K and a=='1' and b=='0':
        ans = max(ans, i+1)
        break
else:
    print(N)
    exit()

k = 0
S = S[::-1]
for i,(a,b) in enumerate(zip(S,S[1:])):
    if a=='0' and b=='1':
        k += 1
    elif k==K and a=='1' and b=='0':
        ans = max(ans, i+1)
        break
else:
    print(N)
    exit()

x = []
y = []
for i,(a,b) in enumerate(zip(T,T[1:])):
    if a==b: continue
    if a=='0':
        x.append(i)
    else:
        y.append(i)

for a,b in zip(x,y[K:]):
    ans = max(ans, b-a)
print(ans)

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

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

if len(X) <= K*2+1:
    print(N)
else:
    ma = 0
    for i in range(0, len(X) - K*2 - 1, 2):
        ma = max(X[i+K*2+1]-X[i], ma)
    print(ma)

Solution (Python):
import sys

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

if not ("1" in S):
    print(N)
    sys.exit()

D=[]
NOW=1
S+="!"


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

    else:
        D.append([S[i-1],NOW])
        NOW=1
    

if D[0][0]=="0":
    D=[["1",0]]+D

if D[-1][0]=="0":
    D.append(["1",0])

if K>=(len(D)-1)//2:
    print(N)
    sys.exit()

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


ANS=0

for i in range(K*2+1,len(SUM),2):
    ANS=max(ANS,SUM[i]-SUM[i-K*2-1])

print(ANS)

Solution (Python):

import sys


def solve(N: int, K: int, S: int):
    group = []
    if S[0] == '0':
        group.append(0)
    tmp = 1
    for i in range(1, N):
        if S[i] == S[i - 1]:
            tmp += 1
        else:
            group.append(tmp)
            tmp = 1
    group.append(tmp)
    if S[-1] == '0':
        group.append(0)
    
    num = K * 2 + 1
    if len(group) <= num:
        print(N)
        return

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

    ret = 0
    for i in range(0, len(group) - num + 1, 2):
        cumsum = cum[i + num] - cum[i]
        
        ret = max(ret, cumsum)
    print(ret)
    return


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

if __name__ == '__main__':
    main()

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

beg=[0]
end=[]

for i in range(N):
	if len(beg) != len(end):
		if S[i] == "0":
			end.append(i-1)
	else:
		if S[i] == "1":
			beg.append(i)

if len(beg) != len(end):
	end.append(N-1)

beg.append(N-1)
end.append(N-1)

ans=[1 + end[i+K] - beg[i] for i in range(len(beg)-K)]
print(max(ans) if len(ans) > 0 else N)

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

for i in s:
    if not sani:
        sani.append([int(i),0,0])
    elif sani[-1][0]==int(i):
        sani[-1][2]+=1
    else:
        sani.append([int(i),sani[-1][2]+1,sani[-1][2]+1])

ans=0
left,right=0,0
zeroCnt=0

while right<len(sani):
    if zeroCnt<k or sani[right][0]==1:
        nowElem=sani[right]
        zeroCnt+=1 if sani[right][0]==0 else 0
        ans=max(ans,sani[right][2]-sani[left][1]+1)
        right+=1
    else:
        zeroCnt-=1 if sani[left][0]==0 else 0
        left+=1

print(ans)

Solution (Python):


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

l = [0]
curr = '1'
for c in S:
    if c != curr:
        l.append(1)
        curr = c
    else:
        l[-1] += 1
if curr == '0':
    l.append(0)

n_group = len(l) // 2
sum_ = 0
k = min(K, n_group)
for i in range(k):
    sum_ += l[2*i] + l[2*i+1]
sum_ += l[2*k]
max_ = sum_
for i in range(k, n_group):
    sum_ -= l[2*i-2*K] + l[2*i+1-2*K]
    sum_ += l[2*i+1] + l[2*i+2]
    max_ = max(max_, sum_)
print(max_)

Solution (Python):

from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LS()
    return l
sys.setrecursionlimit(1000000)
mod = 1000000007


def A():
    a,b = LI()
    if a == b:
        print(2*a)
    else:
        print(2*max(a,b)-1)
    return


def B():
    n = I()
    h = LI()
    m = h[0]
    ans = 1
    for i in range(1,n):
        if m <= h[i]:
            ans += 1
            m = h[i]
    print(ans)
    return


def C():
    s = S()
    ans = 0
    n = len(s)
    t = ["1" if i%2 else "0" for i in range(n)]
    for i in range(n):
        if t[i] != s[i]:
            ans += 1
    ans2 = 0
    t = ["0" if i%2 else "1" for i in range(n)]
    for i in range(n):
        if t[i] != s[i]:
            ans2 += 1
    print(min(ans,ans2))
    return


def D():
    n,k = LI()
    s = S()
    f = [0 for i in range(n)]
    if s[0] == "0":
        f[0] = 1
    for i in range(1,n):
        if s[i] ==  "0" and s[i-1] == "1":
            f[i] = 1
    for i in range(n-1):
        f[i+1] += f[i]
    ans = 0
    for l in range(n):
        r = bisect.bisect_right(f,k+f[l]*int(s[l]))
        ans = max(ans, r-l)
    print(ans)
    return


if __name__ == "__main__":
    D()

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

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

sum = [0] * (m + 1)
for i in range(1, m + 1):
	sum[i] = sum[i - 1] + v[i - 1]

ans = 0
for i in range(m + 1):
	if s[0] == 0:
		if i % 2 == 0:
			ans = max(ans, sum[i] - sum[max(i - k * 2 - 1, 0)])
		else:
			ans = max(ans, sum[i] - sum[max(i - k * 2, 0)])
	else:
		if i % 2 == 0:
			ans = max(ans, sum[i] - sum[max(i - k * 2, 0)])
		else:
			ans = max(ans, sum[i] - sum[max(i - k * 2 - 1, 0)])

k = k - 1
for i in range(m + 1):
	if s[0] == 0:
		if i % 2 == 1:
			ans = max(ans, sum[i] - sum[max(i - k * 2 - 1, 0)])
		else:
			ans = max(ans, sum[i] - sum[max(i - k * 2, 0)])
	else:
		if i % 2 == 1:
			ans = max(ans, sum[i] - sum[max(i - k * 2, 0)])
		else:
			ans = max(ans, sum[i] - sum[max(i - k * 2 - 1, 0)])

print(ans)

Solution (Python):
import numpy as np

if __name__ == "__main__":
    N, K = map(int, input().split())
    S = input()
    T = [0]
    if(S[0] == '0') :
        T.append(0)
    T.append(1)
    for i in range(1, N):
        if(S[i] == S[i-1]):
            T[-1] += 1
        else:
            T.append(1)
    for i in range(3 * K):
        T.append(0)
    SIZE = len(T)
    S = [0] * SIZE
    for i in range(1, SIZE):
        S[i] = S[i-1] + T[i]
    
    
    ans = 0
    for i in range (1, SIZE, 2):
        if(i + 2 * K >= SIZE):
            break
        ans = max(ans, S[i + 2 * K] - S[i-1])
    print(ans)

Solution (Python):
from itertools import chain
N, K = map(int, input().split())
S = input()
LS = []
pre = S[0]
ctr = 0
for s in chain(S, '2'):
    if s == pre:
        ctr += 1
    else:
        LS.append(ctr)
        ctr = 1
        pre = s
if S[0] == '0':
    LS = [0] + LS
if S[-1] == '0':
    LS = LS + [0]
ans = 0
sumi = 0
for i in range(len(LS)):
    if i <= 2*K:
        sumi += LS[i]
    else:
        sumi += LS[i] - LS[i-2*K-1]
    if not i % 2:
        ans = max(ans, sumi)
print(ans)