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<bits/stdc++.h>
#define INF 1e9
#define llINF 1e18
#define MOD 1000000007
#define pb push_back
#define mp make_pair 
#define F first
#define S second
#define ll long long
#define vi vector<ll>
#define vvi vector<vi>
#define BITLE(n) (1LL<<(n))
#define BITCNT(n) (__builtin_popcountll(n))
#define SUBS(s,f,t) (s.substr(f-1,t-f+1))
#define ALL(a) (a).begin(),(a).end()

using namespace std;
struct Grid{ll x,y,t;};
struct Edge{ll to,cost;};
struct Graph{vector<vector<Edge>>E;int V;
  const ll Inf = llINF;const int MAX_V=201010;vector<ll>d;
  Graph(int n):E(n){d.resize(MAX_V);E.resize(n);V=n;}
  void init(){for(int i=0;i<MAX_V;i++)d[i]=Inf;}
  void add_edge(ll from,ll to,ll cost){E[from].pb({to,cost});}
};
vi A(222222);
vi rui(222222);
vvi E(222222);
vector<pair<pair<ll,ll>,pair<pair<ll,ll>,ll> > > cho;//→通り抜ける、左ととおりぬける、とまる、合計
map<pair<int,int>,int>ze;
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll L;cin>>L;
  for(int i=0;i<L;i++){
    cin>>A[i];
  }
  if(L==1){
    cout<<0<<endl;
    return 0;
  }
  for(int i=0;i<L;i++){
    vi num;
    ll sum=0;
    ll gu=0;
    ll ki=0;
    while(i<L&&A[i]!=0){
      num.pb(A[i]);
      sum+=A[i];
      if(A[i]%2)ki++;
      else gu++;
      // cout<<sum<<" "<<A[i]<<endl;
      i++;
    }
    // cout<<num.size()<<endl;
    if(num.size()==0)continue;
    ll zero=0;
    while(i<L&&A[i]==0){zero++;i++;}
    ze[mp(cho.size(),cho.size()+1)]=zero;
    ze[mp(cho.size()+1,cho.size())]=zero;
    if(ki&&!gu){
      rui[cho.size()+1]+=rui[cho.size()]+sum;
      cho.pb(mp(mp(0,0),mp(mp(0,0),sum)));
    }else if(gu&&!ki){
      rui[cho.size()+1]+=rui[cho.size()]+sum;
      ll ho=num.size();
      if(cho.size()==0)
	cho.pb(mp(mp(ho-1,ho),mp(mp(0,0),sum)));
      else if(i==L-1)
	cho.pb(mp(mp(ho,ho-1),mp(mp(0,0),sum)));
      else
	cho.pb(mp(mp(ho,ho),mp(mp(0,0),sum)));
    }else{
      ll le=0,ri=num.size()-1;
      ll lesum=0,risum=0;
      rui[cho.size()+1]+=rui[cho.size()]+sum;
      for(int j=0;j<num.size();j++){if(num[j]%2==0){le=j;break;}}
      for(int j=num.size()-1;j>=0;j--){if(num[j]%2==0){ri=j;break;}}
      ll ha=ri-le+1;
      if(ri==num.size()-1&&le==0){
	if(cho.size()==0)
	  cho.pb(mp(mp(min(gu-1,ki-le),min(gu,ki-((ll)num.size()-1-ri))),mp(mp(min(ki,gu-1),min(ki,gu)),sum)));
	else if(i==L-1)
	  cho.pb(mp(mp(min(gu,ki-le),min(gu-1,ki-((ll)num.size()-1-ri))),mp(mp(min(ki,gu),min(ki,gu-1)),sum)));
	else
	  cho.pb(mp(mp(min(gu,ki-le),min(gu,ki-((ll)num.size()-1-ri))),mp(mp(min(ki,gu),min(ki,gu)),sum)));
      }else if(ri==num.size()-1){
	if(i==L-1)
	  cho.pb(mp(mp(min(gu,ki-le),min(gu-1,ki-((ll)num.size()-1-ri))),mp(mp(min(ki-le,gu),min(ki,gu-1)),sum)));
	else
	  cho.pb(mp(mp(min(gu,ki-le),min(gu,ki-((ll)num.size()-1-ri))),mp(mp(min(ki-le,gu),min(ki,gu)),sum)));
      }else if(le==0){
	if(cho.size()==0)
	  cho.pb(mp(mp(min(gu-1,ki-le),min(gu,ki-((ll)num.size()-1-ri))),mp(mp(min(ki,gu-1),min(ki-((ll)num.size()-1-ri),gu)),sum)));
	else
	  cho.pb(mp(mp(min(gu,ki-le),min(gu,ki-((ll)num.size()-1-ri))),mp(mp(min(ki,gu),min(ki-((ll)num.size()-1-ri),gu)),sum)));
      }else{
 	cho.pb(mp(mp(min(gu,ki-le),min(gu,ki-((ll)num.size()-1-ri))),mp(mp(min(ki-le,gu),min(ki-((ll)num.size()-1-ri),gu)),sum)));
      }
    }
    i-=zero;
  }
  ll ans=llINF;
  ll sum=0;
  for(int i=0;i<cho.size();i++){
    //ここでとまるとき
    ll summ=sum+rui[cho.size()]-rui[i+1]+cho[i].S.F.F;
    //cout<<summ<<" "<<rui[cho.size()]<<" "<<rui[i]<<" "<<sum<<" "<<cho[i].S.F.F<<endl;
    ans=min(ans,summ);
    if(i!=cho.size()-1)
      sum+=cho[i].F.F+ze[mp(i,i+1)];
  }
  sum=0;
  //cout<<cho.size()<<endl;
  for(int i=cho.size()-1;i>=0;i--){
    //ここでとまるとき
    ll summ=sum+rui[i]+cho[i].S.F.S;
    //cout<<summ<<" "<<rui[i]<<" "<<sum<<" "<<cho[i].S.F.S<<endl;
    ans=min(ans,summ);
    if(i!=0)
      sum+=cho[i].F.S+ze[mp(i,i-1)];
  }
  cout<<ans<<endl;
  return 0;
}