int inv = 0;
void MSort( int n, int *a, int *buffer ) {
if (n <= 1) return;
int m = n / 2;
MSort(m, a, buffer);
MSort(n - m, a + m, buffer);
int i = 0, j = m;
for (int k = 0; k < n; k++)
if (j == n || (i < m && a[i] <= a[j]))
buffer[k] = a[i++];
else
inv += m - i, buffer[k] = a[j++];
copy(buffer, buffer + n, a);
}