#include <bits/stdc++.h>
const int inf = 0x3f3f3f3f;
using namespace std;
int yh() {
  int ret = 0;
  bool f = 0;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == EOF) return -1;
    if (c == '-') f = 1;
    c = getchar();
  }
  while (isdigit(c)) ret = (ret << 3) + (ret << 1) + (c ^ 48), c = getchar();
  return f ? -ret : ret;
}
template <typename T>
void yh(T& ret) {
  ret = 0;
  bool f = 0;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == EOF) return ret = -1, void();
    if (c == '-') f = 1;
    c = getchar();
  }
  while (isdigit(c)) ret = (ret << 3) + (ret << 1) + (c ^ 48), c = getchar();
  ret = f ? -ret : ret;
}
const int maxn = 5e5 + 5;
int s[maxn];
int h[maxn];
int lpos[maxn];
int main() {
  int n = yh();
  for (register int i = (1); i <= (n); i++) {
    char c = getchar();
    while (!isdigit(c)) c = getchar();
    s[i] = c - 48;
  }
  long long tot = 0;
  long long cur = 0;
  for (int i = 1; i <= n; i++) {
    if (s[i] == 1) {
      int l = i, r = i;
      while (s[r + 1] == 1) r++;
      for (int j = l, k = 1; j <= r; j++, k++) {
        cur += j - lpos[k];
        tot += cur;
        lpos[k] = r - k + 1;
      }
      i = r;
    } else
      tot += cur;
  }
  cout << tot << '\n';
  return 0;
}
