#include <bits/stdc++.h>
using namespace std;
int arr[1000009];
int main() {
  int n, t = 0, p = -1;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  for (int i = 0; i < n; i++) {
    if (arr[i] == 1) {
      p = i;
      break;
    }
  }
  for (int i = 0; i < n; i++) {
    if (arr[i] == 0) {
      t = 0;
      for (int j = i; j < n; j++) {
        if (arr[j] != 0) break;
        t = j;
      }
      if (t > i) {
        for (int j = i; j <= t; j++) {
          arr[j] = -1;
        }
        i = t;
      }
    }
  }
  for (int i = 0; i < p; i++) arr[i] = -1;
  for (int i = n - 1; i >= 0; i--) {
    if (arr[i] == 1) {
      p = i + 1;
      break;
    }
  }
  for (int i = n; i >= p; i--) {
    arr[i] = -1;
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (arr[i] == 0 or arr[i] == 1) ans++;
  }
  cout << ans << endl;
  return 0;
}
