<b>An Animal Contest 3 P7 - Monkey Lasers (Up Version)</b> <i>Note:</i> This problem is…
- User
An Animal Contest 3 P7 - Monkey Lasers (Up Version)
Note: This problem is a modified version of this problem. The only difference is that in this problem, Moses can go up.
Moses the monkey finds himself in a grid with a ton of lasers!
The grid has (N + 2) rows numbered from 0 to (N + 1) and (M) columns numbered from 1 to (M). Each row (i) from row 1 to (N) has a special laser located on cell (c_i) of that row. However, each laser can only face one direction, either left or right, which Moses knows beforehand. A laser located at column (i) facing left will vaporize everything in any cell on the same row with column (j) where (j < i). Similarly, a laser facing right will vaporize every cell on the same row with column (j \geq i).
Moses can start off in any cell on row 0. He wishes to reach row (N + 1), but has a strong desire to not be vaporized. Thus, from his current cell, Moses can move to any adjacent cell inside the grid that does not cause him to be vaporized, incurring a cost of 1. If Moses moves into a row where the laser is not looking in his direction, he uses his handy-dandy gadget to flip the laser's direction. Note that Moses is not allowed to enter a cell with a laser.
Moses also has a special ability that he can use, and by using his special ability once, he flips the direction of every remaining laser. Using this ability while he is in row (i) has a cost of (k_i). Moreover, the special ability is available to be used an infinite number of times in any row between 0 and (N) inclusive.
Given that Moses can end off in any cell on row (N + 1), what is the smallest possible cost required to reach row (N + 1) without being vaporized?
Constraints
(\bullet\ 1 \leq N \leq 2 \times 10^5)
(\bullet\ 2 \leq M \leq 10^9)
(\bullet\ 1 \leq c_i \leq M)
(\bullet\ 1 \leq k_i \leq 10^9)
Input Specification
The first line contains two space-separated integers (N) and (M).
The next line contains (N) space-separated integers (c_i), the position of the laser for each row from 1 to (N).
The third line contains a string of length (N) where the (i)-th laser is facing left if (d_i) is L and facing right if (d_i) is R.
The fourth and final line contains (N + 1) space-separated integers (k_i), the cost of using the special ability on each row from 0 to (N).
Output Specification
Output one integer representing the minimum cost Moses will incur to reach row (N + 1).
Sample Input 1
[ 2\ 3 1\ 2 LR 7\ 727\ 69 ]
Sample Output 1
[ 11 ] Subtask 1
We observe that we can maintain a state ((r, c, \text{flip})). Note that the flip state is simply a boolean.
Now finally, in order to optimize this to (O(N^2)) states with an (O(N)) transition, we need to observe that there are only (3N) relevant columns, those being the ones to the left, on top of, and to the right of the lasers. Proof is left as an exercise to the reader.
- Assistant