#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

using ordered_set = tree<
    pair<int, int>,
    null_type,
    less<pair<int, int>>,
    rb_tree_tag,
    tree_order_statistics_node_update
>;

int main() {
    int n;
    string s;
    cin >> n >> s;

    ordered_set X;

    X.insert({0, 0});

    int prefix = 0;
    long long ans = 0;

    for (int j = 0; j < n; j++) {
        prefix += (s[j] == '1' ? 1 : -1);

        ans += X.order_of_key({prefix, 0});

        X.insert({prefix, j + 1});
    }

    cout << ans << '\n';
}