#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, S[500010];
  scanf("%d", &N);
  for (int i = 0; i < N; i++) scanf("%1d", S + i);
  using ppp = pair<long long, pair<int, int>>;
  long long ans = 0, sum = 0;
  stack<ppp> st;
  auto push = [&](long long x, int l, int r) {
    if (st.empty() || st.top().first != x) {
      st.push({x, {l, r}});
      sum += x * (r - l);
      return;
    }
    auto p = st.top();
    st.pop();
    assert(p.second.second == l);
    sum += x * (r - l);
    p.second.second = r;
    st.push(p);
  };
  auto pop = [&]() {
    auto p = st.top();
    st.pop();
    sum -= p.first * (p.second.second - p.second.first);
    return p;
  };
  int counter = 0;
  for (int i = 0; i < N; i++) {
    if (S[i] == 0) {
      for (int j = 0; j < counter; j++) {
        ;
        ;
        push(counter - j, i - counter + j, i - counter + j + 1);
      }
      push(0, i, i + 1);
      counter = 0;
    } else {
      counter++;
      while (st.size() && st.top().first < counter) {
        auto p = pop();
        push(p.first + 1, p.second.first, p.second.second);
      }
    }
    long long T = (long long)counter * (counter + 1) / 2;
    ans += T + sum;
    ;
    ;
  }
  printf("%lld\n", ans);
  return 0;
}
