#include <bits/stdc++.h>
using namespace std;
int n, ans;
int main() {
  int i, j, k;
  scanf("%d", &n);
  for (i = k = 0; i < n; i++) {
    scanf("%d", &j);
    if (j == 0 && k == 0)
      continue;
    else if (j == 0 && k == 2)
      k = 1;
    else if (j == 0 && k == 1)
      k = 0;
    else if (j == 1 && k == 1)
      k = 2, ans += 2;
    else if (j == 1)
      ans++, k = 2;
  }
  printf("%d\n", ans);
  return 0;
}
