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;

#[allow(unused_macros)]
macro_rules! scan {
  ($e:expr; $t:ty) => {
    $e.get::<$t>()
  };
  ($e:expr; $($t:ty), *) => {
    ($($e.get::<$t>(),)*)
  };
  ($e:expr => $i:expr; $t:ty) => {
    (0..$i).map(|_| $e.get::<$t>()).collect::<Vec<$t>>()
  };
  ($e:expr => $row:expr, $col:expr; $t:ty) => {
    (0..$i).map(|_| scan!($e => $col; $t)).collect::<Vec<$t>>()
  }
}

struct Scanner<R: Read> {
  reader: R
}

#[allow(dead_code)]
impl<R: Read> Scanner<R> {
  fn new(reader: R) -> Scanner<R> {
    Scanner { reader: reader }
  }
  fn safe_get<T: FromStr>(&mut self) -> Option<T> {
    let token = self.reader.by_ref().bytes().map(|c| c.unwrap() as char)
      .skip_while(|c| c.is_whitespace())
      .take_while(|c| !c.is_whitespace())
      .collect::<String>();
    if token.is_empty() { None } else { token.parse().ok() }
  }
  fn get<T: FromStr>(&mut self) -> T {
    if let Some(v) = self.safe_get() { v } else {
      writeln!(stderr(), "Terminated with EOF").unwrap();
      std::process::exit(0);
    }
  }
}

fn merge(vec: &mut Vec<i32>, left: usize, mid: usize, right: usize, cmp_n: &mut i32) {
  let n1 = mid - left;
  let n2 = right - mid;
  let mut l = Vec::with_capacity(n1 + 1);
  let mut r = Vec::with_capacity(n2 + 1);
  for i in 0..n1 {
    l.push(vec[left + i]);
  }
  for i in 0..n2 {
    r.push(vec[mid + i]);
  }
  l.push(std::i32::MAX);
  r.push(std::i32::MAX);
  let (mut i, mut j) = (0, 0);
  for k in left..right {
    *cmp_n += 1;
    if l[i] <= r[j] {
      vec[k] = l[i];
      i += 1;
    }
    else {
      vec[k] = r[j];
      j += 1;
    }
  }
}

fn merge_sort(vec: &mut Vec<i32>, left: usize, right: usize, cmp_n: &mut i32) {
  if left + 1 < right {
    let mid = (left + right) / 2;
    merge_sort(vec, left, mid, cmp_n);
    merge_sort(vec, mid, right, cmp_n);
    merge(vec, left, mid, right, cmp_n);
  }
}

fn main() {
  let cin = stdin();
  let cin = cin.lock();
  let mut sc = Scanner::new(cin);
  let n = scan!(sc; usize);
  let mut s = scan!(sc => n; i32);
  let mut cmp_n = 0;
  merge_sort(&mut s, 0, n, &mut cmp_n);
  println!("{}", s.iter().map(|&elem| elem.to_string()).collect::<Vec<String>>().join(" "));
  println!("{}", cmp_n);
}