#include <bits/stdc++.h>
using namespace std;
const long long INF = 1000000007;
const long long INFL = 1000000000000000007;
const long double INFS = 0.00000001;
const long long MOD = 1000000007;
const long double PI = 3.14159265;
const long long dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
const long long dir2[8][2] = {{1, 1},  {1, 0},  {1, -1}, {0, 1},
                              {0, -1}, {-1, 1}, {-1, 0}, {-1, -1}};
long long n;
char a[500001];
long long place[500001];
long long ans = 0;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long tot = 0;
  long long one = 0;
  long long i;
  cin >> n;
  for (i = 1; i <= n; i++) cin >> a[i];
  for (i = 1; i <= n; i++) {
    if (a[i] == '1') {
      one++;
      tot += one + place[one - 1];
      if (one > 1) tot--;
      place[one] += place[one - 1];
      if (one == 1)
        place[one - 1] = 0, place[1]++;
      else
        place[one - 1] = 1;
    } else
      one = 0, place[0]++;
    ans += tot;
  }
  cout << ans << endl;
  return 0;
}
