Problem:
 Score : 400 points    Problem Statement N people are arranged in a row from left to right.  You are given a string S of length N consisting of 0 and 1 , and a positive integer K .  The i -th person from the left is standing on feet if the i -th character of S is 0 , and standing on hands if that character is 1 .  You will give the following direction at most K times (possibly zero):  Direction : Choose integers l and r satisfying 1 \leq l \leq r \leq N , and flip the l -th, (l+1) -th, ... , and r -th persons. That is, for each i = l, l+1, ..., r , the i -th person from the left now stands on hands if he/she was standing on feet, and stands on feet if he/she was standing on hands.  Find the maximum possible number of consecutive people standing on hands after at most K directions.      Constraints  N is an integer satisfying 1 \leq N \leq 10^5 .  K is an integer satisfying 1 \leq K \leq 10^5 .  The length of the string S is N .  Each character of the string S is 0 or 1 .         Input Input is given from Standard Input in the following format:  N  K  S       Output Print the maximum possible number of consecutive people standing on hands after at most K directions.        Sample Input 1 5 1
00010      Sample Output 1 4  We can have four consecutive people standing on hands, which is the maximum result, by giving the following direction:   Give the direction with l = 1, r = 3 , which flips the first, second and third persons from the left.        Sample Input 2 14 2
11101010110011      Sample Output 2 8       Sample Input 3 1 1
1      Sample Output 3 1  No directions are necessary.

Solution (Python):
import math
from itertools import accumulate

INF = float("inf")


def main():
    N, K = map(int, input().split())
    S = input()
    zero_list = []
    A = []
    one = 0
    zero = 0

    for i in range(N):
        if S[i] == '1':
            one += 1
            if zero != 0:
                A.append(zero)
                zero_list.append(zero)
                zero = 0
        else:
            zero += 1
            if one != 0:
                A.append(one)
                one = 0

    if one != 0:
        A.append(one)
    elif zero != 0:
        A.append(zero)
        zero_list.append(zero)

    if len(zero_list) <= K:
        print(N)
        return

    section = 0
    if S[0] == '1':
        section = 3 + (K-1) * 2
    else:
        section = 2 + (K-1) * 2

    B = list(accumulate([0] + A))
    
    if S[0] == '1':
        
        m = 0
        i = 0
        while True:
            if i % 2 == 0:
                section = 3 + (K-1) * 2
                end = min(i+section, len(B) - 1)
                
                
                m = max(m, B[end] - B[i])
                if len(B) - 1 <= end:
                    break
            else:
                section = 2 + (K-1) * 2
                end = min(i+section, len(B) - 1)
                
                
                m = max(m, B[end] - B[i])
                if len(B) - 1 <= end:
                    break
            i += 1
        print(m)
    else:
        m = 0
        i = 0
        while True:
            if i % 2 == 0:
                section = 2 + (K-1) * 2
                end = min(i+section, len(B) - 1)
                
                
                m = max(m, B[end] - B[i])
                if len(B) - 1 <= end:
                    break
            else:
                section = 3 + (K-1) * 2
                end = min(i+section, len(B) - 1)
                
                
                m = max(m, B[end] - B[i])
                if len(B) - 1 <= end:
                    break
            i += 1
        print(m)


if __name__ == '__main__':
    main()

Solution (Python):
import sys


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

all_0 = True
for i in range(len(s)):
    if s[i] == '1':
        all_0 = False
        break
if all_0:
    if k == 0:
        print(0)
    else:
        print(n)
    sys.exit()

c0 = 0
c1 = 0
a = [0]
b = []
v = 0
while s[v] == '0':
    c0 += 1
    s = s[1:]
b.append(c0)
c0 = 0
for i in range(len(s)):
    if s[i] == '1':
        c1 += 1
        if c0 != 0:
            b.append(c0)
        c0 = 0
    else:
        c0 += 1
        if c1 != 0:
            a.append(c1)
        c1 = 0
if c0 != 0:
    b.append(c0)
else:
    a.append(c1)
    b.append(0)
a.append(0)



s_a = [0]
s_b = [0]
for i in range(len(a)):
    s_a.append(s_a[i]+a[i])
