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()
block = []
prev = 1
cnt = 0

for s in S:
    if str(prev) != s:
        prev = 1 - prev
        block.append(cnt)
        cnt = 1
    else:
        cnt += 1
else:
    if cnt != 0:
        block.append(cnt)
if len(block) %2 == 0:
    block.append(0)

length = 2 * K + 1
left = 0
right = min(length, len(block))
tmp = sum(block[left:right])
ans = tmp

for i in range(2,len(block),2):
    new_left = left + 2
    tmp -= sum(block[left:new_left])
    left = new_left
    new_right = min(right+2, len(block))
    tmp += sum(block[right:new_right])
    right = new_right
    ans = max(tmp, ans)
print(ans)

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

num = s.replace('10', '1,0')
num = num.replace('01', '0,1').split(',')
num = list(map(len, num))

if s[0] == '0':
    num.insert(0, 0)
if s[-1] == '0':
    num.append(0)

left = 0
right = 2*k + 1
length = sum(num[left:right])
ans = length
for i in range(2, len(num)-k, 2):
    length -= sum(num[left:left+2])
    left = i
    right = i + 2*k + 1
    length += sum(num[right-2:right])
    ans = max(ans, length)
    
print(ans)

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


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_matrix(H):
    '''
    H is number of rows
    '''
    return [list(map(int, read().split())) for _ in range(H)]


def read_map(H):
    '''
    H is number of rows
    文字列で与えられた盤面を読み取る用
    '''
    return [read() for _ in range(H)]


def read_col(H, n_cols):
    '''
    H is number of rows
    n_cols is number of cols

    A列、B列が与えられるようなとき
    '''
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])

    return ret


N, K = read_ints()
S = read()[:-1]



num_cnts = []
now = '1'  
cnt = 0
for s in S:
    if s == now:
        cnt += 1
    if s != now:  
        num_cnts.append(cnt)
        now = s
        cnt = 1
num_cnts.append(cnt)

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

assert len(num_cnts) % 2 == 1, '奇数個じゃない！'


from itertools import accumulate
num_accums = [0]+list(accumulate(num_cnts))


add = min(2*K+1, len(num_cnts))  
ans = 1


for left in range(0, len(num_cnts) - add+1, 2):
    right = left + add
    ans = max(ans, num_accums[right] - num_accums[left])
    

print(ans)

Solution (Python):

import copy
import numpy as np
import random
import bisect 
import math


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

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

if cnt!=0:
    Nums.append(cnt)

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


Sums = [0]
tmp = 0
for i in range(len(Nums)):
    tmp += Nums[i]
    Sums.append(tmp)


ans = 0
Add = 2*K+1
for i in range(len(Sums)):
    if i%2==0:
        left = i
        right = min(i+Add,len(Sums)-1)
        tmp = Sums[right]-Sums[left]
        ans = max(tmp,ans)
print(ans)

Solution (Python):

N, K = list(map(int, input().split()))
S = list(input())
ones = [0]
zeros = []
flag = '1'
for s in S:
    if s == '1':
        if flag == '1':
            ones[-1] += 1
        else:
            ones.append(1)
    else:
        if flag == '1':
            zeros.append(1)
        else:
            zeros[-1] += 1
    flag = s
ones.append(0)

tmp = sum(ones[0:K+1]) + sum(zeros[0:K])
ans = tmp
for i in range(len(zeros) - (K + 1) + 1):
    tmp = tmp + ones[i + K + 1] + zeros[i + K] - ones[i] -zeros[i]
    ans = max(ans,tmp)
print(ans)

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




change = [0]
for i in range(1,N):
    if S[i] == '1' and S[i-1] != '1':
        change.append(i)
    elif S[i] == '0' and S[i-1] != '0':
        change.append(i)

result = 0
for i, point in enumerate(change):
    if S[point] == '1':
        if i + 2 + K*2 > len(change):
            if result < N - point:
                result = N - point
            break
        if result < change[i + 1 + K*2] - 1 - point:
            result = change[i + 1 + K*2] - 1 - point
    else:
        if i + 1 + K*2 > len(change):
            if result < N - point:
                result = N - point
            break
        if i == 0:
            result = change[i + K*2]
        if result < change[i + K*2] - change[i-1]:
            result = change[i + K*2] - change[i-1]
print(result)

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

cnt = 1
for i in range(len(S)):
    if S[i:i+1] == S[i+1:i+2]:
        cnt += 1
    else:
        block.append(cnt)
        cnt = 1
