#include <bits/stdc++.h>
using namespace std;
const int N = 500050;
template <typename T>
inline void read(T& x) {
  T f = 1, c = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    c = c * 10 + ch - '0';
    ch = getchar();
  }
  x = f * c;
}
int n, dp[4][N], hed[N], tal[N];
char s[N];
int hs[N];
int main() {
  scanf("%d%s", &n, s + 1);
  for (int i = 1; i <= n; i++)
    if (s[i] == '1') {
      if (s[i - 1] != '1')
        hed[i] = i;
      else
        hed[i] = hed[i - 1];
    }
  for (int i = n; i >= 1; i--)
    if (s[i] == '1') {
      if (s[i + 1] != '1')
        tal[i] = i;
      else
        tal[i] = tal[i + 1];
    }
  for (int i = 1, cnt = 0; i <= n; i++)
    if (s[i] == '1') {
      if (s[i - 1] != '1')
        cnt = 1;
      else
        cnt++;
      dp[0][i] = max(hs[cnt + 1] + 1, 1);
      dp[1][i] = max(hs[cnt] + 1, 1);
      if (s[i + 1] == '0') {
        for (int j = i - hed[i] + 1; j >= 0; j--) hs[j] = i - j + 1;
      }
    }
  memset(hs, 0x3f, sizeof(hs));
  for (int i = n, cnt = 0; i >= 1; i--)
    if (s[i] == '1') {
      if (s[i + 1] != '1')
        cnt = 1;
      else
        cnt++;
      dp[2][i] = min(hs[cnt + 1] - 1, n);
      dp[3][i] = min(hs[cnt] - 1, n);
      if (s[i - 1] == '0') {
        for (int j = tal[i] - i + 1; j >= 0; j--) hs[j] = i + j - 1;
      }
    }
  long long ans = 0;
  for (int i = 1; i <= n; i++)
    if (s[i] == '1') {
      if (s[i + 1] == '1')
        ans += 1ll * (i - hed[i] + 1) * (hed[i] - dp[1][i] + 1);
      if (s[i - 1] == '1')
        ans += 1ll * (tal[i] - i + 1) * (dp[2][i] - tal[i] + 1);
      else {
        ans += 1ll * (tal[i] - i + 1) * (i - dp[1][tal[i]] + 1) *
               (dp[2][i] - tal[i] + 1);
        for (int j = 1, len = tal[i] - i - 1; j <= len; j++)
          ans += 1ll * j * (len - j + 1);
      }
    }
  printf("%lld\n", ans);
  return 0;
}
