#include using namespace std; #define ll long long #define Mod 1000000007 void merge(int a[], int low, int mid, int high); void merge_sort(int a[], int low, int high); void print(int a[], int size); ll con = 0; int main() { int *a, size; cin>>size; a = new int[size]; //creating array dynamically for(int i=0; i>a[i]; } //printing un-sorted array merge_sort(a, 0, (size-1) ); //sorting the array by passing array, its first & last indexes cout<<2*con; //printing sorted array return 0; } void merge_sort(int a[], int low, int high) { if(low >= high) return; int mid = (low+high)/2; //calculate mid-index merge_sort(a, low, mid); //sort left-half sub-array merge_sort(a, mid+1, high); //sort right-half sub-array merge(a, low, mid, high); //merge them } void merge(int a[], int low, int mid, int high) { con++; int *temp = new int[high-low+1]; //creating temporary array for merging 2 sub-arrays int left = low; //left points to lower index of left sub-array int right = mid+1; //right points to lower index of right sub-array int current = 0; // Merges the two arrays into temp[] while(left <= mid && right <= high) //mid & high points to last indexes of left & right sub-arrays respectively { if(a[left] <= a[right]) //if value of left-sub-array < right-sub-array { temp[current] = a[left]; //...then copying value of left-sub-array in temp left++; } else //else if value of right-sub-array < left-sub-array { temp[current] = a[right]; //...then copying value of left-sub-array in temp right++; } current++; } // Completes the array // Extreme example a = 1, 2, 3 || 4, 5, 6 // The temp array has already been filled with 1, 2, 3, // So, the right side of array a will be used to fill temp. if(left > mid) { for(int i=right; i <= high;i++) { temp[current] = a[i]; //copying all remaining elements of right sub-array in temp current++; } } // Extreme example a = 6, 5, 4 || 3, 2, 1 // The temp array has already been filled with 1, 2, 3 // So, the left side of array a will be used to fill temp. else { for(int i=left; i <= mid; i++) { temp[current] = a[i]; //copying all remaining elements of left sub-array in temp current++; } } // copy into the original array for(int i=0; i<=high-low;i++) //looping from lower-to-higher index of temp-array { a[low + i] = temp[i]; //here low --> lower-index of a particular sub-array } delete[] temp; }