#include <bits/stdc++.h>
using namespace std;
int n;
char s[500010];
int pos[500010];
int main() {
  cin >> n;
  scanf("%s", s + 1);
  int n = strlen(s + 1);
  long long sum = 0;
  long long now = 0;
  for (int i = 1; i <= n; i++) {
    if (s[i] == '0') {
      sum += now;
    } else {
      int l = i;
      int r = i;
      while (r < n && s[r + 1] == '1') {
        r++;
      }
      for (int len = 1; len <= r - l + 1; len++) {
        now += l + len - 1 - pos[len];
        sum += now;
        pos[len] = r - len + 1;
      }
      i = r;
    }
  }
  cout << sum << endl;
  return 0;
}
