Language: C++

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

int main() {
	int N, K;
	string S;
	cin >> N >> K;
	cin >> S;
	int count = 0;
	for (int i = 0; i < N; i++) {
		if (i == 0) {
			if (S.at(i) == '1') {
				count++:
			}
		}
		else {
			if (S.at(i) == '1' && S.at(i - 1) == '0') {
				count++:
			}
		}
	}
	if (S.at(0) == '0'&&S.at(N - 1) == '0') {
		vector<int> vec1(count, 0);
		vector<int> vec0(count + 1, 0);
		int count1 = -1;
		int count0 = 0;
		for (int i = 0; i < N; i++) {
			if (i == 0) {
				vec0.at(count0)++;
			}
			else if (S.at(i) == '1') {
				count1++;
				vec1.at(count1)++;
			}
			else if (S.at(i) = 0) {
				count0++;
				vec0.at(count0)++;
			}
			else if (S.at(i) == '1') {
				vec1.at(count1)++;
			}
			else if (S.at(i) == '0') {
				vec0.at(count0)++;
			}

		}
		int maxi = 0;
		if (K > count) {
			maxi = N;
		}
		else {
			vector<int> ma(count - K + 2, 0);
			for (int i = 0; i < K; i++) {
				ma.at(0) += vec0.at(i) + vec1.at(i);
			}
			ma.at(1) = ma.at(0) - vec0.at(0) + vec0.at(K - 1) + vec1(K - 1);
			for (int i = 1; i < count - K; i++) {
				ma.at(i + 1) = ma.at(i) - vec0.at(i) - vec1.at(i - 1) + vec0.at(K + i - 1) + vec1(K + i - 1);
			}
			ma.at(count - K + 1) = ma.at(count - K) - vec0.at(count - K) + vec0.at(count);
			sort(ma.begin(), ma.end());
			maxi = ma.at(count - K + 1);
		}
		cout << maxi << endl;
	}
	if (S.at(0) == '1'&&S.at(N - 1) == '0') {
		vector<int> vec1(count, 0);
		vector<int> vec0(count, 0);
		int count1 = 0;
		int count0 = -1;
		for (int i = 0; i < N; i++) {
			if (i == 0) {
				vec1.at(count1)++;
			}
			else if (S.at(i) == '1') {
				count1++;
				vec1.at(count1)++;
			}
			else if (S.at(i) = 0) {
				count0++;
				vec0.at(count0)++;
			}
			else if (S.at(i) == '1') {
				vec1.at(count1)++;
			}
			else if (S.at(i) == '0') {
				vec0.at(count0)++;
			}

		}
		int maxi = 0;
		if (K > count - 1) {
			maxi = N;
		}
		else {
			vector<int> ma(count - K + 1, 0);
			for (int i = 0; i < K; i++) {
				ma.at(0) += vec0.at(i) + vec1.at(i);
			}
			ma.at(0) = vec0.at(K);
			for (int i = 1; i < count - K + 1; i++) {
				ma.at(i) = ma.at(i - 1) - vec0.at(i - 1) - vec.at(i - 1) + vec0.at(K + i - 1) + vec1(K + i - 1);
			}
			sort(ma.begin(), ma.end());
			maxi = ma.at(count - K);
		}
		cout << maxi << endl;
	}
	if (S.at(0) == '0'&& S.at(N - 1) == '1') {
		vector<int> vec1(count, 0);
		vector<int> vec0(count, 0);
		int count1 = 1;
		int count0 = 0;
		for (int i = 0; i < N; i++) {
			if (i == 0) {
				vec1.at(count1)++;
			}
			else if (S.at(i) == '1') {
				count1++;
				vec1.at(count1)++;
			}
			else if (S.at(i) = 0) {
				count0++;
				vec0.at(count0)++;
			}
			else if (S.at(i) == '1') {
				vec1.at(count1)++;
			}
			else if (S.at(i) == '0') {
				vec0.at(count0)++;
			}
		}
		int maxi = 0;
		if (K == 0) {
			sort(ma.begin(), ma.end());
		}
		if (K > count - 1) {
			maxi = N;
		}
		else {
			vector<int> ma(count - K + 1, 0);
			for (int i = 0; i < K; i++) {
				ma.at(0) += vec0.at(i) + vec1.at(i);
			}
			ma.at(0) += vec1.at(K);
			for (int i = 1; i < count - K + 1; i++) {
				ma.at(i) = ma.at(i - 1) - vec0.at(i - 1) + vec0.at(K + i - 2) + vec1(K + i - 1);
			}
			ma.at(count - K + 1) = ma.at(count - K) - vec0.at(count - K) + vec0.at(count);
			sort(ma.begin(), ma.end());
			maxi = ma.at(count - K + 1);
		}
		cout << maxi << endl;
	}
	if (S.at(0) == '1'&& S.at(N - 1) == '1') {
		vector<int> vec1(count, 0);
		vector<int> vec0(count, 0);
		int count1 = 0;
		int count0 = -1;
		for (int i = 0; i < N; i++) {
			if (i == 0) {
				vec1.at(count1)++;
			}
			else if (S.at(i) == '1') {
				count1++;
				vec1.at(count1)++;
			}
			else if (S.at(i) = 0) {
				count0++;
				vec0.at(count0)++;
			}
			else if (S.at(i) == '1') {
				vec1.at(count1)++;
			}
			else if (S.at(i) == '0') {
				vec0.at(count0)++;
			}
		}
		int maxi = 0;
		if (K == 0) {
			sort(ma.begin(), ma.end());
		}
		if (K > count - 1) {
			maxi = N;
		}
		else {
			vector<int> ma(count - K + 1, 0);
			for (int i = 0; i < K; i++) {
				ma.at(0) += vec0.at(i) + vec1.at(i);
			}
			ma.at(0) += vec1.at(K);
			for (int i = 1; i < count - K + 1; i++) {
				ma.at(i) = ma.at(i - 1) - vec0.at(i - 1) + vec0.at(K + i - 2) + vec1(K + i - 1);
			}
			ma.at(count - K + 1) = ma.at(count - K) - vec0.at(count - K) + vec0.at(count);
			sort(ma.begin(), ma.end());
			maxi = ma.at(count - K + 1);
		}
		cout >> maxi >> endl;
	}
}


