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++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iterator>
#include <bitset>
#include <unordered_set>
#include <unordered_map>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <utility>
#include <memory>
#include <functional>
#include <deque>
#include <cctype>
#include <ctime>
#include <numeric>
#include <list>
#include <iomanip>

#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <forward_list>

#define cauto const auto&
#else

#endif

using namespace std;


typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll;
typedef vector<vector<long long> > vvll;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}

#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define _rep(i,n) _rep2((i),0,(n))
#define _rep2(i,a,b) for(int i=(a);i<(b);++i)
#define rep(...) GET_MACRO(__VA_ARGS__, _rep2, _rep)(__VA_ARGS__)
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define fi first
#define se second
#define mkp make_pair
#define DEBUG
#ifdef DEBUG
#define dump(x)  cout << #x << " = " << (x) << endl;
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#else
#define dump(x) 
#define debug(x) 
#endif

#define MOD 1000000007LL
#define EPS 1e-8
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
#define maxs(x,y) x=max(x,y)
#define mins(x,y) x=min(x,y)

int func(string s){
	vint v;
	rep(i,s.size()){
		if(s.substr(i,2)=="00"){
			assert(0);
		}
	}
	assert(s[0]!='0');
	assert(s[s.size()-1]!='0');
	// cout<<s<<endl;
	int l = 0;
	int ret = 0;
	rep(i,s.size()){
		if(s[i]=='0'){
			v.PB(i-1-l+1);
			assert(i-l);
			l=i+1;
		}
	}
	v.PB(s.size()-1-l+1);
	if(v.size()==1) return 0;
	set<pii> pq;
	rep(i,v.size()){
		pq.insert(pii(v[i], i));
	}
	int n = v.size();
	vint dp(4,-INF);
	dp[2]=v[0];
	dp[0]=0;
	rep(i,1,n){
		// cout<<i<<endl;
		vint next(4,-INF);
		maxs(next[0], dp[0]);
		maxs(next[0], dp[1]);
		maxs(next[3], dp[2]);
		maxs(next[0], dp[3]);

		maxs(next[1], dp[0]+v[i]);
		// maxs(next[], dp[1]);
		// maxs(next[], dp[2]);
		if(v[i-1]>1) maxs(next[1], dp[3]+v[i]-1);

		if(i<n-1) maxs(next[3], dp[0]+v[i]);
		// maxs(next[], dp[1]);
		// maxs(next[], dp[2]);
		if(i<n-1) maxs(next[3], dp[3]+v[i]-1);

		dp=next;
	}
	int ans = 0;
	rep(i,4) maxs(ans, dp[i]);
	// cout<<s<<" "<<ans<<endl;
	return ans;
}

void mainmain(){
	int n;
	cin>>n;
	string s;
	cin>>s;
	s="00"+s+"00";
	int l = 0;
	int ans = 0;
	if(s.substr(0,2)=="00"){
		int p = 0;
		while(p<n&&s[p]=='0') p++;
		s=s.substr(p);
		n=s.size();
	}
	rep(i,n){
		// cout<<i<<endl;
		if(s[i]=='0'&&s[i+1]=='0'){
			ans += func(s.substr(l, (i-1)-l+1));
			while(i<n&&s[i]=='0'){
				i++;
			}
			l=i;
		}
	}
	cout<<ans<<endl;
}


signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout<<fixed<<setprecision(20);
    mainmain();
}