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 = input()

l = [["",1]]
for c in s:
    if c!=l[-1][0]:
        l.append([c,1])
    else:
        l[-1][1]+=1
      
l.pop(0)
cnt0 = 0
dp = []
z = []
for i in range(len(l)):
    if l[i][0] == '0':
        cnt0 += 1
        z.append((i,l[i][1]))

if len(z)==0:
    print(n)
    exit(0)
for i in range(cnt0):
    idx,cnt = z[i]
    currcnt = 0
    if idx-1>=0:
        currcnt += l[idx-1][1]
    if i >0:
        currcnt += dp[-1][1]
    currcnt+=cnt
    dp.append((idx,currcnt))

res = 0

for i in range(len(dp)):
    tmp = 0
    if i-k>=0:
        tmp = dp[i][1]-dp[i-k][1]
    else:
        tmp = dp[i][1]
    if dp[i][0]+1<len(l):
        tmp += l[dp[i][0]+1][1]
    res = max(res,tmp)
print(res)

Solution (Python):


def solve():
    N, K = list(map(int, input().split(" ")))
    S = input()

    
    
    ruiseki = []
    i = 0
    while True:
        if i >= N:
            break
        
        num = 0
        if S[i] == "1":
            while i < N and S[i] == "1":
                num += 1
                i += 1
            ruiseki.append(["1", num])
        else:
            while i < N and S[i] == "0":
                num += 1
                i += 1
            ruiseki.append(["0", num])
    
    if ruiseki[0][0] == "0":
        i = 0
    else:
        i = 1

def solve2():
    N, K = list(map(int, input().split(" ")))
    S = input()

    i = 0
    ruiseki = [0]
    while True:
        if i >= N:
            break
        
        num = 0
        if S[i] == "1":
            while i < N and S[i] == "1":
                num += 1
                i += 1
            ruiseki.append(num + ruiseki[-1])
        else:
            while i < N and S[i] == "0":
                num += 1
                i += 1
            ruiseki.append(num + ruiseki[-1])

    import math
    if S[0] == "1":
        zero_num = math.floor((len(ruiseki) - 1) / 2)
    else:
        zero_num = math.ceil((len(ruiseki)-1)/2)

    if K >= zero_num:
        print(ruiseki[-1])
        return

    ans = 0
    if S[0] == "1":
        for i in range(2, len(ruiseki), 2):
            end = i + 2*(K-1)
            if end >= len(ruiseki):
                break
            if end+1 < len(ruiseki):
                
                end += 1

            ans = max(ruiseki[end] - ruiseki[i-2], ans)

    else:
        for i in range(1, len(ruiseki), 2):
            end = i + 2*(K-1)
            if end >= len(ruiseki):
                break
            if end+1 < len(ruiseki):
                end += 1

            if i-2 >= 0:              
                ans = max(ruiseki[end]-ruiseki[i-2], ans)
            else:
                ans = max(ruiseki[end]-ruiseki[i-1], ans)

    print(ans)            




if __name__ == "__main__":
    solve2()

Solution (Python):
def mark_zero1(s):
    index = [i for i, s_ in enumerate(s) if s_ == "0" and safe_get(s, i - 1) != "0"]
    return index


def mark_zero2(s):
    index = [i for i, s_ in enumerate(s) if s_ == "0" and safe_get(s, i + 1) != "0"]
    return index


def safe_get(lis, index):
    if index < 0:
        return -1
    elif index >= len(lis):
        return len(s)
    else:
        return lis[index]


def get_score(a, b, i, j):
    return safe_get(a, j + 1) - safe_get(b, i - 1) - 1


n, k = list(map(int, input().split(" ")))
s = input()
a = mark_zero1(s)
b = mark_zero2(s)


try_num = min(len(a), k)
ij_list = [(idx, idx + try_num - 1) for idx in range(len(a) - try_num + 1)]

print(max([get_score(a, b, i, j) for i, j in ij_list]))

Solution (Python):
n, k = map(int, input().split())
s = input()
if s.count("1") == n or s.count("0") == n:
    print(n)
    exit()
