int Bit[maxn]; inlinevoidadd(int i, int v){ while (i <= n) Bit[i] += v, i += lowbit(i); }
inlineintget_sum(int i){ int s = 0; while (i) s += Bit[i], i -= lowbit(i); return s; }
intmain(){ scanf("%d%s", &n, s + 1); for (int i = 1; i <= n; ++i) A[s[i] - 'a'].pb(i), B[s[i] - 'a'].pb(n - i + 1); for (int i = 0; i < 26; ++i) sort(B[i].begin(), B[i].end()); for (int i = 0; i < 26; ++i) { int l = A[i].size(); for (int j = 0; j < l; ++j) a[A[i][j]] = B[i][j]; } ll ans = 0; for (int i = 1; i <= n; ++i) { ans += i - 1 - get_sum(a[i] - 1); add(a[i], 1); } cout << ans << endl; return0; }