Under construction

Still being built. Things may change

Postal Delivery

Time limit2sMemory limit512 MB

Summary
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 00 of a number line, and each delivery location is a single integer coordinate. The truck carries at most KK 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 100100 letters, and 5050 letters go to coordinate −10-10, 175175 letters go to coordinate 1010, and 2020 letters go to coordinate 2525. A maximally efficient plan is this. Deliver the 5050 letters to coordinate −10-10 (2×10=202 \times 10 = 20). Deliver the first 100100 letters to coordinate 1010 (2×10=202 \times 10 = 20). Load the remaining 7575 letters for coordinate 1010 together with the 2020 letters for coordinate 2525, drop the 7575 off on the way, and continue out to coordinate 2525 (2×25=502 \times 25 = 50). The total distance traveled is 9090.

Find the minimum total travel distance needed to deliver every letter and return to the post office.

Input

The first line contains two integers NN and KK. Here NN is the number of delivery locations with 3≤N≤10003 \le N \le 1000, and KK is the carrying capacity of the truck with 1≤K≤100001 \le K \le 10000.

Each of the following NN lines contains two integers xjx_j and tjt_j, the coordinate of a delivery location and the number of letters to deliver there, where −1500≤x1<x2<⋯<xN≤1500-1500 \le x_1 < x_2 < \cdots < x_N \le 1500 and 1≤tj≤8001 \le t_j \le 800 for every jj. No delivery coordinate is 00, 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.

Examples2

  1. Example 1

    Input
    3 100
    -10 50
    10 175
    25 20
    
    Expected output
    90
  2. Example 2

    Input
    5 3
    -1002 800
    -1001 800
    -1000 800
    -999 800
    -998 800
    
    Expected output
    2668000