def merge(a, left, mid, right):
    l, r = [], []
    for i in range(left, mid):
        l.append(a[i])
    l.append(float('inf'))
    for i in range(mid, right):
        r.append(a[i])
    r.append(float('inf'))
    i, j = 0, 0
    for k in range(left, right):
        if l[i] < r[j]:
            a[k] = l[i]
            i += 1
        else:
            a[k] = r[j]
            j += 1


def merge_sort(a, left, right):
    res = 0
    if left + 1 < right:
        mid = (left + right) // 2
        res += merge_sort(a, left, mid)
        res += merge_sort(a, mid, right)
        merge(a, left, mid, right)
        res += right - left
    return res

def main():
    n = int(input())
    a = list(map(int, input().split()))
    ans = merge_sort(a, 0, len(a))
    print(' '.join(map(str, a)))
    print(ans)

if __name__ == '__main__':
    main() def merge(a, left, mid, right):
    l, r = [], []
    for i in range(left, mid):
        l.append(a[i])
    l.append(float('inf'))
    for i in range(mid, right):
        r.append(a[i])
    r.append(float('inf'))
    i, j = 0, 0
    for k in range(left, right):
        if l[i] < r[j]:
            a[k] = l[i]
            i += 1
        else:
            a[k] = r[j]
            j += 1 merge l, r = [], [] l [] r [] for i in range(left, mid):
        l.append(a[i])
     i range(left, mid) range left mid l.append(a[i]) l.append l append a[i] a i l.append(float('inf')) l.append l append float('inf') float 'inf' for i in range(mid, right):
        r.append(a[i])
     i range(mid, right) range mid right r.append(a[i]) r.append r append a[i] a i r.append(float('inf')) r.append r append float('inf') float 'inf' i, j = 0, 0 i 0 j 0 for k in range(left, right):
        if l[i] < r[j]:
            a[k] = l[i]
            i += 1
        else:
            a[k] = r[j]
            j += 1 k range(left, right) range left right if l[i] < r[j]:
            a[k] = l[i]
            i += 1
        else:
            a[k] = r[j]
            j += 1 l[i] < r[j] l[i] l i r[j] r j a[k] = l[i] a[k] a k l[i] l i i += 1 i 1 a[k] = r[j] a[k] a k r[j] r j j += 1 j 1 a a left left mid mid right right def merge_sort(a, left, right):
    res = 0
    if left + 1 < right:
        mid = (left + right) // 2
        res += merge_sort(a, left, mid)
        res += merge_sort(a, mid, right)
        merge(a, left, mid, right)
        res += right - left
    return res merge_sort res = 0 res 0 if left + 1 < right:
        mid = (left + right) // 2
        res += merge_sort(a, left, mid)
        res += merge_sort(a, mid, right)
        merge(a, left, mid, right)
        res += right - left
     left + 1 < right left + 1 left 1 right mid = (left + right) // 2 mid (left + right) // 2 left + right left right 2 res += merge_sort(a, left, mid) res merge_sort(a, left, mid) merge_sort a left mid res += merge_sort(a, mid, right) res merge_sort(a, mid, right) merge_sort a mid right merge(a, left, mid, right) merge a left mid right res += right - left res right - left right left return res res a a left left right right def main():
    n = int(input())
    a = list(map(int, input().split()))
    ans = merge_sort(a, 0, len(a))
    print(' '.join(map(str, a)))
    print(ans) 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 ans = merge_sort(a, 0, len(a)) ans merge_sort(a, 0, len(a)) merge_sort a 0 len(a) len a print(' '.join(map(str, a))) print ' '.join(map(str, a)) ' '.join ' ' join map(str, a) map str a print(ans) print ans if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def merge_sort(a, left, right):
    res = 0
    if left + 1 < right:
        mid = (left + right) // 2
        res += merge_sort(a, left, mid)
        res += merge_sort(a, mid, right)
        merge(a, left, mid, right)
        res += right - left
    return res def merge_sort(a, left, right):
    res = 0
    if left + 1 < right:
        mid = (left + right) // 2
        res += merge_sort(a, left, mid)
        res += merge_sort(a, mid, right)
        merge(a, left, mid, right)
        res += right - left
    return res merge_sort def main():
    n = int(input())
    a = list(map(int, input().split()))
    ans = merge_sort(a, 0, len(a))
    print(' '.join(map(str, a)))
    print(ans) def main():
    n = int(input())
    a = list(map(int, input().split()))
    ans = merge_sort(a, 0, len(a))
    print(' '.join(map(str, a)))
    print(ans) main def merge(a, left, mid, right):
    l, r = [], []
    for i in range(left, mid):
        l.append(a[i])
    l.append(float('inf'))
    for i in range(mid, right):
        r.append(a[i])
    r.append(float('inf'))
    i, j = 0, 0
    for k in range(left, right):
        if l[i] < r[j]:
            a[k] = l[i]
            i += 1
        else:
            a[k] = r[j]
            j += 1 def merge(a, left, mid, right):
    l, r = [], []
    for i in range(left, mid):
        l.append(a[i])
    l.append(float('inf'))
    for i in range(mid, right):
        r.append(a[i])
    r.append(float('inf'))
    i, j = 0, 0
    for k in range(left, right):
        if l[i] < r[j]:
            a[k] = l[i]
            i += 1
        else:
            a[k] = r[j]
            j += 1 merge
def merge(a, left, mid, right):
    l, r = [], []
    for i in range(left, mid):
        l.append(a[i])
    l.append(float('inf'))
    for i in range(mid, right):
        r.append(a[i])
    r.append(float('inf'))
    i, j = 0, 0
    for k in range(left, right):
        if l[i] < r[j]:
            a[k] = l[i]
            i += 1
        else:
            a[k] = r[j]
            j += 1


def merge_sort(a, left, right):
    res = 0
    if left + 1 < right:
        mid = (left + right) // 2
        res += merge_sort(a, left, mid)
        res += merge_sort(a, mid, right)
        merge(a, left, mid, right)
        res += right - left
    return res

def main():
    n = int(input())
    a = list(map(int, input().split()))
    ans = merge_sort(a, 0, len(a))
    print(' '.join(map(str, a)))
    print(ans)

if __name__ == '__main__':
    main()