zero = 0
flag = 0
for i in s:
    if i == "0":
        flag = 1
    else:
        if flag:
            zero += 1
        flag = 0
if flag:
    zero += 1
k = min(zero, k)
renzoku = []
count = 0
flag = 0
for i in range(n):
    if s[i] == "1":
        if flag:
            count += 1
        else:
            renzoku.append(count)
            count = 1
        flag = 1
    else:
        if flag:
            renzoku.append(count)
            count = 1
        else:
            count += 1
        flag = 0
renzoku.append(count)
renzoku_t = [0]
for i in range(len(renzoku)):
    renzoku_t.append(renzoku[i] + renzoku_t[i])
ans = 0
if k == zero:
    print(n)
    exit()
if s[0] == "0":
    ans = renzoku_t[0 + k + k] - renzoku_t[0]
for i in range(1, len(renzoku) - k - k + 1, 2):
    if i == 0 or i == len(renzoku) - k - k:
        ans = max(ans, renzoku_t[i + k + k] - renzoku_t[i])
    else:
        ans = max(ans, renzoku_t[i + k + k + 1] - renzoku_t[i])
print(ans)

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

sentous = []
sentou = 0
counts = [0]
count = 0
prev = 0
for s in S:
    count += 1
    sentou += 1
    if s != prev and s == '1':
        if count > 1:
            sentous.append(sentou - 1)
        sentou = 1
    elif s != prev and s == '0':
        
        if K > 0:
            K -= 1
        else:
            counts.append(count - 1)
            count -= sentous.pop(0)
    prev = s

print(max(max(counts), count))

Solution (Python):


import sys


INF = float('inf')
MOD = 10 ** 9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


def main():
    n, k = LI()
    s = '0' + SI() + '0'

    L = []
    R = []
    if s[1] == '0':
        L.append(1)
        R.append(1)
    for i in range(n + 1):
        if s[i] == '1' and s[i + 1] == '0':
            R.append(i)
        if s[i] == '0' and s[i + 1] == '1':
            L.append(i + 1)

    if s[-2] == '0':
        L.append(n)
        R.append(n)

    res = R[0] - L[0] + 1
    k = min(len(R) - 1, k)
    for i in range(len(R) - k):
        res = max(res, R[i + k] - L[i] + 1)

    
    

    return res


print(main())

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

ans = 0
fpos = []
p = '0'
for i in range(len(S)):
    if p == '0' and S[i] == '1':
        fpos.append(i)
    if p == '1' and S[i] == '0':
        fi = len(fpos) - 1 - K
        if fi >= 0:
            l = (i-1) - (fpos[fi]-1)
            if l > ans:
                ans = l
    p = S[i]

            

p = '1'
cnt = 0
for i in range(len(S)):
    if p == '0' and S[i] == '1':
        cnt += 1
    if p == '1' and S[i] == '0':
        if cnt == K:
            l = i
            if l > ans:
                ans = l
            break
    if i == len(S) - 1:
        l = len(S)
        if l > ans:
            ans = l
        break
    p = S[i]


p = '1'
cnt = 0
for i in range(len(S)-1, -1, -1):
    if p == '0' and S[i] == '1':
        cnt += 1
    if p == '1' and S[i] == '0':
        if cnt == K:
            l = (len(S)-1) - i
            if l > ans:
                ans = l
            break
    if i == 0:
        l = len(S)
        if l > ans:
            ans = l
        break
    p = S[i]

print(ans)

Solution (Python):
import numpy as np
words = lambda t : list(map(t, input().split()))

n,k =  words(int)
s = input()

stt = s[0]

cnts = []
cur = stt
lcnt = 0
for c in s:
    if c == cur:
        lcnt += 1
    else:
        cnts.append(lcnt)
        lcnt = 1
        if cur == '0':
            cur = '1'
        else:
            cur = '0'
cnts.append(lcnt)
zsum = 0
if stt == '0':
    sumstt = 1
    for i in range(min(k*2,len(cnts))):
        zsum += cnts[i]
