Space Station Promenade
Time limit1sMemory limit1024 MB
A circular station has m windows on given modules that must be visited in order from module 1 and back; minimize total travel subject to equal clockwise and counterclockwise distances.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Prefix sum, Greedy, Implementation
- Solved
- No attempts yet
Problem
Astronaut Gustav works on a space station made of modules joined in a circle, so that module is joined to module , module to module , and so on, with module joined to module . The distance between two neighboring modules is . To create artificial gravity, the station rotates at a constant speed around the center of the circle.
The station has been in space for a long time, and it is time to clean the outside of the windows. The lot has fallen on Gustav to do this. There are windows numbered from to , where window is on module . For some reason, the windows must be cleaned in exactly this order. The only entrance and exit to the station is at module .
To move between modules there is a rocket-powered window elevator that travels along the outside of the station. The elevator can only move between neighboring modules, so it cannot take any shortcuts. Gustav wants to choose a route from module , around to all the windows, and back to module . Unfortunately there are two problems: first, the elevator has limited fuel, so Gustav must choose a route that minimizes the distance he travels. Second, the elevator's movements affect the station's rotation, so it must travel the same distance clockwise as counterclockwise.
Find the smallest distance Gustav can travel so that he starts at module , visits all the windows in the correct order, returns to module , and travels the same distance counterclockwise as clockwise.
Input
The first line contains two integers and : the number of modules and the number of windows ( , ). The second line contains integers, the indices of the modules the windows are on ().
Output
One integer, the smallest distance.