Language: C++

#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<functional>
#include<queue>
#include<cmath>
#include<algorithm>
#include<bitset>
#include<utility>
#include<complex>
#include<cstdlib>
#include<set>
#include<cctype>

#define DBG cerr << '!' << endl;
#define REP(i,n) for(int (i) = (0);(i) < (n);++i)
#define rep(i,s,g) for(int (i) = (s);(i) < (g);++i)
#define rrep(i,s,g) for(int (i) = (s);i >= (g);--(i))
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {for(int i = 0;i < (n);i++)cerr << v[i] << ' ';cerr << endl << endl;}
#define SHOW2d(v,i,j) {for(int a = 0;a < i;a++){for(int b = 0;b < j;b++)cerr << v[a][b] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()

using namespace std;

typedef long long ll;
typedef vector<int> iv;
typedef vector<iv> iiv;
typedef vector<string> sv;

int dp[500100];
int prev[500100];
int nxt[500100];

int main()
{
	int n;
	string str;
	cin >> n >> str;
	str = '0'+ str +'0';n++;
	
	prev[0] = 0;
	nxt[n] = n;
	
	rep(i,1,n)
	{
		if(str[i] == '0')
		{
			prev[i] = i;
		}
		else
		{
			prev[i] = prev[i-1];
		}
	}
	rrep(i,n-1,0)
	{
		if(str[i] == '0')
		{
			nxt[i] = i;
		}
		else
		{
			nxt[i] = nxt[i+1];
		}
	}
	
	
	rep(i,2,n)
	{
		dp[i+1] = max(dp[i],dp[i+1]);
		
		if(str[i] == '1')
		{
			
			if(str[prev[i]-1] == '1')
			{
				int tmp = i - prev[i];
				dp[i+1] = max(dp[prev[i]-1]+tmp,dp[i+1]);
			}
			if(str[nxt[i]+1] == '1')
			{
				int tmp = nxt[i] - i;
				dp[nxt[i]+2] = max(dp[nxt[i]+2],dp[i]+tmp);
			}
		}
	}
	
	//SHOW1d(dp,n+2);
	
	cout << dp[n] << endl;
			
			
			
	
	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:46:9: error: reference to 'prev' is ambiguous
   46 |         prev[0] = 0;
      |         ^~~~
In file included from /usr/include/c++/14/string:47,
                 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/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:36:5: note:                 'int prev [500100]'
   36 | int prev[500100];
      |     ^~~~
a.cc:53:25: error: reference to 'prev' is ambiguous
   53 |                         prev[i] = i;
      |                         ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:36:5: note:                 'int prev [500100]'
   36 | int prev[500100];
      |     ^~~~
a.cc:57:25: error: reference to 'prev' is ambiguous
   57 |                         prev[i] = prev[i-1];
      |                         ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:36:5: note:                 'int prev [500100]'
   36 | int prev[500100];
      |     ^~~~
a.cc:57:35: error: reference to 'prev' is ambiguous
   57 |                         prev[i] = prev[i-1];
      |                                   ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:36:5: note:                 'int prev [500100]'
   36 | int prev[500100];
      |     ^~~~
a.cc:80:32: error: reference to 'prev' is ambiguous
   80 |                         if(str[prev[i]-1] == '1')
      |                                ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:36:5: note:                 'int prev [500100]'
   36 | int prev[500100];
      |     ^~~~
a.cc:82:47: error: reference to 'prev' is ambiguous
   82 |                                 int tmp = i - prev[i];
      |                                               ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:36:5: note:                 'int prev [500100]'
   36 | int prev[500100];
      |     ^~~~
a.cc:83:50: error: reference to 'prev' is ambiguous
   83 |                                 dp[i+1] = max(dp[prev[i]-1]+tmp,dp[i+1]);
      |                                                  ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:36:5: note:                 'int prev [500100]'
   36 | int prev[500100];
      |     ^~~~