101243: [AtCoder]ABC124 D - Handstand

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

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.

Input

题意翻译

### 题目描述 有一个长为 $n$ 的字符串 $s$,只含 $0$ 和 $1$。 你可以进行最多 $k$ 次如下操作($0$ 次也可以): - 选择字符串 $s$ 的一个子串,将其中的字符反转($0$ 变成 $1$,$1$ 变成 $0$)。 进行不超过 $k$ 次操作后,求最长的连续的 $1$ 的长度。 ### 输入格式 第一行,$2$ 个正整数 $n,k$; 第二行,字符串 $s$。 ### 输出格式 输出不超过 $k$ 次操作后,最长的连续的 $1$ 的长度。 ### 数据约定 对于 $100\%$ 的数据:$1 \le n, k \le 10^5$。 字符串 $s$ 只由 $0$ 和 $1$ 组成,长度为 $n$。

加入题单

上一题 下一题 算法标签: