#include <bits/stdc++.h>
using namespace std;
const int maxn = 500005;
int his[maxn];
int s[maxn];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  int i, j, k, l, n;
  cin >> n;
  string s1;
  cin >> s1;
  for (i = 1; i <= n; i++) {
    s[i] = s1[i - 1] - '0';
  }
  long long cur = 0;
  long long tot = 0;
  for (i = 1; i <= n; i++) {
    if (s[i] == 0)
      tot += cur;
    else {
      int l = i;
      int r = i;
      while (r <= n && s[r] == 1) {
        r++;
      }
      r--;
      for (int x = 1; x <= r - l + 1; x++) {
        cur += (l + x - 1 - his[x]);
        tot += cur;
        his[x] = r - x + 1;
      }
      i = r;
    }
  }
  cout << tot;
}
