|시간 제한||메모리 제한||제출||정답||맞은 사람||정답 비율|
|2 초||512 MB||0||0||0||0.000%|
There are three parks in Jakarta, called Park 1, Park 2, and Park 3. The new governor of Jakarta wants to add decorations to these parks by adding bricks. These bricks are going to be stacked on top of another on these three parks. There are N bricks in Jakarta, indexed from 1 to N. A brick with index i is smaller than a brick with index i + 1. We do not want to put a bigger brick on top of a smaller brick. Therefore, we can put a brick with index i on top of a brick with index j if and only if i < j.
We define a brick configuration as a distribution of these N bricks on the three parks. Note that there must only be one stack of bricks in each park, and the bricks must be stacked in the order as explained on the previous paragraph. For example, if N = 3, then a possible brick configuration is: brick 1 (on the top) and brick 2 (on the bottom) are in park 1, no brick is in park 2, and brick 3 is in park 3.
We can change a brick configuration to another by doing an operation, which we will:
Initially, the bricks are in some brick configuration, which we will call the initial brick configuration. The new governor of Jakarta wants to try M brick configurations. Therefore, from the initial brick configuration, we must do several operations such that each of the M brick configurations (in any order) wanted by the new governor is seen at least once. In the end, the new governor also wants us to put all the bricks in (any) one park. We want to minimise the total cost to satisfy his demand.
Formally, let G1, G2, ..., GM be the brick configurations wanted by the new governor. We want to construct a sequence of brick configurations C0, C1, ..., Ck (k ≥ 0) with the minimum cost where:
Let us help the new governor of Jakarta!
The first line contains two integers: N M (1 ≤ N ≤ 40; 0 ≤ M ≤ 16) in a line denoting the number of bricks and the number of configurations. The next three lines, each contains three integers. The j-th integer on the i-th line is Ri,j (0 ≤ Ri,j ≤ 1000; Ri,i = 0) denoting the cost of moving a brick from the i-th city to the j-th city. The next three lines denote the initial brick configuration, with the format explained in the next paragraph. The next M blocks describe the configurations wanted by the new governor, where each block contains three lines, with the format of each configuration explained in the next paragraph.
Each configuration is denoted by three lines. The i-th line begins with an integer K (0 ≤ K ≤ N) denoting the number of bricks in park i, followed by K integers: A1 A2 ... AK (1 ≤ Ai ≤ N) denoting the brick indices in park i. It is guaranteed that Ai < Aj for all 1 ≤ i < j ≤ K. It is also guaranteed that each of the integers between 1 and N, inclusive, appears exactly once on the union of the array A on the three lines. For example, the sample brick configuration given on the second paragraph will be illustrated in the first sample input.
The output contains the minimum total cost (in units of money) to satisfy the new governor's demand, in a line.
3 0 0 1 1000 1 0 1 1000 1 0 2 1 2 0 1 3
3 2 0 2 2 2 0 2 2 2 0 2 1 2 1 3 0 3 1 2 3 0 0 0 0 3 1 2 3
Explanation for the 1st sample case
In the first sample, we initially have brick 1 and brick 2 in park 1, and brick 3 in park 3. Since M = 0, our goal is to only reach a brick configuration where all bricks are in the same (any) park. Therefore, we can do the following operations:
Therefore, all of the bricks are now in park 2 and we spend 5 units of money. There is no solution that costs less than 5 units of money. Note that we do not necessarily minimise the number of operations.
Explanation for the 2nd sample case
In the second sample, the optimal solution can be achieved if we satisfy the second configuration first before the first one. From the initial configuration, we can get to the second configuration with 4 operations with the total cost of 8 units. From the second configuration, we can get to the first configuration with 7 operations with the total cost of 14 units. Since the first configuration already has all the bricks stacked in one park, we don’t need any additional operation. Therefore, the total cost is 22 units.