#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, i, j, k = 0, c = 0, l = 0;
  cin >> n;
  int arr[n];
  for (i = 0; i < n; i++) {
    cin >> arr[i];
    if (arr[i] == 1) {
      l++;
      k = 1;
      c++;
    }
    if (k && arr[i] == 0) {
      c++;
      k = 0;
    }
  }
  if (arr[n - 1] == 0 && l != 0)
    cout << c - 1 << endl;
  else
    cout << c << endl;
  return 0;
}
