#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0, f = 1;
  char c = getchar();
  while ((c < '0' || c > '9') && (c != '-')) c = getchar();
  if (c == '-') f = -1, c = getchar();
  while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  return x * f;
}
const int N = 5e5 + 10;
int n, top, stk[N], a[N];
char s[N];
long long pre[N];
inline long long calc2(int l, int r) {
  if (l > r) return 0;
  return 1ll * (l + r) * (r - l + 1) / 2;
}
inline long long calc(int l, int r) {
  if (l == 0) return 0;
  return 1ll * a[r] * (r - l) + calc2(1, a[l] - a[r] - 1);
}
int main() {
  scanf("%d%s", &n, s + 1);
  int l = 0;
  long long ans = 0;
  for (register int i = (1); i <= (n); i++) {
    if (s[i] == '1') {
      if (!l) {
        a[l = i] = 1, stk[++top] = i,
              pre[top] = pre[top - 1] + calc(stk[top - 1], stk[top]);
      } else {
        a[l]++, pre[top] = pre[top - 1] + calc(stk[top - 1], stk[top]);
      }
      while (top > 1 && a[stk[top]] >= a[stk[top - 1]]) {
        stk[top - 1] = stk[top];
        --top, pre[top] = pre[top - 1] + calc(stk[top - 1], stk[top]);
      }
    } else
      l = 0;
    ans += pre[top];
    ans += calc2(1, a[stk[top]] - 1);
    ans += 1ll * a[stk[1]] * stk[1];
  }
  printf("%lld\n", ans);
}
