Problem:
Merge Sort

Write a program of a Merge Sort algorithm implemented by the following pseudocode. You should also report the number of comparisons in the Merge function.

Merge(A, left, mid, right)
n1 = mid - left;
n2 = right - mid;
create array L[0...n1], R[0...n2]
for i = 0 to n1-1
do L[i] = A[left + i]
for i = 0 to n2-1
do R[i] = A[mid + i]
L[n1] = SENTINEL
R[n2] = SENTINEL
i = 0;
j = 0;
for k = left to right-1
if L[i] <= R[j]
then A[k] = L[i]
i = i + 1
else A[k] = R[j]
j = j + 1

Merge-Sort(A, left, right){
if left+1 < right
then mid = (left + right)/2;
call Merge-Sort(A, left, mid)
call Merge-Sort(A, mid, right)
call Merge(A, left, mid, right)

Input

In the first line n is given. In the second line, n integers are given.

Output

In the first line, print the sequence S. Two consequtive elements should be separated by a space character.

In the second line, print the number of comparisons.

Constraints

n ≤ 500000

0 ≤ an element in S ≤ 109

Sample Input 1

10
8 5 9 2 6 3 7 1 10 4

Sample Output 1

1 2 3 4 5 6 7 8 9 10
34

Notes

Solution in C (judge verdict: Accepted)

#include <stdio.h>
#define MAX 500000

void divi(int*,int,int);
void conq(int*,int,int,int);
int youko1,haire1[MAX],count=0;
int main()
{
  int i,lefhasi,righasi;
  scanf("%d",&youko1);
  for(i=0;i<youko1;i++)
    {
      scanf("%d",&haire1[i]);
    }
  lefhasi=0;
  righasi=youko1;
  divi(haire1,lefhasi,righasi);
  for(i=0;i<youko1-1;i++)
    {
      printf("%d ",haire1[i]);
    }
  printf("%d\n",haire1[youko1-1]);
  printf("%d\n",count);
  return 0;
}

void divi(int *haire1,int lefhasi,int righasi)
{
  int man;
  if(lefhasi+1<righasi)
    {
      man=(lefhasi+righasi)/2;
      divi(haire1,lefhasi,man);
      divi(haire1,man,righasi);
      conq(haire1,lefhasi,man,righasi);
    }
}

void conq(int *haire1,int lefhasi,int man,int righasi)
{
  int lefnum,rignum,i=0,j=0,k=0;
  lefnum=man-lefhasi;
  rignum=righasi-man;
  int lefhai[lefnum+1],righai[rignum+1];
  for(i=0;i<lefnum;i++)
    {
      lefhai[i]=haire1[lefhasi+i];
      // printf("%d\n",lefhai[lehasi+i]);
    }
  for(i=0;i<rignum;i++)
    {
      righai[i]=haire1[man+i];
      //printf("%d\n",righai[man+i]);
    }
  lefhai[lefnum]=1000000000;
  righai[rignum]=1000000000;
  i=0;
  for(k=lefhasi;k<righasi;k++)
    {
      if(lefhai[i]<=righai[j])
	{
	  haire1[k]=lefhai[i];
	  i++;
	  count++;
	}
      else
	{
	  haire1[k]=righai[j];
	  j++;
	  count++;
	}
    }
}
//4124232
/*

int divi(int haire1[],int lehasi,int rihasi)
{
  int man;
  if(lehasi+1<rihasi)
    {
      man=(lehasi+rihasi)/2;
      divi(haire1[],lehasi,man);
      divi(haire1[],man,rihasi);
      conq(haire1[],lehasi,man,rihasi);
    }
  return 0;
}

int conq(int haire1[],int lehasi,int man,int rihasi)
{
  int lefnum,rignum,lefhai,righai,i=0,j=0,k=0;
  lefnum=man-lehasi;
  rignum=right-man;
  for(i=1;i<=lefnum-1;i++)
    {
      lefhai[i]=haire1[lehasi+i];
      printf("lefhai[lehasi+i]\n");
    }
  for(i=1;i<=rignum-1;i++)
    {
      righai[i]=haire1[man+i];
      printf("righai[man+i]\n");
    }
  lefhai[lefnum]=50000;
  righai[rignum]=50000;
  i=0;
  for(k=lehasi;k<rihasi;k++)
    {
      if(lefhai[i]<=righai[j])
	{
	  haire1[k]=lefhai[i];
	  i++;
	  count++;
	}
      else
	{
	  haire1[k]=righai[j];
	  j++;
	  count++;
	}
    }
  return 0;
}
  int man,haileft[100],hairight[100],p,q;
  //printf("%d\n",lehasi);
  //printf("%d\n",rihasi);
  if(lehasi+1<rihasi)
    {
      man=(lehasi+rihasi)/2;
      //printf("%d\n",man);
      for(p=lehasi;p<=man;p++)
	{
	  printf("%d ",haire1[p]);
	}
      printf("\n");
      for(q=man+1;q<rihasi;q++)
	{
	  printf("%d ",haire1[q]);
	}
      printf("\n");
      divi(lehasi,man);
      divi(man,rihasi);
      conq(lehasi,man,rihasi);
    }
  return 0;
}
 */