#ALDS1_5-B Merge Sort
from collections import deque

def merge(A,B):
    ans = [0]*(len(A)+len(B))
    count=i=j=0
    A+=[10**9+1]
    B+=[10**9+1]
    for k in range(len(ans)):
        if A[i]<B[j]:
            ans[k]=A[i]
            i+=1
        else:
            ans[k]=B[j]
            j+=1
        count+=1
    return (ans,count)

n = int(input())
S = [int(i) for i in input().split()]
div = deque([S])
mer = deque()
count = 0
while(len(div)>0):
    d = div.pop()
    if(len(d)>2):
        mid = len(d)//2
        div.appendleft(d[:mid])
        div.appendleft(d[mid:])
    elif(len(d)==2):
        if d[0] > d[1]:
            mer.appendleft(d[::-1])
        else:
            mer.appendleft(d)
        count+=2
    else:
        mer.appendleft(d)
        
while(len(mer)>1):
    A = mer.popleft()
    B = mer.popleft()
    ret = merge(A,B)
    mer.append(ret[0])
    count+=ret[1]
    
ans = ""
for i in ret[0]:
    ans += str(i) + " "
print(ans[:-1])
print(count) from collections import deque def merge(A,B):
    ans = [0]*(len(A)+len(B))
    count=i=j=0
    A+=[10**9+1]
    B+=[10**9+1]
    for k in range(len(ans)):
        if A[i]<B[j]:
            ans[k]=A[i]
            i+=1
        else:
            ans[k]=B[j]
            j+=1
        count+=1
    return (ans,count) merge ans = [0]*(len(A)+len(B)) ans [0]*(len(A)+len(B)) [0] 0 len(A)+len(B) len(A) len A len(B) len B count=i=j=0 count 0 =i=j=0 i 0 j A+=[10**9+1] A [10**9+1] 10**9+1 10**9 10 9 1 B+=[10**9+1] B [10**9+1] 10**9+1 10**9 10 9 1 for k in range(len(ans)):
        if A[i]<B[j]:
            ans[k]=A[i]
            i+=1
        else:
            ans[k]=B[j]
            j+=1
        count+=1
     k range(len(ans)) range len(ans) len ans if A[i]<B[j]:
            ans[k]=A[i]
            i+=1
        else:
            ans[k]=B[j]
            j+=1
         A[i]<B[j] A[i] A i B[j] B j ans[k]=A[i] ans[k] ans k A[i] A i i+=1 i 1 ans[k]=B[j] ans[k] ans k B[j] B j j+=1 j 1 count+=1 count 1 return (ans,count) (ans,count) ans count A A B B n = int(input()) n int(input()) int input() input int(i) for i in input().split() for i in input().split() i input().split() ().split () input split for i in input().split() int(i) int i S = [int(i) for i in input().split()] S [int(i) for i in input().split()] div = deque([S]) div deque([S]) deque [S] S mer = deque() mer deque() deque count = 0 count 0 while(len(div)>0):
    d = div.pop()
    if(len(d)>2):
        mid = len(d)//2
        div.appendleft(d[:mid])
        div.appendleft(d[mid:])
    elif(len(d)==2):
        if d[0] > d[1]:
            mer.appendleft(d[::-1])
        else:
            mer.appendleft(d)
        count+=2
    else:
        mer.appendleft(d)
         len(div)>0 len(div) len div 0 d = div.pop() d div.pop() div.pop div pop if(len(d)>2):
        mid = len(d)//2
        div.appendleft(d[:mid])
        div.appendleft(d[mid:])
    elif(len(d)==2):
        if d[0] > d[1]:
            mer.appendleft(d[::-1])
        else:
            mer.appendleft(d)
        count+=2
    else:
        mer.appendleft(d)
         len(d)>2 len(d) len d 2 mid = len(d)//2 mid len(d)//2 len(d) len d 2 div.appendleft(d[:mid]) div.appendleft div appendleft d[:mid] d :mid mid div.appendleft(d[mid:]) div.appendleft div appendleft d[mid:] d mid: mid elif(len(d)==2):
        if d[0] > d[1]:
            mer.appendleft(d[::-1])
        else:
            mer.appendleft(d)
        count+=2
     len(d)==2 len(d) len d 2 if d[0] > d[1]:
            mer.appendleft(d[::-1])
        else:
            mer.appendleft(d)
         d[0] > d[1] d[0] d 0 d[1] d 1 mer.appendleft(d[::-1]) mer.appendleft mer appendleft d[::-1] d ::-1 -1 mer.appendleft(d) mer.appendleft mer appendleft d count+=2 count 2 mer.appendleft(d) mer.appendleft mer appendleft d while(len(mer)>1):
    A = mer.popleft()
    B = mer.popleft()
    ret = merge(A,B)
    mer.append(ret[0])
    count+=ret[1]
     len(mer)>1 len(mer) len mer 1 A = mer.popleft() A mer.popleft() mer.popleft mer popleft B = mer.popleft() B mer.popleft() mer.popleft mer popleft ret = merge(A,B) ret merge(A,B) merge A B mer.append(ret[0]) mer.append mer append ret[0] ret 0 count+=ret[1] count ret[1] ret 1 ans = "" ans "" for i in ret[0]:
    ans += str(i) + " " i ret[0] ret 0 ans += str(i) + " " ans str(i) + " " str(i) str i " " print(ans[:-1]) print ans[:-1] ans :-1 -1 print(count) print count d = div.pop() div.pop() d count+=2 2 count ans = "" "" ans mer = deque() deque() mer B = mer.popleft() mer.popleft() B A = mer.popleft() mer.popleft() A def merge(A,B):
    ans = [0]*(len(A)+len(B))
    count=i=j=0
    A+=[10**9+1]
    B+=[10**9+1]
    for k in range(len(ans)):
        if A[i]<B[j]:
            ans[k]=A[i]
            i+=1
        else:
            ans[k]=B[j]
            j+=1
        count+=1
    return (ans,count) def merge(A,B):
    ans = [0]*(len(A)+len(B))
    count=i=j=0
    A+=[10**9+1]
    B+=[10**9+1]
    for k in range(len(ans)):
        if A[i]<B[j]:
            ans[k]=A[i]
            i+=1
        else:
            ans[k]=B[j]
            j+=1
        count+=1
    return (ans,count) merge n = int(input()) int(input()) n mid = len(d)//2 len(d)//2 mid ans += str(i) + " " str(i) + " " ans S = [int(i) for i in input().split()] [int(i) for i in input().split()] S ret = merge(A,B) merge(A,B) ret count+=ret[1] ret[1] count div = deque([S]) deque([S]) div count = 0 0 count
#ALDS1_5-B Merge Sort
from collections import deque

def merge(A,B):
    ans = [0]*(len(A)+len(B))
    count=i=j=0
    A+=[10**9+1]
    B+=[10**9+1]
    for k in range(len(ans)):
        if A[i]<B[j]:
            ans[k]=A[i]
            i+=1
        else:
            ans[k]=B[j]
            j+=1
        count+=1
    return (ans,count)

n = int(input())
S = [int(i) for i in input().split()]
div = deque([S])
mer = deque()
count = 0
while(len(div)>0):
    d = div.pop()
    if(len(d)>2):
        mid = len(d)//2
        div.appendleft(d[:mid])
        div.appendleft(d[mid:])
    elif(len(d)==2):
        if d[0] > d[1]:
            mer.appendleft(d[::-1])
        else:
            mer.appendleft(d)
        count+=2
    else:
        mer.appendleft(d)
        
while(len(mer)>1):
    A = mer.popleft()
    B = mer.popleft()
    ret = merge(A,B)
    mer.append(ret[0])
    count+=ret[1]
    
ans = ""
for i in ret[0]:
    ans += str(i) + " "
print(ans[:-1])
print(count)
