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

Solution (Python):
n,k=map(int,input().split())
s=list(input())
check=1
cnt=0
l=[]
for i in range(n):
    if check==1 and s[i]=='0':
        check=-1
        l.append(cnt)
        cnt=0
    elif check==-1 and s[i]=='1':
        check=1
        l.append(cnt)
        cnt=0
    cnt+=1
    if i==n-1:
        l.append(cnt)
if s[0]=='1':
    ans=sum(l[0:k*2+1])
    ansl=[ans]
    i=2*k+1
    while i<len(l):
        ans+=l[i]-l[i-2*k-1]-l[i-2*k]
        if i+1<len(l):
            ans+=l[i+1]
        i+=2
        ansl.append(ans)
if s[0]=='0':
    ans=sum(l[0:2*k+1])
    ansl=[ans]
    i=2*k+1
    while i<len(l):
        ans+=l[i]+-l[i-2*k-1]-l[i-2*k]
        if i+1<len(l):
            ans+=l[i+1]
        i+=2
        ansl.append(ans)
print(max(ansl))

Solution (Python):
N, K = map(int, input().split())
S = input()
left = 0
rc = 0
rf = False
lf = False
ans = 0
if set(S) == set("0") or set(S) == set("1"):
    print(len(S))
    exit()
for right in range(N):
    if rf and S[right] == "0" and rc <= K:
        if right != N-1:
            continue
        else:
            ans = max(ans, right - left)
            break
    if S[right] == "1":
        rf = False
    if S[right] == "0":
        rc += 1
        rf = True
    if rc > K:
        while True:
            if S[left] == "1":
                left += 1
            if S[left] == "0":
                while S[left] == "0":
                    left += 1
                break
        rc -= 1
    ans = max(ans, right - left)
print(ans+1)

Solution (Python):



NK = input().split(' ')
N = int(NK[0])
K = int(NK[1])
S = input()

prev = '1'
count = 0
nums = []
for i in S:
    if i == prev:
        count += 1
    else:
        nums.append(count)
        count = 1
        prev = i
if count > 0:
    nums.append(count)


C = min(K * 2 + 1, len(nums))
s1 = 0
for i in range(0, C):
    s1 += nums[i]
sums = [s1]
prev = s1
for i in range(0, len(nums)):
    s = prev - nums[i]
    if i + C < len(nums):
        s += nums[i + C]
    if i % 2 == 1:
        sums.append(s)
    prev = s


print(max(sums))

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

def gen_0_idx(S):
  N = len(S)
  idx_list = []
  start_0 = False
  for i in range(N):
    if S[i] == "0" and not start_0:
      start_idx = i
      start_0 = True
    elif S[i] == "1" and start_0:
      end_idx = i - 1
      idx_list.append((start_idx, end_idx))
      start_0 = False
    if i == N - 1 and start_0:
      end_idx = i
      idx_list.append((start_idx, end_idx))
  return idx_list

idx_list = gen_0_idx(S)
if len(idx_list) <= K:
  print(N)
else:
  N_I = len(idx_list)
  one_len_list = []
  for i in range(N_I - K + 1):
    if i == 0:
      length = idx_list[K][0]
    elif i == N_I - K:
      length = N - idx_list[N_I - K - 1][1] - 1
    else:
      length = idx_list[i + K][0] - idx_list[i - 1][1] - 1
    one_len_list.append(length)
  print(max(one_len_list))

Solution (Python):
nk = input()
nk = nk.split()
n = int(nk[0])
k = int(nk[1])

s = input()

count = 0
now = None
clist = []

for ss in s:
    if now is None:
        if ss == '0':
            clist.append(0)

    if now is None or now == ss:
        count += 1
    else:
        clist.append(count)
        count = 1
    now = ss

clist.append(count)

max_len = k * 2 + 1
max_sum = 0
clist_len = len(clist)
now_sum = None

for i in range(0, clist_len):
    if i % 2 == 1:
        continue
    j = i + max_len

    if now_sum is None:
        now_sum = sum(clist[i:j])
        max_sum = now_sum
    else:
        now_sum -= sum(clist[(i-2):i])
        now_sum += sum(clist[(j-2):(j)])
        max_sum = max(max_sum, now_sum)