for i in range(len(b)):
    s_b.append(s_b[i]+b[i])



ans = 0
if len(b) <= k:
    ans = n
else:
    for i in range(len(a)-k):
        
        ans = max(ans, s_a[i+k+1] - s_a[i] + s_b[i+k] - s_b[i])  
print(ans)

Solution (Python):
N,K = map(int,input().split())
s = input()
s += 'A'
k = 0
con = []
if s[0] == '0':
    con.append(0)
c = 1
for i in range(N):
    if s[i] == s[i+1]:
        c += 1
    else:
        con.append(c)
        c = 1
if s[N-1] == '0':
    con.append(0)
k = len(con)
l = 0
r = 2*K
ans = 0
if k < r:
    ans = sum(con) 
else:
    for i in range(l,r+1):
        ans += con[i]
now = ans        
while(k > r + 1):
    now = now - con[l] - con[l+1] + con[r+1] + con[r+2]
    ans = max(ans,now)
    l += 2
    r += 2
print(ans)

Solution (Python):
n,k = map(int,input().split())
s = list(input())
 
l0 = []
l1 = []
pred = s[0]
if s[0] == "0":
    l0.append(1)
    l1.append(0)
else:
    l1.append(1)
 
for i in range(1,n):
    if pred == s[i]:
        if s[i] == "0":
            l0[-1] += 1
        else:
            l1[-1] += 1
    else:
        if s[i] == "0":
            l0.append(1)
        else:
            l1.append(1)
    pred = s[i]
if s[-1] == "0":    
    l1.append(0)
 
ans = sum(l0[:k])+sum(l1[:k+1])
tmp = ans
for i in range(k,(len(l0))):
    tmp -= l0[i-k]
    tmp -= l1[i-k]
    tmp += l0[i]
    tmp += l1[i+1]
    ans = max(ans, tmp)
print(ans)

Solution (Python):
n,k = map(int,input().split())
s = list(input())
 
l0 = []
l1 = []
pred = s[0]
if s[0] == "0":
    l0.append(1)
    l1.append(0)
else:
    l1.append(1)
 
for i in range(1,n):
    if pred == s[i]:
        if s[i] == "0":
            l0[-1] += 1
        else:
            l1[-1] += 1
    else:
        if s[i] == "0":
            l0.append(1)
        else:
            l1.append(1)
    pred = s[i]
if s[-1] == "0":    
    l1.append(0)
 
ans = sum(l0[:k])+sum(l1[:k+1])
tmp = ans
for i in range(k,min(len(l0),len(l1))):
    tmp -= l0[i-k]
    tmp -= l1[i-k]
    tmp += l0[i]
    tmp += l1[i+1]
    ans = max(ans, tmp)
print(ans)

Solution (Python):

n,k = map(int, input().split( ))
s = input()
c0 = 0
c1 = 0
L0 = []
 
L1 = []
if s[0]=="0":
    L1 = [0]
for i in range(n):
    if s[i]=="0":
        c0+=1
        if c1!=0:
            L1.append(c1)
            c1 = 0
    else:
        c1+=1
        if c0!=0:
            L0.append(c0)
            c0 = 0
if c0:
    L0.append(c0)
if c1:
    L1.append(c1)

L1.append(0)

ans = sum(L0[:k])+sum(L1[:k+1])
tmp = ans
for i in range(k,min(len(L0),len(L1))):
    tmp -= L0[i-k]
    tmp -= L1[i-k]
    tmp += L0[i]
    tmp += L1[i+1]
    ans = max(ans, tmp)
print(ans)

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

l = 0
alt = 0; cnt = 0

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

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

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

add=2*k+1
ans=0
s=[0]*(len(wa)+1)
for i in range(len(s)-1):
    s[i+1]=s[i]+wa[i]




for i in range(0,len(wa),2):
    left=i
    right=min(i+add, len(wa))
    tmp=s[right]-s[left]

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

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

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

add=2*k+1
ans=0

