#include <bits/stdc++.h>
#include <iostream>
using namespace std;
//printf("%.12lf\n",mid);
//const long long sze = 2*(2^(long long)(ceil(log(100000)/log(2))));
/*
// get part of decimal
double intpart;
double thiss = modf((double)(n*m)/(double)k, &intpart);
*/
const long long mod = 1000000007;
const double eps = .0000000001;
inline void prep()
{
    cin.tie(0);
    cin.sync_with_stdio(0);
};
/*
for (int i=0; i<k; i++){
      
}*/
long long dpp[200001][4];
bool already[200001][4];
long long arr[200001];
long long sums[200001];
int n;
long long func(int ind, int many){
  if (ind >= n){
    return 0;
  }
  if (many == 4){
    return 0;
  }
  if (already[ind][many]){
    return dpp[ind][many];
  }
  if (arr[ind] == 0){
    if (many == 0){
      long long minn = arr[ind]+func(ind+1, 0);
      minn = min(minn, arr[ind]+func(ind+1, 1));
      minn = min(minn, arr[ind]+func(ind+1, 2));
      minn = min(minn, arr[ind]+func(ind+1, 3));
      if (ind != 0){
        minn = min(minn, sums[n-1]-sums[ind-1]);
      }else{
        minn = min(minn, sums[n-1]);
      }
      already[ind][many] = true;
      dpp[ind][many] = minn;
      return minn;
    }else if (many == 1){
      long long minn;
     
      minn = func(ind+1, 1)+2;
      minn = min(minn, func(ind+1, 2)+2);
      minn = min(minn, func(ind+1, 3)+1);
      if (ind != 0){
        minn = min(minn, sums[n-1]-sums[ind-1]);
      }else{
        minn = min(minn, sums[n-1]);
      }
      already[ind][many] = true;
      dpp[ind][many] = minn;
      return minn;
    }else if (many == 2){
      long long minn;
      
      minn = func(ind+1, 2)+1;
      minn = min(minn, func(ind+1, 3)+1);
      if (ind != 0){
        minn = min(minn, sums[n-1]-sums[ind-1]);
      }else{
        minn = min(minn, sums[n-1]);
      }
      already[ind][many] = true;
      dpp[ind][many] = minn;
      return minn;
    }else if (many == 3){
      long long minn;
     
      minn = func(ind+1, 3)+2;
      if (ind != 0){
        minn = min(minn, sums[n-1]-sums[ind-1]);
      }else{
        minn = min(minn, sums[n-1]);
      }
      already[ind][many] = true;
      dpp[ind][many] = minn;
      return minn;
    }else{
      if (ind != 0){
        already[ind][many] = true;
        dpp[ind][many] = sums[n-1]-sums[ind-1];
        return dpp[ind][many];
      }else{
        already[ind][many] = true;
        dpp[ind][many] = sums[n-1];
        return dpp[ind][many];
      }
    }
  }else{
    if (many == 0){
      long long minn = arr[ind]+func(ind+1, 0);
      minn = min(minn, arr[ind]+func(ind+1, 1));
      minn = min(minn, arr[ind]+func(ind+1, 2));
      minn = min(minn, arr[ind]+func(ind+1, 3));
      if (ind != 0){
        minn = min(minn, sums[n-1]-sums[ind-1]);
      }else{
        minn = min(minn, sums[n-1]);
      }
      already[ind][many] = true;
      dpp[ind][many] = minn;
      return minn;
    }else if (many == 1){
      long long minn;
      if (arr[ind]%2 == 0){
        minn = func(ind+1, 1);
        minn = min(minn, func(ind+1, 2));
        minn = min(minn, func(ind+1, 3));
        if (ind != 0){
          minn = min(minn, sums[n-1]-sums[ind-1]);
        }else{
          minn = min(minn, sums[n-1]);
        }
      }else{
        minn = 1+func(ind+1, 1);
        minn = min(minn, func(ind+1, 2)+1);
        minn = min(minn, func(ind+1, 3)+1);
        if (ind != 0){
          minn = min(minn, sums[n-1]-sums[ind-1]);
        }else{
          minn = min(minn, sums[n-1]);
        }
      }
      already[ind][many] = true;
      dpp[ind][many] = minn;
      return minn;
    }else if (many == 2){
      long long minn;
      if (arr[ind]%2 == 1){
        minn = func(ind+1, 2);
        minn = min(minn, func(ind+1, 3));
        if (ind != 0){
          minn = min(minn, sums[n-1]-sums[ind-1]);
        }else{
          minn = min(minn, sums[n-1]);
        }
      }else{
        minn = 1+func(ind+1, 2);
        minn = min(minn, func(ind+1, 3)+1);
        if (ind != 0){
          minn = min(minn, sums[n-1]-sums[ind-1]);
        }else{
          minn = min(minn, sums[n-1]);
        }
      }
      already[ind][many] = true;
      dpp[ind][many] = minn;
      return minn;
    }else if (many == 3){
      long long minn;
      if (arr[ind]%2 == 0){
        minn = func(ind+1, 3);
        if (ind != 0){
          minn = min(minn, sums[n-1]-sums[ind-1]);
        }else{
          minn = min(minn, sums[n-1]);
        }
      }else{
        minn = 1+func(ind+1, 3);
        if (ind != 0){
          minn = min(minn, sums[n-1]-sums[ind-1]);
        }else{
          minn = min(minn, sums[n-1]);
        }
      }
      already[ind][many] = true;
      dpp[ind][many] = minn;
      return minn;
    }else{
      if (ind != 0){
        already[ind][many] = true;
        dpp[ind][many] = sums[n-1]-sums[ind-1];
        return dpp[ind][many];
      }else{
        already[ind][many] = true;
        dpp[ind][many] = sums[n-1];
        return dpp[ind][many];
      }
    }
  }
  
}
int main(){
    prep();
    ofstream fout("sleepy.out");
    ifstream fin("sleepy.in");
    // test on if you don't need to make any changes
    cin >> n;
    
    for (int i=0; i<n; i++){
      cin >> arr[i];
      sums[i] = arr[i];
    }
    for (int i=1; i<n; i++){
      sums[i] = sums[i]+sums[i-1];
    }
    cout << min(func(0, 0), min(func(0, 1), min(func(0, 2), func(0, 3)))); 
    return 0;
}    

Measured memory_bytes: 23040