#include <bits/stdc++.h>
using namespace std;
struct debugger {
  template <typename T>
  debugger& operator,(const T& v) {
    cerr << v << " ";
    return *this;
  }
} dbg;
int main() {
  long long int n, t, prev = -1, count = 0;
  cin >> n;
  cin >> t;
  prev = t;
  if (t) count++;
  n--;
  while (n--) {
    prev = t;
    cin >> t;
    if (prev || t) count++;
  }
  if (count && t == 0) count--;
  cout << count << endl;
}
