#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  bool a[n];
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i]) cnt++;
  }
  int ans = 0;
  bool c = 0;
  int j = 0;
  for (int i = 0; i < n; i++) {
    if (a[i]) {
      ans++;
      c = 1;
      j++;
    } else {
      if (c) {
        if (!((i + 1 < n and a[i + 1] == 0) or
              (a[i - 1] == 0 and i - 1 >= 0)) and
            j < cnt) {
          ans++;
        }
      }
    }
  }
  cout << ans;
  return 0;
}
