| Innopolis Open 2024. Final round |
|---|
| Finished |
The VK company urgently needed to move servers from one data center to another. This is a very important and urgent task, because not all services will function properly during their movement, but there is traffic on the roads today, so the servers will be moved by helicopter.
The map of the area over which the helicopter can move is a field consisting of $$$n$$$ rows and $$$m$$$ columns. The numbering of the rows goes from $$$1$$$ to $$$n$$$ from top to bottom, the numbering of the columns goes from $$$1$$$ to $$$m$$$ from left to right. Each cell represents a section of the same height. We will characterize the cell in row $$$i$$$ and column $$$j$$$ as $$$(i, j)$$$, and the minimum height at which the helicopter can safely fly in the corresponding section as $$$a_{i, j}$$$.
The servers need to be moved from cell $$$(1, 1)$$$ to cell $$$(n, m)$$$. The helicopter has already taken off and is at a height of $$$a_{1, 1}$$$. When the helicopter reaches cell $$$(n, m)$$$ at any allowed height, the route is considered completed.
At any given time the height at which the helicopter is located must be at least the minimum flight height for the current cell, that is, if we denote the height at which the helicopter is currently located in cell $$$(i, j)$$$ as $$$h$$$, it must satisfy $$$a_{i, j} \le h$$$. At the same time, from position $$$(i, j)$$$ at height $$$h$$$ the helicopter can:
Due to weather conditions the helicopter may not be able to fly in all directions. The available directions are specified by the string $$$\mathtt{s}$$$. Each possible flight direction is represented by the following letters:
The height can always be increased or decreased. The movement directions 'R' and 'D' are always allowed. It is not possible to move beyond the field boundaries.
Programmers from VK quickly managed to determine the optimal fuel costs for the helicopter's route, even with such a large number of conditions on its movement. Can you do it too?
The first line contains two integers $$$n$$$ and $$$m$$$ — the dimensions of the field over which the helicopter moves ($$$1 \le n, m \le 1000$$$).
The second line contains a string $$$\mathtt{s}$$$, consisting of different characters 'L', 'R', 'U', and 'D' — the allowed flight directions ($$$2 \le |s| \le 4$$$). It is guaranteed that $$$\text{'\t{R}'}, \text{'\t{D}'} \in \mathtt{s}$$$.
Then there are $$$n$$$ lines with $$$m$$$ numbers each — the description of the field; the $$$i$$$-th line contains the numbers $$$a_{i,j}$$$ (the values of the minimum flight height) describing the $$$i$$$-th row of the field ($$$0 \le a_{i, j} \le 10^9$$$).
Output a single number — the minimum amount of fuel needed to travel from the position at height $$$a_{1,1}$$$ in cell $$$(1, 1)$$$ to cell $$$(n, m)$$$.
Points for each subtask are awarded only if all the tests of this subtask and the necessary subtasks are successfully passed. Here, $$$A$$$ denotes $$$\max\limits_{i,j} a_{i,j}$$$.
| Subtask | Points | Constraints | Necessary subtasks | Feedback policy |
| 0 | – | examples | full | |
| 1 | 5 | $$$n, m, A \le 10$$$ | 0 | full |
| 2 | 5 | $$$n, m \le 10^3$$$, $$$A \le 1$$$ | first error | |
| 3 | 5 | $$$n = 1$$$, $$$m \le 10^3$$$ | first error | |
| 4 | 7 | $$$n, m, A \le 500$$$, $$$\mathtt{s} = \text{"\t{RD}"}$$$ | first error | |
| 5 | 15 | $$$n, m \le 10^3$$$, $$$\mathtt{s} = \text{"\t{RD}"}$$$ | 4 | first error |
| 6 | 5 | $$$n, m \le 10^3$$$, and $$$a_{i - 1, j}, a_{i, j - 1} \le a_{i, j}$$$ | first error | |
| 7 | 15 | $$$n, m \le 10^3$$$, $$$\mathtt{s} = \text{"\t{LRD}"}$$$ | 5 | first error |
| 8 | 5 | $$$n, m, A \le 100$$$ | 0, 1 | first error |
| 9 | 13 | $$$n, m, A \le 500$$$ | 0, 1, 4, 8 | first error |
| 10 | 25 | no additional constraints | 0 – 9 | first error |
3 3LRDU1 2 44 4 41 1 1
13
5 5LRD0 10 0 0 00 0 0 10 010 10 10 10 09 5 3 2 03 3 2 5 0
30
In the first example the best path is to fly down to the limit, then right to the limit. The first two flights will have to be made at a height of $$$4$$$, the last two can be made at a height of $$$1$$$.
In the second example, the optimal path passes through cells with $$$a_{i,j} = 0$$$ and costs $$$0$$$ fuel, but for this it is necessary to move upwards, and upward movements are prohibited. In this case it is possible to fly over the "zero" cells through cell $$$(1, 2)$$$ or $$$(2, 4)$$$ with $$$a_{1,2} = a_{2,4} = 10$$$. To fly over either of them it will be necessary to ascend to a height of $$$10$$$ then perform two movements at this height.
| Name |
|---|


