#include <bits/stdc++.h>
using namespace std;
template <class T>
bool uin(T &a, T b) {
  return a > b ? (a = b, true) : false;
}
template <class T>
bool uax(T &a, T b) {
  return a < b ? (a = b, true) : false;
}
const int N = 500010;
int hist[N], s[N];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.precision(10);
  cout << fixed;
  int n;
  cin >> n;
  long long cur = 0, ans = 0;
  for (int i = 1; i <= (int)(n); ++i) {
    char x;
    cin >> x;
    s[i] = x - '0';
  }
  for (int i = 1; i <= (int)(n); ++i) {
    if (s[i] == 0)
      ans += cur;
    else {
      int l = i, r = i;
      for (; r <= n; ++r) {
        if (s[r] != 1) {
          --r;
          break;
        }
      }
      if (r > n) r--;
      for (int j = (int)(l); j <= (int)(r); ++j) {
        int x = j - l + 1;
        cur += j - hist[x];
        ans += cur;
        hist[x] = r - x + 1;
      }
      i = r;
    }
  }
  cout << ans << '\n';
  return 0;
}
