#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> gp;
vector<bool> vis;
char grid[502][502];
bool visted[502][502];
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
void dfs(int node) {
  if (vis[node]) return;
  vis[node] = true;
  for (size_t i = 0; i < gp[node].size(); i++) dfs(gp[node][i]);
}
int fact(int n) {
  if (n == 0) {
    return 1;
  }
  return n * fact(n - 1);
}
int fibonacci(int n) {
  if (n == 1 || n == 2) {
    return 1;
  }
  return fibonacci((n - 1)) + fibonacci((n - 2));
}
bool Ok(long long mid);
long long n, k;
long long a[200001], l = 1, r = 2000000000;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int n, one = 0, cnt = 0;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] == 1) {
      one++;
    }
  }
  for (int i = 0; i < n; i++) {
    if (a[i] == 1) {
      one--;
      if (one > 0) {
        cnt += 2;
        if (a[i + 1] == 1) {
          cnt--;
        }
      } else {
        cnt++;
      }
    }
  }
  cout << cnt << endl;
}
