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 Rust (judge verdict: Accepted)

use std::io::*;
use std::str::FromStr;

fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

fn merge(a: &mut Vec<u64>, left: usize, mid: usize, right: usize) -> usize {
    let n1: usize = mid - left;
    let n2: usize = right - mid;
    let mut l: Vec<u64> = (0..n1)
        .map(|i| {
            let number: u64 = a[left + i];
            number
        })
        .collect();
    let mut r: Vec<u64> = (0..n2)
        .map(|i| {
            let number: u64 = a[mid + i];
            number
        })
        .collect();
    l.push(u64::max_value());
    r.push(u64::max_value());
    let mut i: usize = 0;
    let mut j: usize = 0;
    for k in left..right {
        if l[i] <= r[j] {
            a[k] = l[i];
            i += 1;
        } else {
            a[k] = r[j];
            j += 1;
        }
    }
    return i + j;
}

fn merge_sort(a: &mut Vec<u64>, left: usize, right: usize) -> usize {
    let mut count: usize = 0;

    if left + 1 < right {
        let mid: usize = (left + right) / 2;
        count += merge_sort(a, left, mid);
        count += merge_sort(a, mid, right);
        count += merge(a, left, mid, right);
    }
    return count;
}

fn main() {
    let n: usize = read();
    let mut a: Vec<u64> = (0..n)
        .map(|_| {
            let number: u64 = read();
            number
        })
        .collect();
    let count_number = merge_sort(&mut a, 0, n);
    for (index, number) in a.iter().enumerate() {
        if (index + 1) != a.len() {
            print!("{} ", number);
        } else {
            println!("{}", number);
        }
    }
    println!("{}", count_number);
}