#include <bits/stdc++.h>
#pragma GCC optimize("O2")
using namespace std;
const int N = 5e5 + 10;
const long long mod = 1e9 + 7;
const long long mod2 = 998244353;
const long long inf = 8e18;
const int LOG = 22;
long long pw(long long a, long long b, long long M) {
  return (!b ? 1
             : (b & 1 ? (a * pw(a * a % M, b / 2, M)) % M
                      : pw(a * a % M, b / 2, M)));
}
long long seg[4 * N], lz[4 * N], Ans[N], Nxt[N], Prv[N], ind[N], V[N];
void second(int v, int tl, int tr) {
  if (lz[v] == 0 || tl == tr) {
    return;
  }
  int mid = (tl + tr) >> 1;
  lz[v << 1] += lz[v];
  lz[v << 1 | 1] += lz[v];
  seg[v << 1] += 1ll * (mid - tl + 1) * lz[v];
  seg[v << 1 | 1] += 1ll * (tr - mid) * lz[v];
  lz[v] = 0;
}
void B(int v, int tl, int tr) {
  if (tl == tr) {
    seg[v] = V[tl];
    return;
  }
  int mid = (tl + tr) >> 1;
  B(v << 1, tl, mid);
  B(v << 1 | 1, mid + 1, tr);
  seg[v] = seg[v << 1] + seg[v << 1 | 1];
}
void U(int v, int tl, int tr, int l, int r, long long x) {
  if (l > r) return;
  second(v, tl, tr);
  if (tl == l && tr == r) {
    lz[v] += x;
    seg[v] += 1ll * (tr - tl + 1) * x;
    return;
  }
  int mid = (tl + tr) >> 1;
  if (l <= mid) {
    U(v << 1, tl, mid, l, min(r, mid), x);
  }
  if (r > mid) {
    U(v << 1 | 1, mid + 1, tr, max(mid + 1, l), r, x);
  }
  seg[v] = seg[v << 1] + seg[v << 1 | 1];
}
long long G(int v, int tl, int tr, int l, int r) {
  second(v, tl, tr);
  if (tl == l && tr == r) {
    return seg[v];
  }
  int mid = (tl + tr) >> 1;
  long long ans = 0;
  if (l <= mid) {
    ans = G(v << 1, tl, mid, l, min(r, mid));
  }
  if (r > mid) {
    ans += G(v << 1 | 1, mid + 1, tr, max(mid + 1, l), r);
  }
  return ans;
}
vector<pair<int, int> > Q[N];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  int n;
  string s;
  cin >> n >> s;
  s = "." + s;
  for (int i = 1; i <= n; i++) {
    if (s[i] == '1') {
      Prv[i] = Prv[i - 1] + 1;
    }
  }
  for (int i = n; i >= 1; i--) {
    if (s[i] == '1') {
      Nxt[i] = Nxt[i + 1] + 1;
      Q[i + Nxt[i]].push_back(make_pair(Nxt[i], i));
    }
  }
  deque<pair<int, int> > dq;
  for (int i = n; i >= 1; i--) {
    while (!dq.empty() && dq.front().first <= Prv[i]) {
      dq.pop_front();
    }
    for (auto cu : Q[i]) {
      int it = lower_bound((dq).begin(), (dq).end(), make_pair(cu.first, -1)) -
               dq.begin();
      if (it == (int)dq.size()) {
        Ans[cu.second] = n;
      } else {
        Ans[cu.second] = dq[it].second - 1;
      }
    }
    dq.push_front(make_pair(Prv[i], i));
  }
  for (int i = 1; i <= n; i++) {
    V[i] = max(V[i - 1], Prv[i]);
  }
  for (int i = 1; i <= n; i++) {
  }
  long long ans = 0;
  B(1, 1, n);
  for (int i = 1; i <= n; i++) {
    Ans[i] = max(Ans[i], i + Nxt[i] - 1);
    long long cu = G(1, 1, n, i, n);
    ans += cu;
    if (s[i] == '1') {
      U(1, 1, n, i, Ans[i], -1);
    }
  }
  cout << "\n";
  cout << ans << "\n";
  return 0;
}