Compiler output:
a.cc: In function 'int main()':
a.cc:13:40: error: expected ';' before ':' token
   13 |                                 count++:
      |                                        ^
      |                                        ;
a.cc:18:40: error: expected ';' before ':' token
   18 |                                 count++:
      |                                        ^
      |                                        ;
a.cc:56:81: error: no match for call to '(std::vector<int>) (int)'
   56 |                         ma.at(1) = ma.at(0) - vec0.at(0) + vec0.at(K - 1) + vec1(K - 1);
      |                                                                             ~~~~^~~~~~~
a.cc:58:114: error: no match for call to '(std::vector<int>) (int)'
   58 |                                 ma.at(i + 1) = ma.at(i) - vec0.at(i) - vec1.at(i - 1) + vec0.at(K + i - 1) + vec1(K + i - 1);
      |                                                                                                              ~~~~^~~~~~~~~~~
a.cc:102:76: error: 'vec' was not declared in this scope; did you mean 'vec0'?
  102 |                                 ma.at(i) = ma.at(i - 1) - vec0.at(i - 1) - vec.at(i - 1) + vec0.at(K + i - 1) + vec1(K + i - 1);
      |                                                                            ^~~
      |                                                                            vec0
a.cc:102:117: error: no match for call to '(std::vector<int>) (int)'
  102 |                                 ma.at(i) = ma.at(i - 1) - vec0.at(i - 1) - vec.at(i - 1) + vec0.at(K + i - 1) + vec1(K + i - 1);
      |                                                                                                                 ~~~~^~~~~~~~~~~
a.cc:135:30: error: 'ma' was not declared in this scope; did you mean 'fma'?
  135 |                         sort(ma.begin(), ma.end());
      |                              ^~
      |                              fma
a.cc:147:101: error: no match for call to '(std::vector<int>) (int)'
  147 |                                 ma.at(i) = ma.at(i - 1) - vec0.at(i - 1) + vec0.at(K + i - 2) + vec1(K + i - 1);
      |                                                                                                 ~~~~^~~~~~~~~~~
