This page is still under construction.

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

The Urge to Merge

Time limit5sMemory limit128 MB

Summary
You pick disjoint adjacent pairs in a 3 by n grid to maximize the sum of the products of paired values.
Level

Medium7 of 10

Topics
Dynamic programming, Bit manipulation
Solved
No attempts yet

Problem

The Acme Consulting Group has sent you into a new technology park to raise its dynamism, synergy and sustainability. You are not sure what any of those words mean, but you are good at making money, and making money is what you plan to do.

The park is a 3×n3 \times n grid of facilities. Each facility houses one start-up, and every start-up has a value of its own. Arranging mergers between neighboring start-ups raises that value, which is how you will finally pay for the chain of latte-and-burrito shops you have always wanted to open.

Anti-trust law allows only two start-ups in a single merger, and no start-up may take part in more than one merger. Two start-ups may merge only when their facilities are adjacent, and facilities that touch at a corner are not adjacent. The added value of a merger is the product of the values of the two start-ups in it. You may leave a start-up out of every merger, and then it adds nothing.

Find the set of mergers with the largest total added value. For the grid in the first example the best choice of mergers adds up to 171.

Input

The input holds several test cases.

The first line of a test case is the width nn (1≤n≤10001 \le n \le 1000) of the facility grid. Three lines follow, each with nn integers, giving the values of the start-ups in that row. Every value is between 11 and 100100.

A line holding a single 00 ends the input.

Output

For each test case print one line in the form Case i: v, where ii is the test case number counting from 11 and vv is the largest total added value the mergers can reach.

Examples2

  1. Example 1

    Input
    4
    7 2 4 9
    3 5 9 3
    9 5 1 8
    0
    
    Expected output
    Case 1: 171
    
  2. Example 2

    Input
    1
    5
    6
    7
    2
    1 1
    1 1
    1 1
    3
    100 100 100
    100 100 100
    100 100 100
    0
    
    Expected output
    Case 1: 42
    Case 2: 3
    Case 3: 40000