Evacuation Route
Time limit2sMemory limit512 MB
On a line of W cells, each an exit, entrance, or fire door with a seal time, find the maximum number of people who can reach an exit, where each entrance emits one person per time step.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Greedy, Prefix sum
- Solved
- No attempts yet
Problem
Disaster prevention research is active in Japan, and its importance has grown in recent years. Evaluating evacuation routes is one important research topic. This time we evaluate the safety of a straight corridor.
The corridor is divided into units, numbered from one end to the other. Each unit in the corridor has exactly one of an entrance door, an exit door, or a fire door. There can be multiple entrance doors, exit doors, and fire doors in the corridor.
In this problem we assume a fire breaks out at time . As a result, people outside the corridor who are trying to evacuate enter the corridor through entrance doors, and try to escape through exit doors to reach a safer place. Each evacuating person, in every unit of time, either moves to one unit or stays in the current unit. That is, if a person is in unit at time , then at time that person can choose one of the three numbers that lies between and inclusive, and move to the unit with that number. A unit with a fire door becomes completely sealed after a certain time, so evacuating people cannot enter that unit. Also, people who were inside that unit can no longer move from it to another unit.
The goal of this problem is, given the information about each door, to compute the maximum number of people who can reach an exit door when the evacuating people act optimally.
The corridor information is given as integers .
- means unit is an exit door.
- means unit becomes inaccessible from time onward because of a fire door.
- means unit is an entrance door, and exactly one person appears in unit at each of times . A person who appears at time starts moving from time onward.
Multiple people may be in the same unit.
Find the maximum number of people who can reach an exit door.
Input
The input is given in the following format.
Output
Print the maximum number of people on one line.
Constraints
- All input values are integers.