The bored BaoBao is playing a number game. In the beginning, there are n numbers.For each turn, BaoBao will take out two numbers from the remaining numbers, and calculate the product of them.
Now, BaoBao is curious to know the minimum sum of the products if he plays at least m turns. Can you tell him?
Input
The first line of input contains a positive integer T (about 30), the number of test cases. For each test case:
The first line contains two integers n and m (1<=n<=10e5 ,0<=m<=n/2). Their meanings are described above.
The second line contains n integers (0<=a<=10e4), indicating the numbers.
Output
For each test case output one integer, indicating the minimum sum of the products.
Sample Input
3 4 2 1 3 2 4 3 1 2 3 1 4 0 1 3 2 4
Sample Output
10 2 0
Hint
For the first sample test case, the answer is 1 × 4 + 3 × 2 = 10.
#include<stdio.h>
int main(){
int a[10001];
int b[100100];
int T,cnt;
int n,m,ain;
int cnt1,cnt2;
long long sum;
scanf("%d",&T);
for(cnt=0;cnt<T;cnt++){
sum=0;
for(cnt1=0;cnt1<=10000;cnt1++)a[cnt1]=0;
cnt2=0;
scanf("%d %d",&n,&m);
for(cnt1=0;cnt1<n;cnt1++){
scanf("%d",&ain);
a[ain]++;
}
for(cnt1=0;cnt2<2*m;cnt1++){
if(a[cnt1]>0)for(cnt3=0;cnt3<a[cnt1];cnt3++){
b[cnt2+cnt3+1]=cnt1;
}
cnt2+=a[cnt1];
}
for(cnt1=1;cnt1<=m;cnt1++){
sum+=b[cnt1]*b[2*m-cnt1+1];
}
printf("%ld\n",sum);
}
return 0;
}
之前用快排显示超时,一看a是整数而且不超过一万,完全可以用桶排序