a.cc:181:30: error: 'ma' was not declared in this scope; did you mean 'fma'?
  181 |                         sort(ma.begin(), ma.end());
      |                              ^~
      |                              fma
a.cc:193:101: error: no match for call to '(std::vector<int>) (int)'
  193 |                                 ma.at(i) = ma.at(i - 1) - vec0.at(i - 1) + vec0.at(K + i - 2) + vec1(K + i - 1);
      |                                                                                                 ~~~~^~~~~~~~~~~
a.cc:199:22: error: no match for 'operator>>' (operand types are 'std::ostream' {aka 'std::basic_ostream<char>'} and 'int')
  199 |                 cout >> maxi >> endl;
      |                 ~~~~ ^~ ~~~~
      |                 |       |
      |                 |       int
      |                 std::ostream {aka std::basic_ostream<char>}
a.cc:199:22: note: candidate: 'operator>>(int, int)' (built-in)
  199 |                 cout >> maxi >> endl;
      |                 ~~~~~^~~~~~~
a.cc:199:22: note:   no known conversion for argument 1 from 'std::ostream' {aka 'std::basic_ostream<char>'} to 'int'
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:41,
                 from a.cc:1:
/usr/include/c++/14/cstddef:131:5: note: candidate: 'template<class _IntegerType> constexpr std::__byte_op_t<_IntegerType> std::operator>>(byte, _IntegerType)'
  131 |     operator>>(byte __b, _IntegerType __shift) noexcept
      |     ^~~~~~~~
/usr/include/c++/14/cstddef:131:5: note:   template argument deduction/substitution failed:
a.cc:199:17: note:   cannot convert 'std::cout' (type 'std::ostream' {aka 'std::basic_ostream<char>'}) to type 'std::byte'
  199 |                 cout >> maxi >> endl;
      |                 ^~~~
In file included from /usr/include/c++/14/string:55,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52:
/usr/include/c++/14/bits/basic_string.tcc:835:5: note: candidate: 'template<class _CharT, class _Traits, class _Alloc> std::basic_istream<_CharT, _Traits>& std::operator>>(basic_istream<_CharT, _Traits>&, __cxx11::basic_string<_CharT, _Traits, _Allocator>&)'
  835 |     operator>>(basic_istream<_CharT, _Traits>& __in,
      |     ^~~~~~~~
/usr/include/c++/14/bits/basic_string.tcc:835:5: note:   template argument deduction/substitution failed:
a.cc:199:25: note:   'std::ostream' {aka 'std::basic_ostream<char>'} is not derived from 'std::basic_istream<_CharT, _Traits>'
  199 |                 cout >> maxi >> endl;
      |                         ^~~~
/usr/include/c++/14/bitset:1597:5: note: candidate: 'template<class _CharT, class _Traits, long unsigned int _Nb> std::basic_istream<_CharT, _Traits>& std::operator>>(basic_istream<_CharT, _Traits>&, bitset<_Nb>&)'
 1597 |     operator>>(std::basic_istream<_CharT, _Traits>& __is, bitset<_Nb>& __x)
      |     ^~~~~~~~
/usr/include/c++/14/bitset:1597:5: note:   template argument deduction/substitution failed:
a.cc:199:25: note:   'std::ostream' {aka 'std::basic_ostream<char>'} is not derived from 'std::basic_istream<_CharT, _Traits>'
  199 |                 cout >> maxi >> endl;
      |                         ^~~~
In file included from /usr/include/c++/14/istream:1109,
                 from /usr/include/c++/14/sstream:40,
                 from /usr/include/c++/14/complex:45,
                 from /usr/include/c++/14/ccomplex:39,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:127:
/usr/include/c++/14/bits/istream.tcc:978:5: note: candidate: 'template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(basic_istream<_CharT, _Traits>&, _CharT&)'
  978 |     operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c)
      |     ^~~~~~~~
/usr/include/c++/14/bits/istream.tcc:978:5: note:   template argument deduction/substitution failed:
a.cc:199:25: note:   'std::ostream' {aka 'std::basic_ostream<char>'} is not derived from 'std::basic_istream<_CharT, _Traits>'
  199 |                 cout >> maxi >> endl;
      |                         ^~~~
