#include <bits/stdc++.h>
using namespace std;
template <class T>
struct lazy_seg {
 public:
  vector<T> st, lazy;
  lazy_seg(int n) { st.resize(4 * n + 1, 0), lazy.resize(4 * n + 1, 0); }
  lazy_seg() = default;
  void push(int l, int r, int curr) {
    if (lazy[curr] != 0) {
      st[curr] += lazy[curr] * (r - l + 1);
      if (l != r)
        lazy[2 * curr] += lazy[curr], lazy[2 * curr + 1] += lazy[curr];
      lazy[curr] = 0;
    }
  }
  void pull(int l, int r, int curr) {
    push(l, r, curr);
    int mid = l + (r - l) / 2;
    if (l != r) push(l, mid, 2 * curr), push(mid + 1, r, 2 * curr + 1);
    st[curr] = (st[2 * curr] + st[2 * curr + 1]);
  }
  void upd(int x, int y, T v, int l, int r, int curr) {
    int mid = (l + r) / 2;
    push(l, r, curr);
    if (l == x && r == y) {
      lazy[curr] += v;
      push(l, r, curr);
      return;
    }
    if (y <= mid)
      upd(x, y, v, l, mid, 2 * curr);
    else if (x > mid)
      upd(x, y, v, mid + 1, r, 2 * curr + 1);
    else
      upd(x, mid, v, l, mid, 2 * curr),
          upd(mid + 1, y, v, mid + 1, r, 2 * curr + 1);
    pull(l, r, curr);
  }
  T qry(int x, int y, int l, int r, int curr) {
    int mid = (l + r) / 2;
    push(l, r, curr);
    if (l == x && r == y) return st[curr];
    if (y <= mid)
      return qry(x, y, l, mid, 2 * curr);
    else if (x > mid)
      return qry(x, y, mid + 1, r, 2 * curr + 1);
    return (qry(x, mid, l, mid, 2 * curr) +
            qry(mid + 1, y, mid + 1, r, 2 * curr + 1));
  }
  void build(int l, int r, int curr) {
    int mid = (l + r) / 2;
    if (l == r) {
      st[curr] = 0;
      return;
    }
    build(l, mid, 2 * curr);
    build(mid + 1, r, 2 * curr + 1);
    st[curr] = (st[2 * curr] + st[2 * curr + 1]);
  }
};
const int N = 500005;
int n;
string s;
lazy_seg<long long> f(N);
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  cin >> n >> s;
  long long ans = 0;
  int curl = 0;
  bool has = false;
  stack<pair<int, int> > opt_range;
  opt_range.push({-1000000000, -1});
  for (int i = 0; i < n; i++) {
    long long fa = ans;
    if (s[i] == '0') {
      if (has) {
        opt_range.push({curl, i - 1});
      }
      ans += f.qry(0, i, 0, n - 1, 1);
      has = false;
    } else {
      if (!has) {
        has = 1;
        curl = i;
      }
      if (opt_range.top().second != -1) {
        auto u = opt_range.top();
        f.upd(u.second + 1, i, +1, 0, n - 1, 1);
        u.second--;
        if (u.first > u.second) {
          opt_range.pop();
        } else {
          opt_range.pop();
          opt_range.push(u);
        }
      } else {
        f.upd(0, i, 1, 0, n - 1, 1);
      }
      ans += f.qry(0, i, 0, n - 1, 1);
    }
  }
  printf("%lld\n", ans);
}