block.append(0)

acc = [0, 0]
for i in range(1, len(block)):
    wa = acc[-1]+block[i]
    acc.append(wa)

if S[0] == '0':
    start = 0
else:
    start = 1
end = len(acc) - (2*K+1)

max = 0
for i in range(start, end, 2):
    if max < acc[i+(2*K+1)] - acc[i]:
        max = acc[i+(2*K+1)] - acc[i]

if start >= end:
    max = N

if len(S) == 1:
    if K == 0 and S =='0':
        max = 0
    else:
        max = 1

print(max)

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





m = 0
cur_s = cur_e = 0

while True:
  while cur_e < n and s[cur_e] == '1':
    cur_e+=1
  
  if k <= 0 or cur_e >= n:
    wm = cur_e - cur_s
    if wm > m:
      m = wm
      
    if cur_e >= n:
      break
 
    while cur_s < n and s[cur_s] == '1':
      cur_s+=1
    while cur_s < n and s[cur_s] == '0':
      cur_s+=1
    k+=1

  if k > 0:
    while cur_e < n and s[cur_e] == '0':
      cur_e+=1
    k-=1
    
print(m)

Solution (Python):


def readInts():
  return [int(s) for s in input().split(" ")]

def solve(N, K, S):
  chunks = []
  current = '1'
  count = 0
  for i, c in enumerate(S):
    if c != current:
      chunks.append(count)
      current = '0' if current == '1' else '1'
      count = 0
    count += 1
  chunks.append(count)
  if current == '0':
    chunks.append(0)

  if len(chunks) < 2 * K + 1:
    return N

  cSums = [0 for _ in range(len(chunks) + 1)]
  for i, c in enumerate(chunks):
    cSums[i + 1] = cSums[i] + c

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

  return ans

def main():
  N, K = readInts()
  S = input()

  print(solve(N, K, S))

if __name__ == "__main__":
  main()

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

current_length = 1
sections = []  
current_section = (S[0], 1)

max_length = 0

k = 0












for i in range(1, len(S)):
    
    
    if S[i - 1] == S[i]:
        current_section = (S[i], current_section[1] + 1)
        current_length += 1
    else:
        if current_section[0] == '0':
            if len(sections) > 0 and sections[-1][0] == '1':
                pop = sections.pop(-1)
                sections.append((0, pop[1] + current_section[1]))
            else:
                sections.append(current_section)
            k += 1
            if k > K:
                current_length -= sections[0][1]
                sections.pop(0)
        else:
            sections.append(current_section)

        if current_length > max_length:
            max_length = current_length

        current_section = (S[i], 1)
        current_length += 1

print(max_length)

Solution (Python):
N, k = map(int, input().split())                                                                               
s = input()
l, accum = len(s), []
zeros, ones = [], []
if s[0] == '0':
    ones = [0]
i = 0
while i < l:
    ch = s[i]
    if ch == '0':
        count = 0
        while i < l and s[i] == ch:
            count += 1
            i += 1
        zeros.append(count)
    elif ch == '1':
        count = 0
        while i < l and s[i] == ch:
            count += 1
            i += 1
        ones.append(count)
mx = sum(zeros[:k]) + sum(ones[:k+1])
ones+=[0]
for i in range(1, len(zeros)): zeros[i] += zeros[i-1]
for i in range(1, len(ones)): ones[i] += ones[i-1]
for i in range(1, len(zeros)-k+1):
    mx = max(zeros[k+i-1] + ones[k+i] - zeros[i-1] - ones[i-1], mx)
print(mx)

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

def press(array):
    pressed = []
    cnts = []
    remark = array[0]
    cnt = 0
    for i in range(len(array)):
        cnt += 1
        if i == N-1:
            pressed.append(remark)
            cnts.append(cnt)
        elif S[i] != S[i+1]:
            pressed.append(remark)
            cnts.append(cnt)
            remark = S[i+1]
            cnt = 0
    return pressed,cnts

pressed, cnts = press(list(S))
if pressed[0] == "0":
    pressed = ["1"] + pressed
    cnts = [0] + cnts
if pressed[-1] == "0":
    pressed = pressed + ["1"]
    cnts = cnts + [0]

acc = [0]*(len(cnts)+1)
for i in range(len(cnts)):
    acc[i+1] = acc[i] + cnts[i]

ans = 0

