#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6;
int a[maxn], sum, ans;
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= n; i++) {
    if (a[i] == 1 && a[i + 1] == 0) sum++;
    if (a[i] == 1) ans++;
  }
  cout << ans + (max(sum - 1, 0));
}
