ICPC PACNW 2016 I - Postman

View as PDF

Submit solution


Points: 12
Time limit: 0.6s
Memory limit: 256M

Problem type

A postman delivers letters to his neighbors in a one-dimensional world.

The post office, which contains all of the letters to begin with, is located at x=0, and there are n houses to which the postman needs to deliver the letters. House i is located at position x_i, and there are m_i letters that need to be delivered to this location. But the postman can only carry k letters at once.

The postman must start at the post office, pick up some number of letters less than or equal to his carrying capacity, and then travel to some of the houses dropping off letters. He must then return to the post office, repeating this process until all letters are delivered. At the end, he must return to the post office.

The postman can travel one unit of distance in one unit of time.

What is the minimum amount of time it will take the postman to start at the post office, deliver all the letters, and return to the post office?

Input

The first line of input contains two space-separated integers n (1 \le n \le 1000) and k (1 \le k \le 10^7).

Each of the next n lines contains two space-separated integers x_i (|x_i| \le 10^7) and m_i (1 \le m_i \le 10^7).

Output

Print, on a single line, the minimum amount of time it will take to complete the mail delivery route.

Sample Input 1

4 10
-7 5
-2 3
5 7
9 5

Sample Output 1

42

Sample Input 2

7 1
9400000 10000000
9500000 10000000
9600000 10000000
9700000 10000000
9800000 10000000
9900000 10000000
10000000 10000000

Sample Output 2

1358000000000000
Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported

Comments

There are no comments at the moment.