#include <bits/stdc++.h>
using namespace std;
long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  for (; ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-') f = -1;
  for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
  return x * f;
}
void write(long long x) {
  if (x < 0)
    putchar('-'), write(-x);
  else {
    if (x >= 10) write(x / 10);
    putchar(x % 10 + '0');
  }
}
const long long N = 500010;
char s[N];
long long v[N], a[N], sum[N], n, m, ans, val;
int main() {
  m = read();
  scanf("%s", s + 1);
  for (long long i = (1); i <= (m); ++i)
    if (s[i] != s[i - 1])
      v[++n] = s[i] - '0', a[n] = 1;
    else
      ++a[n];
  for (long long i = (1); i <= (n); ++i)
    if (v[i] == 0)
      ans += a[i] * val, sum[0] += a[i];
    else {
      for (long long j = (1); j <= (a[i]); ++j) {
        val += sum[j - 1];
        sum[j] += sum[j - 1];
        sum[j - 1] = 0;
        ans += val;
      }
      for (long long j = (1); j <= (a[i]); ++j) {
        ++sum[j];
        val += j;
        ans += j * (a[i] - j + 1);
      }
    }
  cout << ans << endl;
}
