#include <bits/stdc++.h>
using namespace std;
const long long Nmax = 5e5 + 100;
long long c[Nmax];
void solve() {
  long long n;
  cin >> n;
  string s;
  cin >> s;
  for (long long i = (0); i < (n + 1); ++i) c[i] = -1;
  long long sum = 0, ans = 0;
  long long lr = -1;
  for (long long i = (0); i < (n); ++i) {
    if (s[i] == '1') {
      if (lr == -1) lr = i;
      sum += i - c[i - lr + 1];
    } else {
      if (lr != -1) {
        for (long long j = (1); j < (i - lr + 1); ++j) c[j] = i - j;
        lr = -1;
      }
    }
    ans += sum;
  }
  cout << ans << "\n";
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  long long t;
  t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
