You are given two vectors v1=(x1,x2,…,xn)v1=(x1,x2,…,xn) and v2=(y1,y2,…,yn)v2=(y1,y2,…,yn). The scalar product of these vectors is a single number, calculated as x1y1+x2y2+…+xnynx1y1+x2y2+…+xnyn.
Suppose you are allowed to permute the coordinates of each vector as you wish. Choose two permutations such that the scalar product of your two new vectors is the smallest possible, and output that minimum scalar product.
Input
The first line of the input file contains the number of testcases, T≤10T≤10. For each test case, the first line contains integer number nn. The next two lines contain nn integers each, giving the coordinates of v1v1 and v2v2 respectively.
You may assume that 1≤n≤8001≤n≤800 and −100000≤xi,yi≤100000−100000≤xi,yi≤100000.
Output
For each test case, output a line
Case #X: Y
where XX is the test case number, starting from 1, and YY is the minimum scalar product of all permutations of the two given vectors.
Sample Input 1 | Sample Output 1 |
---|---|
2 3 1 3 -5 -2 4 1 5 1 2 3 4 5 1 0 1 0 1 |
Case #1: -25 Case #2: 6 |
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=810;
int a[maxn];
int b[maxn];
bool cmp(int x,int y)
{
return x>y;
}
int main()
{
int t;
int f=0;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
for(int i=0; i<n; i++)
scanf("%d",&a[i]);
for(int i=0; i<n; i++)
scanf("%d",&b[i]);
sort(a,a+n);
sort(b,b+n,cmp);
ll ans=0;
for(int i=0; i<n; i++)
{
ans+=(ll)a[i]*b[i];
}
f++;
printf("Case #%d: %lld\n",f,ans);
}
return 0;
}