```
#include<stdio.h>
#include<stdlib.h>
//#define MAX 10001
void quicksort(long long[],int,int);
int partition(long long[],int,int);
void swap(long long[],int,int);
int main()
{
int t,k,q,i,j,qi,c,p;
long long *mot,*sat,*res;
//printf("Enter no of test cases");
scanf("%d",&t);
for(i=0;i<t;i++)
{
p=0;
//printf("Enter no of chefs and no of queries");
scanf("\n%d %d",&k,&q);
mot=(long long*)malloc(k*sizeof(long long));
sat=(long long*)malloc(k*sizeof(long long));
res=(long long*)malloc(k*k*sizeof(long long));
//printf("Enter motivation levels");
for(j=0;j<k;j++)
scanf("%lld",&mot[j]);
//printf("Enter satisfaction levels");
for(j=0;j<k;j++)
scanf("%lld",&sat[j]);
//calc sum[]
for(c=0;c<k;c++)
for(j=0;j<k;j++)
res[p++]=mot[c]+sat[j];
free(mot);
free(sat);
quicksort(res,0,p-1); //partition,swap
for(j=0;j<q;j++)
{
//printf("\n qith lowest sum");
scanf("%d",&qi);
if(qi<=(k*k))
printf("%lld\n",res[qi-1]);
}
free(res);
}
return 0;
}
void quicksort(long long *sum,int low,int high)
{
int m;
if(low<high)
{
m=partition(sum,low,high);
quicksort(sum,low,m-1);
quicksort(sum,m+1,high);
}
}
int partition(long long *sum,int low,int high)
{
long long pivot=sum[low];
int i=low,j=high;
while(i<j)
{
while(sum[i]<=pivot&&i<high)
i++;
while(sum[j]>pivot)
j--;
if(i<j)
swap(sum,i,j);
}
swap(sum,low,j);
return j;
}
void swap(long long *sum,int i,int j)
{
long long temp;
temp=sum[i];
sum[i]=sum[j];
sum[j]=temp;
}
```

This is a code that i posted on codechef but i am getting this SIGSGEV error.I cracked my head a lot but in vain.

The constraints for the problem statement were

1 ≤ t ≤ 5

1 ≤ k ≤ 20000

1 ≤ q ≤ 500

1 ≤ qi ( for i = 1 to Q ) ≤ 10000

1 ≤ Ai ≤ 10^18 ( for i = 1 to K ) (value of element of mot[] can be)

1 ≤ Bi ≤ 10^18 ( for i = 1 to K ) (value of element of sat[] can be)