#include <iostream>
#include <algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<ctime>
#include<set>
#include<iomanip>
#define sqr(x) (x)*(x)
#define fz1(i,n) for ((i)=1;(i)<=(n);(i)++)
#define fd1(i,n) for ((i)=(n);(i)>=1;(i)--)
#define fz0g(i,n) for ((i)=0;(i)<=(n);(i)++)
#define fd0g(i,n) for ((i)=(n);(i)>=0;(i)--)
#define fz0k(i,n) for ((i)=0;(i)<(n);(i)++)
#define fd0k(i,n) for ((i)=(long long)((n)-1);(i)>=0;(i)--)
#define fz(i,x,y) for ((i)=(x);(i)<=(y);(i)++)
#define fd(i,y,x) for ((i)=(y);(i)>=(x);(i)--)
#define ff(c,itr) for (__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
#define rdst(st,len){static char ss[len];scanf(" %s",ss);(st)=ss;}
//#define T_ int
using namespace std;
//struct bit{T_ arr[1000005];int lowbit(int x){return x&(-x);}void add(int x,T_ y){for (int i=x;i<=n;i+=lowbit(i)) arr[i]+=y;}T_ query(int x){T_ s=0;for (int i=x;i>0;i-=lowbit(i)) s+=arr[i];return s;}};
//struct seg{struct treedot{int l,r;T_ sum,add;void update(T_ x){sum+=(r-l+1)*x; add+=x;}}c[800005];void pushdown(int id){if (c[id].add){c[id*2].update(c[id].add);c[id*2+1].update(c[id].add);c[id].add=0;}}void pushup(int id){c[id].sum=c[id*2].sum+c[id*2+1].sum;}void build(int id,int l,int r){if (l>r) return;c[id].l=l;c[id].r=r;c[id].sum=0;c[id].add=0;if (l==r) c[id].sum=0;else{int mid=l+(r-l)/2;build(id*2,l,mid);build(id*2+1,mid+1,r);pushup(id);}}void update(int id,int le,int ri,T_ x){if (le>c[id].r||ri<c[id].l) return;if (le<=c[id].l&&c[id].r<=ri) c[id].update(x);else{pushdown(id);update(id*2,le,ri,x);update(id*2+1,le,ri,x);pushup(id);}}T_ query(int id,int le,int ri){if (ri<c[id].l||c[id].r<le) return 0;if (le<=c[id].l&&c[id].r<=ri) return c[id].sum;T_ ans=0;pushdown(id);ans+=query(id*2,le,ri);ans+=query(id*2+1,le,ri);pushup(id);return ans;}};
//struct bit_2d{T_ a[2005][2005];int lowbit(int x){return x&(-x);}void add(int x,int y,T_ z){int i,j;for (i=x;i<=2000;i+=lowbit(i)){for (j=y;j<=2000;j+=lowbit(j)){a[i][j]+=z;}}}T_ query(int x,int y){int i,j;T_ s=0;for (i=x;i;i-=lowbit(i)){for (j=y;j;j-=lowbit(j)){s+=a[i][j];}}return s;}T_ query(int x,int y,int xx,int yy){return query(xx,yy)-query(x-1,yy)-query(xx,y-1)+query(x-1,y-1);}};
long long n,m,i,j,a[200005],ans,lst,sl[200005],sr[200005],sl2[200005],sr2[200005],sl3[200005],sr3[200005],sl4[200005],sr4[200005],sl5[200005],sr5[200005],sum[200005],s;
int main()
{
	scanf("%lld",&n);
	fz1(i,n)
	{
		scanf("%lld",&a[i]);
		sum[i]=sum[i-1]+a[i];
	}
	ans=sum[n];
	fz1(i,n)
	{
		sl[i]=sl[i-1];
		if (a[i]%2==0&&a[i]!=0) sl[i]++; else sl[i]--;
		sl2[i]=sl2[i-1];
		if (a[i]==0) sl2[i]--;
			else if (a[i]%2==0) sl2[i]+=a[i]-1;
				else sl2[i]+=a[i];
		sl3[i]=sl[i]+sl2[i];
		sl4[i]=min(sl4[i-1],sl3[i]); 
		sl5[i]=max(0ll,sl3[i]-sl4[i-1]);
	}
	fd1(i,n)
	{
		sr[i]=sr[i+1];
		if (a[i]%2==0&&a[i]!=0) sr[i]++; else sr[i]--;
		sr2[i]=sr2[i+1];
		if (a[i]==0) sr2[i]--;
			else if (a[i]%2==0) sr2[i]+=a[i]-1;
				else sr2[i]+=a[i];
		sr3[i]=sr[i]+sr2[i];
		sr4[i]=min(sr4[i+1],sr3[i]);
		sr5[i]=max(0ll,sr3[i]-sr4[i+1]);
	}
	ans=min(ans,sum[n]-sr4[1]);
	fz1(i,n)
	{
		long long t=sl5[i-1];
		if (a[i]==0) t--;
			else if (a[i]%2==0) t+=a[i]-1;
				else t+=a[i];
		sl5[i]=max(sl5[i],t);
		ans=min(ans,sum[n]-sl5[i]-sr5[i+1]);
	}
	printf("%lld\n",ans);
	return 0;
}

Measured memory_bytes: 18944