#include <bits/stdc++.h>
using namespace std;
const long long maxn = 500005;
long long n, m, r;
long long a[maxn], dp[maxn];
inline long long read() {
  long long ret = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -f;
    ch = getchar();
  }
  while (isdigit(ch)) {
    ret = ret * 10 + ch - '0';
    ch = getchar();
  }
  return ret * f;
}
void scan() {
  n = read();
  char ch = getchar();
  long long k = 0;
  while (!isdigit(ch)) ch = getchar();
  while (isdigit(ch)) {
    a[++k] = ch - '0';
    ch = getchar();
  }
}
void solve() {
  long long now = 0, sum = 0, ans = 0;
  for (long long k = 1; k <= n; k++) {
    if (a[k]) {
      sum += now++;
      long long w = k - now + 1;
      sum += w - dp[now];
      dp[now] = w;
    } else if (now)
      while (--now) dp[now] = dp[now + 1] + 1;
    ans += sum;
  }
  printf("%lld\n", ans);
}
signed main() {
  scan();
  solve();
  return 0;
}
