This page is still under construction.

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

Gahee and Sajik Stadium

Time limit1sMemory limit512 MB

Summary
Place N idols on an R by C grid to maximize total charm, counting each cell's charm and the extra charm for adjacent Three Musketeers pairs.
Level

Hard8 of 10

Topics
Graph, Brute force
Solved
No attempts yet

Problem

Gahee wants to place NN idols on the stage of Sajik Stadium, the home ground of the Lotte Giants, which she supports. The stage has RR rows and CC columns. Each idol has a number from 1 to NN. Three of the idols are called the Three Musketeers. When the three are adjacent to each other horizontally, vertically, or diagonally, they produce a special effect. Gahee wants to place the NN idols on the stage to maximize their total charm. Tell her how to do it.

Input

The first line has RR, CC, and NN separated by spaces. The second line has the numbers of the Three Musketeers, a1a_1, a2a_2, and a3a_3, separated by spaces. The third line has the extra charm gained when idol a1a_1 and idol a2a_2 are adjacent, when idol a2a_2 and idol a3a_3 are adjacent, and when idol a1a_1 and idol a3a_3 are adjacent, separated by spaces. The next RR lines each have CC numbers separated by spaces. The cc-th number on the rr-th line is the charm gained when an idol stands at row rr, column cc.

Output

Print the maximum total charm that Gahee can obtain when she places all NN idols on the stage.

Constraints

1≤R≤2001 \le R \le 200. 1≤C≤2001 \le C \le 200. 3≤R×C≤2003 \le R \times C \le 200. 3≤N≤R×C3 \le N \le R \times C. Each charm value at a point on the stage is between 1 and 10410^4. a1a_1, a2a_2, and a3a_3 are all different integers between 1 and NN. The extra charm for each adjacent pair among the Three Musketeers is an integer between 1 and 10410^4.

Examples3

  1. Example 1

    Input
    1 5 3
    1 2 3
    999 999 999
    99 1 99 1 99
    
    Expected output
    2197
    
  2. Example 2

    Input
    1 5 3
    1 2 3
    999 222 333
    99 1 99 1 99
    
    Expected output
    1531
    
  3. Example 3

    Input
    2 3 3
    1 2 3
    99 99 87
    99 11 99
    11 99 11
    
    Expected output
    495