print(max_sum)

Solution (Python):
import sys
input = sys.stdin.readline
def ii(): return int(input())
def mi(): return map(int, input().rstrip().split())
def lmi(): return list(map(int, input().rstrip().split()))
def li(): return list(input().rstrip())



N, K = mi()
strS = li()
S = [int(i) for i in strS]
S.append(0)



cnt = 0
lis0 = []
if S[0] == 1:
    listart = [0]
    cnt += 1
else:
    listart = []
liend = []
for i in range(1, N):
    
    
    if S[i-1] == 0 and S[i] == 1:
        listart.append(i)
        lis0.append(i)
    if S[i-1] == 1 and S[i] == 0:
        liend.append(i-1)


if S[N-1] == 1:
    liend.append(N - 1)
    cnt += 1


len0 = len(listart) + 1 - cnt




if K >= len0:
    print(N)
else:
    ma = max(liend[K - 1] + 1, N - listart[-K])
    for i in range(len(listart) - K):
        ma = max(ma, liend[i+K]-listart[i]+1)
    print(ma)

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

c_1 = [0]
c_0 = [0]
count = 0
before = '1'
answer = 0



for i in S:
    if i == before:
        count += 1
    else:
        if before == '1':
            c_1.append(c_1[-1] + count)
        else:
            c_0.append(c_0[-1] + count)
        if i == '0':
            if K+1 < len(c_1):
                answer = max(answer, c_1[-1] - c_1[-(K+2)] + c_0[-1] - c_0[-(K+1)])
            else:
                answer = max(answer, c_1[-1] + c_0[-1])
        count = 1
        before = i

if i == '1':
    c_1.append(c_1[-1] + count)
    if K + 1 < len(c_1):
        answer = max(answer, c_1[-1] - c_1[-(K + 2)] + c_0[-1] - c_0[-(K + 1)])
    else:
        answer = max(answer, c_1[-1] + c_0[-1])

else:
    c_0.append(c_0[-1] + count)

    if K + 1 < len(c_1):
        answer = max(answer, c_1[-1] - c_1[-(K + 1)] + c_0[-1] - c_0[-(K + 1)])
    else:
        answer = max(answer, c_1[-1] + c_0[-1])


print(answer)

Solution (Python):
import numpy as np



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

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

ma = 0
if start == 1:
    if len(ls) <= 2*k + 1:
        ma = len(s)
    else:
        attack_points = len(ls) // 2
        ls = np.array(ls)
        for i in range(attack_points+1-k):
            try:
                su = np.sum(ls[2*i:2*k+1+2*i])
            except IndexError:
                su = np.sum(ls[2*i:2*k+2*i])
            
            if su > ma:
                ma = su
else:
    if len(ls) <= 2*k - 1:
        ma = len(s)
    else:
        attack_points = (len(ls)+1) // 2 
        ls = np.array(ls)
        for i in range(attack_points+1-k):
            if i == 0:
                try:
                    su = np.sum(ls[:2*k])
                except IndexError:
                    su = np.sum(ls[:2*k-1])
                if su > ma:
                    ma = su
            elif i == attack_points - k:
                try:
                    su = np.sum(ls[2*i-1:2*k+2*i])
                except IndexError:
                    su = np.sum(ls[2*i-1:2*k+2*i-1])
                if su > ma:
                    ma = su
            else:
                try:
                    su = np.sum(ls[2*i-1:2*k+2*i])
                except IndexError:
                    su = np.sum(ls[2*i-1:2*k-1+2*i])
                if su > ma:
                    ma = su
print(ma)

Solution (Python):
tmp=input().split(" ")
n = int(tmp[0])
k = int(tmp[1])

s=input()

def sumrange(arr, l, r):
    
    return arr[r] - arr[l-1]

prev="1"
counts=[0]
count=0
prev_count=0
for c in s:
    if c == prev:
        count+=1
    else:
        counts.append(prev_count + count)
        prev=c
        prev_count += count
        count=1
else:
    counts.append(prev_count + count)



w = k * 2 + 1


