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

def get_i() #空白区切の入力を数値(整数)の配列で返す
  return gets.chomp.split(" ").map(&:to_i)
end
def get_f() #空白区切の入力を数値(実数)の配列で返す
  return gets.chomp.split(" ").map(&:to_f)
end
def get() #空白区切の入力を文字列の配列で返す
  return gets.chomp.split(" ")
end
def get_nsp() #入力されたものを一文字ずつに区切った文字列の配列で返す
  return gets.chomp.split("")
end
def yn_judge(bool,y="Yes",n="No") #boolに真偽を投げることで、trueならy、falseならnの値を出力する
  return bool ? y : n 
end
def array(size1,init=nil,size2=-1) #size2に二次元配列時の最初の要素数、size1に次の配列の大きさ、initに初期値を投げることでその配列を返す
  if size2==-1
    return Array.new(size1){init}
  else
    return Array.new(size2){Array.new(size1){init}}
  end
end


INF=Float::INFINITY
$ans=0
def merge(a,l,m,r)
    n1=m-l
    n2=r-m
    l_arr=array(n1+1)
    r_arr=array(n2+1)
    n1.times do|i|
        l_arr[i]=a[l+i]
    end
    n2.times do|i|
        r_arr[i]=a[m+i]
    end
    l_arr[n1]=INF
    r_arr[n2]=INF
    i=0
    j=0
    l.upto(r-1) do|k|
        if l_arr[i]<=r_arr[j]
            a[k]=l_arr[i]
            i+=1
        else
            a[k]=r_arr[j]
            j+=1
        end
        $ans+=1
    end
end
def merge_sort(a,l,r)
    if l+1<r
        mid=(l+r)/2
        merge_sort(a,l,mid)
        merge_sort(a,mid,r)
        merge(a,l,mid,r)
    end
end
N=gets.to_i
a=get_i
merge_sort(a,0,N)
puts a.join(" ")
puts $ans