#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
const long long mod = 1e9 + 7;
template <typename T>
inline T gcd(T a, T b) {
  return !b ? a : gcd(b, a % b);
}
template <typename T>
inline T q_pow(T a, T x) {
  T ans = 1, tmp = a;
  while (x) {
    if (x & 1) (ans *= tmp) %= mod;
    (tmp *= tmp) %= mod;
    x >>= 1;
  }
  return ans;
}
template <typename T>
inline void re(T &N) {
  int f = 1;
  char c;
  while ((c = getchar()) < '0' || c > '9')
    if (c == '-') f = -1;
  N = c - '0';
  while ((c = getchar()) >= '0' && c <= '9') N = N * 10 + c - '0';
  N *= f;
}
int m, n, t = 1, st, en;
int a[N], b[N];
char s[N];
struct SegT {
  struct node {
    int fi, se;
    int cnt, tag;
    long long sum;
  } e[N << 2];
  inline void push_up(int p) {
    int l = ((p) << 1), r = ((p) << 1 | 1);
    e[p].sum = e[l].sum + e[r].sum;
    if (e[l].fi == e[r].fi) {
      e[p].fi = e[l].fi;
      e[p].cnt = e[l].cnt + e[r].cnt;
      e[p].se = min(e[l].se, e[r].se);
    } else {
      e[p].fi = min(e[l].fi, e[r].fi);
      e[p].cnt = e[l].fi == e[p].fi ? e[l].cnt : e[r].cnt;
      e[p].se = min({e[l].se, e[r].se, e[l].fi == e[p].fi ? e[r].fi : e[l].fi});
    }
  }
  void build(int p, int l, int r) {
    e[p].se = 1e9;
    e[p].cnt = 1;
    if (l == r) return;
    int mid = (l + r) >> 1;
    build(((p) << 1), l, mid);
    build(((p) << 1 | 1), mid + 1, r);
    push_up(p);
  }
  inline void push_down(int p) {
    int l = ((p) << 1), r = ((p) << 1 | 1);
    if (e[p].tag) {
      e[l].tag = e[r].tag = 1;
      if (e[l].fi < e[p].fi) {
        e[l].sum += 1ll * e[l].cnt * (e[p].fi - e[l].fi);
        e[l].fi = e[p].fi;
      }
      if (e[r].fi < e[p].fi) {
        e[r].sum += 1ll * e[r].cnt * (e[p].fi - e[r].fi);
        e[r].fi = e[p].fi;
      }
      e[p].tag = 0;
    }
  }
  void update(int p, int l, int r, int L, int R, int k) {
    if (e[p].fi >= k) return;
    if (L <= l && r <= R && e[p].se > k) {
      e[p].tag = 1;
      e[p].sum += 1ll * (k - e[p].fi) * e[p].cnt;
      e[p].fi = k;
      return;
    }
    push_down(p);
    int mid = (l + r) >> 1;
    if (L <= mid) update(((p) << 1), l, mid, L, R, k);
    if (mid < R) update(((p) << 1 | 1), mid + 1, r, L, R, k);
    push_up(p);
  }
  void kupdate(int p, int l, int r, int L, int R, int k) {
    if (L <= l && r <= R) {
      e[p].fi = e[p].sum = k;
      return;
    }
    push_down(p);
    int mid = (l + r) >> 1;
    if (L <= mid) kupdate(((p) << 1), l, mid, L, R, k);
    if (mid < R) kupdate(((p) << 1 | 1), mid + 1, r, L, R, k);
    push_up(p);
  }
  inline long long query() { return e[1].sum; }
} tr;
int main() {
  re(n);
  tr.build(1, 1, n);
  scanf("%s", s + 1);
  long long cnt = 0, ans = 0;
  for (int i = n; i >= 1; i--) {
    if (s[i] == '1')
      cnt++;
    else {
      for (int j = 1; j <= cnt; j++) tr.kupdate(1, 1, n, i + j, i + j, j);
      cnt = 0;
    }
    ans += (1 + cnt) * cnt / 2;
    if (i + cnt <= n) tr.update(1, 1, n, i + cnt, n, cnt);
    ans += tr.query();
  }
  printf("%lld\n", ans);
}
