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;

//https://qiita.com/tubo28/items/e6076e9040da57368845
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")
}

trait Joinable {
    fn join(self, sep: &str) -> String;
}

//to_string()メソッドの要素を持つイテレータに対して、分割文字列を与えられるようにする
impl<U: ToString, T: Iterator<Item = U>> Joinable for T {
    fn join(self, sep: &str) -> String {
        self.map(|x| x.to_string()).collect::<Vec<_>>().join(sep)
    }
}

//参考
//https://onlinejudge.u-aizu.ac.jp/solutions/problem/ALDS1_5_B/review/4236610/core2duoe6320/Rust
//https://onlinejudge.u-aizu.ac.jp/solutions/problem/ALDS1_5_B/review/2709881/arsenic28/Rust

fn mergesort(a: &mut Vec<u32>, left: usize, right: usize) -> usize {
    //両端のインデックスが隣り合う。2要素になるまで分割する。
    //分割の順番はDFSの順番
    if right - left > 1 {
    //if left + 1 < right {
        let mid = (left + right) / 2;
        let count =
            //左側を分割
            mergesort(a, left, mid)
            //右側を分割
            + mergesort(a, mid, right)
            //統合
            + merge(a, left, mid, right);
        count
    } else {
        0
    }
}

fn merge(a: &mut Vec<u32>, left: usize, mid: usize, right: usize) -> usize {
    //左右に2分割。末尾に番兵を設定。
    let l: Vec<u32> = a[left..mid].iter().cloned().chain(std::iter::once(std::u32::MAX)).collect();
    let r: Vec<u32> = a[mid..right].iter().cloned().chain(std::iter::once(std::u32::MAX)).collect();

    let mut i = 0;
    let mut j = 0;
    let mut count = 0;  

    //println!("left:{} right{}",left,right);
    //left..right部分をソートする
    //lとrの左端を比較し小さい方を選んでいく
    for k in left..right {
        //println!("l:{} r:{}",l[i],r[j]);
        count += 1;
        if l[i] <= r[j] {
            a[k] = l[i].clone();
            i += 1;
        } else {
            a[k] = r[j].clone();
            j += 1;
        }
    }
    count
}

fn main() {
    let n: usize = read();
    let mut a: Vec<u32> = (0..n).map(|_| read()).collect();

    //ベクタ、左端インデックス、右端インデックス
    let count = mergesort(&mut a, 0, n);
    println!("{}", a.iter().join(" "));

    /*
    println!(
        "{}",
        a.iter()
            .map(|x| x.to_string())
            .collect::<Vec<_>>()
            .join(" ")
    );
    */

    println!("{}", count);
}