cho.sh
Notes
Loading...

Marbles in Boxes

Time limit

2s

Memory limit

128 MB

Problem

There are two boxes, one of size A×1 and one of size B×1. The first box contains A marbles, and the second box contains B marbles, stacked from bottom to top. Each marble is labeled with an integer from 1 to N.

Each box has a hole at the bottom. Through this hole, you can see the label on the current bottom marble and remove that marble. You may repeatedly perform one of the following three operations.

  1. Remove the bottom marble from the first box. This operation gives no score.
  2. Remove the bottom marble from the second box. This operation gives no score.
  3. Remove the bottom marbles from both boxes at the same time. Look up the two labels in the score table and add that value to the total score.

The total score starts at 0. Continue until no marbles remain in either box. Find the maximum total score that can be obtained, and an operation sequence that obtains it.

In the score table, the row is the label removed from the first box, and the column is the label removed from the second box.

Input

The first line contains three integers N, A, and B. Each value is between 1 and 1,000, inclusive.

The next N lines contain the score table, with N integers on each line. Each score is between -1,000 and 1,000, inclusive.

The following line contains the A labels in the first box, listed from the bottom marble upward. The final line contains the B labels in the second box, also listed from the bottom marble upward.

Output

Print the maximum total score on the first line.

On the second line, print the operation numbers, in order, separated by spaces, for an operation sequence that obtains this maximum score.