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: Wrong Answer)

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

using namespace std;

#define dump(x)  cerr << #x << " = " << (x) << endl;
#define PB push_back
#define MP make_pair
#define ll long long

inline int toInt(string s){int v;istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x){ostringstream sout;sout<<x;return sout.str();}

int main(){
    std::ios::sync_with_stdio(false);
    int l;
    ll tmp;
    vector<ll> a;
    cin >> l;
    vector<ll> l_accum(l, 0), r_accum(l, 0), l_end(l+1, 0), l_ret(l+1, 0), r_end(l+1, 0), r_ret(l+1, 0);
    for(int i=0;i<l;++i)
    {
        cin >> tmp;
        a.push_back(tmp);
    }

    ll l_tmp = 0;
    ll r_tmp = 0;
    for(int i=0;i<l;++i)
    {
        l_tmp += a[i];
        r_tmp += a[l - i - 1];
        l_accum[i] = l_tmp;
        r_accum[l - i - 1] = r_tmp;
    }

    l_end[1] = (a[0] + 1) % 2;
    l_ret[1] = a[0] % 2;
    for(int i=2;i<=l;++i)
    {
        l_end[i] = l_end[i-1] + (a[i-1] + 1) % 2;
        l_ret[i] = min(l_ret[i-1] + (a[i-1] == 0 ? 2 : a[i-1] % 2), l_accum[i-1]);
    }

    // cerr << "l_end : ";
    // for(int i=0;i<=l;++i) cerr << l_end[i] << " ";
    // cerr << endl;

    // cout << "l_ret : ";
    // for(int i=0;i<=l;++i) cerr << l_ret[i] << " ";
    // cerr << endl;

    r_end[l-1] = (a[l-1] + 1) % 2;
    r_ret[l-1] = a[l-1] % 2;
    for(int i=l-2;i>=0;--i)
    {
        r_end[i] = r_end[i+1] + (a[i] + 1) % 2;
        r_ret[i] = min(r_ret[i+1] + (a[i] == 0 ? 2 : a[i] % 2), r_accum[i]);
    }

    // cerr << "r_end : ";
    // for(int i=0;i<=l;++i) cerr << r_end[i] << " ";
    // cerr << endl;

    // cout << "r_ret : ";
    // for(int i=0;i<=l;++i) cerr << r_ret[i] << " ";
    // cerr << endl;

    // cerr << "l_accum : ";
    // for(int i=0;i<l;++i) cerr << l_accum[i] << " ";
    // cerr << endl;

    // cerr << "r_accum : ";
    // for(int i=0;i<l;++i) cerr << r_accum[i] << " ";
    // cerr << endl;

    ll res = 1ll << 62;
    for(int i=0;i<=l;++i)
    {
        ll ret_ret = l_ret[i] + r_ret[i];
        ll end_ret = l_end[i] + r_ret[i];
        ll ret_end = l_ret[i] + r_end[i];

        res = min(res, min(ret_ret, min(end_ret, ret_end)));
    }

    cout << res << endl;
}