max_score=0
for i in range(0, len(counts)-1, 2):
    if(i + 1 >= len(counts)):
        break
    l = i+1
    r = min(l + w - 1, len(counts)-1)
    score = sumrange(counts, l, r)
    max_score = max(max_score, score)

print(max_score)

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

p=S[0]
l=[]

prev_c0=0
prev_c1=0
c=0
order=0
ind=0
ans=[]
val=0

for s in S:
    if s==p:
        c+=1
    else:
        if p=='0':
            prev_c0=c
        if p=='1':
            order+=1
            val+=prev_c0+c
            l.append(prev_c0+prev_c1)
            if order>K:
                val-=l[ind]
                ind+=1
            ans.append(val)
            prev_c1=c
        c=1
        p=s
if c>=2:
    if p=='0':
        if order<K:
            ans.append(val+c)
        else:
            ans.append(val+c-l[ind])
    if p=='1':
        order+=1
        val+=prev_c0+c
        if order>K:
            val-=l[ind]
        ans.append(val)
if len(ans)>0:
    print(max(ans))
else:
    print(len(S))

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

scores = []

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

cur = 'w'
for i in range(len(S)):
    if cur == S[i]:
        scores[-1] += 1
    else:
        scores.append(1)
        cur = S[i]

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

import numpy as np
scoreCS = np.cumsum(np.array([0]+scores))

ans = -1
for l in range(0, len(scoreCS), 2):
    r = K*2 + 1 + l
    if r >= len(scoreCS):
        break
    ans = max(ans, scoreCS[r] - scoreCS[l])

if ans == -1:
    ans = scoreCS[-1]
print(ans)

Solution (Python):
import numpy as np

b = input().split()
n = int(b[0])
k = int(b[1])
s = input()

arr = []

if int(s[0]) == 0:
    arr.append(0)

before_change = 0
for i in range(1,n):
    if int(s[i - 1]) != int(s[i]):
        arr.append(i - before_change)
        before_change = i
arr.append(n - before_change)
arr = np.array(arr)


n_add = 2 * k + 1
ans = 0

if n <= n_add:
    ans = arr.sum()
else:
    i = 0
    while i + n_add <= n:
        temp = arr[i:i+n_add].sum()
        if temp > ans:
            ans = temp
        i += 2

print(ans)

Solution (Python):
import itertools

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




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

W_ac = list(itertools.accumulate(W))
W_un = list(set(S))
if len(W_ac) <= 2 * K + 1 or len(W_un) == 1:
    ans = N
    print(ans)
else:
    ans = []
    for i in range(2*K+1, len(W_ac)):
        if i % 2 == 1:
            ans.append(W_ac[i] - W_ac[i-(2*K+1)])
    print(max(ans))

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

res = 0
left = 0
right = 0

cnt = 0
for i in range(N-1):
    if S[i] == "0" and S[i+1] == "1":
        cnt += 1
    if cnt == K and S[i+1] == "0":
        right = i
        break
    if i == N - 2:
        right = N - 1
    
res = max(res, right - left + 1)

while right < N-1:
    left += 1
    right += 1
    while left < N:
        if S[left] == "1" and S[left-1] == "0":
            break
        else:
            left += 1
    while right < N - 1:
        if S[right] == "1" and S[right+1] == "0":
            break
        else:
            right += 1
    res = max(res, right - left + 1)
    
print(res)

Solution (Python):
from itertools import accumulate

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

consecutive01s = [[], [0]]
turn = 0
num = 0
for S in Ss+[9]:
    if S == turn:
        num += 1
    else:
        consecutive01s[turn].append(num)
        turn = 1-turn
        num = 1
if turn == 0:
    consecutive01s[0].append(0)
consecutive01s[1].append(0)

N0, N1 = len(consecutive01s[0]), len(consecutive01s[1])
K = min(K, N0)

acc0s = list(accumulate([0]+consecutive01s[0]))
acc1s = list(accumulate([0]+consecutive01s[1]))

len0s = [acc0s[i+K]-acc0s[i] for i in range(N0-K+1)]
len1s = [acc1s[i+K+1]-acc1s[i] for i in range(N1-K)]

ans = max([len0+len1 for len0, len1 in zip(len0s, len1s)])
print(ans)

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

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

