#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const int N = 1e5 + 100;
const int maxn = 1e6;
const int MAXL = sqrt(1e7) + 1;
inline int read() {
  char c = getchar();
  int x = 0, f = 1;
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  return x * f;
}
int n, f[5 * N], cnt = 0;
char s[5 * N];
void solve() {
  scanf("%d", &n);
  scanf("%s", s + 1);
  long long ans = 0, sum = 0;
  for (int i = 1; i <= n; i++) f[i] = n + 1;
  for (int i = n; i >= 1; i--) {
    if (s[i] == '1') {
      cnt++;
      sum += f[cnt] - i;
    } else {
      while (cnt) {
        f[cnt] = i + cnt;
        cnt--;
      }
    }
    ans += sum;
  }
  printf("%lld\n", ans);
}
int main() {
  int T;
  int tt = 0;
  { solve(); }
  return 0;
}
