#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
inline void chkmin(T1 &a, T2 b) {
  if (a > b) a = b;
}
template <typename T1, typename T2>
inline void chkmax(T1 &a, T2 b) {
  if (a < b) a = b;
}
using ll = long long;
using ld = long double;
const string FILENAME = "input";
const int MAXN = 500228;
int n;
string s;
ll cntmid = 0;
ll sumlenmid = 0;
ll sumborder = 0;
ll cnttot = 0;
ll sumtot = 0;
set<pair<int, int> > cur;
ll cursum = 0;
void delcon(pair<int, int> x, pair<int, int> y) {
  if (x.first < 0 && y.first >= n) {
    return;
  }
  if (x.first < 0) {
    cursum -= (ll)(y.second) * (y.second + 1) / 2;
    sumborder -= y.first;
  } else if (y.first >= n) {
    cursum -= (ll)(n - x.first - 1) * ((n - x.first - 1) + 1) / 2;
    sumborder -= n - x.second - 1;
  } else {
    cntmid--;
    cursum -= (ll)(y.second - x.first - 1) * ((y.second - x.first - 1) + 1) / 2;
    sumlenmid -= y.first - x.second - 1;
  }
}
void addcon(pair<int, int> x, pair<int, int> y) {
  if (x.first < 0 && y.first >= n) {
    cursum += (ll)n * (n + 1) / 2;
    return;
  }
  if (x.first < 0) {
    cursum += (ll)(y.second) * (y.second + 1) / 2;
    sumborder += y.first;
  } else if (y.first >= n) {
    cursum += (ll)(n - x.first - 1) * ((n - x.first - 1) + 1) / 2;
    sumborder += n - x.second - 1;
  } else {
    cursum += (ll)(y.second - x.first - 1) * ((y.second - x.first - 1) + 1) / 2;
    cntmid++;
    sumlenmid += y.first - x.second - 1;
  }
}
void add(pair<int, int> x) {
  sumtot += x.second - x.first + 1;
  cnttot++;
  auto d = cur.lower_bound(x);
  pair<int, int> r, l;
  if (d != cur.end()) {
    r = *d;
  }
  if (d != cur.begin()) {
    d--;
    l = *d;
  }
  delcon(l, r);
  addcon(l, x);
  addcon(x, r);
  cur.insert(x);
}
ll kek;
void del(pair<int, int> x) {
  ll len = x.second - x.first + 1;
  sumtot -= x.second - x.first + 1;
  cnttot--;
  cur.erase(x);
  auto d = cur.lower_bound(x);
  pair<int, int> r, l;
  if (d != cur.end()) {
    r = *d;
  }
  if (d != cur.begin()) {
    d--;
    l = *d;
  }
  ll dd = max(0LL, l.second - len + 2);
  ll d1 = min((ll)n - 1, r.first + len - 2);
  kek += (x.first - dd + 1) * (d1 - x.second + 1);
  addcon(l, r);
  delcon(l, x);
  delcon(x, r);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> s;
  vector<pair<int, int> > st;
  int last = -1;
  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      if (last != -1) {
        st.push_back(make_pair(last, i - 1));
      }
      last = -1;
    } else {
      if (last == -1) {
        last = i;
      }
    }
  }
  if (last != -1) {
    st.push_back(make_pair(last, n - 1));
  }
  if (st.empty()) {
    cout << 0 << '\n';
    return 0;
  }
  vector<pair<int, int> > res;
  for (int i = 0; i < (int)(st).size(); i++) {
    res.push_back(make_pair(st[i].second - st[i].first + 1, i));
  }
  cur.insert(make_pair(-1, -1));
  cur.insert(make_pair(n, n));
  for (int i = 0; i < (int)(st).size(); i++) {
    add(st[i]);
  }
  sort((res).begin(), (res).end());
  int uk = 0;
  ll ans = 0;
  for (int len = 1; len <= n; len++) {
    kek = 0;
    int cnt = 0;
    while (uk < (int)(res).size() && res[uk].first <= len) {
      del(st[res[uk].second]);
      cnt++;
      uk++;
    }
    if (uk == (int)(res).size()) {
      ans += kek * len;
      break;
    }
    ll tot = kek + (sumlenmid * 2 + cntmid * (2 * len - 1) + sumborder +
                    sumtot - cnttot * (len - 1));
    ans += tot * len;
  }
  cout << ans << '\n';
}
