Dice Game
InterviewTime limit1sMemory limit128 MB
Simulate a piece on a board of cells, applying a die roll then the cell's instruction, and report how many rolls reach the goal.
- Level
Easy2 of 10
- Topics
- Simulation, Array
- Solved
- No attempts yet
Problem
Sang-geun is playing a board game by himself. The board consists of cells, where cell 1 is the start and cell is the goal. Each cell has a single instruction written on it, telling how many cells to move the piece forward or backward.
Sang-geun repeats the following until he reaches the goal. First, he rolls a die and moves the piece forward by the number rolled. Then he moves the piece once more by the instruction written on the cell where the piece stopped. However, the instruction on the cell reached by following an instruction is not followed.
Landing exactly on cell , or moving past cell , both count as reaching the goal.
Given the sequence of die rolls and the board instructions, write a program that determines how many die rolls it takes for Sang-geun to reach the goal.
Input
The first line contains and , where is the number of times Sang-geun rolls the die. (, )
Each of the next lines contains one integer between and . The -th integer is the instruction written on cell . If , the piece stays where it is; if is positive, it moves cells forward; if is negative, it moves cells backward.
Each of the next lines contains one integer between and . The -th integer is the number that came up on the -th die roll.
The instructions on cell and cell are always . It is guaranteed that the goal can always be reached within die rolls, and no instruction ever moves the piece to a cell numbered less than .
Output
Print how many die rolls it takes for Sang-geun to reach the goal.