#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, i, j, k;
  scanf("%lld", &n);
  long long arr[n];
  for (i = 0; i < n; i++) {
    scanf("%lld", &arr[i]);
  }
  long long start = -1, end = -1;
  for (i = 0; i < n; i++) {
    if (arr[i] == 1) {
      start = i;
      break;
    }
  }
  for (i = n - 1; i >= 0; i--) {
    if (arr[i] == 1) {
      end = i;
      break;
    }
  }
  long long z = 0;
  for (i = start; i <= end; i++) {
    if (arr[i] == 0) {
      long long temp = 1;
      for (j = i + 1; j <= end; j++) {
        if (arr[j] == 0) {
          temp++;
        } else {
          i = j;
          z += temp;
          if (temp == 1) z--;
          break;
        }
      }
    }
  }
  long long ans = end - start + 1 - z;
  if (start == -1 and end == -1) ans = 0;
  printf("%lld\n", ans);
  return 0;
}
