Problem:
Score : 700 points

Problem Statement

N cells are arranged in a row.
Some of them may contain tokens.
You are given a string s that consists of 0s and 1s.
If the i-th character of s is 1, the i-th cell (from left) contains a token.
Otherwise, it doesn't contain a token.

Snuke wants to perform the following operation as many times as possible.
In each operation, he chooses three consecutive cells.
Let's call the cells X, Y, Z from left to right.
In order for the operation to be valid, both X and Z must contain tokens and Y must not contain a token.
Then, he removes these two tokens and puts a new token on Y.

How many operations can he perform if he performs operations in the optimal way?

Constraints

1 \leq N \leq 500,000

|s| = N

Each character in s is either 0 or 1.

Input

Input is given from Standard Input in the following format:

N
s

Output

Print the answer.

Sample Input 1

7
1010101

Sample Output 1

2

For example, he can perform two operations in the following way:

Perform an operation on the last three cells. Now the string that represents tokens becomes 1010010.

Perform an operation on the first three cells. Now the string that represents tokens becomes 0100010.

Note that the choice of operations matters.
For example, if he chooses three cells in the middle first, he can perform no more operations.

Sample Input 2

50
10101000010011011110001001111110000101010111100110

Sample Output 2

10

Solution in C++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using Graph= vector<vector<ll>>;
struct edge{ll to ; ll cost ;} ;
using graph =vector<vector<edge>> ;
#define rep(i,n) for (ll i=0; i < (n); ++i)
#define rep2(i,n,m) for(ll i=n;i<=m;i++)
#define rep3(i,n,m) for(ll i=n;i>=m;i--)
#define pb push_back
#define eb emplace_back
#define ppb pop_back
#define mpa make_pair
#define fi first
#define se second 
#define set20 cout<<fixed<<setprecision(20) ;
const ll INF=1e18 ;
inline void chmax(ll& a,ll b){a=max(a,b);}
inline void chmin(ll& a,ll b){a=min(a,b);}
double pi=acos(-1) ;
ll gcd(ll a, ll b) { return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) { return a/gcd(a,b)*b;}
 

 int main(){
  ll n ;cin>> n ;
  string s; cin>> s ;
  vector<string> A ;
  bool ok=0 ;
  ll l = 0;
  ll r= 0 ;
  
  rep(i,n){
   if(i==0){
     if(s[i]=='1'){
       ok=1 ;
       l = 0 ;
     }
   }
   else if(i==n-1){
     if(ok){
      if(s[i]=='1') r= n-1 ;
      else r= n-2 ;
      A.pb(s.substr(l,r-l+1)) ;
     }
   }

   else{
    if(s[i]=='1'&&ok==0){
      ok=1 ;
      l=i ;
    }
    
    
    else if(s[i]=='0'&&s[i-1]=='0'){
      if(ok){
        ok=0 ;
        r=i-2 ;
        A.pb(s.substr(l,r-l+1)) ;
      }
    }
    
    }
  
  }

  //歯がおかしいいいいいいいいいいいいいいい
  ll ans=0 ;
  for(auto u :A ){
     //cout<<u<<endl ;
    ll p = u.size() ;
    if(p==1) continue ;
    bool ko=0 ;
    ll cnt= 0 ;
    
    rep(i,p){
      if(u[i]=='1') cnt++ ;
    }

    ll ccnt = cnt ;
    if(cnt==p) continue ;
    ll bnt=0 ;
    rep(i,p-1){
      if(u[i]=='1'&&u[i+1]=='1'){
        if(ko){
          cnt -= (bnt+1) ;
        cnt+= ((bnt+1)/2) ;
        ko =0 ;
        bnt= 0 ;
        }
      }
      else{
        ko=1 ;
        if(u[i]=='1') bnt++ ;
      }
      if(i==p-2){
        if(bnt>0){
          cnt -= (bnt+1) ;
          cnt+= ((bnt+1)/2) ;
        }
      }
     
    }
    bool flag =1 ;
    if(p>=2){
    if(!(u[0]=='1'&&u[1]=='1')) flag=0 ;
    if(!(u[p-1]=='1'&&u[p-2]=='1')) flag=0 ;
    }
    rep(i,p-4){
      if(u[i]=='0'&&u[i+1]=='1'&&u[i+2]=='0'&&u[i+3]=='1'&&u[i+4]=='0')
      flag=0 ;
    }
     if(flag){
      bool qq=0 ;
      ll rr=0 ;
      ll seg=0 ;
      ll kio=INF ;
      rep(i,p-1){
        if(u[i]=='1'&&u[i+1]=='1'){
           seg++ ;
           qq=1 ;
        }
        else{
         if(qq){
           if(rr==0) seg++ ;
           chmin(kio,seg) ;
           rr++ ;
         }
        }
        if(i==p-2){
          chmin(kio,seg+1) ;
        }
      }
      cnt-= kio-1 ;
    }
    //cout<<cnt<<endl ;
    ans+= cnt ;
    
  }
  cout<<ans<<endl ;
   return 0 ;
 }