#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 50;
char s[maxn];
vector<pair<int, int> > c;
int mx[maxn << 2];
void build(int rt, int l, int r) {
  if (l == r) {
    mx[rt] = c[l - 1].first;
    return;
  }
  build(rt << 1, l, ((l + r) >> 1));
  build(rt << 1 | 1, ((l + r) >> 1) + 1, r);
  mx[rt] = max(mx[rt << 1], mx[rt << 1 | 1]);
  return;
}
int qryl(int rt, int l, int r, int L, int R, int x) {
  if (r < L || l > R) return -1;
  if (mx[rt] < x) return -1;
  if (l == r) return l;
  if (L <= l && r <= R) {
    int ans = qryl(rt << 1 | 1, ((l + r) >> 1) + 1, r, L, R, x);
    if (ans == -1)
      return qryl(rt << 1, l, ((l + r) >> 1), L, R, x);
    else
      return ans;
  }
  int ans;
  if (R > ((l + r) >> 1)) {
    ans = qryl(rt << 1 | 1, ((l + r) >> 1) + 1, r, L, R, x);
    if (ans != -1) return ans;
  }
  if (L <= ((l + r) >> 1)) {
    ans = qryl(rt << 1, l, ((l + r) >> 1), L, R, x);
    if (ans != -1) return ans;
  }
  return -1;
}
int qryr(int rt, int l, int r, int L, int R, int x) {
  if (r < L || l > R) return -1;
  if (mx[rt] < x) return -1;
  if (l == r) return l;
  if (L <= l && r <= R) {
    int ans = qryr(rt << 1, l, ((l + r) >> 1), L, R, x);
    if (ans == -1) return qryr(rt << 1 | 1, ((l + r) >> 1) + 1, r, L, R, x);
    return ans;
  }
  int ans;
  if (L <= ((l + r) >> 1)) {
    ans = qryr(rt << 1, l, ((l + r) >> 1), L, R, x);
    if (ans != -1) return ans;
  }
  if (R > ((l + r) >> 1)) {
    ans = qryr(rt << 1 | 1, ((l + r) >> 1) + 1, r, L, R, x);
    if (ans != -1) return ans;
  }
  return -1;
}
int main() {
  int n;
  scanf("%d", &n);
  scanf("%s", s + 1);
  int cur = 0;
  for (int i = (1); i < (n + 2); ++i) {
    if (s[i] == '1') {
      cur++;
    } else {
      if (cur) c.push_back(pair<int, int>(cur, i - 1)), cur = 0;
    }
  }
  if (c.size() == 0) {
    printf("0\n");
    return 0;
  }
  build(1, 1, c.size());
  int num = c.size();
  long long ans = 0;
  for (int i = (0); i < (num); ++i) {
    int val = c[i].first;
    int rp = c[i].second;
    int lp = rp - val + 1;
    for (int x = 1; x <= val - 1; ++x) {
      ans += (val - x - 1) * (long long)x;
      int rd1, ld1, rd2, ld2;
      int pos = qryl(1, 1, num, 1, i, x + 1);
      if (pos == -1) {
        ans += lp * (long long)x;
        ld1 = 1;
      } else {
        rd1 = c[pos - 1].second;
        ld1 = rd1 - x + 1;
        ans += (lp - ld1 + 1) * (long long)x;
      }
      pos = qryr(1, 1, num, i + 2, num, x);
      if (pos == -1) {
        ans += (n - rp + 1) * (long long)x;
        rd2 = n;
      } else {
        rd2 = c[pos - 1].second;
        ld2 = rd2 - c[pos - 1].first + 1;
        rd2 = ld2 + x - 1 - 1;
        ans += (rd2 - rp + 1) * (long long)x;
      }
    }
    int pos1 = qryl(1, 1, num, 1, i, val + 1);
    int pos2 = qryr(1, 1, num, i + 2, num, val);
    int ld1, ld2, rd1, rd2;
    if (pos1 == -1) {
      ld1 = 1;
    } else {
      rd1 = c[pos1 - 1].second;
      ld1 = rd1 - val + 1;
    }
    if (pos2 == -1) {
      rd2 = n;
    } else {
      rd2 = c[pos2 - 1].second;
      ld2 = rd2 - c[pos2 - 1].first + 1;
      rd2 = ld2 + val - 1 - 1;
    }
    ans += (long long)(lp - ld1 + 1) * (rd2 - rp + 1) * (long long)val;
  }
  cout << ans << endl;
}
