Border Robot
Time limit1sMemory limit512 MB
Given N sensors on the barrier [0, L] with identification range r, find the minimum robot travel distance needed to move sensors so their intervals cover all of [0, L].
Problem
A high barrier guards a secret laboratory, and N sensors are placed in front of it to identify intruders. The barrier is a straight line, represented by the interval . Each sensor is a point inside this interval.
All sensors have the same identification range . A sensor at point can identify intruders in the interval . This interval is called the identification interval of the sensor.
One robot is responsible for the boundary of the barrier. It starts at the left end of the barrier. The robot can move left and right along the barrier and carry sensors. It can pick up or drop a sensor only at its own position. The robot can carry any number of sensors at once.
If the union of all identification intervals fully covers the barrier , the sensors form a perfect boundary. The robot must move sensors when needed to build a perfect boundary. Minimize the total distance the robot travels.
Given the barrier , the initial positions of N sensors, and the identification range , write a program that prints the minimum total distance the robot must travel to build a perfect boundary.
Input
The first line contains three integers N, L, and r, separated by spaces. They are the number of sensors, the length of the barrier, and the identification range of the sensors.
The second line contains the N initial positions of the sensors, separated by spaces. The positions are given in non-decreasing order, so the list is already sorted.
Output
If no arrangement of the sensors can form a perfect boundary, print -1 on the first line.
Otherwise, print on the first line the minimum total distance the robot must travel to form a perfect boundary. This value is always an integer.
Constraints
- N is an integer with 1 to 1,000,000.
- L is an integer with 1 to .
- r is an integer with 1 to .
- L and r are both integers.
- The initial position of every sensor is an integer from 0 to L, inclusive.
- The initial positions of all sensors are given in non-decreasing order.
Hint
In the first example, the robot moves as follows.
- The robot at 0 moves to 3.
- It picks up the sensor at its current position.
- The robot at 3 moves to 2.
- It drops the carried sensor at its current position.
The final sensor positions are 2 and 6. The union of identification intervals is , which fully covers the barrier . The robot travels a total distance of 4. No smaller distance can form a perfect boundary, so the answer is 4.
In the second example, the robot moves as follows.
- It picks up both sensors at position 0.
- The robot moves from 0 to 1.5.
- It drops one of the carried sensors at its current position.
- The robot moves from 1.5 to 7.
- It drops the remaining sensor at its current position.
Note that the robot can drop sensors at positions that are not integers.