for l in range(0,len(cnts),2):
    r = min(l + 2*K,len(acc)-2)
    res = acc[r+1] - acc[l]
    ans = max(ans,res)

print(ans)

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

one_list = []
zero_list = []

cur_char = S[0]
char_num = 0
for i in range(N):
    if S[i] == cur_char:
        char_num += 1
    else:
        if cur_char == '0':
            zero_list.append(char_num)
        else:
            one_list.append(char_num)
        cur_char = S[i]
        char_num = 1
if cur_char == '0':
    zero_list.append(char_num)
else:
    one_list.append(char_num)

len_zero_list = len(zero_list)
len_one_list = len(one_list)



cont_length_list = []

if K >= len(zero_list):
    ans = N
else:
    if S[0] == '1':
        tmp_length = sum(one_list[:K+1])+sum(zero_list[:K])
        cont_length_list.append(tmp_length)
        for i in range(min(len_zero_list,len_one_list)-K):
            if K+1+i<len_one_list:
                tmp_length += one_list[K+1+i]+zero_list[K+i]
            else:
                tmp_length += zero_list[K+i]
            tmp_length -= one_list[i]+zero_list[i]
            cont_length_list.append(tmp_length)
        
    else:
        tmp_length = sum(one_list[:K])+sum(zero_list[:K])
        cont_length_list.append(tmp_length)
        if K == len_one_list:
            tmp_length+=zero_list[K]
            tmp_length -= zero_list[0]
            cont_length_list.append(tmp_length)
        if K < len_zero_list-1:
            tmp_length += zero_list[K]+one_list[K]
            tmp_length -= zero_list[0]
            cont_length_list.append(tmp_length)
            for i in range(1, len_zero_list-K):
                if K+i<len_one_list and K+i < len_zero_list:
                    tmp_length += one_list[K+i]+zero_list[K+i]
                else:
                    tmp_length += zero_list[K+i]
                tmp_length -= one_list[i-1]+zero_list[i]
                cont_length_list.append(tmp_length)
        
    
    ans = max(cont_length_list)

print(ans)

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


def solve(S, K):
    record = []
    temp = []
    for i in range(len(S) - 1):
        if S[i:i+2] == '01':
            temp.append(i + 1)
        elif S[i:i+2] == '10':
            if not temp: temp = [0, i]
            else: temp.append(i)
            record.append(temp)
            temp = []
    if len(temp) > 0:
        temp.append(len(S) - 1)
        record.append(temp)
    if S[0] == '0':
        record = [[0, 0]] + record
    if S[-1] == '0':
        record = record + [[len(S) - 1, len(S) - 1]]
    if len(record) <= K + 1: return(len(S))
    else:
        cnt = 0
        for i in range(len(record) - K):
            cnt = max(cnt, record[i + K][1] - record[i][0] + 1)
        return(cnt)
print(solve(S, K))

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

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

if S[0]=='0':
    L.insert(0,0)
if S[-1]=='0':
    L.append(N)

max_r = 0
step = K*2+1
if len(L)-step<1:
    step=len(L)-1
for i in range(0, len(L)-step,2):
    max_r = max(max_r, L[i+step]-L[i])

print(max_r)

Solution (Python):
import sys






"""Template"""


class IP:
    """
    入力を取得するクラス
    """

    def __init__(self):
        self.input = sys.stdin.readline

    def I(self):
        """
        1文字の取得に使います
        :return: int
        """
        return int(self.input())

    def S(self):
        """
        1文字の取得(str
        :return: str
        """
        return self.input()

    def IL(self):
        """
        1行を空白で区切りリストにします(int
        :return: リスト
        """
        return list(map(int, self.input().split()))

    def SL(self):
        """
        1行の文字列を空白区切りでリストにします
        :return: リスト
        """
        return list(map(str, self.input().split()))

    def ILS(self, n):
        """
        1列丸々取得します(int
        :param n: 行数
        :return: リスト
        """
        return [int(self.input()) for _ in range(n)]

    def SLS(self, n):
        """
        1列丸々取得します（str
        :param n: 行数
        :return: リスト
        """
        return [self.input() for _ in range(n)]

    def SILS(self, n):
        """
        Some Int LineS
        横に複数、縦にも複数
        :param n: 行数
        :return: list
        """
        return [self.IL() for _ in range(n)]

    def SSLS(self, n):
        """
        Some String LineS
        :param n: 行数
        :return: list
        """
        return [self.SL() for _ in range(n)]


