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 <bits/stdc++.h>
using namespace std;

#define FAST_IO                       \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);

#define dbg(x) cerr << #x << " = " << x << ",  ";
#define edl() cerr << endl;
#define vw(v, x, y)                  \
    cerr << endl                     \
         << (#v) << " is: " << endl; \
    for (int i = (x); i < (y); i++)  \
    {                                \
        cerr << v[i] << " ";         \
    }                                \
    cerr << endl

#define rep(i, a, b) for (int i = (a); i < (b); i++)

#define all(x) (x).begin(), (x).end()
#define rv(x) (x).rbegin(), (x).rend()
#define pb push_back
#define pf push_front

#define F first
#define S second

#define min_self(x, y) x = min(x, y)
#define max_self(x, y) x = max(x, y)

typedef long long ll;
ll const INF = 2147483647, R_INF = 9223372036854775807, MOD = 1e9 + 7;

//const int N =

int main()
{
    FAST_IO

    int n;
    cin >> n;
    vector<int> input(n + 1);

    rep(i, 1, n + 1)
    {
        char c;
        cin >> c;

        input[i] = c - '0';
    }

    vector<int> dp(n + 1, 0);
    int prev = 0;
    rep(i, 1, n + 1)
    {
        dp[i] = dp[i - 1];
        if (input[i] && !prev)
        {
            if (i - 2 >= 0 && !input[i - 1] && input[i - 2])
                prev = i;
        }

        else if (prev)
        {
            if (i - (i - prev) - 2 >= 0)
                dp[i] = max(dp[i], dp[i - (i - prev) - 2] + i - prev + (i == n));
            else
                dp[i] = max(dp[i] , i - prev);

            if (!input[i])
                prev = 0;
        }
    }
    cout << dp[n];
}

// 1010101
// 0001111

// 50
// 10101000010011011110001001111110000101010111100110
// 00011111111111111115555555555559999999101010101010101414141414

// 1101111
// 1000101 = 4