[NOIP Simulation Test]: biology (DP)

Title Description

Flea country vast territory can be seen as a $ n $ lines $ m $ matrix columns, each grid represents a region, we can put the region first row $ i $ $ j $ column grid represented called $ (i, j) $.
The number of fleas Area $ (i, j) $ as $ a [i] [j] $, attractiveness to $ b [i] [j] $.
Now, boring flea king to some areas for review, in order to maintain ease of mind, flea king required number of fleas by reading areas in ascending order.
If some areas without fleas that $ a [i] [j] = 0 $, then the flea king will not be reviewed for these areas.
That is, you need to find a length of $ k (1 \ leqslant k \ leqslant n \ times m) $ path $ (x_i, y_i) $, such that:
    for $ 1 \ leqslant i \ leqslant k $, $ has a [x_i] [y_i]> 0 $.
    For $ 1 \ leqslant i <k $ , there $ a [x_i] [y_i] <a [x_i + 1] [y_i + 1] $.
Your task is to maximize the $ \ sum \ limits_ {i = 1} ^ kb [x_i] [y_i] + \ sum \ limits_ {i = 1} ^ {k-1} | x_i + 1-x_i | + | y_i + 1-y_i | $.


Input Format

First line of $ 2 $ integers $ n, m $.
Then there $ $ n-rows, each row integers $ m $, $ A $ array represents.
Then there $ $ n-rows, each row integers $ m $, $ b $ array represents.


Output Format

A row of integer answer.


Sample

Sample input:

3 3
0 6 8
1 6 1
0 6 8
0 1 2
3 4 5
0 6 7

Sample output:

21


Data range and tips

Sample explained:

Optimal path $ (2,3) \ rightarrow (3,2 ) \ rightarrow (3,3) $.
The best answer is $ 5 + 6 + 7 + | 3-2 | + | 2-3 | + | 3-3 | + | 3-2 | = 21 $.

data range:

For all data, $ 1 \ leqslant n, m \ leqslant 2 \ times {10} ^ 3,0 \ leqslant a_i \ leqslant {10} ^ 6,0 \ leqslant b_i \ leqslant {10} ^ 6 $.
To ensure that there is at least one region $ a [i] [j] > 0 $, all $ a [i] [j] = 0 $ area satisfies $ b [i] [j] = 0 $.

Guess you like

Origin www.cnblogs.com/wzc521/p/11465136.html