LCC '23 Contest 3 S3 - Snowing

View as PDF

Submit solution

Points: 10
Time limit: 3.0s
PyPy 3 15.0s
Python 3 15.0s
Memory limit: 512M
PyPy 3 1G
Python 3 1G

Author:
Problem type

Snow has been asked to shovel the snow from her driveway (how ironic). Her driveway is an N by M metre grid, where each square metre (i, j) has a uniform snow height of h_{ij} cm.

Before clearing the driveway of snow, Snow decides to play a little game. She wants to take a picture for her photography instagram account, but she doesn't like how the snow on her driveway looks. In order for Snow to be satisfied with her driveway, the difference in snow heights between adjacent square metres (including diagonal!) must not exceed 1 cm.

To achieve this aesthetic, Snow will use her precision shoveling skills to remove exactly 1 cm of snow from one square metre at a time. Because it must be so precise, it takes her 1 minute each time.

Given how her driveway looks right now, determine the minimum amount of time it would take for Snow to be satisfied with her driveway.

Constraints

1 \le N, M \le 10^3

1 \le h_{ij} \le 10^9

Input Specification

The first line will contain integers N and M.

The next N lines will contain M space-separated integers, h_{ij}.

Output Specification

Output one integer, the minimum amount of time it would take for Snow to be satisfied with her driveway.

Sample Input

4 4
4 5 3 4
5 6 7 8
3 4 5 5
3 4 5 6

Sample Output

11

Sample Explanation

After 11 minutes, Snow's driveway will look like this:

4 4 3 4
4 4 4 4
3 4 5 5
3 4 5 6

It can be shown that no other driveway will take as little time to create as this one.


Comments

There are no comments at the moment.