#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int c = 0, arr[1001];
  arr[0] = -1;
  int one = 0;
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
    if (arr[i] == 1) one++;
  }
  for (int i = 1; i <= n; i++) {
    if (arr[i] == 0 && arr[i - 1] == 1 && one >= 1) c++;
    if (arr[i] == 1) {
      c++;
      one--;
    }
  }
  cout << c << endl;
}
