#include <bits/stdc++.h>
char s[500005];
int f[500005];
int main() {
  int n, cur = 0;
  long long ans = 0, sum = 0;
  scanf("%d%s", &n, s + 1);
  for (register int i = 1; i <= n; ++i) f[i] = n + 1;
  for (register int i = n; i; --i) {
    if (s[i] == '1') {
      ++cur;
      sum += f[cur] - i;
    } else {
      while (cur) {
        f[cur] = i + cur;
        --cur;
      }
    }
    ans += sum;
  }
  printf("%lld\n", ans);
  return 0;
}
