Wasting time
InterviewTime limit1sMemory limit1024 MB
Find the longest time to first reach cell N from cell 1 when each cell's number sets the move length and at most two direction reversals are allowed.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Graph, Simulation
- Solved
- No attempts yet
Statement
Geondeok does not want to go to school, so he wants to arrive as late as possible. The route to school is a row of cells numbered from on the left to on the right. Geondeok starts on cell , and the school is on cell .
At the start, Geondeok faces the direction of the school. His commute works in an unusual way. Every minute, he moves in the direction he is facing by the number written on his current cell. He cannot move off the route.
Geondeok can reverse the direction he is facing at most twice. How many minutes after leaving does he reach the school cell for the first time if he delays that arrival as much as possible? Time spent reversing direction is ignored.
The first line contains the school cell number .
The second line contains the integers written on each cell, separated by spaces.
Input
The first line contains the school cell number .
The second line contains the integers written on each cell, separated by spaces.
Output
Print the number of minutes until Geondeok first reaches the school cell when he delays that arrival as much as possible. If there is no path that reaches the school, print .