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: Accepted)

using System;
using System.Linq;
using System.Collections.Generic;
using static System.Math;
using static System.Console;
class Z { static void Main() => new K(); }
class K
{
	List<bool> s;
	public K()
	{
		ReadLine();
		s = (ReadLine() + "00").Select(x => x == '1').ToList();
		int N = s.Count, l = 0, ans = 0;
		// split s by 00
		while (true)
		{
			while (l < N && !s[l]) l++;
			if (l >= N) break;
			var r = l + 1;
			while (s[r] || s[r + 1]) r++;
			ans += Solve(l, r);
			l = r;
		}
		WriteLine(ans);
	}
	// 110101110111101 => 201030401 => 2,1,3,4,1
	int Solve(int l, int r)
	{
		var x = new List<int>{ 0 };
		var i = 0;
		while (l < r)
			if (s[l++]) x[i]++;
			else { x.Add(0); i++; }
		return DP(x, x.Count);
	}
	// a 0 b (11011111 = 2 0 5) =>
	// (0)  a  0  1
	// (1) a-1 0  1
	// (2)  1  0  b
	// (3)  1  0 b-1
	// (4)  1  0  1
	// (5)  0  0  0
	static int L(int i, int a) => i == 0 ? a : i == 1 ? a - 1 : i == 5 ? 0 : 1;
	static int R(int i, int a) => i == 2 ? a : i == 3 ? a - 1 : i == 5 ? 0 : 1;
	static int M(int i, int a, int b) => Max(L(i, a), R(i, b));
	static bool P(int i, int a, int b)
	{
		switch (i)
		{
		case 0: return a > 1;
		case 1: return a > 2;
		case 2: return b > 1;
		case 3: return b > 2;
		default: return true;
		}
	}
	static int DP(List<int> x, int n)
	{
		if (n <= 1) return 0;
		var dp = new int[n - 1, 6];
		for (var i = 0; i < 6; i++) if (P(i, x[0], x[1])) dp[0, i] = M(i, x[0], x[1]);
		for (var i = 1; i < n - 1; i++)
			for (var j = 0; j < 6; j++)
				for (var k = 0; k < 6; k++)
					if (P(j, x[i - 1], x[i]) && P(k, x[i], x[i + 1]) && R(j, x[i]) + L(k, x[i]) <= x[i])
						dp[i, k] = Max(dp[i, k], dp[i - 1, j] + M(k, x[i], x[i + 1]));
		var m = 0;
		for (var i = 0; i < 6; i++) m = Max(m, dp[n - 2, i]);
		return m;
	}
}