#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > d;
int main() {
  int n, ans = 0, tmp;
  bool state = false;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> tmp;
    if (tmp == 1) {
      ans++;
      state = true;
    } else if (state) {
      ans++;
      state = false;
    }
  }
  if (state == false && ans > 0) ans--;
  cout << ans;
}
