#include <bits/stdc++.h>
using namespace std;
const int M = 5e5 + 10;
int n, b[M];
string s;
vector<pair<int, int> > v;
long long ans;
long long get(int a, int b) { return (long long)(a + b) * (b - a + 1) / 2; }
void f(int i) {
  v.push_back({0, i});
  int last = 0;
  for (int j = 0; j < v.size() - 1; ++j) {
    ans += (long long)(v[j].second - last) * v[j].first;
    int t = v[j + 1].first;
    ans += get(v[j + 1].first, v[j].first);
    last = v[j].second + v[j].first - v[j + 1].first + 1;
  }
  v.pop_back();
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  cin >> s;
  int now = 0, pos = 0;
  for (int i = 0; i < n; ++i) {
    if (s[i] == '1') {
      if (!now) pos = i;
      ++now;
      while (v.size() && v.back().first <= now) v.pop_back();
      v.push_back({now, pos});
    } else {
      now = 0;
    }
    f(i);
  }
  cout << ans << '\n';
}
