Postman
Time limit1sMemory limit512 MB
Deliver m_i letters to house i at position x_i from the origin, carrying at most k letters per trip, and return to the origin each time; minimize total distance.
Problem
A postman delivers letters to the houses of a one-dimensional world.
The post office holds every letter at the start and sits at coordinate . There are houses that need mail. House sits at coordinate and needs letters. The postman carries at most letters at a time.
The postman starts at the post office, picks up any number of letters up to his carrying capacity, visits some of the houses and drops letters off, then returns to the post office. He repeats this until every letter is delivered, and he ends at the post office. The letters for one house may be carried over several trips.
The postman moves one unit of distance in one unit of time.
Find the minimum time the postman needs to start at the post office, deliver every letter, and return to the post office.
Input
The first line contains two space separated integers and . (, )
Each of the next lines contains two space separated integers and . (, )
Output
Print on a single line the minimum time needed to deliver every letter and return to the post office.