#include<iostream>
using namespace std;
const int LENGTH = 100;
char temp[LENGTH];
int mergesort(char* s, int first, int last) {
if (first == last) return 0;
int mid = (first + last) / 2;
int ret = 0;
ret += mergesort(s, first, mid);
ret += mergesort(s, mid + 1, last);
int pos_f = first, pos_l = mid + 1, count = 0;
while (pos_f <= mid && pos_l <= last) {
if (s[pos_f] < s[pos_l]) {
temp[count++] = s[pos_f];
pos_f++;
}
else {
temp[count++] = s[pos_l];
pos_l++;
ret += mid + 1 - pos_f;
}
}
while (pos_f <= mid) {
temp[count++] = s[pos_f++];
}
while (pos_l <= last) {
temp[count++] = s[pos_l++];
}
int p = 0;
for (int i = first; i <= last; i++) {
s[i] = temp[p++];
}
return ret;
}
int main() {
char* nodes;
int n = 0;
cin >> n;
nodes = new char[n];
for (int i = 0; i < n; i++) {
cin >> nodes[i];
}
cout << mergesort(nodes, 0, n-1) << endl;
system("pause");
return 0;
}
using namespace std;
const int LENGTH = 100;
char temp[LENGTH];
int mergesort(char* s, int first, int last) {
if (first == last) return 0;
int mid = (first + last) / 2;
int ret = 0;
ret += mergesort(s, first, mid);
ret += mergesort(s, mid + 1, last);
int pos_f = first, pos_l = mid + 1, count = 0;
while (pos_f <= mid && pos_l <= last) {
if (s[pos_f] < s[pos_l]) {
temp[count++] = s[pos_f];
pos_f++;
}
else {
temp[count++] = s[pos_l];
pos_l++;
ret += mid + 1 - pos_f;
}
}
while (pos_f <= mid) {
temp[count++] = s[pos_f++];
}
while (pos_l <= last) {
temp[count++] = s[pos_l++];
}
int p = 0;
for (int i = first; i <= last; i++) {
s[i] = temp[p++];
}
return ret;
}
int main() {
char* nodes;
int n = 0;
cin >> n;
nodes = new char[n];
for (int i = 0; i < n; i++) {
cin >> nodes[i];
}
cout << mergesort(nodes, 0, n-1) << endl;
system("pause");
return 0;
}