#include <bits/stdc++.h>
using namespace std;
char s[500005];
long long dp[500005];
int main() {
  int n;
  scanf("%d", &n);
  scanf("%s", s + 1);
  long long ans = 0;
  stack<pair<int, int> > st;
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (s[i] == '0')
      dp[i] = dp[i - 1], cnt = 0;
    else {
      cnt++;
      while (!st.empty() && st.top().first <= cnt) st.pop();
      dp[i] += cnt * 1ll * (cnt + 1) / 2;
      if (st.empty()) {
        dp[i] += (i - cnt) * 1ll * cnt;
      } else {
        dp[i] -= cnt * 1ll * (cnt + 1) / 2;
        dp[i] += cnt * 1ll * cnt;
        dp[i] += dp[st.top().second] + (i - st.top().second - cnt) * 1ll * cnt;
      }
      st.push(make_pair(cnt, i));
    }
    ans += dp[i];
  }
  printf("%lld\n", ans);
}
