Building Blocks
Time limit1sMemory limit128 MB
Remove blocks from a tower so the most remaining blocks sit at an altitude equal to their printed number.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Greedy, Sorting
- Solved
- No attempts yet
Problem
Little Jessica received a set of building blocks for her birthday. The blocks are all cubes of the same size, and each block has a positive integer written on it. Delighted with the gift, she immediately stacked every block into one tall tower.
Her mother explained the game: reshape the tower so that as many blocks as possible sit in their proper place. A block with the number written on it is in its proper place when it rests at altitude . The bottom block is at altitude , the block above it at altitude , and so on.
Jessica may carefully remove some of the blocks. Whenever a block is removed, every block above it slides down by one, and the altitudes are renumbered from the bottom. She wants to remove blocks so that the number of blocks left in their proper place is as large as possible.
Given the tower Jessica first built, determine the largest number of blocks that can end up in their proper place.
Input
The first line contains a single integer (), the initial height of the tower. The second line contains positive integers (), separated by single spaces, where is the number written on the block at altitude in the initial tower, listed from the lowest block to the highest.
Output
Print a single integer: the maximum number of blocks that can be in their proper place after Jessica removes some blocks.
Hint

