AtCoder Study
Time limit1sMemory limit512 MB
Seunghyeong starts at building N in a row of 2N-1 buildings and must collect M scattered Gunsu using steps of 1 or N, with free returns to visited buildings. The task asks for the minimum time.
- Level
Medium7 of 10
- Topics
- Graph, Shortest path, Math
- Solved
- No attempts yet
Problem
Seogang Village is a straight village with a total of buildings in a row, numbered from 1 to from left to right. of Gunsu live in this village, along with Seunghyeong. All of the Gunsu are in different buildings, and Seunghyeong is in building , where K512 is located. The Gunsu and Seunghyeong hold an AtCoder study every Monday. Five minutes before the study starts, not a single Gunsu has arrived! Angry, Seunghyeong plans to use a secret special movement method to bring back the Gunsu scattered around the village to K512.
Seunghyeong can use the following three movement methods.
- He can move to a neighboring building in 1 second. That is, he can move from building to building or building . If he is in building 1 or building , he can only move to building 2 or building , respectively.
- He can skip exactly buildings in 1 second. That is, for , he can move from building to building , and for , he can move from building to building .
- He can instantly move to any building he has visited before, without spending any time. However, buildings skipped while using method 2 do not count as visited.
When Seunghyeong reaches a building with a Gunsu, he picks up the Gunsu right away, so collecting them takes no extra time. Let us calculate the minimum time needed to bring back all the Gunsu.
Input
The first line contains the integers and . (; )
The second line contains the positions through of the Gunsu, separated by spaces, where is the position of the -th Gunsu. All positions are distinct, and never happens. ()
Output
Print the minimum time Seunghyeong needs to bring back all the Gunsu.