#include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[] = {1, 0, -1, 0, 1, -1, 1, -1};
void fast() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
}
int arr[10000], idx;
int mem[1000][2][3000];
int solve(int i, int c, int cnt) {
  if (i == idx) return cnt;
  int &ret = mem[i][c][cnt];
  if (ret != -1) return ret;
  int mn = INT_MAX;
  if (arr[i] == 1)
    mn = min(solve(i + 1, 1, cnt + 1), solve(i + 1, 0, cnt + 2));
  else
    mn = solve(i + 1, c, cnt + c);
  return ret = mn;
}
int main() {
  fast();
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    if (arr[i]) idx = i + 1;
  }
  memset(mem, -1, sizeof mem);
  cout << solve(0, 0, 0) << "\n";
}