else:
    sumstt = 0


sum = 0

for i in range(sumstt, min(len(cnts),sumstt+(k*2+1))):
    sum += cnts[i]


maxsum = max(zsum,sum)

l = sumstt + 2
r = sumstt + k*2+1 + 2

while r <= len(cnts):
    sum = sum - cnts[l-2] - cnts[l-1] + cnts[r-2] + cnts[r-1]
    
    maxsum = max(maxsum, sum)
    l += 2
    r += 2


if s[-1] == '0' and r-2 < len(cnts):
    sum = sum - cnts[l-2] - cnts[l-1] + cnts[r-2]
    
    maxsum = max(maxsum, sum)

print(maxsum)

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

first_one = S[0] == '1'
cnts = []
cnt = 1
for i, e in enumerate(S):
    if i == 0:
        pass
    else:
        if e != S[i-1]:
            cnts.append(cnt)
            cnt = 1
        else:
            cnt += 1
cnts.append(cnt)


zero_obj = []
for i, c in enumerate(cnts):
    if first_one:
        if i % 2 == 1:
            left = 0
            if i - 1 >= 0:
                left = cnts[i-1]
            right = 0
            if i + 1 < len(cnts):
                right = cnts[i+1]
            zero_obj.append((left, c, right))
    else:
        if i % 2 == 0:
            left = 0
            if i - 1 >= 0:
                left = cnts[i-1]
            right = 0
            if i + 1 < len(cnts):
                right = cnts[i+1]
            zero_obj.append((left, c, right))

if len(zero_obj) <= K:
    print(len(S))
else:
    m = -1
    tmp = zero_obj[0][0]
    for i in range(K):
        tmp += zero_obj[i][1] + zero_obj[i][2]
    m = tmp
    for i in range(len(zero_obj) - K):
        tmp -= zero_obj[i][0] + zero_obj[i][1]
        tmp += zero_obj[i+K][1] + zero_obj[i+K][2]
        if tmp > m:
            m = tmp
    print(m)

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


l=[]
p="1"
q=0
for i in s:
    if i==p:
        q+=1
    else:
        l.append(q)
        q=1
        if p=="1":
            p="0"
        else:
            p="1"
l.append(q)

r=[0]
def ruiseki(a):
    for i in range(len(a)):
        r.append(r[i]+a[i])
ruiseki(l)
ans=0

if len(r)-(2*k+2)+1<=0:
    print(r[-1])
    import sys
    sys.exit()
    
for i in range(len(r)-(2*k+2)+1):
    if (i+2*k+1)%2==1:
        ans=max(ans,r[i+2*k+1]-r[i])
ans=max(ans,r[-1]-r[len(r)-(2*k+2)+1])
print(ans)

Solution (Python):
n, k = map(int, input().split())
s = input()
start = [0]
ret = 0
first = False
for i in range(len(s)):
    if s[i] == '1' and first:
        start.append(i)
        first = False
    if s[i] == '0' and not first:
        ret = max(ret, i - start[max(0, len(start) - k - 1)])
        first = True
if first:
    start.append(len(s))
ret = max(ret, len(s) - start[max(0, len(start) - k - 1)])
print(ret)

Solution (Python):
N , K = map(int,input().split())
S = list(input())
row = []
for i in range(N):
	if i == 0:
		judge = S[i]
		row.append(1)
	else:
		if judge == S[i]:
			row[-1] += 1
		else:
			judge = S[i]
			row.append(1)
if S[-1] == "0":
	row.append(0)
if S[0] == "0":
	row.insert(0,0)
max_row = 2 * K + 1

if max_row >= len(row):
	print(N)