left=0
right=0
tmp=0
for i in range(0,len(wa),2):
    nextleft=i
    nextright=min(i+add, len(wa))

    while nextleft > left:
        tmp-=wa[left]
        left+=1
    while nextright > right:
        tmp+=wa[right]
        right+=1

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

Solution (Python):
n, k = map(int, input().split())
s = [int(s) for s in list(input())]
count = 0
tmp = 1
c = []

for i in s:
    if i == tmp:
        count += 1
    else:
        c.append(count)
        tmp = i
        count = 1

c.append(count)
if s[-1] == 0:
    c.append(0)

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

print(ans)

Solution (Python):
import sys

n, k = map(int, input().split())
s = [int(s) for s in list(input())]
count = 0
tmp = 1
c = []
ans = 0

for i in s:
    if i == tmp:
        count += 1
    else:
        c.append(count)
        tmp = i
        count = 1

c.append(count)
if s[-1] == 0:
    c.append(0)


if len(c) <= 2*k+1:
    print(sum(c))
    sys.exit()



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

print(ans)

Solution (Python):
n,k = map(int,input().split())
s = input()
h = list()
h.append(0)
for i in range(n - 1):
    if(s[i]!=s[i + 1]):
        h.append(i + 1)
m = len(h)
for i in range(2*k + 1):
    h.append(n)
mx = 0
for i in range(m):
    if(s[h[i]]=="0"):
        mx = max(mx,h[i + 2*k] - h[i])
    else:
        mx = max(mx,h[i + 2*k + 1] - h[i])
print(mx)

Solution (Python):
n,k=map(int,input().split())
s=input()
t = 0
tc = s[0]
s0 = s[0]
l=[]
if s0 == "0":
    l.append(0)
for c in s:
    if c ==tc:
        t+= 1
    else:
        l.append(t)
        t = 1
        tc = c
l.append(t)
if tc == "0":
    l.append(0)
res = 0
if len(l) < 2*k+1:
    print(sum(l))
else:
    suml = [0]
    for i in range(len(l)):
        suml.append(suml[i]+l[i])
    for i in range(0,len(suml)-(2*k+1),2):
        res = max(res,suml[2*k+1+i]-suml[i])
    print(res)

Solution (Python):
tmp = list(map(int, input().split()))
n = tmp[0]
k = tmp[1]
s = list(map(int, list(input())))

fl = []
rl = []

cnt0 = 0
cnt1 = 0
for i in range(n):
  if s[i] == 0:
    if cnt0 == 0:
      rl.append(cnt1)
      cnt1 = 0
    cnt0 += 1
  elif s[i] == 1:
    if cnt1 == 0 and i != 0:
      fl.append(cnt0)
      cnt0 = 0
    cnt1  += 1

if cnt0 != 0:
  fl.append(cnt0)
  rl.append(0)
elif cnt1 != 0:
  rl.append(cnt1)

rllen = rl[0]
for i in range(min(k, len(fl))):
  rllen += fl[i] + rl[i+1]

maxr = rllen
for i in range(max(0, len(fl)-k)):
  rllen += (fl[i+k] + rl[i+k+1])
  rllen -= (fl[i] + rl[i])
  if maxr < rllen:
    maxr = rllen 

print(maxr)

Solution (Python):
n, k = map(int, input().split())
s = input()
r = 1
ans = 0
cnt = 0

for l in range(0, n):
    if l == 0:
        if s[l] == '0':
            cnt += 1

    if l > 0 and s[l] == '1' and s[l-1] == '0':
        cnt -= 1

    
    while r <= n - 1:        
        
        
        
        
        
        

        if s[r] == '0' and s[r-1] == '1':
            if cnt < k:
                cnt += 1
            else:
                
                
                
                break
        r += 1
    
    length = r - l
    ans = max(ans, length)

print(ans)

Solution (Python):
n, k = map(int, input().split())
s = input()
r = 1
ans = 0
cnt = 0

for l in range(0, n):
    if l == 0:
        if s[l] == '0':
            cnt += 1

    if l > 0 and s[l] == '1' and s[l-1] == '0':
        cnt -= 1

    while True:
        
        
        if r > n - 1:
            length = r - l
            ans = max(ans, length)
            break

        if s[r] == '0' and s[r-1] == '1':
            if cnt < k:
                cnt += 1
            else:
                
                length = r - l
                ans = max(ans, length)
                break
        r += 1

