#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 5e5 + 5, inf = 1e9 + 7;
int n, mda[N], lg[N], s1[N][22], s2[N][22];
string s;
void Build1() {
  for (int i = 2; i <= n; ++i) lg[i] = lg[i / 2] + 1;
  for (int i = 1; i <= n; ++i) {
    s1[i][0] = mda[i];
  }
  for (int j = 1; j <= 20; ++j) {
    for (int i = 1; i + (1 << (j - 1)) <= n; ++i) {
      s1[i][j] = max(s1[i][j - 1], s1[i + (1 << (j - 1))][j - 1]);
    }
  }
}
int Get1(int l, int r) {
  int len = r - l + 1;
  int j = lg[len];
  return max(s1[l][j], s1[r - (1 << j) + 1][j]);
}
void Build2() {
  for (int i = 1; i <= n; ++i) {
    s2[i][0] = mda[i];
  }
  for (int j = 1; j <= 20; ++j) {
    for (int i = 1; i + (1 << (j - 1)) <= n; ++i) {
      s2[i][j] = max(s2[i][j - 1], s2[i + (1 << (j - 1))][j - 1]);
    }
  }
}
int Get2(int l, int r) {
  int len = r - l + 1;
  int j = lg[len];
  return max(s2[l][j], s2[r - (1 << j) + 1][j]);
}
int main() {
  ios ::sync_with_stdio(0), cin.tie(0);
  cin >> n >> s;
  int cur = 0;
  for (int i = n - 1; i >= 0; i--) {
    cur = cur * (s[i] == '1') + (s[i] == '1');
    mda[i + 1] = cur;
  }
  Build1();
  cur = 0;
  for (int i = 0; i < n; ++i) {
    cur = cur * (s[i] == '1') + (s[i] == '1');
    mda[i + 1] = cur;
  }
  Build2();
  ll ans = 0;
  for (int i = 0; i < n; ++i) {
    if (s[i] == '0') continue;
    int j = i;
    while (s[j] == '1') ++j;
    j--;
    int sz = j - i + 1, a = 0, b = 0;
    for (int k = 1; k <= sz; ++k) ans += k * 1LL * (sz - k + 1);
    for (int k = i; k <= j; ++k) {
      int L = 0, R = i - 1, cnt = 0;
      while (R >= L) {
        int M = (L + R) / 2;
        if (M + 1 <= i && Get1(M + 1, i) < k - i + 1) {
          cnt = i - M;
          R = M - 1;
        } else {
          L = M + 1;
        }
      }
      if (k == j) a = cnt;
      ans += cnt * 1LL * (k - i + 1);
    }
    for (int k = j; k >= i; k--) {
      int L = j + 1, R = n - 1, cnt = 0;
      while (R >= L) {
        int M = (L + R) / 2;
        if (j + 2 <= M + 1 && Get2(j + 2, M + 1) <= j - k + 1) {
          cnt = M - j;
          L = M + 1;
        } else {
          R = M - 1;
        }
      }
      if (k == i) b = cnt;
      ans += cnt * 1LL * (j - k + 1);
    }
    ans += a * 1LL * b * sz;
    i = j;
  }
  cout << ans;
  return 0;
}
