Matches
InterviewTime limit1sMemory limit128 MB
Starting from one match, fire spreads to each neighbor no taller than the burning match, and the task asks for the largest group that can burn.
- Level
Medium4 of 10
- Topics
- Dynamic programming, Array
- Solved
- No attempts yet
Problem
There are matches placed in a row, each standing right next to the previous one, all with their heads (the tip that catches fire) pointing up. The -th match has height .
If you set fire to one match, it burns from the head downward and its height shrinks. The current height of a burning match falls from its original height all the way down to .
At the instant a burning match's current height becomes equal to the head height of an adjacent match (the one immediately to its left or right), the fire spreads to that neighbor, which then starts burning as well. A newly ignited match can pass the fire on to its own neighbors in the same way.
You may light exactly one match at the start. Choose it so that as many matches as possible burn, and report the maximum number of matches that can burn.
Input
The first line contains the number of matches (). The second line contains integers () separated by spaces, where is the height of the -th match.
Output
Print a single integer on one line: the maximum number of matches that can burn.