Tipover Transform
Time limit1sMemory limit1024 MB
Given a row of cells with standing blocks of various heights, find the fewest 1 cm cube blocks needed so the hero can walk from cell 0 to cell N.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Greedy, Array
- Solved
- No attempts yet
Problem
Tipover is a puzzle where blocks of different heights are placed on a board and then knocked over so the hero can walk from the start to the goal. One day Sihun got bored while solving this puzzle and came up with the following D Tipover game.
- The board is a row of cells. Each cell is a by square, numbered from to from left to right.
- Each cell can hold at most one block. All blocks start standing upright. A standing block is a cuboid with a width and depth and a height of at least . Each block height is an integer multiple of .
- A standing block can be knocked over to the left or to the right. If a block of height is in cell , knocking it to the left covers cells through , and knocking it to the right covers cells through . A block can be knocked over only if none of the cells it covers contain another block and all of the cells it covers are on the board. A knocked-over block cannot move again.
- Cell holds a block of height . The hero starts on cell and clears the game when reaching cell . The hero can move only to adjacent cells, and only to cells that hold a block, regardless of the block height.
However, Sihun realized that some layouts cannot be cleared. So he added the following rules.
- Before the game starts, the hero can knock over some blocks in advance. There is no constraint on the order in which the blocks are knocked over.
- If no block is in a cell adjacent to the hero's cell, the hero can add a cube block of height to that adjacent cell. A cube block cannot be knocked over.
Sihun wondered how few cube blocks are needed for the game on his board to be cleared. Solve this problem.
Input
The first line contains . () The second line contains integers , separated by spaces. Each is either or an integer from to . If , cell has no block at the start. If , cell has a block of height at the start.
Output
Print the minimum number of cube blocks the hero must add to clear the game, when some blocks have been knocked over in advance.