
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
 
int N[1000000];
int t = 0;
void Merge(int, int, int);
void Merge_Sort(int, int);
 
int main()
{
    int n,i;
    scanf("%d", &n);
    for (i = 0; i<n; i++){
        scanf("%d", &N[i]);
    }
    Merge_Sort(0, n);
    for (i = 0; i<n; i++)    {
        printf("%d", N[i]); 
        if (i < n - 1){  
            printf(" ");        
        }           
    }       
    printf("\n");
    printf("%d\n", t);
    return 0;
}
 
 
 
void Merge(int left, int mid, int right)
{
    int i, j, k, A = mid - left, B = right - mid;
    int L[A], R[B];
    for (i = 0; i<A; i++){
        L[i] = N[left + i];
    }
 
    for (i = 0; i<B; i++){
        R[i] = N[mid + i];
    }
    L[A] = 1000000000;
    R[B] = 1000000000;
    i = 0;
    j = 0;
    for (k = left; k < right; k++){
        if (L[i] <= R[j]){
            N[k] = L[i];
            i++;
        }
        else{
            N[k] = R[j];
            j += 1;
        }
        t = t + 1;
    }
}
 
void Merge_Sort(int left, int right){
    int mid;
    if (left + 1 <right){
        mid = (left + right) / 2;
        Merge_Sort(left, mid);
        Merge_Sort(mid, right);
        Merge(left, mid, right);
    }
}