#include <bits/stdc++.h>
using namespace std;
template <typename P, typename Q>
P CeilDiv(P _dividend, Q _divider) {
  return static_cast<P>((_dividend - 1LL + _divider) / _divider);
}
template <typename Tp>
inline void outarr(Tp _begin, Tp _end, const char* _delim = " ") {
  for (Tp current = _begin; current != _end; ++current) {
    std::cout << *current << _delim;
  }
  std::cout << '\n';
}
using ll = long long;
using pii = std::pair<int, int>;
constexpr int INF = 0x3f3f3f3f;
constexpr int MOD = static_cast<const int>(1e9 + 7);
class SegmentTree {
 public:
  struct Node {
    ll Sum = 0;
    int Max = 0;
    int Push = 0;
    int PushMax = 0;
  };
  explicit SegmentTree(int n) : tree(n << 2), size(n) {}
  void Update(int l, int r, int x) { UpdateImpl(0, 0, size - 1, l, r, x); }
  void Set(int l, int r, int x) { SetImpl(0, 0, size - 1, l, r, x); }
  ll Sum(int l, int r) { return SumImpl(0, 0, size - 1, l, r); }
  int Find(int val) { return FindImpl(0, 0, size - 1, val); }

 private:
  int Left(int v) const { return (v << 1) + 1; }
  int Right(int v) const { return (v << 1) + 2; }
  void UpdateVertex(int v) {
    tree[v].Sum = tree[Left(v)].Sum + tree[Right(v)].Sum;
    tree[v].Max = max(tree[Left(v)].Max, tree[Right(v)].Max);
  }
  void Push(int v, int tl, int tr) {
    const int mid = (tl + tr) >> 1;
    if (tree[v].PushMax != 0) {
      tree[Left(v)].Sum = 1LL * tree[v].PushMax * (mid - tl + 1);
      tree[Left(v)].Max = tree[v].PushMax;
      tree[Left(v)].Push = 0;
      tree[Left(v)].PushMax = tree[v].PushMax;
      tree[Right(v)].Sum = 1LL * tree[v].PushMax * (tr - mid);
      tree[Right(v)].Max = tree[v].PushMax;
      tree[Right(v)].Push = 0;
      tree[Right(v)].PushMax = tree[v].PushMax;
      tree[v].PushMax = 0;
    }
    if (tree[v].Push != 0) {
      tree[Left(v)].Sum += 1LL * tree[v].Push * (mid - tl + 1);
      tree[Left(v)].Max += tree[v].Push;
      tree[Left(v)].Push += tree[v].Push;
      tree[Right(v)].Sum += 1LL * tree[v].Push * (tr - mid);
      tree[Right(v)].Max += tree[v].Push;
      tree[Right(v)].Push += tree[v].Push;
      tree[v].Push = 0;
    }
  }
  void UpdateImpl(int v, int tl, int tr, int l, int r, int x) {
    if (l > r) {
      return;
    }
    if (tl == l && r == tr) {
      tree[v].Sum += x * (tr - tl + 1);
      tree[v].Max += x;
      tree[v].Push += x;
      return;
    }
    Push(v, tl, tr);
    const int mid = (tl + tr) >> 1;
    UpdateImpl(Left(v), tl, mid, l, min(mid, r), x);
    UpdateImpl(Right(v), mid + 1, tr, max(l, mid + 1), r, x);
    UpdateVertex(v);
  }
  void SetImpl(int v, int tl, int tr, int l, int r, int x) {
    if (l > r) {
      return;
    }
    if (tl == l && r == tr) {
      tree[v].Sum = 1LL * x * (tr - tl + 1);
      tree[v].Max = x;
      tree[v].Push = 0;
      tree[v].PushMax = x;
      return;
    }
    Push(v, tl, tr);
    const int mid = (tl + tr) >> 1;
    SetImpl(Left(v), tl, mid, l, min(mid, r), x);
    SetImpl(Right(v), mid + 1, tr, max(l, mid + 1), r, x);
    UpdateVertex(v);
  }
  ll SumImpl(int v, int tl, int tr, int l, int r) {
    if (l > r) {
      return 0;
    }
    if (tl == l && r == tr) {
      return tree[v].Sum;
    }
    Push(v, tl, tr);
    const int mid = (tl + tr) >> 1;
    return SumImpl(Left(v), tl, mid, l, min(mid, r)) +
           SumImpl(Right(v), mid + 1, tr, max(l, mid + 1), r);
  }
  int FindImpl(int v, int tl, int tr, int val) {
    if (tl == tr) {
      return tl;
    }
    Push(v, tl, tr);
    const int mid = (tl + tr) >> 1;
    if (tree[Left(v)].Max >= val) {
      return FindImpl(Left(v), tl, mid, val);
    }
    return FindImpl(Right(v), mid + 1, tr, val);
  }
  vector<Node> tree;
  const int size;
};
int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int n;
  string s;
  cin >> n >> s;
  SegmentTree st(n);
  ll ans = 0;
  ll pre = 0;
  int cur = 0;
  for (int i = n - 1; i >= 0; --i) {
    if (s[i] == '0') {
      ans += pre;
      cur = 0;
      continue;
    }
    ++cur;
    st.Set(i + cur, st.Find(cur), cur);
    st.Update(i, i + cur - 1, 1);
    pre = st.Sum(i, n - 1);
    ans += pre;
  }
  cout << ans << "\n";
  return 0;
}
