A robot on a line must visit every point; each point's repair cost grows linearly with the time it waits, so find the visiting order of minimum total cost.
Hard8Dynamic programmingIntervalsGreedySortingNo attempts yetTime limit1sMemory limit1024 MBMirek repairs monuments for a living. Today he has to fix the defense wall of an old fortress. The wall is about to fall to pieces, so he must hurry. He tracked down a robot built to repair such walls very fast and bought it, then got stuck working out the cheapest repair plan.
Treat the wall as a straight line. Mirek wrote down the coordinate of every point that needs repairing. Repairing point i right away costs Ci, and a coefficient Di says how fast that cost grows while the point waits. If point i is repaired after time t has passed, repairing it costs
Ci+t⋅Di
Moving the robot from coordinate x1 to coordinate x2 takes ∣x1−x2∣ time, and repairing one point takes no time. Time is measured from the moment the robot leaves its starting position. Find the minimum cost of repairing every damaged point.
The first line contains two integers N and P, the number of points that need repairing and the starting position of the robot. (1≤N≤2000, 0≤P≤109)
Each of the next N lines describes one point with three integers Xi, Ci, Di, which are the coordinate of the point and its two cost coefficients. (0≤Xi≤109, 0≤Ci,Di≤106, Xi=P)
No two points have the same coordinate.
Print on one line the integer C, the minimum cost of repairing all points on the wall. The value can exceed the range of a 32-bit integer.
The optimal plan for the first example is:
The total cost is (32+3⋅1)+(5+18⋅1)+(0+7⋅2)=72.