Language: C++

#include<bits/stdc++.h>
using namespace std;

vector<pair<long long, long long> >vec;
long long memo[1000000];
long long N, K;
string S;

int main(){
	cin >> N >> K;
	cin >> S;

	if(S[0] == '0'){
		vec.push_back(make_pair(0, 1));
	}
	
	char ch =  '-';
	long long num = 0;
	for(long long i = 0; i < S.size(); i++){
		if(i == 0){
			ch = S[i];
			num = 1;
		}else{
			if(ch == S[i]){
				num++;
			} else {
				vec.push_back(make_pair(num, ch-'0'));
				ch = S[i];
				num = 1;
			}
		}
	}
	vec.push_back(make_pair(num, ch-'0'));
	if(ch == '0'){
		vec.push_back(make_pair(0, 1));
	}

	for(long long i = 0; i < vec.size(); i++){
		memo[i+1] = memo[i] + vec[i].first;
	}

	long long zerocount = 0;
	for(long long i = 0; i < vec.size(); i++){
		if(vec[i].second == 0){
			zerocount++;
		}
	}
	if(zerocount <= K){
		cout << memo[vec.size()] << endl;
		return 0;
	}


	long long ans = 0;
	for(long long i = 0; i < vec.size(); i+= 2){
		if(i + 2 * K < vec.size()){
			ans = max(ans, memo[i + 2*K + 1] - memo[i]);
		}
	}
	while(true){
		if(skip < K){
			if(vec[right].second == 0){
				skip++;
			}
			right++;
		}
		ans = max(ans, memo[right] - memo[left]);

		if(skip ==  K && vec[right].second == 1){
			right++;
		}
		ans = max(ans, memo[right] - memo[left]);

		if(skip == K){
			if(vec[left].second == 0){
				skip--;
			}
			left++;
		}
		ans = max(ans, memo[right] - memo[left]);

		if(right == vec.size() + 1){
			cout << ans << endl;
			return 0;
		}
	}
	cout << ans << endl;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:61:20: error: 'skip' was not declared in this scope
   61 |                 if(skip < K){
      |                    ^~~~
a.cc:62:32: error: invalid conversion from 'std::ios_base& (*)(std::ios_base&)' to 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-fpermissive]
   62 |                         if(vec[right].second == 0){
      |                                ^~~~~
      |                                |
      |                                std::ios_base& (*)(std::ios_base&)
In file included from /usr/include/c++/14/vector:66,
                 from /usr/include/c++/14/functional:64,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:53,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_vector.h:1128:28: note:   initializing argument 1 of 'std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::operator[](size_type) [with _Tp = std::pair<long long int, long long int>; _Alloc = std::allocator<std::pair<long long int, long long int> >; reference = std::pair<long long int, long long int>&; size_type = long unsigned int]'
 1128 |       operator[](size_type __n) _GLIBCXX_NOEXCEPT
      |                  ~~~~~~~~~~^~~
a.cc:65:25: warning: ISO C++ forbids incrementing a pointer of type 'std::ios_base& (*)(std::ios_base&)' [-Wpointer-arith]
   65 |                         right++;
      |                         ^~~~~
a.cc:65:30: error: lvalue required as increment operand
   65 |                         right++;
      |                              ^~
a.cc:67:36: error: invalid types 'long long int [1000000][std::ios_base&(std::ios_base&)]' for array subscript
   67 |                 ans = max(ans, memo[right] - memo[left]);
      |                                    ^
a.cc:67:50: error: invalid types 'long long int [1000000][std::ios_base&(std::ios_base&)]' for array subscript
   67 |                 ans = max(ans, memo[right] - memo[left]);
      |                                                  ^
a.cc:69:20: error: 'skip' was not declared in this scope
   69 |                 if(skip ==  K && vec[right].second == 1){
      |                    ^~~~
a.cc:69:38: error: invalid conversion from 'std::ios_base& (*)(std::ios_base&)' to 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-fpermissive]
   69 |                 if(skip ==  K && vec[right].second == 1){
      |                                      ^~~~~
      |                                      |
      |                                      std::ios_base& (*)(std::ios_base&)
/usr/include/c++/14/bits/stl_vector.h:1128:28: note:   initializing argument 1 of 'std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::operator[](size_type) [with _Tp = std::pair<long long int, long long int>; _Alloc = std::allocator<std::pair<long long int, long long int> >; reference = std::pair<long long int, long long int>&; size_type = long unsigned int]'
 1128 |       operator[](size_type __n) _GLIBCXX_NOEXCEPT
      |                  ~~~~~~~~~~^~~
a.cc:70:25: warning: ISO C++ forbids incrementing a pointer of type 'std::ios_base& (*)(std::ios_base&)' [-Wpointer-arith]
   70 |                         right++;
      |                         ^~~~~
a.cc:70:30: error: lvalue required as increment operand
   70 |                         right++;
      |                              ^~
a.cc:72:36: error: invalid types 'long long int [1000000][std::ios_base&(std::ios_base&)]' for array subscript
   72 |                 ans = max(ans, memo[right] - memo[left]);
      |                                    ^
a.cc:72:50: error: invalid types 'long long int [1000000][std::ios_base&(std::ios_base&)]' for array subscript
   72 |                 ans = max(ans, memo[right] - memo[left]);
      |                                                  ^
a.cc:74:20: error: 'skip' was not declared in this scope
   74 |                 if(skip == K){
      |                    ^~~~
a.cc:75:32: error: invalid conversion from 'std::ios_base& (*)(std::ios_base&)' to 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-fpermissive]
   75 |                         if(vec[left].second == 0){
      |                                ^~~~
      |                                |
      |                                std::ios_base& (*)(std::ios_base&)
/usr/include/c++/14/bits/stl_vector.h:1128:28: note:   initializing argument 1 of 'std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::operator[](size_type) [with _Tp = std::pair<long long int, long long int>; _Alloc = std::allocator<std::pair<long long int, long long int> >; reference = std::pair<long long int, long long int>&; size_type = long unsigned int]'
 1128 |       operator[](size_type __n) _GLIBCXX_NOEXCEPT
      |                  ~~~~~~~~~~^~~
a.cc:78:25: warning: ISO C++ forbids incrementing a pointer of type 'std::ios_base& (*)(std::ios_base&)' [-Wpointer-arith]
   78 |                         left++;
      |                         ^~~~
a.cc:78:29: error: lvalue required as increment operand
   78 |                         left++;
      |                             ^~
a.cc:80:36: error: invalid types 'long long int [1000000][std::ios_base&(std::ios_base&)]' for array subscript
   80 |                 ans = max(ans, memo[right] - memo[left]);
      |                                    ^
a.cc:80:50: error: invalid types 'long long int [1000000][std::ios_base&(std::ios_base&)]' for array subscript
   80 |                 ans = max(ans, memo[right] - memo[left]);
      |                                                  ^
a.cc:82:26: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
   82 |                 if(right == vec.size() + 1){
      |                    ~~~~~~^~~~~~~~~~~~~~~~~