#include <bits/stdc++.h>
using namespace std;
int main() {
  long long N, i, a = 0;
  cin >> N;
  int A[N];
  for (i = 0; i < N; i++) {
    cin >> A[i];
  }
  int cnt = 0, C = 0;
  for (i = 0; i < N; i++) {
    if (A[i] == 1) cnt++;
  }
  for (i = 0; i < N; i++) {
    int f = 0;
    if (A[i] == 1) {
      a++;
      f = 1;
      C++;
    }
    if (f == 1 && C != cnt) {
      if (A[i + 1] == 0) a++;
    }
  }
  cout << a;
  return 0;
}
