#include <bits/stdc++.h>
using namespace std;
long long tree[1025 * 1025], lazy[1025 * 1025];
long long maks[500003], arr[500003], dp[500002];
void push(int first, int second, int r) {
  tree[r] = lazy[r];
  if (first != second) {
    lazy[2 * r] = lazy[r];
    lazy[2 * r + 1] = lazy[r];
  }
  lazy[r] = 0;
}
void update(int first, int second, int a, int b, int r, long long v) {
  if (lazy[r]) push(a, b, r);
  if (a >= first and b <= second) {
    lazy[r] = v;
    return;
  }
  if (a > second or b < first) return;
  int md = (a + b) / 2;
  update(first, second, a, md, 2 * r, v);
  update(first, second, md + 1, b, 2 * r + 1, v);
  tree[r] = max(tree[2 * r], tree[2 * r + 1]);
}
long long query(int first, int second, int a, int b, int r) {
  if (a > second or b < first) return 0;
  if (a >= first and b <= second) {
    if (lazy[r]) push(a, b, r);
    return tree[r];
  }
  if (lazy[r]) push(a, b, r);
  int md = (a + b) / 2;
  return max(query(first, second, a, md, 2 * r),
             query(first, second, md + 1, b, 2 * r + 1));
}
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    char c;
    cin >> c;
    arr[i] = c - '0';
  }
  long long l = 0;
  long long dod;
  for (long long i = 1ll; i <= n; i++) {
    if (arr[i] == 0) {
      dp[i] = dp[i - 1];
      l = 0ll;
      continue;
    }
    l++;
    dod = min(i, i - query(l, l, 1, n, 1) + l - 1ll);
    update(1, l, 1, n, 1, i);
    dp[i] = dp[i - 1] + dod;
  }
  long long s = 0ll;
  for (int i = 1; i <= n; i++) {
    s += dp[i];
  }
  cout << s << endl;
  return 0;
}
