ICPC PACNW 2016 I - Postman

View as PDF

Submit solution

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

Problem type
Allowed languages
C, C++, Java, Python

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.


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

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


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


Sample Input 2

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

Sample Output 2

Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported


There are no comments at the moment.