#include <bits/stdc++.h>
using namespace std;
bool isrange(int second, int first, int n, int m) {
  if (0 <= second && second < n && 0 <= first && first < m) return true;
  return false;
}
int dy[4] = {1, 0, -1, 0}, dx[4] = {0, 1, 0, -1},
    ddy[8] = {1, 0, -1, 0, 1, 1, -1, -1}, ddx[8] = {0, 1, 0, -1, 1, -1, 1, -1};
const int MAX = 505050;
string a;
int n;
long long int ans;
int dp[MAX];
long long int ndp[MAX];
void dfs(int l, int r) {
  if (l == r) {
    if (a[l] == '1') ans++;
    return;
  }
  int mid = (l + r) / 2;
  dfs(l, mid);
  dfs(mid + 1, r);
  for (int e = 0; e <= (r - l) + 2; e++) {
    ndp[e] = 0;
    dp[e] = 0;
  }
  int lx = mid + 1, rx = mid;
  if (a[mid + 1] == '1') {
    rx = mid + 1;
    for (int e = mid; e >= l; e--) {
      if (a[e] == a[e + 1] && a[e] == '1')
        lx = e;
      else
        break;
    }
    for (int e = mid + 2; e <= r; e++) {
      if (a[e] == a[e - 1] && a[e] == '1')
        rx = e;
      else
        break;
    }
  }
  long long int diff = (rx - (mid + 1) + 1);
  long long int tot = 0, mm = 0;
  for (int e = rx + 1; e <= r; e++) {
    if (a[e] == '1') {
      tot++;
      mm = max(mm, tot);
    } else
      tot = 0;
    ndp[mm] += mm;
    dp[mm]++;
  }
  for (int e = (r - l + 1); e >= 0; e--) {
    dp[e] += dp[e + 1];
    ndp[e] += ndp[e + 1];
  }
  long long int vvv = 0;
  tot = 0;
  mm = 0;
  for (int e = mid; e >= l; e--) {
    if (a[e] == '1') {
      tot++;
      mm = max(mm, tot);
    } else
      tot = 0;
    int ss;
    if (e >= lx) {
      ss = mid - e + 1;
    } else {
      ss = (mid - lx + 1);
    }
    long long int vv = diff + ss;
    long long int kk = (r - rx);
    vvv += (kk - dp[max(mm, vv)]) * max(mm, vv);
    vvv += ndp[max(mm, vv)];
    if (ss >= mm) {
      vvv += (ss * diff) + ((diff * (diff + 1))) / 2;
    } else {
      if (vv >= mm) {
        long long int x1 = vv - mm;
        long long int x2 = diff - x1;
        vvv += x2 * mm;
        vvv += mm * x1 + (x1 * (x1 + 1)) / 2;
      } else {
        vvv += mm * diff;
      }
    }
  }
  ans += vvv;
}
int main(void) {
  cin >> n >> a;
  dfs(0, n - 1);
  printf("%lld", ans);
  return 0;
}
