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# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using static System.Console;
using Pair = System.Collections.Generic.KeyValuePair<int, int>;
//using System.Numerics;
//using static System.Math;

class Program
{
    static void Main()
    {
        //SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false });
        new Program().solve();
        Out.Flush();
    }
    Scanner cin = new Scanner();
    readonly int[] dd = { 0, 1, 0, -1, 0 };
    readonly int mod = 1000000007;
    readonly string alfa = "abcdefghijklmnopqrstuvwxyz";
    readonly string ALFA = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    int N;
    char[] S;
    int[] L;
    int[] R;
    void solve()
    {
        N = cin.nextint;
        S = cin.next.ToCharArray();
        L = new int[N];
        R = new int[N];

        int now = 0;
        for (int i = 0; i < N; i++)
        {
            L[i] = now;
            if (S[i] == '1')
            {
                now++;
            }
            else
            {
                now = 0;
            }
        }
        now = 0;
        for (int i = N - 1; i >= 0; i--)
        {
            R[i] = now;
            if (S[i] == '1')
            {
                now++;
            }
            else
            {
                now = 0;
            }
        }
        //WriteLine(string.Join(" ", L));
        //WriteLine(string.Join(" ", R));

        long ans = 0;

        bool nextzero = false;

        var H = new List<long>();
        for (int i = 0; i <= N; i++)
        {
            //WriteLine(i);
            if (nextzero)
            {
                if (i == N || i == N - 1 || (S[i] == '0' && S[i + 1] == '0'))//精算
                {
                    //for (int j = 0; j < H.Count; j++)
                    //{
                    //    Write(H[j] + " ");
                    //}
                    //WriteLine();



                    if (H.Count > 2)
                    {
                        var sum = H.Sum();
                        if (H.Min() == 1)
                        {
                            ans += sum - H.Count(k => k == 1) / 2;
                        }
                        else ans += sum - 2;

                        //long max = 0;
                        //long tmp = 0;
                        //var C = H.ToArray();
                        //for (int j = 0; j < H.Count; j++)
                        //{
                        //    if (j == H.Count - 1)
                        //    {
                        //        if (C[j - 1] > 0)
                        //        {
                        //            tmp += C[j];
                        //        }
                        //    }
                        //    else if (C[j] > 0 && C[j + 1] > 0)
                        //    {
                        //        tmp += C[j];
                        //        C[j + 1]--;
                        //    }
                        //}

                        //max = tmp;
                        ////WriteLine(tmp);
                        //tmp = 0;
                        //for (int j = H.Count - 1; j >= 0; j--)
                        //{

                        //    if (j == 0)
                        //    {
                        //        if (H[j + 1] > 0)
                        //        {
                        //            tmp += H[j];
                        //        }
                        //    }
                        //    else if (H[j] > 0 && H[j - 1] > 0)
                        //    {
                        //        tmp += H[j];
                        //        H[j - 1]--;
                        //    }
                        //}
                        ////WriteLine(tmp);
                        //max = Math.Max(max, tmp);
                        //ans += max;

                    }
                    else
                    {
                        ans += H.Max();
                    }

                    //WriteLine("ans= " + ans);

                    nextzero = false;
                }
                else
                {
                    H.Add(R[i]);
                    i += R[i];
                }
            }
            else
            {
                if (i == N) break;
                if (S[i] == '0' && L[i] > 0 && R[i] > 0)
                {
                    H = new List<long>();
                    H.Add(L[i]);
                    H.Add(R[i]);
                    nextzero = true;
                    i += R[i];
                }
            }
        }
        WriteLine(ans);

        //for (int i = 2; i < N; i++)
        //{
        //    //Write(i + " ");
        //    if (S[i - 2] == '1' && S[i - 1] == '0' && S[i] == '1')
        //    {

        //        if (L[i - 2] >= R[i])//選べる場合左を優先
        //        {
        //            S[i] = '0';
        //            ans += L[i - 2] + 1;
        //            //Write(1 + " " + ans);

        //        }
        //        else
        //        {
        //            ans += R[i] + 1;

        //            S[i + R[i]] = '0';
        //            S[i + R[i] - 1] = '1';

        //            L[i + R[i] - 1] = 0;
        //            i += R[i];
        //            //Write(2 + " " + ans);
        //        }
        //    }
        //    //WriteLine();
        //}
        //WriteLine(ans);
    }

}

class Scanner
{
    string[] s; int i;
    char[] cs = new char[] { ' ' };
    public Scanner() { s = new string[0]; i = 0; }
    public string[] scan { get { return ReadLine().Split(); } }
    public int[] scanint { get { return Array.ConvertAll(scan, int.Parse); } }
    public long[] scanlong { get { return Array.ConvertAll(scan, long.Parse); } }
    public double[] scandouble { get { return Array.ConvertAll(scan, double.Parse); } }
    public string next
    {
        get
        {
            if (i < s.Length) return s[i++];
            string st = ReadLine();
            while (st == "") st = ReadLine();
            s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
            i = 0;
            return next;
        }
    }
    public int nextint { get { return int.Parse(next); } }
    public long nextlong { get { return long.Parse(next); } }
    public double nextdouble { get { return double.Parse(next); } }
}