| <p> | |
| The final exam is here, and it's now or never for Ethan. | |
| His current grade is abysmal so he needs a strong showing on this exam to have any chance of passing his introductory computer science class. | |
| </p> | |
| <p> | |
| The exam has only one question: devise an algorithm to compute the compactness of a grid tree. | |
| </p> | |
| <p> | |
| Ethan recalls that a "grid tree" is simply an unweighted tree with 2<strong>N</strong> nodes that you can imagine being embedded within a 2x<strong>N</strong> grid. | |
| The top row of the grid contains the nodes 1 ... <strong>N</strong> from left to right, | |
| and the bottom row of the grid contains the nodes (<strong>N</strong> + 1) ... 2<strong>N</strong> from left to right. | |
| Every edge in a grid tree connects a pair of nodes which are adjacent in the 2x<strong>N</strong> grid. | |
| Two nodes are considered adjacent if either they're in the same column, or they're directly side-by-side in the same row. | |
| There must be exactly 2<strong>N</strong>-1 edges that connect the 2<strong>N</strong> nodes to form a single tree. | |
| Additionally, the <em>i</em>th node in the grid tree is labelled with an integer <strong>A<sub>i</sub></strong>. | |
| </p> | |
| <p> | |
| What was "compactness" again? After some intense thought, Ethan comes up with the following pseudocode to compute the compactness, <strong>c</strong>, of a grid tree: | |
| </p> | |
| <ul> | |
| <li> 1. Set <strong>c</strong> to be equal to 0. | |
| <li> 2. Iterate <em>i</em> upwards from 1 to 2<strong>N</strong> - 1: | |
| <li> 2a. Iterate <em>j</em> upwards from <em>i</em>+1 to 2<strong>N</strong>: | |
| <li> 2b. Increase <strong>c</strong> by <strong>A<sub>i</sub></strong> * <strong>A<sub>j</sub></strong> * <code>ShortestDistance(i, j)</code> | |
| <li> 3. Output <strong>c</strong>. | |
| </ul> | |
| <p> | |
| <code>ShortestDistance(i, j)</code> is a function which returns the number of edges on the shortest path from node <em>i</em> to node <em>j</em> in the tree, | |
| which Ethan has implemented correctly. In fact, his whole algorithm is quite correct for once. This is exactly how you compute compactness! | |
| </p> | |
| <p> | |
| There's just one issue — in his code, Ethan has chosen to store <strong>c</strong> using a rather small integer type, | |
| which is at risk of overflowing if <strong>c</strong> becomes too large! | |
| </p> | |
| <p> | |
| Ethan is so close! Feeling sorry for him, you'd like to make some last-minute changes to the tree in order to minimize the final value of <strong>c</strong>, | |
| and thus minimize the probability that it will overflow in Ethan's program and cost him much-needed marks. | |
| You can't change any of the node labels <strong>A<sub>1..2N</sub></strong>, | |
| but you may choose your own set of 2<strong>N</strong> - 1 edges to connect them into a grid tree. | |
| </p> | |
| <p> | |
| For example, if <strong>A</strong> = [1, 3, 2, 2, 4, 5], then the grid of nodes looks like this: | |
| </p> | |
| <p> | |
| You'd like to determine the minimum possible compactness which Ethan's program can produce given a valid tree of your choice. | |
| For example, one optimal tree for the above grid of nodes (which results in the minimum possible compactness of 198) is as follows: | |
| </p> | |
| <h3>Input</h3> | |
| <p> | |
| Input begins with an integer <strong>T</strong>, the number of trees. For each tree, there are three lines. The first line contains the single integer <strong>N</strong>. | |
| The second line contains the <strong>N</strong> space-separated integers <strong>A<sub>1..N</sub></strong>. | |
| The third line contains the <strong>N</strong> space-separated integers <strong>A<sub>N+1..2N</sub></strong>. | |
| </p> | |
| <h3>Output</h3> | |
| <p> | |
| For the <em>i</em>th tree, output a line containing "Case #<em>i</em>: " followed by the minimum possible output of Ethan's program. | |
| </p> | |
| <h3>Constraints</h3> | |
| <p> | |
| 1 ≤ <strong>T</strong> ≤ 80 <br /> | |
| 1 ≤ <strong>N</strong> ≤ 50 <br /> | |
| 1 ≤ <strong>A<sub>i</sub></strong> ≤ 1,000,000 <br /> | |
| </p> | |
| <h3>Explanation of Sample</h3> | |
| <p> | |
| One optimal tree for the first case is given above. For that tree, Ethan's program would compute <strong>c</strong> as the sum of the following values (with some values omitted): | |
| </p> | |
| <ul> | |
| <li> <strong>A<sub>1</sub></strong> * <strong>A<sub>2</sub></strong> * <code>ShortestDistance(1, 2)</code> = 1 * 3 * 1 = 3 | |
| <li> <strong>A<sub>1</sub></strong> * <strong>A<sub>3</sub></strong> * <code>ShortestDistance(1, 3)</code> = 1 * 2 * 4 = 8 | |
| <li> ... | |
| <li> <strong>A<sub>1</sub></strong> * <strong>A<sub>6</sub></strong> * <code>ShortestDistance(1, 6)</code> = 1 * 5 * 3 = 15 | |
| <li> <strong>A<sub>2</sub></strong> * <strong>A<sub>3</sub></strong> * <code>ShortestDistance(2, 3)</code> = 3 * 2 * 3 = 18 | |
| <li> ... | |
| <li> <strong>A<sub>4</sub></strong> * <strong>A<sub>6</sub></strong> * <code>ShortestDistance(4, 6)</code> = 2 * 5 * 2 = 20 | |
| <li> <strong>A<sub>5</sub></strong> * <strong>A<sub>6</sub></strong> * <code>ShortestDistance(5, 6)</code> = 4 * 5 * 1 = 20 | |
| </ul> | |
| <p> | |
| In the second case, there's only one possible tree, for which <strong>c</strong> = 2 * 3 * 1 = 6. | |
| </p> | |
| <p> | |
| In the third case, two of the four possible trees are optimal (the ones omitting either the topmost or leftmost potential edge). | |
| </p> | |