class Idea:
    def __init__(self):
        pass

    def HF(self, p):
        """
        Half enumeration
        半分全列挙です
        pの要素の和の組み合わせを作ります。
        ソート、重複削除行います
        :param p: list : 元となるリスト
        :return: list : 組み合わせられた和のリスト
        """
        return sorted(set(p[i] + p[j] for i in range(len(p)) for j in range(i, len(p))))

    def Bfs2(self, a):
        """
        bit_full_search2
        bit全探索の改良版
        全探索させたら2進数のリストと10進数のリストを返す
        :return: list２つ : １個目 2進数(16桁)  2個目 10進数
        """
        
        
        
        value = []
        for i in range(1 << len(a)):
            output = []

            for j in range(len(a)):
                if self.bit_o(i, j):
                    """右からj+1番目のiが1かどうか判定"""
                    
                    output.append(a[j])
            value.append([format(i, 'b').zfill(16), sum(output)])

        value.sort(key=lambda x: x[1])
        bin = [value[k][0] for k in range(len(value))]
        val = [value[k][1] for k in range(len(value))]
        return bin, val

    def S(self, s, r=0, m=-1):
        """
        ソート関係行います。色々な設定あります。
        :param s: 元となるリスト
        :param r: reversするかどうか 0=False 1=True
        :param m: （2次元配列）何番目のインデックスのソートなのか
        :return: None
        """
        r = bool(r)
        if m == -1:
            s.sort(reverse=r)
        else:
            s.sort(reverse=r, key=lambda x: x[m])

    def bit_n(self, a, b):
        """
        bit探索で使います。0以上のときにTrue出します
        自然数だからn
        :param a: int
        :param b: int
        :return: bool
        """
        return bool((a >> b & 1) > 0)

    def bit_o(self, a, b):
        """
        bit探索で使います。1のときにTrue出すよ
        oneで1
        :param a: int
        :param b: int
        :return: bool
        """
        return bool(((a >> b) & 1) == 1)

    def ceil(self, x, y):
        """
        Round up
        小数点切り上げ割り算
        :param x: int
        :param y: int
        :return: int
        """
        return -(-x // y)

    def ave(self, a):
        """
        平均を求めます
        :param a: list
        :return: int
        """
        return sum(a) / len(a)

    def gcd(self, x, y):
        if y == 0:
            return x
        else:
            return self.gcd(y, x % y)


"""ここからメインコード"""


def main():
    
    r, e, p = range, enumerate, print
    ip = IP()
    id = Idea()
    mod = 10 ** 9 + 7

    """この下から書いてね"""
    
    n, k = ip.IL()
    s = input() + "2"

    
    zeros = []
    ones = []

    
    cnt = 0
    start = 1

    for x in s:
        
        if start == 1 and x != "1":
            ones += [cnt]
            cnt = 1
            start = 0
        
        elif start == 0 and x != "0":
            zeros += [cnt]
            cnt = 1
            start = 1
        
        else:
            cnt += 1

    
    if len(zeros) == len(ones):
        ones += [0]

    ones += [0]
    
    
    ans = sum(ones[:k+1]) + sum(zeros[:k])
    n_ans = ans

    for i in r(len(zeros) - k):
        
        n_ans = n_ans + ones[k+1+i] + zeros[k+i] - ones[i] - zeros[i]
        ans = max(ans, n_ans)

    p(ans)


main()

Solution (Python):
from itertools import accumulate
n, k = map(int, input().split())
s = input()
a = []
cnt = 0
now = s[0]
for ss in s:
    if ss == now:
        cnt += 1
    else:
        a.append(cnt)
        cnt = 1
        now = ss
a.append(cnt)

a = [0]*(1-int(s[0])) + a + [0]*(1-int(s[-1]))
la = len(a)
kk = 2*k+1
if la == 1 or la <= kk:
    print(n)
    exit()
    
ac = [0] + list(accumulate(a))
ans = -float('inf')
for i in range(kk, la+1, 2):
    ans = max(ans, ac[i] - ac[i-kk])
print(ans)

Solution (Python):
"""
map(int,input().split())

"""
N,K = map(int,input().split())
S = input()
s = []
for i in range(0,len(S)):
    s.append(S[i])


Nums = []
now = 1
cnt = 0
for i in range(0,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
W = []

wsm = 0
for i in range(0,len(Nums)):
    wsm = Nums[i] + wsm
    W.append(wsm)

W.append(wsm)
W.append(wsm)

for i in range(0,len(Nums),2):
    tmp = 0
    left = i
    if left == 0:
        left = 1
    right = min(i + Add , len(Nums))
    if left-1<=0:
        tmp = W[right-1]
    else:
        tmp = W[right-1] - W[left-1]
    
    ans = max(tmp,ans)
if  N == 1:
    print(1)
else:
    print(ans)

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


count=0
now=1
num_list=[]
for c_i in s:
    if c_i == now:
        count+=1
    else:
        num_list.append(count)
        count=1
        now=1-now
num_list.append(count)


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


max_len=0
now_sum=0
left=0
right=0

for i in range(0,len(num_list),2):
    nextleft=i
    nextright=min(len(num_list),i+2*k+1)

    while left<nextleft:
        now_sum-=num_list[left]
        left+=1
    while right<nextright:
        now_sum+=num_list[right]
        right+=1
    max_len=max(max_len,now_sum)
    
print(max_len)

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

l, accum = len(s), []
i = 0
while i < l:
    ch = s[i]
    count = 0
    while i < l and s[i] == ch:
        count += 1
        i += 1
    accum.append(count)

l = len(accum)
for i in range(1, l): accum[i] += accum[i-1]

mx = accum[min(2*k - (1-int(s[0])), l-1)]
i = 1
while l > 1:
    idx = min(2*k - (1-int(s[0])) + 2*i, l-1)
    mx = max(accum[idx] - accum[2*(i-1)+int(s[0])], mx)
    if idx >= l-1: break
    i += 1
print(mx)

Solution (Python):
import sys






"""Template"""


class IP:
    """
    入力を取得するクラス
    """

    def __init__(self):
        self.input = sys.stdin.readline

    def I(self):
        """
        1文字の取得に使います
        :return: int
        """
        return int(self.input())

    def S(self):
        """
        1文字の取得(str
        :return: str
        """
        return self.input()

    def IL(self):
        """
        1行を空白で区切りリストにします(int
        :return: リスト
        """
        return list(map(int, self.input().split()))

    def SL(self):
        """
        1行の文字列を空白区切りでリストにします
        :return: リスト
        """
        return list(map(str, self.input().split()))

    def ILS(self, n):
        """
        1列丸々取得します(int
        :param n: 行数
        :return: リスト
        """
        return [int(self.input()) for _ in range(n)]

    def SLS(self, n):
        """
        1列丸々取得します（str
        :param n: 行数
        :return: リスト
        """
        return [self.input() for _ in range(n)]

    def SILS(self, n):
        """
        Some Int LineS
        横に複数、縦にも複数
        :param n: 行数
        :return: list
        """
        return [self.IL() for _ in range(n)]

    def SSLS(self, n):
        """
        Some String LineS
        :param n: 行数
        :return: list
        """
        return [self.SL() for _ in range(n)]


class Idea:
    def __init__(self):
        pass

    def HF(self, p):
        """
        Half enumeration
        半分全列挙です
        pの要素の和の組み合わせを作ります。
        ソート、重複削除行います
        :param p: list : 元となるリスト
        :return: list : 組み合わせられた和のリスト
        """
        return sorted(set(p[i] + p[j] for i in range(len(p)) for j in range(i, len(p))))

    def Bfs2(self, a):
        """
        bit_full_search2
        bit全探索の改良版
        全探索させたら2進数のリストと10進数のリストを返す
        :return: list２つ : １個目 2進数(16桁)  2個目 10進数
        """
        
        
        
        value = []
        for i in range(1 << len(a)):
            output = []

            for j in range(len(a)):
                if self.bit_o(i, j):
                    """右からj+1番目のiが1かどうか判定"""
                    
                    output.append(a[j])
            value.append([format(i, 'b').zfill(16), sum(output)])

        value.sort(key=lambda x: x[1])
        bin = [value[k][0] for k in range(len(value))]
        val = [value[k][1] for k in range(len(value))]
        return bin, val

    def S(self, s, r=0, m=-1):
        """
        ソート関係行います。色々な設定あります。
        :param s: 元となるリスト
        :param r: reversするかどうか 0=False 1=True
        :param m: （2次元配列）何番目のインデックスのソートなのか
        :return: None
        """
        r = bool(r)
        if m == -1:
            s.sort(reverse=r)
        else:
            s.sort(reverse=r, key=lambda x: x[m])

    def bit_n(self, a, b):
        """
        bit探索で使います。0以上のときにTrue出します
        自然数だからn
        :param a: int
        :param b: int
        :return: bool
        """
        return bool((a >> b & 1) > 0)

    def bit_o(self, a, b):
        """
        bit探索で使います。1のときにTrue出すよ
        oneで1
        :param a: int
        :param b: int
        :return: bool
        """
        return bool(((a >> b) & 1) == 1)

    def ceil(self, x, y):
        """
        Round up
        小数点切り上げ割り算
        :param x: int
        :param y: int
        :return: int
        """
        return -(-x // y)

    def ave(self, a):
        """
        平均を求めます
        :param a: list
        :return: int
        """
        return sum(a) / len(a)

    def gcd(self, x, y):
        if y == 0:
            return x
        else:
            return self.gcd(y, x % y)


"""ここからメインコード"""


def main():
    
    r, e, p = range, enumerate, print
    ip = IP()
    id = Idea()
    mod = 10 ** 9 + 7

    """この下から書いてね"""
    n, k = ip.IL()
    s = input() + "2"

    zeros = []
    ones = []
    cnt = 0
    start = 1
    for x in s:
        if start == 1 and x != "1":
            ones += [cnt]
            cnt = 1
            start = 0
        elif start == 0 and x != "0":
            zeros += [cnt]
            cnt = 1
            start = 1
        else:
            cnt += 1

    if len(zeros) == len(ones):
        ones += [0]

    ones += [0]

    ans = sum(ones[:k+1]) + sum(zeros[:k])
    n_ans = ans

    for i in r(len(zeros) - k):
        n_ans = n_ans + ones[k+1+i] + zeros[k+i] - ones[i] - zeros[i]
        ans = max(ans, n_ans)

    p(ans)


main()

Solution (Python):
"""
方針:累積和(0初期化配列のやりかた), 最後が0終わりのケア, 累積和の範囲
"""

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


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




if tmp == '0':
    L.append(0)  





M = len(L)
if int(M/2) <= K:
    print(N)
    exit()

X = [0 for _ in range(M+1)]

for i in range(M):
    X[i+1] = X[i] + L[i]


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

print(ans)

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

left = 0
right = 0
ans = 0
remain = K
if S[0] == "0":
    remain -= 1

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

Solution (Python):
import sys






"""Template"""


class IP:
    """
    入力を取得するクラス
    """

    def __init__(self):
        self.input = sys.stdin.readline

    def I(self):
        """
        1文字の取得に使います
        :return: int
        """
        return int(self.input())

    def S(self):
        """
        1文字の取得(str
        :return: str
        """
        return self.input()

    def IL(self):
        """
        1行を空白で区切りリストにします(int
        :return: リスト
        """
        return list(map(int, self.input().split()))

    def SL(self):
        """
        1行の文字列を空白区切りでリストにします
        :return: リスト
        """
        return list(map(str, self.input().split()))

    def ILS(self, n):
        """
        1列丸々取得します(int
        :param n: 行数
        :return: リスト
        """
        return [int(self.input()) for _ in range(n)]

    def SLS(self, n):
        """
        1列丸々取得します（str
        :param n: 行数
        :return: リスト
        """
        return [self.input() for _ in range(n)]

    def SILS(self, n):
        """
        Some Int LineS
        横に複数、縦にも複数
        :param n: 行数
        :return: list
        """
        return [self.IL() for _ in range(n)]

    def SSLS(self, n):
        """
        Some String LineS
        :param n: 行数
        :return: list
        """
        return [self.SL() for _ in range(n)]


class Idea:
    def __init__(self):
        pass

    def HF(self, p):
        """
        Half enumeration
        半分全列挙です
        pの要素の和の組み合わせを作ります。
        ソート、重複削除行います
        :param p: list : 元となるリスト
        :return: list : 組み合わせられた和のリスト
        """
        return sorted(set(p[i] + p[j] for i in range(len(p)) for j in range(i, len(p))))

    def Bfs2(self, a):
        """
        bit_full_search2
        bit全探索の改良版
        全探索させたら2進数のリストと10進数のリストを返す
        :return: list２つ : １個目 2進数(16桁)  2個目 10進数
        """
        
        
        
        value = []
        for i in range(1 << len(a)):
            output = []

            for j in range(len(a)):
                if self.bit_o(i, j):
                    """右からj+1番目のiが1かどうか判定"""
                    
                    output.append(a[j])
            value.append([format(i, 'b').zfill(16), sum(output)])

        value.sort(key=lambda x: x[1])
        bin = [value[k][0] for k in range(len(value))]
        val = [value[k][1] for k in range(len(value))]
        return bin, val

    def S(self, s, r=0, m=-1):
        """
        ソート関係行います。色々な設定あります。
        :param s: 元となるリスト
        :param r: reversするかどうか 0=False 1=True
        :param m: （2次元配列）何番目のインデックスのソートなのか
        :return: None
        """
        r = bool(r)
        if m == -1:
            s.sort(reverse=r)
        else:
            s.sort(reverse=r, key=lambda x: x[m])

    def bit_n(self, a, b):
        """
        bit探索で使います。0以上のときにTrue出します
        自然数だからn
        :param a: int
        :param b: int
        :return: bool
        """
        return bool((a >> b & 1) > 0)

    def bit_o(self, a, b):
        """
        bit探索で使います。1のときにTrue出すよ
        oneで1
        :param a: int
        :param b: int
        :return: bool
        """
        return bool(((a >> b) & 1) == 1)

    def ceil(self, x, y):
        """
        Round up
        小数点切り上げ割り算
        :param x: int
        :param y: int
        :return: int
        """
        return -(-x // y)

    def ave(self, a):
        """
        平均を求めます
        :param a: list
        :return: int
        """
        return sum(a) / len(a)

    def gcd(self, x, y):
        if y == 0:
            return x
        else:
            return self.gcd(y, x % y)


"""ここからメインコード"""


def main():
    
    r, e, p = range, enumerate, print
    ip = IP()
    id = Idea()
    mod = 10 ** 9 + 7

    """この下から書いてね"""
    n, k = ip.IL()
    s = input()

    zeros = []
    ones = [0]
    prev = "1"
    for x in s:
        if x == "1":
            if prev == "0":
                ones += [1]
            else:
                ones[-1] += 1
        else:
            if prev == "1":
                zeros += [1]
            else:
                zeros[-1] += 1
        prev = x
    ones += [0]

    tmp = sum(zeros[0:k]) + sum(ones[0:k+1])

    ans = tmp
    for i in r(0, len(zeros) - k):
        tmp += zeros[i+k] - zeros[i] + ones[i+k+1] - ones[i]
        ans = max(tmp, ans)

    p(ans)


main()

Solution (Python):
n,k = (int(i) for i in input().split())
s = input()
change = []
leng = len(s)
flag = ''
ans = 0
w = 2*k+1

if s[0] == '0':
  change.append(0)
  
for i in range(leng):
  if s[i] != flag:
    flag = s[i]
    change.append(i)

change.append(leng)

if s[-1] == '0':
  change.append(leng)
leng = len(change)

if leng <= w:
  print(n)
  exit()

for i in range(0,leng-w,2):
  ans = max(ans,change[i+w]-change[i])
print(ans)

Solution (Python):
from itertools import accumulate
 
N, K = map(int, input().split())
S = input()
S = [int(s) for s in S]
 
nums = list()
now = 1
count_tmp = 0
for i in range(N):
    if S[i] == now:
        count_tmp += 1
    else:
        nums.append(count_tmp)
        now = 1 - now
        count_tmp = 1
if count_tmp > 0:
    nums.append(count_tmp)
if len(nums) % 2 == 0:
    nums.append(0)
 
sums = list(accumulate([0] + nums))
 
max_add = 2 * K
ans = 0
for i in range(0, len(nums), 2):
    left = i
    right = min(i + max_add, len(nums) - 1)
    tmp = sums[right + 1] - sums[left]
    ans = max(tmp, ans)
print(ans)

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

ones = [0]
zeros = []
flag = '1'
for s in S:
    if s == '1':
        if flag == '1':
            ones[-1] += 1
        else:
            ones.append(1)
    else:
        if flag == '1':
            zeros.append(1)
        else:
            zeros[-1] += 1
    flag = s
ones.append(0)

tmp = sum(ones[0 : K + 1]) + sum(zeros[0 : K])
ans = tmp
for i in range(len(zeros) - (K + 1) + 1):
    tmp = tmp + ones[i + K + 1] - ones[i] + zeros[i + K] - zeros[i]
    ans = max(ans, tmp)
    
print(ans)