#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> len(n);
  len[n - 1] = s[n - 1] - '0';
  for (int i = n - 2; i >= 0; --i) {
    len[i] = s[i] - '0';
    if (s[i] == '1') len[i] += len[i + 1];
  }
  auto sum = [&](int x) { return (long long)x * (x + 1) / 2; };
  long long ans = 0;
  vector<int> pos;
  for (int i = n - 1; i >= 0; --i) {
    while (!pos.empty() && len[pos.back()] <= len[i]) pos.pop_back();
    if (len[i]) pos.push_back(i);
    for (int j = pos.size(), pl = 0; --j >= 0;) {
      int k = pos[j];
      int l = len[k];
      ans += sum(l - pl) + (long long)(l - pl) * (n - (k + l));
      pl = l;
    }
  }
  cout << ans << "\n";
  return 0;
}
