#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 10;
long long t[maxn << 2], lz[maxn << 2];
char s[maxn];
int n, a[maxn];
void pushdown(int now, int l, int r) {
  if (!lz[now]) return;
  t[now << 1] += lz[now];
  t[now << 1 | 1] += lz[now];
  lz[now << 1] += lz[now];
  lz[now << 1 | 1] += lz[now];
  lz[now] = 0;
}
int quary(int now, int l, int r, int k) {
  if (l == r) return t[now] == k ? l : l - 1;
  pushdown(now, l, r);
  if (t[now << 1 | 1] >= k)
    return quary(now << 1 | 1, ((l + r) >> 1) + 1, r, k);
  return quary(now << 1, l, ((l + r) >> 1), k);
}
void add(int now, int l, int r, int L, int R) {
  if (L > R) return;
  if (l == L && r == R) {
    t[now]++;
    lz[now]++;
    return;
  }
  pushdown(now, l, r);
  add(now << 1, l, ((l + r) >> 1), L, min(((l + r) >> 1), R));
  add(now << 1 | 1, ((l + r) >> 1) + 1, r, max(L, ((l + r) >> 1) + 1), R);
  t[now] = max(t[now << 1], t[now << 1 | 1]);
}
int main() {
  scanf("%d", &n);
  scanf("%s", s + 1);
  for (int i = 1; i <= n; i++) a[i] = s[i] - '0';
  int cnt = 0;
  long long ans = 0;
  long long now = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i]) {
      cnt++;
      int pos = quary(1, 1, n, cnt);
      pos++;
      add(1, 1, n, pos, i);
      now += i - pos + 1;
    } else
      cnt = 0;
    ans += now;
  }
  printf("%lld\n", ans);
}
