#include <bits/stdc++.h>
using namespace std;
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
using ll = long long;
using ld = long double;
const int p = 998244353;
int mul(int a, int b) { return (1LL * a * b) % p; }
int add(int a, int b) {
  int s = (a + b);
  if (s >= p) s -= p;
  return s;
}
int sub(int a, int b) {
  int s = (a + p - b);
  if (s >= p) s -= p;
  return s;
}
int po(int a, int deg) {
  if (deg == 0) return 1;
  if (deg % 2 == 1) return mul(a, po(a, deg - 1));
  int t = po(a, deg / 2);
  return mul(t, t);
}
int inv(int n) { return po(n, p - 2); }
mt19937 rnd(time(0));
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);
  int n;
  cin >> n;
  string s;
  cin >> s;
  map<int, int> cnt;
  ll cursum = 0;
  ll tot = 0;
  int curlen = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      for (int j = 0; j <= curlen; j++) cnt[j]++;
      tot += cursum;
      cursum += 1ll * curlen * (curlen + 1) / 2;
      for (int j = 1; j <= curlen; j++) tot += 1ll * j * (curlen + 1 - j);
      for (int j = 1; j <= curlen; j++) tot += j;
      curlen = 0;
    } else {
      cnt[curlen + 1] += cnt[curlen];
      cursum += cnt[curlen];
      cnt[curlen] = 0;
      curlen++;
      tot += cursum;
    }
  }
  if (curlen > 0) {
    for (int j = 1; j <= curlen; j++) tot += 1ll * j * (curlen + 1 - j);
  }
  cout << tot;
}