else:
	sum_count = []
	temp_sum = 0
	for i in range(max_row):
		temp_sum += row[i]
	sum_count.append(temp_sum)
	i = max_row
	while i < len(row):
		sum_count.append(sum_count[-1] + row[i] + row[i+1] - row[i-max_row] - row[i-max_row+1])
		i += 2
	print(max(sum_count))

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, K, S):
    s1 = list(map(lambda x: len(x), filter(lambda x: x != '', S.split('0'))))
    s0 = list(map(lambda x: len(x), filter(lambda x: x != '', S.split('1'))))


    if len(s0) <= K:
        return N
    
    
    
    

    ans = 0
    s0_top = None
    if S[0] == '0':
        s0_top = s0.pop(0)
        ans = s0_top + sum(s1[:K]) + sum(s0[:K-1])
    
    l1o = 1
    l0o = 0
    l1 = sum(s1[:K+l1o])
    l0 = sum(s0[:K+l0o])
    k = 0
    
    while True:
        tmp = l1 + l0
        
        
        ans = max(ans, tmp)
        if k+K < len(s1) and k+K < len(s0):
            l1 -= s1[k]
            if k+K+l1o < len(s1):
                l1 += s1[k+K+l1o]
            l0 -= s0[k]
            if k+K+l0o < len(s0):
                l0 += s0[k+K+l0o]
        else:
            break
        k += 1
    

    return ans


def main():
    N, K = read_int_n()
    S = read_str()
    rS = ''.join(['0' if c == '1' else '1' for c in S])
    print(max(slv(N, K, S), slv(N, K-1, rS)))

    
    
    
    
    

if __name__ == '__main__':
    main()

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

aa = []
prev = None
for c in s:
    if prev != c:
        aa.append((c, 1))
        prev = c
    else:
        _, count = aa[-1]
        aa[-1] = (prev, count + 1)

kk = 0
candidate = 0
total = 0
i = 0
if aa[i][0] == '1':
    total = aa[i][1]
    candidate = total
    i += 1


while i < len(aa):
    if aa[i][0] == '0':
        if kk < k:
            kk += 1
            total += aa[i][1]
            if i < len(aa) - 1:
                i += 1
                total += aa[i][1]
            candidate = max(candidate, total)
        else:
            total -= aa[i - k * 2][1]
            if i - k * 2 - 1 >= 0:
                total -= aa[i - k * 2 - 1][1]

            total += aa[i][1]
            if i < len(aa) - 1:
                i += 1
                total += aa[i][1]
            candidate = max(candidate, total)

    i += 1

print(candidate)

Solution (Python):



from itertools import chain
from functools import reduce
from collections import OrderedDict, Counter, deque
import operator

IS = lambda: int(input())
IA = lambda: [int(x) for x in input().split()]
IM = lambda N: [IA() for _ in range(N)]

N, K = IA()
S = input()

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

lc = len(con)
if lc <= K * 2 + 1:
    print(sum(con))
else:
    tl = K * 2 + 1
    cur = sum(con[:tl])
    ans = cur
    turn = False
    for l, r in zip(con[:lc - tl], con[tl:]):
        cur -= l
        cur += r
        if turn:
            ans = max(ans, cur)
        turn = not turn
    print(ans)

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

T = [0]
now = S[0]
start = now
for i in range(1,N):
    if S[i]!=now:
        now=S[i]
        T.append(i)

T.append(N)
max_score = 0
for i in range(len(T)):
    if start=="0":
        if i%2==0:
            r=min(2*K+i,len(T)-1)
            l=max(0,i-1)
            max_score = max(T[r]-T[l],max_score)
    else:
        if i%2==1:
            r=min(2*K+i,len(T)-1)
            l=max(0,i-1)
            max_score = max(T[r]-T[l],max_score)
            
print(max_score)

Solution (Python):
import numpy as np
import sys

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

consecutive_ones = [len(x) for x in s.split("0") if x != '']
consecutive_zeros = [len(x) for x in s.split("1") if x != '']

if len(consecutive_zeros) == 0:
    print(n)
    sys.exit(0)

if len(consecutive_ones) == 0:
    if k > 0:
        print(n)
    else:
        print(0)
    sys.exit(0)

cumsum_ones = list(np.convolve(consecutive_ones, np.ones(k+1), mode='valid'))
cumsum_zeros = list(np.convolve(consecutive_zeros, np.ones(k), mode='valid'))

