Educational Round #5 — Editorial
Bessie has been playing the popular game Moortal Cowmbat for a long time. However, the game developers have recently released an update that forces her to change her usual play style.
The game uses buttons, labeled with the first lowercase letters of the Latin alphabet. Bessie’s favorite combination of moves is a string of length , describing a sequence of button presses. After the update, each combo must consist of a sequence of "stripes", where a stripe is defined as a consecutive sequence of the same button pressed at least times in a row. Bessie wants to modify her favorite combination to obtain a new string of the same length , but consisting of stripes that satisfy the updated rules.
It is known that Bessie needs a_{ij} days to learn to press button instead of button in any specific position of her combination (that is, replacing one occurrence of letter in with letter costs a_{ij} days).
Note that sometimes the replacement can be done faster through intermediate buttons. For example, changing directly to may be more expensive than performing two consecutive replacements i \rightarrow k \rightarrow j. Thus, there may exist a transformation path from to with a smaller total cost than the direct replacement.
Help Bessie determine the minimum number of days required to create a combination that satisfies the new requirements.
Input. The first line contains three integers: n~(1 \le n \le 10^5), m~(1 \le m \le 26), and k~(1 \le k \le n).
The second line contains the string .
The next lines each contain integers — the elements of the matrix a_{ij}, where a_{ij} is the number of days required to replace button with button . It is guaranteed that 0 \le a_{ij} \le 1000 and a_{ii} = 0 for all .
Output. Print the minimum number of days Bessie needs to create a combination that meets the new requirements.
Example. In this example, the optimal solution is to replace with , then replace with , and finally replace both 's with . The total cost of these changes is 1 + 4 + 0 + 0 = 5 days, and the resulting string will be .
5 5 2
abcde
0 1 4 4 4
2 0 4 4 4
6 5 0 3 2
5 5 5 0 4
3 7 0 5 05
The player starts with and must consecutively answer questions. Before each question, the player can:
stop the game and take the money currently in hand;
answer the question. If the answer is incorrect, the player leaves the game with nothing. If the answer is correct, the amount of money doubles, and the game proceeds to the next question.
After correctly answering the last question, the player keeps the winnings. The player's goal is to maximize the expected amount of money won.
For each individual question, the player answers correctly with probability . Assume that is uniformly distributed over the interval .
Input. Each line represents a separate test case and contains two numbers: an integer (1 \le n \le 30) and a real number (0 \le t \le 1). The last line contains two zeros and should not be processed.
Output. For each test case, print on a separate line the maximum expected amount of money the player can win under the optimal strategy. Print the answer with three decimal digits.
1 0.5
1 0.3
2 0.6
24 0.25
0 01.500
1.357
2.560
230.138
Comments