#include <bits/stdc++.h>
int n, w[500005];
char s[500005];
long long ans, sum;
std::stack<int> stk;
void del() {
  int u = stk.top();
  stk.pop();
  sum -= (long long)w[u] * (u - stk.top());
}
void ins(int x) {
  if (x) sum += (long long)w[x] * (x - stk.top());
  stk.push(x);
}
long long calc(int x) { return (long long)x * (x + 1) / 2; }
void push(int x) {
  while (w[x] > w[stk.top()]) del();
}
long long get(int x, int y) { return (long long)(x - stk.top()) * y; }
int main() {
  scanf("%d", &n);
  scanf("%s", s + 1);
  s[0] = 'p';
  w[0] = 1e9;
  stk.push(0);
  int begin = 0;
  for (int i = 1; i <= n; ++i) {
    if (s[i] == s[i - 1]) {
      if (s[i] == '1') {
        w[begin]++;
        push(begin);
        ans += sum + calc(i - begin + 1) + get(begin - 1, i - begin + 1);
      } else {
        ans += sum;
      }
    } else if (i != 1) {
      if (s[begin] == '1') {
        int p = w[begin];
        for (int x = begin; x < i; ++x) {
          w[x] = p;
          p--;
        }
        for (int x = begin; x < i; ++x) ins(x);
      }
      begin = i;
      w[begin] = (s[i] == '1');
      ans += sum + get(i, w[i]);
    } else if (i == 1) {
      w[i] = (s[i] == '1');
      begin = i;
      ans += (s[i] == '1');
    }
  }
  printf("%lld", ans);
  return 0;
}
