Valera and Fruits
Description
Valera loves his garden, where n fruit trees grow.
This year he will enjoy a great harvest! On the i-th tree bi fruit grow, they will ripen on a day number ai. Unfortunately, the fruit on the tree get withered, so they can only be collected on day ai and day ai + 1 (all fruits that are not collected in these two days, become unfit to eat).
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 thanv 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?
Input
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.
Next n lines contain the description of trees in the garden. The i-th line contains two space-separated integers ai and bi(1 ≤ ai, bi ≤ 3000) — the day the fruits ripen on the 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
2 3
1 5
2 3
8
5 10
3 20
2 20
1 20
4 20
5 20
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 60 fruits, the remaining fruit will simply wither.
大意:摘水果,每种水果的成熟期为两天,单纯暴力,就把i与时间相比,如果在这个时间内,就有两种情况,一种是摘得水果不够,那就摘下一天的,另一种是已经满了,不过在每次计算后,都要把值减去已经摘了的。原本一直看不出哪儿错了- -结果回寝室一眼看出了,还是得歇一会儿再看清楚!
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; struct edge{ int d; int num; }a[5000]; bool cmp(edge i, edge j){ return i.d < j.d; } int main() { int n,m; while(~scanf("%d%d",&n,&m)){ for(int i = 1; i <= n ; i++) scanf("%d%d", &a[i].d,&a[i].num); sort(a+1, a+n+1,cmp); int ans = 0; for(int i = 1; i <= 3100 ; i++){ int m1 = m; for(int j = 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; }