#include <bits/stdc++.h>
using namespace std;
int arr[500001];
int last[500002];
long long int cnt = 0;
long long int ans = 0;
int main() {
  int n;
  while (cin >> n) {
    for (int i = 1; i <= n; i++) {
      char t;
      cin >> t;
      arr[i] = int(t) - 48;
    }
    for (int i = 0; i <= n; i++) {
      last[i] = 0;
    }
    cnt = 0;
    ans = 0;
    for (int i = 1; i <= n; i++) {
      if (arr[i] == 0) {
        ans = ans + cnt;
      } else {
        int l = i;
        int r = i;
        while (r < n && arr[r + 1] == 1) {
          r++;
        }
        for (int j = 1; j <= (r - l + 1); j++) {
          cnt = cnt + l + j - 1 - last[j];
          last[j] = r - j + 1;
          ans = ans + cnt;
        }
        i = r;
      }
    }
    cout << ans << "\n";
  }
}
