#include <bits/stdc++.h>
#define MOD 1000000007
#define pb push_back
using namespace std;
const int nmax=212345;

int L,a[nmax],l,r,lmax[nmax],rmax[nmax],pos,cnt,cnt2,cmax,tst2,rtst3[nmax],rtst4[nmax],ltst,ltst3[nmax],ltst4[nmax],ncnt[nmax],scnt,icnt,res,tmp[nmax],tmp2,t11,t12[2],spos,flg,flg2;
int flg3;

int main()
{
        (void)scanf("%d",&L);
    for(int i=1; i<=L; ++i)
    {
       (void)scanf("%d",&a[i]);
    }
    pos=1;
    for(int i=1; i<=L+1; ++i)
    {
        tmp[pos]+=a[i];
        if(tmp[pos]>1e6)
            tmp[pos]=1e6;
        if(a[i]==0)
        {
            if(a[i-1]!=0)
            {
                lmax[pos]=l;
                rmax[pos]=i;
                ++pos;
            }
            l=i;
        }
    }
    for(int i2=1; i2<pos; ++i2)
    {
        for(int i=lmax[i2]+1; i<rmax[i2]; ++i)
        {
            if(a[i]%2==0)
            {
                ncnt[i2]+=2;
                a[i]=2;
            }
            else
            {
                ncnt[i2]+=1;
                a[i]=1;
            }
        }
    }
    for(int i2=2; i2<pos; ++i2)
    {
        ltst4[i2]=ltst4[i2-1]+tmp[i2-1];
        if(ltst4[i2]>1e6)
            ltst4[i2]=1e6;
        tmp2=(lmax[i2]-lmax[i2-1])*2-ncnt[i2-1];
        tst2+=tmp[i2-1]-tmp2;
        if(tst2>0)
        {
            ltst3[i2]=ltst3[i2-1]+tmp2;
        }
        else
        {
            ltst3[i2]=ltst4[i2];
            tst2=0;
        }
    }
    tst2=0;
    for(int i2=pos-2; i2>0; --i2)
    {
        rtst4[i2]=rtst4[i2+1]+tmp[i2+1];
        if(rtst4[i2]>1e6)
            rtst4[i2]=1e6;
        tmp2=(rmax[i2+1]-rmax[i2])*2-ncnt[i2+1];
        tst2+=tmp[i2+1]-tmp2;
        if(tst2>0)
        {
            rtst3[i2]=rtst3[i2+1]+tmp2;
        }
        else
        {
            rtst3[i2]=rtst4[i2];
            tst2=0;
        }
    }
    res=1e6;
    rmax[0]=lmax[1]+1;
  	cnt=0;
    for(int i2=1; i2<pos; ++i2)
    {
        cnt+=lmax[i2]-rmax[i2-1]+1;
        if(ltst3[i2]<ltst+cnt)
        {
          	++flg3;
            ltst=ltst3[i2];
            spos=i2;
            cnt=0;
        }
        a[lmax[i2]]=a[rmax[i2]]=1;
        int i=lmax[i2]+1;
        l=lmax[i2]+1;
        r=lmax[i2];
        icnt=scnt=cnt2=0;
        for(; i<rmax[i2]; ++i)
        {
            if(a[i]==1)
            {
                ++scnt;
                ++icnt;
            }
            else
            {
                ++cnt;
                icnt+=2;
                if(a[i-1]==1)
                {
                    if(cnt2<0)
                        cnt2-=r-l+1;
                    else
                        cnt2=-(r-l+1);
                    cnt2+=i-r-1;
                    if(cnt2>=0)
                    {
                        t11=0;
                        flg=0;
                        flg2=0;
                    }
                    else if(flg==1)
                    {
                        flg=0;
                        flg2=cnt2;
                    }
                    l=i;
                }
                if(a[i+1]==1)
                {
                    r=i;
                    if(t11!=0)
                    {
                        if(cnt2-flg2<0)
                            cmax=min(t12[t11]+cnt2,ltst+cnt2-flg2);
                        else
                            cmax=min(t12[t11]+cnt2,ltst);
                        cmax+=cnt-(r-l+1)+min((rmax[i2]-r-1)*2-(ncnt[i2]-icnt)+rtst3[i2],ncnt[i2]-icnt+rtst4[i2]);
                    }
                    else
                    {
                        cmax=ltst+cnt-(r-l+1)+min((rmax[i2]-r-1)*2-(ncnt[i2]-icnt)+rtst3[i2],ncnt[i2]-icnt+rtst4[i2]);
                        if(cnt2-flg2<0)
                            cmax+=cnt2-flg2;
                    }
                    if(cmax<res)
                        res=cmax;
                    if(cnt2<0)
                        tmp2=min(ltst3[i2]+scnt,ltst4[i2]+icnt-(r-l+1)*2+cnt2);
                    else
                        tmp2=min(ltst3[i2]+scnt,ltst4[i2]+icnt-(r-l+1)*2);
                    if(tmp2<ltst+cnt)
                    {
                      	++flg3;
                        if(i2==spos)
                        {
                            if(t11==0)
                            {
                                ++t11;
                                t12[t11]=ltst;
                            }
                            t12[t11]+=cnt;
                        }
                        flg=1;
                        ltst=tmp2;
                        spos=i2;
                        cnt=0;
                    }
                }
            }
        }
        t11=0;
        cmax=ltst+cnt+rtst4[i2];
        if(cmax<res)
            res=cmax;
    }
      cmax=ltst+cnt+rtst4[pos-1];
        if(cmax<res)
            res=cmax;
  
    cout<<res<<endl;
    return 0;
}


Measured memory_bytes: 6400