Language: C++

#include <iostream>
#include <vector>
#include<list>
#include <algorithm>
#include <string>
#include <math.h>
#include<map>
#include <utility>

#include<queue>
using namespace std;
#define REP(i,n) for(int i=0;i<n;i++)
 
#define INF (1e9)
typedef long long unsigned int ll;
#define SIZE_OF_ARRAY(array) (sizeof(array)/sizeof(array[0]))
 

 
int vector_finder(std::vector<int> vec, int number) {
  size_t index = std::distance( vec.begin(), std::find(vec.begin(), vec.end(), number) );
  if (index != vec.size()) { // 発見できたとき
    return 1;
  }
  else { // 発見できなかったとき
    return 0;
  }
}

int N, K;
string S;

struct data{
		int start;
		int end;
		int value;

	
};

bool cmp(const data &a, const data &b)
{
    return a.value >b.value;
}

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

	vector<data> mp;

	bool flag = false;
	int c = 1;
	int start = 0;
	int end = 0;
	REP(i, N)
	{
		if(S[i]=='0'){
			if(flag){
				c++;
			}else{
				flag = true;
				start = i;
			}
			if(i==N-1){
				if(flag){
					end = i;
					data d;
					d.start = start;
					d.end = end;
					d.value = c;
					mp.push_back(d);
				}else{
					start = i;
					end = i;
					data d;
					d.start = start;
					d.end = end;
					d.value = c;
					mp.push_back(d);
				}
			}
		}else{
			if(flag){
				flag = false;
				end = i - 1;
				data d;
				d.start = start;
				d.end = end;
				d.value = c;
				mp.push_back(d);
				c = 1;
			}
		}
	}
	if(mp.empty()){
		cout << N << endl;
		return 0;
	}
	sort(mp.begin(), mp.end(),cmp);


	queue<int> q;

	int prev = mp[0].value;

	int count = 0;
	REP(i, mp.size())
	{
		if(mp[i].value!=prev){
			q.push(count);

			count = 1;
			prev = mp[i].value;
			
		}
		else
		{
			count++;
		}
		if(i==mp.size()-1){
			if(mp[i].value!=prev){
				q.push(count);
				q.push(1);
			}else{
				q.push(count + 1);
			}
		}
	}

	
	int used = K;
	count = 0;
	int s, e;
	while (1)
	{
	
		if (q.empty() || count >= K || mp.size() < count)
		{
			break;
		}
		int qq = q.front();
	
		q.pop();
		if (qq <= used)
		{
			REP(i,qq){
				data d = mp[count];
				s = d.start;
				e = d.end;
				for (int j = d.start; j <= d.end; j++)
				{
					S[j] = '1';
				}
				count++;
			}
			used -= qq;
		}
		else
		{
			vector<int> u;
			REP(i, used)
			{
				int idx = 0;
				int ss, ee = INF;
				REP(j, qq)
				{
					if(!vector_finder(u,j)){
						if(abs(mp[count+j].start-s)<ss||abs(mp[count+j].end-e)<ee){
							idx = j;
							if(abs(mp[count+j].start-s)<ss){
								ss = abs(mp[count + j].start - s);
							}if(abs(mp[count+j].end-e)<ee){
								ee = abs(mp[count + j].end - e);
							}
						}
						u.push_back(idx);
					}
				}

				for (int j = mp[count+idx].start; j <= mp[count+idx].end; j++)
				{
					S[j] = '1';
					
				}
			}

		
		}
	}



	count = 0;
	int m = 0;
	flag = false;
	REP(i, N)
	{
		if(S[i]=='1'){
			if(!flag){
				flag = true;
				count = 1;
			}else{
				count++;
			}
			if(i==N-1){
				
				if(count>m){
				m = count;
				}
			}
		}else{
			if(count>m){
				m = count;
			}
			count = 0;
		}
	}

	cout << m << endl;

	return 0;
}#include <iostream>
#include <vector>
#include<list>
#include <algorithm>
#include <string>
#include <math.h>
#include<map>
#include <utility>

