#include <bits/stdc++.h>
using namespace std;
const long long INF = 2e18;
const long long INFINT = 2e9;
const int N = 1000006;
const int NN = 1006;
int main() {
  ios_base::sync_with_stdio(0);
  int n;
  cin >> n;
  int ans = 0;
  int f = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (x == 0 && ans == 0) continue;
    if (x == 1) {
      f = 0;
    } else {
      f++;
    }
    if (f < 2) ans++;
    if (f == 2) ans--;
  }
  if (f == 1) ans--;
  cout << ans << "\n";
}
