#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
  int r;
  while (b) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
int GCD(int a, int b) {
  if (a == 0)
    return b;
  else
    return GCD(b % a, a);
}
long long int power(long long int a, long long int b) {
  long long int r = 1;
  while (b) {
    if (b & 1) {
      r = r * a;
      b--;
    }
    a = a * a;
    b >>= 1;
  }
  return r;
}
int palindrome(char *s, int len) {
  int i, j, k, count = 0, c = 0;
  for (i = 0, j = len - 1; i < len, j >= 0; i++, j--) {
    if (s[i] == s[j]) count++;
  }
  if (count == len)
    return 1;
  else
    return 0;
}
long long int binary(char *s, long long int len) {
  long long int i, j, k, r = 0, p = 0, num;
  for (i = len - 1; i >= 0; i--) {
    num = s[i] - '0';
    r += (num * power(2, p));
    p++;
  }
  return r;
}
int binary_search(int *ara, int size, int n) {
  int high, mid, low;
  low = 0;
  high = size - 1;
  while (low <= high) {
    mid = (low + high) / 2;
    if (ara[mid] == n) {
      return 1;
    } else if (n > ara[mid])
      low = mid + 1;
    else if (n < ara[mid])
      high = mid - 1;
  }
  return -1;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int i, j, k, n, T, q, count;
  string s1, s2;
  cin >> n;
  int ara[n];
  for (i = 0; i < n; i++) cin >> ara[i];
  if (ara[0] == 1)
    count = 1;
  else
    count = -1;
  for (i = 1; i < n; i++) {
    if (ara[i] == 1 && ara[i - 1] == 0)
      count += 2;
    else if (ara[i] == 1 && ara[i - 1] == 1)
      count++;
  }
  if (count == -1) count = 0;
  cout << count << endl;
}
