#include <bits/stdc++.h>
using namespace std;
inline long long rd() {
  long long x = 0;
  int ch = getchar();
  bool f = 1;
  while (!isdigit(ch) && (ch != '-') && (ch != EOF)) ch = getchar();
  if (ch == '-') {
    f = 0;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 1) + (x << 3) + ch - '0';
    ch = getchar();
  }
  return f ? x : -x;
}
void write(long long x) {
  if (x >= 10)
    write(x / 10), putchar(x % 10 + '0');
  else
    putchar(x + '0');
}
inline void wrt(long long x, int p) {
  if (x < 0) putchar('-'), x = -x;
  write(x);
  if (p) putchar(p);
}
struct node {
  int l, r;
  int L;
  inline int len() { return r - l + 1; }
} sta[500005];
int n, top;
long long sum[500005];
char s[500005];
long long ans = 0;
long long f[500005];
inline long long addit(int x) { return 1ll * x * (x + 1) / 2; }
int main() {
  n = rd();
  scanf("%s", s + 1);
  int la = 0;
  for (int i = 1; i <= n; i++) {
    if (s[i] == '0') {
      la = 0;
      f[i] = f[i - 1];
      ans += f[i];
      continue;
    }
    if (!la) la = i;
    node x = {la, i, 0};
    while (top && x.len() >= sta[top].len()) {
      x.L = sta[top].L - (x.len() - sta[top].len());
      top--;
    }
    long long nowres = 0;
    if (top) {
      long long in = addit(sta[top].len()) - addit(x.len());
      long long out = 1ll * sta[top].len() * (sta[top].l - sta[top].L);
      sum[top] = sum[top - 1] + in + out;
      x.L = sta[top].r - x.len() + 1;
      in = addit(x.len());
      out = 1ll * x.len() * (x.l - x.L);
      ++top;
      sum[top] = sum[top - 1] + in + out;
      sta[top] = x;
    } else {
      x.L = 1;
      sta[top = 1] = x;
      long long in = addit(sta[top].len());
      long long out = 1ll * sta[top].len() * (sta[top].l - sta[top].L);
      sum[top] = in + out;
    }
    f[i] = sum[top];
    ans += f[i];
  }
  wrt(ans, '\n');
}
