#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int t,n,a[N];
int main()
{
cin>>t;
for(int i=1;i<=t;i++){
cin>>n;
for(int j=1;j<=n;j++){
cin>>a[j];
}
sort(a+1,a+1+n);
long long ans=0;
int top=n;
for(int j=n;j>1;j-=2){
if(a[j]>0&&a[j-1]>0){
ans+=a[j]*a[j-1];
top=j-2;
}
else{
break;
}
}
for(int j=1;j<top;j+=2){
ans+=a[j]*a[j+1];
}
cout<<ans<<endl;
for(int j=1;j<=n;j++){
a[j]=0;
}
}
return 0;
}
纯排序暴力,注意负负得正,升序排列后,从后往前加一遍,再从前往后加一遍,记得记录第一次的终点

京公网安备 11010502036488号