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.Text;
using static System.Console;
using static System.Math;

namespace AtTest._700problems
{
    class CodeFestival2017B_D
    {
        static void Main(string[] args)
        {
            Method(args);
            ReadLine();
        }

        static void Method(string[] args)
        {
            int n = ReadInt() + 3;
            string str = Read();
            str += "001";
            List<List<int>> blockList = new List<List<int>>();
            int cnt = 1;
            bool nowZero = str[0] == '0';
            List<int> tmpList = new List<int>();
            for (int i = 1; i < n; i++)
            {
                if (nowZero == (str[i] == '0'))
                {
                    cnt++;
                }
                else
                {
                    if (nowZero && cnt >= 2)
                    {
                        if (tmpList.Count >= 2)
                        {
                            blockList.Add(tmpList);
                        }
                        tmpList = new List<int>();
                    }
                    if (!nowZero)
                    {
                        tmpList.Add(cnt);
                    }
                    cnt = 1;
                    nowZero = !nowZero;
                }
            }
            /*for(int i = 0; i < blockList.Count; i++)
            {
                for(int j = 0; j < blockList[i].Count; j++)
                {
                    Write(blockList[i][j]);
                }
                WriteLine();
            }*/
            int res = 0;
            for (int i = 0; i < blockList.Count; i++)
            {
                int[,] dp = new int[blockList[i].Count, 4];
                //全消し/1残し/1消費/手つかず
                for (int j = 1; j < blockList[i].Count; j++)
                {
                    dp[j, 0] = Max(dp[j - 1, 1],
                        Max(dp[j - 1, 2], dp[j - 1, 3]))
                        + blockList[i][j];
                    dp[j, 3] = Max(
                        Max(dp[j - 1, 0], dp[j - 1, 1]),
                        Max(dp[j - 1, 2], dp[j - 1, 3]));
                    if (blockList[i][j] == 1)
                    {
                        dp[j, 1] = dp[j, 3];
                        dp[j, 2] = dp[j, 3];
                    }
                    else
                    {
                        dp[j, 1] = Max(dp[j - 1, 1],
                            Max(dp[j - 1, 2], dp[j - 1, 3]))
                            + blockList[i][j] - 1;
                        dp[j, 2] = Max(dp[j - 1, 1] + 1,
                            Max(dp[j - 1, 2] + blockList[i][j - 1] - 1,
                            dp[j - 1, 3] + blockList[i][j - 1]));
                    }
                }
                int last = blockList[i].Count - 1;
                int tmp = Max(
                    Max(dp[last, 0], dp[last, 1]),
                    Max(dp[last, 2], dp[last, 3]));
                //WriteLine(tmp);
                res += tmp;
            }
            WriteLine(res);
        }

        private static string Read() { return ReadLine(); }
        private static int ReadInt() { return int.Parse(Read()); }
        private static long ReadLong() { return long.Parse(Read()); }
        private static double ReadDouble() { return double.Parse(Read()); }
        private static int[] ReadInts() { return Array.ConvertAll(Read().Split(), int.Parse); }
        private static long[] ReadLongs() { return Array.ConvertAll(Read().Split(), long.Parse); }
        private static double[] ReadDoubles() { return Array.ConvertAll(Read().Split(), double.Parse); }
    }
}