#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  long long int n, temp, ans = 0;
  vector<long long int> v;
  queue<long long int> unread;
  cin >> n;
  for (long long int i = 0; i < n; i++) {
    cin >> temp;
    v.push_back(temp);
    if (temp == 1) unread.push(i);
  }
  while (unread.empty() == false) {
    long long int u = unread.front();
    unread.pop();
    ans++;
    if (u < n - 1) {
      if (v[u + 1] == 1) continue;
    }
    if (unread.empty() == false) {
      ans++;
    }
  }
  cout << ans << endl;
}
