You are given two vectors v1=(x1,x2,...,xn) and v2=(y1,y2,...,yn). The scalar product of these vectors is a single number, calculated as x1y1+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 integer number T - the number of test cases. For each test case, the first line contains integer number n. The next two lines contain n integers each, giving the coordinates of v 1 and v 2 respectively.
Output
For each test case, output a line
Case #X: Ywhere X is the test case number, starting from 1, and Y is the minimum scalar product of all permutations of the two given vectors.
Limits
Small dataset
T = 1000
1 ≤ n ≤ 8
-1000 ≤ xi, yi ≤ 1000
Large dataset
T = 10
100 ≤ n ≤ 800
-100000 ≤ xi, yi ≤ 100000
Sample
Input |
Output |
2 |
Case #1: -25 |
1 #include<stdio.h> 2 #include<string.h> 3 4 int i,j,n,m; 5 6 long long a[1000],b[1000]; 7 8 long long sum; 9 10 int 11 pre() 12 { 13 memset(a,0,sizeof(a)); 14 memset(b,0,sizeof(b)); 15 sum=0; 16 return 0; 17 } 18 19 int 20 init() 21 { 22 scanf("%d",&n); 23 for(i=1;i<=n;i++) 24 scanf("%lld",&a[i]); 25 for(i=1;i<=n;i++) 26 scanf("%lld",&b[i]); 27 28 return 0; 29 } 30 31 void 32 qsort(long long a[],int head,int tail) 33 { 34 int i,j; 35 long long x; 36 i=head;j=tail; 37 x=a[head]; 38 39 while(i<j) 40 { 41 while((i<j)&(a[j]>=x)) j--; 42 a[i]=a[j]; 43 while((i<j)&(a[i]<=x)) i++; 44 a[j]=a[i]; 45 } 46 a[i]=x; 47 48 if(head<(i-1)) qsort(a,head,i-1); 49 if((i+1)<tail) qsort(a,i+1,tail); 50 } 51 52 53 int 54 main() 55 { 56 int casi,cas; 57 freopen("1.in","r",stdin); 58 freopen("1.out","w",stdout); 59 scanf("%d",&cas); 60 for(casi=1;casi<=cas;casi++) 61 { 62 pre(); 63 init(); 64 qsort(a,1,n); 65 qsort(b,1,n); 66 67 for(i=1;i<=n;i++) 68 sum+=a[i]*b[n-i+1]; 69 70 printf("Case #%d: %lld\n",casi,sum); 71 } 72 return 0; 73 } 74