Merge sort with inversion counting

PHP Hard sorting

Task «Merge sort with inversion counting»

Given an array of integers, it is necessary to implement a merge sort algorithm that also counts the number of inversions in the array. An inversion is a pair of indices (i, j) such that i < j and arr[i] > arr[j]. Output the sorted array and the number of inversions.

Input Format

In the first line, an integer N (1 ≤ N ≤ 10^5) — the number of elements. In the second line, N integers separated by spaces.

Output Format

In the first line, N numbers separated by spaces — an array sorted in ascending order. In the second line, a single integer — the number of inversions.

Examples

Example 1

INPUT
Война и мир Лев Толстой 1869
OUTPUT
Название: Война и мир, Автор: Лев Толстой, Год: 1869

Example 2

INPUT
3 1 2 3
OUTPUT
1 2 3 0

Example 3

INPUT
1984 Джордж Оруэлл 1949
OUTPUT
Название: 1984, Автор: Джордж Оруэлл, Год: 1949
main.php