#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
template <typename T>
using pair2 = pair<T, T>;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using si = set<int>;
using sl = set<ll>;
template <typename T, class F = function<T(T, T)>>
struct DO {
  vector<T> mas;
  vi pos;
  F upd, chg;
  T inf;
  int siz;
  DO(const int &n, const T &neu, const F &u, const F &c)
      : siz(n), upd(u), chg(c), inf(neu) {
    mas.assign(n * 4 + 5, neu);
    pos.resize(n * 4 + 5);
  }
  void modify(int i, int tl, int tr, int id, T value) {
    if (tl + 1 == tr) {
      mas[i] = chg(mas[i], value);
      pos[i] = tl;
      return;
    }
    int mid = (tl + tr) / 2;
    if (id < mid) {
      modify(i * 2, tl, mid, id, value);
    } else {
      modify(i * 2 + 1, mid, tr, id, value);
    }
    mas[i] = upd(mas[i * 2], mas[i * 2 + 1]);
    if (mas[i] == mas[i * 2 + 1]) {
      pos[i] = pos[i * 2 + 1];
    }
  }
  void modify(int point, T value) { modify(1, 0, siz, point, value); }
  T getValue(int i, int tl, int tr, int l, int r) {
    if (l <= tl && tr <= r) {
      return mas[i];
    }
    if (tl >= r || l >= tr) {
      return inf;
    }
    int mid = (tl + tr) / 2;
    return upd(getValue(i * 2, tl, mid, l, r),
               getValue(i * 2 + 1, mid, tr, l, r));
  }
  T getValue(int l, int r) { return getValue(1, 0, siz, l, r + 1); }
  pair<T, int> getValueWithIndex(int i, int tl, int tr, int l, int r) {
    if (l <= tl && tr <= r) {
      return {mas[i], pos[i]};
    }
    if (tl >= r || l >= tr) return {inf, inf};
    int mid = (tl + tr) / 2;
    auto le = getValueWithIndex(i * 2, tl, mid, l, r);
    auto re = getValueWithIndex(i * 2 + 1, mid, tr, l, r);
    if (upd(le.first, re.first) == le.first) return le;
    return re;
  }
  pair<T, int> getValueWithIndex(int l, int r) {
    return getValueWithIndex(1, 0, siz, l, r + 1);
  }
};
int n;
string s;
ll res;
const int N = 500005;
ll t[4 * N], equ[4 * N];
void push(int i, int tl, int tr) {
  if (equ[i] == -1) return;
  t[i] = (tr - tl) * equ[i];
  if (tl + 1 != tr) {
    equ[i * 2] = equ[i * 2 + 1] = equ[i];
  }
  equ[i] = -1;
}
void modify(int i, int tl, int tr, int l, int r, int v) {
  push(i, tl, tr);
  if (l <= tl && tr <= r) {
    equ[i] = v;
    push(i, tl, tr);
    return;
  }
  if (tl >= r || l >= tr) return;
  int m = (tl + tr) / 2;
  modify(i * 2, tl, m, l, r, v);
  modify(i * 2 + 1, m, tr, l, r, v);
  t[i] = t[i * 2] + t[i * 2 + 1];
}
ll get(int i, int tl, int tr, int l, int r) {
  push(i, tl, tr);
  if (l <= tl && tr <= r) return t[i];
  if (tl >= r || l >= tr) return 0;
  int m = (tl + tr) / 2;
  return get(i * 2, tl, m, l, r) + get(i * 2 + 1, m, tr, l, r);
}
void solve() {
  fill(equ, equ + 4 * N, -1);
  cin >> s >> s;
  n = (int)s.size();
  ll res = 0;
  ll curLen = 0;
  ll curAdd = 0;
  DO<int> posTree(
      n + 2, 2e9, [](int a, int b) { return min(a, b); },
      [](int a, int b) { return min(a, b); });
  for (int i = n - 1; i >= 0; --i) {
    if (s[i] == '0') {
      for (int j = i + 1; j <= i + curLen; ++j)
        modify(1, 0, n, j, j + 1, j - (i + 1) + 1);
      if (curLen) {
        posTree.modify(curLen, i + 1);
      }
      curLen = 0;
      curAdd = 0;
      res += get(1, 0, n, i + 1, n);
    } else {
      curLen++;
      curAdd += curLen;
      auto p = posTree.getValueWithIndex(curLen + 1, n + 2);
      int nxt;
      if (p.second == 2e9) {
        nxt = n;
      } else {
        nxt = p.first + curLen - 1;
      }
      modify(1, 0, n, i + curLen, nxt, curLen);
      res += curAdd + get(1, 0, n, i + curLen, n);
    }
  }
  cout << res << "\n";
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.setf(ios::fixed);
  cout.precision(10);
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
