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 in Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {

	private static final char ZERO = '0';
	private static final char ONE = '1';

	public static void main(String[] args) {
		// 1. 入力データの処理
		Scanner sc = new Scanner(System.in);
		//int strLen = sc.nextInt();
		//int commandNum = sc.nextInt();
		//String inputS = sc.next();
		int strLen = 5;
		int commandNum = 1;
		String inputS = "00010";
		sc.close();
		System.out.println(inputS);
		// 2. 0,1の配列に分割
		// 要素数を格納
		List<Integer> block0 = new ArrayList<>(strLen);
		List<Integer> block1 = new ArrayList<>(strLen);
		generateBlock(block0,block1,strLen,inputS);

		// 3. 左からkブロック0を反転していき、1が連続する数を出力
		tryAllPatern(block0,block1,commandNum);

	}

	private static void tryAllPatern(List<Integer> block0, List<Integer> block1, int commandNum) {
		// 試行数
		int tryNum = block0.size() - commandNum + 1;

		int sumMax = 0;
		for(int i = 0; i < tryNum; i++) {
			int sumTmp = 0;
			// 0ブロックが2以上あって、block0が要素数0だったらとばす
			if(i == 0 && block0.get(0) == 0 && block0.size() >= 2) {
				continue;
			}

			// commandNum + 1個の1の要素数を足す(0ブロックに隣接する1のブロック)
			// 対象とする0ブロックの左の1ブロックの要素数を足す
			if(i - 1 >= 0) {
				sumTmp += block1.get(i - 1);
			}

			for(int j = 0; j < commandNum; j++) {
				sumTmp += block1.get(i + j);
			}

			// 0の要素数を足す
			for(int j = 0; j < commandNum; j++) {
				sumTmp += block0.get(i + j);
			}
			if(sumTmp > sumMax) {
				sumMax = sumTmp;
			}
		}
		System.out.println(sumMax);
	}

	// 0ブロック,1ブロックの順で分割する
	// (0ブロック,1ブロック)の周期であるとする
	// はじめが1ではじまっていたり、最後が0で終わっていた場合、要素数0を格納
	private static void generateBlock(List<Integer> block0, List<Integer> block1, int strLen,
			String inputS) {
		// 左端が1であれば要素数0の0ブロックを追加
		if(inputS.charAt(0) != ZERO) {
			block0.add(0);
		}
		// 文字列におけるインデックスのカウンター
		int counterIndex = 0;
		while(counterIndex != strLen) {
			int counter = 0;
			if(inputS.charAt(counterIndex) == ZERO) {
				// 0のとき
				while(counterIndex < strLen && inputS.charAt(counterIndex) == ZERO) {
					counter++;
					counterIndex++;
				}
				//System.out.println("0:" + counter);
				block0.add(counter);
			} else if(inputS.charAt(counterIndex) == ONE) {
				// 1のとき
				while(counterIndex < strLen && inputS.charAt(counterIndex) == ONE) {
					counter++;
					counterIndex++;
				}
				//System.out.println("1:" + counter);
				block1.add(counter);
			}
		}

		// 右端が0であれば要素数0の1ブロックを追加
		if(inputS.charAt(strLen - 1) != ONE) {
			block1.add(0);
		}
	}

}