#P3999. [SHOI2013] 二重镇
[SHOI2013] 二重镇
Description
This is a village full of love, called Double Town. In this affectionate village, the residents live happily and peacefully. Double Town is long and narrow and can be divided into a row of cells. Each cell may be empty, or contain exactly one of the following: grass, shrub, tree, house, or castle. Each type has a level: grass has level , shrub has level , and so on.
You are the builder of this village. You will receive items one after another, and you must place them properly on empty cells. Your goal is to maximize the village’s total popularity. The rules for gaining popularity are explained later. The placement rules are as follows:
- First, every item must be placed somewhere and cannot be discarded. If there is no empty cell, the game ends immediately.
- Second, an item can be placed on one empty cell, or temporarily stored in the warehouse. The warehouse can hold at most one item at a time, and it is initially empty. There is only one warehouse.
- Third, once an item is placed on a cell, as long as conditions are met, the system will automatically merge some items into a larger item. This is mandatory, passive, and instantaneous. Only after merging finishes can you place the next item.
- Fourth, an item stored in the warehouse can be taken out and placed on an empty cell at any time (but not during a merge), or it may remain in the warehouse.
- Fifth, unless you use the warehouse, you cannot change the order in which items are placed.
In summary, the process is: when you receive a new item, decide whether to store it in the warehouse, then decide where to place either the warehouse item or the new item on an empty cell, the system automatically performs merges and awards popularity, and you continue until all items have been placed or there are no empty cells left.
Finally, here are the merge rules. Merging is automatic and mandatory. If two or more consecutive adjacent cells contain items of the same level, they will automatically merge into a new item whose level is one higher. A merge proceeds in three steps:
- Step 1: Determine how many items participate in the merge; these items must be contiguous and of the same level. All participating items disappear, and their cells become empty.
- Step 2: Suppose items of level merge. You gain popularity. For example, if five grasses merge once, the total popularity increases by .
- Step 3: One item of level appears in one cell. If is greater than , skip this step, but you still gain the popularity from Step 2, and the old items from Step 1 are still removed. The higher-level item appears only on one of the participating cells. Each cell records the time when an item was last placed on it; the new item appears on the cell with the latest such time—in other words, on the cell most recently placed upon.
Finally, note that merges may be triggered multiple times. For example, two grasses may merge into a shrub; if that shrub is adjacent to other shrubs, merging continues.
Given and the sequence and levels of the items you receive, arrange them on an initially all-empty village to maximize the final total popularity. When all items have been placed, or at any moment when there are no empty cells, construction ends, and the accumulated popularity at that time is your final score.
Input Format
The first line contains two integers and , separated by a space. is the village size, and is the number of days.
The second line is a string, each character between , indicating the level of the item you can place each day.
Output Format
Output a single integer, the maximum popularity you can obtain.
4 10
1132411235
168
Hint
For of the testdata, , .
For of the testdata, , .
For of the testdata, , .
Translated by ChatGPT 5
京公网安备 11011102002149号