print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    from itertools import groupby
    n,k=map(int,input().split())
    s=input()
    G=groupby(s)
    A=[]
    cnt=0
    if(s[0]=='0'): A.append(0)
    for key,iter in G:
        cnt+=key=='0'
        A.append(len(list(iter)))
    if(s[-1]=='0'): A.append(0)
    if(cnt<=k):
        print(n)
        return

    n=len(A)
    now=sum(A[:2*k+1])
    score=now
    for i in range(2*k+2,n,2):
        now+=A[i]+A[i-1]-A[i-2*k-2]-A[i-2*k-1]
        score=max(score,now)
    print(score)
resolve()

Solution (Python):
from itertools import groupby
N, K = map(int,input().split())
S = list(input())
Sg = groupby(S)
L = []
if S[0] =="0":
  L.append(0)
for k, g in Sg:
  L.append(len(list(g)))
if S[-1] == "0":
  L.append(0)

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

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

Solution (Python):
n, k = map(int, input().split())
s = list(map(int, list(input()))) + [-1]
ls = []
stand_ls = []

tmp, cnt = s[0], 0
for x in s:
    if x != tmp:
        ls.append((tmp, cnt))
        if tmp == 0: stand_ls.append(len(ls)-1)
        cnt = 0
        tmp = x
    cnt += 1
    
if len(ls) == 1 and ls[0][0] == 1: print(ls[0][1]); exit()

def cumsum(s):
    n = len(s)
    cs = [0] * (n+1)
    for i in range(n):
        cs[i+1] = cs[i] + s[i][1]
    return cs

cum = cumsum(ls)

ans = 0
k = min(k, len(stand_ls))
for i in range(len(stand_ls) - (k-1)):
    l, r = stand_ls[i], stand_ls[i+k-1]
    if l > 0: l -= 1
    if r < len(ls)-1: r += 1
    ans = max(ans, cum[r+1] - cum[l])
print(ans)

Solution (Python):
def MAP(): return list(map(int,input().split()))
def INT(): return int(input())
def FLOAT(): return float(input())
MOD = 10**9+7


n,k = MAP()
s = input()
k*=2

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

r = len(lst)
lst += [n]
ans=[]
for i in range(r):
    if s[lst[i]]=="0":
        ans.append(lst[min(i+k,r)] - lst[i])
    else:
        ans.append(lst[min(i+k+1,r)] - lst[i])

print(max(ans))

Solution (Python):
N,K = map(int,input().split())
S = input()
prev = "1"
count = 0
blocks = []
for i,s in enumerate(S):
    if s != prev:
        blocks.append(count)
        prev = s
        count = 1
    else:
        count += 1
blocks.append(count)
if len(blocks) % 2 == 0:
    blocks.append(0)
if len(blocks) // 2 <= K:
    print(sum(blocks))
else:
    ps = sum(blocks[:2*K+1])
    cur_max = ps
    for i in range(2*(K+1),len(blocks),2):
        ps += blocks[i-1] + blocks[i]
        ps -= blocks[i-2-2*K] + blocks[i-1-2*K]
        cur_max = max(cur_max,ps)
    print(cur_max)

Solution (Python):
N, K = [int(_) for _ in input().split()]
S = input()
s1 = []
s0 = []
if S[0] == '0':
    s1.append(0)

tmp = S[0]
cnt = 1
for s in S[1:]:
    if tmp != s:
        if tmp == '0':
            s0.append(cnt)
        else:
            s1.append(cnt)
        cnt = 0
    tmp = s
    cnt += 1
if tmp == '0':
    s0.append(cnt)
    s1.append(0)
else:
    s1.append(cnt)

val = 0

j = 0
for i in range(min(K, len(s0))):
    val += s0[i] + s1[i]
    j = i + 1
val += s1[j]
l = 0

pre = val
for i in range(j, len(s0)):
    tmp = pre - s0[l] - s1[l]
    tmp += s0[i] + s1[i + 1]
    val = max(val,tmp)
    l += 1
    pre = tmp
