Олимпиадный тренинг

Задача 27302. Ski Course Rating


Задача

Темы:
The ski route is described by an M x N grid of heights (1 <= M,N <= 500), each height in the range 0 .. 1,000,000,000.  
 
Some of these cells are marked as route starting points. The organizers want to calculate a difficulty rating for each starting point. Starting point difficulty rating P – is the minimum number D such that a cow can  successfully reach at least T grid cells  (1 <= T <= MN) if it starts at P and can move to an adjacent cell (north, south, west or east) only if the absolute value of the height difference in these cells does not exceed D. < /div>
 
Calculate the difficulty rating for each starting point and print their sum.
 
 
INPUT FORMAT:
 
* Line 1: Integers M, N, T.
 
* Lines 2..1+M: Each of these M lines contains N integer heights.
 
* Lines 2+M..1+2M: Each of these M lines contains N values ​​equal to 0 or 1, where 1 means that this is a cell – starting point


OUTPUT FORMAT:
 
* Line 1: The sum of the difficulty ratings of all starting points (note that this number may not fit into a 32-bit integer, even if each individual rating does).
 

INPUT DETAILS:
 
The terrain is described by a grid of 3 x 5 heights.
The top left and bottom right cells are the starting points.
From each starting point, we should be able to get to 10 cells.
 
OUTPUT DETAILS:
The difficulty rating of the upper left corner is 4.
The difficulty rating of the bottom right corner is 20.
 
Enter Output
3 5 10
20 21 18 99 5
19 22 20 16 17
18 17 40 60 80
1 0 0 0 0
0 0 0 0 0
0 0 0 0 1
24