#P6839. [BJOI2016] 打字机

[BJOI2016] 打字机

Description

During a move, Little J found an old typewriter. Out of curiosity, Little J decided to study how to use it. First, you need to insert a paper tape of length mm into the typewriter. The typewriter has 26 keys, the lowercase letters a to z. Each time you press a key, the typewriter immediately prints that character on the tape and shifts the tape forward by one unit. Little J quickly mastered how to use this typewriter and wanted to try a new challenge.

He took out a dictionary, chose nn words, and assigned a score to each word. Each time a specified word appears in the tape, you gain the corresponding score. For example, if the word eye has score 22 and year has score 33, then the tape eyeyeyear has a score of 99. Little J wants to challenge himself by typing a tape with the highest possible score.

In particular, Little J may occasionally slip and press a character he did not want. Since this old typewriter has no backspace (delete) function, Little J can only accept the mistake and re-plan how to get the highest score under such mistakes. If Little J may press a wrong key at any position, and it is guaranteed that the total number of wrong presses during the whole process does not exceed kk, please compute the maximum score he can guarantee in the worst case.

Input Format

The first line contains 3 non-negative integers n,m,kn, m, k, representing the number of words, the tape length, and the maximum number of wrong key presses.

The next nn lines each contain a string SS and a positive integer aia_i, separated by a space, describing a word and its score.

Output Format

Only one line containing an integer, representing the maximum score in the worst case.

2 4 1
w 1
ha 9
9

Hint

[Sample Explanation]

Below is an incorrect idea:

"There are 44 cases in total: the 1st position is wrong, the 2nd position is wrong, the 3rd position is wrong, and the 4th position is wrong.

  1. If the 1st position is wrong (assume it becomes x, same below), the maximum score is xwha, score 1010.
  2. If the 2nd position is wrong, the maximum score is wxha, also 1010.
  3. If the 3rd position is wrong, the maximum score is haxw, also 1010.
  4. If the 4th position is wrong, the maximum score is hawx, also 1010.

Therefore, in the worst case the maximum score is 1010."

The mistake in this idea is that you cannot decide which key to press first based on which position will be wrong. In other words, you only know which position is wrong after pressing that key. The correct idea is as follows:

  1. At position 11, press h. If it is correct, go to 2; if it is wrong, go to 4.
  2. At position 22, press a. If it is correct, go to 3; if it is wrong, go to 5.
  3. At positions 33 and 44, both press w, then end. With at most 1 mistake, the final tape is hawx or haxw, score 1010.
  4. For the next three positions, press haw in order, then end. Since there will be no more mistakes, the final tape is xhaw, score 1010.
  5. For the next two positions, press ha in order, then end. Since there will be no more mistakes, the final tape is hxha, score 99.

Therefore, in the worst case, the maximum score is 99.

[Constraints]

Testdata points 1,21,2 satisfy n=1n = 1 or k=0k = 0.

Testdata points 161 \sim 6 satisfy n100n \le 100, m500m \le 500, S500∑|S| \le 500, ai1000a_i \le 1000.

Testdata points 7,87,8 satisfy k=0k = 0, S200∑|S| \le 200.

Testdata points 9,109,10 satisfy S50∑|S| \le 50, ai1a_i \le 1.

For 100%100\% of the testdata, n100n \le 100, m109m \le 10^9, S500∑|S| \le 500, ai1000a_i \le 1000, k5k \le 5. Please note that each test point has its own special property.

Translated by ChatGPT 5