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

#include "stdio.h"

int main(void)
{
    int N, K;
    char S[100000];
    int count, max_loc, count_max;
    int num_one_before, num_one_after;    
    int zero_flag = 0;
    int one_flag  = 0;    
    int count_sum = 0;
    int test ;
        
    
    scanf("%d %d", &N, &K);
    scanf("%s", S);

    for(int j = 0; j < K; j++){
    num_one_before = 0;
    num_one_after  = 0;
    count_max      = 0;
    max_loc        = 0;
    count          = 0;
    zero_flag      = 0;
    one_flag       = 0;
    count_sum      = 0;
    
    for(int i = 0; i < 100000; i++){
        if(S[i] == '0' && zero_flag == 0){
            num_one_before = num_one_before + 1;            
            zero_flag = 1;            
        }
        else if(S[i] == '0' && zero_flag == 1 && one_flag == 0){
            num_one_before = num_one_before + 1;
            zero_flag = 1;                       
        }        
         //elseなのでこの条件に来たらzero_flag==1は確定
        else if(S[i] == '1' && zero_flag == 0){
            num_one_before = num_one_before + 1;
            one_flag = 1;            
        }
        else if(S[i] == '1' && zero_flag == 1){
            num_one_after  = num_one_after  + 1;
            one_flag = 1;                        
        }
        //リセット
        else if(S[i] == '0' && zero_flag == 1 && one_flag == 1){
            /* printf("reset\n");            */ 
            zero_flag      = 1;
            one_flag       = 0;            
            count          = num_one_before + num_one_after;
            if(count > count_max){
                count_max = count;
                max_loc   = i - 1 - num_one_after;                
                for(int j = i - 2 - num_one_after; j >= 0 ;j--){
                    if(S[j] == '0'){
                        max_loc = max_loc - 1;
                    }
                    else {
                        break;
                    }
                    
                }
                

            }            
            num_one_before = num_one_after + 1;
            num_one_after  = 0;            
        }
        /* printf("i num:%d\n", i); */
        /* printf("S[i] :%c\n", S[i]); */
        /* printf("one_flag:%d\n", one_flag); */
        /* printf("zero_flag:%d\n", zero_flag);         */
        /* printf("num_one_before :%d\n", num_one_before);         */
        /* printf("num_one_after :%d\n",  num_one_after); */
        /* printf("max_loc :%d\n", max_loc); */
        
        if(S[i] == '\0'){
            count          = num_one_before + num_one_after;
            /* printf("last count :%d\n", count); */
            /* printf("count_max :%d\n", count_max);     */                    
            if(count > count_max){
                count_max = count;
                max_loc   = i - 1 - num_one_after;                
                for(int j = i - 1 - num_one_after; j >= 0 ;j--){
                    if(S[j] == '0'){
                        /* printf("j : %d",         j);                         */
                        /* printf("max_loc : %d\n", max_loc);                         */
                        max_loc = j;
                    }
                    else {                 
                        break;
                    }
                }
            }
            break;            
        }
    }
    /* printf("max_loc : %d\n", max_loc); */
    for(int i = max_loc; i < 100000; i++){
        if(S[i] == '1' || S[i] == '\0')break;        
        S[i] = '1';        
    }
    /* printf("%s\n", S); */
    }
    
    count = 0;    
    for(int i = 0; i < 100000; i++){
        if(S[i] == '1'){
            count = count + 1;
        }        
        else if(S[i] == '0'){           
            if(count > count_sum){
                count_sum = count;
            }
            count = 0;
        }
        else{
            if(count > count_sum){
                count_sum = count;
            }            
            break;
        }
    /* printf("count: %d\n", count);                 */
    }
    printf("%d", count_sum);
    
            
    
    return 0;
}