Tiling Terrace
Time limit2sMemory limit512 MB
Tile a row of soil and rock cells with 1, 2, and 3 cell tiles, using at most K single tiles, to maximize the total ghost score.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Binary search, String
- Solved
- No attempts yet
Problem
Talia has just bought an abandoned house on the outskirts of Jakarta. The house has a long yard that can be represented as a one-dimensional grid of cells. To beautify the house, Talia will build a terrace on the yard by tiling its cells. Each cell contains either soil (.) or rock (#), and there are at most 50 rock cells.
Talia is afraid of ghosts, so she wants to use mystical tiles that repel them. There are three types of tiles:
- Type-1: covers cell and can only be placed on a soil cell (
.). - Type-2: covers cells and can only be placed on two consecutive soil cells (
..). - Type-3: covers cells and can only be placed on consecutive soil-rock-soil cells (
.#.).
A Type-1, Type-2, and Type-3 tile repels , , and ghosts per day, respectively. The following rules apply:
- Tiles may not overlap. Each cell is covered by at most one tile.
- At most Type-1 tiles may be used. Type-2 and Type-3 tiles have no limit.
Talia wants the terrace to repel as many ghosts as possible. She does not have to cover every cell as long as the number of ghosts repelled is maximal. Find that maximum number of ghosts repelled per day.
Input
The first line contains five integers , , , , (, , ): the number of cells, the maximum number of Type-1 tiles, and the number of ghosts repelled per day by a Type-1, Type-2, and Type-3 tile, respectively. The next line contains a string of characters, each either . (soil) or # (rock). There are at most 50 rock cells.
Output
Output one integer: the maximum number of ghosts that can be repelled per day.