to Pay Respects
Time limit1sMemory limit1024 MB
Given a boss fight with regeneration and poison spells, find the maximum total damage when at most K poison casts are allowed.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Binary search, Greedy
- Solved
- No attempts yet
Statement
You are playing a game, and you are going to fight the secret boss. In this game, the boss does not attack you, but they can cast regeneration spells.
The fight consists of exactly rounds. In each round, the following actions can happen, in this order:
- The boss can choose to cast the "Regeneration" spell.
- You can choose to cast the "Poison" spell if you have any mana left.
- You attack with a sword, dealing damage.
- All the passive effects are applied.
There are two types of passive effects: regeneration and poison. The effects stack, so the state of the boss is described by three integers: current health , current poison stacks , and current regeneration stacks . At the start of the fight, there are no poison stacks and no regeneration stacks (). Each poison stack deals damage, and each regeneration stack heals health points.
The spells have the following effects:
"Regeneration": increases the number of regeneration stacks by .
"Poison": increases the number of poison stacks by . If the number of regeneration stacks is strictly positive (), it decreases by .
After each round, decreases by . This value can be negative if the boss heals faster than you deal damage.
For each round, you know whether the boss casts "Regeneration". You have enough mana to cast "Poison" times, and you do not have to use all of it. What is the largest total damage you can deal to the boss? In other words, find the maximum value of . Assume , so you cannot actually kill the boss in rounds. The boss health can rise above the initial value (see the third sample).
Input
The first line contains five integers , , , , (, ).
The second line contains a binary string of length . The -th character is 1 if the boss casts "Regeneration" at the beginning of the -th round, and 0 otherwise.
Output
Output one integer: the largest total damage you can deal during the fight.
Hint
Consider the first sample. You can cast "Poison" at most once. If you cast it in the first round, the boss loses health in that round, then in the second round, for a total of 2021. If you cast it in the second round, the boss loses 1010 in the first round and in the second round, which is also 2021. So the answer is 2021 either way.