Postal Delivery
Time limit2sMemory limit512 MB
Given delivery counts at coordinates on a line and a truck capacity K, find the minimum total distance to deliver all letters and return to the origin.
- Level
Medium5 of 10
- Topics
- Greedy, Sorting, Implementation
- Solved
- No attempts yet
Problem
The postal service wants to cut costs instead of raising postage rates. One way is to minimize the distance a truck travels while carrying mail from the post office to every required location and returning to the post office. All the mail may not fit on the truck at once, so the distance traveled also includes the trips back to the post office to reload.
To keep the problem simple, assume a one dimensional world. The post office sits at the origin of a number line, and each delivery location is a single integer coordinate. The truck carries at most letters at a time, and every run starts and ends at the post office. A single run may visit several locations, and the truck may drop off only part of its load at a location it passes. The distance traveled is the total distance the truck moves along the number line.
Suppose the truck holds letters, and letters go to coordinate , letters go to coordinate , and letters go to coordinate . A maximally efficient plan is this. Deliver the letters to coordinate (). Deliver the first letters to coordinate (). Load the remaining letters for coordinate together with the letters for coordinate , drop the off on the way, and continue out to coordinate (). The total distance traveled is .
Find the minimum total travel distance needed to deliver every letter and return to the post office.
Input
The first line contains two integers and . Here is the number of delivery locations with , and is the carrying capacity of the truck with .
Each of the following lines contains two integers and , the coordinate of a delivery location and the number of letters to deliver there, where and for every . No delivery coordinate is , so no location sits at the post office.
Output
Print, on one line, the minimum total travel distance needed to deliver every letter and return to the post office.