#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, q = 0, s = 0, t = 0;
  cin >> n;
  for (int k = 1; k <= n; k++) {
    cin >> q;
    if (q == 1) {
      if (t == 1)
        s++;
      else {
        t++;
        s++;
      }
    } else {
      if (t == 1) {
        t--;
        s++;
      }
    }
  }
  if (t == 0 && s != 0) s--;
  cout << s;
  return 0;
}