now = None
cnt = []
before_i = 0
for i, c in enumerate(s):
	if i==0:
		now = c
		continue
	if now==c:
		continue
	else:
		cnt.append(i-before_i)
		now = c
		before_i = i
cnt.append(i+1-before_i)
l=0

if s[0] == '0':
	tmp = sum(cnt[:2*k])
	r = 2*k-1
	l0 = True
else:
	tmp = sum(cnt[:2*k+1])
	r = 2*k
	l0 = False

if r>=len(cnt):
	print(tmp)
	exit()

ans = tmp

while r+1<len(cnt):
	if l0:
		tmp = tmp - cnt[l]
		l += 1
		l0 = (cnt[l]==0)
	else:
		tmp = tmp - cnt[l] - cnt[l+1]
		l += 2
		l0 = (cnt[l]==0)
	tmp += cnt[r+1]
	if r+2<len(cnt):
		tmp += cnt[r+2]
		r += 2
	else:
		r += 1
	r0 = (cnt[r]==0)

	if tmp>ans:
		ans = tmp

print(ans)

Solution (Python):
def main():
    N, K = [int(n) for n in input().split()]
    S = input()
    S = [int(s) for s in S]
    cnt = 0
    suc = 1
    zeros = []
    ones = []
    
    for i in S:
        if i == suc:
            cnt += 1
        else:
            if suc == 0:
                zeros.append(cnt)
                cnt = 1
            elif suc == 1:
                ones.append(cnt)
                cnt = 1
            suc ^= 1
    if suc == 0:
        zeros.append(cnt)
    elif suc == 1:
        ones.append(cnt)
    
    if suc != 1:
        ones.append(0)
    
    if len(zeros) <= K:
        print(len(S))
        return
    
    
    s = sum(zeros[:K])+sum(ones[:K+1])
    
    m = s
    left = 0
    right = K-1
    while True:
        if right == len(zeros)-1:
            break
        s -= zeros[left] + ones[left]
        s += zeros[right+1] + ones[right+2]
        if s > m:
            m = s
        
        
        left += 1
        right += 1
    print(m)


if __name__ == "__main__":
    main()

Solution (Python):
import numpy

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

strList = [0, 0]
oneNum = 0
zeroNum = 0
if S[0] == "1":
    strList.append(0)

for i in range(N):
    if S[i] == "1":
        oneNum += 1
        if zeroNum != 0:
            strList.append(zeroNum)
        zeroNum = 0
    if S[i] == "0":
        zeroNum += 1
        if oneNum != 0:
            strList.append(oneNum)
        oneNum = 0

if oneNum != 0:
    strList.append(oneNum)
if zeroNum != 0:
    strList.append(zeroNum)

strList.append(0)
strList.append(0)

strCumsum = numpy.cumsum(strList).tolist()

ans = 0
for i, s in enumerate(strCumsum):
    if i % 2 == 1:
        ans = max(ans, strCumsum[i] - strCumsum[max(0, i - K * 2 - 1)])

print(ans)

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

array = [0]
result = 0
if S[0] == '1':
    flag = False

else:
    flag = True
    array.append(0)

flag2 = False

for i in range(N):
    if S[i] == '0':
        if not flag:
            flag = True
            array.append(i)
            if len(array) == K * 2 + 2:
                flag2 = True
                result = max(array[-1] - array[0], result)
                del array[0]
                del array[0]
            

    else:
        if flag:
            flag = False
            array.append(i)

result = max(N - array[0], result)

print(result if flag2 else N)

Solution (Python):



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

x = [[0, 0]]
current_s = S[0]
count_zero = 0
count_one = 0
for i, s in enumerate(S):
    if s == current_s:
        if s == '0':
            count_zero += 1
        else:
            count_one += 1
    else:
        if current_s == '0':
            x.append([count_one, count_zero])
            count_one = 1
            count_zero = 0
        else:
            count_zero = 1
        current_s = s
x.append([count_one, count_zero])    

if x[-1][-1] != 0:
    x.append([0,0])
num_white = len(x)
        
cum_x = []
current_sum = 0
for i in range(len(x)):
    current_sum += sum(x[i])
    cum_x.append(current_sum)

