#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 15;
char s[N];
long long dp[N];
int mlen[N];
void solve() {
  int n;
  scanf("%d %s ", &n, s + 1);
  stack<int> st;
  st.push(0);
  long long ret = 0;
  for (int i = 1; i <= n;) {
    if (s[i] == '0') {
      ret += dp[st.top()];
      ++i;
    } else {
      int j;
      for (j = i; j <= n; ++j) {
        if (s[j] == '0') break;
        int len = j - i + 1;
        dp[j] += (long long)len * (len + 1) / 2;
        while (true) {
          if (st.top() == 0) {
            dp[j] += (long long)len * (i - 1);
            break;
          } else {
            if (mlen[st.top()] <= len) {
              st.pop();
            } else {
              int ind = st.top();
              dp[j] += dp[ind] + (long long)len * (len - 1) / 2 +
                       (long long)len * (i - 1 - ind);
              break;
            }
          }
        }
        ret += dp[j];
      }
      st.push(j - 1);
      mlen[j - 1] = j - i;
      i = j;
    }
  }
  printf("%lld\n", ret);
}
int main() {
  solve();
  return 0;
}
