#include <bits/stdc++.h>
using namespace std;
long long hist[(int)(5e5 + 10)], res = 0, cur = 0;
int n, s[(int)(5e5 + 10)];
string str;
int main() {
  ios::sync_with_stdio(0);
  cin >> n;
  cin >> str;
  str = ' ' + str;
  for (int i = 1; i <= n; i++) s[i] = str[i] - '0';
  for (int i = 1; i <= n; i++) {
    if (s[i] == 0)
      res += cur;
    else {
      int l = i, r = i;
      while (r < n && s[r + 1] == 1) r++;
      for (int x = 1; x <= r - l + 1; x++) {
        cur += (l + x - 1) - hist[x];
        res += cur;
        hist[x] = r - x + 1;
      }
      i = r;
    }
  }
  cout << res << endl;
}