if num_white <= (K + 1):
    print(N)
else:
    max_value = 0
    for i in range(K, num_white-1):
        max_value = max(max_value, cum_x[i] - cum_x[i - K] + x[i + 1][0])
    print(max_value)

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

c=S[0]

T=[[c, 1]]
for s in S[1:]:
    if c==s:
        T[-1][1]+=1
    else:
        c=s
        T.append([c,1])

ans=-1
p=0
n=p
k=0
s=0
while n<len(T):
    if(T[n][0]=="0"):
        if k<K:
            k+=1
            s+=T[n][1]
        else:
            break
    else:
        s+=T[n][1]
    n+=1
ans=s
for pp in range(len(T)-1):
    p=pp+1
    if T[p][0]=="1":
        s-=T[p-1][1]
        if p+2*k-1<len(T):
            s+=T[p+2*k-1][1]
        if p+2*k<len(T):
            s+=T[p+2*k][1]
    else:
        s-=T[p-1][1]        
    ans=max(ans,s)  
print(ans)

Solution (Python):
from bisect import bisect_left, bisect_right
N, K = map(int, input().split())
S = input()


S += 'X'
Zero_begin = []

begin_checked = False
for i, s in enumerate(S):
    if not begin_checked:
        if s == '0':
            Zero_begin.append(i)
            begin_checked = True

    else:
        if s != '0':
            begin_checked = False


Zero_begin.append(N)
ans = 0
for i in range(N):
    if (i == 0) or (S[i] == '1'):
        left_i = bisect_left(Zero_begin, i)
        right_i = min(left_i + K, len(Zero_begin) - 1)
        ans = max(ans, Zero_begin[right_i] - i)

print(ans)

Solution (Python):

"""
Created on Sat Apr 13 20:57:46 2019

@author: Yamazaki Kenichi
"""
import sys

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

T = []
a,b = 0,0
if S[0] == '0':
    T.append(0)
for c in S:
    if c =='1':
        if a > 0:
            a += 1
        else:
            a = 1
            T.append(b)
            b = 0
    else:
        if b > 0:
            b += 1
        else:
            b = 1
            T.append(a)
            a = 0
T.extend([max(a,b),0,0])
if K*2+3 >= len(T):
    print(sum(T))
    sys.exit()
    
m,tmp = sum(T[1:K*2+2]),sum(T[1:K*2+2])
for i in range(0,len(T)-(K*2+3),2):


    tmp = tmp-T[i+1]-T[i+2]+T[i+K*2+2]+T[i+K*2+3]
    m = max(m,tmp)
print(m)

Solution (Python):
from sys import stdin

def ria(sep = ''):
    if sep == '' :
        return list(map(int, input().split())) 
    else: return list(map(int, input().split(sep)))
def rsa(sep = ''):
    if sep == '' :
        return input().split() 
    else: return input().split(sep)
def ri(): return int(input())
def rd(): return float(input())
def rs(): return input()



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

a = [0] * N
prev = 1
v = 0
for i in range(N):
    if S[i] != prev: v += 1
    a[i] = v
    prev = S[i]

ma = 0
l = 0
r = 0
for v in range(a[0], a[N - 1] + 1):
    while a[l] < v : l += 1
    rv = 0
    if S[l] == '0':
        rv = v + 2 * K - 1
    else :
        rv = v + 2 * K
    while r < N and a[r] <= rv: r += 1
    ma = max(ma, r - l)

print(ma)

Solution (Python):
import sys
from itertools import accumulate

def main():
    input = sys.stdin.readline
    N, K = map(int, input().split())
    S = str(input().strip())
    S += 'x'

    A = []
    S0 = S[0]
    cnt = 0
    for s in S:
        if S0 == s:
            cnt += 1
        else:
            A.append(cnt)
            cnt = 1
            S0 = s


    n = len(A) // 2
    if len(A) % 2 == 1 and S[0] == '0':
        n += 1

    if n <= K:
        return sum(A)

    if S[0] == '0':
        A = [0] + A
    if S[-2] == '0':
        A = A + [0]
    A = [0] + list(accumulate(A))

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

    return ans


