#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const long long mod = 1e9 + 7;
const long long mod1 = 998244353;
const long long inf = 1e18;
const long long nax = 500005;
long long n, dp[nax], suff[nax];
string s;
void solve() {
  cin >> n >> s;
  dp[n] = 0;
  long long ans = 0;
  stack<long long> stk;
  suff[n] = 0;
  for (long long i = n - 1; i >= 0; i--) {
    if (s[i] == '0') {
      suff[i] = 0;
      dp[i] = dp[i + 1];
    } else {
      suff[i] = suff[i + 1] + 1;
      long long nxt = n;
      while (!stk.empty()) {
        if (suff[stk.top()] >= suff[i]) {
          nxt = stk.top();
          break;
        }
        stk.pop();
      }
      long long init = nxt - i;
      long long gap = init - suff[i];
      dp[i] = dp[nxt] + suff[i] * (suff[i] + 1) / 2 + suff[i] * gap;
      if (nxt != n) dp[i] += suff[i] * (suff[i] - 1) / 2;
      if (nxt != n) stk.push(nxt);
      stk.push(i);
    }
    ans += dp[i];
  }
  cout << ans;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long t = 1;
  while (t--) solve();
  return 0;
}
