#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
template <class T, class S>
ostream &operator<<(ostream &o, const pair<T, S> &p) {
  return o << '(' << p.first << ", " << p.second << ')';
}
template <template <class, class...> class T, class... A>
typename enable_if<!is_same<T<A...>, string>(), ostream &>::type operator<<(
    ostream &o, T<A...> V) {
  o << '[';
  for (auto a : V) o << a << ", ";
  return o << ']';
}
long long int n, dp[500010], contrib[500010], mx[500010], sz[500010],
    lst[500010], lf[500010];
string s;
long long int r(long long int i) {
  if (~dp[i]) return dp[i];
  if (i == n) return dp[i] = 0;
  if (s[i] == '0') return dp[i] = r(i + 1);
  r(i + contrib[i]);
  if (sz[i] == mx[i])
    return dp[i] = (n - i) * sz[i] - ((sz[i]) * ((sz[i]) - 1) / 2);
  return dp[i] = r(i + contrib[i] - sz[i]) + (contrib[i] - sz[i]) * sz[i];
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  memset(dp, -1, sizeof dp);
  cin >> n >> s;
  for (long long int i = 0; i < (n); i++) {
    if (s[i] == '0' || !i || s[i - 1] == '0')
      lf[i] = i;
    else
      lf[i] = lf[i - 1];
  }
  long long int cur = 0;
  for (long long int i = n - 1; i > (-1); i--) {
    if (s[i] == '1')
      cur++;
    else
      cur = 0;
    sz[i] = cur;
    mx[i] = max(mx[i + 1], cur);
    if (sz[i] == mx[i])
      contrib[i] = n - i;
    else if (s[i] == '0')
      contrib[i] = 0;
    else
      contrib[i] = lf[lst[cur]] + sz[i] - i;
    lst[cur] = i;
  }
  long long int ans = 0;
  for (long long int i = 0; i < (n); i++) ans += r(i);
  cout << ans << '\n';
}
