#include <bits/stdc++.h>
using namespace std;
long long int z[1000000], x[1000000], y[1000000], m, szu, wy[100][1];
char s[1000000], cc;
long long ile(long long a, long long b) {
  long long w, c, d;
  c = a / b;
  d = a % b;
  w = (b - d) * c * c;
  w += (d) * (c + 1) * (c + 1);
  return (w);
}
int main() {
  long long int chu, d, e, f, g, i, j, q, t, k, a, n, b, c, w, p, aa, bb, cc,
      dd, nn, kk;
  t = 1;
  for (q = 1; q <= t; q++) {
    scanf("%lld%s", &n, s);
    w = 0;
    a = 0;
    b = 0;
    x[0] = 1;
    for (i = 0; i < n; i++) {
      if (s[i] == '1') {
        b++;
        a += x[b - 1] + b - 1;
        x[b] += x[b - 1];
        x[b - 1] = 1;
      } else {
        b = 0;
        x[b]++;
      }
      w += a;
    }
    printf("%lld\n", w);
  }
  return (0);
}
