#include <bits/stdc++.h>
using namespace std;
vector<int> v, idx;
int main() {
  int n;
  scanf("%d", &n);
  int cnt = 0, zcnt = 0, ans = 0, fans = 0, flag = 0;
  for (int i = 0; i < n; i++) {
    int temp;
    scanf("%d", &temp);
    if (temp == 1 && cnt != 0) {
      ans += min(1, zcnt);
      zcnt = 0;
      cnt++;
    }
    if (temp == 1 && cnt == 0) {
      cnt = 1;
      flag = 1;
    }
    if (flag && temp == 0) zcnt++;
    v.push_back(temp);
  }
  fans = cnt + ans;
  printf("%d\n", fans);
}