/usr/include/c++/14/istream:849:5: note: candidate: 'template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(basic_istream<char, _Traits>&, unsigned char&)'
  849 |     operator>>(basic_istream<char, _Traits>& __in, unsigned char& __c)
      |     ^~~~~~~~
/usr/include/c++/14/istream:849:5: note:   template argument deduction/substitution failed:
a.cc:199:25: note:   'std::ostream' {aka 'std::basic_ostream<char>'} is not derived from 'std::basic_istream<char, _Traits>'
  199 |                 cout >> maxi >> endl;
      |                         ^~~~
/usr/include/c++/14/istream:854:5: note: candidate: 'template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(basic_istream<char, _Traits>&, signed char&)'
  854 |     operator>>(basic_istream<char, _Traits>& __in, signed char& __c)
      |     ^~~~~~~~
/usr/include/c++/14/istream:854:5: note:   template argument deduction/substitution failed:
a.cc:199:25: note:   'std::ostream' {aka 'std::basic_ostream<char>'} is not derived from 'std::basic_istream<char, _Traits>'
  199 |                 cout >> maxi >> endl;
      |                         ^~~~
/usr/include/c++/14/istream:896:5: note: candidate: 'template<class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(basic_istream<_CharT, _Traits>&, _CharT*)'
  896 |     operator>>(basic_istream<_CharT, _Traits>& __in, _CharT* __s)
      |     ^~~~~~~~
/usr/include/c++/14/istream:896:5: note:   template argument deduction/substitution failed:
a.cc:199:25: note:   'std::ostream' {aka 'std::basic_ostream<char>'} is not derived from 'std::basic_istream<_CharT, _Traits>'
  199 |                 cout >> maxi >> endl;
      |                         ^~~~
/usr/include/c++/14/istream:939:5: note: candidate: 'template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(basic_istream<char, _Traits>&, unsigned char*)'
  939 |     operator>>(basic_istream<char, _Traits>& __in, unsigned char* __s)
      |     ^~~~~~~~
/usr/include/c++/14/istream:939:5: note:   template argument deduction/substitution failed:
a.cc:199:25: note:   'std::ostream' {aka 'std::basic_ostream<char>'} is not derived from 'std::basic_istream<char, _Traits>'
  199 |                 cout >> maxi >> endl;
      |                         ^~~~
/usr/include/c++/14/istream:945:5: note: candidate: 'template<class _Traits> std::basic_istream<char, _Traits>& std::operator>>(basic_istream<char, _Traits>&, signed char*)'
  945 |     operator>>(basic_istream<char, _Traits>& __in, signed char* __s)
      |     ^~~~~~~~
/usr/include/c++/14/istream:945:5: note:   template argument deduction/substitution failed:
a.cc:199:25: note:   'std::ostream' {aka 'std::basic_ostream<char>'} is not derived from 'std::basic_istream<char, _Traits>'
  199 |                 cout >> maxi >> endl;
      |                         ^~~~
/usr/include/c++/14/istream:1099:5: note: candidate: 'template<class _Istream, class _Tp> _Istream&& std::operator>>(_Istream&&, _Tp&&)'
 1099 |     operator>>(_Istream&& __is, _Tp&& __x)
      |     ^~~~~~~~
/usr/include/c++/14/istream:1099:5: note:   template argument deduction/substitution failed:
/usr/include/c++/14/istream: In substitution of 'template<class _Istream, class _Tp> _Istream&& std::operator>>(_Istream&&, _Tp&&) [with _Istream = std::basic_ostream<char>&; _Tp = int&]':
a.cc:199:11:   required from here
  199 |                 cout >> maxi >> endl;
      |                         ^~~~
/usr/include/c++/14/istream:1099:5: error: no type named 'type' in 'struct std::enable_if<false, void>'
 1099 |     operator>>(_Istream&& __is, _Tp&& __x)
      |     ^~~~~~~~
/usr/include/c++/14/complex:509:5: note: candidate: 'template<class _Tp, class _CharT, class _Traits> std::basic_istream<_CharT, _Traits>& std::operator>>(b