#include <bits/stdc++.h>
using namespace std;
const int oo = 1001;
int x[1001], n, ct;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> x[i];
  }
  ct = 0;
  bool open = false;
  for (int i = 0; i < n; ++i) {
    if (x[i]) {
      if (!open)
        ct++, open = 1;
      else
        ct++;
    } else if (open and !x[i])
      open = 0, ct++;
  }
  cout << max(0, ct - (1 - x[n - 1]));
  return 0;
}
