#include <bits/stdc++.h>
using namespace std;
class Input {
 public:
  std::vector<long long> groups;
};
Input read_input(std::istream &in) {
  long long n;
  in >> n;
  std::string s;
  in >> s;
  long long cur_group = 0;
  Input out;
  auto cur_char = '0';
  for (auto c : s) {
    if (c != cur_char) {
      out.groups.push_back(cur_group);
      cur_group = 1;
      cur_char = (cur_char == '1' ? '0' : '1');
    } else
      ++cur_group;
  }
  out.groups.push_back(cur_group);
  return out;
}
long long solve(const Input &input) {
  std::vector<long long> sums;
  long long summand = 0;
  long long sum = 0;
  bool is_empty = true;
  for (auto cur_group : input.groups) {
    for (long long num = 1; num <= cur_group; ++num) {
      auto contrib = num;
      if (is_empty) contrib = 0;
      if (sums.size() < contrib + 1) sums.resize(contrib + 1);
      ++sums[contrib];
      summand += contrib;
      if (contrib > 0) {
        auto transferred =
            contrib == 1 ? sums[contrib - 1] : sums[contrib - 1] - 1;
        sums[contrib - 1] -= transferred;
        sums[contrib] += transferred;
        summand += transferred;
      }
      sum += summand;
    }
    is_empty = !is_empty;
  }
  return sum;
}
class Solution {
 public:
  void solve(std::istream &in, std::ostream &out) {
    out << ::solve(read_input(in)) << '\n';
  }
};
void solve(std::istream &in, std::ostream &out) {
  out << std::setprecision(12);
  Solution solution;
  solution.solve(in, out);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  istream &in = cin;
  ostream &out = cout;
  solve(in, out);
  return 0;
}
