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 C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <iostream>
#include <numeric>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n, k;
  string s;
  cin >> n >> k;
  cin >> s;

  vector<int> stand_size(n, 0);
  vector<int> back_size(n, 0);
  int stand_c = 0;
  int back_c = 0;

  int group_num = 0;

  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '0') {
      stand_c++;
      if (back_c > 0) {
        back_size[group_num] = back_c;
        back_c = 0;
        group_num++;
      }
    } else {
      back_c++;
      if (stand_c > 0) {
        stand_size[group_num] = stand_c;
        stand_c = 0;
        group_num++;
      }
    }

    if (i == (s.size() - 1)) {
      if (back_c > 0) {
        back_size[group_num] = back_c;
      } else {
        stand_size[group_num] = stand_c;
      }
      group_num++;
    }
  }

  int max_ind_center = 0;
  int max_sum = 0;
  for (int i = 1; i < group_num; i+=2) {
    int sum;
    if (s[0] == '1') {
      sum = back_size[i-1] + stand_size[i] + back_size[i+1];
    } else {
      if ((i - 2) < 0) {
        sum = stand_size[i-1] + back_size[i];
      } else {
        sum = back_size[i-2] + stand_size[i-1] + back_size[i];
      }
    }
    if (sum > max_sum) {
      max_sum = sum;
      max_ind_center = i;
    }
  }

  int prior = max_ind_center;
  int nex = max_ind_center;
  for (int i = 0; i < (k - 1); i++) {
    int comp_prior = 0;
    int comp_next = 0;
    if (s[0] == '1') {
      if (prior < 2) {
        comp_prior = 0;
      } else if (prior == 2) {
        comp_prior += stand_size[prior-2];
      } else {
        comp_prior += stand_size[prior-2] + back_size[prior-3];
      }

      if (nex >= (group_num - 1)) {
        comp_next = 0;
      } else if (nex >= (group_num - 2)) {
        comp_next += stand_size[nex+2];
      } else {
        comp_next += stand_size[nex+2] + back_size[nex+3];
      }
    } else {
      if (prior < 3) {
        comp_prior = 0;
      } else if (prior == 3) {
        comp_prior += stand_size[prior-3];
      } else {
        comp_prior += stand_size[prior-3] + back_size[prior-4];
      }

      if (nex >= (group_num - 1)) {
        comp_next = 0;
      } else if (nex >= (group_num - 2)) {
        comp_next += stand_size[nex+1];
      } else {
        comp_next += stand_size[nex+1] + back_size[nex+2];
      }
    }
    if (comp_prior > comp_next) {
      max_sum += comp_prior;
      prior -= 2;
    } else {
      max_sum += comp_next;
      nex += 2;
    }
  }

  if (max_sum == 0) {
    max_sum = 1;
  }

  cout << max_sum << endl;

  return 0;
}