首页 > 试题广场 >

carpet

[编程题]carpet
White Cloud has a rectangle carpet of . Grid (i,j) has a color and a cost .
White Rabbit will choose a subrectangle B of from A and the color of each grid is , the cost of B is the (maximum number in the corresponding subrectangle of ).
Then colorB is continuously translated and copied in an infinite times, that is, expand colorB into an infinite new matrix, colorC, which satisfies .
White Rabbit must ensure that colorA is a subrectangle of colorC.
You need to find the minimum cost way.

输入描述:
The first line of input contains two integers 
For the next line of n lines, each line contains m lowercase English characters, denoting colorA.
For the next line of n lines, each line contains m integers in range [0,1000000000], denoting costA.


输出描述:
Print the minimum cost.
示例1

输入

2 5
acaca
acaca
3 9 2 8 7
4 5 7 3 1

输出

18

说明

choose subrectangle colorA[1...1][3...4]=ca, After copying unlimited copies
colorC=
cacacacaca ...
cacacacaca ...
cacacacaca ...
cacacacaca ...
cacacacaca ...
.........
colorA is a subrectangle of colorC
the cost is max(3,1)*(1+1)*(2+1).

这道题你会答吗?花几分钟告诉大家答案吧!