#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int n;
ll a[200005];
ll sum_odd[200005];
ll sum_even[200005];
ll sum_r[200005];
ll sum_l[200005];
ll sum[200005];
ll zero[200005];

class segtree{
public:
    static const int N=1<<18;
    ll dp[1<<19];
    ll lazy[1<<19];
    segtree() {
        memset(dp, 0, sizeof(dp));
    }

    void lazy_eval(int k){
        dp[k]+=lazy[k];
        if(k<N-1){
            lazy[k*2+1]+=lazy[k];
            lazy[k*2+2]+=lazy[k];
        }
        lazy[k]=0;
    }

    void lazy_upd(int k){
        dp[k]=min(dp[k*2+1],dp[k*2+2]);
    }

    void update(int a,int b,ll v,int k=0,int l=0,int r=N){
        lazy_eval(k);
        if(b<=l || r<=a)return;
        if(a<=l && r<=b) {
            lazy[k]+=v;
            lazy_eval(k);
            return;
        }
        ll mid=(l+r)/2;
        update(a,b,v,k*2+1,l,mid);
        update(a,b,v,k*2+2,mid,r);
        lazy_upd(k);
    }

    ll query(int a,int b,int k=0,int l=0,int r=N){
        lazy_eval(k);
        if(b<=l || r<=a)return (1LL<<60);
        if(a<=l && r<=b)return dp[k];
        ll mid=(l+r)/2;
        ll vl=query(a,b,k*2+1,l,mid);
        ll vr=query(a,b,k*2+2,mid,r);
        lazy_upd(k);
        return min(vl,vr);
    }
};

segtree seg;

int main(void){
    scanf("%d",&n);
    ll ans=0;
    for(int i=0;i<n;i++){
        scanf("%lld",&a[i]);
        ans+=a[i];
    }
    for(int i=0;i<n;i++){
        sum_odd[i+1]+=sum_odd[i];
        sum_odd[i+1]+=(a[i])%2LL;

        sum_even[i+1]+=sum_even[i];
        sum_even[i+1]+=(a[i]+1)%2LL;

        sum[i+1]+=sum[i];
        sum[i+1]+=a[i];

        zero[i+1]+=zero[i];
        if(a[i]==0)zero[i+1]+=2;
    }

    for(int i=n-1;i>=0;i--){
        sum_r[i]+=sum_r[i+1];
        if(a[i]==0){
            sum_r[i]-=2;
        }else if(a[i]>=2){
            sum_r[i]+=a[i]-(a[i]%2);
        }
    }

    for(int i=0;i<n;i++){
        sum_l[i+1]+=sum_l[i];
        if(a[i]==0){
            sum_l[i+1]-=2;
        }else if(a[i]>=2){
            sum_l[i+1]+=a[i]-(a[i]%2);
        }
    }
    int rbest=n;
    ll sumc=0;
    for(int i=n-1;i>=0;i--){
        if(a[i]==0){
            sumc-=2;
        }else{
            sumc+=a[i]-(a[i]%2);
        }
        if(sumc<0) {
            sumc = 0;
            rbest = i;
        }
        //printf("%d %d %lld %lld %lld\n",i,rbest,sum_odd[rbest]-sum_odd[i],(zero[rbest]-zero[i]),sum[n]-sum[rbest]);
        seg.update(i,i+1,sum_odd[rbest]-sum_odd[i]+sum[n]-sum[rbest]+(zero[rbest]-zero[i]));
    }
    for(int i=0;i<=n;i++){
        seg.update(i,i+1,sum_even[i]);
    }
    int lbest=0;
    sumc=0;
    for(int i=0;i<n;i++){
        ll val=seg.query(i,n+1);
        val+=sum_odd[i]-sum_odd[lbest]+sum[lbest]+zero[i]-zero[lbest];
        ans=min(ans,val);
        //printf("%lld %d %lld %lld %lld\n",val,lbest,sumc,sum_odd[i]-sum_odd[lbest]+sum[lbest],zero[i]-zero[lbest]);
        if(a[i]==0){
            sumc-=2;
        }else{
            sumc+=a[i]-(a[i]%2);
        }
        if(sumc<0){
            sumc=0;
            lbest=i+1;
        }
        if(a[i]%2==0LL){
            seg.update(i,n+1,-1);
        }
    }
    printf("%lld\n",ans);
    return 0;
}

Measured memory_bytes: 19328