#include <bits/stdc++.h>
using namespace std;
int n;
string s;
vector<int> d;
vector<long long> t;
long long cal(int l, int r) {
  if (l == r) return s[l] == '1';
  long long res = 0;
  int m = (l + r) >> 1;
  res += cal(l, m) + cal(m + 1, r);
  int numLef = 0;
  while (m - numLef >= l && s[m - numLef] == '1') numLef++;
  int numRig = 0;
  while (m + 1 + numRig <= r && s[m + 1 + numRig] == '1') numRig++;
  d.resize(r - m);
  t.resize(d.size() + 1);
  int cnt = 0, Max = 0;
  for (int i = m + 1; i <= r; i++) {
    if (s[i] == '1')
      cnt++;
    else
      cnt = 0;
    Max = max(Max, cnt);
    d[i - m - 1] = Max;
  }
  t[d.size()] = 0;
  for (int i = (int)d.size() - 1; i >= 0; i--) t[i] = t[i + 1] + d[i];
  if (numLef) {
    for (int i = 1, j = numRig; i <= numLef; i++) {
      res += 1LL * i * numRig + 1LL * numRig * (numRig + 1) / 2;
      for (; j < d.size() && d[j] < i + numRig; j++)
        ;
      res += 1LL * (j - numRig) * (i + numRig) + t[j];
    }
    int cnt = numLef, Max = numLef;
    for (int i = m + 1; i <= r; i++) {
      if (s[i] == '1')
        cnt++;
      else
        cnt = 0;
      Max = max(Max, cnt);
      d[i - m - 1] = Max;
    }
    t[d.size()] = 0;
    for (int i = (int)d.size() - 1; i >= 0; i--) t[i] = t[i + 1] + d[i];
  }
  Max = numLef, cnt = 0;
  for (int i = m - numLef, j = 0; i >= l; i--) {
    if (s[i] == '1')
      cnt++;
    else
      cnt = 0;
    Max = max(Max, cnt);
    for (; j < d.size() && d[j] < Max; j++)
      ;
    res += 1LL * j * Max + t[j];
  }
  return res;
}
int main() {
  if (fopen("main.in", "r")) freopen("main.in", "r", stdin);
  cin >> n >> s;
  s = ' ' + s + ' ';
  cout << cal(1, n) << '\n';
  return 0;
}
