luogu#P3080. [USACO13MAR] The Cow Run G/S
[USACO13MAR] The Cow Run G/S
Problem Description
Farmer John has forgotten to repair a hole in the fence on his farm, and his cows () have escaped and gone on a rampage! Each minute a cow is outside the fence, she causes one dollar worth of damage. FJ must visit each cow to install a halter that will calm the cow and stop the damage.
Fortunately, the cows are positioned at distinct locations along a straight line on a road outside the farm. FJ knows the location of each cow (, ) relative to the gate (position ) where FJ starts.
FJ moves at one unit of distance per minute and can install a halter instantly. Please determine the order that FJ should visit the cows so he can minimize the total cost of the damage; you should compute the minimum total damage cost in this case.
Input Format
- Line : The number of cows, .
- Lines : Line contains the integer .
Output Format
- Line : The minimum total cost of the damage.
4
-2
-12
3
7
50
Hint
Four cows placed in positions: , , , and .
The optimal visit order is , , , . FJ arrives at position in minutes for a total of dollars in damage for that cow.
He then travels to position (distance: ) where the cumulative damage is dollars for that cow.
He spends more minutes to get to at a cost of dollars for that cow.
Finally, he spends minutes to go to with a cost of dollars.
The total damage is dollars.