print(val)

Solution (Python):
n,k=map(int,input().split())
s=list(input())
cnt=0
ans=0
j=0
for i in range(n):
    if s[i]=="0":
        if i==0 or s[i-1]=="1":
            cnt+=1
    if cnt>k:
        while s[j]=="1":
            j+=1
        while s[j]=="0":
            j+=1
        cnt-=1
    ans=max(ans,i-j+1)

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 = input()
a = [i for i in range(1, n) if s[i] != s[i-1]]
a = [0]+ a if s[0] == '1' else [0, 0] + a
a += [n] if s[-1] == '1' else [n, n]
l, x = len(a), k * 2 + 1
if l <= x:
    print(n)
else:
    ans = max(a[i + x] - a[i] for i in range(0, l - x + 1, 2))
    print(ans)

Solution (Python):
from itertools import groupby, accumulate
N, K = map(int, input().split())
S = groupby(list(input()))
state = []
number = []
for i, j in S:
    state.append(i)
    number.append(len(list(j)))

answer = 0

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

number = list(accumulate([0] + number))
l = len(number) - 1

if state[0] == '0':
    answer = number[min(l, 2 * K)] - number[0]

start = state.index('1') + 1
for i in range(start, l + 1, 2):
    answer = max(answer, number[min(2 * K + i, l)] - number[i - 1])

print(answer)

Solution (Python):
n,k=map(int,input().split())
s=input()
b=[1]
if s[0]=="0":b=[0,1]
for i in range(1,n):
  if s[i-1]!=s[i]:b+=[1]
  else:b[-1]+=1
if s[-1]=="0":b+=[0]
if len(b)//2<=k:print(n);exit()


x=a=sum(b[:k*2+1])

