题目:
The bored BaoBao is playing a number game. In the beginning, there are 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 (about 30), the number of test cases. For each test case:
The first line contains two integers and (, ). Their meanings are described above.
The second line contains integers (), 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.
For the second sample test case, the answer is 2 × 1 = 2.
题解:
一开始的时候,我很天真的以为,只要头尾相乘就可以。然后保证不要溢出就行了,然而事实证明我太天真了。其实只要排序后保证
∑ai∗a2∗m−i+1
∑
a
i
∗
a
2
∗
m
−
i
+
1
这样就可以保证是最小的了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <set>
#include <map>
#include <queue>
#define scd(a) scanf("%d",&a)
#define scdd(a,b) scanf("%d%d",&a,&b)
#define scddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define mset(var,val) memset(var,val,sizeof(var))
#define test(a) cout<<a<<endl
#define test2(a,b) cout<<a<<" "<<b<<endl
#define test3(a,b,c) cout<<a<<" "<<b<<" "<<c<<endl
const int N= 2e5+7;
const int mod =1e9+7 ;
using namespace std;
int a[N];
void work(){
int n,m;
scdd(n,m);
for(int i =1;i<=n;i++){
scd(a[i]);
// test(a[i]);
}
sort(a+1,a+n+1);
long long ans=0;
for(int i =1;i<=m;i++){
ans = 1ll*(ans)+1ll*(1ll*a[i]*a[2*m-i+1]);
}
printf("%lld\n",ans);
}
int main(){
#ifdef local
freopen("in.txt","r",stdin);
#endif
int t;
scd(t);
while(t--){
work();
}
}