if s[0] == "0":
    cumsum_ones.insert(0, sum(consecutive_ones[:k]))

if s[-1] == "0":
    cumsum_ones.append(sum(consecutive_ones[-k:]))

z = [x + y for x,y in zip(cumsum_ones, cumsum_zeros)]
result = int(max(z))
print(result)

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



human = []

is_handstood = False
cnt = 0

for ch in s:
    if ch == "0" and not is_handstood:
    	human.append(cnt)
    	is_handstood = True
    	cnt = 1
    elif ch == "1" and is_handstood:
    	human.append(cnt)
    	is_handstood = False
    	cnt = 1
    else:
        cnt += 1
human.append(cnt)
if is_handstood:
    human.append(0)

total_human = [0 for i in range(len(human))]
total_human[0] = human[0]
for i in range(1, len(human)):
    total_human[i] = total_human[i - 1] + human[i]

if len(human) <= 2 * k + 1:
    max_human = n
else:
    max_human = total_human[k * 2]
    for i in range(k * 2 + 2, len(total_human), 2):
        if total_human[i] - total_human[i - k * 2 - 1] > max_human:
        	max_human = total_human[i] - total_human[i - k * 2 - 1]

print(max_human)

Solution (Python):
import sys
from collections import deque

input = sys.stdin.readline
n, k = map(int, input().split())
s_list = list(map(int, list(input()[:-1])))
new_list = []
tmp = 0
for s in s_list:
    if s == 1:
        if tmp < 0:
            new_list.append(tmp)
            tmp = 1
        else:
            tmp += 1
    else:
        if tmp > 0:
            new_list.append(tmp)
            tmp = -1
        else:
            tmp -= 1
new_list.append(tmp)
q = deque([]) 
new_len = len(new_list)
if new_list[0] < 0:
    flag = 0
else:
    flag = 1
check = 0
cnt = 0
tmp = 0
for p in new_list:
    if p > 0:
        q.append(p)
        tmp += p
    else:
        if check < k:
            q.append(p)
            check += 1
            tmp += abs(p)
        else:
            pop_p = q.popleft()
            if pop_p > 0: 
                tmp -= pop_p
                tmp -= abs(q.popleft())
            else:
                tmp -= abs(pop_p)
            q.append(p)
            tmp += abs(p)
    cnt = max(cnt, tmp)
print(cnt)

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

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(len(li)):
    cursum[i+1] = cursum[i]+ li[i]

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

ans = 0
for i in range(1,len(cursum)):
    if is_s_1:
        low_limit = max(i-2*k-1,0)
    else:
        low_limit = max(i-2*k,0)
    ans = max(ans,cursum[i] - cursum[low_limit])
    is_s_1 = not is_s_1

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

Solution (Python):
N, K = map(int, input().split())
S = list(input())
left = 0
right = 0
onecnt = 0
zerocnt = 0
zeroflag = False
leftflag = False
ans = 0

while right < N:
    if S[right] == '0' and not zeroflag:
        zerocnt += 1
        zeroflag = True
    elif S[right] == '1' and zeroflag:
        zeroflag = False
        
    if zerocnt > K and zeroflag:
        tmp = right - left
        if tmp > ans:
            ans = tmp
        while True:
            if S[left] == '0' and not leftflag:
                leftflag = True
            elif S[left] == '1' and leftflag:
                leftflag = False
                break
            left += 1
        zerocnt -= 1
    right += 1

if right - left > ans:
    ans = right - left

print(ans)

Solution (Python):
import numpy as np
N, K = map(int, input().split())
x = list(input())
xn = "1"
c = 0
y = []
for n in range(N):
    if x[n] == xn:
        c += 1
    else:
        xn = x[n]
        y.append(c)
        c = 1
y.append(c)
if len(y) % 2 == 0:
    y.append(0)
y = np.array(y)
l = 2 * K + 1
if l >= len(y):
    print(np.sum(y))
