#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 5;
int n, m;
int a[N], X[N], Y[N];
char s[N];
set<pair<int, int> > st;
long long ans;
long long inc[N];
int main() {
  scanf("%d%s", &n, s + 1);
  for (int i = 1; i <= n; i++) inc[i] = inc[i - 1] + 1ll * i * (i + 1) / 2;
  for (int i = 1; i <= n; i++) a[i] = s[i] - '0';
  for (int i = 1; i <= n; i++)
    if (a[i] == 1) {
      X[++m] = i;
      int j = i;
      while (j <= n && a[j] == 1) j++;
      Y[m] = j - 1;
      i = j;
    }
  for (int i = 1; i <= m; i++) {
    int x = X[i], y = Y[i];
    for (int r = x; r < y; r++) st.insert(make_pair(r - x + 1, x));
    for (int l = y; l > x; l--) st.insert(make_pair(y - l + 1, l));
    st.insert(make_pair(y - x + 1, x));
  }
  for (int i = 1; i <= m; i++) {
    set<pair<int, int> >::iterator it;
    int x = X[i], y = Y[i];
    for (int r = x; r < y; r++) {
      int len = r - x + 1, l;
      it = st.find(make_pair(len, x));
      if (it == st.begin())
        l = 1;
      else {
        it--;
        if (it->first != len)
          l = 1;
        else
          l = (it->second) + 1;
      }
      ans += 1ll * (x - l + 1) * len;
    }
    for (int l = y; l > x; l--) {
      int len = y - l + 1, r;
      it = st.find(make_pair(len + 1, l - 1));
      it++;
      if (it == st.end())
        r = n;
      else if (it->first != len + 1)
        r = n;
      else
        r = (it->second) + len - 1;
      ans += 1ll * (r - y + 1) * len;
    }
    int len = y - x + 1, l, r;
    it = st.find(make_pair(len, x));
    if (it == st.begin())
      l = 1;
    else {
      it--;
      if (it->first != len)
        l = 1;
      else
        l = (it->second) + 1;
    }
    it = st.lower_bound(make_pair(len + 1, l));
    if (it == st.end())
      r = n;
    else if (it->first != len + 1)
      r = n;
    else
      r = (it->second) + len - 1;
    ans += 1ll * (r - y + 1) * (x - l + 1) * len;
    if (y - x > 1) ans += inc[y - x - 1];
  }
  printf("%lld\n", ans);
  return 0;
}
