The ICPC World Finals in Astana are around the corner, and your team has prepared a list of must-visit places! Your journey starts at your home and spans several key cities and touristic spots across the vast and beautiful landscapes of Kazakhstan before reaching the competition venue in Astana. Along the way, you will pass through several key stops (positions) you must visit, including the bustling city of Almaty, the historical city of Shymkent, and the scenic wonders near Karaganda.
The ICPC, aware that competitors have many places to visit, has created a special teleportation device to help teams complete their touristic duties on time for the competition. You can either travel to the next stop normally or use the special teleportation device. Each time you use a teleport, it is destroyed, and a new teleport point is created at your last stop before teleporting.
To simplify the planning, consider that your home and the first teleport is at point 0 and the touristic spots are arranged in a line. The cost of normal travel is the absolute difference in distance between your current stop and the next stop. The cost of a teleport is the absolute difference in distance between your last teleport position and the next stop.
Your task is to determine the minimum total travel cost to reach the competition venue in Astana from your home using the teleportation device. Note that your teammates put a lot of care and effort into crafting this itinerary and want to visit the touristic spots in order.
The first line contains a positive integer $$$n (1 \leq n \leq 1000)$$$, the number of spots.
The next line contains $$$n$$$ space separated positive integers $$$a_i (1 \leq a_i \leq 10^9)$$$, the coordinates of the touristic spots.
A single number, the minimum total distance traveled to visit all spots in order.
310 4 12
16
71 2 1 1 2 2 1
3
The solution to the first instance proceeds as follows: