codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

441B.md (1978B)


      1 Source: https://codeforces.com/problemset/problem/441/B
      2 
      3 B. Valera and Fruits
      4 
      5 time limit per test
      6 
      7 1 second
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 stdin
     16 
     17 output
     18 
     19 stdout
     20 
     21 Valera loves his garden, where *n* fruit trees grow.
     22 
     23 This year he will enjoy a great harvest! On the *i*-th tree *b**i* fruit grow, they will ripen on a day number *a**i*. Unfortunately, the fruit on the tree get withered, so they can only be collected on day *a**i* and day *a**i* + 1 (all fruits that are not collected in these two days, become unfit to eat).
     24 
     25 Valera is not very fast, but there are some positive points. Valera is ready to work every day. In one day, Valera can collect no more than *v* fruits. The fruits may be either from the same tree, or from different ones. What is the maximum amount of fruit Valera can collect for all time, if he operates optimally well?
     26 
     27 Input
     28 
     29 The first line contains two space-separated integers *n* and *v* (1 ≤ *n*, *v* ≤ 3000) — the number of fruit trees in the garden and the number of fruits that Valera can collect in a day.
     30 
     31 Next *n* lines contain the description of trees in the garden. The *i*-th line contains two space-separated integers *a**i* and *b**i* (1 ≤ *a**i*, *b**i* ≤ 3000) — the day the fruits ripen on the *i*-th tree and the number of fruits on the *i*-th tree.
     32 
     33 Output
     34 
     35 Print a single integer — the maximum number of fruit that Valera can collect.
     36 
     37 Examples
     38 
     39 Input
     40 
     41 ```
     42 2 3  
     43 1 5  
     44 2 3
     45 ```
     46 
     47 Output
     48 
     49 ```
     50 8
     51 ```
     52 
     53 Input
     54 
     55 ```
     56 5 10  
     57 3 20  
     58 2 20  
     59 1 20  
     60 4 20  
     61 5 20
     62 ```
     63 
     64 Output
     65 
     66 ```
     67 60
     68 ```
     69 
     70 Note
     71 
     72 In the first sample, in order to obtain the optimal answer, you should act as follows.
     73 
     74 * On the first day collect 3 fruits from the 1-st tree.
     75 * On the second day collect 1 fruit from the 2-nd tree and 2 fruits from the 1-st tree.
     76 * On the third day collect the remaining fruits from the 2-nd tree.
     77 
     78 In the second sample, you can only collect 60 fruits, the remaining fruit will simply wither.