for i in range(k,len(b)//2):
  a+=b[i*2+1]+b[(i+1)*2]
  a-=b[(i-k)*2]+b[(i-k)*2+1]
  x=max(a,x)
  
print(x)

Solution (Python):

n,k = map(int,input().split())
S = input()
nums = []
now = 1
cnt = 0
for i in range(n):
    if S[i] == str(now):
        cnt += 1
    else:
        nums.append(cnt)
        now = 1 - now
        cnt = 1
if cnt != 0:
    nums.append(cnt)

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

add = 2*k + 1
ans = 0
l = 0
r = 0
tmp = 0

for i in range(0,len(nums),2):
    nl = i
    nr = min(i+add,len(nums))
    
    while nl > l:
        tmp -= nums[l]
        l += 1
    while nr > r:
        tmp += nums[r]
        r += 1
    ans = max(tmp,ans)

print(ans)

Solution (Python):






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

if len(set(S)) == 1:
    print(N)
    exit()

handstand_range_idx = []
range_start_idx = 0
for idx in range(1,N):
    if (S[idx-1] == "1" and S[idx] == "0"):
        range_end_idx = idx - 1
        handstand_range_idx.append([range_start_idx, range_end_idx])
    elif idx == N-1 and S[idx] == "1":
        range_end_idx = idx
        handstand_range_idx.append([range_start_idx, range_end_idx])
    elif S[idx-1] == "0" and S[idx] == "1":
        range_start_idx = idx

ans = 0
try:
    ans = max(handstand_range_idx[K-1][1]+1, (N-1) - handstand_range_idx[-K][0] + 1)
except IndexError: 
    print(N)
    exit()

for first_link_range_idx in range(len(handstand_range_idx)):
    try:
        ans = max(ans, handstand_range_idx[first_link_range_idx + K][1] - handstand_range_idx[first_link_range_idx][0] + 1)
    except IndexError:
        pass

print(ans)

Solution (Python):
N,K=map(int,input().split())
S=input()
X,Y=[],[]
M=0
for i in range(len(S)):
  if(S[i]=='1' and M==0):
    X.append(i)
    M+=1
  elif(S[i]=='1'):
    M+=1
  else:
    if(M):
      Y.append(i-1)
    M=0
if(M):
  Y.append(len(S)-1)
ans=0
if(len(X)>=K):
  for i in range(len(X)-K):
    ans=max(ans,Y[i+K]-X[i]+1)
else:
  ans=len(S)
if(len(X)):
  ans=max(ans,Y[min(K-1,len(X)-1)]+1)
  ans=max(ans,len(S)-X[max(len(X)-K,0)])
print(ans)

Solution (Python):
import numpy as np

N, K = map(int, input().split())
S = list(input()) + ['2']

blocks = []
cur = 1
seq = 0
for s in S:
    if int(s) == cur:
        seq += 1
    else:
        blocks.append(seq)
        cur = 1 - cur
        seq = 1
answer = 0
blocks = np.array(blocks)
for i in range(0, len(blocks), 2):
    answer = max(answer, blocks[i:i+2*K+1].sum())
print(answer)

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

Solution (Python):
import sys
import bisect
import collections
import fractions
import heapq
import math
from operator import mul
from functools import reduce
from functools import lru_cache


def slove():
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    n, k = list(map(int, input().rstrip('\n').split()))
    s = str(input().rstrip('\n'))
    ls = []
    cnt = 1
    for i in range(1, n):
        if s[i] != s[i-1]:
            ls.append(cnt)
            cnt = 0
        cnt += 1
    ls.append(cnt)

    wc = 0
    b = True if s[0] == "1" else False
    fb = True if s[0] == "1" else False
    cnt = 0
    pos = 0
    m_cnt = 0
    for i in range(len(ls)):
        if not b:
            wc += 1
        if wc > k:
            cnt -= ls[pos]
            pos += 1
            if fb:
                cnt -= ls[pos]
                pos += 1
            else:
                fb = not b
            wc -= 1
        b = not b
        cnt += ls[i]
        m_cnt = max(m_cnt, cnt)
    print(m_cnt)


if __name__ == '__main__':
    slove()

Solution (Python):
import sys
def input():
    return sys.stdin.readline()[:-1]
N,K=map(int,input().split())
S=input()
l=[0]
t="1"
for i in S:
    if t==i:
        l[-1]+=1
    else:
        l.append(1)
        if t=="1":
            t="0"
        else:
            t="1"
if t=="0":
    l.append(0)
a=sum(l[:2*K+1])
A=a
for i in range(2*K+2,len(l),2):
    a+=l[i]+l[i-1]
    a-=l[i-2*K-2]+l[i-2*K-1]
    A=max(A,a)
print(A)

Solution (Python):
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')
ans = int(0)

N, K = LI()
S = str(input())
S = S + "2"
d1 = []
d0 = []
k = S[0]
if k=="0":
    d1.append(0)
cou = int(1)
for i in range(N):
    if S[i]!=S[i+1]:
        if S[i]=="0":
            d0.append(cou)
        if S[i]=="1":
            d1.append(cou)
        cou = int(1)
    else:
        cou +=1




ansC = []
t = 2*K+1
newN = len(d1)
if len(d1)==1 or d0==[] or K>=N//2+1:
    ansC.append(N)

if newN!=len(d0):
    d0.append(0)
    d0.append(0)
sumd1 = [0]*(newN+t+5)
sumd0 = [0]*(newN+t+5)
for i in range(newN):
    sumd1[i+1] = d1[i]
    sumd0[i+1] = d0[i]
for i in range(newN+1):
    sumd1[i+1] += sumd1[i]
    sumd0[i+1] += sumd0[i]


if ansC==[] and (newN - K + 1)>=1:
    for i in range(newN - K + 1):
        ans = (sumd1[i + K + 1] - sumd1[i]) + (sumd0[i + K] - sumd0[i])
        ansC.append(ans)
else:
    ansC.append(N)
print(max(ansC))

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

a_list = []
b_list = []

if s[0] == "0":
    a_list.append(0)
p = 0
for i in s:
    if i == "1" and p < 0:
        b_list.append(-p)
        p = 1
    elif  i == "1" and p >= 0:
        p += 1
    elif  i == "0" and p > 0:
        a_list.append(p)
        p = -1
    elif  i == "0" and p <= 0:
        p -= 1
if p < 0:
    b_list.append(-p)
    a_list.append(0)
else:
    a_list.append(p)


if len(b_list) <= k:
    ans = n
else:
    m = len(b_list) - k
    sequence = sum(a_list[:k+1]) + sum(b_list[:k])
    ans = sequence
    for i in range(m):
        sequence += a_list[i+k+1] - a_list[i]
        sequence += b_list[i+k] - b_list[i]
        ans = max(sequence, ans)
print(ans)

Solution (Python):
n,k = map(int,input().split())
s = input()
c = s[0]
t = 0
l = [0]
for x in s:
  if x == c:
    t += 1
  else:
    l.append(t)
    c = x
    t = 1
f = s[0] == "0"
l.append(t)
m = len(l)
for i in range(1,m):
  l[i] += l[i-1]
l += [n]*2*(k+1)
ans = l[2*k]-l[0]
for i in range(f,m+1,2):
  ans = max(ans,l[i+2*k+1] - l[i])
print(ans)

Solution (Python):
N, K = [int(x) for x in input().split()]
S = input()
lst = []
if (S[0] == '0'): lst.append(0)
cnt = 1
for i in range(N-1):
    if (S[i] == S[i+1]):
        cnt += 1
    else:
        lst.append(cnt)
        cnt = 1 
lst.append(cnt)
if (S[-1] == '0'): lst.append(0)
tmp = [0] + lst[:]
ans = 0 if (len(lst) != 1) else lst[0] 
for i in range(len(lst)):
    tmp[i+1] += tmp[i]
if (((len(lst)+1)//2-K) < 1):
    ans = tmp[-1]
else:
    for i in range(0, (len(lst)+1)//2-K):
        l = 2*i
        r = l + 1 + 2*K
        s = tmp[r] - tmp[l]
        if (ans < s):
            ans = s
print(ans)

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

now = 1
cnt = 0
for s in S:
    if str(now) == s:
        cnt += 1
    else:
        Data.append(cnt)
        cnt = 1
        now = 1 - now
if cnt > 0:
    Data.append(cnt)
if now == 0:
    Data.append(0)

DataSum = [0]
for i in range(len(Data)):
    DataSum.append(Data[i]+DataSum[i])




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

print(ans)

Solution (Python):
import numpy as np

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

cur = 1 
blocks = np.zeros(10**5+1, dtype = np.int64)
seq = 0
p = 0
for s in S:
    if s == cur:
        blocks[p] += 1
    else:
        p += 1
        blocks[p] += 1        
        cur = 1 - cur
W = min(2*K, 10**5)
answer = sum = blocks[:W+1].sum()
for i in range(W+1, 10**5, 2):
    if i+1 > 10 ** 5:
        break
    sum = sum + blocks[i] + blocks[i+1] - blocks[i-W-1] - blocks[i-W]
    if sum > answer:
        answer = sum
print(answer)

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

if s[0]=="1":
    lists1[0]+=1
elif s[0]=="0":
    lists0[0]+=1     

if s[0]=="1" and s[-2]=="1":
    
    if k>=len(lists0):
        print(n)
    elif k<len(lists0):
        big=0
        ans=sum(lists1[:k+1])+sum(lists0[:k])
        for i in range(len(lists0)-k):
            ans1=ans+lists1[k+1+i]+lists0[k+i]-lists1[i]-lists0[i]
            big=max(big,max(ans,ans1))
            ans=ans1
        print(big)
       
        
elif s[0]=="1" and s[-2]=="0":
    
    lists1.append(0)

    if k>=len(lists0):
        print(n)
    elif k<len(lists0):
        big=0
        ans=sum(lists1[:k+1])+sum(lists0[:k])
        for i in range(len(lists0)-k):
            ans1=ans+lists1[k+1+i]+lists0[k+i]-lists1[i]-lists0[i]
            big=max(max(ans,ans1),big)
            ans=ans1
        print(big)
    
elif s[0]=="0" and s[-2]=="1":
    
    lists1.insert(0,0)

    if k>=len(lists0):
        print(n)
    elif k<len(lists0):
        big=0
        ans=sum(lists1[:k+1])+sum(lists0[:k])
        for i in range(len(lists0)-k):
            ans1=ans+lists1[k+1+i]+lists0[k+i]-lists1[i]-lists0[i]
            big=max(big,max(ans,ans1))
            ans=ans1
        print(big)    
elif s[0]=="0" and s[-2]=="0":
    
    lists1.insert(0,0)
    lists1.append(0)

    if k>=len(lists0):
        print(n)
    elif k<len(lists0):            
        big=0
        ans=sum(lists1[:k+1])+sum(lists0[:k])
        for i in range(len(lists0)-k):
            ans1=ans+lists1[k+1+i]+lists0[k+i]-lists1[i]-lists0[i]
            big=max(big,max(ans,ans1))
            ans=ans1
        print(big)

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

tmp = S[0]
l = [0]
for i in range(N):
    if S[i] != tmp:
        l.append(i)
        tmp = S[i]
r = len(l)
l.extend([N] * (K*2 + 1))    

x = [0] * (r)
for i in range(r):
    if S[l[i]] == 0:
        x[i] = l[i+K*2] - l[i]
    else:
        x[i] = l[i+K*2+1] - l[i]
        
print(max(x))

Solution (Python):
import numpy as np

N, K = map(int, input().split())
S = np.array([int(x) for x in input()] + [2])

blocks = [] 
seq = 1
for i in range(N):
    if S[i+1] == S[i]:
        seq += 1
    else:
        blocks.append(seq)
        seq = 1
blocks = np.array(blocks)

answer = blocks[:2*K].sum()

start = 0 if S[0] == 1 else 1
for i in range(start, len(blocks), 2):
    answer = max(answer, blocks[i:i+(2*K+1)].sum())

print(answer)

Solution (Python):
N, K = map(int, input().split())
S = list(map(int, input()))
lst = []
cnt = 0
now = 1

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


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

ret = 0
add = 2 * K + 1
left = 0
right = 0
total = 0

for i in range(0, len(lst), 2):
	nextleft = i
	nextright = min(i + add, len(lst))
	while nextleft > left:
		total -= lst[left]
		left += 1
	while nextright > right:
		total += lst[right]
		right += 1
	ret = max(ret, total)
	
print(ret)

Solution (Python):



def main():
    from itertools import accumulate

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

    nums = list()
    now = 1
    count = 0

    
    
    
    
    for si in s:
        if si == str(now):
            count += 1
        else:
            nums.append(count)
            now = 1 - now  
            count = 1

    
    if count != 0:
        nums.append(count)

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

    add = 2 * k + 1

    
    summed = list(accumulate([0] + nums))

    tmp = 0
    ans = 0

    
    for i in range(0, len(nums), 2):
        left = i
        right = min(i + add, len(nums))
        tmp = summed[right] - summed[left]

        ans = max(ans, tmp)

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):



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

    nums = list()
    now = 1
    count = 0

    
    
    
    
    for si in s:
        if si == str(now):
            count += 1
        else:
            nums.append(count)
            now = 1 - now  
            count = 1

    
    if count != 0:
        nums.append(count)

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

    add = 2 * k + 1

    
    left = 0
    right = 0  
    tmp = 0
    ans = 0

    
    for i in range(0, len(nums), 2):
        
        next_left = i
        next_right = min(i + add, len(nums))

        
        
        while next_left > left:
            tmp -= nums[left]
            left += 1

        
        while next_right > right:
            tmp += nums[right]
            right += 1

        ans = max(ans, tmp)

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):



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

    nums = list()
    now = 1
    count = 0

    for si in s:
        if si == str(now):
            count += 1
        else:
            nums.append(count)
            now = 1 - now
            count = 1

    if count != 0:
        nums.append(count)

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

    add = 2 * k + 1

    left = 0
    right = 0
    tmp = 0
    ans = 0

    for i in range(0, len(nums), 2):
        next_left = i
        next_right = min(i + add, len(nums))

        while next_left > left:
            tmp -= nums[left]
            left += 1

        while next_right > right:
            tmp += nums[right]
            right += 1

        ans = max(ans, tmp)

    print(ans)


if __name__ == '__main__':
    main()