I am getting wrong answer for this solution for [kingship][1]. Can anyone explain why?
my
[2] :
#include<stdio.h>
void quicksort(int x[],int first,int last)
{
int pivot,j,temp,i;
if(first<last)
{
pivot=first;
i=first;
j=last;
while(i<j){
while(x[i]<=x[pivot]&&i<last)
i++;
while(x[j]>x[pivot])
j--;
if(i<j){
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);
}
}
int get_num()
{
int num=0;
char c=getchar_unlocked();
while(!(c>='0' && c<='9'))
c=getchar_unlocked();
while(c>='0' && c<='9')
{
num=(num<<3)+(num<<1)+c-'0';
c=getchar_unlocked();
}
return num;
}
int main()
{
int t=get_num();
while(t--)
{
int n=get_num();
int a[1000010]={0};
int i=0;
while(i<n)
{
a[i++]=get_num();
}
quicksort(a,0,n-1);
i=1;
long long int sum=0;
while(i<n)
{
sum+=a[0]*a[i];
i++;
}
printf("%lld\n",sum);
}
return 0;
}
I used long long int for overcoming int size overflow. I came up with similar code with Java which was accepted.
[1]: http://www.codechef.com/COOK43/problems/KINGSHIP
[2]: http://www.codechef.com/viewsolution/3426567