#include <bits/stdc++.h>
using namespace std;
const long long N = 5e5 + 10;
long long ones[N], go[N], calc[N];
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  string s;
  cin >> s;
  s.insert(s.begin(), '0');
  for (long long i = 1; i <= n; i++) {
    if (s[i] == '0') {
      ones[i] = 0;
    } else {
      ones[i] = ones[i - 1] + 1;
    }
  }
  vector<pair<long long, long long>> st;
  for (long long i = 1; i <= n; i++) {
    while (!st.empty() && st.back().first <= ones[i]) {
      st.pop_back();
    }
    if (!st.empty()) {
      go[i] = st.back().second;
    } else {
      go[i] = 0;
    }
    st.push_back({ones[i], i});
  }
  long long ans = 0;
  for (long long i = 1; i <= n; i++) {
    if (s[i] == '0') {
      calc[i] = calc[i - 1];
      ans += calc[i];
      continue;
    }
    calc[i] = ones[i] * (ones[i] + 1) / 2ll;
    calc[i] += ones[i] * (i - go[i] - ones[i]);
    if (go[i]) {
      calc[i] +=
          calc[go[i]] + (ones[i] * ones[i] - (ones[i] * (ones[i] + 1) / 2ll));
    }
    ans += calc[i];
  }
  cout << ans << '\n';
}