if __name__ == '__main__':
    print(main())

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

l = []
n = 0
p = '1'
for s in S:
    if s != p:
        l.append(n)
        p = s
        n = 1
    else:
        n = n + 1
l.append(n)

nw = sum(l[0:(min(K * 2 + 1, N))])
mx = nw
for j in range(2,len(l),2):
    lef = l[j - 2] + l[j - 1]
    rig = 0
    if (j + K * 2 - 1) < len(l):
        rig = rig + l[j + K * 2 - 1]
        if (j + K * 2) < len(l):
            rig = rig + l[j + K * 2]
    nw = nw - lef + rig
    mx = max(mx, nw)
print(mx)

Solution (Python):
from collections import deque
N, K = map(int, input().split())
S = list(input())
d = deque()
p = 0
q = 0
num = 0
i = 0
while i < N:
    if S[i] == '0':
        tmp = 0
        while i < N and S[i] == '0':
            tmp += 1
            i += 1
        d.append(p + tmp)
        q += d[-1]
        last = 0
        if len(d) > K:
            q -= d.popleft()
        num = max(num, q)
    else:
        j = i
        while j < N and S[j] == '1':
            j += 1
        p = j - i
        i = j
        num = max(num, p + q)
print(num)

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

a = [0]
i = 0
p = "1"
for t in s:
  if t == p:
    a[i] += 1
  else:
    p = t
    i += 1
    a += [1]
a += [0]


if len(a) <= 2*k+1:
  print(sum(a))
  quit()

x = sum(a[:2*k+1])
m = x 
for i in range(2*k+1,len(a)):
  
  x = x + a[i] - a[i-(2*k+1)]
  if i%2 == 0:
    m = max(m,x)
print(m)

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

keep = []
flag = False
start, end = 0, 0
while start < N and end < N:
    if flag:
        if S[end] == "1": end += 1
        else:
            keep.append(end - start)
            start = end
            flag = False
    else:
        if S[start] == "1":
            keep.append(start - end)
            flag = True
            end = start + 1
        else:
            start += 1
if flag: keep.append(end - start)
else: keep.append(start - end)

stand = len(keep) // 2
if len(keep) % 2 == 1: 
    stand += 1
    keep.append(0)

if stand <= K: print(N)
else:
    maxL = sum(keep[:2*K])
    length = maxL
    for i in range(1, stand - K+1):
        length += keep[2*(K+i-1)]+keep[2*(K+i-1)+1] - keep[2*(i-1)]
        if i >= 2: length -= keep[2*(i-1)-1]
        maxL = max(maxL, length)
    print(maxL)

Solution (Python):
size,K = map(int, raw_input().split())
S = raw_input()
flag = 0
a = []
temp1 = []
temp2 = []
n = 0
for c in S:
	if(flag == 0):
		if(c == '1'):
			temp1.append(n)
			flag = 1
	else:
		if(c == '0'):
			flag = 0
			temp2.append(n)
	n += 1
n = 0
if(K <= len(temp2)):
	a.append(temp2[K-1])
	for i in temp1:
		if(n + K < len(temp2)):
			a.append(temp2[n + K] - i)
		else:
			a.append(size - i)
		n += 1
else:
	a.append(size)
print(max(a))

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

count = 1
if s[0] == 0:
    count_list = [0]
elif s[0] == 1:
    count_list = []

for i in range(n - 1):
    if s[i] == s[i + 1]:
        count = count + 1
    else:
        count_list.append(count)
        count = 1
count_list.append(count)

count_list2 = [0]
count2 = 0
for i in range(len(count_list)):
    count2 = count2 + count_list[i]
    count_list2.append(count2)

maximum = []
i = 0
while 2 * i < len(count_list2):
    l = count_list2[2 * i]
    if 2 * i + 2 * k + 1 > len(count_list2) - 1:
        r = count_list2[len(count_list2) - 1]
    else:
        r = count_list2[2 * i + 2 * k + 1]
    maximum.append(r - l)
    i = i + 1

print(max(maximum))

