#include <bits/stdc++.h>
using namespace std;
int read() {
  int x = 0;
  bool flg = false;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') flg = true;
  for (; isdigit(ch); ch = getchar()) x = (x << 3) + (x << 1) + (ch ^ 48);
  return flg ? -x : x;
}
int n;
char s[500010];
struct C {
  int p, L;
} stk[500010];
int top;
long long getsum(int l, int r) { return (l + r) * 1LL * (r - l + 1) / 2; }
long long coef(int o) {
  if (!o) return 0;
  const C &v = stk[o];
  int P = stk[o + 1].p - v.p;
  return getsum(stk[o + 1].L + 1, stk[o].L) +
         (P - stk[o].L + stk[o + 1].L) * 1LL * stk[o + 1].L;
}
void solve() {
  n = read();
  scanf("%s", s + 1);
  long long ans = 0, tot = 0;
  for (int i(1); i <= (n); i++) {
    if (s[i] == '0') {
      ans += tot;
      if (top) ans += getsum(1, stk[top].L) + stk[1].L * 1LL * (stk[1].p - 1);
      continue;
    }
    if (i == 1 || s[i - 1] == '0') {
      stk[++top] = (C){i, 1};
      tot += coef(top - 1);
    } else {
      C v = stk[top--];
      v.L++;
      while (top && v.L > stk[top].L) tot -= coef(top), top--;
      tot -= coef(top);
      stk[++top] = v;
      tot += coef(top - 1);
    }
    ans += tot;
    ans += getsum(1, stk[top].L) + stk[1].L * 1LL * (stk[1].p - 1);
  }
  printf("%lld\n", ans);
}
int main() {
  for (int T = 1; T--;) solve();
}
