Problem:
Score : 600 points

Problem Statement

Snuke stands on a number line. He has L ears, and he will walk along the line continuously under the following conditions:

He never visits a point with coordinate less than 0, or a point with coordinate greater than L.

He starts walking at a point with integer coordinate, and also finishes walking at a point with integer coordinate.

He only changes direction at a point with integer coordinate.

Each time when Snuke passes a point with coordinate i-0.5, where i is an integer, he put a stone in his i-th ear.

After Snuke finishes walking, Ringo will repeat the following operations in some order so that, for each i, Snuke's i-th ear contains A_i stones:

Put a stone in one of Snuke's ears.

Remove a stone from one of Snuke's ears.

Find the minimum number of operations required when Ringo can freely decide how Snuke walks.

Constraints

1 \leq L \leq 2\times 10^5

0 \leq A_i \leq 10^9(1\leq i\leq L)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

L
A_1
:
A_L

Output

Print the minimum number of operations required when Ringo can freely decide how Snuke walks.

Sample Input 1

4
1
0
2
3

Sample Output 1

1

Assume that Snuke walks as follows:

He starts walking at coordinate 3 and finishes walking at coordinate 4, visiting coordinates 3,4,3,2,3,4 in this order.

Then, Snuke's four ears will contain 0,0,2,3 stones, respectively.
Ringo can satisfy the requirement by putting one stone in the first ear.

Sample Input 2

8
2
0
0
2
1
3
4
1

Sample Output 2

3

Sample Input 3

7
314159265
358979323
846264338
327950288
419716939
937510582
0

Sample Output 3

1

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)


#if 1
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <stack>
#include <array>
#include <deque>
#include <algorithm>
#include <utility>
#include <cstdint>
#include <functional>
#include <iomanip>
#include <numeric>
#include <assert.h>
#include <bitset>
#include <list>
#include <cmath>

auto& in = std::cin;
auto& out = std::cout;
#define all_range(C) std::begin(C), std::end(C)
const double PI = 3.141592653589793238462643383279502884197169399375105820974944;


template<typename T, typename U>
std::enable_if_t<std::rank<T>::value == 0> fill_all(T& arr, const U& v) {
	arr = v;
}
template<typename ARR, typename U>
std::enable_if_t<std::rank<ARR>::value != 0> fill_all(ARR& arr, const U& v) {
	for (auto& i : arr) {
		fill_all(i, v);
	}
}

constexpr int64_t INF = 2000102000000000;

int32_t N;
int64_t A[200010];

int64_t sumR[200010];
int64_t sumL_[200010];
const auto sumL = sumL_ + 5;

int64_t dpR[200010];
int64_t funcR(int i)
{
	if (i == N) {
		return 0;
	}
	if (dpR[i] != INF) {
		return dpR[i];
	}
	dpR[i] = sumR[i + 1];
	if (A[i+1] == 0) {
		dpR[i] = std::min(dpR[i], 2 + funcR(i + 1));
	}
	else if(A[i+1]%2==0){
		dpR[i] = std::min(dpR[i],  funcR(i + 1));
	}
	else {
		dpR[i] = std::min(dpR[i], 1 + funcR(i + 1));
	}
	return dpR[i];
}
int64_t dpL[200010];
int64_t funcL(int i)
{
	if (i == 0) {
		return 0;
	}
	if (dpL[i] != INF) {
		return dpL[i];
	}
	dpL[i] = sumL[i];
	int64_t cost = 0;
	if (A[i] == 0) {
		cost = 2;
	}
	else if (A[i] % 2 == 0) {
		cost = 0;
	}
	else {
		cost = 1;
	}
	return dpL[i] = std::min(dpL[i], cost + funcL(i - 1));
}
int64_t dpL1[200010];
int64_t funcL1(int i)
{
	if (i == 0) {
		return 0;
	}
	if (dpL1[i] != INF) {
		return dpL1[i];
	}
	dpL1[i] = funcL(i);
	int64_t cost = 0;
	if (A[i] == 0) {
		cost = 1;
	}
	else if (A[i] % 2 == 0) {
		cost = 1;
	}
	else {
		cost = 0;
	}
	return dpL1[i] = std::min(dpL1[i], cost + funcL1(i - 1));
}
int64_t dpR1[200010];
int64_t funcR1(int i)
{
	if (i == N) {
		return 0;
	}
	if (dpR1[i] != INF) {
		return dpR1[i];
	}
	dpR1[i] = funcR(i);
	int64_t cost = 0;
	if (A[i + 1] == 0) {
		cost = 1;
	}
	else if (A[i + 1] % 2 == 0) {
		cost = 1;
	}
	else {
		cost = 0;
	}
	return dpR1[i] = std::min(dpR1[i], cost + funcR1(i + 1));
}

int main()
{
	fill_all(dpR, INF);
	fill_all(dpR1, INF);
	fill_all(dpL, INF);
	fill_all(dpL1, INF);

	using std::endl;
	in.sync_with_stdio(false);
	out.sync_with_stdio(false);
	in.tie(nullptr);
	out.tie(nullptr);

	in >> N;
	for (int i = 1; i <= N; i++)
	{
		in >> A[i];
	}
	for (int i = 1; i <= N; i++)
	{
		sumL[i] = sumL[i-1] + A[i];
	}
	for (int i = N; i >= 0; i--)
	{
		sumR[i] = sumR[i+1] + A[i];
	}

	int64_t min = INF;
	for (int i = 0; i <= N; i++)
	{
		min = std::min({ min, funcL(i) + funcR1(i), funcL1(i) + funcR(i) });
	}
	out << min << endl;

	return 0;
}
#endif