Solution (Python):
N, K = [int(_) for _ in input().split()]
S = input()
O = [[0, 0]]
is1 = False
for i in range(N):
    if S[i] == '1':
        if not is1:
            O += [[i, N]]
        is1 = True
    elif S[i] == '0':
        if is1:
            O[-1][-1] = i
        is1 = False
O += [[N, N]]
K = min([len(O) - 1, K])
ans = 0
for i in range(len(O) - K):
    ans = max([ans, O[i + K][1] - O[i][0]])
print(ans)

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

first = s[0]
end = s[-1]
len_list = []

count = 0
pre = first
for i in range(n):
    if s[i] == pre:
        count += 1
    else:
        len_list.append(count)
        pre = s[i]
        count = 1
len_list.append(count)
sum_list = [0]
sum_tmp = 0
for i in len_list:
    sum_tmp += i
    sum_list.append(sum_tmp)

size = len(len_list)

if size < 2*k + 1:
    print(n)
else:
    ans = 0
    index = 0
    if first == '0':
        index = 1
        ans = sum_list[2*k] - sum_list[0]
    if end == '0':
        ans = max((sum_list[size] - sum_list[size-2*k]), ans)

    while index + 2 * k < size:
        ans = max(ans, (sum_list[index+2*k+1] - sum_list[index]))
        index += 2

    print(ans)

Solution (Python):
N,K = list(map(int,input().split()))
S = input()
n = []
p = S[0]
c = 1
for s in S[1:]:
    if s != p:
        n.append(c)
        p = s
        c = 0
    c+=1
n.append(c)
ans = max(n)
sum_n = [0]
for i in n:
    sum_n.append(i+sum_n[-1])
if S[0]=="0":
    for i in range(1,len(n)-2*K+1,2):
        ans = max(ans,sum_n[min(i+2*K+1,len(sum_n)-1)]-sum_n[i])
    sum_n.pop(1)
    n.pop(0)
    K -=1
for i in range(0,len(n)-2*K+1,2):
    ans = max(ans,sum_n[min(i+2*K+1,len(sum_n)-1)]-sum_n[i])
if (len(n)-2*K+1)<=0:
    ans = max(ans,sum_n[-1])
print(ans)

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




l=[]
a=0
b=s[0]
for i in range(n):
  if b==s[i]:
    a+=1
  else:
    l.append(a)
    a=1
    b=s[i]
l.append(a)
l=[0,0]+l
for i in range(1,len(l)):
  l[i]+=l[i-1]
ans=0
b=int(s[0])
if k*2+1<=len(l):
  aa=[k*2-1,k*2,k*2+1]
  for a in aa:
    for i in range(b,len(l)-a,2):
      ans=max(ans,l[i+a]-l[i])
else:
  aa=[len(l)-2,len(l)-1,len(l)]
  for a in aa:
    for i in range(b,len(l)-a,2):
      ans=max(ans,l[i+a]-l[i])
print(ans)

Solution (Python):



import sys


n,k = map(int, input().split())
sn = list(str(input()))
snum = [1] * (10**5)

l = 0
for i in range(1,n):
  if sn[i-1] == sn[i]:
    snum[l] += 1
  else:
    l += 1
snum = snum[:l+1]

if sn[0] == '1':
  snum = [0] + snum
if sn[-1] == '1':
  snum = snum + [0]

csnum = [0] * (len(snum)+1)
for i in range(len(snum)):
  csnum[i+1] = csnum[i] + snum[i]



l = 0
mx = 0
for r in range(len(csnum)):
  if r&1 == 1 and (r-l+1)//2 > k:
    l += 2
  mx = max(mx, csnum[r] - csnum[max(0,l-1)])

print(mx)

Solution (Python):
N, K = map(int, input().split())
s = input()
li = [(0, 0)]
i = 0
while i < N:
    if s[i] == '1':
        l = i
    while i < N and s[i] == '1':
        i += 1
        if not (i < N and s[i] == '1'):
            break
    else:
        i += 1
        continue
    r = i - 1
    li.append((l, r))
    i += 1
li.append((N - 1, N - 1))
ans = 0
if K > len(li):
    K = len(li) - 1
for i in range(len(li) - K):
    ans = max(ans, li[i + K][1] - li[i][0])
print(ans + 1)

Solution (Python):
import math