#include<queue>
using namespace std;
#define REP(i,n) for(int i=0;i<n;i++)
 
#define INF (1e9)
typedef long long unsigned int ll;
#define SIZE_OF_ARRAY(array) (sizeof(array)/sizeof(array[0]))
 

 
int vector_finder(std::vector<int> vec, int number) {
  size_t index = std::distance( vec.begin(), std::find(vec.begin(), vec.end(), number) );
  if (index != vec.size()) { // 発見できたとき
    return 1;
  }
  else { // 発見できなかったとき
    return 0;
  }
}

int N, K;
string S;

struct data{
		int start;
		int end;
		int value;

	
};

bool cmp(const data &a, const data &b)
{
    return a.value >b.value;
}

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

	vector<data> mp;

	bool flag = false;
	int c = 1;
	int start = 0;
	int end = 0;
	REP(i, N)
	{
		if(S[i]=='0'){
			if(flag){
				c++;
			}else{
				flag = true;
				start = i;
			}
			if(i==N-1){
				if(flag){
					end = i;
					data d;
					d.start = start;
					d.end = end;
					d.value = c;
					mp.push_back(d);
				}else{
					start = i;
					end = i;
					data d;
					d.start = start;
					d.end = end;
					d.value = c;
					mp.push_back(d);
				}
			}
		}else{
			if(flag){
				flag = false;
				end = i - 1;
				data d;
				d.start = start;
				d.end = end;
				d.value = c;
				mp.push_back(d);
				c = 1;
			}
		}
	}
	if(mp.empty()){
		cout << N << endl;
		return 0;
	}
	sort(mp.begin(), mp.end(),cmp);


	queue<int> q;

	int prev = mp[0].value;

	int count = 0;
	REP(i, mp.size())
	{
		if(mp[i].value!=prev){
			q.push(count);

			count = 1;
			prev = mp[i].value;
			
		}
		else
		{
			count++;
		}
		if(i==mp.size()-1){
			if(mp[i].value!=prev){
				q.push(count);
				q.push(1);
			}else{
				q.push(count + 1);
			}
		}
	}

	
	int used = K;
	count = 0;
	int s, e;
	while (1)
	{
	
		if (q.empty() || count >= K || mp.size() < count)
		{
			break;
		}
		int qq = q.front();
	
		q.pop();
		if (qq <= used)
		{
			REP(i,qq){
				data d = mp[count];
				s = d.start;
				e = d.end;
				for (int j = d.start; j <= d.end; j++)
				{
					S[j] = '1';
				}
				count++;
			}
			used -= qq;
		}
		else
		{
			vector<int> u;
			REP(i, used)
			{
				int idx = 0;
				int ss, ee = INF;
				REP(j, qq)
				{
					if(!vector_finder(u,j)){
						if(abs(mp[count+j].start-s)<ss||abs(mp[count+j].end-e)<ee){
							idx = j;
							if(abs(mp[count+j].start-s)<ss){
								ss = abs(mp[count + j].start - s);
							}if(abs(mp[count+j].end-e)<ee){
								ee = abs(mp[count + j].end - e);
							}
						}
						u.push_back(idx);
					}
				}

				for (int j = mp[count+idx].start; j <= mp[count+idx].end; j++)
				{
					S[j] = '1';
					
				}
			}

		
		}
	}



	count = 0;
	int m = 0;
	flag = false;
	REP(i, N)
	{
		if(S[i]=='1'){
			if(!flag){
				flag = true;
				count = 1;
			}else{
				count++;
			}
			if(i==N-1){
				
				if(count>m){
				m = count;
				}
			}
		}else{
			if(count>m){
				m = count;
			}
			count = 0;
		}
	}

	cout << m << endl;

	return 0;
}

Compiler output:
a.cc:225:2: error: stray '#' in program
  225 | }#include <iostream>
      |  ^
