This page is still under construction.

Parts of this page are still being built. What you see may change.

Minimum Scalar Product (Small)

Interview

Time limit5sMemory limit512 MB

Summary
Permute two vectors to minimize their dot product and print the minimum.
Level

Medium4 of 10

Topics
Sorting, Greedy, Math
Solved
No attempts yet

Problem

You are given two vectors v1=(x1,x2,…,xn)v_1 = (x_1, x_2, \dots, x_n) and v2=(y1,y2,…,yn)v_2 = (y_1, y_2, \dots, y_n). The scalar product of the two vectors is a single number, computed as x1y1+x2y2+⋯+xnynx_1 y_1 + x_2 y_2 + \dots + x_n y_n.

You may permute the coordinates of each vector however you like. Choose two permutations that make the scalar product of the resulting vectors as small as possible, then print that minimum scalar product.

Input

The first line contains the number of test cases TT. For each test case, the first line contains an integer nn, and the next two lines contain nn integers each, giving the coordinates of v1v_1 and v2v_2 in order.

Limits

  • 1≤T≤10001 \le T \le 1000
  • 1≤n≤81 \le n \le 8
  • −1000≤xi,yi≤1000-1000 \le x_i, y_i \le 1000

Output

For each test case, print one line

Case #X: Y

where XX is the test case number starting from 1, and YY is the minimum scalar product over all permutations of the two given vectors.

Examples2

  1. Example 1

    Input
    2
    3
    1 3 -5
    -2 4 1
    5
    1 2 3 4 5
    1 0 1 0 1
    
    Expected output
    Case #1: -25
    Case #2: 6
    
  2. Example 2

    Input
    3
    1
    -1000
    1000
    1
    1000
    1000
    1
    0
    -1000
    
    Expected output
    Case #1: -1000000
    Case #2: 1000000
    Case #3: 0