else:
    z = [np.sum(y[2 * i:2 * i + l]) for i in range((len(y) + 3 - l) // 2)]
    print(max(z))

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

def main():
    n, k = [int(i) for i in input().split()]
    s = input() + "3"
    s0 = []
    s1 = []
    cnt = 0
    start = 1
    for i in range(n+1):
        if start == 1 and s[i] != "1":
            s1.append(cnt)
            cnt = 1
            start = 0
        elif start == 0 and s[i] != "0":
            s0.append(cnt)
            cnt = 1
            start = 1
        else:
            cnt += 1

    
    
    

    ans = sum(s1[0:0+k+1]) + sum(s0[0:0+k])
    n_ans = ans

    for i in range(n):
        try:
            n_ans = n_ans + s1[k+1+i] + s0[k+i] - s1[i] - s0[i]
            ans = max(ans, n_ans)
        except:
            try:
                n_ans = n_ans + s0[k+i] - s1[i] - s0[i]
                ans = max(ans, n_ans)
            except:
                pass

    print(ans)
    

main()

Solution (Python):
n, k = map(int, input().split())
s = input()
s += "@" 
ans, tmp = 0, 0
r, l = 0, 0
t, j = 0, 0
d = {}
cnt = 0
while True:
    bit = s[r] 
    if bit == "@": 
        tmp = r - l
        ans = max(ans, tmp)
        break
    if bit == "1":
        tmp = r - l + 1
        ans = max(ans, tmp)
    else:
        if cnt == k:
            l = d[j]; j += 1
            cnt -= 1

        while s[r+1] == "0":
            r += 1
        d[t] = r+1; t += 1
        cnt += 1

    r += 1 

print(ans)

Solution (Python):
def renzoku(S):
    N = len(S)
    ans = []
    tmp = [0] * N
    for i in range(N-1):
        if S[i] == S[i+1]:
          tmp[i+1] = tmp[i] + 1  
    for i in range(N-1):
        if tmp[i] < tmp[i+1]:
            pass
        else:
            ans.append(  (S[i], tmp[i]+1)  )
    
    i = N-1
    ans.append(  (S[i], tmp[i]+1)  )
    
    
    
    return ans


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

    ren = renzoku(S)
    num = [ r[1] for r in ren]
    typ = [ r[0] for r in ren]
    numnew = len(ren)

    i = 0
    j = -1
    if typ[i] == "0":
        j = K*2 - 1
        pass
    else:
        j = K*2 
        pass

    if numnew <= j:
        
        print(N)
        exit()
    

    
    tmp = 0
    p = i
    
    while p <= j:
        tmp += num[p]
        p += 1
    
    
    ans = max(ans, tmp)
    while True:
        i += 1
        ans = max(ans, tmp)
        if typ[i] == "0":
            tmp -= num[i-1] 
        else:
            tmp -= num[i-1]
            prevJ = j
            j = min(j+2, numnew-1) 
            for p in range(prevJ+1, j+1):
                tmp += num[p]
            ans = max(ans, tmp)
        if j == numnew -1:
            break
    print(ans)

Solution (Python):
n,k=map(int,input().split())
s=input()
z=[]
c=1
if n==1:
    print(1)
    exit()
for i in range(1,n):
    if s[i]==s[i-1]:
        c+=1
    else:
        z+=[c]
        c=1
z+=[c]
if s[0]=="0":
    z=[0]+z
if s[-1]=="0":
    z=z+[0]
lz=len(z)
pre=sum(z[0:2*k+1])
mx=pre
for i in range(2,lz-2*k,2):
    pre=pre-z[i-2]-z[i-1]+z[i+2*k-1]+z[i+2*k]
    if pre>mx:
        mx=pre
print(mx)

Solution (Python):

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

hu=[]
ima=""
imal=0
for i in range(N):
    if i == 0:
        ima=Slist[0]
        imal=1
        if Slist[0] == "0":
            hu.append(0)
    else:
        if ima==Slist[i]:
            imal+=1
        else:
            hu.append(imal)
            ima=Slist[i]
            imal=1
hu.append(imal)
Ngu=len(hu)
rui=[0]
for i in range(Ngu):
    if i == 0:
        rui.append(hu[0])
    else:
        karui=rui[i]+hu[i]
        rui.append(karui)

        
MAX=0

for i in range(Ngu+1):
    if i%2==0:
        if 2*K+i-1<Ngu+1:
            if i > 0:
                kari=rui[2*K+i-1]-rui[i-1]
            else:
                kari=rui[2*K+i-1]
        else:
            if i > 0:
                kari=rui[-1]-rui[i-1]
            else:
                kari=rui[-1]
    else:
        if 2*K+i<Ngu+1:
            if i > 0:
                kari=rui[2*K+i]-rui[i-1]
            else:
                kari=rui[2*K+i]
        else:
            if i > 0:
                kari=rui[-1]-rui[i-1]
            else:
                kari=rui[-1]
    MAX=max(kari,MAX) 
print(MAX)

Solution (Python):
import math

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

S=input()
zerostart=0

if S[0]=='0':
  zerostart=1

continouslist=[]

nowchar=S[0]
nowcon=1
for i in range(1,len(S)):
  if nowchar!=S[i]:
    continouslist.append(nowcon)
    nowcon=1
    nowchar=S[i]
  else:
    nowcon+=1
continouslist.append(nowcon)

if zerostart:
  
  if K>=math.ceil(len(continouslist)/2):
    print(N)
    exit()
  else:
    now=0
    ans=0
    for i in range(len(continouslist)):
      now+=continouslist[i]
      if K<=int(i/2) and i%2==0:
        now-=continouslist[i-2*K]
        if (i-2*K)!=0:
          now-=continouslist[i-2*K-1]
      
      ans=max(now,ans)
  
else:
  
  if K>=math.floor(len(continouslist)/2):
    print(N)
    exit()
  else:
    now=0
    ans=0
    for i in range(len(continouslist)):
      now+=continouslist[i]
      if K<int(i/2)+1 and i%2==1:
        now-=continouslist[i-2*K]
        now-=continouslist[i-2*K-1]
      
      ans=max(now,ans)

print(ans)

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


    
check="1"
seq=0
for i in s:
    if check!=i:
        check=i
        li.append(seq)
        seq=1
    else:
        seq+=1
        
li.append(seq)

for  i in range(len(li)):
    if i%2==0:
        li_o.append(li[i])
    else:
        li_z.append(li[i])
    
if s[n-1]=="0":
    li_o.append(0)


if len(li_o)<=k:
    print(n)
else:
    m=sum(li_o[0:0+k+1])+sum(li_z[0:0+k])
    tmp=m
    for i in range(1,len(li_o)-k):
        tmp=tmp-li_o[i-1]+li_o[i+k]-li_z[i-1]+li_z[i+k-1]
        m=max(tmp,m)
        
    print(m)

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

kukan = []
cnt0 = 0
cnt1 = 0
for n in range(N):
        if S[n] == '0':
                cnt0 +=1
                if cnt1 > 0:
                        kukan.append(cnt1)
                cnt1 = 0
        if S[n] == '1':
                cnt1 +=1
                if cnt0 > 0:
                        kukan.append(cnt0)
                cnt0 = 0

kukan.append(max(cnt1,cnt0))


ans = []

if S[0] == '0':
        start = 1
else:
        start = 0

l = len(kukan)

if l == 1:
        print(N)
        exit()

cnt = [0]*(l+1)
for i in range(1,l+1):
        cnt[i] = cnt[i-1] + kukan[i-1]


        


for i in range(1,l-K+1):
        if (i+start)%2 == 1:
                
                ans.append(cnt[min(l,i+2*K)]-cnt[i-1])
        else:
                
                ans.append(cnt[min(l,i+2*K-1)]-cnt[i-1])


print(max(ans))

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

o = []
z = []

i = 0
while True:
  cnt = 0
  while i <= N-1 and int(S[i]) == 1:
    cnt += 1
    i += 1
  o.append(cnt)
  if i > N-1:
    break
  cnt = 0
  while i <= N-1 and int(S[i]) == 0:
    cnt += 1
    i += 1
  z.append(cnt)



if len(z) <= K:
  print(len(S))
  sys.exit()

num = o[0]
for j in range(K):
  num += z[j]+o[j+1]

max_num = num
for j in range(K, len(z)):
  num -= o[j-K]+z[j-K]
  num += z[j]+o[j+1]
  max_num = max(max_num, num)

print(max_num)

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

ls = []
temp = 0
n = 1
for i in S:
    if n == i:
        temp += 1
    else:
        ls.append(temp)
        n = i
        temp = 1
if temp != 0: 
    ls.append(temp)
if n == 0:
    ls.append(0)

if K * 2 + 1 > len(ls):
    m = 0
    for i in ls:
        m += i
    print(m)
else:
    m = 0
    temp = 0
    for i in range(2*K + 1):
        temp += ls[i]
    m = temp
    for i in range(len(ls) // 2 - K):
        temp = temp - ls[2*i] - ls[2*i+1] + ls[2*K + 2*i + 1] + ls[2*K + 2*i + 2]
        if m < temp:
            m = temp

    print(m)

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

isOne = True
cnt = 0
ans = 0
for i in S:
    if isOne:
        if i == "0":
            countList.append(cnt)
            isOne = False
    else:
        if i == "1":
            countList.append(cnt)
            isOne = True
    cnt += 1
countList.append(cnt)
if(isOne == False):
    countList.append(cnt)
K = min(len(countList)//2, K)
for i in range(len(countList)):
    if i*2+2*K<len(countList):
        if i*2-1 < 0:
            ans = max(ans, countList[i*2+2*K])
            continue
        ans = max(ans, countList[i*2+2*K]-countList[i*2-1])
    else:
        break

if len(countList)==1:
    ans = countList[0]
print(ans)

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

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(len(li)):
    cursum[i+1] = cursum[i]+ li[i]

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

ans = 0
for i in range(1,len(cursum)):
    if is_s_1:
        low_limit = max(i-2*k-1,0)
    else:
        low_limit = max(i-2*k,0)
    ans = max(ans,cursum[i] - cursum[low_limit])
    is_s_1 = not is_s_1

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

Solution (Python):
_n,_k = map(int,input().split())
_s = input()
_w=[]
if _s[0]=='0':
    _w.append(0)
ps=''
for s in _s:
    if ps==s:
        _w[-1]+=1
    else:
        _w.append(1)
        ps=s
if ps=='0':
    _w.append(0)
lw=len(_w)
wsum=sum(_w[0:(2*_k+1)])
wsum_max=wsum
for j in range(0,lw,2):
    if j+2*_k+1>=lw:
        break
    wsum-=_w[j]
    wsum-=_w[j+1]
    wsum+=_w[j+2*_k+1]
    wsum+=_w[j+2*_k+2]
    if wsum_max<wsum:
        wsum_max=wsum
print(wsum_max)

Solution (Python):

import numpy as np
from functools import lru_cache
import time

def main(): 
    if True:
        N,K = list(map(int,input().split()))
        S = np.asarray(list(map(int,list(str(input())))))
    else:
        N,K = [10**5,1]
        T = np.asarray(range(N))
        S = np.zeros(N)
        S[T%2==0] = 1

    SN = np.zeros(10**5+2)
    first = S[0]
    tmpnum = first
    k = 0
    if first == 0:
        k += 1
    k += 1

    count = 0
    for n in range(N)[1:]:
        if S[n] == tmpnum:
            count += 1
        else:
            count += 1
            SN[k] = count
            k += 1
            tmpnum = S[n]
    else:
        count += 1
        SN[k] = count
        if tmpnum == 0:
            SN[k+1] = count

    max = 0
    flag = 0
    for n in range(SN.size)[2*K+1::2]:
        if SN[n] == 0: break
        if max < SN[n] - SN[n-2*K-1]:
            max = SN[n] - SN[n-2*K-1]
        flag = 1
    
    if flag == 0:
        print(int(np.max(SN)))
    else:
        print(int(max))

if __name__ =='__main__':
    main()