Plzz correct the following code for segmented tree for sum of an array,,!!

#include<stdio.h>
#include<stdlib.h>

int *p;
int *a;
int i;
int mid;

int main(){
int n;

  printf("enter the value of n\n");
  scanf("%d",&n);

  p=calloc(2*n-1,sizeof(int));
  // p[2*n-1];
  a=calloc(n,sizeof(int));
  //a[n];
  printf("enter the original array");
  for(i=0;i<n;i++)
  {
   scanf("%d",&a[i]);

  }

  sum(0,n-1,0);


   for(i=0;i<(2*n-1);i++)
  {
   printf("%d\n",p[i]);

  }

// free§; free(a);

return 0;
}

int sum(int l,int r,int index)
{

  if(l==r)
  {
      p[index]=a[l];
      int q=a[l];
    return q;
  }

   else
    {    mid=(l+r)/2;
        p[index]=sum(l,mid,(2*index)+1)+sum(mid+1,r,(2*index)+2);
        int s=p[index];
        return s;
    }

}