#include <bits/stdc++.h>
using namespace std;
bool visited[1000001];
vector<int> v, w;
void ts() { cerr << 1.0 * clock() / CLOCKS_PER_SEC << ".s\n"; }
int bin(int low, int high, int key) {
  while (low <= high) {
    int mid = (low + high) / 2;
    if (v[mid] < key) {
      low = mid + 1;
    } else if (v[mid] > key) {
      high = mid - 1;
    } else
      return mid;
  }
}
void f() {
  bool t = false;
  for (int i = 0; i < v.size(); i++) {
    if (v[i] == 0 && t == false)
      ;
    else if (v[i] == 1) {
      w.push_back(1);
      t = true;
    } else if (v[i] == 0 && t == true) {
      w.push_back(0);
    }
  }
}
int main() {
  int n, temp, temp1 = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &temp);
    v.push_back(temp);
  }
  f();
  v = w;
  w.clear();
  reverse(v.begin(), v.end());
  f();
  for (int i = 0; i < w.size(); i++) {
    temp = 0;
    if (w[i] == 1)
      temp1++;
    else {
      while (w[i + temp] != 1 && w.size() > i + temp) {
        temp++;
      }
      if (temp == 1) {
        temp1++;
      } else
        i = i + temp - 1;
    }
  }
  printf("%d\n", temp1);
  return 0;
}