list=input().split(" ")
n=int(list[0])
k=int(list[1])
s=input()

list2=[]
renzoku=0

if s[0]=='0':
    list2.append(0)

for x in range(n-1):
    renzoku+=1
    if int(s[x])!=int(s[x+1]):
        list2.append(renzoku)
        renzoku=0

list2.append(renzoku+1)
if len(list2)%2==0:
    list2.append(0)



kaisu=math.ceil(len(list2)/2)-k
max=0
if kaisu<=0:
    max=len(s)

for y in range(kaisu):
    
    if y==0:
        gokei=sum(list2[y*2:y*2+(2*k+1)])
    else:
        gokei-=(list2[y*2-2]+list2[y*2-1])
        gokei+=(list2[y*2+k*2-1]+list2[y*2+k*2])
    if gokei>max:
        max=gokei
print(max)

Solution (Python):
n,k = (int(i) for i in input().split())
s = input()
x,y,f,num,ans = [],[],1,0,0
for i in range(n):
    if f and s[i]=="1":
        f = 0
        x.append(i)
    elif 1-f and s[i]=="0":
        f,num = 1,num+1
        y.append(i-1)
if x and x[0]: num+=1
if int(s[-1]): y.append(n-1)
if num<=k: print(n)
else:
    if x[0]: ans = y[k-1]+1
    if s[-1]=="0": ans = max(ans,n-x[-k])
    for i in range(len(y)-k): ans = max(y[i+k]-x[i]+1,ans)
    print(ans)

Solution (Python):
from bisect import bisect_right

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

t = []
abs_t = []

cur = s[0]
cnt = 0
for ss in s:
    if cur == ss:
        cnt += 1
    else:
        sgn = 1 if cur == '1' else -1
        
        t.append(cnt * sgn)
        abs_t.append(cnt)

        cur = ss
        cnt = 1
sgn = 1 if cur == '1' else -1

t.append(cnt * sgn)
abs_t.append(cnt)

M = len(t)

sgn_cnt = [0]
for tt in t:
    sgn_cnt.append(sgn_cnt[-1] + (1 if tt < 0 else 0))


c_abst = [0]
for att in abs_t:
    c_abst.append(c_abst[-1] + att)



ans = 0
for i in range(M):
    ok = bisect_right(sgn_cnt, (K + sgn_cnt[i]))
    ok -= 1
    
    
    ans = max(ans, c_abst[min(ok, M)] - c_abst[i])
print(ans)

Solution (Python):
def handstand(N: int, K: int, S: str)->int:
    seq = [[], []]
    cur_c, cur_n = S[0], 0

    if S[0] == '0':
        seq[1].append(0)

    for c in S:
        if cur_c != c:
            seq[int(cur_c)].append(cur_n)
            cur_c = c
            cur_n = 0
        cur_n += 1
    else:
        seq[int(cur_c)].append(cur_n)

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

    if len(seq[0]) <= K:
        
        return N

    cum = [sum(seq[0][:K]), sum(seq[1][:K+1])]
    max_up = sum(cum)

    for k in range(len(seq[0])-K):
        cum[0] += seq[0][k+K] - seq[0][k]
        cum[1] += seq[1][k+K+1] - seq[1][k]

        max_up = max(max_up, sum(cum))

    return max_up


if __name__ == "__main__":
    N, K = map(int, input().split())
    S = input()
    ans = handstand(N, K, S)
    print(ans)

Solution (Python):
N, K = [int(s) for s in input().split()]
S = input()
group0 = []
group1 = []
now = '1'
now_n = 0
for c in S:
    if c == now:
        now_n += 1
    else:
        if now == '1':
            group1.append(now_n)
        else:
            group0.append(now_n)
        now = c
        now_n = 1
if now == '1':
    group1.append(now_n)
else:
    group0.append(now_n)
    group1.append(0)

first = 0
last = min(K, len(group0))
now_group = sum(group0[first:last]) + sum(group1[first:last+1])
max_group = now_group
for i in range(last, len(group0)):
    now_group += group0[i] + group1[i+1] - group0[first] - group1[first]
    max_group = max(max_group, now_group)
    first += 1
print(max_group)