Online Shopping
Ray recently moved to a new city, and he's looking for some new furniture on Taobao to fill out his apartment. Ray is a thrifty shopper, so he found many items that are on sale, and he wants to maximise the amount of money saved. Ironically, Ray has virtually unlimited money to spend (as he works at the prestigious Sphinx Trading), but he can only fit a certain amount of items in his space.
Given a list of items, their original and discounted prices, and the max amount of items Ray can buy, what is the maximum amount of money he can save (i.e. the difference between the total listed price of all the items he buys, and the discounted price)? He can only buy one of each item.
Input
The first line contains an integer , the number of items Ray found on sale on Taobao, and an integer
, the maximum amount of items Ray can fit in his apartment.
The next lines each contain
integers separated by a space. The first integer is
, the original price of the item. The second integer is
, the discounted price.
Output
What is the maximum amount Ray can save by buying sale items, while not buying more than items? The amount saved is equal to the original price minus the discounted price.
Example
Input 1
5 3
10 2
300 289
89 78
50 49
100 80
Output 2
42
- Buying the item on sale for
from
saves
.
- The item on sale from
to
also saves
.
- Finally, the item on sale for
from
saves
.
As such,
Input 2
4 2
100 95
8 2
77 67
38 30
Output 2
18
We buy the final two items.
Comments