Problem:
Merge Sort

Write a program of a Merge Sort algorithm implemented by the following pseudocode. You should also report the number of comparisons in the Merge function.

Merge(A, left, mid, right)
n1 = mid - left;
n2 = right - mid;
create array L[0...n1], R[0...n2]
for i = 0 to n1-1
do L[i] = A[left + i]
for i = 0 to n2-1
do R[i] = A[mid + i]
L[n1] = SENTINEL
R[n2] = SENTINEL
i = 0;
j = 0;
for k = left to right-1
if L[i] <= R[j]
then A[k] = L[i]
i = i + 1
else A[k] = R[j]
j = j + 1

Merge-Sort(A, left, right){
if left+1 < right
then mid = (left + right)/2;
call Merge-Sort(A, left, mid)
call Merge-Sort(A, mid, right)
call Merge(A, left, mid, right)

Input

In the first line n is given. In the second line, n integers are given.

Output

In the first line, print the sequence S. Two consequtive elements should be separated by a space character.

In the second line, print the number of comparisons.

Constraints

n ≤ 500000

0 ≤ an element in S ≤ 109

Sample Input 1

10
8 5 9 2 6 3 7 1 10 4

Sample Output 1

1 2 3 4 5 6 7 8 9 10
34

Notes

Solution in PHP (judge verdict: Runtime Error)

<?php
	$cnt = 0;
	function merge(&$array, $p, $q, $r){
		global $cnt;
		$left = [];
		$right = [];

		$left_num = $q - $p + 1;
		$right_num = $r - $q;
		for($i = 0; $i < $left_num; ++$i){
			$left[] = $array[$i + $p];
		}
		for($j = 0; $j < $right_num; ++$j){
			$right[] = $array[$j + $q + 1];
		}
		$left[] = PHP_INT_MAX;
		$right[] = PHP_INT_MAX;	

		$i = 0;
		$j = 0;
		for($k = $p; $k <= $r; ++$k){
			++$cnt;
			if($left[$i] < $right[$j]){
				$array[$k] = $left[$i];
				++$i;
			}else{
				$array[$k] = $right[$j];
				++$j;
			}
		}
	}
	function mergeSort(&$array, $p, $r){
		if($p < $r){
			$q = (int)(($p + $r)/2);
			mergeSort($array, $p, $q);
			mergeSort($array, $q+1, $r);
			merge($array, $p, $q, $r);
		}
	}

	function print_array($array){
		for($i = 0; $i < count($array); ++$i){
			echo $array[$i];
			if($i != count($array) - 1)
				echo " ";
		}
		echo PHP_EOL;
	}

	$n = intval(trim(fgets(STDIN)));
	$input = trim(fgets(STDIN));
	$numbers = explode(" ", $input);

	mergeSort($numbers, 0, $n - 1);
	print_array($numbers);
	echo $cnt . PHP_EOL;
?>