#include <bits/stdc++.h>
using namespace std;
int dp[1024][2];
int s[1024];
int n;
int cnt;
int f(int idx, int md) {
  if (md && idx == cnt - 1) return 0;
  if (dp[idx][md] != -1) return dp[idx][md];
  int r;
  if (md) {
    r = f(idx + 1, 1) + 2;
    if (idx + 1 < cnt) {
      r = min(r, f(idx + 1, 1) + (s[idx + 1] - s[idx]));
    }
  } else {
    r = f(idx, 1) + 1;
  }
  return dp[idx][md] = r;
}
int main() {
  cin >> n;
  int t, prev = -1;
  memset(dp, -1, sizeof(dp));
  for (int i = 0; i < n; i++) {
    cin >> t;
    if (t == 1) {
      s[cnt++] = i;
    }
  }
  if (cnt == 0)
    cout << 0 << endl;
  else
    cout << f(0, 0) << endl;
  return 0;
}
