def bubble_sort(array):
    flag = True
    while flag:
        flag = False
        for i in range(N-1, 0, -1):
            if array[i] < array[i-1]:
                array[i], array[i-1] = array[i-1], array[i]
                flag = True

def merge_count(array):
    if len(array) <= 1:
        return array, 0
    mid = len(array) // 2
    left, cl = merge_count(array[:mid])
    right, cr = merge_count(array[mid:])
    count = cl + cr
    left = list(reversed(left))
    right = list(reversed(right))
    for i in range(len(array)):
        if not left:
            array[i] = right.pop()
        elif not right:
            array[i] = left.pop()
        elif left[-1] <= right[-1]:
            array[i] = left.pop()
        else:
            array[i] = right.pop()
            count += len(left)
    return array, count

if __name__ == "__main__":
    N = int(input())
    A = list(map(int, input().split()))
    a, b = merge_count(A)
    print(*a)
    print(b) def bubble_sort(array):
    flag = True
    while flag:
        flag = False
        for i in range(N-1, 0, -1):
            if array[i] < array[i-1]:
                array[i], array[i-1] = array[i-1], array[i]
                flag = True bubble_sort flag = True flag True while flag:
        flag = False
        for i in range(N-1, 0, -1):
            if array[i] < array[i-1]:
                array[i], array[i-1] = array[i-1], array[i]
                flag = True flag flag = False flag False for i in range(N-1, 0, -1):
            if array[i] < array[i-1]:
                array[i], array[i-1] = array[i-1], array[i]
                flag = True i range(N-1, 0, -1) range N-1 N 1 0 -1 if array[i] < array[i-1]:
                array[i], array[i-1] = array[i-1], array[i]
                flag = True array[i] < array[i-1] array[i] array i array[i-1] array i-1 i 1 array[i], array[i-1] = array[i-1], array[i] array[i] array i array[i-1] array i-1 i 1 array[i-1] array i-1 i 1 array[i] array i flag = True flag True array array def merge_count(array):
    if len(array) <= 1:
        return array, 0
    mid = len(array) // 2
    left, cl = merge_count(array[:mid])
    right, cr = merge_count(array[mid:])
    count = cl + cr
    left = list(reversed(left))
    right = list(reversed(right))
    for i in range(len(array)):
        if not left:
            array[i] = right.pop()
        elif not right:
            array[i] = left.pop()
        elif left[-1] <= right[-1]:
            array[i] = left.pop()
        else:
            array[i] = right.pop()
            count += len(left)
    return array, count merge_count if len(array) <= 1:
        return array, 0
     len(array) <= 1 len(array) len array 1 return array, 0 return array, 0 array 0 mid = len(array) // 2 mid len(array) // 2 len(array) len array 2 left, cl = merge_count(array[:mid]) left merge_count(array[:mid]) merge_count array[:mid] array :mid mid cl right, cr = merge_count(array[mid:]) right merge_count(array[mid:]) merge_count array[mid:] array mid: mid cr count = cl + cr count cl + cr cl cr left = list(reversed(left)) left list(reversed(left)) list reversed(left) reversed left right = list(reversed(right)) right list(reversed(right)) list reversed(right) reversed right for i in range(len(array)):
        if not left:
            array[i] = right.pop()
        elif not right:
            array[i] = left.pop()
        elif left[-1] <= right[-1]:
            array[i] = left.pop()
        else:
            array[i] = right.pop()
            count += len(left)
     i range(len(array)) range len(array) len array if not left:
            array[i] = right.pop()
        elif not right:
            array[i] = left.pop()
        elif left[-1] <= right[-1]:
            array[i] = left.pop()
        else:
            array[i] = right.pop()
            count += len(left)
     not left left array[i] = right.pop() array[i] array i right.pop() right.pop right pop elif not right:
            array[i] = left.pop()
         not right right array[i] = left.pop() array[i] array i left.pop() left.pop left pop elif left[-1] <= right[-1]:
            array[i] = left.pop()
         left[-1] <= right[-1] left[-1] left -1 right[-1] right -1 array[i] = left.pop() array[i] array i left.pop() left.pop left pop array[i] = right.pop() array[i] array i right.pop() right.pop right pop count += len(left) count len(left) len left return array, count return array, count array count array array if __name__ == "__main__":
    N = int(input())
    A = list(map(int, input().split()))
    a, b = merge_count(A)
    print(*a)
    print(b) __name__ == "__main__" __name__ "__main__" N = int(input()) N int(input()) int input() input A = list(map(int, input().split())) A list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split a, b = merge_count(A) a merge_count(A) merge_count A b print(*a) print *a a print(b) print b A = list(map(int, input().split())) list(map(int, input().split())) A def bubble_sort(array):
    flag = True
    while flag:
        flag = False
        for i in range(N-1, 0, -1):
            if array[i] < array[i-1]:
                array[i], array[i-1] = array[i-1], array[i]
                flag = True def bubble_sort(array):
    flag = True
    while flag:
        flag = False
        for i in range(N-1, 0, -1):
            if array[i] < array[i-1]:
                array[i], array[i-1] = array[i-1], array[i]
                flag = True bubble_sort b = merge_count(A) merge_count(A) b N = int(input()) int(input()) N a, b = merge_count(A) merge_count(A) a def merge_count(array):
    if len(array) <= 1:
        return array, 0
    mid = len(array) // 2
    left, cl = merge_count(array[:mid])
    right, cr = merge_count(array[mid:])
    count = cl + cr
    left = list(reversed(left))
    right = list(reversed(right))
    for i in range(len(array)):
        if not left:
            array[i] = right.pop()
        elif not right:
            array[i] = left.pop()
        elif left[-1] <= right[-1]:
            array[i] = left.pop()
        else:
            array[i] = right.pop()
            count += len(left)
    return array, count def merge_count(array):
    if len(array) <= 1:
        return array, 0
    mid = len(array) // 2
    left, cl = merge_count(array[:mid])
    right, cr = merge_count(array[mid:])
    count = cl + cr
    left = list(reversed(left))
    right = list(reversed(right))
    for i in range(len(array)):
        if not left:
            array[i] = right.pop()
        elif not right:
            array[i] = left.pop()
        elif left[-1] <= right[-1]:
            array[i] = left.pop()
        else:
            array[i] = right.pop()
            count += len(left)
    return array, count merge_count
def bubble_sort(array):
    flag = True
    while flag:
        flag = False
        for i in range(N-1, 0, -1):
            if array[i] < array[i-1]:
                array[i], array[i-1] = array[i-1], array[i]
                flag = True

def merge_count(array):
    if len(array) <= 1:
        return array, 0
    mid = len(array) // 2
    left, cl = merge_count(array[:mid])
    right, cr = merge_count(array[mid:])
    count = cl + cr
    left = list(reversed(left))
    right = list(reversed(right))
    for i in range(len(array)):
        if not left:
            array[i] = right.pop()
        elif not right:
            array[i] = left.pop()
        elif left[-1] <= right[-1]:
            array[i] = left.pop()
        else:
            array[i] = right.pop()
            count += len(left)
    return array, count

if __name__ == "__main__":
    N = int(input())
    A = list(map(int, input().split()))
    a, b = merge_count(A)
    print(*a)
    print(b)