#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 500;
int gti(void) {
  char c = getchar();
  int ret = 0, st = 1;
  for (; c != '-' && !isdigit(c); c = getchar())
    ;
  if (c == '-') st = -1, c = getchar();
  for (; isdigit(c); c = getchar()) ret = ret * 10 + c - '0';
  return ret * st;
}
char s[N];
long long stk[N], top = 1, l[N];
int main(void) {
  int n = gti();
  long long ans = 0, tot = 0;
  scanf("%s", s);
  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      if (stk[top] != 0) stk[++top] = 0, l[top] = i;
    } else {
      if (stk[top] == 0) l[top] = i;
      if (top > 1)
        tot -= (l[top] - l[top - 1] - (stk[top - 1] - stk[top])) * stk[top];
      stk[top]++;
      if (top > 1)
        tot += (l[top] - l[top - 1] - (stk[top - 1] - stk[top])) * stk[top];
    }
    while (top > 1 && stk[top - 1] <= stk[top]) {
      tot -= (l[top] - l[top - 1] - (stk[top - 1] - stk[top])) * stk[top];
      if (top > 2)
        tot -= (l[top - 1] - l[top - 2] - (stk[top - 2] - stk[top - 1])) *
               stk[top - 1];
      stk[top - 1] = stk[top], l[top - 1] = l[top], --top;
      if (top > 1)
        tot += (l[top] - l[top - 1] - (stk[top - 1] - stk[top])) * stk[top];
    }
    ans += tot;
    if (top) ans += 1ll * stk[1] * (stk[1] + 1ll) / 2 + stk[1] * l[1];
  }
  printf("%lld\n", ans);
  return 0;
}
