#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
using namespace std;
int mod = 1e9 + 7;
int inf = 2e9;
long long int linf = 1000000ll * 10000 * 10000;
const int maxn = 5e5 + 20;
int A[maxn];
long long int res, psum[maxn];
long long int C3(long long int x) {
  if (x < 3) return 0;
  x *= (x - 1) * (x - 2);
  x /= 6;
  return x;
}
void DAQ(long long int l, long long int r) {
  if (r - l <= 2) {
    int ans = 0;
    for (int i = l; i < r; i++) {
      if (A[i]) ans++;
    }
    if (r - l == 2) ans += A[l] + A[l + 1];
    res += ans;
    return;
  }
  long long int tmp = (r - l) / 3;
  long long int mid1 = l + tmp - 1;
  long long int mid2 = r - tmp;
  long long int last = -1;
  for (int i = mid1 + 1; i < mid2; i++) {
    if (A[i] == 0) last = i;
  }
  if (last == -1) {
    while (mid1 >= l && A[mid1]) mid1--;
    while (mid2 < r && A[mid2]) mid2++;
    long long int len = mid2 - mid1 - 1;
    res += C3(len + 2);
    res += len * (mid1 - l + 1) * (r - mid2);
    DAQ(l, mid1 + 1);
    DAQ(mid2, r);
    if (mid1 >= l) {
      int curmx = 0, mx = 0;
      for (int i = mid1; i >= l; i--) {
        if (A[i] == 1) {
          curmx++;
        } else {
          curmx = 0;
        }
        mx = max(mx, curmx);
        if (mx == 0) {
          psum[1]++;
          psum[len + 1]--;
        } else {
          psum[mx] += mx;
          psum[mx + 1] -= mx;
          psum[mx + 1]++;
          psum[len + 1]--;
        }
      }
    }
    if (mid2 < r) {
      int curmx = 0, mx = 0;
      for (int i = mid2; i < r; i++) {
        if (A[i] == 1) {
          curmx++;
        } else {
          curmx = 0;
        }
        mx = max(mx, curmx);
        if (mx == 0) {
          psum[1]++;
          psum[len + 1]--;
        } else {
          psum[mx] += mx;
          psum[mx + 1] -= mx;
          psum[mx + 1]++;
          psum[len + 1]--;
        }
      }
    }
  } else {
    vector<int> vec1, vec2;
    int curmx = 0, mx = 0;
    for (int i = last - 1; i >= l; i--) {
      if (A[i] == 1) {
        curmx++;
      } else {
        curmx = 0;
      }
      mx = max(mx, curmx);
      vec1.push_back(mx);
    }
    curmx = 0;
    mx = 0;
    for (int i = last + 1; i < r; i++) {
      if (A[i] == 1) {
        curmx++;
      } else {
        curmx = 0;
      }
      mx = max(mx, curmx);
      vec2.push_back(mx);
    }
    int pos = 0;
    for (int i : vec1) {
      while (pos < vec2.size() && vec2[pos] <= i) pos++;
      res += 1ll * (pos + 1) * i;
    }
    pos = 0;
    for (int i : vec2) {
      while (pos < vec1.size() && vec1[pos] < i) pos++;
      res += 1ll * (pos + 1) * i;
    }
    DAQ(l, last);
    DAQ(last + 1, r);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n;
  string s;
  cin >> n;
  cin >> s;
  for (int i = 0; i < n; i++) {
    A[i] = (s[i] == '1');
  }
  DAQ(0, n);
  for (int i = 1; i < maxn; i++) {
    psum[i] += psum[i - 1];
    res += 1ll * i * psum[i];
  }
  cout << res;
  return 0;
}
