Moortal Cowmbat
Time limit1sMemory limit512 MB
Rewrite a length-N string into streaks of at least K identical letters, where changing any single position from letter i to j costs a shortest-path distance over an M-letter graph; minimize total cost.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Shortest path, Prefix sum, Graph
- Solved
- No attempts yet
Problem
Bessie has been playing the popular fighting game Moortal Cowmbat for a long time. However, the game developers have recently rolled out an update that forces Bessie to change her play style.
The game uses buttons labeled by the first lowercase letters (). Bessie's favorite combo in the game is a length- string of button presses (). Because of the most recent update, every combo must now be made from a series of "streaks", where a streak is a series of the same button pressed at least times in a row (). Bessie wants to modify her favorite combo into a new combo of the same length that is made from streaks of button presses, so that it satisfies the changed rules.
Bessie needs days to train herself to press button instead of button at any specific location in her combo. That is, it costs to change a single specific letter in from to . Switching from button to an intermediate button and then from button to button may take less time than switching from to directly. More generally, there may be a path of changes that starts at and ends at and gives the best overall cost for switching from button ultimately to button .
Help Bessie determine the smallest possible number of days she needs to create a combo that satisfies the new requirements.
Input
The first line of input contains , , and . The second line contains , and the final lines contain an matrix of values , where is an integer in the range and for all .
Output
Output a single number, representing the minimum number of days Bessie needs to change her combo into one that satisfies the new requirements.
Hint
The optimal solution in this example is to change the a into b, change the d into e, and then change both e's into c's. This takes days, and the final combo string is bbccc.