Icicles
Time limit1sMemory limit128 MB
Icicles grow each hour when strictly longer than both neighbors and snap at length L; find the hour when all have broken.
- Level
Hard8 of 10
- Topics
- Simulation, Implementation, Array, Math
- Solved
- No attempts yet
Problem
Under the eaves of JOI's house in Canada, a fine row of icicles has formed. Curious about them, JOI decides to take a closer look.
There are icicles () hanging in a straight line under the eaves. The -th icicle () hangs at the position cm from the left end, and its initial length is cm (where is a positive integer). The icicles grow according to the following rules.
- The -th icicle grows by cm each hour if and only if it is strictly longer than both the -th and the -th icicles. For the two icicles at the ends, only the single existing neighbor is considered: the st icicle grows while it is longer than the nd, and the -th icicle grows while it is longer than the -th.
- The moment an icicle reaches a length of cm (), it snaps off at the base. A broken icicle is thereafter treated as an icicle of length cm.
Initially, every two adjacent icicles have different lengths. Under this condition, after enough time all icicles will have snapped off and become cm long. Compute the number of hours it takes until every icicle has broken.
Input
The first line contains two integers and , the number of icicles and the breaking length, separated by a space. Each of the next lines contains one integer (), the initial length of the -th icicle.
Output
Print a single integer: the number of hours until all icicles have broken.
Hint
Consider icicles with initial lengths and breaking length . The st, nd, rd, and th icicles break after , , , and hours respectively. Hence all icicles have broken after hours, so the answer is .