#include <bits/stdc++.h>

using namespace std;
using namespace placeholders;

template <class T> void mini(T &l, T r) {l = min(l, r);}
template <class T> void maxi(T &l, T r) {l = max(l, r);}

template <class TH> void _dbg(const char *sdbg, TH h) {
  cerr << sdbg << "=" << h << "\n";
}
template <class TH, class ...TA> void _dbg(const char *sdbg, TH h, TA ...a) {
  while (*sdbg != ',') cerr << *sdbg++; cerr << "=" << h << ","; _dbg(sdbg + 1, a...);
}
template <class T> ostream &operator<<(ostream &os, vector <T> v) {
  os << "[";
  for (auto vv : v) os << vv << ",";
  return os << "]";
}
template <class L, class R> ostream &operator<<(ostream &os, pair <L, R> p) {
  return os << "(" << p.first << "," << p.second << ")";
}

#define eput(...) //_dbg(#__VA_ARGS__, __VA_ARGS__)

const int N = 5e5 + 10;

int n;
char s[N];
int pre[N], nxt[N];
int dd[N][2];
int dp[N][3];

int get(int x, int l, int r) {
  if (l > r) return 0;
  return dd[r][x] - (l == 0 ? 0 : dd[l - 1][x]);
}

vector < pair <int, int> > split() {
  vector < pair <int, int> > v;
  for (int i = 0; i < n; ++i) {
    if (s[i] == '0') {
      if (i == 0 || s[i - 1] == '0' || i == n - 1 || s[i + 1] == '0') {
        //split
      } else {
        if (v.size() > 0) {
          v.back().second = i;
        }
      }
    } else {
      if (v.empty() || v.back().second + 1 < i) {
        v.push_back(make_pair(i, i));
      } else {
        v.back().second = i;
      }
    }
  }
  return v;
}

int go(char *s, int n) {
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < 3; ++j) {
      dp[i][j] = 0;
    }
  }
  fill_n(pre, n, -1);
  int lst = -1;
  for (int i = 1; i < n; ++i) {
    pre[i] = lst;
    if (s[i] == '0') lst = i;
  }
  for (int i = 0; i < n; ++i) {
    if (s[i] == '0') {
      int p = pre[i];
      if (p == -1) {
        dp[i][0] = 0;
        maxi(dp[i][1], i);
        maxi(dp[i][2], 0);
      } else {
        maxi(dp[i][0], dp[p][0] + 0);
        maxi(dp[i][0], dp[p][1] + 0);
        maxi(dp[i][0], dp[p][2] + i - p - 1);
        maxi(dp[i][1], dp[p][0] + i - p - 1);
        maxi(dp[i][2], dp[p][0] + 0);
        if (i - p - 2 > 0) {
          maxi(dp[i][1], dp[p][1] + i - p - 2);
          //maxi(dp[i][1], dp[p][2] + 
          maxi(dp[i][2], dp[p][1] + 0);
          maxi(dp[i][2], dp[p][2] + i - p - 2);
        }
      }
    }
  }

  int pp = pre[n - 1];
  if (pp == -1) return 0;
  int ret = 0;
  maxi(ret, dp[pp][0]);
  eput(ret);
  maxi(ret, dp[pp][1]);
  eput(ret);
  maxi(ret, dp[pp][2] + n - pp - 1);
  return ret;
}


int go(int l, int r) {
  return go(s + l, r - l + 1);
  /*
     for (int i = l; i < r; ++i) {
     int tmp = 0;
  //eput(i, tmp, l, pre[i], nxt[i + 1], r);
  tmp += get(1, l, pre[i]);
  //cerr << get(1, l, pre[i]) << endl;
  tmp -= get(0, l, pre[i] - 1);
  //cerr << get(0, l, pre[i]) << endl;
  tmp += get(1, nxt[i + 1], r);
  //cerr << get(1, nxt[i + 1], r) << endl;
  tmp -= get(0, nxt[i + 1] + 1, r);
  //cerr << get(0, nxt[i + 1], r) << endl;
  maxi(ret, tmp);
  }*/
}

void run() {
  scanf("%d", &n);
  scanf("%s", s);
  dd[0][s[0] - '0']++;
  for (int i = 1; i < n; ++i) {
    dd[i][0] = dd[i - 1][0] + (s[i] == '0');
    dd[i][1] = dd[i - 1][1] + (s[i] == '1');
  }
  auto v = split();
  eput(v);
  pre[0] = -1;
  for (int i = 1; i < n; ++i) {
    if (s[i - 1] == '0') {
      pre[i] = i - 1;
    } else {
      pre[i] = pre[i - 1];
    }
  }
  nxt[n - 1] = n;
  for (int i = n - 2; i >= 0; --i) {
    if (s[i + 1] == '0') nxt[i] = i + 1;
    else nxt[i] = nxt[i + 1];
  }
  int ans = 0;
  for (auto p : v) {
    eput(go(p.first, p.second));
    ans += go(p.first, p.second);
  }
  printf("%d\n", ans);
}


int main() {
  run();
  return 0;
}


Measured memory_bytes: 14464