Harnessing your entrepreneurial spirit, you have decided to found a start-up. Your start-up, the first of its kind, will sell ice cream at the local beach. Every day, the beach gets visitors which sit at specific locations on the beach. The visitors would all love to buy ice cream, but they don't like walking around in the sun. A visitor's reluctance to buy ice cream is calculated by multiplying their distance from the nearest ice cream stand by their reluctance factor: a number unique to each visitor.
You have access to ice cream stands, which you can place anywhere on the beach. Moreover, you know where every visitor will sit and you know their reluctance factor. Since you don't want your start-up to sink, you'd like to carefully position your stands to minimize the total reluctance of all the visitors.
Can you figure out the minimum total reluctance that you can achieve?
The input contains test cases.
Each test case starts with two integers , where is the number of visitors and is the number of ice cream stands. The next lines describe the visitors coming to the beach. Each line contains two integers and , where represents the locations of the visitor on the beach and represents the reluctance factor of that visitor .
Visitors will be listed in ascending order of and no two visitors will be at the same location. Since the beach is long and thin, it can be thought of as being one dimensional.
For of the cases, .
For of the cases, .
For each test case, your program should output the minimum total reluctance of all the visitors, modulo , or . *
2 1 10 10 20 10 2 2 10 10 20 10 4 2 1 10000 100 10 150 10 200 10
100 0 1000
Note: Only cases are shown in this sample.
Explanation of Sample Output
In the first test case, it's best to put your only ice cream stand anywhere between positions and . If we were to put it at position , then each visitor would have a reluctance of , for a total reluctance of .
In the second test case, you can perfectly accommodate both guests by putting your two carts at locations and .
In the last test case, it's best to put one cart at location to appease the incredibly reluctant visitor and put your second cart at location .
* This means that if the minimum total reluctance is you should output , the remainder after dividing by .
Educational Computing Organization of Ontario - statements, test data and other materials can be found at ecoocs.org