#include <bits/stdc++.h>
using namespace std;
vector<pair<int, char>> v;
vector<int> zeros;
string s;
long long solve(int lo, int hi) {
  if (hi - lo == 1) {
    if (s[lo] == '0') return 0;
    if (s[lo] == '1') return 1;
  }
  auto idx1 = lower_bound(zeros.begin(), zeros.end(), lo);
  auto idx2 = lower_bound(zeros.begin(), zeros.end(), hi);
  if (idx1 == idx2) {
    long long len = hi - lo;
    return len * (len + 1) * (len + 2) / 6;
  }
  int mid;
  mid = *(idx1 + (idx2 - idx1) / 2);
  if (lo == mid) {
    long long len = hi - lo;
    return len * (len + 1) * (len + 2) / 6 - len;
  }
  long long ans = solve(lo, mid) + solve(mid, hi);
  int running = 0;
  int best = 0;
  vector<long long> lt;
  for (int i = mid - 1; i >= lo; i--) {
    if (s[i] == '1')
      running++;
    else
      running = 0;
    best = max(best, running);
    lt.push_back(best);
  }
  vector<long long> rg;
  running = 0;
  best = 0;
  for (int i = mid; i < hi; i++) {
    if (s[i] == '1')
      running++;
    else
      running = 0;
    best = max(best, running);
    rg.push_back(best);
  }
  while (true) {
    if (lt.empty() && rg.empty()) {
      return ans;
    }
    if (lt.empty() || rg.back() > lt.back()) {
      ans += (mid - lo) * rg.back();
      rg.pop_back();
      hi--;
    } else {
      ans += (hi - mid) * lt.back();
      lt.pop_back();
      lo++;
    }
  }
}
int main() {
  int n;
  cin >> n;
  cin >> s;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '0') zeros.push_back(i);
  }
  cout << solve(0, n);
}
