Time limit: 1s
Memory limit: 256MB
Input:
Output:
Task
Given a grid of integers consisting of rows and columns, where each of its cells has a color and a number written on it.
Find the maximum sum of numbers on the cells of a path that goes only right and down, such that its starting and ending cells have the same color.
Input data
The input file consists of:
- a line containing integers , .
- lines, the -th of which consisting of the integers , where is the integer in the cell .
- lines, the -th of which consisting of the integers , where is the color of the cell .
Output data
Output a single line containing a single integer -- the answer to the problem.
Constraints and clarifications
- .
- .
- for each and .
- for each and .
| # | Score | Constraints |
|---|---|---|
| 1 | 0 | Examples |
| 2 | 4 | and |
| 3 | 7 | and |
| 4 | 18 | and |
| 5 | 5 | |
| 6 | 12 | |
| 7 | 54 | No additional restrictions |
Example 1
stdin
3 3
1 1 2
0 -1 -4
1 5 1
3 2 3
4 5 6
2 3 1
stdout
7
Explanation
In the first example, the maximum sum path goes through the cells , , , .
Notice that it's possible to achieve a greater sum by continuing to the cell ,
but that path shouldn't be considered because its starting cell has a different color form its ending cell .
Example 2
stdin
2 2
0 -1
1 1
1 1
1 1
stdout
2
Explanation
In the second example, the maximum sum path goes through the cells and .