#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int letter, prev = 0;
  int ans = 0;
  while (n-- > 0) {
    cin >> letter;
    if (letter == 0 && prev == 0) continue;
    if (letter == 1) {
      ans++;
      prev = 1;
    }
    if (letter == 0 && prev == 1) {
      ans++;
      prev = 0;
    }
  }
  if (prev == 0 && ans > 0) ans--;
  cout << ans << endl;
}
