#include <bits/stdc++.h>
using namespace std;
int N;
string str;
int A[500005];
int B[500005];
int C[500005];
int now[500005];
vector<int> Q[500005];
struct {
  long long Get(int l, int r, int id) { return st[id] + ad[id] * (r - l + 1); }
  void up(int l, int r, int id) {
    int m = (l + r) / 2;
    st[id] = (Get(l, m, id * 2) + Get(m + 1, r, id * 2 + 1));
  }
  void down(int l, int r, int id) {
    long long val = ad[id];
    ad[id] = 0;
    st[id] += val * (r - l + 1);
    if (l == r) return;
    ad[id * 2] += val, ad[id * 2 + 1] += val;
  }
  void build(int l, int r, int id) {
    ad[id] = 0;
    if (l == r) {
      st[id] = B[l];
      return;
    }
    int m = (l + r) / 2;
    build(l, m, id * 2);
    build(m + 1, r, id * 2 + 1);
    up(l, r, id);
  }
  long long Sum(int L, int R, int l, int r, int id) {
    down(l, r, id);
    if (l >= L && r <= R) return st[id];
    if (l > R || r < L) return 0;
    int m = (l + r) / 2;
    return Sum(L, R, l, m, id * 2) + Sum(L, R, m + 1, r, id * 2 + 1);
  }
  void Add(int L, int R, long long x, int l, int r, int id) {
    down(l, r, id);
    if (l >= L && r <= R) {
      ad[id] += x;
      return;
    }
    if (l > R || r < L) return;
    int m = (l + r) / 2;
    Add(L, R, x, l, m, id * 2);
    Add(L, R, x, m + 1, r, id * 2 + 1);
    up(l, r, id);
  }
  long long st[500005 * 4], ad[500005 * 4];
} segt;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N >> str;
  if (str[0] == '1') {
    A[1] = B[1] = 1;
  }
  for (int i = 2; i <= N; i++) {
    if (str[i - 1] == '0') {
      A[i] = 0;
      B[i] = B[i - 1];
    } else {
      A[i] = A[i - 1] + 1;
      B[i] = max(B[i - 1], A[i]);
    }
  }
  if (str[N - 1] == '1') C[N] = 1;
  for (int i = N - 1; i >= 1; i--) {
    if (str[i - 1] == '1') {
      C[i] = C[i + 1] + 1;
    }
  }
  for (int i = 1; i <= N; i++) {
    if (A[i]) {
      Q[A[i]].push_back(i);
    }
  }
  segt.build(1, N, 1);
  long long ans = segt.Sum(1, N, 1, N, 1);
  for (int i = 2; i <= N; i++) {
    if (A[i - 1] == 0) {
      ans += segt.Sum(1, N, 1, N, 1);
      continue;
    }
    if (A[i - 1] == 1) {
      int len = C[i - 1];
      for (int j = 1; j <= len; j++) now[j]++;
    }
    int len = C[i];
    int nx = (now[len + 1] == (int)(Q[len + 1].size()))
                 ? N + 1
                 : Q[len + 1][now[len + 1]];
    segt.Add(i - 1, nx - 1, -1, 1, N, 1);
    ans += segt.Sum(1, N, 1, N, 1);
  }
  cout << ans << '\n';
  return 0;
}
