#include <bits/stdc++.h>
using namespace std;
using ll = long long;
string a;
ll ans;
void Solve(int s, int e) {
  if (s == e) {
    if (a[s] == '1') ans++;
    return;
  }
  int m = s + (e - s) / 2, mid = 0;
  if (a[m] == '1' && a[m + 1] == '1') {
    int l = m, r = m + 1;
    mid = 2;
    while (r + 1 <= e && a[r + 1] == '1') r++, mid++;
    while (l - 1 >= s && a[l - 1] == '1') l--, mid++;
    ll szl = m - l + 1;
    ll szr = r - m;
    ll tmp = szl * (szr + 1) * szr / 2 + szr * (szl + 1) * szl / 2;
    ans += tmp;
    int ll = l, rr = r;
    int cur = 0;
    while (ll - 1 >= s)
      if (a[ll - 1] == '0')
        cur = 0, ll--;
      else if (cur + 1 >= mid)
        break;
      else
        cur++, ll--;
    cur = 0;
    while (rr + 1 <= e)
      if (a[rr + 1] == '0')
        cur = 0, rr++;
      else if (cur + 1 > mid)
        break;
      else
        cur++, rr++;
    tmp = ((l - ll) * 1LL * (rr - r) + rr - r + l - ll) * 1LL * mid;
    ans += tmp;
    ll = l;
    rr = r;
    r = m + 1;
    cur = 0;
    while (r < rr) {
      while (l - 1 >= s) {
        if (a[l - 1] == '0')
          l--, cur = 0;
        else if (cur + 1 >= r - ll + 1)
          break;
        else
          cur++, l--;
      }
      ans += (r - ll + 1) * 1LL * (ll - l);
      r++;
    }
    r = rr;
    l = m;
    cur = 0;
    while (l > ll) {
      while (r + 1 <= e) {
        if (a[r + 1] == '0')
          cur = 0, r++;
        else if (cur + 1 > rr - l + 1)
          break;
        else
          cur++, r++;
      }
      ans += (rr - l + 1) * 1LL * (r - rr);
      l--;
    }
  }
  {
    int l = m + 1, r = m, vatl = 0;
    if (mid) {
      while (l - 1 >= s && a[l - 1] == '1') l--;
      vatl = m - l + 1;
      r++;
      vatl++;
    }
    l--;
    int maxl = 0, curl = 0, curr = vatl, maxr = vatl;
    while (l >= s) {
      if (a[l] == '1')
        maxl = max(maxl, ++curl);
      else
        curl = 0;
      while (r + 1 <= e && maxr <= maxl) {
        if (a[r + 1] == '0') {
          r++, curr = 0;
          continue;
        }
        if (curr + 1 > maxl) break;
        curr++;
        maxr = max(maxr, curr);
        r++;
      }
      if (maxr <= maxl) {
        ll tmp = maxl * 1LL * (r - m);
        ans += tmp;
      }
      l--;
    }
  }
  {
    int l = m + 1, r = m, vatr = 0;
    if (mid) {
      while (r + 1 <= e && a[r + 1] == '1') r++;
      vatr = r - (m + 1) + 1;
      l--;
      vatr++;
    }
    r++;
    int maxr = 0, curr = 0, curl = vatr, maxl = vatr;
    while (r <= e) {
      if (a[r] == '1')
        maxr = max(maxr, ++curr);
      else
        curr = 0;
      while (l - 1 >= s && maxr > maxl) {
        if (a[l - 1] == '0') {
          l--;
          curl = 0;
          continue;
        }
        if (curl + 1 >= maxr) break;
        maxl = max(maxl, ++curl);
        l--;
      }
      if (maxr > maxl) {
        ll tmp = maxr * 1LL * (m - l + 1);
        ans += tmp;
      }
      r++;
    }
  }
  Solve(s, m);
  Solve(m + 1, e);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  cin >> a;
  Solve(0, n - 1);
  cout << ans << '\n';
}
