#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 5;
int fa[N], mx[N * 4];
void update(int l, int r, int root, int p, int v) {
  if (l == r) {
    mx[root] = v;
    return;
  }
  int mid = l + r >> 1;
  if (mid >= p)
    update(l, mid, root << 1, p, v);
  else
    update(mid + 1, r, root << 1 | 1, p, v);
  mx[root] = max(mx[root << 1], mx[root << 1 | 1]);
}
int query(int l, int r, int root, int ql, int qr, int v) {
  if (mx[root] < v) return -1;
  if (l == r) return l;
  int mid = l + r >> 1;
  if (l >= ql && r <= qr) {
    if (mx[root << 1] >= v)
      return query(l, mid, root << 1, ql, qr, v);
    else
      return query(mid + 1, r, root << 1 | 1, ql, qr, v);
  }
  int ans = -1;
  if (mid >= ql) ans = query(l, mid, root << 1, ql, qr, v);
  if (ans == -1 && mid < qr) ans = query(mid + 1, r, root << 1 | 1, ql, qr, v);
  return ans;
}
char s[N];
int main() {
  int n;
  scanf("%d", &n);
  scanf("%s", s + 1);
  fa[n + 1] = n + 1;
  for (int i = n; i; i--) fa[i] = s[i] == '0' ? i : fa[i + 1];
  long long sum = 0, mx = 0, now = 0;
  for (int i = 1; i <= n; i++) {
    if (s[i] == '1')
      now++;
    else
      now = 0;
    mx = max(mx, now);
    sum += mx;
    update(1, n, 1, i, now);
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += sum;
    if (s[i] == '0') continue;
    int p = fa[i];
    int en;
    if (p != n + 1) {
      int v = p - i;
      en = query(1, n, 1, p, n, v);
      if (en == -1)
        en = n;
      else
        en--;
    } else
      en = n;
    sum -= (en - i + 1);
  }
  printf("%lld\n", ans);
  return 0;
}