a.cc:41:16: error: reference to 'data' is ambiguous
   41 | bool cmp(const data &a, const data &b)
      |                ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:33:8: note:                 'struct data'
   33 | struct data{
      |        ^~~~
a.cc:41:31: error: reference to 'data' is ambiguous
   41 | bool cmp(const data &a, const data &b)
      |                               ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:33:8: note:                 'struct data'
   33 | struct data{
      |        ^~~~
a.cc: In function 'bool cmp(const int&, const int&)':
a.cc:43:14: error: request for member 'value' in 'a', which is of non-class type 'const int'
   43 |     return a.value >b.value;
      |              ^~~~~
a.cc:43:23: error: request for member 'value' in 'b', which is of non-class type 'const int'
   43 |     return a.value >b.value;
      |                       ^~~~~
a.cc: In function 'int main()':
a.cc:52:20: error: template argument 1 is invalid
   52 |         vector<data> mp;
      |                    ^
a.cc:52:20: error: template argument 2 is invalid
a.cc:70:41: error: reference to 'data' is ambiguous
   70 |                                         data d;
      |                                         ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:33:8: note:                 'struct data'
   33 | struct data{
      |        ^~~~
a.cc:71:41: error: 'd' was not declared in this scope
   71 |                                         d.start = start;
      |                                         ^
a.cc:74:44: error: request for member 'push_back' in 'mp', which is of non-class type 'int'
   74 |                                         mp.push_back(d);
      |                                            ^~~~~~~~~
a.cc:78:41: error: reference to 'data' is ambiguous
   78 |                                         data d;
      |                                         ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:33:8: note:                 'struct data'
   33 | struct data{
      |        ^~~~
a.cc:79:41: error: 'd' was not declared in this scope
   79 |                                         d.start = start;
      |                                         ^
a.cc:82:44: error: request for member 'push_back' in 'mp', which is of non-class type 'int'
   82 |                                         mp.push_back(d);
      |                                            ^~~~~~~~~
a.cc:89:33: error: reference to 'data' is ambiguous
   89 |                                 data d;
      |                                 ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:33:8: note:                 'struct data'
   33 | struct data{
      |        ^~~~
a.cc:90:33: error: 'd' was not declared in this scope
   90 |                                 d.start = start;
      |                                 ^
a.cc:93:36: error: request for member 'push_back' in 'mp', which is of non-class type 'int'
   93 |                                 mp.push_back(d);
      |                                    ^~~~~~~~~
a.cc:98:15: error: request for member 'empty' in 'mp', which is of non-class type 'int'
   98 |         if(mp.empty()){
      |               ^~~~~
a.cc:102:17: error: request for member 'begin' in 'mp', which is of non-class type 'int'
  102 |         sort(mp.begin(), mp.end(),cmp);
      |                 ^~~~~
a.cc:102:29: error: request for member 'end' in 'mp', which is of non-class type 'int'
  102 |         sort(mp.begin(), mp.end(),cmp);
      |                             ^~~
a.cc:107:22: error: invalid types 'int[int]' for array subscript
  107 |         int prev = mp[0].value;
      |                      ^
a.cc:110:19: error: request for member 'size' in 'mp', which is of non-class type 'int'
  110 |         REP(i, mp.size())
      |                   ^~~~
a.cc:12:32: note: in definition of macro 'REP'
   12 | #define REP(i,n) for(int i=0;i<n;i++)
      |                                ^
a.cc:112:22: error: invalid types 'int[int]' for array subscript
  112 |                 if(mp[i].value!=prev){
      |                      ^
a.cc:116:34: error: invalid types 'int[int]' for array subscript
  116 |                         prev = mp[i].value;
      |                                  ^
a.cc:123:26: error: request for member 'size' in 'mp', which is of non-class type 'int'
  123 |                 if(i==mp.size()-1){
      |                          ^~~~
a.cc:124:30: error: invalid types 'int[int]' for array subscript
  124 |                         if(mp[i].value!=prev){
      |                              ^
a.cc:140:51: error: request for member 'size' in 'mp', which is of non-class type 'int'
  140 |                 if (q.empty() || count >= K || mp.size() < count)
      |                                                   ^~~~
a.cc:150:33: error: reference to 'data' is ambiguous
  150 |                                 data d = mp[count];
      |                                 ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned