Description
Valera loves his garden, where n fruit trees grow.
This year he'll enjoy a great harvest! On the i-th tree bi fruit grow, they'll ripen on a day number a i. Unfortunately, the fruit on the tree get withered, so they can-only is collected on day a i and day ai + 1 (all fruits that is not collected in these II days, become Unfit to eat).
Valera is isn't very fast, but there is some positive points. Valera is ready-to-work every day. In one day, the Valera can collect no more thanv fruits. The fruits may is either from the same tree, or from different ones. What's the maximum amount of fruit Valera can collect for all time, if he operates optimally well?
Input
The first line contains the space-separated integers n and v(1≤ n, v ≤30 XX)-the number of fruit trees in the garden and the number of fruits that Valera can collect in a day.
Next n lines contain the description of trees in the garden. The i-th line contains II space-separated integers ai and b C11>i(1≤ ai, bi ≤3000)-the Day the fruits ripen on th E i-th tree and the number of fruits on the i-th tree.
Output
Print a single integer-the maximum number of fruit that Valera can collect.
Sample Input
Input
2 3
1 5
2 3
Output
8
Input
5 10
3 20
2 20
1 20
4 20
5 20
Output
60
Hint
In the first sample, in order to obtain the optimal answer, you should act as follows.
- On the first day collect 3 fruits from the 1-st tree.
- On the second day collect 1 fruit from the 2-nd tree and 2 fruits from the 1-st tree.
- On the third day collect the remaining fruits from the 2-nd tree.
In the second sample, you can only collect fruits, the remaining fruit would simply wither.
Main idea: Picking fruit, each fruit maturity for two days, simple violence, I and time compared, if in this time, there are two situations, one is to pick the fruit is not enough, then take the next day, another is already full, but after each calculation, the value minus has been picked. Originally always see what is wrong--results back to the bedroom at a glance, or have to rest a while to see clearly!
#include <cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespacestd;structedge{intD; intnum;} a[ the];BOOLCMP (Edge I, Edge j) {returnI.D <J.D.;}intMain () {intn,m; while(~SCANF ("%d%d",&n,&m)) { for(inti =1; I <= N; i++) scanf ("%d%d", &a[i].d,&a[i].num); Sort (a+1, a+n+1, CMP); intAns =0; for(inti =1; I <=3100; i++){ intM1 =m; for(intj =1; J <= N; j + +){ if(I-A[J].D >=0&&I-A[J].D <=1){ if(A[j].num <=M1) {ans+=A[j].num; M1-=A[j].num; A[j].num=0; } Else{ans+=M1; A[j].num-=M1; M1=0; Break; } }}} printf ("%d\n", ans); } return 0; }View Code
Valera and Fruits