#include <bits/stdc++.h>
using namespace std;
void test_case() {
  int n;
  string s;
  cin >> n >> s;
  vector<int> cnt(n), last(n + 1, -1), next(n);
  int tmp = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      cnt[i] = ++tmp;
      if (last[tmp] != -1) next[last[tmp]] = i;
      last[tmp] = i;
    } else
      cnt[i] = tmp = 0;
  }
  for (int i = 1; i <= n; i++)
    if (last[i] != -1) next[last[i]] = n;
  long long total = 0;
  int mx = 0;
  for (int i = 0; i < n; i++) {
    mx = max(mx, cnt[i]);
    total += mx;
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    if (cnt[i] == 0)
      ans += total;
    else {
      for (; i + 1 < n && cnt[i + 1] != 0; i++)
        ;
      for (int j = 0; j < cnt[i]; j++) {
        ans += total;
        total -= next[i - j] - i + cnt[i] - j - 1;
      }
    }
  }
  cout << ans << '\n';
}
int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int Case = 1;
  for (int i = 0; i < Case; i++) {
    test_case();
  }
  return 0;
}
