Problem I
Thin Ice
Languages
en
is
Uolevi is at a frozen lake in the shape of an $n \times m grid$, with one coin on each square. Each square has a durability: the maximum number of coins the ice on the square can withstand.
In one step, Uolevi can move one square up, down, left or right, but not outside the lake. If there is a coin on the square Uolevi is currently in, he can pick it up. When Uolevi moves to a square, the number of coins on the square must never exceed the durability of the square. This includes the coins Uolevi is carrying, and the one on the ice if it has not yet been picked up. Uolevi’s own weight is negligible.
Uolevi wants to start and end a trip on some edge squares of the lake. What is the largest number of coins he can gather during the trip?
Input
The first line of input contains the integers $n$ and $m$: the height and width of the lake. Then follow $n$ lines with $m$ integers each: the durability d of the ice on each square.
Output
Print the largest number of coins that Uolevi can gather.
Scoring
Group |
Points |
Constraints |
1 |
17 |
$1 \leq n \cdot m, d \leq 16$. |
2 |
12 |
$1 \leq n \cdot m \leq 200\, 000$, $1 \leq d \leq 5$. |
3 |
11 |
$n = 1$, $1 \leq m, d \leq 100$. |
4 |
19 |
$n = 1$, $1 \leq m, d \leq 200\, 000$. |
5 |
14 |
$1 \leq n \cdot m, d \leq 1\, 000$. |
6 |
27 |
$1 \leq n \cdot m, d \leq 200\, 000$. |
Explanation of sample
In the sample input Uolevi can start on the top left square. He moves down by one and picks up that coin. He then moves right and picks up that coin. Next he moves down, then left, and picks up that coin. Then he moves right and takes a coin twice. At this point he is on the edge and can leave with five coins. He can not collect six coins and return to the edge, as no edge square has durability $ \geq 6$.
Sample Input 1 | Sample Output 1 |
---|---|
3 4 1 1 1 1 1 3 6 1 3 4 5 1 |
5 |