#include <bits/stdc++.h>
using namespace std;
long long dp[1000002], sum = 0;
int32_t main() {
  long long n;
  cin >> n;
  long long sumsum = 0, akt = 0, pop = 0;
  for (long long i = 0; i < n; i++) {
    char c;
    cin >> c;
    if (c == '1') {
      sum += dp[akt] + akt;
      dp[akt + 1] += dp[akt];
      if (akt == 0) {
        dp[akt + 1]++;
        sum += akt + 1;
      }
      dp[akt] = 1;
      sumsum += sum;
      pop = 1;
      akt++;
    } else {
      if (pop == 1) {
        dp[0] = 0;
      }
      dp[0]++;
      sumsum += sum;
      akt = 0;
      pop = 0;
    }
  }
  cout << sumsum << "\n";
};
