#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
const long long MAX = 1000000000;
const long long LLONG = 9223372036854775807;
const int N = 100000;
const double EPS = 0.00000001;
const int sqsz = 5000;
template <typename T>
inline int sgn(const T& val) {
  return (T(0) < val) - (val < T(0));
}
inline unsigned long long hsh(int x, int y) {
  return (((unsigned long long)x) << 32) + y;
}
template <typename T>
inline T gcd(T a, T b) {
  while (b) {
    a %= b;
    swap(a, b);
  }
  return a;
}
inline void fastio() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
int main() {
  int n, ci = 0, l = 0;
  long long res = 0, lsum = 0;
  string s;
  cin >> n >> s;
  vector<pair<int, int>> lens;
  for (int i = 0; i < n; i++) {
    if (s[i] - '0') {
      lsum = 0;
      if (!l) {
        l = 1;
        if (!lens.size() || l < lens[lens.size() - 1].first)
          lens.push_back({1, i});
        else
          lens[lens.size() - 1].second = i;
      } else {
        l++;
        if (lens.size() > 1 && l >= lens[lens.size() - 2].first) {
          lens.pop_back();
        }
        lens[lens.size() - 1].second = i - l + 1;
        lens[lens.size() - 1].first = l;
      }
      ci = lens.size() - 1;
      int lle = 0, lp = i;
      for (int j = ci; j >= 0; j--) {
        lsum +=
            lle * ((long long)lp - (lens[j].first + lens[j].second - lle + 1));
        lsum += (((long long)lle + lens[j].first) *
                 ((long long)lens[j].first - lle + 1)) /
                2;
        lle = lens[j].first;
        lp = lens[j].second;
      }
      lsum += (long long)lle * (long long)lp;
      res += lsum;
    } else {
      res += lsum;
      l = 0;
    }
  }
  printf("%lli\n", res);
}
